about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Woerister <michaelwoerister@posteo>2019-04-09 14:47:00 +0200
committerMichael Woerister <michaelwoerister@posteo>2019-04-09 14:47:00 +0200
commit724ca0584e2be0714edf2f30b86230666d7a9d19 (patch)
treeb1d6fefc28723601ecf869b940b09e4203e5ec57
parent3750348daff89741e3153e0e120aa70a45ff5b68 (diff)
downloadrust-724ca0584e2be0714edf2f30b86230666d7a9d19.tar.gz
rust-724ca0584e2be0714edf2f30b86230666d7a9d19.zip
Exclude profiler-generated symbols from MSVC __imp_-symbol workaround.
-rw-r--r--src/librustc_codegen_llvm/back/write.rs18
-rw-r--r--src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/Makefile11
-rw-r--r--src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/test.rs1
3 files changed, 29 insertions, 1 deletions
diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs
index f0ed201ad5c..3628adeb3e9 100644
--- a/src/librustc_codegen_llvm/back/write.rs
+++ b/src/librustc_codegen_llvm/back/write.rs
@@ -795,6 +795,7 @@ fn create_msvc_imps(
     } else {
         "\x01__imp_"
     };
+
     unsafe {
         let i8p_ty = Type::i8p_llcx(llcx);
         let globals = base::iter_globals(llmod)
@@ -802,14 +803,23 @@ fn create_msvc_imps(
                 llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage &&
                     llvm::LLVMIsDeclaration(val) == 0
             })
-            .map(move |val| {
+            .filter_map(|val| {
+                // Exclude some symbols that we know are not Rust symbols.
                 let name = CStr::from_ptr(llvm::LLVMGetValueName(val));
+                if ignored(name.to_bytes()) {
+                    None
+                } else {
+                    Some((val, name))
+                }
+            })
+            .map(move |(val, name)| {
                 let mut imp_name = prefix.as_bytes().to_vec();
                 imp_name.extend(name.to_bytes());
                 let imp_name = CString::new(imp_name).unwrap();
                 (imp_name, val)
             })
             .collect::<Vec<_>>();
+
         for (imp_name, val) in globals {
             let imp = llvm::LLVMAddGlobal(llmod,
                                           i8p_ty,
@@ -818,4 +828,10 @@ fn create_msvc_imps(
             llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
         }
     }
+
+    // Use this function to exclude certain symbols from `__imp` generation.
+    fn ignored(symbol_name: &[u8]) -> bool {
+        // These are symbols generated by LLVM's profiling instrumentation
+        symbol_name.starts_with(b"__llvm_profile_")
+    }
 }
diff --git a/src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/Makefile b/src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/Makefile
new file mode 100644
index 00000000000..dc52e91317a
--- /dev/null
+++ b/src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/Makefile
@@ -0,0 +1,11 @@
+-include ../tools.mk
+
+all:
+ifeq ($(PROFILER_SUPPORT),1)
+	$(RUSTC) -O -Ccodegen-units=1 -Z pgo-gen="$(TMPDIR)/test.profraw" --emit=llvm-ir test.rs
+	# We expect symbols starting with "__llvm_profile_".
+	$(CGREP) "__llvm_profile_" < $(TMPDIR)/test.ll
+	# We do NOT expect the "__imp_" version of these symbols.
+	$(CGREP) -v "__imp___llvm_profile_" < $(TMPDIR)/test.ll # 64 bit
+	$(CGREP) -v "__imp____llvm_profile_" < $(TMPDIR)/test.ll # 32 bit
+endif
diff --git a/src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/test.rs b/src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/test.rs
new file mode 100644
index 00000000000..f328e4d9d04
--- /dev/null
+++ b/src/test/run-make-fulldeps/pgo-gen-no-imp-symbols/test.rs
@@ -0,0 +1 @@
+fn main() {}