Skip to content

Commit 0e33894

Browse files
pablogsalMic92
authored andcommitted
Fix alignment problem when rewriting sections
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.
1 parent d985189 commit 0e33894

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/patchelf.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
847847
neededSpace += headerTableSpace;
848848
debug("needed space is %d\n", neededSpace);
849849

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

852852
// In older version of binutils (2.30), readelf would check if the dynamic
853853
// section segment is strictly smaller than the file (and not same size).
@@ -883,7 +883,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
883883
rdi(lastSeg.p_type) == PT_LOAD &&
884884
rdi(lastSeg.p_flags) == (PF_R | PF_W) &&
885885
rdi(lastSeg.p_align) == alignStartPage) {
886-
auto segEnd = roundUp(rdi(lastSeg.p_offset) + rdi(lastSeg.p_memsz), getPageSize());
886+
auto segEnd = roundUp(rdi(lastSeg.p_offset) + rdi(lastSeg.p_memsz), alignStartPage);
887887
if (segEnd == startOffset) {
888888
auto newSz = startOffset + neededSpace - rdi(lastSeg.p_offset);
889889
wri(lastSeg.p_filesz, wri(lastSeg.p_memsz, newSz));
@@ -902,6 +902,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
902902
wri(phdr.p_filesz, wri(phdr.p_memsz, neededSpace));
903903
wri(phdr.p_flags, PF_R | PF_W);
904904
wri(phdr.p_align, alignStartPage);
905+
assert(startPage % alignStartPage == startOffset % alignStartPage);
905906
}
906907

907908
normalizeNoteSegments();

0 commit comments

Comments
 (0)