@@ -117,6 +117,7 @@ pub trait Linker {
117117 fn partial_relro ( & mut self ) ;
118118 fn no_relro ( & mut self ) ;
119119 fn optimize ( & mut self ) ;
120+ fn pgo_gen ( & mut self ) ;
120121 fn debuginfo ( & mut self ) ;
121122 fn no_default_libraries ( & mut self ) ;
122123 fn build_dylib ( & mut self , out_filename : & Path ) ;
@@ -280,6 +281,24 @@ impl<'a> Linker for GccLinker<'a> {
280281 }
281282 }
282283
284+ fn pgo_gen ( & mut self ) {
285+ if !self . sess . target . target . options . linker_is_gnu { return }
286+
287+ // If we're doing PGO generation stuff and on a GNU-like linker, use the
288+ // "-u" flag to properly pull in the profiler runtime bits.
289+ //
290+ // This is because LLVM otherwise won't add the needed initialization
291+ // for us on Linux (though the extra flag should be harmless if it
292+ // does).
293+ //
294+ // See https://reviews.llvm.org/D14033 and https://reviews.llvm.org/D14030.
295+ //
296+ // Though it may be worth to try to revert those changes upstream, since
297+ // the overhead of the initialization should be minor.
298+ self . cmd . arg ( "-u" ) ;
299+ self . cmd . arg ( "__llvm_profile_runtime" ) ;
300+ }
301+
283302 fn debuginfo ( & mut self ) {
284303 // Don't do anything special here for GNU-style linkers.
285304 }
@@ -509,6 +528,10 @@ impl<'a> Linker for MsvcLinker<'a> {
509528 // Needs more investigation of `/OPT` arguments
510529 }
511530
531+ fn pgo_gen ( & mut self ) {
532+ // Nothing needed here.
533+ }
534+
512535 fn debuginfo ( & mut self ) {
513536 // This will cause the Microsoft linker to generate a PDB file
514537 // from the CodeView line tables in the object files.
@@ -712,6 +735,10 @@ impl<'a> Linker for EmLinker<'a> {
712735 self . cmd . args ( & [ "--memory-init-file" , "0" ] ) ;
713736 }
714737
738+ fn pgo_gen ( & mut self ) {
739+ // noop, but maybe we need something like the gnu linker?
740+ }
741+
715742 fn debuginfo ( & mut self ) {
716743 // Preserve names or generate source maps depending on debug info
717744 self . cmd . arg ( match self . sess . opts . debuginfo {
@@ -877,6 +904,9 @@ impl Linker for WasmLd {
877904 fn optimize ( & mut self ) {
878905 }
879906
907+ fn pgo_gen ( & mut self ) {
908+ }
909+
880910 fn debuginfo ( & mut self ) {
881911 }
882912
0 commit comments