2019-05-14 00:01:26 +08:00
|
|
|
# REQUIRES: x86
|
|
|
|
|
|
|
|
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
|
|
|
|
|
|
|
|
# exits with return code 42 on linux
|
|
|
|
.globl _start
|
|
|
|
_start:
|
|
|
|
nop
|
|
|
|
|
|
|
|
# Increase max-page-size to 64k while using the default x86_64 common page size
|
|
|
|
# of 4k. If the last loadable segment is executable then lld aligns the next
|
|
|
|
# section using the common page size.
|
|
|
|
|
|
|
|
# RUN: ld.lld -z max-page-size=0x10000 -z common-page-size=0x1000 %t -o %t2
|
|
|
|
# RUN: llvm-readobj --sections -l %t2 | FileCheck --check-prefix=CHECK-MAX %s
|
|
|
|
|
|
|
|
# CHECK-MAX: Sections [
|
|
|
|
# CHECK-MAX-NEXT: Section {
|
|
|
|
# CHECK-MAX-NEXT: Index: 0
|
|
|
|
# CHECK-MAX-NEXT: Name: (0)
|
|
|
|
# CHECK-MAX-NEXT: Type: SHT_NULL (0x0)
|
|
|
|
# CHECK-MAX-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-MAX-NEXT: ]
|
|
|
|
# CHECK-MAX-NEXT: Address: 0x0
|
|
|
|
# CHECK-MAX-NEXT: Offset: 0x0
|
|
|
|
# CHECK-MAX-NEXT: Size: 0
|
|
|
|
# CHECK-MAX-NEXT: Link: 0
|
|
|
|
# CHECK-MAX-NEXT: Info: 0
|
|
|
|
# CHECK-MAX-NEXT: AddressAlignment: 0
|
|
|
|
# CHECK-MAX-NEXT: EntrySize: 0
|
|
|
|
# CHECK-MAX-NEXT: }
|
|
|
|
# CHECK-MAX-NEXT: Section {
|
|
|
|
# CHECK-MAX-NEXT: Index: 1
|
|
|
|
# CHECK-MAX-NEXT: Name: .text (1)
|
|
|
|
# CHECK-MAX-NEXT: Type: SHT_PROGBITS (0x1)
|
|
|
|
# CHECK-MAX-NEXT: Flags [ (0x6)
|
|
|
|
# CHECK-MAX-NEXT: SHF_ALLOC (0x2)
|
|
|
|
# CHECK-MAX-NEXT: SHF_EXECINSTR (0x4)
|
|
|
|
# CHECK-MAX-NEXT: ]
|
2019-09-16 15:05:34 +08:00
|
|
|
# CHECK-MAX-NEXT: Address: 0x210120
|
|
|
|
# CHECK-MAX-NEXT: Offset: 0x120
|
2019-05-14 00:01:26 +08:00
|
|
|
# CHECK-MAX-NEXT: Size: 1
|
|
|
|
# CHECK-MAX-NEXT: Link: 0
|
|
|
|
# CHECK-MAX-NEXT: Info: 0
|
|
|
|
# CHECK-MAX-NEXT: AddressAlignment: 4
|
|
|
|
# CHECK-MAX-NEXT: EntrySize: 0
|
|
|
|
# CHECK-MAX-NEXT: }
|
|
|
|
# CHECK-MAX-NEXT: Section {
|
|
|
|
# CHECK-MAX-NEXT: Index: 2
|
|
|
|
# CHECK-MAX-NEXT: Name: .comment (7)
|
|
|
|
# CHECK-MAX-NEXT: Type: SHT_PROGBITS (0x1)
|
|
|
|
# CHECK-MAX-NEXT: Flags [ (0x30)
|
|
|
|
# CHECK-MAX-NEXT: SHF_MERGE (0x10)
|
|
|
|
# CHECK-MAX-NEXT: SHF_STRINGS (0x20)
|
|
|
|
# CHECK-MAX-NEXT: ]
|
|
|
|
# CHECK-MAX-NEXT: Address: 0x0
|
2019-09-16 15:05:34 +08:00
|
|
|
# CHECK-MAX-NEXT: Offset: 0x121
|
2019-05-14 00:01:26 +08:00
|
|
|
# CHECK-MAX-NEXT: Size: 8
|
|
|
|
# CHECK-MAX-NEXT: Link: 0
|
|
|
|
# CHECK-MAX-NEXT: Info: 0
|
|
|
|
# CHECK-MAX-NEXT: AddressAlignment: 1
|
|
|
|
# CHECK-MAX-NEXT: EntrySize: 1
|
|
|
|
|
|
|
|
# CHECK-MAX: ProgramHeaders [
|
|
|
|
# CHECK-MAX-NEXT: ProgramHeader {
|
|
|
|
# CHECK-MAX-NEXT: Type: PT_PHDR (0x6)
|
|
|
|
# CHECK-MAX-NEXT: Offset: 0x40
|
|
|
|
# CHECK-MAX-NEXT: VirtualAddress: 0x200040
|
|
|
|
# CHECK-MAX-NEXT: PhysicalAddress: 0x200040
|
|
|
|
# CHECK-MAX-NEXT: FileSize: 224
|
|
|
|
# CHECK-MAX-NEXT: MemSize: 224
|
|
|
|
# CHECK-MAX-NEXT: Flags [ (0x4)
|
|
|
|
# CHECK-MAX-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-MAX-NEXT: ]
|
|
|
|
# CHECK-MAX-NEXT: Alignment: 8
|
|
|
|
# CHECK-MAX-NEXT: }
|
|
|
|
# CHECK-MAX-NEXT: ProgramHeader {
|
|
|
|
# CHECK-MAX-NEXT: Type: PT_LOAD (0x1)
|
|
|
|
# CHECK-MAX-NEXT: Offset: 0x0
|
|
|
|
# CHECK-MAX-NEXT: VirtualAddress: 0x200000
|
|
|
|
# CHECK-MAX-NEXT: PhysicalAddress: 0x200000
|
|
|
|
# CHECK-MAX-NEXT: FileSize: 288
|
|
|
|
# CHECK-MAX-NEXT: MemSize: 288
|
|
|
|
# CHECK-MAX-NEXT: Flags [ (0x4)
|
|
|
|
# CHECK-MAX-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-MAX-NEXT: ]
|
|
|
|
# CHECK-MAX-NEXT: Alignment: 65536
|
|
|
|
# CHECK-MAX-NEXT: }
|
|
|
|
# CHECK-MAX-NEXT: ProgramHeader {
|
|
|
|
# CHECK-MAX-NEXT: Type: PT_LOAD (0x1)
|
2019-09-16 15:05:34 +08:00
|
|
|
# CHECK-MAX-NEXT: Offset: 0x120
|
|
|
|
# CHECK-MAX-NEXT: VirtualAddress: 0x210120
|
|
|
|
# CHECK-MAX-NEXT: PhysicalAddress: 0x210120
|
[ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified
This patch
1) adds -z separate-code and -z noseparate-code (default).
2) changes the condition that the last page of last PF_X PT_LOAD is
padded with trap instructions.
Current condition (after D33630): if there is no `SECTIONS` commands.
After this change: if -z separate-code is specified.
-z separate-code was introduced to ld.bfd in 2018, to place the text
segment in its own pages. There is no overlap in pages between an
executable segment and a non-executable segment:
1) RX cannot load initial contents from R or RW(or non-SHF_ALLOC).
2) R and RW(or non-SHF_ALLOC) cannot load initial contents from RX.
lld's current status:
- Between R and RX: in `Writer<ELFT>::fixSectionAlignments()`, the start of a
segment is always aligned to maxPageSize, so the initial contents loaded by R
and RX do not overlap. I plan to allow overlaps in D64906 if -z noseparate-code
is in effect.
- Between RX and RW(or non-SHF_ALLOC if RW doesn't exist):
we currently unconditionally pad the last page to commonPageSize
(defaults to 4096 on all targets we support).
This patch will make it effective only if -z separate-code is specified.
-z separate-code is a dubious feature that intends to reduce the number
of ROP gadgets (which is actually ineffective because attackers can find
plenty of gadgets in the text segment, no need to find gadgets in
non-code regions).
With the overlapping PT_LOAD technique D64906, -z noseparate-code
removes two more alignments at segment boundaries than -z separate-code.
This saves at most defaultCommonPageSize*2 bytes, which are significant
on targets with large defaultCommonPageSize (AArch64/MIPS/PPC: 65536).
Issues/feedback on alignment at segment boundaries to help understand
the implication:
* binutils PR24490 (the situation on ld.bfd is worse because they have
two R-- on both sides of R-E so more alignments.)
* In binutils, the 2018-02-27 commit "ld: Add --enable-separate-code" made -z separate-code the default on Linux.
https://github.com/richfelker/musl-cross-make/commit/d969dea983a2cc54a1e0308a0cdeb6c3307e4bfa
In musl-cross-make, binutils is configured with --disable-separate-code
to address size regressions caused by -z separate-code. (lld actually has the same
issue, which I plan to fix in a future patch. The ld.bfd x86 status is
worse because they default to max-page-size=0x200000).
* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=237676 people want
smaller code size. This patch will remove one alignment boundary.
* Stef O'Rear: I'm opposed to any kind of page alignment at the
text/rodata line (having a partial page of text aliased as rodata and
vice versa has no demonstrable harm, and I actually care about small
systems).
So, make -z noseparate-code the default.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64903
llvm-svn: 367537
2019-08-01 17:58:25 +08:00
|
|
|
# CHECK-MAX-NEXT: FileSize: 1
|
|
|
|
# CHECK-MAX-NEXT: MemSize: 1
|
2019-05-14 00:01:26 +08:00
|
|
|
# CHECK-MAX-NEXT: Flags [ (0x5)
|
|
|
|
# CHECK-MAX-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-MAX-NEXT: PF_X (0x1)
|
|
|
|
# CHECK-MAX-NEXT: ]
|
|
|
|
# CHECK-MAX-NEXT: Alignment: 65536
|
|
|
|
# CHECK-MAX-NEXT: }
|
|
|
|
# CHECK-MAX-NEXT: ProgramHeader {
|
|
|
|
# CHECK-MAX-NEXT: Type: PT_GNU_STACK (0x6474E551)
|
|
|
|
# CHECK-MAX-NEXT: Offset: 0x0
|
|
|
|
# CHECK-MAX-NEXT: VirtualAddress: 0x0
|
|
|
|
# CHECK-MAX-NEXT: PhysicalAddress: 0x0
|
|
|
|
# CHECK-MAX-NEXT: FileSize: 0
|
|
|
|
# CHECK-MAX-NEXT: MemSize: 0
|
|
|
|
# CHECK-MAX-NEXT: Flags [ (0x6)
|
|
|
|
# CHECK-MAX-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-MAX-NEXT: PF_W (0x2)
|
|
|
|
# CHECK-MAX-NEXT: ]
|
|
|
|
# CHECK-MAX-NEXT: Alignment: 0
|
|
|
|
|
|
|
|
# Increase common-page-size to max-page-size. Expect to see a larger offset
|
|
|
|
# of the first Section after the executable loadable segment due to the higher
|
|
|
|
# alignment requirement.
|
|
|
|
|
|
|
|
# RUN: ld.lld -z max-page-size=0x10000 -z common-page-size=0x10000 %t -o %t3
|
|
|
|
# RUN: llvm-readobj --sections -l %t3 | FileCheck --check-prefix=CHECK-COMMON %s
|
|
|
|
|
|
|
|
# Check that we truncate common-page-size to max-page-size
|
|
|
|
|
|
|
|
# RUN: ld.lld -z max-page-size=0x10000 -z common-page-size=0x100000 %t -o %t4
|
|
|
|
# RUN: llvm-readobj --sections -l %t4 | FileCheck --check-prefix=CHECK-COMMON %s
|
|
|
|
|
|
|
|
# CHECK-COMMON: Sections [
|
|
|
|
# CHECK-COMMON-NEXT: Section {
|
|
|
|
# CHECK-COMMON-NEXT: Index: 0
|
|
|
|
# CHECK-COMMON-NEXT: Name: (0)
|
|
|
|
# CHECK-COMMON-NEXT: Type: SHT_NULL (0x0)
|
|
|
|
# CHECK-COMMON-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-COMMON-NEXT: ]
|
|
|
|
# CHECK-COMMON-NEXT: Address: 0x0
|
|
|
|
# CHECK-COMMON-NEXT: Offset: 0x0
|
|
|
|
# CHECK-COMMON-NEXT: Size: 0
|
|
|
|
# CHECK-COMMON-NEXT: Link: 0
|
|
|
|
# CHECK-COMMON-NEXT: Info: 0
|
|
|
|
# CHECK-COMMON-NEXT: AddressAlignment: 0
|
|
|
|
# CHECK-COMMON-NEXT: EntrySize: 0
|
|
|
|
# CHECK-COMMON-NEXT: }
|
|
|
|
# CHECK-COMMON-NEXT: Section {
|
|
|
|
# CHECK-COMMON-NEXT: Index: 1
|
|
|
|
# CHECK-COMMON-NEXT: Name: .text (1)
|
|
|
|
# CHECK-COMMON-NEXT: Type: SHT_PROGBITS (0x1)
|
|
|
|
# CHECK-COMMON-NEXT: Flags [ (0x6)
|
|
|
|
# CHECK-COMMON-NEXT: SHF_ALLOC (0x2)
|
|
|
|
# CHECK-COMMON-NEXT: SHF_EXECINSTR (0x4)
|
|
|
|
# CHECK-COMMON-NEXT: ]
|
2019-09-16 15:05:34 +08:00
|
|
|
# CHECK-COMMON-NEXT: Address: 0x210120
|
|
|
|
# CHECK-COMMON-NEXT: Offset: 0x120
|
2019-05-14 00:01:26 +08:00
|
|
|
# CHECK-COMMON-NEXT: Size: 1
|
|
|
|
# CHECK-COMMON-NEXT: Link: 0
|
|
|
|
# CHECK-COMMON-NEXT: Info: 0
|
|
|
|
# CHECK-COMMON-NEXT: AddressAlignment: 4
|
|
|
|
# CHECK-COMMON-NEXT: EntrySize: 0
|
|
|
|
# CHECK-COMMON-NEXT: }
|
|
|
|
# CHECK-COMMON-NEXT: Section {
|
|
|
|
# CHECK-COMMON-NEXT: Index: 2
|
|
|
|
# CHECK-COMMON-NEXT: Name: .comment (7)
|
|
|
|
# CHECK-COMMON-NEXT: Type: SHT_PROGBITS (0x1)
|
|
|
|
# CHECK-COMMON-NEXT: Flags [ (0x30)
|
|
|
|
# CHECK-COMMON-NEXT: SHF_MERGE (0x10)
|
|
|
|
# CHECK-COMMON-NEXT: SHF_STRINGS (0x20)
|
|
|
|
# CHECK-COMMON-NEXT: ]
|
|
|
|
# CHECK-COMMON-NEXT: Address: 0x0
|
2019-09-16 15:05:34 +08:00
|
|
|
# CHECK-COMMON-NEXT: Offset: 0x121
|
2019-05-14 00:01:26 +08:00
|
|
|
# CHECK-COMMON-NEXT: Size: 8
|
|
|
|
# CHECK-COMMON-NEXT: Link: 0
|
|
|
|
# CHECK-COMMON-NEXT: Info: 0
|
|
|
|
# CHECK-COMMON-NEXT: AddressAlignment: 1
|
|
|
|
# CHECK-COMMON-NEXT: EntrySize: 1
|
|
|
|
|
|
|
|
# CHECK-COMMON: ProgramHeaders [
|
|
|
|
# CHECK-COMMON-NEXT: ProgramHeader {
|
|
|
|
# CHECK-COMMON-NEXT: Type: PT_PHDR (0x6)
|
|
|
|
# CHECK-COMMON-NEXT: Offset: 0x40
|
|
|
|
# CHECK-COMMON-NEXT: VirtualAddress: 0x200040
|
|
|
|
# CHECK-COMMON-NEXT: PhysicalAddress: 0x200040
|
|
|
|
# CHECK-COMMON-NEXT: FileSize: 224
|
|
|
|
# CHECK-COMMON-NEXT: MemSize: 224
|
|
|
|
# CHECK-COMMON-NEXT: Flags [ (0x4)
|
|
|
|
# CHECK-COMMON-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-COMMON-NEXT: ]
|
|
|
|
# CHECK-COMMON-NEXT: Alignment: 8
|
|
|
|
# CHECK-COMMON-NEXT: }
|
|
|
|
# CHECK-COMMON-NEXT: ProgramHeader {
|
|
|
|
# CHECK-COMMON-NEXT: Type: PT_LOAD (0x1)
|
|
|
|
# CHECK-COMMON-NEXT: Offset: 0x0
|
|
|
|
# CHECK-COMMON-NEXT: VirtualAddress: 0x200000
|
|
|
|
# CHECK-COMMON-NEXT: PhysicalAddress: 0x200000
|
|
|
|
# CHECK-COMMON-NEXT: FileSize: 288
|
|
|
|
# CHECK-COMMON-NEXT: MemSize: 288
|
|
|
|
# CHECK-COMMON-NEXT: Flags [ (0x4)
|
|
|
|
# CHECK-COMMON-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-COMMON-NEXT: ]
|
|
|
|
# CHECK-COMMON-NEXT: Alignment: 65536
|
|
|
|
# CHECK-COMMON-NEXT: }
|
|
|
|
# CHECK-COMMON-NEXT: ProgramHeader {
|
|
|
|
# CHECK-COMMON-NEXT: Type: PT_LOAD (0x1)
|
2019-09-16 15:05:34 +08:00
|
|
|
# CHECK-COMMON-NEXT: Offset: 0x120
|
|
|
|
# CHECK-COMMON-NEXT: VirtualAddress: 0x210120
|
|
|
|
# CHECK-COMMON-NEXT: PhysicalAddress: 0x210120
|
[ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified
This patch
1) adds -z separate-code and -z noseparate-code (default).
2) changes the condition that the last page of last PF_X PT_LOAD is
padded with trap instructions.
Current condition (after D33630): if there is no `SECTIONS` commands.
After this change: if -z separate-code is specified.
-z separate-code was introduced to ld.bfd in 2018, to place the text
segment in its own pages. There is no overlap in pages between an
executable segment and a non-executable segment:
1) RX cannot load initial contents from R or RW(or non-SHF_ALLOC).
2) R and RW(or non-SHF_ALLOC) cannot load initial contents from RX.
lld's current status:
- Between R and RX: in `Writer<ELFT>::fixSectionAlignments()`, the start of a
segment is always aligned to maxPageSize, so the initial contents loaded by R
and RX do not overlap. I plan to allow overlaps in D64906 if -z noseparate-code
is in effect.
- Between RX and RW(or non-SHF_ALLOC if RW doesn't exist):
we currently unconditionally pad the last page to commonPageSize
(defaults to 4096 on all targets we support).
This patch will make it effective only if -z separate-code is specified.
-z separate-code is a dubious feature that intends to reduce the number
of ROP gadgets (which is actually ineffective because attackers can find
plenty of gadgets in the text segment, no need to find gadgets in
non-code regions).
With the overlapping PT_LOAD technique D64906, -z noseparate-code
removes two more alignments at segment boundaries than -z separate-code.
This saves at most defaultCommonPageSize*2 bytes, which are significant
on targets with large defaultCommonPageSize (AArch64/MIPS/PPC: 65536).
Issues/feedback on alignment at segment boundaries to help understand
the implication:
* binutils PR24490 (the situation on ld.bfd is worse because they have
two R-- on both sides of R-E so more alignments.)
* In binutils, the 2018-02-27 commit "ld: Add --enable-separate-code" made -z separate-code the default on Linux.
https://github.com/richfelker/musl-cross-make/commit/d969dea983a2cc54a1e0308a0cdeb6c3307e4bfa
In musl-cross-make, binutils is configured with --disable-separate-code
to address size regressions caused by -z separate-code. (lld actually has the same
issue, which I plan to fix in a future patch. The ld.bfd x86 status is
worse because they default to max-page-size=0x200000).
* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=237676 people want
smaller code size. This patch will remove one alignment boundary.
* Stef O'Rear: I'm opposed to any kind of page alignment at the
text/rodata line (having a partial page of text aliased as rodata and
vice versa has no demonstrable harm, and I actually care about small
systems).
So, make -z noseparate-code the default.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64903
llvm-svn: 367537
2019-08-01 17:58:25 +08:00
|
|
|
# CHECK-COMMON-NEXT: FileSize: 1
|
|
|
|
# CHECK-COMMON-NEXT: MemSize: 1
|
2019-05-14 00:01:26 +08:00
|
|
|
# CHECK-COMMON-NEXT: Flags [ (0x5)
|
|
|
|
# CHECK-COMMON-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-COMMON-NEXT: PF_X (0x1)
|
|
|
|
# CHECK-COMMON-NEXT: ]
|
|
|
|
# CHECK-COMMON-NEXT: Alignment: 65536
|
|
|
|
# CHECK-COMMON-NEXT: }
|
|
|
|
# CHECK-COMMON-NEXT: ProgramHeader {
|
|
|
|
# CHECK-COMMON-NEXT: Type: PT_GNU_STACK (0x6474E551)
|
|
|
|
# CHECK-COMMON-NEXT: Offset: 0x0
|
|
|
|
# CHECK-COMMON-NEXT: VirtualAddress: 0x0
|
|
|
|
# CHECK-COMMON-NEXT: PhysicalAddress: 0x0
|
|
|
|
# CHECK-COMMON-NEXT: FileSize: 0
|
|
|
|
# CHECK-COMMON-NEXT: MemSize: 0
|
|
|
|
# CHECK-COMMON-NEXT: Flags [ (0x6)
|
|
|
|
# CHECK-COMMON-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-COMMON-NEXT: PF_W (0x2)
|
|
|
|
# CHECK-COMMON-NEXT: ]
|
|
|
|
# CHECK-COMMON-NEXT: Alignment: 0
|