Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
13 changed files
with
143 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# REQUIRES: x86 | ||
# Verify that the fill between sections has a default of interrupt instructions | ||
# (0xcc on x86/x86_64) for executable sections and zero for other sections. | ||
|
||
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o | ||
# RUN: ld.lld %t1.o -o %t1.elf | ||
# RUN: llvm-objdump -s %t1.elf > %t1.sections | ||
# RUN: FileCheck %s --input-file %t1.sections --check-prefix=TEXT | ||
# RUN: FileCheck %s --input-file %t1.sections --check-prefix=DATA | ||
|
||
# RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t2.o | ||
# RUN: ld.lld %t2.o -o %t2.elf | ||
# RUN: llvm-objdump -s %t2.elf > %t2.sections | ||
# RUN: FileCheck %s --input-file %t2.sections --check-prefix=TEXT | ||
# RUN: FileCheck %s --input-file %t2.sections --check-prefix=DATA | ||
|
||
# TEXT: Contents of section .text: | ||
# TEXT-NEXT: 11cccccc cccccccc cccccccc cccccccc | ||
# TEXT-NEXT: 22 | ||
# DATA: Contents of section .data: | ||
# DATA-NEXT: 33000000 00000000 00000000 00000000 | ||
# DATA-NEXT: 44 | ||
|
||
.section .text.1,"ax",@progbits | ||
.align 16 | ||
.byte 0x11 | ||
|
||
.section .text.2,"ax",@progbits | ||
.align 16 | ||
.byte 0x22 | ||
|
||
.section .data.1,"a",@progbits | ||
.align 16 | ||
.byte 0x33 | ||
|
||
.section .data.2,"a",@progbits | ||
.align 16 | ||
.byte 0x44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# REQUIRES: x86 | ||
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t | ||
|
||
## Check that padding of executable sections are filled with trap bytes if not | ||
## otherwise specified in the script. | ||
# RUN: echo "SECTIONS { .exec : { *(.exec*) } }" > %t.script | ||
# RUN: ld.lld -o %t.out --script %t.script %t | ||
# RUN: llvm-objdump -s %t.out | FileCheck %s --check-prefix=EXEC | ||
# EXEC: 0000 66cccccc cccccccc cccccccc cccccccc | ||
# EXEC-NEXT: 0010 66 | ||
|
||
## Check that a fill expression or command overrides the default filler... | ||
# RUN: echo "SECTIONS { .exec : { *(.exec*) }=0x11223344 }" > %t2.script | ||
# RUN: ld.lld -o %t2.out --script %t2.script %t | ||
# RUN: llvm-objdump -s %t2.out | FileCheck %s --check-prefix=OVERRIDE | ||
# RUN: echo "SECTIONS { .exec : { FILL(0x11223344); *(.exec*) } }" > %t3.script | ||
# RUN: ld.lld -o %t3.out --script %t3.script %t | ||
# RUN: llvm-objdump -s %t3.out | FileCheck %s --check-prefix=OVERRIDE | ||
# OVERRIDE: Contents of section .exec: | ||
# OVERRIDE-NEXT: 0000 66112233 44112233 44112233 44112233 | ||
# OVERRIDE-NEXT: 0010 66 | ||
|
||
## ...even for a value of zero. | ||
# RUN: echo "SECTIONS { .exec : { *(.exec*) }=0x00000000 }" > %t4.script | ||
# RUN: ld.lld -o %t4.out --script %t4.script %t | ||
# RUN: llvm-objdump -s %t4.out | FileCheck %s --check-prefix=ZERO | ||
# RUN: echo "SECTIONS { .exec : { FILL(0x00000000); *(.exec*) } }" > %t5.script | ||
# RUN: ld.lld -o %t5.out --script %t5.script %t | ||
# RUN: llvm-objdump -s %t5.out | FileCheck %s --check-prefix=ZERO | ||
# ZERO: Contents of section .exec: | ||
# ZERO-NEXT: 0000 66000000 00000000 00000000 00000000 | ||
# ZERO-NEXT: 0010 66 | ||
|
||
.section .exec.1,"ax" | ||
.align 16 | ||
.byte 0x66 | ||
|
||
.section .exec.2,"ax" | ||
.align 16 | ||
.byte 0x66 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters