Skip to content

Pass -pg to linker when using -Zinstrument-mcount#152457

Open
pmur wants to merge 1 commit intorust-lang:mainfrom
pmur:murp/mcount-link-pg
Open

Pass -pg to linker when using -Zinstrument-mcount#152457
pmur wants to merge 1 commit intorust-lang:mainfrom
pmur:murp/mcount-link-pg

Conversation

@pmur
Copy link
Contributor

@pmur pmur commented Feb 10, 2026

This selects a slightly different crt on gnu targets which enables the profiler within glibc.

This makes using gprof a little easier with Rust binaries. Otherwise, rustc must be passed -Clink-args=-pg to ensure the correct startup code is linked.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 10, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 10, 2026

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: codegen, compiler
  • codegen, compiler expanded to 66 candidates
  • Random selection from 13 candidates

@mati865
Copy link
Member

mati865 commented Feb 11, 2026

The change itself is correct, but perhaps it deserves a test?

g(r)crt1.o enabled by -pg is a startup code for the c runtime. Is it right to call it the crt?

@bjorn3
Copy link
Member

bjorn3 commented Feb 11, 2026

Is this also correct for musl and other targets that use a gnu toolchain?

@mati865
Copy link
Member

mati865 commented Feb 11, 2026

Good question.
This flag is supported by GCC and Clang, so any target that uses them should accept it. If GCC/Clang does a wrong thing for a specific target, this is arguably not our fault, yet we still might want to add workarounds.

@pmur
Copy link
Contributor Author

pmur commented Feb 11, 2026

g(r)crt1.o enabled by -pg is a startup code for the c runtime. Is it right to call it the crt?

That's maybe a detail about the glibc specific implementation which can be redacted? non-gnu targets seem to have a little more varied behavior.

Is this also correct for musl and other targets that use a gnu toolchain?

musl, no. Linking should fail with unresolved mcount-ish symbols. Looking around at Clang, it does seem a few other platforms should respect this option. @mati865, I think you're right. If the target lacks support for this option, it will likely fail similar to musl. Removing the gnu check is likely helpful for other targets.

Clang and gcc use this option to control linking behavior too. Some
targets need to be linked against a special crt which enables
profiling at runtime.

This makes using gprof a little easier with Rust binaries. Otherwise,
rustc must be passed `-Clink-args=-pg` to ensure the correct startup
code is linked.
@pmur pmur force-pushed the murp/mcount-link-pg branch from 78a8cde to a4a5208 Compare February 11, 2026 17:46
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Feb 11, 2026
@mati865
Copy link
Member

mati865 commented Feb 12, 2026

Just to double check we don't regress here.
Musl (and likely other libc like uclibc) failed to before and still do after this PR, albeit with different errors, right?

FreeBSD seems to support it: https://man.freebsd.org/cgi/man.cgi?query=moncontrol&sektion=3&apropos=0&manpath=freebsd

Even mingw-w64 provides the necessary objects.

@pmur
Copy link
Contributor Author

pmur commented Feb 12, 2026

Just to double check we don't regress here.
Musl (and likely other libc like uclibc) failed to before and still do after this PR, albeit with different errors, right?

I verified musl will fail to compile in the same way with and without -pg. It fails to link due to the missing mcount symbol. I think uclibc will fail similarly, it appears to lack an mcount implementation too.

@pmur
Copy link
Contributor Author

pmur commented Feb 25, 2026

ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants