Skip to content

Commit

Permalink
Resize segment mapping rewritten sections if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoguim authored and Mic92 committed Apr 23, 2023
1 parent 6e7b82e commit 65cdee9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
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

0 comments on commit 65cdee9

Please sign in to comment.