Skip to content

Commit 884eccc

Browse files
committed
Fix endianness issues for powerpc PIE
Previously when running `patchelf --set-rpath "/usr/sbin" my_bin` on a PIE ppc32 binary that had no RPATH a few issues were encountered. This commit fixes: 1. The PT_PHDR being sorted improperly due to the type being read in incorrect endianness 2. The aligment being set to default 0x1000 due to the machine arch being read in incorrect endianness 3. The interpreter being clobbered due to the replace sections routine reading sh_offset and sh_size in incorrect endianness 4. The PHDR segment having an incorrect virt and phys address due to reading the e_phoff in the incorrect endianness This also fixes a read of the shdr.sh_type in writeReplacedSections but this was not encountered during testing.
1 parent d2e81f0 commit 884eccc

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/patchelf.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ class ElfFile
136136
bool operator ()(const Elf_Phdr & x, const Elf_Phdr & y)
137137
{
138138
// A PHDR comes before everything else.
139-
if (y.p_type == PT_PHDR) return false;
140-
if (x.p_type == PT_PHDR) return true;
139+
if (elfFile->rdi(y.p_type) == PT_PHDR) return false;
140+
if (elfFile->rdi(x.p_type) == PT_PHDR) return true;
141141

142142
// Sort non-PHDRs by address.
143143
return elfFile->rdi(x.p_paddr) < elfFile->rdi(y.p_paddr);
@@ -453,7 +453,7 @@ unsigned int ElfFile<ElfFileParamNames>::getPageSize() const
453453
// Architectures (and ABIs) can have different minimum section alignment
454454
// requirements. There is no authoritative list of these values. The
455455
// current list is extracted from GNU gold's source code (abi_pagesize).
456-
switch (hdr->e_machine) {
456+
switch (rdi(hdr->e_machine)) {
457457
case EM_SPARC:
458458
case EM_MIPS:
459459
case EM_PPC:
@@ -665,7 +665,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
665665
for (auto & i : replacedSections) {
666666
std::string sectionName = i.first;
667667
Elf_Shdr & shdr = findSection(sectionName);
668-
if (shdr.sh_type != SHT_NOBITS)
668+
if (rdi(shdr.sh_type) != SHT_NOBITS)
669669
memset(contents + rdi(shdr.sh_offset), 'X', rdi(shdr.sh_size));
670670
}
671671

@@ -778,9 +778,9 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
778778
/* Some sections may already be replaced so account for that */
779779
unsigned int i = 1;
780780
Elf_Addr pht_size = sizeof(Elf_Ehdr) + (phdrs.size() + num_notes + 1)*sizeof(Elf_Phdr);
781-
while( shdrs[i].sh_offset <= pht_size && i < rdi(hdr->e_shnum) ) {
781+
while( rdi(shdrs[i].sh_offset) <= pht_size && i < rdi(hdr->e_shnum) ) {
782782
if (not haveReplacedSection(getSectionName(shdrs[i])))
783-
replaceSection(getSectionName(shdrs[i]), shdrs[i].sh_size);
783+
replaceSection(getSectionName(shdrs[i]), rdi(shdrs[i].sh_size));
784784
i++;
785785
}
786786

@@ -835,7 +835,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
835835
assert(curOff == startOffset + neededSpace);
836836

837837
/* Write out the updated program and section headers */
838-
rewriteHeaders(hdr->e_phoff);
838+
rewriteHeaders(rdi(hdr->e_phoff));
839839
}
840840

841841

0 commit comments

Comments
 (0)