Skip to content

Unused symbol breaks Profiled Guided Optimization (PGO) #57258

Open
@robsmith11

Description

@robsmith11

My crate is structured to build both a bin and a dylib that is loaded by a 3rd-party program. The stand-alone bin part doesn't actually use any of the external symbols, so despite it sharing code with the lib part, it runs fine without the symbols being defined.

However, when I turn on PGO, these undefined symbols cause linking errors. My expectation is that if my crate works without PGO, it should work with PGO enabled, but maybe that's incorrect?

My actual crate gets these undefined symbols from here:
https://github.com/robsmith11/krust/blob/master/src/kbindings.rs#L483

But this minimal example reproduces the problem:

fn main() {
}

extern "C" {
    pub fn f();
}

#[no_mangle]
pub extern fn g() {
    unsafe { f(); }
}
$ RUSTFLAGS="-Z pgo-gen=/tmp/prof.data" cargo run --release 
    Compiling play v0.1.0 (/home/me/e/code/play)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/me/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/tmp/target_play/release/deps/play-e890b10933e5432f.play.1uu11z5v-cgu.0.rcgu.o" "-o" "/tmp/target_play/release/deps/play-e890b10933e5432f" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/tmp/target_play/release/deps" "-L" "/home/me/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/tmp/rustcvLfVyu/libprofiler_builtins-4ecfbf392fb1b2b9.rlib" "-Wl,--start-group" "/tmp/rustcvLfVyu/libstd-4f6a41fe25da1d4f.rlib" "-Wl,--end-group" "/home/me/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-cd088bc8787919ee.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil" "-u" "__llvm_profile_runtime"
  = note: /usr/bin/ld: /tmp/target_play/release/deps/play-e890b10933e5432f.play.1uu11z5v-cgu.0.rcgu.o: in function `g':
          play.1uu11z5v-cgu.0:(.text.g+0xa): undefined reference to `f'
          collect2: error: ld returned 1 exit status

EDIT:
Not sure whether it's relevant, but removing the #[no_mangle] makes it work with PGO.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions