File tree 3 files changed +52
-0
lines changed
compiler/rustc_codegen_ssa/src/back
3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -2015,6 +2015,12 @@ fn add_linked_symbol_object(
2015
2015
file. set_mangling ( object:: write:: Mangling :: None ) ;
2016
2016
}
2017
2017
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
+
2018
2024
// ld64 requires a relocation to load undefined symbols, see below.
2019
2025
// Not strictly needed if linking with lld, but might as well do it there too.
2020
2026
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
+
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
+ #[ cfg_attr( windows, link( name = "this_lib_does_not_exist" , kind = "raw-dylib" ) ) ]
10
+ unsafe extern "C" {
11
+ unsafe static THIS_SYMBOL_SHOULD_BE_UNDEFINED : usize ;
12
+ }
13
+
14
+ #[ unsafe( no_mangle) ]
15
+ pub unsafe fn function_marked_with_no_mangle ( ) {
16
+ println ! ( "FUNCTION_MARKED_WITH_NO_MANGLE = {}" , unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED } ) ;
17
+ }
18
+
19
+ #[ used( compiler) ]
20
+ pub static FUNCTION_MARKED_WITH_USED : unsafe fn ( ) = || {
21
+ println ! ( "FUNCTION_MARKED_WITH_USED = {}" , unsafe { THIS_SYMBOL_SHOULD_BE_UNDEFINED } ) ;
22
+ } ;
23
+
24
+ fn main ( ) {
25
+ println ! ( "MAIN" ) ;
26
+ }
You can’t perform that action at this time.
0 commit comments