Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -453,7 +453,7 @@ unsigned int ElfFile<ElfFileParamNames>::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:
Expand Down Expand Up @@ -665,7 +665,7 @@ void ElfFile<ElfFileParamNames>::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));
}

Expand Down Expand Up @@ -778,9 +778,9 @@ void ElfFile<ElfFileParamNames>::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++;
}

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

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


Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \
Expand Down
54 changes: 54 additions & 0 deletions tests/no-rpath-pie-powerpc.sh
Original file line number Diff line number Diff line change
@@ -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

Binary file added tests/no-rpath-prebuild/no-rpath-pie-powerpc
Binary file not shown.