2018-06-27 00:58:19 +08:00
|
|
|
# REQUIRES: sparc
|
2017-06-29 01:05:39 +08:00
|
|
|
# RUN: llvm-mc -filetype=obj -triple=sparc64-unknown-openbsd %s -o %t
|
|
|
|
# RUN: ld.lld %t -o %t2
|
2019-05-01 13:49:01 +08:00
|
|
|
# RUN: llvm-readobj --file-headers --sections -l --symbols %t2 \
|
2017-06-29 01:05:39 +08:00
|
|
|
# RUN: | FileCheck %s
|
|
|
|
|
|
|
|
# exits with return code 42 on OpenBSD/sparc64
|
|
|
|
.global _start
|
|
|
|
_start:
|
|
|
|
mov 42, %o0
|
|
|
|
mov 1, %g1
|
|
|
|
ta 0
|
|
|
|
|
|
|
|
# CHECK: ElfHeader {
|
|
|
|
# CHECK-NEXT: Ident {
|
|
|
|
# CHECK-NEXT: Magic: (7F 45 4C 46)
|
|
|
|
# CHECK-NEXT: Class: 64-bit (0x2)
|
|
|
|
# CHECK-NEXT: DataEncoding: BigEndian (0x2)
|
|
|
|
# CHECK-NEXT: FileVersion: 1
|
|
|
|
# CHECK-NEXT: OS/ABI: SystemV (0x0)
|
|
|
|
# CHECK-NEXT: ABIVersion: 0
|
|
|
|
# CHECK-NEXT: Unused: (00 00 00 00 00 00 00)
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Type: Executable (0x2)
|
|
|
|
# CHECK-NEXT: Machine: EM_SPARCV9 (0x2B)
|
|
|
|
# CHECK-NEXT: Version: 1
|
|
|
|
# CHECK-NEXT: Entry: [[ENTRY:0x[0-9A-F]+]]
|
|
|
|
# CHECK-NEXT: ProgramHeaderOffset: 0x40
|
2019-08-28 17:45:06 +08:00
|
|
|
# CHECK-NEXT: SectionHeaderOffset: 0x1A0
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: HeaderSize: 64
|
|
|
|
# CHECK-NEXT: ProgramHeaderEntrySize: 56
|
|
|
|
# CHECK-NEXT: ProgramHeaderCount: 4
|
|
|
|
# CHECK-NEXT: SectionHeaderEntrySize: 64
|
|
|
|
# CHECK-NEXT: SectionHeaderCount: 6
|
|
|
|
# CHECK-NEXT: StringTableSectionIndex: 4
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Sections [
|
|
|
|
# CHECK-NEXT: Section {
|
|
|
|
# CHECK-NEXT: Index: 0
|
|
|
|
# CHECK-NEXT: Name: (0)
|
|
|
|
# CHECK-NEXT: Type: SHT_NULL (0x0)
|
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Address: 0x0
|
|
|
|
# CHECK-NEXT: Offset: 0x0
|
|
|
|
# CHECK-NEXT: Size: 0
|
|
|
|
# CHECK-NEXT: Link: 0
|
|
|
|
# CHECK-NEXT: Info: 0
|
|
|
|
# CHECK-NEXT: AddressAlignment: 0
|
|
|
|
# CHECK-NEXT: EntrySize: 0
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Section {
|
|
|
|
# CHECK-NEXT: Index: 1
|
|
|
|
# CHECK-NEXT: Name: .text
|
|
|
|
# CHECK-NEXT: Type: SHT_PROGBITS (0x1)
|
|
|
|
# CHECK-NEXT: Flags [ (0x6)
|
|
|
|
# CHECK-NEXT: SHF_ALLOC (0x2)
|
|
|
|
# CHECK-NEXT: SHF_EXECINSTR (0x4)
|
|
|
|
# CHECK-NEXT: ]
|
2019-08-28 17:45:06 +08:00
|
|
|
# CHECK-NEXT: Address: 0x200120
|
|
|
|
# CHECK-NEXT: Offset: 0x120
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: Size: 12
|
|
|
|
# CHECK-NEXT: Link: 0
|
|
|
|
# CHECK-NEXT: Info: 0
|
|
|
|
# CHECK-NEXT: AddressAlignment: 4
|
|
|
|
# CHECK-NEXT: EntrySize: 0
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Section {
|
|
|
|
# CHECK-NEXT: Index: 2
|
|
|
|
# CHECK-NEXT: Name: .comment
|
|
|
|
# CHECK-NEXT: Type: SHT_PROGBITS (0x1)
|
|
|
|
# CHECK-NEXT: Flags [ (0x30)
|
|
|
|
# CHECK-NEXT: SHF_MERGE (0x10)
|
|
|
|
# CHECK-NEXT: SHF_STRINGS (0x20)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Address: 0x0
|
2019-08-28 17:45:06 +08:00
|
|
|
# CHECK-NEXT: Offset: 0x12C
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: Size: 8
|
|
|
|
# CHECK-NEXT: Link: 0
|
|
|
|
# CHECK-NEXT: Info: 0
|
|
|
|
# CHECK-NEXT: AddressAlignment: 1
|
2017-11-16 00:56:20 +08:00
|
|
|
# CHECK-NEXT: EntrySize: 1
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Section {
|
|
|
|
# CHECK-NEXT: Index: 3
|
|
|
|
# CHECK-NEXT: Name: .symtab
|
|
|
|
# CHECK-NEXT: Type: SHT_SYMTAB (0x2)
|
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Address: 0x0
|
2019-08-28 17:45:06 +08:00
|
|
|
# CHECK-NEXT: Offset: 0x138
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: Size: 48
|
|
|
|
# CHECK-NEXT: Link: 5
|
|
|
|
# CHECK-NEXT: Info: 1
|
|
|
|
# CHECK-NEXT: AddressAlignment: 8
|
|
|
|
# CHECK-NEXT: EntrySize: 24
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Section {
|
|
|
|
# CHECK-NEXT: Index: 4
|
|
|
|
# CHECK-NEXT: Name: .shstrtab
|
|
|
|
# CHECK-NEXT: Type: SHT_STRTAB (0x3)
|
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Address: 0x0
|
2019-08-28 17:45:06 +08:00
|
|
|
# CHECK-NEXT: Offset: 0x168
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: Size: 42
|
|
|
|
# CHECK-NEXT: Link: 0
|
|
|
|
# CHECK-NEXT: Info: 0
|
|
|
|
# CHECK-NEXT: AddressAlignment: 1
|
|
|
|
# CHECK-NEXT: EntrySize: 0
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Section {
|
|
|
|
# CHECK-NEXT: Index: 5
|
|
|
|
# CHECK-NEXT: Name: .strtab
|
|
|
|
# CHECK-NEXT: Type: SHT_STRTAB (0x3)
|
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Address: 0x0
|
2019-08-28 17:45:06 +08:00
|
|
|
# CHECK-NEXT: Offset: 0x192
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: Size: 8
|
|
|
|
# CHECK-NEXT: Link: 0
|
|
|
|
# CHECK-NEXT: Info: 0
|
|
|
|
# CHECK-NEXT: AddressAlignment: 1
|
|
|
|
# CHECK-NEXT: EntrySize: 0
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: ProgramHeaders [
|
|
|
|
# CHECK-NEXT: ProgramHeader {
|
|
|
|
# CHECK-NEXT: Type: PT_PHDR (0x6)
|
|
|
|
# CHECK-NEXT: Offset: 0x40
|
[ELF] Map the ELF header at imageBase
If there is no readonly section, we map:
* The ELF header at imageBase+maxPageSize
* Program headers at imageBase+maxPageSize+sizeof(Ehdr)
* The first section .text at imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)
Due to the interaction between Writer<ELFT>::fixSectionAlignments and
LinkerScript::allocateHeaders,
`alignDown(p_vaddr(R PT_LOAD)) = alignDown(p_vaddr(RX PT_LOAD))`.
The RX PT_LOAD will override the R PT_LOAD at runtime, which is not ideal:
```
// PHDR at 0x401034, should be 0x400034
PHDR 0x000034 0x00401034 0x00401034 0x000a0 0x000a0 R 0x4
// R PT_LOAD contains just Ehdr and program headers.
// At 0x401000, should be 0x400000
LOAD 0x000000 0x00401000 0x00401000 0x000d4 0x000d4 R 0x1000
LOAD 0x0000d4 0x004010d4 0x004010d4 0x00001 0x00001 R E 0x1000
```
* createPhdrs allocates the headers to the R PT_LOAD.
* fixSectionAlignments assigns `imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)` (formula: `alignTo(dot, maxPageSize) + dot % config->maxPageSize`) to addrExpr of .text
* allocateHeaders computes the minimum address among SHF_ALLOC sections, i.e. addr(.text)
* allocateHeaders sets address of ELF header to `addr(.text)-sizeof(Ehdr)-sizeof(program headers) = imageBase+maxPageSize`
The main observation is that when the SECTIONS command is not used, we
don't have to call allocateHeaders. This requires an assumption that
the presence of PT_PHDR and addresses of headers can be decided
regardless of address information.
This may seem natural because dot is not manipulated by a linker script.
The other thing is that we have to drop the special rule for -T<section>
in `getInitialDot`. If -Ttext is smaller than the image base, the headers
will not be allocated with the old behavior (allocateHeaders is called)
but always allocated with the new behavior.
The behavior change is not a problem. Whether and where headers are
allocated can vary among linkers, or ld.bfd across different versions
(--enable-separate-code or not). It is thus advised to use a linker
script with the PHDRS command to have a consistent behavior across
linkers. If PT_PHDR is needed, an explicit --image-base can be a simpler
alternative.
Differential Revision: https://reviews.llvm.org/D67325
llvm-svn: 371957
2019-09-16 15:04:16 +08:00
|
|
|
# CHECK-NEXT: VirtualAddress: 0x100040
|
|
|
|
# CHECK-NEXT: PhysicalAddress: 0x100040
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: FileSize: 224
|
|
|
|
# CHECK-NEXT: MemSize: 224
|
|
|
|
# CHECK-NEXT: Flags [ (0x4)
|
|
|
|
# CHECK-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Alignment: 8
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: ProgramHeader {
|
|
|
|
# CHECK-NEXT: Type: PT_LOAD (0x1)
|
|
|
|
# CHECK-NEXT: Offset: 0x0
|
[ELF] Map the ELF header at imageBase
If there is no readonly section, we map:
* The ELF header at imageBase+maxPageSize
* Program headers at imageBase+maxPageSize+sizeof(Ehdr)
* The first section .text at imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)
Due to the interaction between Writer<ELFT>::fixSectionAlignments and
LinkerScript::allocateHeaders,
`alignDown(p_vaddr(R PT_LOAD)) = alignDown(p_vaddr(RX PT_LOAD))`.
The RX PT_LOAD will override the R PT_LOAD at runtime, which is not ideal:
```
// PHDR at 0x401034, should be 0x400034
PHDR 0x000034 0x00401034 0x00401034 0x000a0 0x000a0 R 0x4
// R PT_LOAD contains just Ehdr and program headers.
// At 0x401000, should be 0x400000
LOAD 0x000000 0x00401000 0x00401000 0x000d4 0x000d4 R 0x1000
LOAD 0x0000d4 0x004010d4 0x004010d4 0x00001 0x00001 R E 0x1000
```
* createPhdrs allocates the headers to the R PT_LOAD.
* fixSectionAlignments assigns `imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)` (formula: `alignTo(dot, maxPageSize) + dot % config->maxPageSize`) to addrExpr of .text
* allocateHeaders computes the minimum address among SHF_ALLOC sections, i.e. addr(.text)
* allocateHeaders sets address of ELF header to `addr(.text)-sizeof(Ehdr)-sizeof(program headers) = imageBase+maxPageSize`
The main observation is that when the SECTIONS command is not used, we
don't have to call allocateHeaders. This requires an assumption that
the presence of PT_PHDR and addresses of headers can be decided
regardless of address information.
This may seem natural because dot is not manipulated by a linker script.
The other thing is that we have to drop the special rule for -T<section>
in `getInitialDot`. If -Ttext is smaller than the image base, the headers
will not be allocated with the old behavior (allocateHeaders is called)
but always allocated with the new behavior.
The behavior change is not a problem. Whether and where headers are
allocated can vary among linkers, or ld.bfd across different versions
(--enable-separate-code or not). It is thus advised to use a linker
script with the PHDRS command to have a consistent behavior across
linkers. If PT_PHDR is needed, an explicit --image-base can be a simpler
alternative.
Differential Revision: https://reviews.llvm.org/D67325
llvm-svn: 371957
2019-09-16 15:04:16 +08:00
|
|
|
# CHECK-NEXT: VirtualAddress: 0x100000
|
|
|
|
# CHECK-NEXT: PhysicalAddress: 0x100000
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: FileSize: 288
|
|
|
|
# CHECK-NEXT: MemSize: 288
|
|
|
|
# CHECK-NEXT: Flags [
|
|
|
|
# CHECK-NEXT: PF_R
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Alignment: 1048576
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: ProgramHeader {
|
|
|
|
# CHECK-NEXT: Type: PT_LOAD (0x1)
|
2019-08-28 17:45:06 +08:00
|
|
|
# CHECK-NEXT: Offset: 0x120
|
|
|
|
# CHECK-NEXT: VirtualAddress: 0x200120
|
|
|
|
# CHECK-NEXT: PhysicalAddress: 0x200120
|
[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-NEXT: FileSize: 12
|
|
|
|
# CHECK-NEXT: MemSize: 12
|
2017-06-29 01:05:39 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x5)
|
|
|
|
# CHECK-NEXT: PF_R (0x4)
|
|
|
|
# CHECK-NEXT: PF_X (0x1)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Alignment: 1048576
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: ProgramHeader {
|
|
|
|
# CHECK-NEXT: Type: PT_GNU_STACK
|
|
|
|
# CHECK-NEXT: Offset: 0x0
|
|
|
|
# CHECK-NEXT: VirtualAddress: 0x0
|
|
|
|
# CHECK-NEXT: PhysicalAddress: 0x0
|
|
|
|
# CHECK-NEXT: FileSize: 0
|
|
|
|
# CHECK-NEXT: MemSize: 0
|
|
|
|
# CHECK-NEXT: Flags [
|
|
|
|
# CHECK-NEXT: PF_R
|
|
|
|
# CHECK-NEXT: PF_W
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Alignment: 0
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: ]
|
2020-03-04 11:49:14 +08:00
|
|
|
# CHECK-NEXT: Symbols [
|
|
|
|
# CHECK-NEXT: Symbol {
|
|
|
|
# CHECK-NEXT: Name: (0)
|
|
|
|
# CHECK-NEXT: Value: 0x0
|
|
|
|
# CHECK-NEXT: Size: 0
|
|
|
|
# CHECK-NEXT: Binding: Local (0x0)
|
|
|
|
# CHECK-NEXT: Type: None (0x0)
|
|
|
|
# CHECK-NEXT: Other: 0
|
|
|
|
# CHECK-NEXT: Section: Undefined (0x0)
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
|
|
|
# CHECK-NEXT: Name: _start
|
|
|
|
# CHECK-NEXT: Value: [[ENTRY]]
|
|
|
|
# CHECK-NEXT: Size: 0
|
|
|
|
# CHECK-NEXT: Binding: Global (0x1)
|
|
|
|
# CHECK-NEXT: Type: None (0x0)
|
|
|
|
# CHECK-NEXT: Other: 0
|
|
|
|
# CHECK-NEXT: Section: .text
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: ]
|