@@ -337,7 +337,12 @@ pub(crate) trait Linker {
337337 fn debuginfo ( & mut self , strip : Strip , natvis_debugger_visualizers : & [ PathBuf ] ) ;
338338 fn no_crt_objects ( & mut self ) ;
339339 fn no_default_libraries ( & mut self ) ;
340- fn export_symbols ( & mut self , tmpdir : & Path , crate_type : CrateType , symbols : & [ String ] ) ;
340+ fn export_symbols (
341+ & mut self ,
342+ tmpdir : & Path ,
343+ crate_type : CrateType ,
344+ symbols : & [ ( String , SymbolExportKind ) ] ,
345+ ) ;
341346 fn subsystem ( & mut self , subsystem : & str ) ;
342347 fn linker_plugin_lto ( & mut self ) ;
343348 fn add_eh_frame_header ( & mut self ) { }
@@ -770,7 +775,12 @@ impl<'a> Linker for GccLinker<'a> {
770775 }
771776 }
772777
773- fn export_symbols ( & mut self , tmpdir : & Path , crate_type : CrateType , symbols : & [ String ] ) {
778+ fn export_symbols (
779+ & mut self ,
780+ tmpdir : & Path ,
781+ crate_type : CrateType ,
782+ symbols : & [ ( String , SymbolExportKind ) ] ,
783+ ) {
774784 // Symbol visibility in object files typically takes care of this.
775785 if crate_type == CrateType :: Executable {
776786 let should_export_executable_symbols =
@@ -799,7 +809,7 @@ impl<'a> Linker for GccLinker<'a> {
799809 // Write a plain, newline-separated list of symbols
800810 let res: io:: Result < ( ) > = try {
801811 let mut f = File :: create_buffered ( & path) ?;
802- for sym in symbols {
812+ for ( sym, _ ) in symbols {
803813 debug ! ( " _{sym}" ) ;
804814 writeln ! ( f, "_{sym}" ) ?;
805815 }
@@ -814,7 +824,7 @@ impl<'a> Linker for GccLinker<'a> {
814824 // .def file similar to MSVC one but without LIBRARY section
815825 // because LD doesn't like when it's empty
816826 writeln ! ( f, "EXPORTS" ) ?;
817- for symbol in symbols {
827+ for ( symbol, _ ) in symbols {
818828 debug ! ( " _{symbol}" ) ;
819829 // Quote the name in case it's reserved by linker in some way
820830 // (this accounts for names with dots in particular).
@@ -831,7 +841,7 @@ impl<'a> Linker for GccLinker<'a> {
831841 writeln ! ( f, "{{" ) ?;
832842 if !symbols. is_empty ( ) {
833843 writeln ! ( f, " global:" ) ?;
834- for sym in symbols {
844+ for ( sym, _ ) in symbols {
835845 debug ! ( " {sym};" ) ;
836846 writeln ! ( f, " {sym};" ) ?;
837847 }
@@ -1086,19 +1096,15 @@ impl<'a> Linker for MsvcLinker<'a> {
10861096 }
10871097 }
10881098
1089- // Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
1090- // export symbols from a dynamic library. When building a dynamic library,
1091- // however, we're going to want some symbols exported, so this function
1092- // generates a DEF file which lists all the symbols.
1093- //
1094- // The linker will read this `*.def` file and export all the symbols from
1095- // the dynamic library. Note that this is not as simple as just exporting
1096- // all the symbols in the current crate (as specified by `codegen.reachable`)
1097- // but rather we also need to possibly export the symbols of upstream
1098- // crates. Upstream rlibs may be linked statically to this dynamic library,
1099- // in which case they may continue to transitively be used and hence need
1100- // their symbols exported.
1101- fn export_symbols ( & mut self , tmpdir : & Path , crate_type : CrateType , symbols : & [ String ] ) {
1099+ fn export_symbols (
1100+ & mut self ,
1101+ tmpdir : & Path ,
1102+ crate_type : CrateType ,
1103+ _symbols : & [ ( String , SymbolExportKind ) ] ,
1104+ ) {
1105+ // We already add /EXPORT arguments to the .drectve section of symbols.o. We generate
1106+ // a .DEF file here anyway as it might prevent auto-export of some symbols.
1107+
11021108 // Symbol visibility takes care of this typically
11031109 if crate_type == CrateType :: Executable {
11041110 let should_export_executable_symbols =
@@ -1116,10 +1122,6 @@ impl<'a> Linker for MsvcLinker<'a> {
11161122 // straight to exports.
11171123 writeln ! ( f, "LIBRARY" ) ?;
11181124 writeln ! ( f, "EXPORTS" ) ?;
1119- for symbol in symbols {
1120- debug ! ( " _{symbol}" ) ;
1121- writeln ! ( f, " {symbol}" ) ?;
1122- }
11231125 } ;
11241126 if let Err ( error) = res {
11251127 self . sess . dcx ( ) . emit_fatal ( errors:: LibDefWriteFailure { error } ) ;
@@ -1259,14 +1261,19 @@ impl<'a> Linker for EmLinker<'a> {
12591261 self . cc_arg ( "-nodefaultlibs" ) ;
12601262 }
12611263
1262- fn export_symbols ( & mut self , _tmpdir : & Path , _crate_type : CrateType , symbols : & [ String ] ) {
1264+ fn export_symbols (
1265+ & mut self ,
1266+ _tmpdir : & Path ,
1267+ _crate_type : CrateType ,
1268+ symbols : & [ ( String , SymbolExportKind ) ] ,
1269+ ) {
12631270 debug ! ( "EXPORTED SYMBOLS:" ) ;
12641271
12651272 self . cc_arg ( "-s" ) ;
12661273
12671274 let mut arg = OsString :: from ( "EXPORTED_FUNCTIONS=" ) ;
12681275 let encoded = serde_json:: to_string (
1269- & symbols. iter ( ) . map ( |sym| "_" . to_owned ( ) + sym) . collect :: < Vec < _ > > ( ) ,
1276+ & symbols. iter ( ) . map ( |( sym, _ ) | "_" . to_owned ( ) + sym) . collect :: < Vec < _ > > ( ) ,
12701277 )
12711278 . unwrap ( ) ;
12721279 debug ! ( "{encoded}" ) ;
@@ -1428,8 +1435,13 @@ impl<'a> Linker for WasmLd<'a> {
14281435
14291436 fn no_default_libraries ( & mut self ) { }
14301437
1431- fn export_symbols ( & mut self , _tmpdir : & Path , _crate_type : CrateType , symbols : & [ String ] ) {
1432- for sym in symbols {
1438+ fn export_symbols (
1439+ & mut self ,
1440+ _tmpdir : & Path ,
1441+ _crate_type : CrateType ,
1442+ symbols : & [ ( String , SymbolExportKind ) ] ,
1443+ ) {
1444+ for ( sym, _) in symbols {
14331445 self . link_args ( & [ "--export" , sym] ) ;
14341446 }
14351447
@@ -1563,7 +1575,7 @@ impl<'a> Linker for L4Bender<'a> {
15631575 self . cc_arg ( "-nostdlib" ) ;
15641576 }
15651577
1566- fn export_symbols ( & mut self , _: & Path , _: CrateType , _: & [ String ] ) {
1578+ fn export_symbols ( & mut self , _: & Path , _: CrateType , _: & [ ( String , SymbolExportKind ) ] ) {
15671579 // ToDo, not implemented, copy from GCC
15681580 self . sess . dcx ( ) . emit_warn ( errors:: L4BenderExportingSymbolsUnimplemented ) ;
15691581 }
@@ -1720,12 +1732,17 @@ impl<'a> Linker for AixLinker<'a> {
17201732
17211733 fn no_default_libraries ( & mut self ) { }
17221734
1723- fn export_symbols ( & mut self , tmpdir : & Path , _crate_type : CrateType , symbols : & [ String ] ) {
1735+ fn export_symbols (
1736+ & mut self ,
1737+ tmpdir : & Path ,
1738+ _crate_type : CrateType ,
1739+ symbols : & [ ( String , SymbolExportKind ) ] ,
1740+ ) {
17241741 let path = tmpdir. join ( "list.exp" ) ;
17251742 let res: io:: Result < ( ) > = try {
17261743 let mut f = File :: create_buffered ( & path) ?;
17271744 // FIXME: use llvm-nm to generate export list.
1728- for symbol in symbols {
1745+ for ( symbol, _ ) in symbols {
17291746 debug ! ( " _{symbol}" ) ;
17301747 writeln ! ( f, " {symbol}" ) ?;
17311748 }
@@ -1769,9 +1786,21 @@ fn for_each_exported_symbols_include_dep<'tcx>(
17691786 }
17701787}
17711788
1772- pub ( crate ) fn exported_symbols ( tcx : TyCtxt < ' _ > , crate_type : CrateType ) -> Vec < String > {
1789+ pub ( crate ) fn exported_symbols (
1790+ tcx : TyCtxt < ' _ > ,
1791+ crate_type : CrateType ,
1792+ ) -> Vec < ( String , SymbolExportKind ) > {
17731793 if let Some ( ref exports) = tcx. sess . target . override_export_symbols {
1774- return exports. iter ( ) . map ( ToString :: to_string) . collect ( ) ;
1794+ return exports
1795+ . iter ( )
1796+ . map ( |sym| {
1797+ (
1798+ sym. to_string ( ) ,
1799+ // FIXME use the correct export kind for this symbol
1800+ SymbolExportKind :: Text ,
1801+ )
1802+ } )
1803+ . collect ( ) ;
17751804 }
17761805
17771806 if let CrateType :: ProcMacro = crate_type {
@@ -1781,16 +1810,20 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St
17811810 }
17821811}
17831812
1784- fn exported_symbols_for_non_proc_macro ( tcx : TyCtxt < ' _ > , crate_type : CrateType ) -> Vec < String > {
1813+ fn exported_symbols_for_non_proc_macro (
1814+ tcx : TyCtxt < ' _ > ,
1815+ crate_type : CrateType ,
1816+ ) -> Vec < ( String , SymbolExportKind ) > {
17851817 let mut symbols = Vec :: new ( ) ;
17861818 let export_threshold = symbol_export:: crates_export_threshold ( & [ crate_type] ) ;
17871819 for_each_exported_symbols_include_dep ( tcx, crate_type, |symbol, info, cnum| {
17881820 // Do not export mangled symbols from cdylibs and don't attempt to export compiler-builtins
17891821 // from any cdylib. The latter doesn't work anyway as we use hidden visibility for
17901822 // compiler-builtins. Most linkers silently ignore it, but ld64 gives a warning.
17911823 if info. level . is_below_threshold ( export_threshold) && !tcx. is_compiler_builtins ( cnum) {
1792- symbols. push ( symbol_export:: exporting_symbol_name_for_instance_in_crate (
1793- tcx, symbol, cnum,
1824+ symbols. push ( (
1825+ symbol_export:: exporting_symbol_name_for_instance_in_crate ( tcx, symbol, cnum) ,
1826+ info. kind ,
17941827 ) ) ;
17951828 symbol_export:: extend_exported_symbols ( & mut symbols, tcx, symbol, cnum) ;
17961829 }
@@ -1799,7 +1832,7 @@ fn exported_symbols_for_non_proc_macro(tcx: TyCtxt<'_>, crate_type: CrateType) -
17991832 symbols
18001833}
18011834
1802- fn exported_symbols_for_proc_macro_crate ( tcx : TyCtxt < ' _ > ) -> Vec < String > {
1835+ fn exported_symbols_for_proc_macro_crate ( tcx : TyCtxt < ' _ > ) -> Vec < ( String , SymbolExportKind ) > {
18031836 // `exported_symbols` will be empty when !should_codegen.
18041837 if !tcx. sess . opts . output_types . should_codegen ( ) {
18051838 return Vec :: new ( ) ;
@@ -1809,7 +1842,10 @@ fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<String> {
18091842 let proc_macro_decls_name = tcx. sess . generate_proc_macro_decls_symbol ( stable_crate_id) ;
18101843 let metadata_symbol_name = exported_symbols:: metadata_symbol_name ( tcx) ;
18111844
1812- vec ! [ proc_macro_decls_name, metadata_symbol_name]
1845+ vec ! [
1846+ ( proc_macro_decls_name, SymbolExportKind :: Data ) ,
1847+ ( metadata_symbol_name, SymbolExportKind :: Data ) ,
1848+ ]
18131849}
18141850
18151851pub ( crate ) fn linked_symbols (
@@ -1906,7 +1942,13 @@ impl<'a> Linker for PtxLinker<'a> {
19061942
19071943 fn ehcont_guard ( & mut self ) { }
19081944
1909- fn export_symbols ( & mut self , _tmpdir : & Path , _crate_type : CrateType , _symbols : & [ String ] ) { }
1945+ fn export_symbols (
1946+ & mut self ,
1947+ _tmpdir : & Path ,
1948+ _crate_type : CrateType ,
1949+ _symbols : & [ ( String , SymbolExportKind ) ] ,
1950+ ) {
1951+ }
19101952
19111953 fn subsystem ( & mut self , _subsystem : & str ) { }
19121954
@@ -1975,10 +2017,15 @@ impl<'a> Linker for LlbcLinker<'a> {
19752017
19762018 fn ehcont_guard ( & mut self ) { }
19772019
1978- fn export_symbols ( & mut self , _tmpdir : & Path , _crate_type : CrateType , symbols : & [ String ] ) {
2020+ fn export_symbols (
2021+ & mut self ,
2022+ _tmpdir : & Path ,
2023+ _crate_type : CrateType ,
2024+ symbols : & [ ( String , SymbolExportKind ) ] ,
2025+ ) {
19792026 match _crate_type {
19802027 CrateType :: Cdylib => {
1981- for sym in symbols {
2028+ for ( sym, _ ) in symbols {
19822029 self . link_args ( & [ "--export-symbol" , sym] ) ;
19832030 }
19842031 }
@@ -2052,11 +2099,16 @@ impl<'a> Linker for BpfLinker<'a> {
20522099
20532100 fn ehcont_guard ( & mut self ) { }
20542101
2055- fn export_symbols ( & mut self , tmpdir : & Path , _crate_type : CrateType , symbols : & [ String ] ) {
2102+ fn export_symbols (
2103+ & mut self ,
2104+ tmpdir : & Path ,
2105+ _crate_type : CrateType ,
2106+ symbols : & [ ( String , SymbolExportKind ) ] ,
2107+ ) {
20562108 let path = tmpdir. join ( "symbols" ) ;
20572109 let res: io:: Result < ( ) > = try {
20582110 let mut f = File :: create_buffered ( & path) ?;
2059- for sym in symbols {
2111+ for ( sym, _ ) in symbols {
20602112 writeln ! ( f, "{sym}" ) ?;
20612113 }
20622114 } ;
0 commit comments