@@ -3,7 +3,7 @@ mod raw_dylib;
3
3
use std:: collections:: BTreeSet ;
4
4
use std:: ffi:: OsString ;
5
5
use std:: fs:: { File , OpenOptions , read} ;
6
- use std:: io:: { BufWriter , Write } ;
6
+ use std:: io:: { BufReader , BufWriter , Write } ;
7
7
use std:: ops:: { ControlFlow , Deref } ;
8
8
use std:: path:: { Path , PathBuf } ;
9
9
use std:: process:: { ExitStatus , Output , Stdio } ;
@@ -184,6 +184,12 @@ pub fn link_binary(
184
184
) ;
185
185
}
186
186
187
+ if sess. target . binary_format == BinaryFormat :: Elf {
188
+ if let Err ( err) = warn_if_linked_with_gold ( sess, & out_filename) {
189
+ info ! ( ?err, "Error while checking if gold was the linker" ) ;
190
+ }
191
+ }
192
+
187
193
if output. is_stdout ( ) {
188
194
if output. is_tty ( ) {
189
195
sess. dcx ( ) . emit_err ( errors:: BinaryOutputToTty {
@@ -3425,3 +3431,54 @@ fn add_lld_args(
3425
3431
}
3426
3432
}
3427
3433
}
3434
+
3435
+ // gold has been deprecated with binutils 2.44
3436
+ // and is known to behave incorrectly around Rust programs.
3437
+ // There have been reports of being unable to bootstrap with gold:
3438
+ // https://github.com/rust-lang/rust/issues/139425
3439
+ // Additionally, gold miscompiles SHF_GNU_RETAIN sections, which are
3440
+ // emitted with `#[used(linker)]`.
3441
+ fn warn_if_linked_with_gold ( sess : & Session , path : & Path ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
3442
+ use object:: read:: elf:: { FileHeader , SectionHeader } ;
3443
+ use object:: read:: { ReadCache , ReadRef , Result } ;
3444
+ use object:: { Endianness , elf} ;
3445
+
3446
+ fn elf_has_gold_version_note < ' a > (
3447
+ elf : & impl FileHeader ,
3448
+ data : impl ReadRef < ' a > ,
3449
+ ) -> Result < bool > {
3450
+ let endian = elf. endian ( ) ?;
3451
+
3452
+ let section =
3453
+ elf. sections ( endian, data) ?. section_by_name ( endian, b".note.gnu.gold-version" ) ;
3454
+ if let Some ( ( _, section) ) = section {
3455
+ if let Some ( mut notes) = section. notes ( endian, data) ? {
3456
+ return Ok ( notes. any ( |note| {
3457
+ note. is_ok_and ( |note| note. n_type ( endian) == elf:: NT_GNU_GOLD_VERSION )
3458
+ } ) ) ;
3459
+ }
3460
+ }
3461
+
3462
+ Ok ( false )
3463
+ }
3464
+
3465
+ let data = ReadCache :: new ( BufReader :: new ( File :: open ( path) ?) ) ;
3466
+
3467
+ let was_linked_with_gold = if sess. target . pointer_width == 64 {
3468
+ let elf = elf:: FileHeader64 :: < Endianness > :: parse ( & data) ?;
3469
+ elf_has_gold_version_note ( elf, & data) ?
3470
+ } else if sess. target . pointer_width == 32 {
3471
+ let elf = elf:: FileHeader32 :: < Endianness > :: parse ( & data) ?;
3472
+ elf_has_gold_version_note ( elf, & data) ?
3473
+ } else {
3474
+ return Ok ( ( ) ) ;
3475
+ } ;
3476
+
3477
+ if was_linked_with_gold {
3478
+ let mut warn =
3479
+ sess. dcx ( ) . struct_warn ( "the gold linker is deprecated and has known bugs with Rust" ) ;
3480
+ warn. help ( "consider using LLD or ld from GNU binutils instead" ) ;
3481
+ warn. emit ( ) ;
3482
+ }
3483
+ Ok ( ( ) )
3484
+ }
0 commit comments