1From c61c2960d782c67566790b210163ff9c799f018a Mon Sep 17 00:00:00 2001 2From: Conrad Ratschan <ratschance@gmail.com> 3Date: Sat, 3 Oct 2020 20:17:24 -0500 4Subject: [PATCH] Fix endianness issues for powerpc PIE 5 6Previously when running `patchelf --set-rpath "/usr/sbin" my_bin` on a 7PIE ppc32 binary that had no RPATH a few issues were encountered. 8 9This commit fixes: 10 111. The PT_PHDR being sorted improperly due to the type being read in 12 incorrect endianness 13 143. The interpreter being clobbered due to the replace sections routine 15 reading sh_offset and sh_size in incorrect endianness 16 174. The PHDR segment having an incorrect virt and phys address due to 18 reading the e_phoff in the incorrect endianness 19 20This also fixes a read of the shdr.sh_type in writeReplacedSections but 21this was not encountered during testing. 22 23Fetch from: https://github.com/NixOS/patchelf/commit/884eccc4f061a3dbdbe63a4c73f1cc9bbf77fa7d 24 25Backported to v0.9. Removed item 2 from the fix list as it is not 26applicable to v0.9. 27 28Signed-off-by: Conrad Ratschan <conrad.ratschan@rockwellcollins.com> 29--- 30 src/patchelf.cc | 12 ++++++------ 31 1 file changed, 6 insertions(+), 6 deletions(-) 32 33diff --git a/src/patchelf.cc b/src/patchelf.cc 34index fa2945e..e60b17c 100644 35--- a/src/patchelf.cc 36+++ b/src/patchelf.cc 37@@ -173,8 +173,8 @@ private: 38 ElfFile * elfFile; 39 bool operator ()(const Elf_Phdr & x, const Elf_Phdr & y) 40 { 41- if (x.p_type == PT_PHDR) return true; 42- if (y.p_type == PT_PHDR) return false; 43+ if (elfFile->rdi(x.p_type) == PT_PHDR) return true; 44+ if (elfFile->rdi(y.p_type) == PT_PHDR) return false; 45 return elfFile->rdi(x.p_paddr) < elfFile->rdi(y.p_paddr); 46 } 47 }; 48@@ -586,7 +586,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff, 49 { 50 string sectionName = i->first; 51 Elf_Shdr & shdr = findSection(sectionName); 52- if (shdr.sh_type != SHT_NOBITS) 53+ if (rdi(shdr.sh_type) != SHT_NOBITS) 54 memset(contents + rdi(shdr.sh_offset), 'X', rdi(shdr.sh_size)); 55 } 56 57@@ -667,9 +667,9 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary() 58 /* Some sections may already be replaced so account for that */ 59 unsigned int i = 1; 60 Elf_Addr pht_size = sizeof(Elf_Ehdr) + (phdrs.size() + 1)*sizeof(Elf_Phdr); 61- while( shdrs[i].sh_offset <= pht_size && i < rdi(hdr->e_shnum) ) { 62+ while( rdi(shdrs[i].sh_offset) <= pht_size && i < rdi(hdr->e_shnum) ) { 63 if (not haveReplacedSection(getSectionName(shdrs[i]))) 64- replaceSection(getSectionName(shdrs[i]), shdrs[i].sh_size); 65+ replaceSection(getSectionName(shdrs[i]), rdi(shdrs[i].sh_size)); 66 i++; 67 } 68 69@@ -723,7 +723,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary() 70 assert(curOff == startOffset + neededSpace); 71 72 /* Write out the updated program and section headers */ 73- rewriteHeaders(hdr->e_phoff); 74+ rewriteHeaders(rdi(hdr->e_phoff)); 75 } 76 77 78-- 792.17.1 80 81