Skip to content

Commit ac212d0

Browse files
authored
Merge pull request #475 from brenoguim/breno.474
Use the largest segment alignment for libraries requiring non-standard alignments #474
2 parents ea2fca7 + 80f6866 commit ac212d0

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
rm -f dist/*
9090
cd patchelf-*
9191
./configure --prefix /patchelf
92-
make check
92+
make check || (cat tests/test-suite.log; exit 1)
9393
make install-strip
9494
cd -
9595
tar -czf ./dist/patchelf-\$(cat patchelf-*/version)-\$(uname -m).tar.gz -C /patchelf .

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ check_DATA = libbig-dynstr.debug
119119
# - with libtool, it is difficult to control options
120120
# - with libtool, it is not possible to compile convenience *dynamic* libraries :-(
121121
check_PROGRAMS += libfoo.so libfoo-scoped.so libbar.so libbar-scoped.so libsimple.so libsimple-execstack.so libbuildid.so libtoomanystrtab.so \
122-
phdr-corruption.so many-syms-main libmany-syms.so
122+
phdr-corruption.so many-syms-main libmany-syms.so liboveralign.so
123123

124124
libbuildid_so_SOURCES = simple.c
125125
libbuildid_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,--build-id
@@ -143,6 +143,9 @@ libbar_scoped_so_LDFLAGS = $(LDFLAGS_sharedlib)
143143
libsimple_so_SOURCES = simple.c
144144
libsimple_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-z,noexecstack
145145

146+
liboveralign_so_SOURCES = simple.c
147+
liboveralign_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-z,max-page-size=0x10000
148+
146149
libsimple_execstack_so_SOURCES = simple.c
147150
libsimple_execstack_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-z,execstack
148151

tests/set-rpath-library.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mkdir -p "${SCRATCH}/libsB"
1414
cp main-scoped "${SCRATCH}/"
1515
cp libfoo-scoped.so "${SCRATCH}/libsA/"
1616
cp libbar-scoped.so "${SCRATCH}/libsB/"
17+
cp liboveralign.so "${SCRATCH}/"
1718

1819
oldRPath=$(../src/patchelf --print-rpath "${SCRATCH}"/main-scoped)
1920
if test -z "$oldRPath"; then oldRPath="/oops"; fi
@@ -56,3 +57,12 @@ if test "$exitCode" != 46; then
5657
echo "bad exit code!"
5758
exit 1
5859
fi
60+
61+
# ALL loads should have the same alignment
62+
lib="${SCRATCH}/liboveralign.so"
63+
../src/patchelf --set-rpath "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "$lib"
64+
num_alignments=$(${READELF} -W -l "${lib}" | awk '/LOAD/ { print $NF }' | sort -u | wc -l)
65+
echo "$num_alignments"
66+
if test "${num_alignments}" -ne "1"; then
67+
exit 1
68+
fi

0 commit comments

Comments
 (0)