summaryrefslogtreecommitdiffstats
path: root/tests/run-make/symbol-visibility
diff options
context:
space:
mode:
Diffstat (limited to 'tests/run-make/symbol-visibility')
-rw-r--r--tests/run-make/symbol-visibility/Makefile123
-rw-r--r--tests/run-make/symbol-visibility/a_cdylib.rs12
-rw-r--r--tests/run-make/symbol-visibility/a_proc_macro.rs9
-rw-r--r--tests/run-make/symbol-visibility/a_rust_dylib.rs15
-rw-r--r--tests/run-make/symbol-visibility/an_executable.rs7
-rw-r--r--tests/run-make/symbol-visibility/an_rlib.rs12
6 files changed, 178 insertions, 0 deletions
diff --git a/tests/run-make/symbol-visibility/Makefile b/tests/run-make/symbol-visibility/Makefile
new file mode 100644
index 000000000..9159af214
--- /dev/null
+++ b/tests/run-make/symbol-visibility/Makefile
@@ -0,0 +1,123 @@
+# ignore-cross-compile
+include ../tools.mk
+
+# ignore-windows-msvc
+
+NM=nm -D
+CDYLIB_NAME=liba_cdylib.so
+RDYLIB_NAME=liba_rust_dylib.so
+PROC_MACRO_NAME=liba_proc_macro.so
+EXE_NAME=an_executable
+COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.so
+
+ifeq ($(UNAME),Darwin)
+NM=nm -gU
+CDYLIB_NAME=liba_cdylib.dylib
+RDYLIB_NAME=liba_rust_dylib.dylib
+PROC_MACRO_NAME=liba_proc_macro.dylib
+EXE_NAME=an_executable
+COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dylib
+endif
+
+ifdef IS_WINDOWS
+NM=nm -g
+CDYLIB_NAME=liba_cdylib.dll.a
+RDYLIB_NAME=liba_rust_dylib.dll.a
+PROC_MACRO_NAME=liba_proc_macro.dll
+EXE_NAME=an_executable.exe
+COMBINED_CDYLIB_NAME=libcombined_rlib_dylib.dll.a
+endif
+
+# `grep` regex for symbols produced by either `legacy` or `v0` mangling
+RE_ANY_RUST_SYMBOL="_ZN.*h.*E\|_R[a-zA-Z0-9_]+"
+
+all:
+ $(RUSTC) -Zshare-generics=no an_rlib.rs
+ $(RUSTC) -Zshare-generics=no a_cdylib.rs
+ $(RUSTC) -Zshare-generics=no a_rust_dylib.rs
+ $(RUSTC) -Zshare-generics=no a_proc_macro.rs
+ $(RUSTC) -Zshare-generics=no an_executable.rs
+ $(RUSTC) -Zshare-generics=no a_cdylib.rs --crate-name combined_rlib_dylib --crate-type=rlib,cdylib
+
+ # Check that a cdylib exports its public #[no_mangle] functions
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
+ # Check that a cdylib exports the public #[no_mangle] functions of dependencies
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
+ # Check that a cdylib DOES NOT export any public Rust functions
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
+
+ # Check that a Rust dylib exports its monomorphic functions
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rust_dylib)" -eq "1" ]
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rust_dylib)" -eq "1" ]
+ # Check that a Rust dylib does not export generics if -Zshare-generics=no
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rust_dylib)" -eq "0" ]
+
+
+ # Check that a Rust dylib exports the monomorphic functions from its dependencies
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rlib)" -eq "1" ]
+ # Check that a Rust dylib does not export generics if -Zshare-generics=no
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "0" ]
+
+ # Check that a proc macro exports its public #[no_mangle] functions
+ # FIXME(#99978) avoid exporting #[no_mangle] symbols for proc macros
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
+ # Check that a proc macro exports the public #[no_mangle] functions of dependencies
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
+ # Check that a proc macro DOES NOT export any public Rust functions
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
+
+# FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
+ifndef IS_WINDOWS
+ # Check that an executable does not export any dynamic symbols
+ [ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "0" ]
+ [ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_rust_function_from_exe)" -eq "0" ]
+endif
+
+
+ # Check the combined case, where we generate a cdylib and an rlib in the same
+ # compilation session:
+ # Check that a cdylib exports its public #[no_mangle] functions
+ [ "$$($(NM) $(TMPDIR)/$(COMBINED_CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
+ # Check that a cdylib exports the public #[no_mangle] functions of dependencies
+ [ "$$($(NM) $(TMPDIR)/$(COMBINED_CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
+ # Check that a cdylib DOES NOT export any public Rust functions
+ [ "$$($(NM) $(TMPDIR)/$(COMBINED_CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
+
+
+ $(RUSTC) -Zshare-generics=yes an_rlib.rs
+ $(RUSTC) -Zshare-generics=yes a_cdylib.rs
+ $(RUSTC) -Zshare-generics=yes a_rust_dylib.rs
+ $(RUSTC) -Zshare-generics=yes a_proc_macro.rs
+ $(RUSTC) -Zshare-generics=yes an_executable.rs
+
+ # Check that a cdylib exports its public #[no_mangle] functions
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
+ # Check that a cdylib exports the public #[no_mangle] functions of dependencies
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
+ # Check that a cdylib DOES NOT export any public Rust functions
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
+
+ # Check that a Rust dylib exports its monomorphic functions, including generics this time
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rust_dylib)" -eq "1" ]
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rust_dylib)" -eq "1" ]
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rust_dylib)" -eq "1" ]
+
+ # Check that a Rust dylib exports the monomorphic functions from its dependencies
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_rust_function_from_rlib)" -eq "1" ]
+ [ "$$($(NM) $(TMPDIR)/$(RDYLIB_NAME) | grep -v __imp_ | grep -c public_generic_function_from_rlib)" -eq "1" ]
+
+ # Check that a proc macro exports its public #[no_mangle] functions
+ # FIXME(#99978) avoid exporting #[no_mangle] symbols for proc macros
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_cdylib)" -eq "1" ]
+ # Check that a proc macro exports the public #[no_mangle] functions of dependencies
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "1" ]
+ # Check that a proc macro DOES NOT export any public Rust functions
+ [ "$$($(NM) $(TMPDIR)/$(CDYLIB_NAME) | grep -v __imp_ | grep -c $(RE_ANY_RUST_SYMBOL))" -eq "0" ]
+
+ifndef IS_WINDOWS
+ # Check that an executable does not export any dynamic symbols
+ [ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_c_function_from_rlib)" -eq "0" ]
+ [ "$$($(NM) $(TMPDIR)/$(EXE_NAME) | grep -v __imp_ | grep -c public_rust_function_from_exe)" -eq "0" ]
+endif
diff --git a/tests/run-make/symbol-visibility/a_cdylib.rs b/tests/run-make/symbol-visibility/a_cdylib.rs
new file mode 100644
index 000000000..d4fbff85b
--- /dev/null
+++ b/tests/run-make/symbol-visibility/a_cdylib.rs
@@ -0,0 +1,12 @@
+#![crate_type="cdylib"]
+
+extern crate an_rlib;
+
+// This should not be exported
+pub fn public_rust_function_from_cdylib() {}
+
+// This should be exported
+#[no_mangle]
+pub extern "C" fn public_c_function_from_cdylib() {
+ an_rlib::public_c_function_from_rlib();
+}
diff --git a/tests/run-make/symbol-visibility/a_proc_macro.rs b/tests/run-make/symbol-visibility/a_proc_macro.rs
new file mode 100644
index 000000000..9fd1a8a67
--- /dev/null
+++ b/tests/run-make/symbol-visibility/a_proc_macro.rs
@@ -0,0 +1,9 @@
+#![crate_type = "proc-macro"]
+
+extern crate an_rlib;
+
+// This should not be exported
+#[no_mangle]
+extern "C" fn public_c_function_from_cdylib() {
+ an_rlib::public_c_function_from_rlib();
+}
diff --git a/tests/run-make/symbol-visibility/a_rust_dylib.rs b/tests/run-make/symbol-visibility/a_rust_dylib.rs
new file mode 100644
index 000000000..a47df0ab7
--- /dev/null
+++ b/tests/run-make/symbol-visibility/a_rust_dylib.rs
@@ -0,0 +1,15 @@
+#![crate_type="dylib"]
+
+extern crate an_rlib;
+
+// This should be exported
+pub fn public_rust_function_from_rust_dylib() {}
+
+// This should be exported
+#[no_mangle]
+pub extern "C" fn public_c_function_from_rust_dylib() {
+ let _ = public_generic_function_from_rust_dylib(1u16);
+}
+
+// This should be exported if -Zshare-generics=yes
+pub fn public_generic_function_from_rust_dylib<T>(x: T) -> T { x }
diff --git a/tests/run-make/symbol-visibility/an_executable.rs b/tests/run-make/symbol-visibility/an_executable.rs
new file mode 100644
index 000000000..3f5e125ad
--- /dev/null
+++ b/tests/run-make/symbol-visibility/an_executable.rs
@@ -0,0 +1,7 @@
+#![crate_type="bin"]
+
+extern crate an_rlib;
+
+pub fn public_rust_function_from_exe() {}
+
+fn main() {}
diff --git a/tests/run-make/symbol-visibility/an_rlib.rs b/tests/run-make/symbol-visibility/an_rlib.rs
new file mode 100644
index 000000000..3696422b1
--- /dev/null
+++ b/tests/run-make/symbol-visibility/an_rlib.rs
@@ -0,0 +1,12 @@
+#![crate_type="rlib"]
+
+pub fn public_rust_function_from_rlib() {}
+
+#[no_mangle]
+pub extern "C" fn public_c_function_from_rlib() {
+ let _ = public_generic_function_from_rlib(0u64);
+}
+
+pub fn public_generic_function_from_rlib<T>(x: T) -> T {
+ x
+}