Blame SOURCES/elfutils-0.176-elf-update.patch

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