Skip to content

Commit

Permalink
Avoid overlapping program header table with section header table NixO…
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoguim committed Feb 18, 2023
1 parent 5908e16 commit 9971836
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,14 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
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 ) {
Elf_Addr firstFreeOffset = sizeof(Elf_Ehdr) + (phdrs.size() + num_notes + 1)*sizeof(Elf_Phdr);
if (rdi(hdr()->e_shoff) < firstFreeOffset) {
// The new program header table will overlap with the section header table.
// Put the section header table right after it, and account for its size.
wri(hdr()->e_shoff, firstFreeOffset);
firstFreeOffset += rdi(hdr()->e_shnum) * rdi(hdr()->e_shentsize);
}
while( i < rdi(hdr()->e_shnum) && rdi(shdrs.at(i).sh_offset) <= firstFreeOffset ) {
if (not haveReplacedSection(getSectionName(shdrs.at(i))))
replaceSection(getSectionName(shdrs.at(i)), rdi(shdrs.at(i).sh_size));
i++;
Expand Down

0 comments on commit 9971836

Please sign in to comment.