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