diff --git a/src/patchelf.cc b/src/patchelf.cc index 82b4b46c..86429c41 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -843,7 +843,13 @@ void ElfFile::rewriteSectionsLibrary() neededSpace += headerTableSpace; debug("needed space is %d\n", neededSpace); - Elf_Off startOffset = roundUp(fileContents->size(), getPageSize()); + /* glibc earlier than 2.35 requires that the LOAD segment satisfies + (p_vaddr mod p_align) == (p_offset mod p_align). + The ELF specification requires that loadable process segments satisfy + (p_vaddr mod pagesize) == (p_offset mod pagesize), so glibc is probably + wrong, but here startOffset is calculated according to p_align for + compatibility. */ + 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). @@ -879,7 +885,7 @@ void ElfFile::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));