[PATCH 1/4] strip: Always copy over any phdrs if there are any.

2018-10-26 Thread Mark Wielaard
Ignore the type of ELF file, just copy over any phdrs if the original
file contained any. Also refuse to move around any allocated sections
based on whether there are any phdrs instead of on ELF file type.

Signed-off-by: Mark Wielaard 
---
 src/ChangeLog |  6 +
 src/strip.c   | 70 +++
 2 files changed, 48 insertions(+), 28 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 602312e..5a57e8f 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2018-10-24  Mark Wielaard  
+
+   * strip.c (handle_elf): Always copy over phdrs if there are any
+   and check phnum instead of e_type to determine whether to move
+   allocated sections.
+
 2018-10-16  Mark Wielaard  
 
* readelf.c (print_debug_frame_section): Make sure readp is never
diff --git a/src/strip.c b/src/strip.c
index fdebc5e..cb479de 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -588,49 +588,63 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
   else
 newelf = elf_clone (elf, ELF_C_EMPTY);
 
-  if (unlikely (gelf_newehdr (newelf, gelf_getclass (elf)) == 0)
-  || (ehdr->e_type != ET_REL
- && unlikely (gelf_newphdr (newelf, phnum) == 0)))
+  if (unlikely (gelf_newehdr (newelf, gelf_getclass (elf)) == 0))
 {
-  error (0, 0, gettext ("cannot create new file '%s': %s"),
+  error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"),
 output_fname ?: fname, elf_errmsg (-1));
   goto fail;
 }
 
   /* Copy over the old program header if needed.  */
-  if (ehdr->e_type != ET_REL)
-for (cnt = 0; cnt < phnum; ++cnt)
-  {
-   GElf_Phdr phdr_mem;
-   GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
-   if (phdr == NULL
-   || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0))
- INTERNAL_ERROR (fname);
-  }
+  if (phnum > 0)
+{
+  if (unlikely (gelf_newphdr (newelf, phnum) == 0))
+   {
+ error (0, 0, gettext ("cannot create new phdr for file '%s': %s"),
+output_fname ?: fname, elf_errmsg (-1));
+ goto fail;
+   }
+
+  for (cnt = 0; cnt < phnum; ++cnt)
+   {
+ GElf_Phdr phdr_mem;
+ GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
+ if (phdr == NULL
+ || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0))
+   INTERNAL_ERROR (fname);
+   }
+}
 
   if (debug_fname != NULL)
 {
   /* Also create an ELF descriptor for the debug file */
   debugelf = elf_begin (debug_fd, ELF_C_WRITE_MMAP, NULL);
-  if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0)
- || (ehdr->e_type != ET_REL
- && unlikely (gelf_newphdr (debugelf, phnum) == 0)))
+  if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
{
- error (0, 0, gettext ("cannot create new file '%s': %s"),
+ error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"),
 debug_fname, elf_errmsg (-1));
  goto fail_close;
}
 
   /* Copy over the old program header if needed.  */
-  if (ehdr->e_type != ET_REL)
-   for (cnt = 0; cnt < phnum; ++cnt)
- {
-   GElf_Phdr phdr_mem;
-   GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
-   if (phdr == NULL
-   || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0))
- INTERNAL_ERROR (fname);
- }
+  if (phnum > 0)
+   {
+ if (unlikely (gelf_newphdr (debugelf, phnum) == 0))
+   {
+ error (0, 0, gettext ("cannot create new phdr for file '%s': %s"),
+debug_fname, elf_errmsg (-1));
+ goto fail_close;
+   }
+
+ for (cnt = 0; cnt < phnum; ++cnt)
+   {
+ GElf_Phdr phdr_mem;
+ GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
+ if (phdr == NULL
+ || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0))
+   INTERNAL_ERROR (fname);
+   }
+   }
 }
 
   /* Number of sections.  */
@@ -738,7 +752,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
 to keep the layout of all allocated sections as similar as
 possible to the original file.  In relocatable object files
 everything can be moved.  */
-  if (ehdr->e_type == ET_REL
+  if (phnum == 0
  || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
shdr_info[cnt].shdr.sh_offset = 0;
 
@@ -2328,7 +2342,7 @@ while computing checksum for debug information"));
   /* The ELF library better follows our layout when this is not a
  relocatable object file.  */
   elf_flagelf (newelf, ELF_C_SET,
-  (ehdr->e_type != ET_REL ? ELF_F_LAYOUT : 0)
+  (phnum > 0 ? ELF_F_LAYOUT : 0)
   | (permissive ? ELF_F_PERMISSIVE : 0));
 
   /* Finally write the file.

Add eu-strip --reloc-debug-sections-only option

2018-10-26 Thread Mark Wielaard
Hi,

eu-strip already supports --reloc-debug-sections to resolve all
relocations between .debug_ sections while creating a separate
.debug file with -f. It is sometimes useful to do the same without
doing any other stripping.

  --reloc-debug-sections-only
 Similar to --reloc-debug-sections, but resolve all
 trivial relocations between debug sections in
 place.  No other stripping is performed (operation
 is not reversable, incompatible with -f, -g,
 --remove-comment and --remove-section)

For example to ease debugging, tracing and profiling some distributions
ship an uncompressed vmlinux ELF file which includes DWARF debuginfo.
Since the vmlinux ELF kernel is relocatable, the relocations between the
.debug sections are also still left in. On Fedora this file is 662M.
eu-strip --reloc-debug-sections-only will remove more than 250MB of
(unneeded) relocations between the .debug sections.

To reuse as much code as possible this patch series first refactors
some of the code. It also makes it so that the type of ELF file doesn't
matter for doing relocations (normally it only makes sense for ET_REL
files, but the vmlinux ELF image is an ET_EXEC with SHT_RELA sections).

It also adds a new testcase to run-strip-reloc.sh to make sure that the
result of creating a separate debug file using --reloc-debug-sections
is identical to creating a separate debug file and then running eu-strip
with --reloc-debug-sections-only.

[PATCH 1/4] strip: Always copy over any phdrs if there are any
[PATCH 2/4] strip: Split out debug section relocation into separate helper
[PATCH 3/4] strip: Extract code to update shdrstrndx into new common function
[PATCH 4/4] strip: Add --reloc-debug-sections-only option

 Cheers,

 Mark


[PATCH 2/4] strip: Split out debug section relocation into separate helper functions.

2018-10-26 Thread Mark Wielaard
Extract a couple of helper functions out of handle_elf (secndx_name,
get_xndxdata and remove_debug_relocations) so they can be reused more
easily in the future.

Signed-off-by: Mark Wielaard 
---
 src/ChangeLog |   7 +
 src/strip.c   | 541 +++---
 2 files changed, 299 insertions(+), 249 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 5a57e8f..f55ff6c 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,12 @@
 2018-10-24  Mark Wielaard  
 
+   * strip.c (handle_elf): Extract code into separate functions...
+   (secndx_name): ... new function.
+   (get_xndxdata): Likewise.
+   (remove_debug_relocations): Likewise.
+
+2018-10-24  Mark Wielaard  
+
* strip.c (handle_elf): Always copy over phdrs if there are any
and check phnum instead of e_type to determine whether to move
allocated sections.
diff --git a/src/strip.c b/src/strip.c
index cb479de..a7b69a6 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -354,6 +354,297 @@ parse_opt (int key, char *arg, struct argp_state *state)
   return 0;
 }
 
+static const char *
+secndx_name (Elf *elf, size_t ndx)
+{
+  size_t shstrndx;
+  GElf_Shdr mem;
+  Elf_Scn *sec = elf_getscn (elf, ndx);
+  GElf_Shdr *shdr = gelf_getshdr (sec, &mem);
+  if (shdr == NULL || elf_getshdrstrndx (elf, &shstrndx) < 0)
+return "???";
+  return elf_strptr (elf, shstrndx, shdr->sh_name) ?: "???";
+}
+
+/* Get the extended section index table data for a symbol table section.  */
+static Elf_Data *
+get_xndxdata (Elf *elf, Elf_Scn *symscn)
+{
+  Elf_Data *xndxdata = NULL;
+  GElf_Shdr shdr_mem;
+  GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
+  if (shdr != NULL && shdr->sh_type == SHT_SYMTAB)
+{
+  size_t scnndx = elf_ndxscn (symscn);
+  Elf_Scn *xndxscn = NULL;
+  while ((xndxscn = elf_nextscn (elf, xndxscn)) != NULL)
+   {
+ GElf_Shdr xndxshdr_mem;
+ GElf_Shdr *xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem);
+
+ if (xndxshdr != NULL
+ && xndxshdr->sh_type == SHT_SYMTAB_SHNDX
+ && xndxshdr->sh_link == scnndx)
+   {
+ xndxdata = elf_getdata (xndxscn, NULL);
+ break;
+   }
+   }
+}
+
+  return xndxdata;
+}
+
+/* Remove any relocations between debug sections in ET_REL
+   for the debug file when requested.  These relocations are always
+   zero based between the unallocated sections.  */
+static void
+remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr,
+ const char *fname, size_t shstrndx)
+{
+  Elf_Scn *scn = NULL;
+  while ((scn = elf_nextscn (elf, scn)) != NULL)
+{
+  /* We need the actual section and header from the elf
+not just the cached original in shdr_info because we
+might want to change the size.  */
+  GElf_Shdr shdr_mem;
+  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
+  if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
+   {
+ /* Make sure that this relocation section points to a
+section to relocate with contents, that isn't
+allocated and that is a debug section.  */
+ Elf_Scn *tscn = elf_getscn (elf, shdr->sh_info);
+ GElf_Shdr tshdr_mem;
+ GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem);
+ if (tshdr->sh_type == SHT_NOBITS
+ || tshdr->sh_size == 0
+ || (tshdr->sh_flags & SHF_ALLOC) != 0)
+   continue;
+
+ const char *tname =  elf_strptr (elf, shstrndx,
+  tshdr->sh_name);
+ if (! tname || ! ebl_debugscn_p (ebl, tname))
+   continue;
+
+ /* OK, lets relocate all trivial cross debug section
+relocations. */
+ Elf_Data *reldata = elf_getdata (scn, NULL);
+ if (reldata == NULL || reldata->d_buf == NULL)
+   INTERNAL_ERROR (fname);
+
+ /* Make sure we adjust the uncompressed debug data
+(and recompress if necessary at the end).  */
+ GElf_Chdr tchdr;
+ int tcompress_type = 0;
+ if (gelf_getchdr (tscn, &tchdr) != NULL)
+   {
+ tcompress_type = tchdr.ch_type;
+ if (elf_compress (tscn, 0, 0) != 1)
+   INTERNAL_ERROR (fname);
+   }
+
+ Elf_Data *tdata = elf_getdata (tscn, NULL);
+ if (tdata == NULL || tdata->d_buf == NULL
+ || tdata->d_type != ELF_T_BYTE)
+   INTERNAL_ERROR (fname);
+
+ /* Pick up the symbol table and shndx table to
+resolve relocation symbol indexes.  */
+ Elf64_Word symt = shdr->sh_link;
+ Elf_Data *symdata, *xndxdata;
+ Elf_Scn * symscn = elf_getscn (elf, symt);
+ symdata = elf_getdata (symscn, NULL);
+ xndxdata = get_xndxdata (elf, symscn);
+ if (symdata == NULL)
+   INTERNAL_ERROR (fname);
+
+ /* Apply one relocation.  Returns true 

[PATCH 3/4] strip: Extract code to update shdrstrndx into new common function.

2018-10-26 Thread Mark Wielaard
Signed-off-by: Mark Wielaard 
---
 src/ChangeLog |  5 
 src/strip.c   | 94 +++
 2 files changed, 54 insertions(+), 45 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index f55ff6c..766c839 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,10 @@
 2018-10-24  Mark Wielaard  
 
+   * strip.c (handle_elf): Extract code to update shdrstrndx into...
+   (update_shdrstrndx): ... this new function.
+
+2018-10-24  Mark Wielaard  
+
* strip.c (handle_elf): Extract code into separate functions...
(secndx_name): ... new function.
(get_xndxdata): Likewise.
diff --git a/src/strip.c b/src/strip.c
index a7b69a6..1151206 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -395,6 +395,37 @@ get_xndxdata (Elf *elf, Elf_Scn *symscn)
   return xndxdata;
 }
 
+/* Updates the shdrstrndx for the given Elf by updating the Ehdr and
+   possibly the section zero extension field.  Returns zero on success.  */
+static int
+update_shdrstrndx (Elf *elf, size_t shdrstrndx)
+{
+  GElf_Ehdr ehdr;
+  if (gelf_getehdr (elf, &ehdr) == 0)
+return 1;
+
+  if (shdrstrndx < SHN_LORESERVE)
+ehdr.e_shstrndx = shdrstrndx;
+  else
+{
+  ehdr.e_shstrndx = SHN_XINDEX;
+  Elf_Scn *scn0 = elf_getscn (elf, 0);
+  GElf_Shdr shdr0_mem;
+  GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
+  if (shdr0 == NULL)
+   return 1;
+
+  shdr0->sh_link = shdrstrndx;
+  if (gelf_update_shdr (scn0, shdr0) == 0)
+   return 1;
+}
+
+  if (unlikely (gelf_update_ehdr (elf, &ehdr) == 0))
+return 1;
+
+  return 0;
+}
+
 /* Remove any relocations between debug sections in ET_REL
for the debug file when requested.  These relocations are always
zero based between the unallocated sections.  */
@@ -1444,6 +1475,14 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
   debugehdr->e_entry = ehdr->e_entry;
   debugehdr->e_flags = ehdr->e_flags;
 
+  if (unlikely (gelf_update_ehdr (debugelf, debugehdr) == 0))
+   {
+ error (0, 0, gettext ("%s: error while updating ELF header: %s"),
+debug_fname, elf_errmsg (-1));
+ result = 1;
+ goto fail_close;
+   }
+
   size_t shdrstrndx;
   if (elf_getshdrstrndx (elf, &shdrstrndx) < 0)
{
@@ -1453,36 +1492,9 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
  goto fail_close;
}
 
-  if (shstrndx < SHN_LORESERVE)
-   debugehdr->e_shstrndx = shdrstrndx;
-  else
+  if (update_shdrstrndx (debugelf, shdrstrndx) != 0)
{
- debugehdr->e_shstrndx = SHN_XINDEX;
- Elf_Scn *scn0 = elf_getscn (debugelf, 0);
- GElf_Shdr shdr0_mem;
- GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
- if (shdr0 == NULL)
-   {
- error (0, 0, gettext ("%s: error getting zero section: %s"),
-debug_fname, elf_errmsg (-1));
- result = 1;
- goto fail_close;
-   }
-
- shdr0->sh_link = shdrstrndx;
- if (gelf_update_shdr (scn0, shdr0) == 0)
-   {
- error (0, 0, gettext ("%s: error while updating zero section: 
%s"),
-debug_fname, elf_errmsg (-1));
- result = 1;
- goto fail_close;
-   }
-
-   }
-
-  if (unlikely (gelf_update_ehdr (debugelf, debugehdr) == 0))
-   {
- error (0, 0, gettext ("%s: error while creating ELF header: %s"),
+ error (0, 0, gettext ("%s: error updating shdrstrndx: %s"),
 debug_fname, elf_errmsg (-1));
  result = 1;
  goto fail_close;
@@ -2348,26 +2360,18 @@ while computing checksum for debug information"));
  & ~((GElf_Off) (offsize - 1)));
   newehdr->e_shentsize = gelf_fsize (elf, ELF_T_SHDR, 1, EV_CURRENT);
 
-  /* The new section header string table index.  */
-  if (likely (idx < SHN_HIRESERVE) && likely (idx != SHN_XINDEX))
-newehdr->e_shstrndx = idx;
-  else
+  if (gelf_update_ehdr (newelf, newehdr) == 0)
 {
-  /* The index does not fit in the ELF header field.  */
-  shdr_info[0].scn = elf_getscn (elf, 0);
-
-  if (gelf_getshdr (shdr_info[0].scn, &shdr_info[0].shdr) == NULL)
-   INTERNAL_ERROR (fname);
-
-  shdr_info[0].shdr.sh_link = idx;
-  (void) gelf_update_shdr (shdr_info[0].scn, &shdr_info[0].shdr);
-
-  newehdr->e_shstrndx = SHN_XINDEX;
+  error (0, 0, gettext ("%s: error while creating ELF header: %s"),
+output_fname ?: fname, elf_errmsg (-1));
+  cleanup_debug ();
+  return 1;
 }
 
-  if (gelf_update_ehdr (newelf, newehdr) == 0)
+  /* The new section header string table index.  */
+  if (update_shdrstrndx (newelf, idx) != 0)
 {
-  error (0, 0, gettext ("%s: error while creating ELF header: %s"),
+  error (0, 0, gettext ("%s: error updating shdrstrndx: %s"),
 output

[PATCH 4/4] strip: Add --reloc-debug-sections-only option.

2018-10-26 Thread Mark Wielaard
This option does the same thing as --reloc-debug-sections without doing
any other strip operation. This is useful when you want to remove the
debug section relocations in a separate ET_REL debug file that was created
without --reloc-debug-sections, or for a file (like the linux debug vmlinux)
that you don't want to strip, but for which the debug section relocations
can be resolved already.

Signed-off-by: Mark Wielaard 
---
 src/ChangeLog|  11 
 src/strip.c  | 155 +--
 tests/ChangeLog  |   4 ++
 tests/run-strip-reloc.sh |  11 
 4 files changed, 175 insertions(+), 6 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 766c839..0eed9ae 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,14 @@
+2018-10-26  Mark Wielaard  
+
+   * strip.c (OPT_RELOC_DEBUG_ONLY): New define.
+   (options): Add reloc-debug-sections-only.
+   (reloc_debug_only): New static bool.
+   (main): Check reloc_debug_only is the only strip option used.
+   (parse_opt): Handle OPT_RELOC_DEBUG_ONLY.
+   (handle_debug_relocs): New function.
+   (handle_elf): Add local variables lastsec_offset and lastsec_size.
+   Handle reloc_debug_only.
+
 2018-10-24  Mark Wielaard  
 
* strip.c (handle_elf): Extract code to update shdrstrndx into...
diff --git a/src/strip.c b/src/strip.c
index 1151206..e953c4d 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -1,5 +1,5 @@
 /* Discard section not used at runtime from object files.
-   Copyright (C) 2000-2012, 2014, 2015, 2016, 2017 Red Hat, Inc.
+   Copyright (C) 2000-2012, 2014, 2015, 2016, 2017, 2018 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper , 2000.
 
@@ -61,6 +61,7 @@ ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
 #define OPT_STRIP_SECTIONS 0x102
 #define OPT_RELOC_DEBUG0x103
 #define OPT_KEEP_SECTION   0x104
+#define OPT_RELOC_DEBUG_ONLY0x105
 
 
 /* Definitions of arguments for argp functions.  */
@@ -82,6 +83,8 @@ static const struct argp_option options[] =
 N_("Copy modified/access timestamps to the output"), 0 },
   { "reloc-debug-sections", OPT_RELOC_DEBUG, NULL, 0,
 N_("Resolve all trivial relocations between debug sections if the removed 
sections are placed in a debug file (only relevant for ET_REL files, operation 
is not reversable, needs -f)"), 0 },
+  { "reloc-debug-sections-only", OPT_RELOC_DEBUG_ONLY, NULL, 0,
+N_("Similar to --reloc-debug-sections, but resolve all trivial relocations 
between debug sections in place.  No other stripping is performed (operation is 
not reversable, incompatible with -f, -g, --remove-comment and 
--remove-section)"), 0 },
   { "remove-comment", OPT_REMOVE_COMMENT, NULL, 0,
 N_("Remove .comment section"), 0 },
   { "remove-section", 'R', "SECTION", 0, N_("Remove the named section.  
SECTION is an extended wildcard pattern.  May be given more than once.  Only 
non-allocated sections can be removed."), 0 },
@@ -159,6 +162,9 @@ static bool permissive;
 /* If true perform relocations between debug sections.  */
 static bool reloc_debug;
 
+/* If true perform relocations between debug sections only.  */
+static bool reloc_debug_only;
+
 /* Sections the user explicitly wants to keep or remove.  */
 struct section_pattern
 {
@@ -240,6 +246,12 @@ main (int argc, char *argv[])
 error (EXIT_FAILURE, 0,
   gettext ("--reloc-debug-sections used without -f"));
 
+  if (reloc_debug_only &&
+  (debug_fname != NULL || remove_secs != NULL
+   || remove_comment == true || remove_debug == true))
+error (EXIT_FAILURE, 0,
+  gettext ("--reloc-debug-sections-only incompatible with -f, -g, 
--remove-comment and --remove-section"));
+
   /* Tell the library which version we are expecting.  */
   elf_version (EV_CURRENT);
 
@@ -307,6 +319,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
   reloc_debug = true;
   break;
 
+case OPT_RELOC_DEBUG_ONLY:
+  reloc_debug_only = true;
+  break;
+
 case OPT_REMOVE_COMMENT:
   remove_comment = true;
   break;
@@ -774,6 +790,116 @@ process_file (const char *fname)
   return result;
 }
 
+/* Processing for --reloc-debug-sections-only.  */
+static int
+handle_debug_relocs (Elf *elf, Ebl *ebl, Elf *new_elf,
+GElf_Ehdr *ehdr, const char *fname, size_t shstrndx,
+GElf_Off *last_offset, GElf_Xword *last_size)
+{
+
+  /* Copy over the ELF header.  */
+  if (gelf_update_ehdr (new_elf, ehdr) == 0)
+{
+  error (0, 0, "couldn't update new ehdr: %s", elf_errmsg (-1));
+  return 1;
+}
+
+  /* Copy over sections and record end of allocated sections.  */
+  GElf_Off lastoffset = 0;
+  Elf_Scn *scn = NULL;
+  while ((scn = elf_nextscn (elf, scn)) != NULL)
+{
+  /* Get the header.  */
+  GElf_Shdr shdr;
+  if (gelf_getshdr (scn, &shdr) == NULL)
+   {
+ error (0, 0, "couldn't get shdr: %s", elf

Re: [PATCH] addr2line: Use elf_getshdrstrndx not Ehdr field to print section name.

2018-10-26 Thread Mark Wielaard
On Wed, 2018-10-24 at 16:18 +0200, Mark Wielaard wrote:
> Using the Ehdr field directly doesn't work when there are a large
> number of sections.

Pushed to master.


Re: [PATCH] readelf: Use shstrndx to lookup section names.

2018-10-26 Thread Mark Wielaard
On Wed, 2018-10-24 at 16:01 +0200, Mark Wielaard wrote:
> The function section_name would use the Ehdr e_shstrndx field to find the
> index of the section index string table directly. But it should use
> elf_getshdrstrndx. Adjust all callers.

Pushed to master.