diff --git a/src/patchelf.cc b/src/patchelf.cc index c62d7b69..fd4ebcab 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -136,8 +136,8 @@ class ElfFile bool operator ()(const Elf_Phdr & x, const Elf_Phdr & y) { // A PHDR comes before everything else. - if (y.p_type == PT_PHDR) return false; - if (x.p_type == PT_PHDR) return true; + if (elfFile->rdi(y.p_type) == PT_PHDR) return false; + if (elfFile->rdi(x.p_type) == PT_PHDR) return true; // Sort non-PHDRs by address. return elfFile->rdi(x.p_paddr) < elfFile->rdi(y.p_paddr); @@ -453,7 +453,7 @@ unsigned int ElfFile::getPageSize() const // Architectures (and ABIs) can have different minimum section alignment // requirements. There is no authoritative list of these values. The // current list is extracted from GNU gold's source code (abi_pagesize). - switch (hdr->e_machine) { + switch (rdi(hdr->e_machine)) { case EM_SPARC: case EM_MIPS: case EM_PPC: @@ -665,7 +665,7 @@ void ElfFile::writeReplacedSections(Elf_Off & curOff, for (auto & i : replacedSections) { std::string sectionName = i.first; Elf_Shdr & shdr = findSection(sectionName); - if (shdr.sh_type != SHT_NOBITS) + if (rdi(shdr.sh_type) != SHT_NOBITS) memset(contents + rdi(shdr.sh_offset), 'X', rdi(shdr.sh_size)); } @@ -778,9 +778,9 @@ void ElfFile::rewriteSectionsLibrary() /* Some sections may already be replaced so account for that */ unsigned int i = 1; Elf_Addr pht_size = sizeof(Elf_Ehdr) + (phdrs.size() + num_notes + 1)*sizeof(Elf_Phdr); - while( shdrs[i].sh_offset <= pht_size && i < rdi(hdr->e_shnum) ) { + while( rdi(shdrs[i].sh_offset) <= pht_size && i < rdi(hdr->e_shnum) ) { if (not haveReplacedSection(getSectionName(shdrs[i]))) - replaceSection(getSectionName(shdrs[i]), shdrs[i].sh_size); + replaceSection(getSectionName(shdrs[i]), rdi(shdrs[i].sh_size)); i++; } @@ -835,7 +835,7 @@ void ElfFile::rewriteSectionsLibrary() assert(curOff == startOffset + neededSpace); /* Write out the updated program and section headers */ - rewriteHeaders(hdr->e_phoff); + rewriteHeaders(rdi(hdr->e_phoff)); } diff --git a/tests/Makefile.am b/tests/Makefile.am index dbb7580e..db297386 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -25,6 +25,7 @@ src_TESTS = \ force-rpath.sh \ plain-needed.sh \ output-flag.sh \ + no-rpath-pie-powerpc.sh \ build-id.sh build_TESTS = \ diff --git a/tests/no-rpath-pie-powerpc.sh b/tests/no-rpath-pie-powerpc.sh new file mode 100755 index 00000000..5bdb7857 --- /dev/null +++ b/tests/no-rpath-pie-powerpc.sh @@ -0,0 +1,54 @@ +#! /bin/sh -e +set -x +SCRATCH=scratch/no-rpath-pie-powerpc + +no_rpath_bin="${srcdir}/no-rpath-prebuild/no-rpath-pie-powerpc" + +if [ ! -f $no_rpath_bin ]; then + echo "no 'no-rpath' binary for '$ARCH' in '${srcdir}/no-rpath-prebuild'" + exit 1 +fi + +rm -rf ${SCRATCH} +mkdir -p ${SCRATCH} + +cp $no_rpath_bin ${SCRATCH}/no-rpath + +oldRPath=$(../src/patchelf --print-rpath ${SCRATCH}/no-rpath) +if test -n "$oldRPath"; then exit 1; fi +../src/patchelf \ + --set-interpreter "$(../src/patchelf --print-interpreter ../src/patchelf)" \ + --set-rpath /foo:/bar:/xxxxxxxxxxxxxxx ${SCRATCH}/no-rpath + +newRPath=$(../src/patchelf --print-rpath ${SCRATCH}/no-rpath) +if ! echo "$newRPath" | grep -q '/foo:/bar'; then + echo "incomplete RPATH" + exit 1 +fi + +# Tests for powerpc PIE endianness regressions +readelfData=$(readelf -l ${SCRATCH}/no-rpath 2>&1) + +if [ $(echo "$readelfData" | grep --count "PHDR") != 1 ]; then + # Triggered if PHDR errors appear on stderr + echo "Unexpected number of occurences of PHDR in readelf results" + exit 1 +fi + +virtAddr=$(echo "$readelfData" | grep "PHDR" | awk '{print $3}') +if [ "$virtAddr" != "0x00000034" ]; then + # Triggered if the virtual address is the incorrect endianness + echo "Unexpected virt addr, expected [0x00000034] got [$virtAddr]" + exit 1 +fi + +echo "$readelfData" | grep "LOAD" | while read -r line ; do + align=$(echo "$line" | awk '{print $NF}') + if [ "$align" != "0x10000" ]; then + # Triggered if the target arch was not detected properly + echo "Unexpected Align for LOAD segment, expected [0x10000] got [$align]" + echo "Load segment: [$line]" + exit 1 + fi +done + diff --git a/tests/no-rpath-prebuild/no-rpath-pie-powerpc b/tests/no-rpath-prebuild/no-rpath-pie-powerpc new file mode 100755 index 00000000..09462b82 Binary files /dev/null and b/tests/no-rpath-prebuild/no-rpath-pie-powerpc differ