Skip to content

Commit

Permalink
Make symbols.o trick work when linking with ld64
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Dec 4, 2024
1 parent c44b3d5 commit c6251a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
30 changes: 29 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2085,8 +2085,19 @@ fn add_linked_symbol_object(
file.set_mangling(object::write::Mangling::None);
}

// ld64 requires a relocation to load undefined symbols, see below.
let ld64_section_helper = if file.format() == object::BinaryFormat::MachO {
Some(file.add_section(
file.segment_name(object::write::StandardSegment::Text).to_vec(),
"__text".into(),
object::SectionKind::Text,
))
} else {
None
};

for (sym, kind) in symbols.iter() {
file.add_symbol(object::write::Symbol {
let symbol = file.add_symbol(object::write::Symbol {
name: sym.clone().into(),
value: 0,
size: 0,
Expand All @@ -2100,6 +2111,23 @@ fn add_linked_symbol_object(
section: object::write::SymbolSection::Undefined,
flags: object::SymbolFlags::None,
});

// TODO: Explain.

Check failure on line 2115 in compiler/rustc_codegen_ssa/src/back/link.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
if let Some(section) = ld64_section_helper {
// TODO: Use architecture-specific data.

Check failure on line 2117 in compiler/rustc_codegen_ssa/src/back/link.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
let offset = file.section_mut(section).append_data(&[0, 0, 0, 20], 4);
file.add_relocation(section, object::write::Relocation {
offset,
addend: 0,
symbol,
flags: object::write::RelocationFlags::MachO {
r_type: object::macho::ARM64_RELOC_BRANCH26,
r_pcrel: true,
r_length: 2,
},
})
.expect("failed adding relocation");
}
}

let path = tmpdir.join("symbols.o");
Expand Down
3 changes: 2 additions & 1 deletion tests/run-make/include-all-symbols-linking/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod foo {
#[link_section = ".rodata.STATIC"]
#[cfg_attr(target_os = "linux", link_section = ".rodata.STATIC")]
#[cfg_attr(target_vendor = "apple", link_section = "__DATA,STATIC")]
#[used]
static STATIC: [u32; 10] = [1; 10];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-make/include-all-symbols-linking/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
// See https://github.com/rust-lang/rust/pull/95604
// See https://github.com/rust-lang/rust/issues/47384

//@ only-linux
// TODO: Fix this file properly

Check failure on line 10 in tests/run-make/include-all-symbols-linking/rmake.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
// Reason: differences in object file formats on OSX and Windows
// causes errors in the llvm_objdump step

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

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

0 comments on commit c6251a8

Please sign in to comment.