Skip to content

Commit fb67d22

Browse files
committed
Use the .drectve section for exporting symbols from dlls on Windows
While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.
1 parent 68ac5ab commit fb67d22

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,9 +1976,11 @@ fn add_linked_symbol_object(
19761976
cmd: &mut dyn Linker,
19771977
sess: &Session,
19781978
tmpdir: &Path,
1979-
symbols: &[(String, SymbolExportKind)],
1979+
crate_type: CrateType,
1980+
linked_symbols: &[(String, SymbolExportKind)],
1981+
exported_symbols: &[String],
19801982
) {
1981-
if symbols.is_empty() {
1983+
if linked_symbols.is_empty() && exported_symbols.is_empty() {
19821984
return;
19831985
}
19841986

@@ -2015,7 +2017,7 @@ fn add_linked_symbol_object(
20152017
None
20162018
};
20172019

2018-
for (sym, kind) in symbols.iter() {
2020+
for (sym, kind) in linked_symbols.iter() {
20192021
let symbol = file.add_symbol(object::write::Symbol {
20202022
name: sym.clone().into(),
20212023
value: 0,
@@ -2073,6 +2075,38 @@ fn add_linked_symbol_object(
20732075
}
20742076
}
20752077

2078+
if sess.target.is_like_msvc {
2079+
// Symbol visibility takes care of this for executables typically
2080+
let should_filter_symbols = if crate_type == CrateType::Executable {
2081+
sess.opts.unstable_opts.export_executable_symbols
2082+
} else {
2083+
true
2084+
};
2085+
if should_filter_symbols {
2086+
// Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
2087+
// export symbols from a dynamic library. When building a dynamic library,
2088+
// however, we're going to want some symbols exported, so this adds a
2089+
// `.drectve` section which lists all the symbols using /EXPORT arguments.
2090+
//
2091+
// The linker will read these arguments from the `.drectve` section and
2092+
// export all the symbols from the dynamic library. Note that this is not
2093+
// as simple as just exporting all the symbols in the current crate (as
2094+
// specified by `codegen.reachable`) but rather we also need to possibly
2095+
// export the symbols of upstream crates. Upstream rlibs may be linked
2096+
// statically to this dynamic library, in which case they may continue to
2097+
// transitively be used and hence need their symbols exported.
2098+
let drectve = exported_symbols
2099+
.into_iter()
2100+
.map(|sym| format!(" /EXPORT:\"{sym}\""))
2101+
.collect::<Vec<_>>()
2102+
.join("");
2103+
2104+
let section =
2105+
file.add_section(vec![], b".drectve".to_vec(), object::SectionKind::Linker);
2106+
file.append_section_data(section, drectve.as_bytes(), 1);
2107+
}
2108+
}
2109+
20762110
let path = tmpdir.join("symbols.o");
20772111
let result = std::fs::write(&path, file.write().unwrap());
20782112
if let Err(error) = result {
@@ -2247,7 +2281,9 @@ fn linker_with_args(
22472281
cmd,
22482282
sess,
22492283
tmpdir,
2284+
crate_type,
22502285
&codegen_results.crate_info.linked_symbols[&crate_type],
2286+
&codegen_results.crate_info.exported_symbols[&crate_type],
22512287
);
22522288

22532289
// Sanitizer libraries.

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,47 +1086,8 @@ impl<'a> Linker for MsvcLinker<'a> {
10861086
}
10871087
}
10881088

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]) {
1102-
// Symbol visibility takes care of this typically
1103-
if crate_type == CrateType::Executable {
1104-
let should_export_executable_symbols =
1105-
self.sess.opts.unstable_opts.export_executable_symbols;
1106-
if !should_export_executable_symbols {
1107-
return;
1108-
}
1109-
}
1110-
1111-
let path = tmpdir.join("lib.def");
1112-
let res: io::Result<()> = try {
1113-
let mut f = File::create_buffered(&path)?;
1114-
1115-
// Start off with the standard module name header and then go
1116-
// straight to exports.
1117-
writeln!(f, "LIBRARY")?;
1118-
writeln!(f, "EXPORTS")?;
1119-
for symbol in symbols {
1120-
debug!(" _{symbol}");
1121-
writeln!(f, " {symbol}")?;
1122-
}
1123-
};
1124-
if let Err(error) = res {
1125-
self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
1126-
}
1127-
let mut arg = OsString::from("/DEF:");
1128-
arg.push(path);
1129-
self.link_arg(&arg);
1089+
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, _symbols: &[String]) {
1090+
// We already add /EXPORT arguments to the .drectve section of symbols.o.
11301091
}
11311092

11321093
fn subsystem(&mut self, subsystem: &str) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for MSVC link.exe failing to export weak definitions from dlls.
2+
3+
//@ build-pass
4+
//@ only-msvc
5+
//@ revisions: link_exe lld
6+
//@[lld] needs-rust-lld
7+
//@[lld] compile-flags: -Zunstable-options -Clink-self-contained=+linker -Zlinker-features=+lld
8+
9+
#![feature(linkage)]
10+
#![crate_type = "cdylib"]
11+
12+
#[linkage = "weak"]
13+
#[no_mangle]
14+
pub fn weak_function() {}
15+
16+
#[linkage = "weak"]
17+
#[no_mangle]
18+
pub static WEAK_STATIC: u8 = 42;

0 commit comments

Comments
 (0)