Skip to content

Commit 1c81162

Browse files
committed
Build as a 'bin' crate, linking with rust-lld
This eliminated the two awkward `ld` steps from the Makefile, and seems to produce a smaller `.efi`. It seems to work, based on quick testing in qemu. Using 'uefi' for target because apparently Rust cares: rust-lang/rust#71881 Some changes to the target specification here are from `rustc -Z unstable-options --print target-spec-json --target x86_64-unknown-uefi`. We probably want more of the settings from there, but most likely still need our own custom target to change some things.
1 parent e7567d8 commit 1c81162

File tree

5 files changed

+21
-45
lines changed

5 files changed

+21
-45
lines changed

Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ name = "system76_firmware_setup"
33
version = "1.0.0"
44
edition = "2018"
55

6-
[lib]
7-
name = "system76_firmware_setup"
8-
path = "src/lib.rs"
9-
crate-type = ["staticlib"]
10-
116
[profile.release]
127
lto = true
138

Makefile

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TARGET?=x86_64-efi-pe
1+
TARGET?=x86_64-uefi-pe
22

33
export LD=ld
44
export RUST_TARGET_PATH=$(CURDIR)/targets
@@ -40,40 +40,10 @@ $(BUILD)/efi.img: $(BUILD)/boot.efi
4040
mcopy -i $@.tmp res/startup.nsh ::startup.nsh
4141
mv $@.tmp $@
4242

43-
$(BUILD)/boot.efi: $(BUILD)/boot.o
44-
$(LD) \
45-
-m i386pep \
46-
--oformat pei-x86-64 \
47-
--dll \
48-
--image-base 0 \
49-
--section-alignment 32 \
50-
--file-alignment 32 \
51-
--major-os-version 0 \
52-
--minor-os-version 0 \
53-
--major-image-version 0 \
54-
--minor-image-version 0 \
55-
--major-subsystem-version 0 \
56-
--minor-subsystem-version 0 \
57-
--subsystem 11 \
58-
--heap 0,0 \
59-
--stack 0,0 \
60-
--pic-executable \
61-
--entry _start \
62-
--no-insert-timestamp \
63-
$< -o $@
64-
#--subsystem 10
65-
66-
$(BUILD)/boot.o: $(BUILD)/boot.a
67-
rm -rf $(BUILD)/boot
68-
mkdir $(BUILD)/boot
69-
cd $(BUILD)/boot && ar x ../boot.a
70-
ld -r $(BUILD)/boot/*.o -o $@
71-
72-
$(BUILD)/boot.a: Cargo.lock Cargo.toml res/* src/* src/*/*
43+
$(BUILD)/boot.efi: Cargo.lock Cargo.toml res/* src/* src/*/*
7344
mkdir -p $(BUILD)
7445
cargo rustc \
7546
-Z build-std=core,alloc \
76-
--lib \
7747
--target $(TARGET) \
7848
--release \
7949
-- \

firmware-setup.inf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
VALID_ARCHITECTURES = X64
1515

1616
[Binaries.X64]
17-
PE32|build/x86_64-efi-pe/boot.efi|*
17+
PE32|build/x86_64-uefi-pe/boot.efi|*
1818

src/lib.rs renamed to src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![no_std]
2+
#![no_main]
23
#![feature(llvm_asm)]
34
#![feature(const_fn)]
45
#![feature(core_intrinsics)]

targets/x86_64-efi-pe.json renamed to targets/x86_64-uefi-pe.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
{
2-
"linker": "false",
3-
"linker-flavor": "ld",
4-
"llvm-target": "x86_64-efi-pe",
2+
"linker": "rust-lld",
3+
"linker-flavor": "lld-link",
4+
"lld-flavor": "link",
5+
"llvm-target": "x86_64-unknown-windows",
56
"target-endian": "little",
67
"target-pointer-width": "64",
78
"target-c-int-width": "32",
89
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
910
"arch": "x86_64",
10-
"os": "efi",
11+
"os": "uefi",
1112
"env": "pe",
1213
"vendor": "unknown",
1314
"target-family": "none",
1415
"pre-link-args": {
15-
"ld": ["-m64", "-nostdlib", "-static"]
16+
"lld-link": [
17+
"/entry:_start",
18+
"/heap:0,0",
19+
"/stack:0,0",
20+
"/dll",
21+
"/base:0",
22+
"/align:32",
23+
"/filealign:32",
24+
"/subsystem:efi_boot_service_driver"
25+
]
1626
},
1727
"cpu": "core2",
1828
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-3dnow,-3dnowa,-avx,-avx2,+soft-float",
1929
"dynamic-linking": false,
20-
"executables": false,
30+
"executables": true,
2131
"relocation-model": "pic",
2232
"disable-redzone": true,
2333
"eliminate-frame-pointer": false,
24-
"exe-suffix": "",
34+
"exe-suffix": ".efi",
2535
"has-rpath": false,
2636
"no-compiler-rt": true,
2737
"no-default-libraries": true,

0 commit comments

Comments
 (0)