Skip to content

Fix assertion failure when setting the interpreter of a debug-only executable #595

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,6 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
Stop when we reach an irreplacable section (such as one of type
SHT_PROGBITS). These cannot be moved in virtual address space
since that would invalidate absolute references to them. */
assert(lastReplaced + 1 < shdrs.size()); /* !!! I'm lazy. */
size_t startOffset = rdi(shdrs.at(lastReplaced + 1).sh_offset);
Elf_Addr startAddr = rdi(shdrs.at(lastReplaced + 1).sh_addr);
std::string prevSection;
for (unsigned int i = 1; i <= lastReplaced; ++i) {
Elf_Shdr & shdr(shdrs.at(i));
Expand All @@ -1010,8 +1007,6 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
if ((rdi(shdr.sh_type) == SHT_PROGBITS && sectionName != ".interp")
|| prevSection == ".dynstr")
{
startOffset = rdi(shdr.sh_offset);
startAddr = rdi(shdr.sh_addr);
lastReplaced = i - 1;
break;
}
Expand All @@ -1022,6 +1017,13 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
prevSection = std::move(sectionName);
}

while (lastReplaced < shdrs.size() && shdrs.at(lastReplaced).sh_type == SHT_NOBITS)
++lastReplaced;

assert(lastReplaced + 1 < shdrs.size()); /* !!! I'm lazy. */
size_t startOffset = rdi(shdrs.at(lastReplaced + 1).sh_offset);
Elf_Addr startAddr = rdi(shdrs.at(lastReplaced + 1).sh_addr);

debug("first reserved offset/addr is 0x%x/0x%llx\n",
startOffset, (unsigned long long) startAddr);

Expand Down Expand Up @@ -1238,7 +1240,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
section. Note that not all executables have .dynamic sections
(e.g., those produced by klibc's klcc). */
auto shdrDynamic = tryFindSectionHeader(".dynamic");
if (shdrDynamic) {
if (shdrDynamic && rdi((*shdrDynamic).get().sh_type) != SHT_NOBITS) {
auto dyn_table = (Elf_Dyn *) (fileContents->data() + rdi((*shdrDynamic).get().sh_offset));
unsigned int d_tag;
for (auto dyn = dyn_table; (d_tag = rdi(dyn->d_tag)) != DT_NULL; dyn++)
Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ src_TESTS = \
shared-rpath.sh \
short-first-segment.sh \
empty-note.sh \
set-interpreter-same.sh
set-interpreter-same.sh \
debug-interp.sh

build_TESTS = \
$(no_rpath_arch_TESTS)
Expand Down
13 changes: 13 additions & 0 deletions tests/debug-interp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename "$0" .sh)
OBJCOPY=${OBJCOPY:-objcopy}

rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp simple "${SCRATCH}/"

${OBJCOPY} --only-keep-debug "${SCRATCH}/simple" "${SCRATCH}/simple.debug"

# Check if patchelf handles debug-only executables
../src/patchelf --set-interpreter /oops "${SCRATCH}/simple.debug"