@@ -5,9 +5,7 @@ use std::path::{Path, PathBuf};
55use std:: { env, io, iter, mem, str} ;
66
77use cc:: windows_registry;
8- use object:: read:: archive:: ArchiveFile ;
9- use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
10- use rustc_data_structures:: memmap:: Mmap ;
8+ use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap } ;
119use rustc_hir:: def_id:: { CrateNum , LOCAL_CRATE } ;
1210use rustc_metadata:: {
1311 find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
@@ -1813,33 +1811,11 @@ fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<String> {
18131811 vec ! [ proc_macro_decls_name, metadata_symbol_name]
18141812}
18151813
1816- fn add_linked_objects (
1817- archive_path : & Path ,
1818- linked_symbols : & mut FxHashSet < String > ,
1819- ) -> Option < FxIndexSet < u64 > > {
1820- let archive_map = unsafe { Mmap :: map ( File :: open ( & archive_path) . unwrap ( ) ) . unwrap ( ) } ;
1821- let archive = ArchiveFile :: parse ( & * archive_map)
1822- . map_err ( |err| io:: Error :: new ( io:: ErrorKind :: InvalidData , err) )
1823- . unwrap ( ) ;
1824- let Some ( archive_symbols) = archive. symbols ( ) . unwrap ( ) else {
1825- return None ;
1826- } ;
1827- let mut offsets = FxIndexSet :: default ( ) ;
1828- for symbol in archive_symbols {
1829- let symbol = symbol. unwrap ( ) ;
1830- let name = std:: str:: from_utf8 ( symbol. name ( ) ) . unwrap ( ) ;
1831- if linked_symbols. remove ( name) {
1832- offsets. insert ( symbol. offset ( ) . 0 ) ;
1833- }
1834- }
1835- Some ( offsets)
1836- }
1837-
18381814pub ( crate ) fn linked_objects (
18391815 tcx : TyCtxt < ' _ > ,
18401816 crate_type : CrateType ,
18411817 linked_symbols : & mut Vec < ( String , SymbolExportKind ) > ,
1842- ) -> FxIndexMap < CrateNum , FxIndexSet < u64 > > {
1818+ ) -> FxIndexMap < CrateNum , FxHashSet < String > > {
18431819 match crate_type {
18441820 CrateType :: Executable | CrateType :: Cdylib | CrateType :: Dylib => ( ) ,
18451821 CrateType :: Staticlib | CrateType :: ProcMacro | CrateType :: Rlib => {
@@ -1852,14 +1828,21 @@ pub(crate) fn linked_objects(
18521828 are_upstream_rust_objects_already_included ( tcx. sess ) ;
18531829 let export_threshold = symbol_export:: crates_export_threshold ( & [ crate_type] ) ;
18541830 for_each_exported_symbols_include_dep ( tcx, crate_type, |exported_symbols, cnum| {
1831+ let exported_symbols = exported_symbols
1832+ . iter ( )
1833+ . filter ( |( _, info) | info. level . is_below_threshold ( export_threshold) || info. used ) ;
18551834 if cnum == LOCAL_CRATE {
1856- // We don't know here if the symbols are undefined, so we add them all.
1857- // Since the local crate is always linked directly to object files, `#[used]` works as expected .
1835+ // Since the local crate is always linked directly to object files, `#[used]` works as expected,
1836+ // we only need add undefined symbols .
18581837 linked_symbols. extend (
18591838 exported_symbols
1860- . iter ( )
1861- . filter ( |( _, info) | {
1862- info. level . is_below_threshold ( export_threshold) || info. used
1839+ . filter ( |( symbol, _) | match symbol {
1840+ ExportedSymbol :: NonGeneric { cgu, .. } => cgu. is_none ( ) ,
1841+ ExportedSymbol :: Generic ( ..)
1842+ | ExportedSymbol :: DropGlue ( ..)
1843+ | ExportedSymbol :: AsyncDropGlueCtorShim ( ..) => false ,
1844+ ExportedSymbol :: ThreadLocalShim ( _def_id) => false ,
1845+ ExportedSymbol :: NoDefId ( ..) => true ,
18631846 } )
18641847 . map ( |& ( symbol, info) | {
18651848 (
@@ -1872,35 +1855,33 @@ pub(crate) fn linked_objects(
18721855 ) ;
18731856 return ;
18741857 }
1875- // TODO: let lto = upstream_rust_objects_already_included && !ignored_for_lto(tcx.sess, &codegen_results.crate_info, cnum);
1858+ // FIXME: should be ` let lto = upstream_rust_objects_already_included && !ignored_for_lto(tcx.sess, &codegen_results.crate_info, cnum);`
18761859 let lto = upstream_rust_objects_already_included;
1877- if lto {
1878- return ;
1860+ let mut cgus = FxHashSet :: default ( ) ;
1861+ for & ( symbol, info) in exported_symbols {
1862+ match symbol {
1863+ ExportedSymbol :: NonGeneric { cgu : Some ( cgu) , .. } => {
1864+ if !lto {
1865+ cgus. insert ( cgu. as_str ( ) . to_string ( ) ) ;
1866+ }
1867+ }
1868+ ExportedSymbol :: NonGeneric { cgu : None , .. } | ExportedSymbol :: NoDefId ( ..) => {
1869+ // Unresolved symbols may come from external libraries.
1870+ linked_symbols. push ( (
1871+ symbol_export:: linking_symbol_name_for_instance_in_crate ( tcx, symbol, cnum) ,
1872+ info. kind ,
1873+ ) ) ;
1874+ }
1875+ ExportedSymbol :: Generic ( ..)
1876+ | ExportedSymbol :: DropGlue ( ..)
1877+ | ExportedSymbol :: AsyncDropGlueCtorShim ( ..)
1878+ | ExportedSymbol :: ThreadLocalShim ( ..) => { }
1879+ } ;
18791880 }
1880- let symbols: Vec < _ > = exported_symbols
1881- . iter ( )
1882- . filter ( |( _, info) | info. level . is_below_threshold ( export_threshold) || info. used )
1883- . map ( |& ( symbol, info) | {
1884- (
1885- symbol_export:: linking_symbol_name_for_instance_in_crate ( tcx, symbol, cnum) ,
1886- info. kind ,
1887- )
1888- } )
1889- . collect ( ) ;
1890- if symbols. is_empty ( ) {
1881+ if cgus. is_empty ( ) {
18911882 return ;
18921883 }
1893- let used_crate_source = tcx. used_crate_source ( cnum) ;
1894- let cratepath = & used_crate_source. rlib . as_ref ( ) . unwrap ( ) . 0 ;
1895- let mut crate_linked_symbols: FxHashSet < _ > =
1896- symbols. iter ( ) . map ( |( symbol, _) | symbol. to_string ( ) ) . collect ( ) ;
1897- if let Some ( archive_offsets) = add_linked_objects ( cratepath, & mut crate_linked_symbols) {
1898- objects. insert ( cnum, archive_offsets) ;
1899- }
1900- // Unresolved symbols may come from external libraries.
1901- linked_symbols. extend (
1902- symbols. into_iter ( ) . filter ( |( symbol, _) | crate_linked_symbols. contains ( symbol) ) ,
1903- ) ;
1884+ objects. insert ( cnum, cgus) ;
19041885 } ) ;
19051886
19061887 objects
0 commit comments