-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Use the .drectve section for exporting symbols from dlls on Windows #142568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bjorn3
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
bjorn3:windows_symbols_o_export
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,7 +337,12 @@ pub(crate) trait Linker { | |
fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]); | ||
fn no_crt_objects(&mut self); | ||
fn no_default_libraries(&mut self); | ||
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]); | ||
fn export_symbols( | ||
&mut self, | ||
tmpdir: &Path, | ||
crate_type: CrateType, | ||
symbols: &[(String, SymbolExportKind)], | ||
); | ||
fn subsystem(&mut self, subsystem: &str); | ||
fn linker_plugin_lto(&mut self); | ||
fn add_eh_frame_header(&mut self) {} | ||
|
@@ -770,7 +775,12 @@ impl<'a> Linker for GccLinker<'a> { | |
} | ||
} | ||
|
||
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) { | ||
fn export_symbols( | ||
&mut self, | ||
tmpdir: &Path, | ||
crate_type: CrateType, | ||
symbols: &[(String, SymbolExportKind)], | ||
) { | ||
// Symbol visibility in object files typically takes care of this. | ||
if crate_type == CrateType::Executable { | ||
let should_export_executable_symbols = | ||
|
@@ -799,7 +809,7 @@ impl<'a> Linker for GccLinker<'a> { | |
// Write a plain, newline-separated list of symbols | ||
let res: io::Result<()> = try { | ||
let mut f = File::create_buffered(&path)?; | ||
for sym in symbols { | ||
for (sym, _) in symbols { | ||
debug!(" _{sym}"); | ||
writeln!(f, "_{sym}")?; | ||
} | ||
|
@@ -814,7 +824,7 @@ impl<'a> Linker for GccLinker<'a> { | |
// .def file similar to MSVC one but without LIBRARY section | ||
// because LD doesn't like when it's empty | ||
writeln!(f, "EXPORTS")?; | ||
for symbol in symbols { | ||
for (symbol, _) in symbols { | ||
debug!(" _{symbol}"); | ||
// Quote the name in case it's reserved by linker in some way | ||
// (this accounts for names with dots in particular). | ||
|
@@ -831,7 +841,7 @@ impl<'a> Linker for GccLinker<'a> { | |
writeln!(f, "{{")?; | ||
if !symbols.is_empty() { | ||
writeln!(f, " global:")?; | ||
for sym in symbols { | ||
for (sym, _) in symbols { | ||
debug!(" {sym};"); | ||
writeln!(f, " {sym};")?; | ||
} | ||
|
@@ -1086,19 +1096,15 @@ impl<'a> Linker for MsvcLinker<'a> { | |
} | ||
} | ||
|
||
// Currently the compiler doesn't use `dllexport` (an LLVM attribute) to | ||
// export symbols from a dynamic library. When building a dynamic library, | ||
// however, we're going to want some symbols exported, so this function | ||
// generates a DEF file which lists all the symbols. | ||
// | ||
// The linker will read this `*.def` file and export all the symbols from | ||
// the dynamic library. Note that this is not as simple as just exporting | ||
// all the symbols in the current crate (as specified by `codegen.reachable`) | ||
// but rather we also need to possibly export the symbols of upstream | ||
// crates. Upstream rlibs may be linked statically to this dynamic library, | ||
// in which case they may continue to transitively be used and hence need | ||
// their symbols exported. | ||
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) { | ||
fn export_symbols( | ||
&mut self, | ||
tmpdir: &Path, | ||
crate_type: CrateType, | ||
_symbols: &[(String, SymbolExportKind)], | ||
) { | ||
// We already add /EXPORT arguments to the .drectve section of symbols.o. We generate | ||
// a .DEF file here anyway as it might prevent auto-export of some symbols. | ||
|
||
// Symbol visibility takes care of this typically | ||
if crate_type == CrateType::Executable { | ||
let should_export_executable_symbols = | ||
|
@@ -1116,10 +1122,6 @@ impl<'a> Linker for MsvcLinker<'a> { | |
// straight to exports. | ||
writeln!(f, "LIBRARY")?; | ||
writeln!(f, "EXPORTS")?; | ||
for symbol in symbols { | ||
debug!(" _{symbol}"); | ||
writeln!(f, " {symbol}")?; | ||
} | ||
}; | ||
if let Err(error) = res { | ||
self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error }); | ||
|
@@ -1259,14 +1261,19 @@ impl<'a> Linker for EmLinker<'a> { | |
self.cc_arg("-nodefaultlibs"); | ||
} | ||
|
||
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { | ||
fn export_symbols( | ||
&mut self, | ||
_tmpdir: &Path, | ||
_crate_type: CrateType, | ||
symbols: &[(String, SymbolExportKind)], | ||
) { | ||
debug!("EXPORTED SYMBOLS:"); | ||
|
||
self.cc_arg("-s"); | ||
|
||
let mut arg = OsString::from("EXPORTED_FUNCTIONS="); | ||
let encoded = serde_json::to_string( | ||
&symbols.iter().map(|sym| "_".to_owned() + sym).collect::<Vec<_>>(), | ||
&symbols.iter().map(|(sym, _)| "_".to_owned() + sym).collect::<Vec<_>>(), | ||
) | ||
.unwrap(); | ||
debug!("{encoded}"); | ||
|
@@ -1428,8 +1435,13 @@ impl<'a> Linker for WasmLd<'a> { | |
|
||
fn no_default_libraries(&mut self) {} | ||
|
||
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { | ||
for sym in symbols { | ||
fn export_symbols( | ||
&mut self, | ||
_tmpdir: &Path, | ||
_crate_type: CrateType, | ||
symbols: &[(String, SymbolExportKind)], | ||
) { | ||
for (sym, _) in symbols { | ||
self.link_args(&["--export", sym]); | ||
} | ||
|
||
|
@@ -1563,7 +1575,7 @@ impl<'a> Linker for L4Bender<'a> { | |
self.cc_arg("-nostdlib"); | ||
} | ||
|
||
fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[String]) { | ||
fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[(String, SymbolExportKind)]) { | ||
// ToDo, not implemented, copy from GCC | ||
self.sess.dcx().emit_warn(errors::L4BenderExportingSymbolsUnimplemented); | ||
} | ||
|
@@ -1720,12 +1732,17 @@ impl<'a> Linker for AixLinker<'a> { | |
|
||
fn no_default_libraries(&mut self) {} | ||
|
||
fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { | ||
fn export_symbols( | ||
&mut self, | ||
tmpdir: &Path, | ||
_crate_type: CrateType, | ||
symbols: &[(String, SymbolExportKind)], | ||
) { | ||
let path = tmpdir.join("list.exp"); | ||
let res: io::Result<()> = try { | ||
let mut f = File::create_buffered(&path)?; | ||
// FIXME: use llvm-nm to generate export list. | ||
for symbol in symbols { | ||
for (symbol, _) in symbols { | ||
debug!(" _{symbol}"); | ||
writeln!(f, " {symbol}")?; | ||
} | ||
|
@@ -1769,9 +1786,15 @@ fn for_each_exported_symbols_include_dep<'tcx>( | |
} | ||
} | ||
|
||
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> { | ||
pub(crate) fn exported_symbols( | ||
tcx: TyCtxt<'_>, | ||
crate_type: CrateType, | ||
) -> Vec<(String, SymbolExportKind)> { | ||
if let Some(ref exports) = tcx.sess.target.override_export_symbols { | ||
return exports.iter().map(ToString::to_string).collect(); | ||
return exports | ||
.iter() | ||
.map(|sym| (sym.to_string(), SymbolExportKind::Text /* FIXME */)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment here to explain what needs fixing (and, maybe, why it's difficult)? |
||
.collect(); | ||
} | ||
|
||
if let CrateType::ProcMacro = crate_type { | ||
|
@@ -1781,16 +1804,20 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St | |
} | ||
} | ||
|
||
fn exported_symbols_for_non_proc_macro(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> { | ||
fn exported_symbols_for_non_proc_macro( | ||
tcx: TyCtxt<'_>, | ||
crate_type: CrateType, | ||
) -> Vec<(String, SymbolExportKind)> { | ||
let mut symbols = Vec::new(); | ||
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); | ||
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| { | ||
// Do not export mangled symbols from cdylibs and don't attempt to export compiler-builtins | ||
// from any cdylib. The latter doesn't work anyway as we use hidden visibility for | ||
// compiler-builtins. Most linkers silently ignore it, but ld64 gives a warning. | ||
if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum) { | ||
symbols.push(symbol_export::exporting_symbol_name_for_instance_in_crate( | ||
tcx, symbol, cnum, | ||
symbols.push(( | ||
symbol_export::exporting_symbol_name_for_instance_in_crate(tcx, symbol, cnum), | ||
info.kind, | ||
)); | ||
symbol_export::extend_exported_symbols(&mut symbols, tcx, symbol, cnum); | ||
} | ||
|
@@ -1799,7 +1826,7 @@ fn exported_symbols_for_non_proc_macro(tcx: TyCtxt<'_>, crate_type: CrateType) - | |
symbols | ||
} | ||
|
||
fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<String> { | ||
fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<(String, SymbolExportKind)> { | ||
// `exported_symbols` will be empty when !should_codegen. | ||
if !tcx.sess.opts.output_types.should_codegen() { | ||
return Vec::new(); | ||
|
@@ -1809,7 +1836,10 @@ fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<String> { | |
let proc_macro_decls_name = tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id); | ||
let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx); | ||
|
||
vec![proc_macro_decls_name, metadata_symbol_name] | ||
vec![ | ||
(proc_macro_decls_name, SymbolExportKind::Data), | ||
(metadata_symbol_name, SymbolExportKind::Data), | ||
] | ||
} | ||
|
||
pub(crate) fn linked_symbols( | ||
|
@@ -1906,7 +1936,13 @@ impl<'a> Linker for PtxLinker<'a> { | |
|
||
fn ehcont_guard(&mut self) {} | ||
|
||
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, _symbols: &[String]) {} | ||
fn export_symbols( | ||
&mut self, | ||
_tmpdir: &Path, | ||
_crate_type: CrateType, | ||
_symbols: &[(String, SymbolExportKind)], | ||
) { | ||
} | ||
|
||
fn subsystem(&mut self, _subsystem: &str) {} | ||
|
||
|
@@ -1975,10 +2011,15 @@ impl<'a> Linker for LlbcLinker<'a> { | |
|
||
fn ehcont_guard(&mut self) {} | ||
|
||
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { | ||
fn export_symbols( | ||
&mut self, | ||
_tmpdir: &Path, | ||
_crate_type: CrateType, | ||
symbols: &[(String, SymbolExportKind)], | ||
) { | ||
match _crate_type { | ||
CrateType::Cdylib => { | ||
for sym in symbols { | ||
for (sym, _) in symbols { | ||
self.link_args(&["--export-symbol", sym]); | ||
} | ||
} | ||
|
@@ -2052,11 +2093,16 @@ impl<'a> Linker for BpfLinker<'a> { | |
|
||
fn ehcont_guard(&mut self) {} | ||
|
||
fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { | ||
fn export_symbols( | ||
&mut self, | ||
tmpdir: &Path, | ||
_crate_type: CrateType, | ||
symbols: &[(String, SymbolExportKind)], | ||
) { | ||
let path = tmpdir.join("symbols"); | ||
let res: io::Result<()> = try { | ||
let mut f = File::create_buffered(&path)?; | ||
for sym in symbols { | ||
for (sym, _) in symbols { | ||
writeln!(f, "{sym}")?; | ||
} | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe that we need the def file any more, we should be able to get away with passing
/DLL
to the linker.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the mingw docs I read something about the presence of a .DEF file disabling auto-export of certain symbols. I don't know if the same applies to MSVC, so I added it here just to be safe.