Skip to content

Commit d2f576c

Browse files
authored
Rollup merge of rust-lang#71804 - petrochenkov:static-pie, r=cuviper
linker: Support `-static-pie` and `-static -shared` This PR adds support for passing linker arguments for creating statically linked position-independent executables and "statically linked" shared libraries. Therefore it incorporates the majority of rust-lang#70740 except for the linker rerun hack and actually flipping the "`static-pie` is supported" switch for musl targets.
2 parents 642e47e + 96a466c commit d2f576c

File tree

3 files changed

+127
-134
lines changed

3 files changed

+127
-134
lines changed

src/librustc_codegen_ssa/back/link.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,10 @@ fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
11941194
};
11951195

11961196
// Adjust the output kind to target capabilities.
1197-
let pic_exe_supported = sess.target.target.options.position_independent_executables;
1198-
let static_pic_exe_supported = false; // FIXME: Add this option to target specs.
1199-
let static_dylib_supported = sess.target.target.options.crt_static_allows_dylibs;
1197+
let opts = &sess.target.target.options;
1198+
let pic_exe_supported = opts.position_independent_executables;
1199+
let static_pic_exe_supported = opts.static_position_independent_executables;
1200+
let static_dylib_supported = opts.crt_static_allows_dylibs;
12001201
match kind {
12011202
LinkOutputKind::DynamicPicExe if !pic_exe_supported => LinkOutputKind::DynamicNoPicExe,
12021203
LinkOutputKind::StaticPicExe if !static_pic_exe_supported => LinkOutputKind::StaticNoPicExe,
@@ -1580,16 +1581,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
15801581
}
15811582

15821583
// NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1583-
// FIXME: Support `StaticPicExe` correctly.
1584-
match link_output_kind {
1585-
LinkOutputKind::DynamicPicExe | LinkOutputKind::StaticPicExe => {
1586-
cmd.position_independent_executable()
1587-
}
1588-
LinkOutputKind::DynamicNoPicExe | LinkOutputKind::StaticNoPicExe => {
1589-
cmd.no_position_independent_executable()
1590-
}
1591-
_ => {}
1592-
}
1584+
cmd.set_output_kind(link_output_kind, out_filename);
15931585

15941586
// OBJECT-FILES-NO, AUDIT-ORDER
15951587
add_relro_args(cmd, sess);
@@ -1618,17 +1610,6 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
16181610
tmpdir,
16191611
);
16201612

1621-
// NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1622-
// FIXME: Merge with the previous `link_output_kind` match,
1623-
// and support `StaticPicExe` and `StaticDylib` correctly.
1624-
match link_output_kind {
1625-
LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {
1626-
cmd.build_static_executable()
1627-
}
1628-
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => cmd.build_dylib(out_filename),
1629-
_ => {}
1630-
}
1631-
16321613
// OBJECT-FILES-NO, AUDIT-ORDER
16331614
if sess.opts.cg.profile_generate.enabled() {
16341615
cmd.pgo_gen();

0 commit comments

Comments
 (0)