Skip to content

Commit 7539730

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 3129d37 commit 7539730

File tree

3 files changed

+66
-47
lines changed

3 files changed

+66
-47
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,9 +1957,11 @@ fn add_linked_symbol_object(
19571957
cmd: &mut dyn Linker,
19581958
sess: &Session,
19591959
tmpdir: &Path,
1960-
symbols: &[(String, SymbolExportKind)],
1960+
crate_type: CrateType,
1961+
linked_symbols: &[(String, SymbolExportKind)],
1962+
exported_symbols: &[(String, SymbolExportKind)],
19611963
) {
1962-
if symbols.is_empty() {
1964+
if linked_symbols.is_empty() && exported_symbols.is_empty() {
19631965
return;
19641966
}
19651967

@@ -1996,7 +1998,7 @@ fn add_linked_symbol_object(
19961998
None
19971999
};
19982000

1999-
for (sym, kind) in symbols.iter() {
2001+
for (sym, kind) in linked_symbols.iter() {
20002002
let symbol = file.add_symbol(object::write::Symbol {
20012003
name: sym.clone().into(),
20022004
value: 0,
@@ -2054,6 +2056,41 @@ fn add_linked_symbol_object(
20542056
}
20552057
}
20562058

2059+
if sess.target.is_like_msvc {
2060+
// Symbol visibility takes care of this for executables typically
2061+
let should_filter_symbols = if crate_type == CrateType::Executable {
2062+
sess.opts.unstable_opts.export_executable_symbols
2063+
} else {
2064+
true
2065+
};
2066+
if should_filter_symbols {
2067+
// Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
2068+
// export symbols from a dynamic library. When building a dynamic library,
2069+
// however, we're going to want some symbols exported, so this adds a
2070+
// `.drectve` section which lists all the symbols using /EXPORT arguments.
2071+
//
2072+
// The linker will read these arguments from the `.drectve` section and
2073+
// export all the symbols from the dynamic library. Note that this is not
2074+
// as simple as just exporting all the symbols in the current crate (as
2075+
// specified by `codegen.reachable`) but rather we also need to possibly
2076+
// export the symbols of upstream crates. Upstream rlibs may be linked
2077+
// statically to this dynamic library, in which case they may continue to
2078+
// transitively be used and hence need their symbols exported.
2079+
let drectve = exported_symbols
2080+
.into_iter()
2081+
.map(|(sym, kind)| match kind {
2082+
SymbolExportKind::Text | SymbolExportKind::Tls => format!(" /EXPORT:\"{sym}\""),
2083+
SymbolExportKind::Data => format!(" /EXPORT:\"{sym}\",DATA"),
2084+
})
2085+
.collect::<Vec<_>>()
2086+
.join("");
2087+
2088+
let section =
2089+
file.add_section(vec![], b".drectve".to_vec(), object::SectionKind::Linker);
2090+
file.append_section_data(section, drectve.as_bytes(), 1);
2091+
}
2092+
}
2093+
20572094
let path = tmpdir.join("symbols.o");
20582095
let result = std::fs::write(&path, file.write().unwrap());
20592096
if let Err(error) = result {
@@ -2228,7 +2265,9 @@ fn linker_with_args(
22282265
cmd,
22292266
sess,
22302267
tmpdir,
2268+
crate_type,
22312269
&codegen_results.crate_info.linked_symbols[&crate_type],
2270+
&codegen_results.crate_info.exported_symbols[&crate_type],
22322271
);
22332272

22342273
// Sanitizer libraries.

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,53 +1097,13 @@ impl<'a> Linker for MsvcLinker<'a> {
10971097
}
10981098
}
10991099

1100-
// Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
1101-
// export symbols from a dynamic library. When building a dynamic library,
1102-
// however, we're going to want some symbols exported, so this function
1103-
// generates a DEF file which lists all the symbols.
1104-
//
1105-
// The linker will read this `*.def` file and export all the symbols from
1106-
// the dynamic library. Note that this is not as simple as just exporting
1107-
// all the symbols in the current crate (as specified by `codegen.reachable`)
1108-
// but rather we also need to possibly export the symbols of upstream
1109-
// crates. Upstream rlibs may be linked statically to this dynamic library,
1110-
// in which case they may continue to transitively be used and hence need
1111-
// their symbols exported.
11121100
fn export_symbols(
11131101
&mut self,
1114-
tmpdir: &Path,
1115-
crate_type: CrateType,
1116-
symbols: &[(String, SymbolExportKind)],
1102+
_tmpdir: &Path,
1103+
_crate_type: CrateType,
1104+
_symbols: &[(String, SymbolExportKind)],
11171105
) {
1118-
// Symbol visibility takes care of this typically
1119-
if crate_type == CrateType::Executable {
1120-
let should_export_executable_symbols =
1121-
self.sess.opts.unstable_opts.export_executable_symbols;
1122-
if !should_export_executable_symbols {
1123-
return;
1124-
}
1125-
}
1126-
1127-
let path = tmpdir.join("lib.def");
1128-
let res: io::Result<()> = try {
1129-
let mut f = File::create_buffered(&path)?;
1130-
1131-
// Start off with the standard module name header and then go
1132-
// straight to exports.
1133-
writeln!(f, "LIBRARY")?;
1134-
writeln!(f, "EXPORTS")?;
1135-
for (symbol, kind) in symbols {
1136-
let kind_marker = if *kind == SymbolExportKind::Data { " DATA" } else { "" };
1137-
debug!(" _{symbol}");
1138-
writeln!(f, " {symbol}{kind_marker}")?;
1139-
}
1140-
};
1141-
if let Err(error) = res {
1142-
self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
1143-
}
1144-
let mut arg = OsString::from("/DEF:");
1145-
arg.push(path);
1146-
self.link_arg(&arg);
1106+
// We already add /EXPORT arguments to the .drectve section of symbols.o.
11471107
}
11481108

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

0 commit comments

Comments
 (0)