c0e8f3
commit d7193bd7c9dc2a979352eee7fc446dacd3e97779
c0e8f3
Author: Mark Wielaard <mark@klomp.org>
c0e8f3
Date:   Sun May 12 00:37:45 2019 +0200
c0e8f3
c0e8f3
    libelf: Mark shdr_flags dirty if offset or size changes during update.
c0e8f3
    
c0e8f3
    We forgot to mark the shdr_flags dirty when only the sh_size or
c0e8f3
    sh_offset changed during elf_update (). This meant that if there were
c0e8f3
    no other shdr changes we only wrote out the section data, but didn't
c0e8f3
    write out the shdr table to the file.
c0e8f3
    
c0e8f3
    Add a testcase that puts some sections in the reverse order and then
c0e8f3
    writes out the resulting file again without doing any other
c0e8f3
    updates. This would show the issue after write out of the
c0e8f3
    (re-reversed) ELF file (the .shstrtab section offset would be wrong
c0e8f3
    causing all section names to be garbage). Also run a self test.
c0e8f3
    
c0e8f3
    Signed-off-by: Mark Wielaard <mark@klomp.org>
c0e8f3
c0e8f3
diff --git a/libelf/elf32_updatenull.c b/libelf/elf32_updatenull.c
c0e8f3
index 2ce6a59..303055a 100644
c0e8f3
--- a/libelf/elf32_updatenull.c
c0e8f3
+++ b/libelf/elf32_updatenull.c
c0e8f3
@@ -366,12 +366,15 @@ __elfw2(LIBELFBITS,updatenull_wrlock) (Elf *elf, int *change_bop, size_t shnum)
c0e8f3
 		    }
c0e8f3
 
c0e8f3
 		  /* See whether the section size is correct.  */
c0e8f3
+		  int size_changed = 0;
c0e8f3
 		  update_if_changed (shdr->sh_size, (GElf_Word) offset,
c0e8f3
-				     changed);
c0e8f3
+				     size_changed);
c0e8f3
+		  changed |= size_changed;
c0e8f3
 
c0e8f3
 		  if (shdr->sh_type != SHT_NOBITS)
c0e8f3
 		    size += offset;
c0e8f3
 
c0e8f3
+		  scn->shdr_flags |= (offset_changed | size_changed);
c0e8f3
 		  scn->flags |= changed;
c0e8f3
 		}
c0e8f3
 
c0e8f3
diff --git a/tests/Makefile.am b/tests/Makefile.am
c0e8f3
index 80900e4..87428aa 100644
c0e8f3
--- a/tests/Makefile.am
c0e8f3
+++ b/tests/Makefile.am
c0e8f3
@@ -60,7 +60,7 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
c0e8f3
 		  fillfile dwarf_default_lower_bound dwarf-die-addr-die \
c0e8f3
 		  get-units-invalid get-units-split attr-integrate-skel \
c0e8f3
 		  all-dwarf-ranges unit-info next_cfi \
c0e8f3
-		  elfcopy addsections xlate_notes
c0e8f3
+		  elfcopy addsections xlate_notes elfrdwrnop
c0e8f3
 
c0e8f3
 asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
c0e8f3
 	    asm-tst6 asm-tst7 asm-tst8 asm-tst9
c0e8f3
@@ -157,6 +157,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
c0e8f3
 	run-all-dwarf-ranges.sh run-unit-info.sh \
c0e8f3
 	run-reloc-bpf.sh \
c0e8f3
 	run-next-cfi.sh run-next-cfi-self.sh \
c0e8f3
+	run-reverse-sections.sh run-reverse-sections-self.sh \
c0e8f3
 	run-copyadd-sections.sh run-copymany-sections.sh \
c0e8f3
 	run-typeiter-many.sh run-strip-test-many.sh \
c0e8f3
 	run-strip-version.sh run-xlate-note.sh
c0e8f3
@@ -419,6 +420,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
c0e8f3
 	     run-unit-info.sh run-next-cfi.sh run-next-cfi-self.sh \
c0e8f3
 	     testfile-riscv64.bz2 testfile-riscv64-s.bz2 \
c0e8f3
 	     testfile-riscv64-core.bz2 \
c0e8f3
+	     run-reverse-sections.sh run-reverse-sections-self.sh \
c0e8f3
 	     run-copyadd-sections.sh run-copymany-sections.sh \
c0e8f3
 	     run-typeiter-many.sh run-strip-test-many.sh \
c0e8f3
 	     testfile-debug-rel-ppc64-g.o.bz2 \
c0e8f3
@@ -598,6 +600,7 @@ next_cfi_LDADD = $(libelf) $(libdw)
c0e8f3
 elfcopy_LDADD = $(libelf)
c0e8f3
 addsections_LDADD = $(libelf)
c0e8f3
 xlate_notes_LDADD = $(libelf)
c0e8f3
+elfrdwrnop_LDADD = $(libelf)
c0e8f3
 
c0e8f3
 # We want to test the libelf header against the system elf.h header.
c0e8f3
 # Don't include any -I CPPFLAGS. Except when we install our own elf.h.
c0e8f3
diff --git a/tests/elfcopy.c b/tests/elfcopy.c
c0e8f3
index 9000cc9..d457bad 100644
c0e8f3
--- a/tests/elfcopy.c
c0e8f3
+++ b/tests/elfcopy.c
c0e8f3
@@ -69,9 +69,11 @@ setshstrndx (Elf *elf, size_t ndx)
c0e8f3
 
c0e8f3
 /* Copies all elements of an ELF file either using mmap or read.  */
c0e8f3
 static void
c0e8f3
-copy_elf (const char *in, const char *out, bool use_mmap)
c0e8f3
+copy_elf (const char *in, const char *out, bool use_mmap, bool reverse_offs)
c0e8f3
 {
c0e8f3
-  printf ("\ncopy_elf: %s -> %s (%s)\n", in, out, use_mmap ? "mmap" : "read");
c0e8f3
+  printf ("\ncopy_elf: %s -> %s (%s,%s)\n", in, out,
c0e8f3
+	  use_mmap ? "mmap" : "read",
c0e8f3
+	  reverse_offs ? "reverse" : "same");
c0e8f3
 
c0e8f3
   /* Existing ELF file.  */
c0e8f3
   int fda = open (in, O_RDONLY);
c0e8f3
@@ -182,8 +184,28 @@ copy_elf (const char *in, const char *out, bool use_mmap)
c0e8f3
 	}
c0e8f3
     }
c0e8f3
 
c0e8f3
+  GElf_Off *offs = NULL;
c0e8f3
+  size_t shnum;
c0e8f3
+  if (reverse_offs)
c0e8f3
+    {
c0e8f3
+      if (elf_getshdrnum (elfa, &shnum) < 0)
c0e8f3
+	{
c0e8f3
+	  printf ("couldn't get shdrnum: %s\n", elf_errmsg (-1));
c0e8f3
+	  exit (1);
c0e8f3
+	}
c0e8f3
+
c0e8f3
+      offs = (GElf_Off *) malloc (shnum * sizeof (GElf_Off));
c0e8f3
+      if (offs == NULL)
c0e8f3
+	{
c0e8f3
+	  printf ("couldn't allocate memory for offs\n");
c0e8f3
+	  exit (1);
c0e8f3
+	}
c0e8f3
+    }
c0e8f3
+
c0e8f3
   /* Copy all sections, headers and data.  */
c0e8f3
   Elf_Scn *scn = NULL;
c0e8f3
+  size_t last_off = 0;
c0e8f3
+  GElf_Shdr last_shdr = { .sh_type = SHT_NULL };
c0e8f3
   while ((scn = elf_nextscn (elfa, scn)) != NULL)
c0e8f3
     {
c0e8f3
       /* Get the header.  */
c0e8f3
@@ -194,6 +216,34 @@ copy_elf (const char *in, const char *out, bool use_mmap)
c0e8f3
 	  exit (1);
c0e8f3
 	}
c0e8f3
 
c0e8f3
+      if (reverse_offs)
c0e8f3
+	{
c0e8f3
+	  offs[last_off] = shdr.sh_offset;
c0e8f3
+
c0e8f3
+	  if (last_shdr.sh_type != SHT_NULL
c0e8f3
+	      && last_shdr.sh_addralign == shdr.sh_addralign
c0e8f3
+	      && shdr.sh_addralign == 1
c0e8f3
+	      && last_shdr.sh_type != SHT_NOBITS
c0e8f3
+	      && shdr.sh_type != SHT_NOBITS
c0e8f3
+	      && (phnum == 0
c0e8f3
+		  || ((shdr.sh_flags & SHF_ALLOC) == 0
c0e8f3
+		      && (last_shdr.sh_flags & SHF_ALLOC) == 0)))
c0e8f3
+	    {
c0e8f3
+	      printf ("Swapping offsets of section %zd and %zd\n",
c0e8f3
+		      last_off, last_off + 1);
c0e8f3
+	      GElf_Word off = offs[last_off - 1];
c0e8f3
+	      offs[last_off - 1] = off + shdr.sh_size;
c0e8f3
+	      offs[last_off] = off;
c0e8f3
+	      last_shdr.sh_type = SHT_NULL;
c0e8f3
+	    }
c0e8f3
+	  else
c0e8f3
+	    {
c0e8f3
+	      last_shdr = shdr;
c0e8f3
+	      offs[last_off] = shdr.sh_offset;
c0e8f3
+	    }
c0e8f3
+	  last_off++;
c0e8f3
+	}
c0e8f3
+
c0e8f3
       /* Create new section.  */
c0e8f3
       Elf_Scn *new_scn = elf_newscn (elfb);
c0e8f3
       if (new_scn == NULL)
c0e8f3
@@ -223,9 +273,34 @@ copy_elf (const char *in, const char *out, bool use_mmap)
c0e8f3
 	}
c0e8f3
     }
c0e8f3
 
c0e8f3
-  /* Write everything to disk.  If there are any phdrs then we want
c0e8f3
-     the exact same layout.  Do we want ELF_F_PERMISSIVE?  */
c0e8f3
-  if (phnum > 0)
c0e8f3
+  if (reverse_offs)
c0e8f3
+    {
c0e8f3
+      last_off = 0;
c0e8f3
+      scn = NULL;
c0e8f3
+      while ((scn = elf_nextscn (elfb, scn)) != NULL)
c0e8f3
+	{
c0e8f3
+	  GElf_Shdr shdr;
c0e8f3
+	  if (gelf_getshdr (scn, &shdr) == NULL)
c0e8f3
+	    {
c0e8f3
+	      printf ("couldn't get shdr for updating: %s\n", elf_errmsg (-1));
c0e8f3
+	      exit (1);
c0e8f3
+	    }
c0e8f3
+
c0e8f3
+	  shdr.sh_offset = offs[last_off++];
c0e8f3
+
c0e8f3
+	  if (gelf_update_shdr (scn, &shdr) == 0)
c0e8f3
+	    {
c0e8f3
+	      printf ("couldn't update shdr sh_off: %s\n", elf_errmsg (-1));
c0e8f3
+	      exit (1);
c0e8f3
+	    }
c0e8f3
+	}
c0e8f3
+      free (offs);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  /* Write everything to disk.  If there are any phdrs, or we want to
c0e8f3
+     update the offsets, then we want the exact same layout.  Do we
c0e8f3
+     want ELF_F_PERMISSIVE?  */
c0e8f3
+  if (phnum > 0 || reverse_offs)
c0e8f3
     elf_flagelf (elfb, ELF_C_SET, ELF_F_LAYOUT);
c0e8f3
   if (elf_update (elfb, ELF_C_WRITE) < 0)
c0e8f3
     {
c0e8f3
@@ -264,9 +339,9 @@ main (int argc, const char *argv[])
c0e8f3
   elf_version (EV_CURRENT);
c0e8f3
 
c0e8f3
   /* Takes the given file, and create a new identical one.  */
c0e8f3
-  if (argc < 3 || argc > 4)
c0e8f3
+  if (argc < 3 || argc > 5)
c0e8f3
     {
c0e8f3
-      fprintf (stderr, "elfcopy [--mmap] in.elf out.elf\n");
c0e8f3
+      fprintf (stderr, "elfcopy [--mmap] [--reverse-offs] in.elf out.elf\n");
c0e8f3
       exit (1);
c0e8f3
     }
c0e8f3
 
c0e8f3
@@ -278,9 +353,16 @@ main (int argc, const char *argv[])
c0e8f3
       argn++;
c0e8f3
     }
c0e8f3
 
c0e8f3
+  bool reverse_offs = false;
c0e8f3
+  if (strcmp (argv[argn], "--reverse-offs") == 0)
c0e8f3
+    {
c0e8f3
+      reverse_offs = true;
c0e8f3
+      argn++;
c0e8f3
+    }
c0e8f3
+
c0e8f3
   const char *in = argv[argn++];
c0e8f3
   const char *out = argv[argn];
c0e8f3
-  copy_elf (in, out, use_mmap);
c0e8f3
+  copy_elf (in, out, use_mmap, reverse_offs);
c0e8f3
 
c0e8f3
   return 0;
c0e8f3
 }
c0e8f3
diff --git a/tests/elfrdwrnop.c b/tests/elfrdwrnop.c
c0e8f3
new file mode 100644
c0e8f3
index 0000000..997150b
c0e8f3
--- /dev/null
c0e8f3
+++ b/tests/elfrdwrnop.c
c0e8f3
@@ -0,0 +1,100 @@
c0e8f3
+/* Test program for reading and writing out the same file in-place
c0e8f3
+   Copyright (C) 2019 Red Hat, Inc.
c0e8f3
+   This file is part of elfutils.
c0e8f3
+
c0e8f3
+   This file is free software; you can redistribute it and/or modify
c0e8f3
+   it under the terms of the GNU General Public License as published by
c0e8f3
+   the Free Software Foundation; either version 3 of the License, or
c0e8f3
+   (at your option) any later version.
c0e8f3
+
c0e8f3
+   elfutils is distributed in the hope that it will be useful, but
c0e8f3
+   WITHOUT ANY WARRANTY; without even the implied warranty of
c0e8f3
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c0e8f3
+   GNU General Public License for more details.
c0e8f3
+
c0e8f3
+   You should have received a copy of the GNU General Public License
c0e8f3
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
c0e8f3
+
c0e8f3
+
c0e8f3
+#ifdef HAVE_CONFIG_H
c0e8f3
+# include <config.h>
c0e8f3
+#endif
c0e8f3
+
c0e8f3
+#include <errno.h>
c0e8f3
+#include <fcntl.h>
c0e8f3
+#include <inttypes.h>
c0e8f3
+#include <stdbool.h>
c0e8f3
+#include <stdio.h>
c0e8f3
+#include <stdlib.h>
c0e8f3
+#include <string.h>
c0e8f3
+#include <unistd.h>
c0e8f3
+#include <sys/types.h>
c0e8f3
+#include <sys/stat.h>
c0e8f3
+
c0e8f3
+#include ELFUTILS_HEADER(elf)
c0e8f3
+#include <gelf.h>
c0e8f3
+
c0e8f3
+
c0e8f3
+int
c0e8f3
+main (int argc, const char *argv[])
c0e8f3
+{
c0e8f3
+  /* Takes the given file, and create a new identical one.  */
c0e8f3
+  if (argc != 2)
c0e8f3
+    {
c0e8f3
+      fprintf (stderr, "elfrdwrnop elf-file\n");
c0e8f3
+      exit (1);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  elf_version (EV_CURRENT);
c0e8f3
+
c0e8f3
+  const char *name = argv[1];
c0e8f3
+  printf ("elfrdwrdnop %s\n", name);
c0e8f3
+
c0e8f3
+  int fd = open (name, O_RDWR);
c0e8f3
+  if (fd < 0)
c0e8f3
+    {
c0e8f3
+      fprintf (stderr, "Couldn't open file '%s': %s\n",
c0e8f3
+	       name, strerror (errno));
c0e8f3
+      exit (1);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  Elf *elf = elf_begin (fd, ELF_C_RDWR, NULL);
c0e8f3
+  if (elf == NULL)
c0e8f3
+    {
c0e8f3
+      fprintf (stderr, "Couldn't open ELF file '%s': %s\n",
c0e8f3
+	       name, elf_errmsg (-1));
c0e8f3
+      exit (1);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  /* Write everything to disk.  If there are any phdrs, then we want
c0e8f3
+     the exact same layout.  */
c0e8f3
+  size_t phnum;
c0e8f3
+  if (elf_getphdrnum (elf, &phnum) != 0)
c0e8f3
+    {
c0e8f3
+      printf ("cannot get phdrs: %s\n", elf_errmsg (-1));
c0e8f3
+      exit (1);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  if (phnum > 0)
c0e8f3
+    elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT);
c0e8f3
+
c0e8f3
+  if (elf_update (elf, ELF_C_WRITE) < 0)
c0e8f3
+    {
c0e8f3
+      printf ("failure in elf_update: %s\n", elf_errmsg (-1));
c0e8f3
+      exit (1);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  if (elf_end (elf) != 0)
c0e8f3
+    {
c0e8f3
+      printf ("couldn't cleanup elf '%s': %s\n", name, elf_errmsg (-1));
c0e8f3
+      exit (1);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  if (close (fd) != 0)
c0e8f3
+    {
c0e8f3
+      printf ("couldn't close '%s': %s\n", name, strerror (errno));
c0e8f3
+      exit (1);
c0e8f3
+    }
c0e8f3
+
c0e8f3
+  return 0;
c0e8f3
+}
c0e8f3
diff --git a/tests/run-reverse-sections-self.sh b/tests/run-reverse-sections-self.sh
c0e8f3
new file mode 100755
c0e8f3
index 0000000..71afd6a
c0e8f3
--- /dev/null
c0e8f3
+++ b/tests/run-reverse-sections-self.sh
c0e8f3
@@ -0,0 +1,45 @@
c0e8f3
+#! /bin/sh
c0e8f3
+# Copyright (C) 2019 Red Hat, Inc.
c0e8f3
+# This file is part of elfutils.
c0e8f3
+#
c0e8f3
+# This file is free software; you can redistribute it and/or modify
c0e8f3
+# it under the terms of the GNU General Public License as published by
c0e8f3
+# the Free Software Foundation; either version 3 of the License, or
c0e8f3
+# (at your option) any later version.
c0e8f3
+#
c0e8f3
+# elfutils is distributed in the hope that it will be useful, but
c0e8f3
+# WITHOUT ANY WARRANTY; without even the implied warranty of
c0e8f3
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c0e8f3
+# GNU General Public License for more details.
c0e8f3
+#
c0e8f3
+# You should have received a copy of the GNU General Public License
c0e8f3
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
c0e8f3
+
c0e8f3
+. $srcdir/test-subr.sh
c0e8f3
+
c0e8f3
+test_reverse_self ()
c0e8f3
+{
c0e8f3
+  in_file="$1"
c0e8f3
+  base_name="$(basename ${in_file})"
c0e8f3
+  out_file="${base_name}.rev"
c0e8f3
+  out_file_mmap="${out_file}.mmap"
c0e8f3
+
c0e8f3
+  tempfiles ${out_file} ${out_file_mmap}
c0e8f3
+
c0e8f3
+  # Reverse the offsets (the files should still be the same otherwise)
c0e8f3
+  testrun ${abs_builddir}/elfcopy --reverse-offs ${in_file} ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
c0e8f3
+  # An in-place nop will likely revert them back
c0e8f3
+  testrun ${abs_builddir}/elfrdwrnop ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
c0e8f3
+}
c0e8f3
+
c0e8f3
+# Only really makes sense for ET_REL files, but try all, just to check
c0e8f3
+# it also works if we keep the order for the allocated sections.
c0e8f3
+for file in $self_test_files; do
c0e8f3
+  test_reverse_self $file
c0e8f3
+done
c0e8f3
+
c0e8f3
+exit 0
c0e8f3
diff --git a/tests/run-reverse-sections.sh b/tests/run-reverse-sections.sh
c0e8f3
new file mode 100755
c0e8f3
index 0000000..102a126
c0e8f3
--- /dev/null
c0e8f3
+++ b/tests/run-reverse-sections.sh
c0e8f3
@@ -0,0 +1,69 @@
c0e8f3
+#! /bin/sh
c0e8f3
+# Copyright (C) 2019 Red Hat, Inc.
c0e8f3
+# This file is part of elfutils.
c0e8f3
+#
c0e8f3
+# This file is free software; you can redistribute it and/or modify
c0e8f3
+# it under the terms of the GNU General Public License as published by
c0e8f3
+# the Free Software Foundation; either version 3 of the License, or
c0e8f3
+# (at your option) any later version.
c0e8f3
+#
c0e8f3
+# elfutils is distributed in the hope that it will be useful, but
c0e8f3
+# WITHOUT ANY WARRANTY; without even the implied warranty of
c0e8f3
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c0e8f3
+# GNU General Public License for more details.
c0e8f3
+#
c0e8f3
+# You should have received a copy of the GNU General Public License
c0e8f3
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
c0e8f3
+
c0e8f3
+. $srcdir/test-subr.sh
c0e8f3
+
c0e8f3
+test_reverse ()
c0e8f3
+{
c0e8f3
+  in_file="$1"
c0e8f3
+  out_file="${in_file}.rev"
c0e8f3
+  out_file_mmap="${out_file}.mmap"
c0e8f3
+
c0e8f3
+  testfiles ${in_file}
c0e8f3
+  tempfiles ${out_file} ${out_file_mmap}
c0e8f3
+
c0e8f3
+  # Reverse the offsets (the files should still be the same otherwise)
c0e8f3
+  testrun ${abs_builddir}/elfcopy --reverse-offs ${in_file} ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
c0e8f3
+  # An in-place nop will likely revert them back
c0e8f3
+  testrun ${abs_builddir}/elfrdwrnop ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
c0e8f3
+  testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
c0e8f3
+}
c0e8f3
+
c0e8f3
+# A collection of random testfiles to test 32/64bit, little/big endian
c0e8f3
+# and non-ET_REL (with phdrs)/ET_REL (without phdrs).
c0e8f3
+
c0e8f3
+# 32bit, big endian, rel
c0e8f3
+test_reverse testfile29
c0e8f3
+
c0e8f3
+# 64bit, big endian, rel
c0e8f3
+test_reverse testfile23
c0e8f3
+
c0e8f3
+# 32bit, little endian, rel
c0e8f3
+test_reverse testfile9
c0e8f3
+
c0e8f3
+# 64bit, little endian, rel
c0e8f3
+test_reverse testfile38
c0e8f3
+
c0e8f3
+# 32bit, big endian, non-rel
c0e8f3
+test_reverse testfile26
c0e8f3
+
c0e8f3
+# 64bit, big endian, non-rel
c0e8f3
+test_reverse testfile27
c0e8f3
+
c0e8f3
+# 32bit, little endian, non-rel
c0e8f3
+test_reverse testfile
c0e8f3
+
c0e8f3
+# 64bit, little endian, non-rel
c0e8f3
+# Don't use testfile10. It has section headers in the middle of the file.
c0e8f3
+# Same for testfile12. It is legal, but not the point of this testcase.
c0e8f3
+# test_reverse testfile10
c0e8f3
+test_reverse testfile13
c0e8f3
+
c0e8f3
+exit 0
c0e8f3
diff -ru elfutils-0.176.orig/tests/Makefile.in elfutils-0.176/tests/Makefile.in
c0e8f3
--- elfutils-0.176.orig/tests/Makefile.in	2019-06-03 14:57:17.223607024 +0200
c0e8f3
+++ elfutils-0.176/tests/Makefile.in	2019-06-03 14:58:32.671049626 +0200
c0e8f3
@@ -131,8 +131,8 @@
c0e8f3
 	get-units-invalid$(EXEEXT) get-units-split$(EXEEXT) \
c0e8f3
 	attr-integrate-skel$(EXEEXT) all-dwarf-ranges$(EXEEXT) \
c0e8f3
 	unit-info$(EXEEXT) next_cfi$(EXEEXT) elfcopy$(EXEEXT) \
c0e8f3
-	addsections$(EXEEXT) xlate_notes$(EXEEXT) $(am__EXEEXT_1) \
c0e8f3
-	$(am__EXEEXT_2) $(am__EXEEXT_4)
c0e8f3
+	addsections$(EXEEXT) xlate_notes$(EXEEXT) elfrdwrnop$(EXEEXT) \
c0e8f3
+	$(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_4)
c0e8f3
 @BIARCH_TRUE@am__append_5 = backtrace-child-biarch
c0e8f3
 TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile$(EXEEXT) \
c0e8f3
 	test-nlist$(EXEEXT) update1$(EXEEXT) update2$(EXEEXT) \
c0e8f3
@@ -209,7 +209,8 @@
c0e8f3
 	run-get-units-invalid.sh run-get-units-split.sh \
c0e8f3
 	run-attr-integrate-skel.sh run-all-dwarf-ranges.sh \
c0e8f3
 	run-unit-info.sh run-reloc-bpf.sh run-next-cfi.sh \
c0e8f3
-	run-next-cfi-self.sh run-copyadd-sections.sh \
c0e8f3
+	run-next-cfi-self.sh run-reverse-sections.sh \
c0e8f3
+	run-reverse-sections-self.sh run-copyadd-sections.sh \
c0e8f3
 	run-copymany-sections.sh run-typeiter-many.sh \
c0e8f3
 	run-strip-test-many.sh run-strip-version.sh run-xlate-note.sh \
c0e8f3
 	$(am__EXEEXT_2) $(am__append_8) $(am__EXEEXT_5)
c0e8f3
@@ -451,6 +452,9 @@
c0e8f3
 elfputzdata_SOURCES = elfputzdata.c
c0e8f3
 elfputzdata_OBJECTS = elfputzdata.$(OBJEXT)
c0e8f3
 elfputzdata_DEPENDENCIES = $(am__DEPENDENCIES_2)
c0e8f3
+elfrdwrnop_SOURCES = elfrdwrnop.c
c0e8f3
+elfrdwrnop_OBJECTS = elfrdwrnop.$(OBJEXT)
c0e8f3
+elfrdwrnop_DEPENDENCIES = $(am__DEPENDENCIES_2)
c0e8f3
 elfshphehdr_SOURCES = elfshphehdr.c
c0e8f3
 elfshphehdr_OBJECTS = elfshphehdr.$(OBJEXT)
c0e8f3
 elfshphehdr_DEPENDENCIES = $(am__DEPENDENCIES_2)
c0e8f3
@@ -660,13 +664,13 @@
c0e8f3
 	./$(DEPDIR)/early-offscn.Po ./$(DEPDIR)/ecp.Po \
c0e8f3
 	./$(DEPDIR)/elfcopy.Po ./$(DEPDIR)/elfgetchdr.Po \
c0e8f3
 	./$(DEPDIR)/elfgetzdata.Po ./$(DEPDIR)/elfputzdata.Po \
c0e8f3
-	./$(DEPDIR)/elfshphehdr.Po ./$(DEPDIR)/elfstrmerge.Po \
c0e8f3
-	./$(DEPDIR)/elfstrtab.Po ./$(DEPDIR)/emptyfile.Po \
c0e8f3
-	./$(DEPDIR)/fillfile.Po ./$(DEPDIR)/find-prologues.Po \
c0e8f3
-	./$(DEPDIR)/funcretval.Po ./$(DEPDIR)/funcscopes.Po \
c0e8f3
-	./$(DEPDIR)/get-aranges.Po ./$(DEPDIR)/get-files.Po \
c0e8f3
-	./$(DEPDIR)/get-lines.Po ./$(DEPDIR)/get-pubnames.Po \
c0e8f3
-	./$(DEPDIR)/get-units-invalid.Po \
c0e8f3
+	./$(DEPDIR)/elfrdwrnop.Po ./$(DEPDIR)/elfshphehdr.Po \
c0e8f3
+	./$(DEPDIR)/elfstrmerge.Po ./$(DEPDIR)/elfstrtab.Po \
c0e8f3
+	./$(DEPDIR)/emptyfile.Po ./$(DEPDIR)/fillfile.Po \
c0e8f3
+	./$(DEPDIR)/find-prologues.Po ./$(DEPDIR)/funcretval.Po \
c0e8f3
+	./$(DEPDIR)/funcscopes.Po ./$(DEPDIR)/get-aranges.Po \
c0e8f3
+	./$(DEPDIR)/get-files.Po ./$(DEPDIR)/get-lines.Po \
c0e8f3
+	./$(DEPDIR)/get-pubnames.Po ./$(DEPDIR)/get-units-invalid.Po \
c0e8f3
 	./$(DEPDIR)/get-units-split.Po ./$(DEPDIR)/getsrc_die.Po \
c0e8f3
 	./$(DEPDIR)/hash.Po ./$(DEPDIR)/line2addr.Po \
c0e8f3
 	./$(DEPDIR)/low_high_pc.Po ./$(DEPDIR)/msg_tst.Po \
c0e8f3
@@ -718,19 +722,19 @@
c0e8f3
 	dwfl-bug-getmodules.c dwfl-bug-report.c dwfl-proc-attach.c \
c0e8f3
 	dwfl-report-elf-align.c dwfllines.c dwflmodtest.c dwflsyms.c \
c0e8f3
 	early-offscn.c ecp.c elfcopy.c elfgetchdr.c elfgetzdata.c \
c0e8f3
-	elfputzdata.c elfshphehdr.c elfstrmerge.c elfstrtab.c \
c0e8f3
-	emptyfile.c fillfile.c find-prologues.c funcretval.c \
c0e8f3
-	funcscopes.c get-aranges.c get-files.c get-lines.c \
c0e8f3
-	get-pubnames.c get-units-invalid.c get-units-split.c \
c0e8f3
-	getsrc_die.c hash.c line2addr.c low_high_pc.c msg_tst.c \
c0e8f3
-	newdata.c newfile.c newscn.c next-files.c next-lines.c \
c0e8f3
-	next_cfi.c peel_type.c rdwrmmap.c rerequest_tag.c saridx.c \
c0e8f3
-	scnnames.c sectiondump.c show-abbrev.c show-die-info.c \
c0e8f3
-	showptable.c strptr.c system-elf-libelf-test.c \
c0e8f3
-	test-elf_cntl_gelf_getshdr.c test-flag-nobits.c test-nlist.c \
c0e8f3
-	typeiter.c typeiter2.c unit-info.c update1.c update2.c \
c0e8f3
-	update3.c update4.c varlocs.c vdsosyms.c vendorelf.c \
c0e8f3
-	xlate_notes.c zstrptr.c
c0e8f3
+	elfputzdata.c elfrdwrnop.c elfshphehdr.c elfstrmerge.c \
c0e8f3
+	elfstrtab.c emptyfile.c fillfile.c find-prologues.c \
c0e8f3
+	funcretval.c funcscopes.c get-aranges.c get-files.c \
c0e8f3
+	get-lines.c get-pubnames.c get-units-invalid.c \
c0e8f3
+	get-units-split.c getsrc_die.c hash.c line2addr.c \
c0e8f3
+	low_high_pc.c msg_tst.c newdata.c newfile.c newscn.c \
c0e8f3
+	next-files.c next-lines.c next_cfi.c peel_type.c rdwrmmap.c \
c0e8f3
+	rerequest_tag.c saridx.c scnnames.c sectiondump.c \
c0e8f3
+	show-abbrev.c show-die-info.c showptable.c strptr.c \
c0e8f3
+	system-elf-libelf-test.c test-elf_cntl_gelf_getshdr.c \
c0e8f3
+	test-flag-nobits.c test-nlist.c typeiter.c typeiter2.c \
c0e8f3
+	unit-info.c update1.c update2.c update3.c update4.c varlocs.c \
c0e8f3
+	vdsosyms.c vendorelf.c xlate_notes.c zstrptr.c
c0e8f3
 DIST_SOURCES = addrcfi.c addrscopes.c addsections.c aggregate_size.c \
c0e8f3
 	all-dwarf-ranges.c alldts.c allfcts.c allregs.c arextract.c \
c0e8f3
 	arls.c arsymtest.c asm-tst1.c asm-tst2.c asm-tst3.c asm-tst4.c \
c0e8f3
@@ -745,19 +749,19 @@
c0e8f3
 	dwfl-bug-getmodules.c dwfl-bug-report.c dwfl-proc-attach.c \
c0e8f3
 	dwfl-report-elf-align.c dwfllines.c dwflmodtest.c dwflsyms.c \
c0e8f3
 	early-offscn.c ecp.c elfcopy.c elfgetchdr.c elfgetzdata.c \
c0e8f3
-	elfputzdata.c elfshphehdr.c elfstrmerge.c elfstrtab.c \
c0e8f3
-	emptyfile.c fillfile.c find-prologues.c funcretval.c \
c0e8f3
-	funcscopes.c get-aranges.c get-files.c get-lines.c \
c0e8f3
-	get-pubnames.c get-units-invalid.c get-units-split.c \
c0e8f3
-	getsrc_die.c hash.c line2addr.c low_high_pc.c msg_tst.c \
c0e8f3
-	newdata.c newfile.c newscn.c next-files.c next-lines.c \
c0e8f3
-	next_cfi.c peel_type.c rdwrmmap.c rerequest_tag.c saridx.c \
c0e8f3
-	scnnames.c sectiondump.c show-abbrev.c show-die-info.c \
c0e8f3
-	showptable.c strptr.c system-elf-libelf-test.c \
c0e8f3
-	test-elf_cntl_gelf_getshdr.c test-flag-nobits.c test-nlist.c \
c0e8f3
-	typeiter.c typeiter2.c unit-info.c update1.c update2.c \
c0e8f3
-	update3.c update4.c varlocs.c vdsosyms.c vendorelf.c \
c0e8f3
-	xlate_notes.c zstrptr.c
c0e8f3
+	elfputzdata.c elfrdwrnop.c elfshphehdr.c elfstrmerge.c \
c0e8f3
+	elfstrtab.c emptyfile.c fillfile.c find-prologues.c \
c0e8f3
+	funcretval.c funcscopes.c get-aranges.c get-files.c \
c0e8f3
+	get-lines.c get-pubnames.c get-units-invalid.c \
c0e8f3
+	get-units-split.c getsrc_die.c hash.c line2addr.c \
c0e8f3
+	low_high_pc.c msg_tst.c newdata.c newfile.c newscn.c \
c0e8f3
+	next-files.c next-lines.c next_cfi.c peel_type.c rdwrmmap.c \
c0e8f3
+	rerequest_tag.c saridx.c scnnames.c sectiondump.c \
c0e8f3
+	show-abbrev.c show-die-info.c showptable.c strptr.c \
c0e8f3
+	system-elf-libelf-test.c test-elf_cntl_gelf_getshdr.c \
c0e8f3
+	test-flag-nobits.c test-nlist.c typeiter.c typeiter2.c \
c0e8f3
+	unit-info.c update1.c update2.c update3.c update4.c varlocs.c \
c0e8f3
+	vdsosyms.c vendorelf.c xlate_notes.c zstrptr.c
c0e8f3
 am__can_run_installinfo = \
c0e8f3
   case $$AM_UPDATE_INFO_DIR in \
c0e8f3
     n|no|NO) false;; \
c0e8f3
@@ -1405,6 +1409,7 @@
c0e8f3
 	     run-unit-info.sh run-next-cfi.sh run-next-cfi-self.sh \
c0e8f3
 	     testfile-riscv64.bz2 testfile-riscv64-s.bz2 \
c0e8f3
 	     testfile-riscv64-core.bz2 \
c0e8f3
+	     run-reverse-sections.sh run-reverse-sections-self.sh \
c0e8f3
 	     run-copyadd-sections.sh run-copymany-sections.sh \
c0e8f3
 	     run-typeiter-many.sh run-strip-test-many.sh \
c0e8f3
 	     testfile-debug-rel-ppc64-g.o.bz2 \
c0e8f3
@@ -1566,6 +1571,7 @@
c0e8f3
 elfcopy_LDADD = $(libelf)
c0e8f3
 addsections_LDADD = $(libelf)
c0e8f3
 xlate_notes_LDADD = $(libelf)
c0e8f3
+elfrdwrnop_LDADD = $(libelf)
c0e8f3
 
c0e8f3
 # We want to test the libelf header against the system elf.h header.
c0e8f3
 # Don't include any -I CPPFLAGS. Except when we install our own elf.h.
c0e8f3
@@ -1822,6 +1828,10 @@
c0e8f3
 	@rm -f elfputzdata$(EXEEXT)
c0e8f3
 	$(AM_V_CCLD)$(LINK) $(elfputzdata_OBJECTS) $(elfputzdata_LDADD) $(LIBS)
c0e8f3
 
c0e8f3
+elfrdwrnop$(EXEEXT): $(elfrdwrnop_OBJECTS) $(elfrdwrnop_DEPENDENCIES) $(EXTRA_elfrdwrnop_DEPENDENCIES) 
c0e8f3
+	@rm -f elfrdwrnop$(EXEEXT)
c0e8f3
+	$(AM_V_CCLD)$(LINK) $(elfrdwrnop_OBJECTS) $(elfrdwrnop_LDADD) $(LIBS)
c0e8f3
+
c0e8f3
 elfshphehdr$(EXEEXT): $(elfshphehdr_OBJECTS) $(elfshphehdr_DEPENDENCIES) $(EXTRA_elfshphehdr_DEPENDENCIES) 
c0e8f3
 	@rm -f elfshphehdr$(EXEEXT)
c0e8f3
 	$(AM_V_CCLD)$(LINK) $(elfshphehdr_OBJECTS) $(elfshphehdr_LDADD) $(LIBS)
c0e8f3
@@ -2086,6 +2096,7 @@
c0e8f3
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/elfgetchdr.Po@am__quote@ # am--include-marker
c0e8f3
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/elfgetzdata.Po@am__quote@ # am--include-marker
c0e8f3
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/elfputzdata.Po@am__quote@ # am--include-marker
c0e8f3
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/elfrdwrnop.Po@am__quote@ # am--include-marker
c0e8f3
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/elfshphehdr.Po@am__quote@ # am--include-marker
c0e8f3
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/elfstrmerge.Po@am__quote@ # am--include-marker
c0e8f3
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/elfstrtab.Po@am__quote@ # am--include-marker
c0e8f3
@@ -3709,6 +3720,20 @@
c0e8f3
 	--log-file $$b.log --trs-file $$b.trs \
c0e8f3
 	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
c0e8f3
 	"$$tst" $(AM_TESTS_FD_REDIRECT)
c0e8f3
+run-reverse-sections.sh.log: run-reverse-sections.sh
c0e8f3
+	@p='run-reverse-sections.sh'; \
c0e8f3
+	b='run-reverse-sections.sh'; \
c0e8f3
+	$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
c0e8f3
+	--log-file $$b.log --trs-file $$b.trs \
c0e8f3
+	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
c0e8f3
+	"$$tst" $(AM_TESTS_FD_REDIRECT)
c0e8f3
+run-reverse-sections-self.sh.log: run-reverse-sections-self.sh
c0e8f3
+	@p='run-reverse-sections-self.sh'; \
c0e8f3
+	b='run-reverse-sections-self.sh'; \
c0e8f3
+	$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
c0e8f3
+	--log-file $$b.log --trs-file $$b.trs \
c0e8f3
+	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
c0e8f3
+	"$$tst" $(AM_TESTS_FD_REDIRECT)
c0e8f3
 run-copyadd-sections.sh.log: run-copyadd-sections.sh
c0e8f3
 	@p='run-copyadd-sections.sh'; \
c0e8f3
 	b='run-copyadd-sections.sh'; \
c0e8f3
@@ -3997,6 +4022,7 @@
c0e8f3
 	-rm -f ./$(DEPDIR)/elfgetchdr.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfgetzdata.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfputzdata.Po
c0e8f3
+	-rm -f ./$(DEPDIR)/elfrdwrnop.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfshphehdr.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfstrmerge.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfstrtab.Po
c0e8f3
@@ -4147,6 +4173,7 @@
c0e8f3
 	-rm -f ./$(DEPDIR)/elfgetchdr.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfgetzdata.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfputzdata.Po
c0e8f3
+	-rm -f ./$(DEPDIR)/elfrdwrnop.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfshphehdr.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfstrmerge.Po
c0e8f3
 	-rm -f ./$(DEPDIR)/elfstrtab.Po
c0e8f3
diff --git a/tests/elfcopy.c b/tests/elfcopy.c
c0e8f3
index d457bad..4542222 100644
c0e8f3
--- a/tests/elfcopy.c
c0e8f3
+++ b/tests/elfcopy.c
c0e8f3
@@ -225,6 +225,7 @@ copy_elf (const char *in, const char *out, bool use_mmap, bool reverse_offs)
c0e8f3
 	      && shdr.sh_addralign == 1
c0e8f3
 	      && last_shdr.sh_type != SHT_NOBITS
c0e8f3
 	      && shdr.sh_type != SHT_NOBITS
c0e8f3
+	      && last_shdr.sh_offset + last_shdr.sh_size == shdr.sh_offset
c0e8f3
 	      && (phnum == 0
c0e8f3
 		  || ((shdr.sh_flags & SHF_ALLOC) == 0
c0e8f3
 		      && (last_shdr.sh_flags & SHF_ALLOC) == 0)))