Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use platform dependent mcount function #59506

Merged
merged 6 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/librustc_codegen_llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,37 @@ pub fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
if cx.sess().instrument_mcount() {
// Similar to `clang -pg` behavior. Handled by the
// `post-inline-ee-instrument` LLVM pass.

// The function name varies on platforms.
// See test/CodeGen/mcount.c in clang.
let mcount_name = if cfg!(target_os = "netbsd") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cfg is wrong here, because it will be checking for the host (the computer running the compiler) rather than for the target (whatever is intended to run the final executable).

You should be checking for stuff in sess().target or, even better, make this name a part of target definitions and use that value directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this name a part of target definitions and use that value directly.

The targets are defined in src/librustc_target/spec/(e.g. like this)? If so, I should add target_mcount or something in Ok(Target {..}), right?

https://github.com/rust-lang/rust/blob/master/src/librustc_target/spec/aarch64_unknown_netbsd.rs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, I should add target_mcount or something in Ok(Target {..}), right?

Yeah.

const_cstr!("__mcount")
} else if cfg!(any(
target_arch = "mips", target_arch = "mips64",
target_arch = "powerpc", target_arch = "powerpc64")) {
const_cstr!("_mcount")
} else if cfg!(target_os = "darwin") {
const_cstr!("\01mcount")
} else if cfg!(target_arch = "aarch64")
&& (cfg!(target_os = "linux")
|| (cfg!(target_os = "unknown") && cfg!(target_env = "gnu")))
{
const_cstr!("\01_mcount")
} else if cfg!(target_arch = "arm")
&& cfg!(any(target_os = "linux", target_os = "unknown"))
{
if cfg!(target_env = "gnu") {
const_cstr!("\01__gnu_mcount_nc")
} else {
const_cstr!("\01mcount")
}
} else {
const_cstr!("mcount")
};

llvm::AddFunctionAttrStringValue(
llfn, llvm::AttributePlace::Function,
const_cstr!("instrument-function-entry-inlined"), const_cstr!("mcount"));
const_cstr!("instrument-function-entry-inlined"), mcount_name);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/codegen/instrument-mcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ignore-tidy-linelength
// compile-flags: -Z instrument-mcount

#![crate_type = "lib"]

// CHECK: attributes #{{.*}} "instrument-function-entry-inlined"="{{_*}}mcount" "no-frame-pointer-elim"="true"
nagisa marked this conversation as resolved.
Show resolved Hide resolved
pub fn foo() {}