@@ -337,7 +337,12 @@ pub(crate) trait Linker {
337
337
fn debuginfo ( & mut self , strip : Strip , natvis_debugger_visualizers : & [ PathBuf ] ) ;
338
338
fn no_crt_objects ( & mut self ) ;
339
339
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
+ ) ;
341
346
fn subsystem ( & mut self , subsystem : & str ) ;
342
347
fn linker_plugin_lto ( & mut self ) ;
343
348
fn add_eh_frame_header ( & mut self ) { }
@@ -770,7 +775,12 @@ impl<'a> Linker for GccLinker<'a> {
770
775
}
771
776
}
772
777
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
+ ) {
774
784
// Symbol visibility in object files typically takes care of this.
775
785
if crate_type == CrateType :: Executable {
776
786
let should_export_executable_symbols =
@@ -799,7 +809,7 @@ impl<'a> Linker for GccLinker<'a> {
799
809
// Write a plain, newline-separated list of symbols
800
810
let res: io:: Result < ( ) > = try {
801
811
let mut f = File :: create_buffered ( & path) ?;
802
- for sym in symbols {
812
+ for ( sym, _ ) in symbols {
803
813
debug ! ( " _{sym}" ) ;
804
814
writeln ! ( f, "_{sym}" ) ?;
805
815
}
@@ -814,7 +824,7 @@ impl<'a> Linker for GccLinker<'a> {
814
824
// .def file similar to MSVC one but without LIBRARY section
815
825
// because LD doesn't like when it's empty
816
826
writeln ! ( f, "EXPORTS" ) ?;
817
- for symbol in symbols {
827
+ for ( symbol, _ ) in symbols {
818
828
debug ! ( " _{symbol}" ) ;
819
829
// Quote the name in case it's reserved by linker in some way
820
830
// (this accounts for names with dots in particular).
@@ -831,7 +841,7 @@ impl<'a> Linker for GccLinker<'a> {
831
841
writeln ! ( f, "{{" ) ?;
832
842
if !symbols. is_empty ( ) {
833
843
writeln ! ( f, " global:" ) ?;
834
- for sym in symbols {
844
+ for ( sym, _ ) in symbols {
835
845
debug ! ( " {sym};" ) ;
836
846
writeln ! ( f, " {sym};" ) ?;
837
847
}
@@ -1086,19 +1096,15 @@ impl<'a> Linker for MsvcLinker<'a> {
1086
1096
}
1087
1097
}
1088
1098
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
+
1102
1108
// Symbol visibility takes care of this typically
1103
1109
if crate_type == CrateType :: Executable {
1104
1110
let should_export_executable_symbols =
@@ -1116,10 +1122,6 @@ impl<'a> Linker for MsvcLinker<'a> {
1116
1122
// straight to exports.
1117
1123
writeln ! ( f, "LIBRARY" ) ?;
1118
1124
writeln ! ( f, "EXPORTS" ) ?;
1119
- for symbol in symbols {
1120
- debug ! ( " _{symbol}" ) ;
1121
- writeln ! ( f, " {symbol}" ) ?;
1122
- }
1123
1125
} ;
1124
1126
if let Err ( error) = res {
1125
1127
self . sess . dcx ( ) . emit_fatal ( errors:: LibDefWriteFailure { error } ) ;
@@ -1259,14 +1261,19 @@ impl<'a> Linker for EmLinker<'a> {
1259
1261
self . cc_arg ( "-nodefaultlibs" ) ;
1260
1262
}
1261
1263
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
+ ) {
1263
1270
debug ! ( "EXPORTED SYMBOLS:" ) ;
1264
1271
1265
1272
self . cc_arg ( "-s" ) ;
1266
1273
1267
1274
let mut arg = OsString :: from ( "EXPORTED_FUNCTIONS=" ) ;
1268
1275
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 < _ > > ( ) ,
1270
1277
)
1271
1278
. unwrap ( ) ;
1272
1279
debug ! ( "{encoded}" ) ;
@@ -1428,8 +1435,13 @@ impl<'a> Linker for WasmLd<'a> {
1428
1435
1429
1436
fn no_default_libraries ( & mut self ) { }
1430
1437
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 {
1433
1445
self . link_args ( & [ "--export" , sym] ) ;
1434
1446
}
1435
1447
@@ -1563,7 +1575,7 @@ impl<'a> Linker for L4Bender<'a> {
1563
1575
self . cc_arg ( "-nostdlib" ) ;
1564
1576
}
1565
1577
1566
- fn export_symbols ( & mut self , _: & Path , _: CrateType , _: & [ String ] ) {
1578
+ fn export_symbols ( & mut self , _: & Path , _: CrateType , _: & [ ( String , SymbolExportKind ) ] ) {
1567
1579
// ToDo, not implemented, copy from GCC
1568
1580
self . sess . dcx ( ) . emit_warn ( errors:: L4BenderExportingSymbolsUnimplemented ) ;
1569
1581
}
@@ -1720,12 +1732,17 @@ impl<'a> Linker for AixLinker<'a> {
1720
1732
1721
1733
fn no_default_libraries ( & mut self ) { }
1722
1734
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
+ ) {
1724
1741
let path = tmpdir. join ( "list.exp" ) ;
1725
1742
let res: io:: Result < ( ) > = try {
1726
1743
let mut f = File :: create_buffered ( & path) ?;
1727
1744
// FIXME: use llvm-nm to generate export list.
1728
- for symbol in symbols {
1745
+ for ( symbol, _ ) in symbols {
1729
1746
debug ! ( " _{symbol}" ) ;
1730
1747
writeln ! ( f, " {symbol}" ) ?;
1731
1748
}
@@ -1769,9 +1786,21 @@ fn for_each_exported_symbols_include_dep<'tcx>(
1769
1786
}
1770
1787
}
1771
1788
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 ) > {
1773
1793
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 ( ) ;
1775
1804
}
1776
1805
1777
1806
if let CrateType :: ProcMacro = crate_type {
@@ -1781,16 +1810,20 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St
1781
1810
}
1782
1811
}
1783
1812
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 ) > {
1785
1817
let mut symbols = Vec :: new ( ) ;
1786
1818
let export_threshold = symbol_export:: crates_export_threshold ( & [ crate_type] ) ;
1787
1819
for_each_exported_symbols_include_dep ( tcx, crate_type, |symbol, info, cnum| {
1788
1820
// Do not export mangled symbols from cdylibs and don't attempt to export compiler-builtins
1789
1821
// from any cdylib. The latter doesn't work anyway as we use hidden visibility for
1790
1822
// compiler-builtins. Most linkers silently ignore it, but ld64 gives a warning.
1791
1823
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 ,
1794
1827
) ) ;
1795
1828
symbol_export:: extend_exported_symbols ( & mut symbols, tcx, symbol, cnum) ;
1796
1829
}
@@ -1799,7 +1832,7 @@ fn exported_symbols_for_non_proc_macro(tcx: TyCtxt<'_>, crate_type: CrateType) -
1799
1832
symbols
1800
1833
}
1801
1834
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 ) > {
1803
1836
// `exported_symbols` will be empty when !should_codegen.
1804
1837
if !tcx. sess . opts . output_types . should_codegen ( ) {
1805
1838
return Vec :: new ( ) ;
@@ -1809,7 +1842,10 @@ fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<String> {
1809
1842
let proc_macro_decls_name = tcx. sess . generate_proc_macro_decls_symbol ( stable_crate_id) ;
1810
1843
let metadata_symbol_name = exported_symbols:: metadata_symbol_name ( tcx) ;
1811
1844
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
+ ]
1813
1849
}
1814
1850
1815
1851
pub ( crate ) fn linked_symbols (
@@ -1906,7 +1942,13 @@ impl<'a> Linker for PtxLinker<'a> {
1906
1942
1907
1943
fn ehcont_guard ( & mut self ) { }
1908
1944
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
+ }
1910
1952
1911
1953
fn subsystem ( & mut self , _subsystem : & str ) { }
1912
1954
@@ -1975,10 +2017,15 @@ impl<'a> Linker for LlbcLinker<'a> {
1975
2017
1976
2018
fn ehcont_guard ( & mut self ) { }
1977
2019
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
+ ) {
1979
2026
match _crate_type {
1980
2027
CrateType :: Cdylib => {
1981
- for sym in symbols {
2028
+ for ( sym, _ ) in symbols {
1982
2029
self . link_args ( & [ "--export-symbol" , sym] ) ;
1983
2030
}
1984
2031
}
@@ -2052,11 +2099,16 @@ impl<'a> Linker for BpfLinker<'a> {
2052
2099
2053
2100
fn ehcont_guard ( & mut self ) { }
2054
2101
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
+ ) {
2056
2108
let path = tmpdir. join ( "symbols" ) ;
2057
2109
let res: io:: Result < ( ) > = try {
2058
2110
let mut f = File :: create_buffered ( & path) ?;
2059
- for sym in symbols {
2111
+ for ( sym, _ ) in symbols {
2060
2112
writeln ! ( f, "{sym}" ) ?;
2061
2113
}
2062
2114
} ;
0 commit comments