[ELF] Pad x86 executable sections with 0xcc int3 instructions
authorJames Henderson <jh7370@my.bristol.ac.uk>
Thu, 6 Apr 2017 09:29:08 +0000 (09:29 +0000)
committerJames Henderson <jh7370@my.bristol.ac.uk>
Thu, 6 Apr 2017 09:29:08 +0000 (09:29 +0000)
commit8dd4c06a774035e512116baccc605d5ccf729a51
tree19739e00c6c1c526fc436d289aea928b44d2c452
parent1b4b59a4156083c7703436a7d6bed2f6f56952f5
[ELF] Pad x86 executable sections with 0xcc int3 instructions

Executable sections should not be padded with zero by default. On some
architectures, 0x00 is the start of a valid instruction sequence, so can confuse
disassembly between InputSections (and indeed the start of the next InputSection
in some situations). Further, in the case of misjumps into padding, padding may
start to be executed silently.

On x86, the "0xcc" byte represents the int3 trap instruction. It is a single
byte long so can serve well as padding. This change switches x86 (and x86_64) to
use this value for padding in executable sections, if no linker script directive
overrides it. It also puts the behaviour into place making it easy to change the
behaviour of other targets when desired. I do not know the relevant instruction
sequences for trap instructions on other targets however, so somebody should add
this separately.

Because the old behaviour simply wrote padding in the whole section before
overwriting most of it, this change also modifies the padding algorithm to write
padding only where needed. This in turn has caused a small behaviour change with
regards to what values are written via Fill commands in linker scripts, bringing
it into line with ld.bfd. The fill value is now written starting from the end of
the previous block, which means that it always starts from the first byte of the
fill, whereas the old behaviour meant that the padding sometimes started mid-way
through the fill value. See the test changes for more details.

Reviewed by: ruiu

Differential Revision: https://reviews.llvm.org/D30886

Bugzilla: http://bugs.llvm.org/show_bug.cgi?id=32227
llvm-svn: 299635
13 files changed:
lld/ELF/LinkerScript.cpp
lld/ELF/LinkerScript.h
lld/ELF/OutputSections.cpp
lld/ELF/OutputSections.h
lld/ELF/SyntheticSections.cpp
lld/ELF/Target.cpp
lld/ELF/Target.h
lld/ELF/Writer.cpp
lld/test/ELF/default-fill.s [new file with mode: 0644]
lld/test/ELF/linkerscript/excludefile.s
lld/test/ELF/linkerscript/fill-exec-sections.s [new file with mode: 0644]
lld/test/ELF/linkerscript/fill.s
lld/test/ELF/linkerscript/sections-padding.s