Skip to content

Commit

Permalink
Merge #447
Browse files Browse the repository at this point in the history
447: Split segment size fix r=Mic92 a=otherjason



Co-authored-by: Jason <otherjason@nodomain.com>
  • Loading branch information
bors[bot] and Jason committed Jan 10, 2023
2 parents c6f4069 + 3a6d771 commit 583fd5a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ static uint64_t roundUp(uint64_t n, uint64_t m)


template<ElfFileParams>
void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, size_t startOffset)
void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, size_t startOffset, size_t extraBytes)
{
assert(startOffset >= sizeof(Elf_Ehdr));

Expand Down Expand Up @@ -512,7 +512,7 @@ void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, size_t start
wri(phdr.p_offset, phdrs.at(splitIndex).p_offset - splitShift - shift);
wri(phdr.p_paddr, phdrs.at(splitIndex).p_paddr - splitShift - shift);
wri(phdr.p_vaddr, phdrs.at(splitIndex).p_vaddr - splitShift - shift);
wri(phdr.p_filesz, wri(phdr.p_memsz, splitShift + shift));
wri(phdr.p_filesz, wri(phdr.p_memsz, splitShift + extraBytes));
wri(phdr.p_flags, PF_R | PF_W);
wri(phdr.p_align, getPageSize());
}
Expand Down Expand Up @@ -912,12 +912,14 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
neededSpace += sizeof(Elf_Phdr);
debug("needed space is %d\n", neededSpace);

unsigned int neededPages = roundUp(neededSpace - startOffset, getPageSize()) / getPageSize();
/* Calculate how many bytes are needed out of the additional pages. */
size_t extraSpace = neededSpace - startOffset;
unsigned int neededPages = roundUp(extraSpace, getPageSize()) / getPageSize();
debug("needed pages is %d\n", neededPages);
if (neededPages * getPageSize() > firstPage)
error("virtual address space underrun!");

shiftFile(neededPages, startOffset);
shiftFile(neededPages, startOffset, extraSpace);

firstPage -= neededPages * getPageSize();
startOffset += neededPages * getPageSize();
Expand Down
2 changes: 1 addition & 1 deletion src/patchelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ElfFile

void sortShdrs();

void shiftFile(unsigned int extraPages, size_t sizeOffset);
void shiftFile(unsigned int extraPages, size_t sizeOffset, size_t extraBytes);

std::string getSectionName(const Elf_Shdr & shdr) const;

Expand Down

0 comments on commit 583fd5a

Please sign in to comment.