Skip to content

Commit c6251a8

Browse files
committed
Make symbols.o trick work when linking with ld64
1 parent c44b3d5 commit c6251a8

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -2085,8 +2085,19 @@ fn add_linked_symbol_object(
20852085
file.set_mangling(object::write::Mangling::None);
20862086
}
20872087

2088+
// ld64 requires a relocation to load undefined symbols, see below.
2089+
let ld64_section_helper = if file.format() == object::BinaryFormat::MachO {
2090+
Some(file.add_section(
2091+
file.segment_name(object::write::StandardSegment::Text).to_vec(),
2092+
"__text".into(),
2093+
object::SectionKind::Text,
2094+
))
2095+
} else {
2096+
None
2097+
};
2098+
20882099
for (sym, kind) in symbols.iter() {
2089-
file.add_symbol(object::write::Symbol {
2100+
let symbol = file.add_symbol(object::write::Symbol {
20902101
name: sym.clone().into(),
20912102
value: 0,
20922103
size: 0,
@@ -2100,6 +2111,23 @@ fn add_linked_symbol_object(
21002111
section: object::write::SymbolSection::Undefined,
21012112
flags: object::SymbolFlags::None,
21022113
});
2114+
2115+
// TODO: Explain.
2116+
if let Some(section) = ld64_section_helper {
2117+
// TODO: Use architecture-specific data.
2118+
let offset = file.section_mut(section).append_data(&[0, 0, 0, 20], 4);
2119+
file.add_relocation(section, object::write::Relocation {
2120+
offset,
2121+
addend: 0,
2122+
symbol,
2123+
flags: object::write::RelocationFlags::MachO {
2124+
r_type: object::macho::ARM64_RELOC_BRANCH26,
2125+
r_pcrel: true,
2126+
r_length: 2,
2127+
},
2128+
})
2129+
.expect("failed adding relocation");
2130+
}
21032131
}
21042132

21052133
let path = tmpdir.join("symbols.o");

tests/run-make/include-all-symbols-linking/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod foo {
2-
#[link_section = ".rodata.STATIC"]
2+
#[cfg_attr(target_os = "linux", link_section = ".rodata.STATIC")]
3+
#[cfg_attr(target_vendor = "apple", link_section = "__DATA,STATIC")]
34
#[used]
45
static STATIC: [u32; 10] = [1; 10];
56
}

tests/run-make/include-all-symbols-linking/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
// See https://github.com/rust-lang/rust/pull/95604
88
// See https://github.com/rust-lang/rust/issues/47384
99

10-
//@ only-linux
10+
// TODO: Fix this file properly
1111
// Reason: differences in object file formats on OSX and Windows
1212
// causes errors in the llvm_objdump step
1313

1414
use run_make_support::{dynamic_lib_name, llvm_objdump, llvm_readobj, rustc};
1515

1616
fn main() {
1717
rustc().crate_type("lib").input("lib.rs").run();
18-
rustc().crate_type("cdylib").link_args("-Tlinker.ld").input("main.rs").run();
18+
rustc().crate_type("cdylib").input("main.rs").run();
1919
// Ensure `#[used]` and `KEEP`-ed section is there
2020
llvm_objdump()
2121
.arg("--full-contents")

0 commit comments

Comments
 (0)