Skip to content

Commit 1ed3171

Browse files
Finish build script
1 parent 9a568db commit 1ed3171

File tree

4 files changed

+68
-45
lines changed

4 files changed

+68
-45
lines changed

build.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ use std::process::Command;
88

99
fn main() {
1010
// Read environment variables set by cargo
11+
let out_dir_path = env::var("OUT_DIR").expect("Missing OUT_DIR environment variable");
12+
let out_dir = Path::new(&out_dir_path);
13+
1114
let cargo_path = env::var("CARGO").expect("Missing CARGO environment variable");
1215
let cargo = Path::new(&cargo_path);
1316

1417
let manifest_dir_path = env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR environment variable");
1518
let manifest_dir = Path::new(&manifest_dir_path);
1619

17-
// Calculate target directory
18-
let current_dir = env::current_dir().expect("Couldn't get current directory");
19-
let target_dir_rel = manifest_dir.join("target");
20-
let target_dir = current_dir.join(target_dir_rel);
21-
2220
// Find the objcopy binary
2321
let llvm_tools = LlvmTools::new().expect("LLVM tools not found");
2422
let objcopy = llvm_tools
@@ -34,8 +32,8 @@ fn main() {
3432
"no_int13h_extensions",
3533
"dap_load_failed",
3634
],
37-
"i8086-bootsector.json",
38-
&target_dir,
35+
"../i8086-real_mode.json",
36+
&out_dir,
3937
&objcopy,
4038
&cargo,
4139
);
@@ -46,8 +44,8 @@ fn main() {
4644
&[
4745
"second_stage",
4846
],
49-
"i8086-stage_2.json",
50-
&target_dir,
47+
"../i8086-real_mode.json",
48+
&out_dir,
5149
&objcopy,
5250
&cargo,
5351
);
@@ -57,12 +55,13 @@ fn build_subproject(
5755
subproject_dir: &Path,
5856
global_symbols: &[&str],
5957
target_file_path: &str,
60-
target_dir: &Path,
58+
root_out_dir: &Path,
6159
objcopy: &Path,
6260
cargo: &Path,
6361
) {
6462
let subproject_name = subproject_dir.file_stem().expect("Couldn't get subproject name").to_str().expect("Subproject Name is not valid UTF-8");
6563
let target_file = Path::new(&target_file_path).file_stem().expect("Couldn't get target file stem");
64+
let target_dir = root_out_dir.join("target").join(&subproject_name);
6665

6766
// We have to export at least 1 symbol
6867
assert!(
@@ -82,8 +81,8 @@ fn build_subproject(
8281
// Cross-compile core (cargo-xbuild no longer needed)
8382
build_cmd.arg("-Zbuild-std=core");
8483

85-
// Use root package target directory
86-
build_cmd.arg(format!("--target-dir={}", &target_dir.join(&subproject_name).display()));
84+
// Use calculated target directory
85+
build_cmd.arg(format!("--target-dir={}", &target_dir.display()));
8786

8887
// Use the passed target
8988
build_cmd.arg("--target").arg(target_file_path);
@@ -93,7 +92,7 @@ fn build_subproject(
9392
assert!(build_status.success(), "Subcrate build failed!");
9493

9594
// Compute the path to the binary
96-
let binary_dir = target_dir.join(&subproject_name).join(&target_file).join("release");
95+
let binary_dir = target_dir.join(&target_file).join("release");
9796
let binary_path = binary_dir.join(format!("lib{}.a", &subproject_name));
9897

9998
// Use passed objcopy

linker.ld

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
ENTRY(_start)
2+
3+
SECTIONS {
4+
. = 0x500;
5+
_stack_start = .;
6+
. = 0x7c00;
7+
_stack_end = .;
8+
9+
.bootsector :
10+
{
11+
_bootloader_start = .;
12+
13+
*(.bootstrap)
14+
*bootsector*
15+
}
16+
17+
/* We write the magic number in the linker script to cause a link-time error if the bootloader overflows the 512 byte limit */
18+
. = 0x7c00 + 510;
19+
.magic :
20+
{
21+
SHORT(0xaa55)
22+
}
23+
24+
.rest_of_bootloader :
25+
{
26+
_rest_of_bootloader_start = .;
27+
28+
*stage_2*
29+
*core*
30+
31+
*(.text .text.*)
32+
*(.data .data.*)
33+
*(.rodata .rodata.*)
34+
*(.bss .bss.*)
35+
*(.got .got.plt)
36+
37+
. = ALIGN(512);
38+
39+
_rest_of_bootloader_end = .;
40+
}
41+
42+
/* Cause link time error if bootloader overflows 1MB limit */
43+
. = 0x100000;
44+
45+
_bootloader_end = .;
46+
47+
_protected_mode_stack_start = .;
48+
. += 0x1000;
49+
_protected_mode_stack_end = .;
50+
51+
/DISCARD/ :
52+
{
53+
*(.eh_frame)
54+
}
55+
}

src/real/bootsector/i8086-bootsector.json renamed to src/real/i8086-real_mode.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,5 @@
1717
"os": "none",
1818
"vendor": "unknown",
1919
"relocation_model": "static",
20-
"eliminate_frame_pointer": true,
21-
"pre-link-args": {
22-
"ld.lld": [
23-
"--script=real/linker.ld"
24-
]
25-
}
20+
"eliminate_frame_pointer": true
2621
}

src/real/stage_2/i8086-stage_2.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)