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