Skip to content

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,9 +1976,11 @@ fn add_linked_symbol_object(
cmd: &mut dyn Linker,
sess: &Session,
tmpdir: &Path,
symbols: &[(String, SymbolExportKind)],
crate_type: CrateType,
linked_symbols: &[(String, SymbolExportKind)],
exported_symbols: &[String],
) {
if symbols.is_empty() {
if linked_symbols.is_empty() && exported_symbols.is_empty() {
return;
}

Expand Down Expand Up @@ -2015,7 +2017,7 @@ fn add_linked_symbol_object(
None
};

for (sym, kind) in symbols.iter() {
for (sym, kind) in linked_symbols.iter() {
let symbol = file.add_symbol(object::write::Symbol {
name: sym.clone().into(),
value: 0,
Expand Down Expand Up @@ -2073,6 +2075,38 @@ fn add_linked_symbol_object(
}
}

if sess.target.is_like_msvc {
// Symbol visibility takes care of this for executables typically
let should_filter_symbols = if crate_type == CrateType::Executable {
sess.opts.unstable_opts.export_executable_symbols
} else {
true
};
if should_filter_symbols {
// 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 adds a
// `.drectve` section which lists all the symbols using /EXPORT arguments.
//
// The linker will read these arguments from the `.drectve` section 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.
let drectve = exported_symbols
.into_iter()
.map(|sym| format!(" /EXPORT:\"{sym}\""))
.collect::<Vec<_>>()
.join("");

let section =
file.add_section(vec![], b".drectve".to_vec(), object::SectionKind::Linker);
file.append_section_data(section, drectve.as_bytes(), 1);
}
}

let path = tmpdir.join("symbols.o");
let result = std::fs::write(&path, file.write().unwrap());
if let Err(error) = result {
Expand Down Expand Up @@ -2247,7 +2281,9 @@ fn linker_with_args(
cmd,
sess,
tmpdir,
crate_type,
&codegen_results.crate_info.linked_symbols[&crate_type],
&codegen_results.crate_info.exported_symbols[&crate_type],
);

// Sanitizer libraries.
Expand Down
21 changes: 4 additions & 17 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,19 +1086,10 @@ 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]) {
// 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 =
Expand All @@ -1116,10 +1107,6 @@ impl<'a> Linker for MsvcLinker<'a> {
// straight to exports.
writeln!(f, "LIBRARY")?;
writeln!(f, "EXPORTS")?;
for symbol in symbols {
Copy link
Contributor

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.

Copy link
Member Author

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.

debug!(" _{symbol}");
writeln!(f, " {symbol}")?;
}
};
if let Err(error) = res {
self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
Expand Down
Loading