Skip to content

Commit 6e175fc

Browse files
committed
set subsections_via_symbols for ld64 helper sections
1 parent 65fa0ab commit 6e175fc

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Diff for: 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

Diff for: 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
}

Diff for: tests/ui/linking/executable-no-mangle-strip.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
unsafe extern "C" {
8+
unsafe static THIS_SYMBOL_SHOULD_BE_UNDEFINED: usize;
9+
}
10+
11+
#[unsafe(no_mangle)]
12+
pub unsafe fn function_marked_with_no_mangle() {
13+
println!("THIS_SYMBOL_SHOULD_BE_UNDEFINED = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
14+
}
15+
16+
#[used]
17+
pub static FUNCTION_MARKED_WITH_USED: unsafe fn() = || {
18+
println!("THIS_SYMBOL_SHOULD_BE_UNDEFINED = {}", unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED });
19+
};
20+
21+
pub fn marker() -> usize {
22+
0
23+
}
24+
25+
fn main() {
26+
assert_eq!(marker(), 0_usize);
27+
}

0 commit comments

Comments
 (0)