]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Don't export builtins/panic/alloc syms
authorAlex Crichton <alex@alexcrichton.com>
Sun, 22 Jan 2017 20:53:35 +0000 (12:53 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 27 Jan 2017 07:47:39 +0000 (23:47 -0800)
This hides symbols from various unstable and implementation-detail
crates of the standard library. Although typically transitive exported
`pub extern` functions are exported from cdylibs, these crates aren't
necessary as they're all implementation details.

Closes #34493

src/liballoc_jemalloc/build.rs
src/liballoc_jemalloc/lib.rs
src/liballoc_jemalloc/pthread_atfork_dummy.c [new file with mode: 0644]
src/librustc_trans/back/symbol_export.rs

index 1edcb0b1f24ded01964c8fc3166e572136727372..1143df0c6302d85e022ea634ed0e1812b55af838 100644 (file)
@@ -181,4 +181,15 @@ fn main() {
     } else if !target.contains("windows") && !target.contains("musl") {
         println!("cargo:rustc-link-lib=pthread");
     }
+
+    // The pthread_atfork symbols is used by jemalloc on android but the really
+    // old android we're building on doesn't have them defined, so just make
+    // sure the symbols are available.
+    if target.contains("androideabi") {
+        println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
+        gcc::Config::new()
+            .flag("-fvisibility=hidden")
+            .file("pthread_atfork_dummy.c")
+            .compile("libpthread_atfork_dummy.a");
+    }
 }
index 241f8149d24d2fbd3d2f619fe2195156c3f9fb48..fc8a5455d1d07406a8e4557fd260534f441c3859 100644 (file)
@@ -143,7 +143,7 @@ pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
     // we're building on doesn't have them defined, so just make sure the symbols
     // are available.
     #[no_mangle]
-    #[cfg(target_os = "android")]
+    #[cfg(all(target_os = "android", not(cargobuild)))]
     pub extern "C" fn pthread_atfork(_prefork: *mut u8,
                                      _postfork_parent: *mut u8,
                                      _postfork_child: *mut u8)
diff --git a/src/liballoc_jemalloc/pthread_atfork_dummy.c b/src/liballoc_jemalloc/pthread_atfork_dummy.c
new file mode 100644 (file)
index 0000000..f798afe
--- /dev/null
@@ -0,0 +1,6 @@
+// See comments in build.rs for why this exists
+int pthread_atfork(void* prefork,
+                   void* postfork_parent,
+                   void* postfork_child) {
+  return 0;
+}
index 81be2d02f125b0797bbaa7ff009fe69a21703b49..bea3ca8df70e0babf243b0c40c210ddbe75bc4ae 100644 (file)
@@ -81,11 +81,25 @@ pub fn compute_from<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
         for cnum in scx.sess().cstore.crates() {
             debug_assert!(cnum != LOCAL_CRATE);
 
+            // If this crate is a plugin and/or a custom derive crate, then
+            // we're not even going to link those in so we skip those crates.
             if scx.sess().cstore.plugin_registrar_fn(cnum).is_some() ||
                scx.sess().cstore.derive_registrar_fn(cnum).is_some() {
                 continue;
             }
 
+            // Check to see if this crate is a "special runtime crate". These
+            // crates, implementation details of the standard library, typically
+            // have a bunch of `pub extern` and `#[no_mangle]` functions as the
+            // ABI between them. We don't want their symbols to have a `C`
+            // export level, however, as they're just implementation details.
+            // Down below we'll hardwire all of the symbols to the `Rust` export
+            // level instead.
+            let special_runtime_crate =
+                scx.sess().cstore.is_allocator(cnum) ||
+                scx.sess().cstore.is_panic_runtime(cnum) ||
+                scx.sess().cstore.is_compiler_builtins(cnum);
+
             let crate_exports = scx
                 .sess()
                 .cstore
@@ -93,7 +107,24 @@ pub fn compute_from<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
                 .iter()
                 .map(|&def_id| {
                     let name = Instance::mono(scx, def_id).symbol_name(scx);
-                    let export_level = export_level(scx, def_id);
+                    let export_level = if special_runtime_crate {
+                        // We can probably do better here by just ensuring that
+                        // it has hidden visibility rather than public
+                        // visibility, as this is primarily here to ensure it's
+                        // not stripped during LTO.
+                        //
+                        // In general though we won't link right if these
+                        // symbols are stripped, and LTO currently strips them.
+                        if name == "rust_eh_personality" ||
+                           name == "rust_eh_register_frames" ||
+                           name == "rust_eh_unregister_frames" {
+                            SymbolExportLevel::C
+                        } else {
+                            SymbolExportLevel::Rust
+                        }
+                    } else {
+                        export_level(scx, def_id)
+                    };
                     debug!("EXPORTED SYMBOL (re-export): {} ({:?})", name, export_level);
                     (name, export_level)
                 })