Skip to content

Commit 578138c

Browse files
authored
Unrolled build for #146027
Rollup merge of #146027 - usamoi:raw-dylib-elf-as-needed, r=bjorn3 support link modifier `as-needed` for raw-dylib-elf This pull request: * emits `-Wl,--as-needed` instead of `-Wl,--no-as-needed` for `raw-dylib-elf`, keeping it consistent with `dylib` * allows combination of link kind `raw-dylib` and link modifier `as-needed`, thus allowing free choice of behavior r? `@bjorn3` cc #135694 cc #99424
2 parents d2acb42 + 21dd997 commit 578138c

File tree

8 files changed

+82
-31
lines changed

8 files changed

+82
-31
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
attr_parsing_as_needed_compatibility =
2-
linking modifier `as-needed` is only compatible with `dylib` and `framework` linking kinds
2+
linking modifier `as-needed` is only compatible with `dylib`, `framework` and `raw-dylib` linking kinds
33
44
attr_parsing_bundle_needs_static =
55
linking modifier `bundle` is only compatible with `static` linking kind

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ impl<S: Stage> CombineAttributeParser<S> for LinkParser {
180180
}
181181

182182
(sym::as_dash_needed, Some(NativeLibKind::Dylib { as_needed }))
183-
| (sym::as_dash_needed, Some(NativeLibKind::Framework { as_needed })) => {
183+
| (sym::as_dash_needed, Some(NativeLibKind::Framework { as_needed }))
184+
| (sym::as_dash_needed, Some(NativeLibKind::RawDylib { as_needed })) => {
184185
report_unstable_modifier!(native_link_modifiers_as_needed);
185186
assign_modifier(as_needed)
186187
}
@@ -219,12 +220,12 @@ impl<S: Stage> CombineAttributeParser<S> for LinkParser {
219220

220221
// Do this outside of the loop so that `import_name_type` can be specified before `kind`.
221222
if let Some((_, span)) = import_name_type {
222-
if kind != Some(NativeLibKind::RawDylib) {
223+
if !matches!(kind, Some(NativeLibKind::RawDylib { .. })) {
223224
cx.emit_err(ImportNameTypeRaw { span });
224225
}
225226
}
226227

227-
if let Some(NativeLibKind::RawDylib) = kind
228+
if let Some(NativeLibKind::RawDylib { .. }) = kind
228229
&& name.as_str().contains('\0')
229230
{
230231
cx.emit_err(RawDylibNoNul { span: name_span });
@@ -315,7 +316,7 @@ impl LinkParser {
315316
cx.emit_err(RawDylibOnlyWindows { span: nv.value_span });
316317
}
317318

318-
NativeLibKind::RawDylib
319+
NativeLibKind::RawDylib { as_needed: None }
319320
}
320321
sym::link_dash_arg => {
321322
if !features.link_arg_attribute() {

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ fn print_native_static_libs(
14901490
NativeLibKind::Static { bundle: None | Some(true), .. }
14911491
| NativeLibKind::LinkArg
14921492
| NativeLibKind::WasmImportModule
1493-
| NativeLibKind::RawDylib => None,
1493+
| NativeLibKind::RawDylib { .. } => None,
14941494
}
14951495
})
14961496
// deduplication of consecutive repeated libraries, see rust-lang/rust#113209
@@ -2364,13 +2364,13 @@ fn linker_with_args(
23642364
cmd.add_object(&output_path);
23652365
}
23662366
} else {
2367-
for link_path in raw_dylib::create_raw_dylib_elf_stub_shared_objects(
2367+
for (link_path, as_needed) in raw_dylib::create_raw_dylib_elf_stub_shared_objects(
23682368
sess,
23692369
codegen_results.crate_info.used_libraries.iter(),
23702370
&raw_dylib_dir,
23712371
) {
23722372
// Always use verbatim linkage, see comments in create_raw_dylib_elf_stub_shared_objects.
2373-
cmd.link_dylib_by_name(&link_path, true, false);
2373+
cmd.link_dylib_by_name(&link_path, true, as_needed);
23742374
}
23752375
}
23762376
// As with add_upstream_native_libraries, we need to add the upstream raw-dylib symbols in case
@@ -2411,13 +2411,13 @@ fn linker_with_args(
24112411
cmd.add_object(&output_path);
24122412
}
24132413
} else {
2414-
for link_path in raw_dylib::create_raw_dylib_elf_stub_shared_objects(
2414+
for (link_path, as_needed) in raw_dylib::create_raw_dylib_elf_stub_shared_objects(
24152415
sess,
24162416
native_libraries_from_nonstatics,
24172417
&raw_dylib_dir,
24182418
) {
24192419
// Always use verbatim linkage, see comments in create_raw_dylib_elf_stub_shared_objects.
2420-
cmd.link_dylib_by_name(&link_path, true, false);
2420+
cmd.link_dylib_by_name(&link_path, true, as_needed);
24212421
}
24222422
}
24232423

@@ -2726,7 +2726,7 @@ fn add_native_libs_from_crate(
27262726
cmd.link_framework_by_name(name, verbatim, as_needed.unwrap_or(true))
27272727
}
27282728
}
2729-
NativeLibKind::RawDylib => {
2729+
NativeLibKind::RawDylib { as_needed: _ } => {
27302730
// Handled separately in `linker_with_args`.
27312731
}
27322732
NativeLibKind::WasmImportModule => {}

compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn collate_raw_dylibs_windows<'a>(
3131
let mut dylib_table = FxIndexMap::<String, FxIndexMap<Symbol, &DllImport>>::default();
3232

3333
for lib in used_libraries {
34-
if lib.kind == NativeLibKind::RawDylib {
34+
if let NativeLibKind::RawDylib { .. } = lib.kind {
3535
let ext = if lib.verbatim { "" } else { ".dll" };
3636
let name = format!("{}{}", lib.name, ext);
3737
let imports = dylib_table.entry(name.clone()).or_default();
@@ -128,12 +128,12 @@ pub(super) fn create_raw_dylib_dll_import_libs<'a>(
128128
fn collate_raw_dylibs_elf<'a>(
129129
sess: &Session,
130130
used_libraries: impl IntoIterator<Item = &'a NativeLib>,
131-
) -> Vec<(String, Vec<DllImport>)> {
131+
) -> Vec<(String, Vec<DllImport>, bool)> {
132132
// Use index maps to preserve original order of imports and libraries.
133-
let mut dylib_table = FxIndexMap::<String, FxIndexMap<Symbol, &DllImport>>::default();
133+
let mut dylib_table = FxIndexMap::<String, (FxIndexMap<Symbol, &DllImport>, bool)>::default();
134134

135135
for lib in used_libraries {
136-
if lib.kind == NativeLibKind::RawDylib {
136+
if let NativeLibKind::RawDylib { as_needed } = lib.kind {
137137
let filename = if lib.verbatim {
138138
lib.name.as_str().to_owned()
139139
} else {
@@ -142,17 +142,19 @@ fn collate_raw_dylibs_elf<'a>(
142142
format!("{prefix}{}{ext}", lib.name)
143143
};
144144

145-
let imports = dylib_table.entry(filename.clone()).or_default();
145+
let (stub_imports, stub_as_needed) =
146+
dylib_table.entry(filename.clone()).or_insert((Default::default(), true));
146147
for import in &lib.dll_imports {
147-
imports.insert(import.name, import);
148+
stub_imports.insert(import.name, import);
148149
}
150+
*stub_as_needed = *stub_as_needed && as_needed.unwrap_or(true);
149151
}
150152
}
151153
sess.dcx().abort_if_errors();
152154
dylib_table
153155
.into_iter()
154-
.map(|(name, imports)| {
155-
(name, imports.into_iter().map(|(_, import)| import.clone()).collect())
156+
.map(|(name, (imports, as_needed))| {
157+
(name, imports.into_iter().map(|(_, import)| import.clone()).collect(), as_needed)
156158
})
157159
.collect()
158160
}
@@ -161,10 +163,10 @@ pub(super) fn create_raw_dylib_elf_stub_shared_objects<'a>(
161163
sess: &Session,
162164
used_libraries: impl IntoIterator<Item = &'a NativeLib>,
163165
raw_dylib_so_dir: &Path,
164-
) -> Vec<String> {
166+
) -> Vec<(String, bool)> {
165167
collate_raw_dylibs_elf(sess, used_libraries)
166168
.into_iter()
167-
.map(|(load_filename, raw_dylib_imports)| {
169+
.map(|(load_filename, raw_dylib_imports, as_needed)| {
168170
use std::hash::Hash;
169171

170172
// `load_filename` is the *target/loader* filename that will end up in NEEDED.
@@ -205,7 +207,7 @@ pub(super) fn create_raw_dylib_elf_stub_shared_objects<'a>(
205207
});
206208
};
207209

208-
temporary_lib_name
210+
(temporary_lib_name, as_needed)
209211
})
210212
.collect()
211213
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ pub enum NativeLibKind {
309309
},
310310
/// Dynamic library (e.g. `foo.dll` on Windows) without a corresponding import library.
311311
/// On Linux, it refers to a generated shared library stub.
312-
RawDylib,
312+
RawDylib {
313+
/// Whether the dynamic library will be linked only if it satisfies some undefined symbols
314+
as_needed: Option<bool>,
315+
},
313316
/// A macOS-specific kind of dynamic libraries.
314317
Framework {
315318
/// Whether the framework will be linked only if it satisfies some undefined symbols
@@ -332,11 +335,10 @@ impl NativeLibKind {
332335
NativeLibKind::Static { bundle, whole_archive } => {
333336
bundle.is_some() || whole_archive.is_some()
334337
}
335-
NativeLibKind::Dylib { as_needed } | NativeLibKind::Framework { as_needed } => {
336-
as_needed.is_some()
337-
}
338-
NativeLibKind::RawDylib
339-
| NativeLibKind::Unspecified
338+
NativeLibKind::Dylib { as_needed }
339+
| NativeLibKind::Framework { as_needed }
340+
| NativeLibKind::RawDylib { as_needed } => as_needed.is_some(),
341+
NativeLibKind::Unspecified
340342
| NativeLibKind::LinkArg
341343
| NativeLibKind::WasmImportModule => false,
342344
}
@@ -349,7 +351,9 @@ impl NativeLibKind {
349351
pub fn is_dllimport(&self) -> bool {
350352
matches!(
351353
self,
352-
NativeLibKind::Dylib { .. } | NativeLibKind::RawDylib | NativeLibKind::Unspecified
354+
NativeLibKind::Dylib { .. }
355+
| NativeLibKind::RawDylib { .. }
356+
| NativeLibKind::Unspecified
353357
)
354358
}
355359
}

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<'tcx> Collector<'tcx> {
218218
.flatten()
219219
{
220220
let dll_imports = match attr.kind {
221-
NativeLibKind::RawDylib => foreign_items
221+
NativeLibKind::RawDylib { .. } => foreign_items
222222
.iter()
223223
.map(|&child_item| {
224224
self.build_dll_import(

compiler/rustc_session/src/config/native_libs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ fn parse_and_apply_modifier(cx: &ParseNativeLibCx<'_>, modifier: &str, native_li
135135
),
136136

137137
("as-needed", NativeLibKind::Dylib { as_needed })
138-
| ("as-needed", NativeLibKind::Framework { as_needed }) => {
138+
| ("as-needed", NativeLibKind::Framework { as_needed })
139+
| ("as-needed", NativeLibKind::RawDylib { as_needed }) => {
139140
cx.on_unstable_value(
140141
"linking modifier `as-needed` is unstable",
141142
", the `-Z unstable-options` flag must also be passed to use it",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//@ only-elf
2+
//@ needs-dynamic-linking
3+
4+
//@ only-gnu
5+
//@ only-x86_64
6+
//@ revisions: as_needed no_as_needed no_modifier merge_1 merge_2 merge_3 merge_4
7+
8+
//@ [as_needed] run-pass
9+
//@ [no_as_needed] run-fail
10+
//@ [no_modifier] run-pass
11+
//@ [merge_1] run-fail
12+
//@ [merge_2] run-fail
13+
//@ [merge_3] run-fail
14+
//@ [merge_4] run-pass
15+
16+
#![allow(incomplete_features)]
17+
#![feature(raw_dylib_elf)]
18+
#![feature(native_link_modifiers_as_needed)]
19+
20+
#[cfg_attr(
21+
as_needed,
22+
link(name = "taiqannf1y28z2rw", kind = "raw-dylib", modifiers = "+as-needed")
23+
)]
24+
#[cfg_attr(
25+
no_as_needed,
26+
link(name = "taiqannf1y28z2rw", kind = "raw-dylib", modifiers = "-as-needed")
27+
)]
28+
#[cfg_attr(no_modifier, link(name = "taiqannf1y28z2rw", kind = "raw-dylib"))]
29+
unsafe extern "C" {}
30+
31+
#[cfg_attr(merge_1, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "+as-needed"))]
32+
#[cfg_attr(merge_2, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "-as-needed"))]
33+
#[cfg_attr(merge_3, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "-as-needed"))]
34+
#[cfg_attr(merge_4, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "+as-needed"))]
35+
unsafe extern "C" {}
36+
37+
#[cfg_attr(merge_1, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "-as-needed"))]
38+
#[cfg_attr(merge_2, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "+as-needed"))]
39+
#[cfg_attr(merge_3, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "-as-needed"))]
40+
#[cfg_attr(merge_4, link(name = "k9nm7qxoa79bg7e6", kind = "raw-dylib", modifiers = "+as-needed"))]
41+
unsafe extern "C" {}
42+
43+
fn main() {}

0 commit comments

Comments
 (0)