Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6098e79

Browse files
committed
Limit symbols exported from proc macros
Only __rustc_proc_macro_decls_*__ and rust_metadata_* need to be exported for proc macros to work. All other symbols only increase binary size and have the potential to conflict with symbols from the host compiler.
1 parent 760d8a2 commit 6098e79

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,7 @@ impl<'a> Linker for GccLinker<'a> {
656656
return;
657657
}
658658

659-
if crate_type == CrateType::ProcMacro {
660-
return;
661-
}
659+
// FIXME hide #[no_mangle] symbols for proc-macros
662660

663661
let is_windows = self.sess.target.is_like_windows;
664662
let path = tmpdir.join(if is_windows { "list.def" } else { "list" });

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,18 @@ fn exported_symbols_provider_local<'tcx>(
257257
}));
258258
}
259259

260-
if tcx.sess.crate_types().contains(&CrateType::Dylib) {
260+
if tcx.sess.crate_types().contains(&CrateType::Dylib)
261+
|| tcx.sess.crate_types().contains(&CrateType::ProcMacro)
262+
{
261263
let symbol_name = metadata_symbol_name(tcx);
262264
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
263265

264266
symbols.push((
265267
exported_symbol,
266268
SymbolExportInfo {
267-
level: SymbolExportLevel::Rust,
269+
level: SymbolExportLevel::C,
268270
kind: SymbolExportKind::Data,
269-
used: false,
271+
used: true,
270272
},
271273
));
272274
}

src/test/run-make-fulldeps/symbol-visibility/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ include ../tools.mk
55
NM=nm -D
66
CDYLIB_NAME=liba_cdylib.so
77
RDYLIB_NAME=liba_rust_dylib.so
8+
PROC_MACRO_NAME=liba_proc_macro.so
89
EXE_NAME=an_executable
910
COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.so
1011

1112
ifeq ($(UNAME),Darwin)
1213
NM=nm -gU
1314
CDYLIB_NAME=liba_cdylib.dylib
1415
RDYLIB_NAME=liba_rust_dylib.dylib
16+
PROC_MACRO_NAME=liba_proc_macro.dylib
1517
EXE_NAME=an_executable
1618
COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dylib
1719
endif
@@ -20,6 +22,7 @@ ifdef IS_WINDOWS
2022
NM=nm -g
2123
CDYLIB_NAME=liba_cdylib.dll.a
2224
RDYLIB_NAME=liba_rust_dylib.dll.a
25+
PROC_MACRO_NAME=liba_proc_macro.dll
2326
EXE_NAME=an_executable.exe
2427
COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dll.a
2528
endif
@@ -31,6 +34,7 @@ all:
3134
$(RUSTC) -Zshare-generics=no an_rlib.rs
3235
$(RUSTC) -Zshare-generics=no a_cdylib.rs
3336
$(RUSTC) -Zshare-generics=no a_rust_dylib.rs
37+
$(RUSTC) -Zshare-generics=no a_proc_macro.rs
3438
$(RUSTC) -Zshare-generics=no an_executable.rs
3539
$(RUSTC) -Zshare-generics=no a_cdylib.rs --crate-name combined_rlib_dylib --crate-type=rlib,cdylib
3640

@@ -54,6 +58,14 @@ all:
5458
# Check that a Rust dylib does not export generics if -Zshare-generics=no
5559
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "0" ]
5660

61+
# Check that a proc macro exports its public #[no_mangle] functions
62+
# FIXME avoid exporting #[no_mangle] symbols for proc macros
63+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
64+
# Check that a proc macro exports the public #[no_mangle] functions of dependencies
65+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
66+
# Check that a proc macro DOES NOT export any public Rust functions
67+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
68+
5769
# FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
5870
ifndef IS_WINDOWS
5971
# Check that an executable does not export any dynamic symbols
@@ -75,6 +87,7 @@ endif
7587
$(RUSTC) -Zshare-generics=yes an_rlib.rs
7688
$(RUSTC) -Zshare-generics=yes a_cdylib.rs
7789
$(RUSTC) -Zshare-generics=yes a_rust_dylib.rs
90+
$(RUSTC) -Zshare-generics=yes a_proc_macro.rs
7891
$(RUSTC) -Zshare-generics=yes an_executable.rs
7992

8093
# Check that a cdylib exports its public #[no_mangle] functions
@@ -94,6 +107,14 @@ endif
94107
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rlib)" -eq "1" ]
95108
[ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "1" ]
96109

110+
# Check that a proc macro exports its public #[no_mangle] functions
111+
# FIXME avoid exporting #[no_mangle] symbols for proc macros
112+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
113+
# Check that a proc macro exports the public #[no_mangle] functions of dependencies
114+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
115+
# Check that a proc macro DOES NOT export any public Rust functions
116+
[ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
117+
97118
ifndef IS_WINDOWS
98119
# Check that an executable does not export any dynamic symbols
99120
[ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "0" ]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_type = "proc-macro"]
2+
3+
extern crate an_rlib;
4+
5+
// This should not be exported
6+
#[no_mangle]
7+
extern "C" fn public_c_function_from_cdylib() {
8+
an_rlib::public_c_function_from_rlib();
9+
}

0 commit comments

Comments
 (0)