Skip to content

Commit b75f384

Browse files
authored
Rollup merge of #93901 - petrochenkov:linkmod, r=wesleywiser
Stabilize native library modifier syntax and the `whole-archive` modifier specifically Stabilization report: #93901 (comment) cc #81490
2 parents c5cf08d + 1004783 commit b75f384

33 files changed

+270
-165
lines changed

Diff for: compiler/rustc_ast_passes/src/feature_gate.rs

-8
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
387387
if attr.has_name(sym::link) {
388388
for nested_meta in attr.meta_item_list().unwrap_or_default() {
389389
if nested_meta.has_name(sym::modifiers) {
390-
gate_feature_post!(
391-
self,
392-
native_link_modifiers,
393-
nested_meta.span(),
394-
"native link modifiers are experimental"
395-
);
396-
397390
if let Some(modifiers) = nested_meta.value_str() {
398391
for modifier in modifiers.as_str().split(',') {
399392
if let Some(modifier) = modifier.strip_prefix(&['+', '-']) {
@@ -412,7 +405,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
412405
gate_modifier!(
413406
"bundle" => native_link_modifiers_bundle
414407
"verbatim" => native_link_modifiers_verbatim
415-
"whole-archive" => native_link_modifiers_whole_archive
416408
"as-needed" => native_link_modifiers_as_needed
417409
);
418410
}

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+62-41
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
18441844
// This change is somewhat breaking in practice due to local static libraries being linked
18451845
// as whole-archive (#85144), so removing whole-archive may be a pre-requisite.
18461846
if sess.opts.debugging_opts.link_native_libraries {
1847-
add_local_native_libraries(cmd, sess, codegen_results);
1847+
add_local_native_libraries(cmd, sess, codegen_results, crate_type);
18481848
}
18491849

18501850
// Upstream rust libraries and their nobundle static libraries
@@ -2016,6 +2016,16 @@ fn add_order_independent_options(
20162016
add_rpath_args(cmd, sess, codegen_results, out_filename);
20172017
}
20182018

2019+
// A dylib may reexport symbols from the linked rlib or native static library.
2020+
// Even if some symbol is reexported it's still not necessarily counted as used and may be
2021+
// dropped, at least with `ld`-like ELF linkers. So we have to link some rlibs and static
2022+
// libraries as whole-archive to avoid losing reexported symbols.
2023+
// FIXME: Find a way to mark reexported symbols as used and avoid this use of whole-archive.
2024+
fn default_to_whole_archive(sess: &Session, crate_type: CrateType, cmd: &dyn Linker) -> bool {
2025+
crate_type == CrateType::Dylib
2026+
&& !(sess.target.limit_rdylib_exports && cmd.exported_symbol_means_used_symbol())
2027+
}
2028+
20192029
/// # Native library linking
20202030
///
20212031
/// User-supplied library search paths (-L on the command line). These are the same paths used to
@@ -2029,6 +2039,7 @@ fn add_local_native_libraries(
20292039
cmd: &mut dyn Linker,
20302040
sess: &Session,
20312041
codegen_results: &CodegenResults,
2042+
crate_type: CrateType,
20322043
) {
20332044
let filesearch = sess.target_filesearch(PathKind::All);
20342045
for search_path in filesearch.search_paths() {
@@ -2046,14 +2057,18 @@ fn add_local_native_libraries(
20462057
codegen_results.crate_info.used_libraries.iter().filter(|l| relevant_lib(sess, l));
20472058

20482059
let search_path = OnceCell::new();
2049-
let mut last = (NativeLibKind::Unspecified, None);
2060+
let mut last = (None, NativeLibKind::Unspecified, None);
20502061
for lib in relevant_libs {
20512062
let Some(name) = lib.name else {
20522063
continue;
20532064
};
20542065

20552066
// Skip if this library is the same as the last.
2056-
last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };
2067+
last = if (lib.name, lib.kind, lib.verbatim) == last {
2068+
continue;
2069+
} else {
2070+
(lib.name, lib.kind, lib.verbatim)
2071+
};
20572072

20582073
let verbatim = lib.verbatim.unwrap_or(false);
20592074
match lib.kind {
@@ -2064,15 +2079,19 @@ fn add_local_native_libraries(
20642079
NativeLibKind::Framework { as_needed } => {
20652080
cmd.link_framework(name, as_needed.unwrap_or(true))
20662081
}
2067-
NativeLibKind::Static { bundle: None | Some(true), .. }
2068-
| NativeLibKind::Static { whole_archive: Some(true), .. } => {
2069-
cmd.link_whole_staticlib(
2070-
name,
2071-
verbatim,
2072-
&search_path.get_or_init(|| archive_search_paths(sess)),
2073-
);
2082+
NativeLibKind::Static { whole_archive, .. } => {
2083+
if whole_archive == Some(true)
2084+
|| (whole_archive == None && default_to_whole_archive(sess, crate_type, cmd))
2085+
{
2086+
cmd.link_whole_staticlib(
2087+
name,
2088+
verbatim,
2089+
&search_path.get_or_init(|| archive_search_paths(sess)),
2090+
);
2091+
} else {
2092+
cmd.link_staticlib(name, verbatim)
2093+
}
20742094
}
2075-
NativeLibKind::Static { .. } => cmd.link_staticlib(name, verbatim),
20762095
NativeLibKind::RawDylib => {
20772096
// FIXME(#58713): Proper handling for raw dylibs.
20782097
bug!("raw_dylib feature not yet implemented");
@@ -2197,34 +2216,37 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21972216
// external build system already has the native dependencies defined, and it
21982217
// will provide them to the linker itself.
21992218
if sess.opts.debugging_opts.link_native_libraries {
2200-
let mut last = None;
2219+
let mut last = (None, NativeLibKind::Unspecified, None);
22012220
for lib in &codegen_results.crate_info.native_libraries[&cnum] {
2221+
let Some(name) = lib.name else {
2222+
continue;
2223+
};
22022224
if !relevant_lib(sess, lib) {
2203-
// Skip libraries if they are disabled by `#[link(cfg=...)]`
22042225
continue;
22052226
}
22062227

22072228
// Skip if this library is the same as the last.
2208-
if last == lib.name {
2229+
last = if (lib.name, lib.kind, lib.verbatim) == last {
22092230
continue;
2210-
}
2211-
2212-
if let Some(static_lib_name) = lib.name {
2213-
if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
2214-
lib.kind
2231+
} else {
2232+
(lib.name, lib.kind, lib.verbatim)
2233+
};
2234+
2235+
if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
2236+
lib.kind
2237+
{
2238+
let verbatim = lib.verbatim.unwrap_or(false);
2239+
if whole_archive == Some(true)
2240+
|| (whole_archive == None
2241+
&& default_to_whole_archive(sess, crate_type, cmd))
22152242
{
2216-
let verbatim = lib.verbatim.unwrap_or(false);
2217-
if whole_archive == Some(true) {
2218-
cmd.link_whole_staticlib(
2219-
static_lib_name,
2220-
verbatim,
2221-
search_path.get_or_init(|| archive_search_paths(sess)),
2222-
);
2223-
} else {
2224-
cmd.link_staticlib(static_lib_name, verbatim);
2225-
}
2226-
2227-
last = lib.name;
2243+
cmd.link_whole_staticlib(
2244+
name,
2245+
verbatim,
2246+
search_path.get_or_init(|| archive_search_paths(sess)),
2247+
);
2248+
} else {
2249+
cmd.link_staticlib(name, verbatim);
22282250
}
22292251
}
22302252
}
@@ -2282,15 +2304,10 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
22822304
let cratepath = &src.rlib.as_ref().unwrap().0;
22832305

22842306
let mut link_upstream = |path: &Path| {
2285-
// If we're creating a dylib, then we need to include the
2286-
// whole of each object in our archive into that artifact. This is
2287-
// because a `dylib` can be reused as an intermediate artifact.
2288-
//
2289-
// Note, though, that we don't want to include the whole of a
2290-
// compiler-builtins crate (e.g., compiler-rt) because it'll get
2291-
// repeatedly linked anyway.
2307+
// We don't want to include the whole compiler-builtins crate (e.g., compiler-rt)
2308+
// regardless of the default because it'll get repeatedly linked anyway.
22922309
let path = fix_windows_verbatim_for_gcc(path);
2293-
if crate_type == CrateType::Dylib
2310+
if default_to_whole_archive(sess, crate_type, cmd)
22942311
&& codegen_results.crate_info.compiler_builtins != Some(cnum)
22952312
{
22962313
cmd.link_whole_rlib(&path);
@@ -2401,7 +2418,7 @@ fn add_upstream_native_libraries(
24012418
sess: &Session,
24022419
codegen_results: &CodegenResults,
24032420
) {
2404-
let mut last = (NativeLibKind::Unspecified, None);
2421+
let mut last = (None, NativeLibKind::Unspecified, None);
24052422
for &cnum in &codegen_results.crate_info.used_crates {
24062423
for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
24072424
let Some(name) = lib.name else {
@@ -2412,7 +2429,11 @@ fn add_upstream_native_libraries(
24122429
}
24132430

24142431
// Skip if this library is the same as the last.
2415-
last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };
2432+
last = if (lib.name, lib.kind, lib.verbatim) == last {
2433+
continue;
2434+
} else {
2435+
(lib.name, lib.kind, lib.verbatim)
2436+
};
24162437

24172438
let verbatim = lib.verbatim.unwrap_or(false);
24182439
match lib.kind {

Diff for: compiler/rustc_codegen_ssa/src/back/linker.rs

+11
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ pub trait Linker {
186186
fn no_crt_objects(&mut self);
187187
fn no_default_libraries(&mut self);
188188
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]);
189+
fn exported_symbol_means_used_symbol(&self) -> bool {
190+
true
191+
}
189192
fn subsystem(&mut self, subsystem: &str);
190193
fn group_start(&mut self);
191194
fn group_end(&mut self);
@@ -724,6 +727,10 @@ impl<'a> Linker for GccLinker<'a> {
724727
}
725728
}
726729

730+
fn exported_symbol_means_used_symbol(&self) -> bool {
731+
self.sess.target.is_like_windows || self.sess.target.is_like_osx
732+
}
733+
727734
fn subsystem(&mut self, subsystem: &str) {
728735
self.linker_arg("--subsystem");
729736
self.linker_arg(&subsystem);
@@ -1471,6 +1478,10 @@ impl<'a> Linker for L4Bender<'a> {
14711478
return;
14721479
}
14731480

1481+
fn exported_symbol_means_used_symbol(&self) -> bool {
1482+
false
1483+
}
1484+
14741485
fn subsystem(&mut self, subsystem: &str) {
14751486
self.cmd.arg(&format!("--subsystem {}", subsystem));
14761487
}

Diff for: compiler/rustc_feature/src/accepted.rs

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ declare_features! (
215215
/// Allows patterns with concurrent by-move and by-ref bindings.
216216
/// For example, you can write `Foo(a, ref b)` where `a` is by-move and `b` is by-ref.
217217
(accepted, move_ref_pattern, "1.49.0", Some(68354), None),
218+
/// Allows specifying modifiers in the link attribute: `#[link(modifiers = "...")]`
219+
(accepted, native_link_modifiers, "1.61.0", Some(81490), None),
220+
/// Allows specifying the whole-archive link modifier
221+
(accepted, native_link_modifiers_whole_archive, "1.61.0", Some(81490), None),
218222
/// Allows using `#![no_std]`.
219223
(accepted, no_std, "1.6.0", None, None),
220224
/// Allows defining identifiers beyond ASCII.

Diff for: compiler/rustc_feature/src/active.rs

-4
Original file line numberDiff line numberDiff line change
@@ -446,16 +446,12 @@ declare_features! (
446446
(active, must_not_suspend, "1.57.0", Some(83310), None),
447447
/// Allows using `#[naked]` on functions.
448448
(active, naked_functions, "1.9.0", Some(32408), None),
449-
/// Allows specifying modifiers in the link attribute: `#[link(modifiers = "...")]`
450-
(active, native_link_modifiers, "1.53.0", Some(81490), None),
451449
/// Allows specifying the as-needed link modifier
452450
(active, native_link_modifiers_as_needed, "1.53.0", Some(81490), None),
453451
/// Allows specifying the bundle link modifier
454452
(active, native_link_modifiers_bundle, "1.53.0", Some(81490), None),
455453
/// Allows specifying the verbatim link modifier
456454
(active, native_link_modifiers_verbatim, "1.53.0", Some(81490), None),
457-
/// Allows specifying the whole-archive link modifier
458-
(active, native_link_modifiers_whole_archive, "1.53.0", Some(81490), None),
459455
/// Allow negative trait implementations.
460456
(active, negative_impls, "1.44.0", Some(68318), None),
461457
/// Allows the `!` type. Does not imply 'exhaustive_patterns' (below) any more.

Diff for: compiler/rustc_llvm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(nll)]
2-
#![feature(native_link_modifiers)]
2+
#![cfg_attr(bootstrap, feature(native_link_modifiers))]
33
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
44

55
// NOTE: This crate only exists to allow linking on mingw targets.

Diff for: compiler/rustc_metadata/src/native_libs.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,18 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector<'tcx> {
125125

126126
// Do this outside the above loop so we don't depend on modifiers coming
127127
// after kinds
128-
if let Some(item) = items.iter().find(|item| item.has_name(sym::modifiers)) {
128+
let mut modifiers_count = 0;
129+
for item in items.iter().filter(|item| item.has_name(sym::modifiers)) {
129130
if let Some(modifiers) = item.value_str() {
131+
modifiers_count += 1;
130132
let span = item.name_value_literal_span().unwrap();
133+
let mut has_duplicate_modifiers = false;
131134
for modifier in modifiers.as_str().split(',') {
132135
let (modifier, value) = match modifier.strip_prefix(&['+', '-']) {
133136
Some(m) => (m, modifier.starts_with('+')),
134137
None => {
138+
// Note: this error also excludes the case with empty modifier
139+
// string, like `modifiers = ""`.
135140
sess.span_err(
136141
span,
137142
"invalid linking modifier syntax, expected '+' or '-' prefix \
@@ -143,6 +148,9 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector<'tcx> {
143148

144149
match (modifier, &mut lib.kind) {
145150
("bundle", NativeLibKind::Static { bundle, .. }) => {
151+
if bundle.is_some() {
152+
has_duplicate_modifiers = true;
153+
}
146154
*bundle = Some(value);
147155
}
148156
("bundle", _) => {
@@ -153,9 +161,17 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector<'tcx> {
153161
);
154162
}
155163

156-
("verbatim", _) => lib.verbatim = Some(value),
164+
("verbatim", _) => {
165+
if lib.verbatim.is_some() {
166+
has_duplicate_modifiers = true;
167+
}
168+
lib.verbatim = Some(value);
169+
}
157170

158171
("whole-archive", NativeLibKind::Static { whole_archive, .. }) => {
172+
if whole_archive.is_some() {
173+
has_duplicate_modifiers = true;
174+
}
159175
*whole_archive = Some(value);
160176
}
161177
("whole-archive", _) => {
@@ -168,6 +184,9 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector<'tcx> {
168184

169185
("as-needed", NativeLibKind::Dylib { as_needed })
170186
| ("as-needed", NativeLibKind::Framework { as_needed }) => {
187+
if as_needed.is_some() {
188+
has_duplicate_modifiers = true;
189+
}
171190
*as_needed = Some(value);
172191
}
173192
("as-needed", _) => {
@@ -190,12 +209,22 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector<'tcx> {
190209
}
191210
}
192211
}
212+
if has_duplicate_modifiers {
213+
let msg =
214+
"same modifier is used multiple times in a single `modifiers` argument";
215+
sess.span_err(item.span(), msg);
216+
}
193217
} else {
194218
let msg = "must be of the form `#[link(modifiers = \"...\")]`";
195219
sess.span_err(item.span(), msg);
196220
}
197221
}
198222

223+
if modifiers_count > 1 {
224+
let msg = "multiple `modifiers` arguments in a single `#[link]` attribute";
225+
sess.span_err(m.span, msg);
226+
}
227+
199228
// In general we require #[link(name = "...")] but we allow
200229
// #[link(wasm_import_module = "...")] without the `name`.
201230
let requires_name = kind_specified || lib.wasm_import_module.is_none();
@@ -349,6 +378,15 @@ impl Collector<'_> {
349378
.drain_filter(|lib| {
350379
if let Some(lib_name) = lib.name {
351380
if lib_name.as_str() == passed_lib.name {
381+
// FIXME: This whole logic is questionable, whether modifiers are
382+
// involved or not, library reordering and kind overriding without
383+
// explicit `:rename` in particular.
384+
if lib.has_modifiers() || passed_lib.has_modifiers() {
385+
self.tcx.sess.span_err(
386+
self.tcx.def_span(lib.foreign_module.unwrap()),
387+
"overriding linking modifiers from command line is not supported"
388+
);
389+
}
352390
if passed_lib.kind != NativeLibKind::Unspecified {
353391
lib.kind = passed_lib.kind;
354392
}

0 commit comments

Comments
 (0)