Navigation Menu

Skip to content

Commit

Permalink
Fill executable segments with trap instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jun 16, 2017
1 parent eb2244c commit 5fcc112
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lld/ELF/Writer.cpp
Expand Up @@ -67,6 +67,7 @@ template <class ELFT> class Writer {
void fixPredefinedSymbols();
void openFile();
void writeHeader();
void writeTrapInstr();
void writeSections();
void writeSectionsBinary();
void writeBuildId();
Expand Down Expand Up @@ -246,6 +247,7 @@ template <class ELFT> void Writer<ELFT>::run() {
return;

if (!Config->OFormatBinary) {
writeTrapInstr();
writeHeader();
writeSections();
} else {
Expand Down Expand Up @@ -1840,6 +1842,22 @@ template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
}
}

template <class ELFT> void Writer<ELFT>::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 <class ELFT> void Writer<ELFT>::writeSections() {
uint8_t *Buf = Buffer->getBufferStart();
Expand Down

0 comments on commit 5fcc112

Please sign in to comment.