Re: entries in PT_DYNAMIC section refer to the correct address at runtime?

2017-09-15 Thread Alan Modra
On Fri, Sep 15, 2017 at 02:57:36PM +0800, Yubin Ruan wrote:
> 2017-09-14 18:25 GMT+08:00 Alan Modra :
> > DT_ entries that specify addresses will need to be offset by the base
> > address.
> 
> Will base address be automatically adjusted by the dynamic linker if
> it tries to loaded it at a different base address than that specified
> in the `p_vaddr'?

That might depend on the dynamic linker.  glibc ld.so doesn't as far
as I know.  Build yourself a testcase and inspect the .dynamic section
of a shared library or PIE under gdb.

-- 
Alan Modra
Australia Development Lab, IBM


[COMMITTED] Internationalized messages should not contain the '\v' escape sequence.

2017-09-15 Thread Mark Wielaard
Replace horizontal tab '\v' with double line feed '\n\n' in doc strings.
Regenerate .po files.

Signed-off-by: Mark Wielaard 
---
 po/ChangeLog  |   4 +
 po/de.po  | 754 
 po/es.po  | 760 
 po/fr.po  |   2 +-
 po/it.po  |   2 +-
 po/ja.po  | 754 
 po/nl.po  |   2 +-
 po/pl.po  | 780 +-
 po/ru.po  |   2 +-
 po/uk.po  | 779 -
 po/zh_CN.po   |   2 +-
 src/ChangeLog |   5 +
 src/stack.c   |   3 +-
 src/unstrip.c |   3 +-
 14 files changed, 1942 insertions(+), 1910 deletions(-)

diff --git a/po/ChangeLog b/po/ChangeLog
index 465ae16..2c27e64 100644
--- a/po/ChangeLog
+++ b/po/ChangeLog
@@ -1,3 +1,7 @@
+2017-09-01  Mark Wielaard  
+
+   * *.po: Regenerated. Replace \v with \n\n.
+
 2017-08-02  Mark Wielaard  
 
* *.po: Update for 0.170.
diff --git a/src/ChangeLog b/src/ChangeLog
index daedfca..8370b69 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2017-09-01  Mark Wielaard  
+
+   * stack.c (main): Replace \v in doc string with \n\n.
+   * unstrip.c (main): Likewise.
+
 2017-05-04  Ulf Hermann  
 
* stack.c: Print pid_t using %lld.
diff --git a/src/stack.c b/src/stack.c
index 7c180b7..52ae3a8 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -678,7 +678,8 @@ main (int argc, char **argv)
 {
   .options = options,
   .parser = parse_opt,
-  .doc = N_("Print a stack for each thread in a process or core file.\v\
+  .doc = N_("Print a stack for each thread in a process or core file.\n\
+\n\
 Program exits with return code 0 if all frames were shown without \
 any errors.  If some frames were shown, but there were some non-fatal \
 errors, possibly causing an incomplete backtrace, the program exits \
diff --git a/src/unstrip.c b/src/unstrip.c
index 5074909..f368e69 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -2383,7 +2383,8 @@ main (int argc, char **argv)
   .children = argp_children,
   .args_doc = N_("STRIPPED-FILE DEBUG-FILE\n[MODULE...]"),
   .doc = N_("\
-Combine stripped files with separate symbols and debug information.\v\
+Combine stripped files with separate symbols and debug information.\n\
+\n\
 The first form puts the result in DEBUG-FILE if -o was not given.\n\
 \n\
 MODULE arguments give file name patterns matching modules to process.\n\
-- 
1.8.3.1



[PATCH] ar: Check whether ar header values fit.

2017-09-15 Thread Mark Wielaard
When compiling with -O3 gcc finds an interesting error:

src/ar.c: In function ‘do_oper_insert’:
src/ar.c:1077:56: error: ‘%-*ld’ directive output may be truncated writing 
between 6 and 10 bytes into a region of size 7 [-Werror=format-truncation=]
   snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld", bufsize, val);
^
The problem is that the ar header values have to fit in a limited
(not zero terminated) string. We should check the snprintf return
value to see if the values are representable.

Also make ar valgrind and ubsan clean and add a minimal sanity test.

Reported-by: Matthias Klose 
Signed-off-by: Mark Wielaard 
---
 src/ChangeLog |  9 
 src/ar.c  | 66 ++-
 tests/ChangeLog   |  6 +
 tests/Makefile.am |  4 ++--
 tests/run-ar.sh   | 40 +
 5 files changed, 103 insertions(+), 22 deletions(-)
 create mode 100755 tests/run-ar.sh

diff --git a/src/ChangeLog b/src/ChangeLog
index daedfca..3c34026 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,12 @@
+2017-09-10  Mark Wielaard  
+
+   * ar.c (do_oper_delete): Remove DEBUG conditional check.
+   (no0print): Return bool. Check snprintf return value.
+   (do_oper_insert): Initialize elf. Remove DEBUG conditional check.
+   Check no0print calls succeed. Explicitly elf_end found elfs.
+   (do_oper_extract): Make sure we don't create an empty variable
+   length array.
+
 2017-05-04  Ulf Hermann  
 
* stack.c: Print pid_t using %lld.
diff --git a/src/ar.c b/src/ar.c
index ec32cee..818115b 100644
--- a/src/ar.c
+++ b/src/ar.c
@@ -1,5 +1,5 @@
 /* Create, modify, and extract from archives.
-   Copyright (C) 2005-2012, 2016 Red Hat, Inc.
+   Copyright (C) 2005-2012, 2016, 2017 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper , 2005.
 
@@ -442,7 +442,7 @@ static int
 do_oper_extract (int oper, const char *arfname, char **argv, int argc,
 long int instance)
 {
-  bool found[argc];
+  bool found[argc > 0 ? argc : 1];
   memset (found, '\0', sizeof (found));
 
   size_t name_max = 0;
@@ -1056,13 +1056,11 @@ do_oper_delete (const char *arfname, char **argv, int 
argc,
 goto nonew_unlink;
 
  errout:
-#ifdef DEBUG
   elf_end (elf);
 
   arlib_fini ();
 
   close (fd);
-#endif
 
   not_found (argc, argv, found);
 
@@ -1070,12 +1068,18 @@ do_oper_delete (const char *arfname, char **argv, int 
argc,
 }
 
 
-static void
+/* Prints the given value in the given buffer without a trailing zero char.
+   Returns false if the given value doesn't fit in the given buffer.  */
+static bool
 no0print (bool ofmt, char *buf, int bufsize, long int val)
 {
   char tmpbuf[bufsize + 1];
-  snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld", bufsize, val);
+  int ret = snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld",
+ bufsize, val);
+  if (ret >= (int) sizeof (tmpbuf))
+return false;
   memcpy (buf, tmpbuf, bufsize);
+  return true;
 }
 
 
@@ -1084,7 +1088,7 @@ do_oper_insert (int oper, const char *arfname, char 
**argv, int argc,
const char *member)
 {
   int status = 0;
-  Elf *elf;
+  Elf *elf = NULL;
   struct stat st;
   int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, oper != oper_move);
 
@@ -1303,13 +1307,11 @@ do_oper_insert (int oper, const char *arfname, char 
**argv, int argc,
 
   if (status != 0)
 {
-#ifdef DEBUG
   elf_end (elf);
 
   arlib_fini ();
 
   close (fd);
-#endif
 
   return status;
 }
@@ -1463,14 +1465,36 @@ do_oper_insert (int oper, const char *arfname, char 
**argv, int argc,
  memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
}
 
- no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
-   all->sec);
- no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid), all->uid);
- no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid), all->gid);
- no0print (true, arhdr.ar_mode, sizeof (arhdr.ar_mode),
-   all->mode);
- no0print (false, arhdr.ar_size, sizeof (arhdr.ar_size),
-   all->size);
+ if (! no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
+ all->sec))
+   {
+ error (0, errno, gettext ("cannot represent ar_date"));
+ goto nonew_unlink;
+   }
+ if (! no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid),
+ all->uid))
+   {
+ error (0, errno, gettext ("cannot represent ar_uid"));
+ goto nonew_unlink;
+   }
+ if (! no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid),
+ all->gid))
+   {
+ error (0, err