Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -264,6 +264,8 @@ extern LinkerScript *Script; +void fill(uint8_t *Buf, size_t Size, uint32_t Filler); + } // end namespace elf } // end namespace lld Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -307,7 +307,7 @@ // Fill [Buf, Buf + Size) with Filler. // This is used for linker script "=fillexp" command. -static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { +void elf::fill(uint8_t *Buf, size_t Size, uint32_t Filler) { size_t I = 0; for (; I + 4 < Size; I += 4) memcpy(Buf + I, &Filler, 4); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1830,12 +1830,12 @@ Sec->writeTo(Buf + Sec->Offset); } -static void fillTrapInstr(uint8_t *I, uint8_t *End) { - for (; I + 4 < End; I += 4) - memcpy(I, &Target->TrapInstr, 4); +static void fillPage(uint8_t *Buf, uint64_t Offset, uint32_t Filler) { + uint64_t Begin = alignDown(Offset, Target->PageSize); + uint64_t End = alignTo(Offset, Target->PageSize); + fill(Buf + Begin, End - Begin, Filler); } - // Fill the first and the last page of executable segments with trap // instructions instead of leaving them as zero. Even though it is not required // by any standard , it is in general a good thing to do for security reasons. @@ -1851,10 +1851,8 @@ // We only fill the first and the last page of the segment because the // middle part will be overwritten by output sections. - fillTrapInstr(Buf + alignDown(P->p_offset, Target->PageSize), - Buf + alignTo(P->p_offset, Target->PageSize)); - fillTrapInstr(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize), - Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize)); + fillPage(Buf, P->p_offset, Target->TrapInstr); + fillPage(Buf, P->p_offset + P->p_filesz, Target->TrapInstr); } PhdrEntry *LastRX = nullptr; Index: test/ELF/build-id.s =================================================================== --- test/ELF/build-id.s +++ test/ELF/build-id.s @@ -48,15 +48,15 @@ # DEFAULT: Contents of section .note.test: # DEFAULT: Contents of section .note.gnu.build-id: # DEFAULT-NEXT: 04000000 08000000 03000000 474e5500 ............GNU. -# DEFAULT-NEXT: d618a375 bc6301ec +# DEFAULT-NEXT: b0148597 ba5eb7e9 # MD5: Contents of section .note.gnu.build-id: # MD5-NEXT: 04000000 10000000 03000000 474e5500 ............GNU. -# MD5-NEXT: 051084fe ce1f30ed e035b79e 11262808 +# MD5-NEXT: dce3bcaf 5219454c e89a1fc7 86ab17bd # SHA1: Contents of section .note.gnu.build-id: # SHA1-NEXT: 04000000 14000000 03000000 474e5500 ............GNU. -# SHA1-NEXT: c70b9aec 903fd291 8b677cd8 1e04f8b6 +# SHA1-NEXT: 2f716666 fe3668fe 370a02a1 579c3eb2 # UUID: Contents of section .note.gnu.build-id: # UUID-NEXT: 04000000 10000000 03000000 474e5500 ............GNU.