1From 5df4791bf077127684faceeeea8bfab063e43774 Mon Sep 17 00:00:00 2001
2From: Richard Purdie <richard.purdie@linuxfoundation.org>
3Date: Wed, 3 Jun 2020 12:14:58 +0100
4Subject: [PATCH] Fix shared library corruption when rerunning patchelf
5
6When running patchelf on some existing patchelf'd binaries to change to longer
7RPATHS, ldd would report the binaries as invalid. The output of objdump -x on
8those libraryies should show the top of the .dynamic section is getting trashed,
9something like:
10
110x600000001 0x0000000000429000
120x335000 0x0000000000335000
130xc740 0x000000000000c740
140x1000 0x0000000000009098
15SONAME libglib-2.0.so.0
16
17(which should be RPATH and DT_NEEDED entries)
18
19This was tracked down to the code which injects the PT_LOAD section.
20
21The issue is that if the program headers were previously relocated to the end
22of the file which was how patchelf operated previously, the relocation code
23wouldn't work properly on a second run as it now assumes they're located after
24the elf header. This change forces them back to immediately follow the elf
25header which is where the code has made space for them.
26
27Should fix https://github.com/NixOS/patchelf/issues/170
28and https://github.com/NixOS/patchelf/issues/192
29
30Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
31
32Fetch from: https://github.com/NixOS/patchelf/commit/ad5f1f078b716802dfb8f7226cb1d5c720348a78
33
34Backported to v0.9
35
36Signed-off-by: Conrad Ratschan <conrad.ratschan@rockwellcollins.com>
37---
38 src/patchelf.cc | 1 +
39 1 file changed, 1 insertion(+)
40
41diff --git a/src/patchelf.cc b/src/patchelf.cc
42index c2147af..1224a89 100644
43--- a/src/patchelf.cc
44+++ b/src/patchelf.cc
45@@ -706,6 +706,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
46
47
48     /* Add a segment that maps the replaced sections into memory. */
49+    wri(hdr->e_phoff, sizeof(Elf_Ehdr));
50     phdrs.resize(rdi(hdr->e_phnum) + 1);
51     wri(hdr->e_phnum, rdi(hdr->e_phnum) + 1);
52     Elf_Phdr & phdr = phdrs[rdi(hdr->e_phnum) - 1];
53--
542.17.1
55
56