Update comments as the function does not write to the first page anymore.
Also refactored the code a bit.
llvm-svn: 310886
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 88019ba..56ba962 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1851,31 +1851,31 @@
Sec->writeTo<ELFT>(Buf + Sec->Offset);
}
-static void fillTrapInstr(uint8_t *I, uint8_t *End) {
+static void fillTrap(uint8_t *I, uint8_t *End) {
for (; I + 4 < End; I += 4)
memcpy(I, &Target->TrapInstr, 4);
}
-
-// 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.
+// Fill 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.
+//
+// We'll leave other pages in segments as-is because the rest will be
+// overwritten by output sections.
template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
if (Script->Opt.HasSections)
return;
+ // Fill the last page.
uint8_t *Buf = Buffer->getBufferStart();
+ for (PhdrEntry *P : Phdrs)
+ if (P->p_type == PT_LOAD && (P->p_flags & PF_X))
+ fillTrap(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize),
+ Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize));
- for (PhdrEntry *P : Phdrs) {
- if (P->p_type != PT_LOAD || !(P->p_flags & PF_X))
- continue;
-
- // We only fill the last page of the segment because the rest will be
- // overwritten by output sections.
- fillTrapInstr(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize),
- Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize));
- }
-
+ // Round up the file size of the last segment to the page boundary iff it is
+ // an executable segment to ensure that other other tools don't accidentally
+ // trim the instruction padding (e.g. when stripping the file).
PhdrEntry *LastRX = nullptr;
for (PhdrEntry *P : Phdrs) {
if (P->p_type != PT_LOAD)
@@ -1885,10 +1885,6 @@
else
LastRX = nullptr;
}
-
- // Round up the file size of the last segment to the page boundary iff it is
- // an executable segment to ensure that other other tools don't accidentally
- // trim the instruction padding (e.g. when stripping the file).
if (LastRX)
LastRX->p_filesz = alignTo(LastRX->p_filesz, Target->PageSize);
}