This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed
compiler/rustc_codegen_ssa/src/back Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -2015,6 +2015,12 @@ fn add_linked_symbol_object(
20152015 file. set_mangling ( object:: write:: Mangling :: None ) ;
20162016 }
20172017
2018+ if file. format ( ) == object:: BinaryFormat :: MachO {
2019+ // Divide up the sections into sub-sections via symbols for dead code stripping.
2020+ // Without this flag, unused `#[no_mangle]` or `#[used]` cannot be discard on MachO targets.
2021+ file. set_subsections_via_symbols ( ) ;
2022+ }
2023+
20182024 // ld64 requires a relocation to load undefined symbols, see below.
20192025 // Not strictly needed if linking with lld, but might as well do it there too.
20202026 let ld64_section_helper = if file. format ( ) == object:: BinaryFormat :: MachO {
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 1+ //@ run-pass
2+ //@ ignore-windows-gnu: only statics marked with used can be GC-ed on windows-gnu
3+
4+ // Regression test for <https://github.com/rust-lang/rust/issues/139744>.
5+ // Functions in the binary marked with no_mangle should be GC-ed if they
6+ // are not indirectly referenced by main.
7+
8+ #![ feature( used_with_arg) ]
9+
10+ #[ cfg_attr( windows, link( name = "this_lib_does_not_exist" , kind = "raw-dylib" ) ) ]
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+ #[ used( compiler) ]
21+ pub static FUNCTION_MARKED_WITH_USED : unsafe fn ( ) = || {
22+ println ! ( "FUNCTION_MARKED_WITH_USED = {}" , unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED } ) ;
23+ } ;
24+
25+ fn main ( ) {
26+ println ! ( "MAIN" ) ;
27+ }
You can’t perform that action at this time.
0 commit comments