Skip to content

Commit 637f96b

Browse files
committed
Set the alignment of the new LOAD segment the same as others
1 parent ea2fca7 commit 637f96b

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

src/patchelf.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,17 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
800800
page of other segments. */
801801
Elf_Addr startPage = 0;
802802
Elf_Addr firstPage = 0;
803+
unsigned alignStartPage = getPageSize();
803804
for (auto & phdr : phdrs) {
804-
Elf_Addr thisPage = roundUp(rdi(phdr.p_vaddr) + rdi(phdr.p_memsz), getPageSize());
805+
Elf_Addr thisPage = rdi(phdr.p_vaddr) + rdi(phdr.p_memsz);
805806
if (thisPage > startPage) startPage = thisPage;
806807
if (rdi(phdr.p_type) == PT_PHDR) firstPage = rdi(phdr.p_vaddr) - rdi(phdr.p_offset);
808+
unsigned thisAlign = rdi(phdr.p_align);
809+
alignStartPage = std::max(alignStartPage, thisAlign);
807810
}
808811

812+
startPage = roundUp(startPage, alignStartPage);
813+
809814
debug("last page is 0x%llx\n", (unsigned long long) startPage);
810815
debug("first page is 0x%llx\n", (unsigned long long) firstPage);
811816

@@ -873,7 +878,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
873878
if (!phdrs.empty() &&
874879
rdi(lastSeg.p_type) == PT_LOAD &&
875880
rdi(lastSeg.p_flags) == (PF_R | PF_W) &&
876-
rdi(lastSeg.p_align) == getPageSize()) {
881+
rdi(lastSeg.p_align) == alignStartPage) {
877882
auto segEnd = roundUp(rdi(lastSeg.p_offset) + rdi(lastSeg.p_memsz), getPageSize());
878883
if (segEnd == startOffset) {
879884
auto newSz = startOffset + neededSpace - rdi(lastSeg.p_offset);
@@ -892,7 +897,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
892897
wri(phdr.p_vaddr, wri(phdr.p_paddr, startPage));
893898
wri(phdr.p_filesz, wri(phdr.p_memsz, neededSpace));
894899
wri(phdr.p_flags, PF_R | PF_W);
895-
wri(phdr.p_align, getPageSize());
900+
wri(phdr.p_align, alignStartPage);
896901
}
897902

898903
normalizeNoteSegments();

tests/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ build_TESTS = \
5555

5656
TESTS = $(src_TESTS) $(build_TESTS)
5757

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

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

tests/libnon-default-alignment.so.zip

172 KB
Binary file not shown.

tests/set-rpath-library.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ rm -rf "${SCRATCH}"
1010
mkdir -p "${SCRATCH}"
1111
mkdir -p "${SCRATCH}/libsA"
1212
mkdir -p "${SCRATCH}/libsB"
13+
mkdir -p "${SCRATCH}/libnon-default-alignment"
1314

1415
cp main-scoped "${SCRATCH}/"
1516
cp libfoo-scoped.so "${SCRATCH}/libsA/"
1617
cp libbar-scoped.so "${SCRATCH}/libsB/"
18+
cp ../../tests/libnon-default-alignment.so.zip "${SCRATCH}/libnon-default-alignment/"
19+
(cd "${SCRATCH}/libnon-default-alignment/"; unzip libnon-default-alignment.so.zip)
1720

1821
oldRPath=$(../src/patchelf --print-rpath "${SCRATCH}"/main-scoped)
1922
if test -z "$oldRPath"; then oldRPath="/oops"; fi
@@ -56,3 +59,12 @@ if test "$exitCode" != 46; then
5659
echo "bad exit code!"
5760
exit 1
5861
fi
62+
63+
# ALL loads should have the same alignment
64+
lib="${SCRATCH}/libnon-default-alignment/libnon-default-alignment.so"
65+
../src/patchelf --set-rpath "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "$lib"
66+
num_alignments=$(${READELF} -W -l "${lib}" | grep LOAD | awk '{ print $NF }' | sort -u | wc -l)
67+
echo "$num_alignments"
68+
if test "${num_alignments}" -ne "1"; then
69+
exit 1
70+
fi

0 commit comments

Comments
 (0)