Skip to content

Commit 0c07d7d

Browse files
authored
Unrolled build for #150494
Rollup merge of #150494 - extern_linkage_dso_local, r=bjorn3 Fix dso_local for external statics with linkage Tracking issue of the feature: #127488 DSO local attributes are not correctly applied to extern statics with `#[linkage = "foo"]` as we generate an internal global for such statics, and the we evaluate (and apply) DSO attributes on the internal one instead. Fix this by applying DSO local attributes on the actually extern ones, too.
2 parents ad04f76 + 5467a39 commit 0c07d7d

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ fn check_and_apply_linkage<'ll, 'tcx>(
187187
};
188188
llvm::set_linkage(g1, base::linkage_to_llvm(linkage));
189189

190+
// Normally this is done in `get_static_inner`, but when as we generate an internal global,
191+
// it will apply the dso_local to the internal global instead, so do it here, too.
192+
cx.assume_dso_local(g1, true);
193+
190194
// Declare an internal global `extern_with_linkage_foo` which
191195
// is initialized with the address of `foo`. If `foo` is
192196
// discarded during linking (for example, if `foo` has weak
Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1-
//@ only-loongarch64-unknown-linux-gnu
1+
//@ ignore-powerpc64 (handles dso_local differently)
2+
//@ ignore-apple (handles dso_local differently)
23

3-
//@ revisions: DEFAULT DIRECT INDIRECT
4+
//@ revisions: DEFAULT PIE DIRECT INDIRECT
45
//@ [DEFAULT] compile-flags: -C relocation-model=static
5-
//@ [DIRECT] compile-flags: -C relocation-model=static -Z direct-access-external-data=yes
6+
//@ [PIE] compile-flags: -C relocation-model=pie
7+
//@ [DIRECT] compile-flags: -C relocation-model=pie -Z direct-access-external-data=yes
68
//@ [INDIRECT] compile-flags: -C relocation-model=static -Z direct-access-external-data=no
79

810
#![crate_type = "rlib"]
11+
#![feature(linkage)]
912

10-
// DEFAULT: @VAR = external {{.*}} global i32
11-
// DIRECT: @VAR = external dso_local {{.*}} global i32
12-
// INDIRECT: @VAR = external {{.*}} global i32
13+
unsafe extern "C" {
14+
// CHECK: @VAR = external
15+
// DEFAULT-SAME: dso_local
16+
// PIE-NOT: dso_local
17+
// DIRECT-SAME: dso_local
18+
// INDIRECT-NOT: dso_local
19+
// CHECK-SAME: global i32
20+
safe static VAR: i32;
1321

14-
extern "C" {
15-
static VAR: i32;
22+
// When "linkage" is used, we generate an indirection global.
23+
// Check dso_local is still applied to the actual global.
24+
// CHECK: @EXTERNAL = external
25+
// DEFAULT-SAME: dso_local
26+
// PIE-NOT: dso_local
27+
// DIRECT-SAME: dso_local
28+
// INDIRECT-NOT: dso_local
29+
// CHECK-SAME: global i8
30+
#[linkage = "external"]
31+
safe static EXTERNAL: *const u32;
32+
33+
// CHECK: @WEAK = extern_weak
34+
// DEFAULT-SAME: dso_local
35+
// PIE-NOT: dso_local
36+
// DIRECT-SAME: dso_local
37+
// INDIRECT-NOT: dso_local
38+
// CHECK-SAME: global i8
39+
#[linkage = "extern_weak"]
40+
safe static WEAK: *const u32;
1641
}
1742

1843
#[no_mangle]
19-
pub fn get() -> i32 {
20-
unsafe { VAR }
44+
pub fn refer() {
45+
core::hint::black_box(VAR);
46+
core::hint::black_box(EXTERNAL);
47+
core::hint::black_box(WEAK);
2148
}

0 commit comments

Comments
 (0)