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

Compute program header flags in rewriteSectionsLibrary #467

Draft
wants to merge 2 commits 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
20 changes: 14 additions & 6 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -787,24 +787,22 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
/* When normalizing note segments we will in the worst case be adding
1 program header for each SHT_NOTE section. */
unsigned int num_notes = std::count_if(shdrs.begin(), shdrs.end(),
[this](Elf_Shdr shdr) { return rdi(shdr.sh_type) == SHT_NOTE; });
[this](const Elf_Shdr & shdr) { return rdi(shdr.sh_type) == SHT_NOTE; });

/* Because we're adding a new section header, we're necessarily increasing
the size of the program header table. This can cause the first section
to overlap the program header table in memory; we need to shift the first
few segments to someplace else. */
/* Some sections may already be replaced so account for that */
unsigned int i = 1;
Elf_Addr pht_size = sizeof(Elf_Ehdr) + (phdrs.size() + num_notes + 1)*sizeof(Elf_Phdr);
while( i < rdi(hdr()->e_shnum) && rdi(shdrs.at(i).sh_offset) <= pht_size ) {
for (unsigned int i = 1; i < rdi(hdr()->e_shnum) && rdi(shdrs.at(i).sh_offset) <= pht_size; i++) {
if (not haveReplacedSection(getSectionName(shdrs.at(i))))
replaceSection(getSectionName(shdrs.at(i)), rdi(shdrs.at(i).sh_size));
i++;
}

/* Compute the total space needed for the replaced sections */
off_t neededSpace = 0;
for (auto & s : replacedSections)
for (const auto & s : replacedSections)
neededSpace += roundUp(s.second.size(), sectionAlignment);
debug("needed space is %d\n", neededSpace);

Expand Down Expand Up @@ -844,7 +842,17 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
wri(phdr.p_offset, startOffset);
wri(phdr.p_vaddr, wri(phdr.p_paddr, startPage));
wri(phdr.p_filesz, wri(phdr.p_memsz, neededSpace));
wri(phdr.p_flags, PF_R | PF_W);
{
bool need_write = false, need_exec = false;
for (const auto & rs : replacedSections) {
const auto flags = findSectionHeader(rs.first).sh_flags;
if (flags & SHF_WRITE)
need_write = true;
if (flags & SHF_EXECINSTR)
need_exec = true;
}
wri(phdr.p_flags, PF_R | (need_write ? PF_W : 0) | (need_exec ? PF_X : 0));
Comment on lines +847 to +854
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to write this would be:

auto flagUnion = PF_R;
for (const auto& rs : replacedSections) {
    const auto flags = findSectionHeader(rs.first).sh_flags;
    flagUnion |= (flags & SHF_WRITE) ? PF_W : 0;
    flagUnion |= (flags & SHF_EXECINSTR) ? PF_X : 0;
}
wri(phdr.p_flags, flagUnion);

But that's style, feel free to drop.

}
wri(phdr.p_align, getPageSize());


Expand Down