Skip to content

Commit 073d679

Browse files
josephlrrbradford
authored andcommitted
Linker: Change to rust-lld for linking
When building for a custom or bare metal platform, it is often be more useful to use the LLVM linker shipped with Rust (rust-lld). As Rust is heavily integrated with LLVM, using LLVM's linker can result in smaller and simpler binaries. This change does two things, - Changes the linker script (`layout.ld`) to: - Correctly combine sections together - Remove `BLOCK` as it does the same thing as `ALIGN`. See: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/expressions.html - Changes `target.json` to use `rust-lld` The results: - The debug build now works!! - The release binary is now smaller and simpler - Size shrinks from 116 KB to 70 KB - Stripped size shinks from 89 KB to 49 KB - Number of sections shinks from 333 tp 10 - Program headers shrinks from 7 to 4 - The unnecessary dynamaic section, relocation section, and `.dynsym` section are now gone. - Symbol table entries shrink from 540 to 220 - We can now (potentially) use LTO Testing: - Ran with Firecrakcer using instructions in `README.md` - All builds (before and after this change) kernel panic as expected, due to the virtio block device reset issue. - Ran with `cloud-hypervisor`; everything boots/works normally. Signed-off-by: Joe Richey <[email protected]>
1 parent bbcb868 commit 073d679

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

layout.ld

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ SECTIONS
44
{
55
. = 1M;
66
start_of_text = . ;
7-
.text BLOCK(4K) : ALIGN(4K)
7+
.text : ALIGN(4K)
88
{
9-
*(.text)
9+
*(.text .text.*)
1010
}
1111
end_of_text = . ;
1212

1313
start_of_data = . ;
14-
.rodata BLOCK(4K) : ALIGN(4K)
14+
.rodata : ALIGN(4K)
1515
{
16-
*(.rodata)
16+
*(.rodata .rodata.*)
1717
}
1818

19-
.data BLOCK(4K) : ALIGN(4K)
19+
.data : ALIGN(4K)
2020
{
21-
*(.data)
21+
*(.data .data.*)
2222
}
2323
end_of_data = . ;
2424

25-
.bss BLOCK(4K) : ALIGN(4K)
25+
.bss : ALIGN(4K)
2626
{
2727
*(COMMON)
28-
*(.bss)
28+
*(.bss .bss.*)
2929
}
3030
}

target.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
"target-c-int-width": "32",
88
"os": "none",
99
"executables": true,
10-
"linker": "gcc",
11-
"linker-flavor": "gcc",
10+
"linker": "rust-lld",
11+
"linker-flavor": "ld.lld",
1212
"panic-strategy": "abort",
1313
"disable-redzone": true,
1414
"features": "-mmx,-sse,+soft-float",
1515
"pre-link-args": {
16-
"gcc": [
17-
"-Wl,--script=layout.ld",
18-
"-Wl,--nmagic",
19-
"-nostartfiles"
20-
]
16+
"ld.lld": [
17+
"--script=layout.ld"
18+
]
2119
}
2220
}

0 commit comments

Comments
 (0)