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 committed Sep 5, 2024
1 parent a0f5433 commit a827949
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 @@ -846,7 +846,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 @@ -882,7 +882,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 @@ -901,6 +901,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 a827949

Please sign in to comment.