Skip to content

Commit 261a915

Browse files
committed
Use platform dependent mcount function
1 parent 237bf32 commit 261a915

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/librustc_codegen_llvm/attributes.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,37 @@ pub fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
7777
if cx.sess().instrument_mcount() {
7878
// Similar to `clang -pg` behavior. Handled by the
7979
// `post-inline-ee-instrument` LLVM pass.
80+
81+
// The function name varies on platforms.
82+
// See test/CodeGen/mcount.c in clang.
83+
let mcount_name = if cfg!(target_os = "netbsd") {
84+
const_cstr!("__mcount")
85+
} else if cfg!(any(
86+
target_arch = "mips", target_arch = "mips64",
87+
target_arch = "powerpc", target_arch = "powerpc64")) {
88+
const_cstr!("_mcount")
89+
} else if cfg!(target_os = "darwin") {
90+
const_cstr!("\01mcount")
91+
} else if cfg!(target_arch = "aarch64")
92+
&& (cfg!(target_os = "linux")
93+
|| (cfg!(target_os = "unknown") && cfg!(target_env = "gnu")))
94+
{
95+
const_cstr!("\01_mcount")
96+
} else if cfg!(target_arch = "arm")
97+
&& cfg!(any(target_os = "linux", target_os = "unknown"))
98+
{
99+
if cfg!(target_env = "gnu") {
100+
const_cstr!("\01__gnu_mcount_nc")
101+
} else {
102+
const_cstr!("\01mcount")
103+
}
104+
} else {
105+
const_cstr!("mcount")
106+
};
107+
80108
llvm::AddFunctionAttrStringValue(
81109
llfn, llvm::AttributePlace::Function,
82-
const_cstr!("instrument-function-entry-inlined"), const_cstr!("mcount"));
110+
const_cstr!("instrument-function-entry-inlined"), mcount_name);
83111
}
84112
}
85113

src/test/codegen/instrument-mcount.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ignore-tidy-linelength
2+
// compile-flags: -Z instrument-mcount
3+
4+
#![crate_type = "lib"]
5+
6+
// CHECK: attributes #{{.*}} "instrument-function-entry-inlined"="{{_*}}mcount" "no-frame-pointer-elim"="true"
7+
pub fn foo() {}

0 commit comments

Comments
 (0)