Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize segment mapping rewritten sections if needed #482 #485

Merged
merged 1 commit into from
Apr 23, 2023
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
12 changes: 12 additions & 0 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \
Expand All @@ -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)

Expand Down
Binary file added tests/short-first-segment.gz
Binary file not shown.
31 changes: 31 additions & 0 deletions tests/short-first-segment.sh
Original file line number Diff line number Diff line change
@@ -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