Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 57c71d4

Browse files
committed
Use lld as the linker and add a linker script
This patch aims only to add linker script semantics and should retain linking equivalence. It does not aim to produce address for address equality with `ld`. Functional changes in linker output should follow in additional patches. Before this patch, `ld` produced the following: ``` Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000002898 0x0000000000002898 R 0x1000 LOAD 0x0000000000003000 0x0000000000003000 0x0000000000003000 0x00000000000340ae 0x00000000000340ae RWE 0x1000 LOAD 0x0000000000038000 0x0000000000038000 0x0000000000038000 0x0000000000007520 0x0000000000007520 R 0x1000 LOAD 0x000000000003f728 0x0000000000040728 0x0000000000040728 0x00000000000018d8 0x00000000000020d8 RW 0x1000 DYNAMIC 0x00000000000408c0 0x00000000000418c0 0x00000000000418c0 0x0000000000000110 0x0000000000000110 RW 0x8 NOTE 0x0000000000000238 0x0000000000000238 0x0000000000000238 0x0000000000000024 0x0000000000000024 R 0x4 GNU_EH_FRAME 0x000000000003c638 0x000000000003c638 0x000000000003c638 0x0000000000000a04 0x0000000000000a04 R 0x4 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 0x10 GNU_RELRO 0x000000000003f728 0x0000000000040728 0x0000000000040728 0x00000000000018d8 0x00000000000018d8 R 0x1 Section to Segment mapping: Segment Sections... 00 .note.gnu.build-id .gnu.hash .dynsym .dynstr .rela.dyn 01 .init .text 02 .rodata .eh_frame_hdr .eh_frame 03 .data.rel.ro .dynamic .got .bss 04 .dynamic 05 .note.gnu.build-id 06 .eh_frame_hdr 07 08 .data.rel.ro .dynamic .got ``` After this patch, `lld` + linker script produce the following: ``` Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000003830 0x0000000000003830 R 0x1000 LOAD 0x0000000000004000 0x0000000000004000 0x0000000000004000 0x00000000000340ae 0x00000000000340ae RWE 0x1000 LOAD 0x0000000000039000 0x0000000000039000 0x0000000000039000 0x0000000000007464 0x0000000000007464 R 0x1000 LOAD 0x0000000000041000 0x0000000000041000 0x0000000000041000 0x0000000000001920 0x0000000000002800 RW 0x1000 DYNAMIC 0x0000000000042198 0x0000000000042198 0x0000000000042198 0x00000000000000d0 0x00000000000000d0 RW 0x8 NOTE 0x0000000000001000 0x0000000000001000 0x0000000000001000 0x0000000000000018 0x0000000000000018 R 0x4 GNU_EH_FRAME 0x000000000003d42c 0x000000000003d42c 0x000000000003d42c 0x0000000000000a04 0x0000000000000a04 R 0x4 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 0x0 GNU_RELRO 0x0000000000041000 0x0000000000041000 0x0000000000041000 0x0000000000001920 0x0000000000002000 R 0x1 Section to Segment mapping: Segment Sections... 00 .note.gnu.build-id .gnu.hash .dynsym .dynstr .rela.dyn 01 .init .text 02 .rodata .eh_frame_hdr .eh_frame 03 .data.rel.ro .dynamic .got .bss 04 .dynamic 05 .note.gnu.build-id 06 .eh_frame_hdr 07 08 .data.rel.ro .dynamic .got ``` Signed-off-by: Nathaniel McCallum <[email protected]>
1 parent f7f2e60 commit 57c71d4

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

.cargo/config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ rustflags = [
44
"-C", "relocation-model=pic",
55
"-C", "link-args=-Wl,--sort-section=alignment -nostartfiles",
66
"-C", "link-self-contained=no",
7+
"-C", "link-arg=-Tlink.script",
8+
9+
# Using GNU ld on Fedora 34 produces this assertion:
10+
# = note: /usr/bin/ld: BFD version 2.35.2-3.fc34 assertion fail elf.c:6278
11+
# collect2: error: ld returned 1 exit status
12+
"-C", "link-arg=-fuse-ld=lld",
713
]

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jobs:
1010
with:
1111
toolchain: ${{ matrix.toolchain }}
1212
override: true
13+
- name: Install lld
14+
run: sudo apt install lld
1315
- uses: actions-rs/cargo@v1
1416
with:
1517
command: build

link.script

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
ENTRY(_start)
2+
3+
PHDRS {
4+
preamble PT_LOAD FILEHDR PHDRS;
5+
text PT_LOAD;
6+
rodata PT_LOAD;
7+
data PT_LOAD;
8+
9+
dynamic PT_DYNAMIC;
10+
note PT_NOTE;
11+
12+
frame 0x6474e550; /* GNU_EH_FRAME */
13+
stack 0x6474e551 FLAGS(6); /* GNU_STACK */
14+
relro 0x6474e552 FLAGS(4); /* GNU_RELRO */
15+
}
16+
17+
SECTIONS {
18+
. = SIZEOF_HEADERS;
19+
20+
. = ALIGN(CONSTANT(MAXPAGESIZE));
21+
.interp : { *(.interp) } :preamble
22+
.note.gnu.build-id : { *(.note.gnu.build-id) } :preamble :note
23+
.hash : { *(.hash) } :preamble
24+
.gnu.hash : { *(.gnu.hash) } :preamble
25+
.dynsym : { *(.dynsym) } :preamble
26+
.dynstr : { *(.dynstr) } :preamble
27+
.gnu.version : { *(.gnu.version) } :preamble
28+
.gnu.version_d : { *(.gnu.version_d) } :preamble
29+
.gnu.version_r : { *(.gnu.version_r) } :preamble
30+
.rela.dyn : { *(.rela.*) } :preamble
31+
.rela.plt : { *(.rela.plt) *(.rela.iplt) } :preamble
32+
33+
. = ALIGN(CONSTANT(MAXPAGESIZE));
34+
.init : { KEEP (*(SORT_NONE(.init))) } :text
35+
.fini : { KEEP (*(SORT_NONE(.fini))) } :text
36+
.text : { *(.text .text.*) } :text
37+
.plt : { *(.plt) *(.iplt) } :text
38+
.plt.got : { *(.plt.got) } :text
39+
.plt.sec : { *(.plt.sec) } :text
40+
41+
. = ALIGN(CONSTANT(MAXPAGESIZE));
42+
.rodata : { *(.rodata .rodata.*) } :rodata
43+
.rodata1 : { *(.rodata1) } :rodata
44+
.eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } :rodata :frame
45+
.eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } :rodata
46+
47+
. = ALIGN(CONSTANT(MAXPAGESIZE));
48+
.data.rel.ro : { *(.data.rel.ro .data.rel.ro.*) } :data :relro
49+
.dynamic : { *(.dynamic) } :data :relro :dynamic
50+
.got : { *(.got) } :data :relro
51+
.got.plt : { *(.got.plt) } :data :relro
52+
53+
. = ALIGN(CONSTANT(MAXPAGESIZE)); /* Align here? New section? */
54+
.eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } :data
55+
.data : { *(.data .data.*) } :data
56+
.data1 : { *(.data1) } :data
57+
.bss : { *(.bss .bss.*) } :data
58+
}

0 commit comments

Comments
 (0)