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