Skip to content

Commit

Permalink
Fix alignment problem when rewriting sections
Browse files Browse the repository at this point in the history
After commit ac212d0 the code to
rewrite alignment section has been changed to use the largest alignment
in the list of segments instead of the alignment that it's retrieved
using getPageSize().

Unfortunately the code didn't update the offset as well to keep the
invariant p_vaddr % alignment == p_offset % alignment.
  • Loading branch information
pablogsal authored and Mic92 committed Nov 18, 2024
1 parent d985189 commit 0e33894
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
neededSpace += headerTableSpace;
debug("needed space is %d\n", neededSpace);

Elf_Off startOffset = roundUp(fileContents->size(), getPageSize());
Elf_Off startOffset = roundUp(fileContents->size(), alignStartPage);

// In older version of binutils (2.30), readelf would check if the dynamic
// section segment is strictly smaller than the file (and not same size).
Expand Down Expand Up @@ -883,7 +883,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
rdi(lastSeg.p_type) == PT_LOAD &&
rdi(lastSeg.p_flags) == (PF_R | PF_W) &&
rdi(lastSeg.p_align) == alignStartPage) {
auto segEnd = roundUp(rdi(lastSeg.p_offset) + rdi(lastSeg.p_memsz), getPageSize());
auto segEnd = roundUp(rdi(lastSeg.p_offset) + rdi(lastSeg.p_memsz), alignStartPage);
if (segEnd == startOffset) {
auto newSz = startOffset + neededSpace - rdi(lastSeg.p_offset);
wri(lastSeg.p_filesz, wri(lastSeg.p_memsz, newSz));
Expand All @@ -902,6 +902,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
wri(phdr.p_filesz, wri(phdr.p_memsz, neededSpace));
wri(phdr.p_flags, PF_R | PF_W);
wri(phdr.p_align, alignStartPage);
assert(startPage % alignStartPage == startOffset % alignStartPage);
}

normalizeNoteSegments();
Expand Down

0 comments on commit 0e33894

Please sign in to comment.