Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2767,6 +2767,10 @@ fn add_order_independent_options(
cmd.pgo_gen();
}

if sess.opts.unstable_opts.instrument_mcount {
cmd.enable_profiling();
}

if sess.opts.cg.control_flow_guard != CFGuard::Disabled {
cmd.control_flow_guard();
}
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ pub(crate) trait Linker {
fn add_no_exec(&mut self) {}
fn add_as_needed(&mut self) {}
fn reset_per_library_state(&mut self) {}
fn enable_profiling(&mut self) {}
}

impl dyn Linker + '_ {
Expand Down Expand Up @@ -732,6 +733,19 @@ impl<'a> Linker for GccLinker<'a> {
self.link_or_cc_args(&["-u", "__llvm_profile_runtime"]);
}

fn enable_profiling(&mut self) {
// This flag is also used when linking to choose target specific
// libraries needed to enable profiling.
self.cc_arg("-pg");
// On windows-gnu targets, libgmon also needs to be linked, and this
// requires readding libraries to satisfy its dependencies.
if self.sess.target.is_like_windows {
self.cc_arg("-lgmon");
self.cc_arg("-lkernel32");
self.cc_arg("-lmsvcrt");
}
}

fn control_flow_guard(&mut self) {}

fn ehcont_guard(&mut self) {}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/base/windows_gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub(crate) fn opts() -> TargetOptions {
// FIXME(davidtwco): Support Split DWARF on Windows GNU - may require LLVM changes to
// output DWO, despite using DWARF, doesn't use ELF..
supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]),
mcount: "_mcount".into(),
..Default::default()
}
}
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/base/windows_gnullvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub(crate) fn opts() -> TargetOptions {
// FIXME(davidtwco): Support Split DWARF on Windows GNU - may require LLVM changes to
// output DWO, despite using DWARF, doesn't use ELF..
supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]),
mcount: "_mcount".into(),
..Default::default()
}
}
3 changes: 3 additions & 0 deletions tests/run-make/instrument-mcount-link-pg/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello World!");
}
19 changes: 19 additions & 0 deletions tests/run-make/instrument-mcount-link-pg/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// When building a binary instrumented with mcount, verify the
// binary is linked with the correct crt to enable profiling.
//
//@ only-gnu
//@ ignore-cross-compile

use run_make_support::{path, run, rustc};

fn main() {
// Compile instrumentation enabled binary, and verify -pg is passed
let link_args =
rustc().input("main.rs").arg("-Zinstrument-mcount").print("link-args").run().stdout_utf8();
assert!(link_args.contains("\"-pg\""));

// Run it, and verify gmon.out is created
assert!(!path("gmon.out").exists());
run("main");
assert!(path("gmon.out").exists());
}
Loading