Skip to content

Commit d69cd46

Browse files
authored
Rollup merge of #85772 - luqmana:ignored-metadata, r=petrochenkov
Preserve metadata w/ Solaris-like linkers. #84468 moved the `-zignore` linker flag from the `gc_sections` method to `add_as_needed` which is more accurate but Solaris-style linkers will also end up removing an unreferenced ELF sections [1]. This had the unfortunate side effect of causing the `.rustc` section (which has the metada) to be removed which could cause issues when trying to link against the resulting crates or use proc macros. Since the `-zignore` is positional, we fix this by moving the metadata objects to before the flag. [1] Specifically a section is considered unreferenced if: * The section is allocatable * No other sections bind to (relocate) to this section * The section provides no global symbols https://docs.oracle.com/cd/E19683-01/817-3677/6mj8mbtbs/index.html#chapter4-19
2 parents b3bcf4a + cffef33 commit d69cd46

File tree

1 file changed

+6
-3
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+6
-3
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1836,10 +1836,16 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
18361836
// Make the binary compatible with data execution prevention schemes.
18371837
cmd.add_no_exec();
18381838

1839+
// OBJECT-FILES-YES
1840+
add_local_crate_metadata_objects(cmd, crate_type, codegen_results);
1841+
18391842
// NO-OPT-OUT, OBJECT-FILES-NO
18401843
// Avoid linking to dynamic libraries unless they satisfy some undefined symbols
18411844
// at the point at which they are specified on the command line.
18421845
// Must be passed before any dynamic libraries.
1846+
// On solaris-like systems, this also will ignore unreferenced ELF sections
1847+
// from relocatable objects. For that reason, we move the metadata objects
1848+
// to before this flag as they would otherwise be removed.
18431849
cmd.add_as_needed();
18441850

18451851
// NO-OPT-OUT, OBJECT-FILES-NO
@@ -1891,9 +1897,6 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
18911897
// dynamic library.
18921898
cmd.export_symbols(tmpdir, crate_type);
18931899

1894-
// OBJECT-FILES-YES
1895-
add_local_crate_metadata_objects(cmd, crate_type, codegen_results);
1896-
18971900
// OBJECT-FILES-YES
18981901
add_local_crate_allocator_objects(cmd, codegen_results);
18991902

0 commit comments

Comments
 (0)