0010-Fix-endianness-issues-for-powerpc-PIE.patch 3.0 KB

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