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

Extend LOAD segments by the size of the new section #470

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
21 changes: 17 additions & 4 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,10 +922,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
Elf_Shdr & shdr(shdrs.at(i));
std::string sectionName = getSectionName(shdr);
debug("looking at section '%s'\n", sectionName.c_str());
/* !!! Why do we stop after a .dynstr section? I can't
remember! */
if ((rdi(shdr.sh_type) == SHT_PROGBITS && sectionName != ".interp")
|| prevSection == ".dynstr")
if (rdi(shdr.sh_type) == SHT_PROGBITS && sectionName != ".interp")
{
startOffset = rdi(shdr.sh_offset);
startAddr = rdi(shdr.sh_addr);
Expand Down Expand Up @@ -994,6 +991,22 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()

firstPage -= neededPages * getPageSize();
startOffset += neededPages * getPageSize();
} else {
// Calculate how many bytes are needed out of the additional pages.
Elf_Off curOff = sizeof(Elf_Ehdr) + phdrs.size() * sizeof(Elf_Phdr);
size_t extraSpace = neededSpace - curOff;

/* Find segment mapping the rewritten sections and increase its size
Also make it RW (not great) */
for (auto& segment : phdrs)
if (rdi(segment.p_type) == PT_LOAD &&
rdi(segment.p_offset) <= curOff &&
rdi(segment.p_offset) + rdi(segment.p_filesz) >= curOff)
{
wri(segment.p_filesz, rdi(segment.p_filesz) + extraSpace);
wri(segment.p_memsz, rdi(segment.p_memsz) + extraSpace);
wri(segment.p_flags, PF_R | PF_W);
}
}


Expand Down