Skip to content

Commit

Permalink
Set the alignment of the new LOAD segment the same as others
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoguim committed Mar 7, 2023
1 parent ea2fca7 commit 637f96b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -800,12 +800,17 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
page of other segments. */
Elf_Addr startPage = 0;
Elf_Addr firstPage = 0;
unsigned alignStartPage = getPageSize();
for (auto & phdr : phdrs) {
Elf_Addr thisPage = roundUp(rdi(phdr.p_vaddr) + rdi(phdr.p_memsz), getPageSize());
Elf_Addr thisPage = rdi(phdr.p_vaddr) + rdi(phdr.p_memsz);
if (thisPage > startPage) startPage = thisPage;
if (rdi(phdr.p_type) == PT_PHDR) firstPage = rdi(phdr.p_vaddr) - rdi(phdr.p_offset);
unsigned thisAlign = rdi(phdr.p_align);
alignStartPage = std::max(alignStartPage, thisAlign);
}

startPage = roundUp(startPage, alignStartPage);

debug("last page is 0x%llx\n", (unsigned long long) startPage);
debug("first page is 0x%llx\n", (unsigned long long) firstPage);

Expand Down Expand Up @@ -873,7 +878,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
if (!phdrs.empty() &&
rdi(lastSeg.p_type) == PT_LOAD &&
rdi(lastSeg.p_flags) == (PF_R | PF_W) &&
rdi(lastSeg.p_align) == getPageSize()) {
rdi(lastSeg.p_align) == alignStartPage) {
auto segEnd = roundUp(rdi(lastSeg.p_offset) + rdi(lastSeg.p_memsz), getPageSize());
if (segEnd == startOffset) {
auto newSz = startOffset + neededSpace - rdi(lastSeg.p_offset);
Expand All @@ -892,7 +897,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
wri(phdr.p_vaddr, wri(phdr.p_paddr, startPage));
wri(phdr.p_filesz, wri(phdr.p_memsz, neededSpace));
wri(phdr.p_flags, PF_R | PF_W);
wri(phdr.p_align, getPageSize());
wri(phdr.p_align, alignStartPage);
}

normalizeNoteSegments();
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ build_TESTS = \

TESTS = $(src_TESTS) $(build_TESTS)

EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness empty-note
EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness empty-note libnon-default-alignment.so.zip

TESTS_ENVIRONMENT = PATCHELF_DEBUG=1 STRIP=$(STRIP) OBJDUMP=$(OBJDUMP) READELF=$(READELF) OBJCOPY=$(OBJCOPY)

Expand Down
Binary file added tests/libnon-default-alignment.so.zip
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/set-rpath-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"
mkdir -p "${SCRATCH}/libsA"
mkdir -p "${SCRATCH}/libsB"
mkdir -p "${SCRATCH}/libnon-default-alignment"

cp main-scoped "${SCRATCH}/"
cp libfoo-scoped.so "${SCRATCH}/libsA/"
cp libbar-scoped.so "${SCRATCH}/libsB/"
cp ../../tests/libnon-default-alignment.so.zip "${SCRATCH}/libnon-default-alignment/"
(cd "${SCRATCH}/libnon-default-alignment/"; unzip libnon-default-alignment.so.zip)

oldRPath=$(../src/patchelf --print-rpath "${SCRATCH}"/main-scoped)
if test -z "$oldRPath"; then oldRPath="/oops"; fi
Expand Down Expand Up @@ -56,3 +59,12 @@ if test "$exitCode" != 46; then
echo "bad exit code!"
exit 1
fi

# ALL loads should have the same alignment
lib="${SCRATCH}/libnon-default-alignment/libnon-default-alignment.so"
../src/patchelf --set-rpath "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "$lib"
num_alignments=$(${READELF} -W -l "${lib}" | grep LOAD | awk '{ print $NF }' | sort -u | wc -l)
echo "$num_alignments"
if test "${num_alignments}" -ne "1"; then
exit 1
fi

0 comments on commit 637f96b

Please sign in to comment.