Skip to content

Commit 3d43603

Browse files
committed
set subsections_via_symbols for ld64 helper sections
1 parent 65fa0ab commit 3d43603

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@ fn add_linked_symbol_object(
20012001
return;
20022002
}
20032003

2004-
let Some(mut file) = super::metadata::create_object_file(sess) else {
2004+
let Some(mut file) = super::metadata::create_object_file(sess, true) else {
20052005
return;
20062006
};
20072007

compiler/rustc_codegen_ssa/src/back/metadata.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a
200200
}
201201
}
202202

203-
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
203+
pub(crate) fn create_object_file(
204+
sess: &Session,
205+
set_subsections_via_symbols: bool,
206+
) -> Option<write::Object<'static>> {
204207
let endianness = match sess.target.options.endian {
205208
Endian::Little => Endianness::Little,
206209
Endian::Big => Endianness::Big,
@@ -221,6 +224,11 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
221224

222225
file.set_macho_build_version(macho_object_build_version_for_target(sess))
223226
}
227+
if binary_format == BinaryFormat::MachO {
228+
if set_subsections_via_symbols {
229+
file.set_subsections_via_symbols();
230+
}
231+
}
224232
if binary_format == BinaryFormat::Coff {
225233
// Disable the default mangler to avoid mangling the special "@feat.00" symbol name.
226234
let original_mangling = file.mangling();
@@ -455,7 +463,7 @@ pub(crate) fn create_wrapper_file(
455463
section_name: String,
456464
data: &[u8],
457465
) -> (Vec<u8>, MetadataPosition) {
458-
let Some(mut file) = create_object_file(sess) else {
466+
let Some(mut file) = create_object_file(sess, false) else {
459467
if sess.target.is_like_wasm {
460468
return (
461469
create_metadata_file_for_wasm(sess, data, &section_name),
@@ -543,7 +551,7 @@ pub fn create_compressed_metadata_file(
543551
packed_metadata.write_all(&(metadata.stub_or_full().len() as u64).to_le_bytes()).unwrap();
544552
packed_metadata.extend(metadata.stub_or_full());
545553

546-
let Some(mut file) = create_object_file(sess) else {
554+
let Some(mut file) = create_object_file(sess, false) else {
547555
if sess.target.is_like_wasm {
548556
return create_metadata_file_for_wasm(sess, &packed_metadata, ".rustc");
549557
}

tests/ui/linking/cdylib-no-mangle.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ only-apple
2+
//@ build-fail
3+
//@ dont-check-compiler-stderr
4+
//@ dont-check-compiler-stdout
5+
6+
// Regression test for <https://github.com/rust-lang/rust/issues/139744>.
7+
// Functions in the dynamic library marked with no_mangle should not be GC-ed.
8+
9+
#![crate_type = "cdylib"]
10+
11+
unsafe extern "C" {
12+
unsafe static THIS_SYMBOL_SHOULD_BE_UNDEFINED: usize;
13+
}
14+
15+
#[unsafe(no_mangle)]
16+
pub unsafe fn function_marked_with_no_mangle() {
17+
println!("FUNCTION_MARKED_WITH_NO_MANGLE = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
18+
}
19+
20+
//~? ERROR linking
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ build-pass
2+
3+
// Regression test for <https://github.com/rust-lang/rust/issues/139744>.
4+
// Functions in the binary marked with no_mangle should be GC-ed if they
5+
// are not indirectly referenced by main.
6+
7+
#![feature(used_with_arg)]
8+
9+
unsafe extern "C" {
10+
unsafe static THIS_SYMBOL_SHOULD_BE_UNDEFINED: usize;
11+
}
12+
13+
#[unsafe(no_mangle)]
14+
pub unsafe fn function_marked_with_no_mangle() {
15+
println!("FUNCTION_MARKED_WITH_NO_MANGLE = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
16+
}
17+
18+
#[used(compiler)]
19+
pub static FUNCTION_MARKED_WITH_USED: unsafe fn() = || {
20+
println!("FUNCTION_MARKED_WITH_USED = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
21+
};
22+
23+
fn main() {
24+
println!("MAIN");
25+
}

0 commit comments

Comments
 (0)