Skip to content

Commit 7002dad

Browse files
authored
Rollup merge of rust-lang#59752 - Zoxc:dylib-fix, r=michaelwoerister
Limit dylib symbols This makes `windows-gnu` match the behavior of `windows-msvc`. It probably doesn't make sense to export these symbols on other platforms either.
2 parents f247393 + 0174797 commit 7002dad

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

src/librustc_codegen_ssa/back/linker.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -372,25 +372,18 @@ impl<'a> Linker for GccLinker<'a> {
372372
}
373373

374374
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
375-
// If we're compiling a dylib, then we let symbol visibility in object
376-
// files to take care of whether they're exported or not.
377-
//
378-
// If we're compiling a cdylib, however, we manually create a list of
379-
// exported symbols to ensure we don't expose any more. The object files
380-
// have far more public symbols than we actually want to export, so we
381-
// hide them all here.
382-
if crate_type == CrateType::Dylib ||
383-
crate_type == CrateType::ProcMacro {
384-
return
385-
}
375+
// We manually create a list of exported symbols to ensure we don't expose any more.
376+
// The object files have far more public symbols than we actually want to export,
377+
// so we hide them all here.
386378

387-
// Symbol visibility takes care of this for the WebAssembly.
388-
// Additionally the only known linker, LLD, doesn't support the script
389-
// arguments just yet
390-
if self.sess.target.target.arch == "wasm32" {
379+
if !self.sess.target.target.options.limit_rdylib_exports {
391380
return;
392381
}
393382

383+
if crate_type == CrateType::ProcMacro {
384+
return
385+
}
386+
394387
let mut arg = OsString::new();
395388
let path = tmpdir.join("list");
396389

src/librustc_target/spec/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,9 @@ pub struct TargetOptions {
751751
/// wasm32 where the whole program either has simd or not.
752752
pub simd_types_indirect: bool,
753753

754+
/// Pass a list of symbol which should be exported in the dylib to the linker.
755+
pub limit_rdylib_exports: bool,
756+
754757
/// If set, have the linker export exactly these symbols, instead of using
755758
/// the usual logic to figure this out from the crate itself.
756759
pub override_export_symbols: Option<Vec<String>>,
@@ -846,6 +849,7 @@ impl Default for TargetOptions {
846849
emit_debug_gdb_scripts: true,
847850
requires_uwtable: false,
848851
simd_types_indirect: true,
852+
limit_rdylib_exports: true,
849853
override_export_symbols: None,
850854
merge_functions: MergeFunctions::Aliases,
851855
target_mcount: "mcount".to_string(),
@@ -1152,6 +1156,7 @@ impl Target {
11521156
key!(emit_debug_gdb_scripts, bool);
11531157
key!(requires_uwtable, bool);
11541158
key!(simd_types_indirect, bool);
1159+
key!(limit_rdylib_exports, bool);
11551160
key!(override_export_symbols, opt_list);
11561161
key!(merge_functions, MergeFunctions)?;
11571162
key!(target_mcount);
@@ -1367,6 +1372,7 @@ impl ToJson for Target {
13671372
target_option_val!(emit_debug_gdb_scripts);
13681373
target_option_val!(requires_uwtable);
13691374
target_option_val!(simd_types_indirect);
1375+
target_option_val!(limit_rdylib_exports);
13701376
target_option_val!(override_export_symbols);
13711377
target_option_val!(merge_functions);
13721378
target_option_val!(target_mcount);

src/librustc_target/spec/solaris_base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub fn opts() -> TargetOptions {
88
has_rpath: true,
99
target_family: Some("unix".to_string()),
1010
is_like_solaris: true,
11+
limit_rdylib_exports: false, // Linker doesn't support this
1112

1213
.. Default::default()
1314
}

src/librustc_target/spec/wasm32_base.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ pub fn options() -> TargetOptions {
106106
// no dynamic linking, no need for default visibility!
107107
default_hidden_visibility: true,
108108

109+
// Symbol visibility takes care of this for the WebAssembly.
110+
// Additionally the only known linker, LLD, doesn't support the script
111+
// arguments just yet
112+
limit_rdylib_exports: false,
113+
109114
// we use the LLD shipped with the Rust toolchain by default
110115
linker: Some("rust-lld".to_owned()),
111116
lld_flavor: LldFlavor::Wasm,

src/librustc_target/spec/wasm32_experimental_emscripten.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn target() -> Result<Target, String> {
2424
is_like_emscripten: true,
2525
max_atomic_width: Some(32),
2626
post_link_args,
27+
limit_rdylib_exports: false,
2728
target_family: Some("unix".to_string()),
2829
.. Default::default()
2930
};

src/librustc_target/spec/wasm32_unknown_emscripten.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub fn target() -> Result<Target, String> {
2626
is_like_emscripten: true,
2727
max_atomic_width: Some(32),
2828
post_link_args,
29+
limit_rdylib_exports: false,
2930
target_family: Some("unix".to_string()),
3031
codegen_backend: "emscripten".to_string(),
3132
.. Default::default()

0 commit comments

Comments
 (0)