Resize segment mapping rewritten sections if needed

This commit is contained in:
Breno Rodrigues Guimaraes
2023-03-20 07:48:00 -03:00
committed by Jörg Thalheim
parent 6e7b82e839
commit 65cdee9044
4 changed files with 45 additions and 1 deletions

View File

@@ -1030,6 +1030,18 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
firstPage -= neededPages * getPageSize();
startOffset += neededPages * getPageSize();
} else {
Elf_Off rewrittenSectionsOffset = sizeof(Elf_Ehdr) + phdrs.size() * sizeof(Elf_Phdr);
for (auto& phdr : phdrs)
if (rdi(phdr.p_type) == PT_LOAD &&
rdi(phdr.p_offset) <= rewrittenSectionsOffset &&
rdi(phdr.p_offset) + rdi(phdr.p_filesz) > rewrittenSectionsOffset &&
rdi(phdr.p_filesz) < neededSpace)
{
wri(phdr.p_filesz, neededSpace);
wri(phdr.p_memsz, neededSpace);
break;
}
}

View File

@@ -50,6 +50,7 @@ src_TESTS = \
rename-dynamic-symbols.sh \
overlapping-segments-after-rounding.sh \
shared-rpath.sh \
short-first-segment.sh \
empty-note.sh
build_TESTS = \
@@ -58,7 +59,7 @@ build_TESTS = \
TESTS = $(src_TESTS) $(build_TESTS)
EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness empty-note \
overlapping-segments-after-rounding
overlapping-segments-after-rounding short-first-segment.gz
TESTS_ENVIRONMENT = PATCHELF_DEBUG=1 STRIP=$(STRIP) OBJDUMP=$(OBJDUMP) READELF=$(READELF) OBJCOPY=$(OBJCOPY)

BIN
tests/short-first-segment.gz Executable file

Binary file not shown.

31
tests/short-first-segment.sh Executable file
View File

@@ -0,0 +1,31 @@
#! /bin/sh -e
PATCHELF=$(readlink -f "../src/patchelf")
SCRATCH="scratch/$(basename "$0" .sh)"
READELF=${READELF:-readelf}
EXEC_NAME="short-first-segment"
if ! gzip --version >/dev/null; then
echo "skipping test: gzip not found"
exit 77
fi
if test "$(uname -i)" != x86_64 || test "$(uname)" != Linux; then
echo "skipping test: not supported on x86_64 Linux"
exit 77
fi
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"
gzip -c -d "${srcdir:?}/${EXEC_NAME}.gz" > "${SCRATCH}/${EXEC_NAME}"
cd "${SCRATCH}"
ldd "${EXEC_NAME}"
${PATCHELF} --add-rpath lalalalalalalala --output modified1 "${EXEC_NAME}"
ldd modified1
${PATCHELF} --add-needed "libXcursor.so.1" --output modified2 modified1
ldd modified2