diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 4ff06388ec78..164171bc9726 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -67,6 +67,7 @@ template class Writer { void fixPredefinedSymbols(); void openFile(); void writeHeader(); + void writeTrapInstr(); void writeSections(); void writeSectionsBinary(); void writeBuildId(); @@ -246,6 +247,7 @@ template void Writer::run() { return; if (!Config->OFormatBinary) { + writeTrapInstr(); writeHeader(); writeSections(); } else { @@ -1840,6 +1842,22 @@ template void Writer::writeSectionsBinary() { } } +template void Writer::writeTrapInstr() { + uint8_t *Buf = Buffer->getBufferStart(); + + for (PhdrEntry &P : Phdrs) { + if (!(P.p_flags & PF_X)) + continue; + + // FIXME: We should fill only the first and the last page of the segment + // because the middle part will be overwritten by output sections. + uint8_t *I = Buf + P.p_offset; + uint8_t *End = I + P.p_filesz; + for (; I + 4 < End; I += 4) + memcpy(I, &Target->TrapInstr, 4); + } +} + // Write section contents to a mmap'ed file. template void Writer::writeSections() { uint8_t *Buf = Buffer->getBufferStart();