[PATCH 04/10] elfcmp: Get, check and shdrstrndx for section names.

Mark Wielaard mark@klomp.org
Thu Sep 13 23:02:00 GMT 2018


elfcmp would use the Ehdr e_shstrndx field to find the shdr string
index table. Use elf_getshdrstrndx instead to be able to handle ELF
files with more than SHN_LORESERVE sections.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog |  4 ++++
 src/elfcmp.c  | 20 ++++++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 7d046ec..79da69b 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2018-09-13  Mark Wielaard  <mark@klomp.org>
+
+	* elfcmp.c (main): Get, check and shdrstrndx for section names.
+
 2018-09-12  Mark Wielaard  <mark@klomp.org>
 
 	* elfcmp.c (main): Call ebl_section_strip_p without ehdr.
diff --git a/src/elfcmp.c b/src/elfcmp.c
index b68df68..d5dc1ff 100644
--- a/src/elfcmp.c
+++ b/src/elfcmp.c
@@ -235,6 +235,22 @@ main (int argc, char *argv[])
       DIFFERENCE;
     }
 
+  size_t shstrndx1;
+  size_t shstrndx2;
+  if (elf_getshdrstrndx (elf1, &shstrndx1) != 0)
+    error (2, 0, gettext ("cannot get hdrstrndx of '%s': %s"),
+	   fname1, elf_errmsg (-1));
+  if (elf_getshdrstrndx (elf2, &shstrndx2) != 0)
+    error (2, 0, gettext ("cannot get hdrstrndx of '%s': %s"),
+	   fname2, elf_errmsg (-1));
+  if (shstrndx1 != shstrndx2)
+    {
+      if (! quiet)
+	error (0, 0, gettext ("%s %s diff: shdr string index"),
+	       fname1, fname2);
+      DIFFERENCE;
+    }
+
   /* Iterate over all sections.  We expect the sections in the two
      files to match exactly.  */
   Elf_Scn *scn1 = NULL;
@@ -251,7 +267,7 @@ main (int argc, char *argv[])
 	  scn1 = elf_nextscn (elf1, scn1);
 	  shdr1 = gelf_getshdr (scn1, &shdr1_mem);
 	  if (shdr1 != NULL)
-	    sname1 = elf_strptr (elf1, ehdr1->e_shstrndx, shdr1->sh_name);
+	    sname1 = elf_strptr (elf1, shstrndx1, shdr1->sh_name);
 	}
       while (scn1 != NULL
 	     && ebl_section_strip_p (ebl1, shdr1, sname1, true, false));
@@ -264,7 +280,7 @@ main (int argc, char *argv[])
 	  scn2 = elf_nextscn (elf2, scn2);
 	  shdr2 = gelf_getshdr (scn2, &shdr2_mem);
 	  if (shdr2 != NULL)
-	    sname2 = elf_strptr (elf2, ehdr2->e_shstrndx, shdr2->sh_name);
+	    sname2 = elf_strptr (elf2, shstrndx2, shdr2->sh_name);
 	}
       while (scn2 != NULL
 	     && ebl_section_strip_p (ebl2, shdr2, sname2, true, false));
-- 
1.8.3.1



More information about the Elfutils-devel mailing list