Skip to content

Commit 34fc6d0

Browse files
committed
Use localized symbols
1 parent cf9517f commit 34fc6d0

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

real_mode/Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

real_mode/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ edition = "2018"
88

99
[dependencies]
1010

11+
[build-dependencies]
12+
llvm-tools = "0.1.1"
13+
1114
[profile.release]
1215
opt-level = "s"
1316
lto = true

real_mode/build.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
use std::process::Command;
22
use std::env;
3-
use std::fs::{self, File};
43
use std::path::Path;
4+
use llvm_tools::{LlvmTools, exe};
55

66
fn main() {
77
let out_dir = env::var("OUT_DIR").unwrap();
8+
let llvm_tools = LlvmTools::new().expect("LLVM tools not found");
9+
let objcopy = llvm_tools.tool(&exe("llvm-objcopy")).expect("llvm-objcopy not found");
810

9-
// first stage
10-
let mut cmd = Command::new("cargo");
11-
cmd.arg("xbuild").arg("--release");
12-
cmd.arg("--manifest-path=first_stage/Cargo.toml");
13-
cmd.arg("-Z").arg("unstable-options");
14-
cmd.arg("--out-dir").arg(&out_dir);
15-
let status = cmd.status().unwrap();
16-
assert!(status.success());
17-
18-
// second stage
11+
build_subproject(Path::new("first_stage"), &["_start", "print_char"], &out_dir, &objcopy);
12+
build_subproject(Path::new("second_stage"), &["second_stage"], &out_dir, &objcopy);
13+
}
14+
15+
fn build_subproject(dir: &Path, global_symbols: &[&str], out_dir: &str, objcopy: &Path) {
16+
let dir_name = dir.file_name().unwrap().to_str().unwrap();
17+
let manifest_path = dir.join("Cargo.toml");
18+
let out_path = Path::new(&out_dir);
19+
assert!(global_symbols.len() > 0, "must have at least one global symbol");
20+
21+
// build
1922
let mut cmd = Command::new("cargo");
2023
cmd.arg("xbuild").arg("--release");
21-
cmd.arg("--manifest-path=second_stage/Cargo.toml");
24+
cmd.arg(format!("--manifest-path={}", manifest_path.display()));
2225
cmd.arg("-Z").arg("unstable-options");
2326
cmd.arg("--out-dir").arg(&out_dir);
27+
cmd.arg("--target-dir").arg("target");
28+
cmd.env("XBUILD_SYSROOT_PATH", format!("target/{}-sysroot", dir_name));
2429
let status = cmd.status().unwrap();
2530
assert!(status.success());
2631

27-
let concat_script = Path::new(&out_dir).join("concat.mri");
28-
fs::write(&concat_script, "
29-
create libreal_mode.a
30-
addlib libfirst_stage.a
31-
addlib libsecond_stage.a
32-
save
33-
end
34-
").unwrap();
35-
36-
// concat archives
37-
let mut cmd = Command::new("ar");
38-
cmd.arg("-M").stdin(File::open(concat_script).unwrap());
39-
cmd.current_dir(&out_dir);
32+
// localize symbols
33+
let mut cmd = Command::new(objcopy);
34+
for symbol in global_symbols {
35+
cmd.arg("-G").arg(symbol);
36+
}
37+
cmd.arg(out_path.join(format!("lib{}.a", dir_name)));
4038
let status = cmd.status().unwrap();
4139
assert!(status.success());
42-
40+
41+
// emit linker flags
4342
println!("cargo:rustc-link-search=native={}", out_dir);
44-
println!("cargo:rustc-link-lib=static=real_mode");
43+
println!("cargo:rustc-link-lib=static={}", dir_name);
4544
}

real_mode/linker.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SECTIONS {
99
_bootloader_start = .;
1010
.boot :
1111
{
12-
*(.boot)
12+
*first_stage*(.boot)
1313
}
1414
.first_stage_text :
1515
{

0 commit comments

Comments
 (0)