@@ -25,7 +25,6 @@ use rustc_middle::bug;
2525use rustc_middle:: lint:: lint_level;
2626use rustc_middle:: middle:: debugger_visualizer:: DebuggerVisualizerFile ;
2727use rustc_middle:: middle:: dependency_format:: Linkage ;
28- use rustc_middle:: middle:: exported_symbols:: SymbolExportKind ;
2928use rustc_session:: config:: {
3029 self , CFGuard , CrateType , DebugInfo , LinkerFeaturesCli , OutFileName , OutputFilenames ,
3130 OutputType , PrintKind , SplitDwarfKind , Strip ,
@@ -2054,84 +2053,6 @@ fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor
20542053 }
20552054}
20562055
2057- /// Add a synthetic object file that contains reference to all symbols that we want to expose to
2058- /// the linker.
2059- ///
2060- /// Background: we implement rlibs as static library (archives). Linkers treat archives
2061- /// differently from object files: all object files participate in linking, while archives will
2062- /// only participate in linking if they can satisfy at least one undefined reference (version
2063- /// scripts doesn't count). This causes `#[no_mangle]` or `#[used]` items to be ignored by the
2064- /// linker, and since they never participate in the linking, using `KEEP` in the linker scripts
2065- /// can't keep them either. This causes #47384.
2066- ///
2067- /// To keep them around, we could use `--whole-archive` and equivalents to force rlib to
2068- /// participate in linking like object files, but this proves to be expensive (#93791). Therefore
2069- /// we instead just introduce an undefined reference to them. This could be done by `-u` command
2070- /// line option to the linker or `EXTERN(...)` in linker scripts, however they does not only
2071- /// introduce an undefined reference, but also make them the GC roots, preventing `--gc-sections`
2072- /// from removing them, and this is especially problematic for embedded programming where every
2073- /// byte counts.
2074- ///
2075- /// This method creates a synthetic object file, which contains undefined references to all symbols
2076- /// that are necessary for the linking. They are only present in symbol table but not actually
2077- /// used in any sections, so the linker will therefore pick relevant rlibs for linking, but
2078- /// unused `#[no_mangle]` or `#[used]` can still be discard by GC sections.
2079- ///
2080- /// There's a few internal crates in the standard library (aka libcore and
2081- /// libstd) which actually have a circular dependence upon one another. This
2082- /// currently arises through "weak lang items" where libcore requires things
2083- /// like `rust_begin_unwind` but libstd ends up defining it. To get this
2084- /// circular dependence to work correctly we declare some of these things
2085- /// in this synthetic object.
2086- fn add_linked_symbol_object (
2087- cmd : & mut dyn Linker ,
2088- sess : & Session ,
2089- tmpdir : & Path ,
2090- symbols : & [ ( String , SymbolExportKind ) ] ,
2091- ) {
2092- if symbols. is_empty ( ) {
2093- return ;
2094- }
2095-
2096- let Some ( mut file) = super :: metadata:: create_object_file ( sess) else {
2097- return ;
2098- } ;
2099-
2100- if file. format ( ) == object:: BinaryFormat :: Coff {
2101- // NOTE(nbdd0121): MSVC will hang if the input object file contains no sections,
2102- // so add an empty section.
2103- file. add_section ( Vec :: new ( ) , ".text" . into ( ) , object:: SectionKind :: Text ) ;
2104-
2105- // We handle the name decoration of COFF targets in `symbol_export.rs`, so disable the
2106- // default mangler in `object` crate.
2107- file. set_mangling ( object:: write:: Mangling :: None ) ;
2108- }
2109-
2110- for ( sym, kind) in symbols. iter ( ) {
2111- file. add_symbol ( object:: write:: Symbol {
2112- name : sym. clone ( ) . into ( ) ,
2113- value : 0 ,
2114- size : 0 ,
2115- kind : match kind {
2116- SymbolExportKind :: Text => object:: SymbolKind :: Text ,
2117- SymbolExportKind :: Data => object:: SymbolKind :: Data ,
2118- SymbolExportKind :: Tls => object:: SymbolKind :: Tls ,
2119- } ,
2120- scope : object:: SymbolScope :: Unknown ,
2121- weak : false ,
2122- section : object:: write:: SymbolSection :: Undefined ,
2123- flags : object:: SymbolFlags :: None ,
2124- } ) ;
2125- }
2126-
2127- let path = tmpdir. join ( "symbols.o" ) ;
2128- let result = std:: fs:: write ( & path, file. write ( ) . unwrap ( ) ) ;
2129- if let Err ( error) = result {
2130- sess. dcx ( ) . emit_fatal ( errors:: FailedToWrite { path, error } ) ;
2131- }
2132- cmd. add_object ( & path) ;
2133- }
2134-
21352056/// Add object files containing code from the current crate.
21362057fn add_local_crate_regular_objects ( cmd : & mut dyn Linker , codegen_results : & CodegenResults ) {
21372058 for obj in codegen_results. modules . iter ( ) . filter_map ( |m| m. object . as_ref ( ) ) {
@@ -2289,13 +2210,6 @@ fn linker_with_args(
22892210 // Pre-link CRT objects.
22902211 add_pre_link_objects ( cmd, sess, flavor, link_output_kind, self_contained_crt_objects) ;
22912212
2292- add_linked_symbol_object (
2293- cmd,
2294- sess,
2295- tmpdir,
2296- & codegen_results. crate_info . linked_symbols [ & crate_type] ,
2297- ) ;
2298-
22992213 // Sanitizer libraries.
23002214 add_sanitizer_libraries ( sess, flavor, crate_type, cmd) ;
23012215
@@ -2684,7 +2598,7 @@ fn add_native_libs_from_crate(
26842598 NativeLibKind :: Static { bundle, whole_archive } => {
26852599 if link_static {
26862600 let bundle = bundle. unwrap_or ( true ) ;
2687- let whole_archive = whole_archive == Some ( true ) ;
2601+ let whole_archive = whole_archive. unwrap_or ( link_output_kind . is_dylib ( ) ) ;
26882602 if bundle && cnum != LOCAL_CRATE {
26892603 if let Some ( filename) = lib. filename {
26902604 // If rlib contains native libs as archives, they are unpacked to tmpdir.
@@ -2706,7 +2620,7 @@ fn add_native_libs_from_crate(
27062620 // link kind is unspecified.
27072621 if !link_output_kind. can_link_dylib ( ) && !sess. target . crt_static_allows_dylibs {
27082622 if link_static {
2709- cmd. link_staticlib_by_name ( name, verbatim, false ) ;
2623+ cmd. link_staticlib_by_name ( name, verbatim, link_output_kind . is_dylib ( ) ) ;
27102624 }
27112625 } else if link_dynamic {
27122626 cmd. link_dylib_by_name ( name, verbatim, true ) ;
@@ -2956,7 +2870,7 @@ fn add_static_crate(
29562870 let cratepath = & src. rlib . as_ref ( ) . unwrap ( ) . 0 ;
29572871
29582872 let mut link_upstream =
2959- |path : & Path | cmd. link_staticlib_by_path ( & rehome_lib_path ( sess, path) , false ) ;
2873+ |path : & Path | cmd. link_staticlib_by_path ( & rehome_lib_path ( sess, path) , true ) ;
29602874
29612875 if !are_upstream_rust_objects_already_included ( sess)
29622876 || ignored_for_lto ( sess, & codegen_results. crate_info , cnum)
0 commit comments