1use std::{env, fs::File, io::Write, path::PathBuf};
2
3pub fn simink_gen_blinker(offset: usize) {
4 let target = env::var("TARGET").unwrap();
5 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
6 if target.starts_with("aarch64") {
7 let mut f = File::create(out.join("linker.ld")).unwrap();
8 writeln!(f, "OUTPUT_ARCH(aarch64)\nENTRY(_text)\nSECTIONS\n{{").unwrap();
9 writeln!(
10 f,
11 " /DISCARD/ : {{
12 *(.discard)
13 *(.discard.*)
14 *(.interp .dynamic)
15 *(.dynsym, .dynstr .hash .gnu.hash)
16 *(.eh_frame)
17 }}"
18 )
19 .unwrap();
20 writeln!(f, " . = {:#x};", offset).unwrap();
21 writeln!(
22 f,
23 " .head.text : {{
24 _text = .;
25 KEEP(*(.head.text))
26 }}
27 .text : {{
28 _stext = .;
29 . = ALIGN(8);
30 *(.text.hot .text.* .text.fixup .text.unlikely)
31 *(.fixup)
32 *(.gnu.warning)
33 . = ALIGN(16);
34 *(.got)
35 }}
36
37 .got.plt : {{ *(.got.plt) }}
38 . = ALIGN(0x10000);
39 _etext = .;
40
41 . = ALIGN(4096);
42 .rodata : AT(ADDR(.rodata)) {{
43 __start_rodata = .;
44 *(.rodata) *(.rodata.*)
45 }}
46 .rodata1 : AT(ADDR(.rodata1)) {{
47 *(.rodata1)
48 __end_rodata = .;
49 }}
50 .notes : AT(ADDR(.notes)) {{
51 __start_notes = .;
52 KEEP(*(.note.*))
53 __stop_notes = .;
54 }}
55
56 . = ALIGN(4096);
57 .data : AT(ADDR(.data)) {{
58 _sdata = .;
59 . = ALIGN(1 << 14);
60 __start_stack = .;
61 . = __start_stack + (1 << 14);
62 __top_stack = .;
63 . = ALIGN(4096);
64 *(.data*)
65 CONSTRUCTORS
66 _edata = .;
67 }}
68
69 __bss_start = .;
70 .sbss : AT(ADDR(.sbss)) {{
71 *(.dynsbss) *(.sbss) *(.scommon)
72 }}
73 .bss : AT(ADDR(.bss)) {{
74 *(.dynbss) *(.bss*) *(COMMON)
75 }}
76 . = ALIGN(8);
77 __bss_stop = .;
78 _end = .;
79 .stab 0 : {{ *(.stab) }} .stabstr 0 : {{ *(.stabstr) }} .stab.excl 0 : {{ *(.stab.excl) }} .stab.exclstr 0 : {{ *(.stab.exclstr) }} .stab.index 0 : {{ *(.stab.index) }} .stab.indexstr 0 : {{ *(.stab.indexstr) }}
80 .debug 0 : {{ *(.debug) }} .line 0 : {{ *(.line) }} .debug_srcinfo 0 : {{ *(.debug_srcinfo) }} .debug_sfnames 0 : {{ *(.debug_sfnames) }} .debug_aranges 0 : {{ *(.debug_aranges) }} .debug_pubnames 0 : {{ *(.debug_pubnames) }} .debug_info 0 : {{ *(.debug_info .gnu.linkonce.wi.*) }} .debug_abbrev 0 : {{ *(.debug_abbrev) }} .debug_line 0 : {{ *(.debug_line) }} .debug_frame 0 : {{ *(.debug_frame) }} .debug_str 0 : {{ *(.debug_str) }} .debug_loc 0 : {{ *(.debug_loc) }} .debug_macinfo 0 : {{ *(.debug_macinfo) }} .debug_pubtypes 0 : {{ *(.debug_pubtypes) }} .debug_ranges 0 : {{ *(.debug_ranges) }} .debug_weaknames 0 : {{ *(.debug_weaknames) }} .debug_funcnames 0 : {{ *(.debug_funcnames) }} .debug_typenames 0 : {{ *(.debug_typenames) }} .debug_varnames 0 : {{ *(.debug_varnames) }} .debug_gnu_pubnames 0 : {{ *(.debug_gnu_pubnames) }} .debug_gnu_pubtypes 0 : {{ *(.debug_gnu_pubtypes) }} .debug_types 0 : {{ *(.debug_types) }} .debug_addr 0 : {{ *(.debug_addr) }} .debug_line_str 0 : {{ *(.debug_line_str) }} .debug_loclists 0 : {{ *(.debug_loclists) }} .debug_macro 0 : {{ *(.debug_macro) }} .debug_names 0 : {{ *(.debug_names) }} .debug_rnglists 0 : {{ *(.debug_rnglists) }} .debug_str_offsets 0 : {{ *(.debug_str_offsets) }}
81 .comment 0 : {{ *(.comment) }} .symtab 0 : {{ *(.symtab) }} .strtab 0 : {{ *(.strtab) }} .shstrtab 0 : {{ *(.shstrtab) }}
82 .plt : {{
83 *(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt)
84 }}
85 ASSERT(SIZEOF(.plt) == 0, \"Unexpected run-time procedure linkages detected!\")
86 .data.rel.ro : {{ *(.data.rel.ro) }}
87 ASSERT(SIZEOF(.data.rel.ro) == 0, \"Unexpected RELRO detected!\")
88}}"
89 )
90 .unwrap();
91 }
92
93 println!("cargo:rustc-link-search={}", out.display());
94
95 println!("cargo:rerun-if-changed=build.rs");
96 println!("cargo:rustc-link-arg=-Tlinker.ld");
97}