Skip to content

Commit 42c0494

Browse files
Use Default visibility for rustc-generated C symbol declarations
Non-default visibilities should only be used for definitions, not declarations, otherwise linking can fail. Co-authored-by: Collin Baker <[email protected]>
1 parent bfe5e8c commit 42c0494

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

compiler/rustc_codegen_llvm/src/declare.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
8484
unnamed: llvm::UnnamedAddr,
8585
fn_type: &'ll Type,
8686
) -> &'ll Value {
87-
// Declare C ABI functions with the visibility used by C by default.
88-
let visibility = Visibility::from_generic(self.tcx.sess.default_visibility());
89-
90-
declare_raw_fn(self, name, llvm::CCallConv, unnamed, visibility, fn_type)
87+
// Visibility should always be default for declarations, otherwise the linker may report an
88+
// error.
89+
declare_raw_fn(self, name, llvm::CCallConv, unnamed, Visibility::Default, fn_type)
9190
}
9291

9392
/// Declare an entry Function

tests/codegen/default-visibility.rs

+16
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ pub static tested_symbol: [u8; 6] = *b"foobar";
3131
// PROTECTED: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = protected constant
3232
// INTERPOSABLE: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = constant
3333
// DEFAULT: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = constant
34+
35+
pub fn do_memcmp(left: &[u8], right: &[u8]) -> i32 {
36+
left.cmp(right) as i32
37+
}
38+
39+
// CHECK: define {{.*}} @{{.*}}do_memcmp{{.*}} {
40+
// CHECK: }
41+
42+
// `do_memcmp` should invoke core::intrinsic::compare_bytes which emits a call
43+
// to the C symbol `memcmp` (at least on x86_64-unknown-linux-gnu). This symbol
44+
// should *not* be declared hidden or protected.
45+
46+
// HIDDEN: declare i32 @memcmp
47+
// PROTECTED: declare i32 @memcmp
48+
// INTERPOSABLE: declare i32 @memcmp
49+
// DEFAULT: declare i32 @memcmp

0 commit comments

Comments
 (0)