Skip to content

Commit 94b2bc2

Browse files
committed
Remove weak-intrinsics attribute
export all symbols as `weak` for non `windows`/`apple` targets. Additionally fixes an issue when creating `weak` instrinsics: Before this commit, generated code will be ```rust #[linkage = "weak"] pub extern "C" fn <name>(...) -> ... { // code... } pub mod <name> { #[linkage = "weak"] #[no_mangle] pub extern "C" fn <name>(...) -> ... { super::<name>(...) } } ``` The issue is that there is 2 `weak` linkage, the first one is not required, and this is the cause for the bug in rust-lang/rust#124042. Along with the remval of the `weak-intrinsics` feature, we fixed this issue as well.
1 parent 40e9b37 commit 94b2bc2

File tree

6 files changed

+13
-125
lines changed

6 files changed

+13
-125
lines changed

Cargo.toml

-11
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,6 @@ rustc-dep-of-std = ['compiler-builtins', 'core']
6666
# are not normally public but are required by the `testcrate`
6767
public-test-deps = []
6868

69-
# Marks all intrinsics functions with weak linkage so that they can be
70-
# replaced at link time by another implementation. This is particularly useful
71-
# for mixed Rust/C++ binaries that want to use the C++ intrinsics, otherwise
72-
# linking against the Rust stdlib will replace those from the compiler-rt
73-
# library.
74-
#
75-
# Unlike the "c" feature, the intrinsics are still provided by the Rust
76-
# implementations and each will be used unless a stronger symbol replaces
77-
# it during linking.
78-
weak-intrinsics = []
79-
8069
[[example]]
8170
name = "intrinsics"
8271
required-features = ["compiler-builtins"]

src/arm.rs

-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ macro_rules! bl {
2020
intrinsics! {
2121
// NOTE This function and the ones below are implemented using assembly because they are using a
2222
// custom calling convention which can't be implemented using a normal Rust function.
23-
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
2423
#[naked]
2524
#[cfg(not(target_env = "msvc"))]
2625
pub unsafe extern "C" fn __aeabi_uidivmod() {
@@ -36,7 +35,6 @@ intrinsics! {
3635
);
3736
}
3837

39-
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
4038
#[naked]
4139
pub unsafe extern "C" fn __aeabi_uldivmod() {
4240
core::arch::asm!(
@@ -53,7 +51,6 @@ intrinsics! {
5351
);
5452
}
5553

56-
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
5754
#[naked]
5855
pub unsafe extern "C" fn __aeabi_idivmod() {
5956
core::arch::asm!(
@@ -67,7 +64,6 @@ intrinsics! {
6764
);
6865
}
6966

70-
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
7167
#[naked]
7268
pub unsafe extern "C" fn __aeabi_ldivmod() {
7369
core::arch::asm!(
@@ -84,17 +80,13 @@ intrinsics! {
8480
);
8581
}
8682

87-
// The following functions use weak linkage to allow users to override
88-
// with custom implementation.
8983
// FIXME: The `*4` and `*8` variants should be defined as aliases.
9084

91-
#[weak]
9285
#[cfg(not(target_os = "ios"))]
9386
pub unsafe extern "aapcs" fn __aeabi_memcpy(dest: *mut u8, src: *const u8, n: usize) {
9487
crate::mem::memcpy(dest, src, n);
9588
}
9689

97-
#[weak]
9890
#[cfg(not(target_os = "ios"))]
9991
pub unsafe extern "aapcs" fn __aeabi_memcpy4(dest: *mut u8, src: *const u8, n: usize) {
10092
// We are guaranteed 4-alignment, so accessing at u32 is okay.
@@ -112,38 +104,32 @@ intrinsics! {
112104
__aeabi_memcpy(dest as *mut u8, src as *const u8, n);
113105
}
114106

115-
#[weak]
116107
#[cfg(not(target_os = "ios"))]
117108
pub unsafe extern "aapcs" fn __aeabi_memcpy8(dest: *mut u8, src: *const u8, n: usize) {
118109
__aeabi_memcpy4(dest, src, n);
119110
}
120111

121-
#[weak]
122112
#[cfg(not(target_os = "ios"))]
123113
pub unsafe extern "aapcs" fn __aeabi_memmove(dest: *mut u8, src: *const u8, n: usize) {
124114
crate::mem::memmove(dest, src, n);
125115
}
126116

127-
#[weak]
128117
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
129118
pub unsafe extern "aapcs" fn __aeabi_memmove4(dest: *mut u8, src: *const u8, n: usize) {
130119
__aeabi_memmove(dest, src, n);
131120
}
132121

133-
#[weak]
134122
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
135123
pub unsafe extern "aapcs" fn __aeabi_memmove8(dest: *mut u8, src: *const u8, n: usize) {
136124
__aeabi_memmove(dest, src, n);
137125
}
138126

139-
#[weak]
140127
#[cfg(not(target_os = "ios"))]
141128
pub unsafe extern "aapcs" fn __aeabi_memset(dest: *mut u8, n: usize, c: i32) {
142129
// Note the different argument order
143130
crate::mem::memset(dest, c, n);
144131
}
145132

146-
#[weak]
147133
#[cfg(not(target_os = "ios"))]
148134
pub unsafe extern "aapcs" fn __aeabi_memset4(dest: *mut u8, n: usize, c: i32) {
149135
let mut dest = dest as *mut u32;
@@ -161,25 +147,21 @@ intrinsics! {
161147
__aeabi_memset(dest as *mut u8, n, byte as i32);
162148
}
163149

164-
#[weak]
165150
#[cfg(not(target_os = "ios"))]
166151
pub unsafe extern "aapcs" fn __aeabi_memset8(dest: *mut u8, n: usize, c: i32) {
167152
__aeabi_memset4(dest, n, c);
168153
}
169154

170-
#[weak]
171155
#[cfg(not(target_os = "ios"))]
172156
pub unsafe extern "aapcs" fn __aeabi_memclr(dest: *mut u8, n: usize) {
173157
__aeabi_memset(dest, n, 0);
174158
}
175159

176-
#[weak]
177160
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
178161
pub unsafe extern "aapcs" fn __aeabi_memclr4(dest: *mut u8, n: usize) {
179162
__aeabi_memset4(dest, n, 0);
180163
}
181164

182-
#[weak]
183165
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
184166
pub unsafe extern "aapcs" fn __aeabi_memclr8(dest: *mut u8, n: usize) {
185167
__aeabi_memset4(dest, n, 0);

src/lib.rs

-8
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ mod macros;
4545
pub mod float;
4646
pub mod int;
4747

48-
#[cfg(any(
49-
all(target_family = "wasm", target_os = "unknown"),
50-
target_os = "uefi",
51-
target_os = "none",
52-
target_os = "xous",
53-
all(target_vendor = "fortanix", target_env = "sgx"),
54-
target_os = "windows"
55-
))]
5648
pub mod math;
5749
pub mod mem;
5850

src/macros.rs

+13-79
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ macro_rules! public_test_dep {
2525
/// platforms need and elsewhere in this library it just looks like normal Rust
2626
/// code.
2727
///
28-
/// When the weak-intrinsics feature is enabled, all intrinsics functions are
29-
/// marked with #[linkage = "weak"] so that they can be replaced by another
30-
/// implementation at link time. This is particularly useful for mixed Rust/C++
31-
/// binaries that want to use the C++ intrinsics, otherwise linking against the
32-
/// Rust stdlib will replace those from the compiler-rt library.
28+
/// All intrinsics functions are marked with #[linkage = "weak"] when
29+
/// `not(windows) and not(target_vendor = "apple")` so that they can be replaced
30+
/// by another implementation at link time. This is particularly useful for mixed
31+
/// Rust/C++ binaries that want to use the C++ intrinsics, otherwise linking against
32+
/// the Rust stdlib will replace those from the compiler-rt library.
3333
///
3434
/// This macro is structured to be invoked with a bunch of functions that looks
3535
/// like:
@@ -53,10 +53,6 @@ macro_rules! public_test_dep {
5353
///
5454
/// A quick overview of attributes supported right now are:
5555
///
56-
/// * `weak` - indicates that the function should always be given weak linkage.
57-
/// This attribute must come before other attributes, as the other attributes
58-
/// will generate the final output function and need to have `weak` modify
59-
/// them.
6056
/// * `maybe_use_optimized_c_shim` - indicates that the Rust implementation is
6157
/// ignored if an optimized C version was compiled.
6258
/// * `aapcs_on_arm` - forces the ABI of the function to be `"aapcs"` on ARM and
@@ -128,67 +124,6 @@ macro_rules! intrinsics {
128124
intrinsics!($($rest)*);
129125
);
130126

131-
// Explicit weak linkage gets dropped when weak-intrinsics is on since it
132-
// will be added unconditionally to all intrinsics and would conflict
133-
// otherwise.
134-
(
135-
#[weak]
136-
$(#[$($attr:tt)*])*
137-
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
138-
$($body:tt)*
139-
}
140-
141-
$($rest:tt)*
142-
) => (
143-
#[cfg(feature = "weak-intrinsics")]
144-
intrinsics! {
145-
$(#[$($attr)*])*
146-
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
147-
$($body)*
148-
}
149-
}
150-
151-
#[cfg(not(feature = "weak-intrinsics"))]
152-
intrinsics! {
153-
$(#[$($attr)*])*
154-
#[linkage = "weak"]
155-
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
156-
$($body)*
157-
}
158-
}
159-
160-
intrinsics!($($rest)*);
161-
);
162-
// Same as above but for unsafe.
163-
(
164-
#[weak]
165-
$(#[$($attr:tt)*])*
166-
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
167-
$($body:tt)*
168-
}
169-
170-
$($rest:tt)*
171-
) => (
172-
#[cfg(feature = "weak-intrinsics")]
173-
intrinsics! {
174-
$(#[$($attr)*])*
175-
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
176-
$($body)*
177-
}
178-
}
179-
180-
#[cfg(not(feature = "weak-intrinsics"))]
181-
intrinsics! {
182-
$(#[$($attr)*])*
183-
#[linkage = "weak"]
184-
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
185-
$($body)*
186-
}
187-
}
188-
189-
intrinsics!($($rest)*);
190-
);
191-
192127
// Right now there's a bunch of architecture-optimized intrinsics in the
193128
// stock compiler-rt implementation. Not all of these have been ported over
194129
// to Rust yet so when the `c` feature of this crate is enabled we fall back
@@ -211,7 +146,7 @@ macro_rules! intrinsics {
211146
$($rest:tt)*
212147
) => (
213148
#[cfg($name = "optimized-c")]
214-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
149+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
215150
pub $(unsafe $($empty)? )? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
216151
extern $abi {
217152
fn $name($($argname: $ty),*) $(-> $ret)?;
@@ -311,15 +246,14 @@ macro_rules! intrinsics {
311246
) => (
312247
#[cfg(all(any(windows, target_os = "uefi"), target_arch = "x86_64"))]
313248
$(#[$($attr)*])*
314-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
315249
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
316250
$($body)*
317251
}
318252

319253
#[cfg(all(any(windows, target_os = "uefi"), target_arch = "x86_64"))]
320254
pub mod $name {
321255
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
322-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
256+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
323257
pub extern $abi fn $name( $($argname: $ty),* )
324258
-> $crate::macros::win64_128bit_abi_hack::U64x2
325259
{
@@ -360,7 +294,7 @@ macro_rules! intrinsics {
360294
#[cfg(target_arch = "arm")]
361295
pub mod $name {
362296
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
363-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
297+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
364298
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
365299
super::$name($($argname),*)
366300
}
@@ -369,7 +303,7 @@ macro_rules! intrinsics {
369303
#[cfg(target_arch = "arm")]
370304
pub mod $alias {
371305
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
372-
#[cfg_attr(any(all(not(windows), not(target_vendor="apple")), feature = "weak-intrinsics"), linkage = "weak")]
306+
#[cfg_attr(all(not(windows), not(target_vendor="apple")), linkage = "weak")]
373307
pub extern "aapcs" fn $alias( $($argname: $ty),* ) $(-> $ret)? {
374308
super::$name($($argname),*)
375309
}
@@ -405,7 +339,7 @@ macro_rules! intrinsics {
405339
pub mod $name {
406340
$(#[$($attr)*])*
407341
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
408-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
342+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
409343
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
410344
super::$name($($argname),*)
411345
}
@@ -429,7 +363,7 @@ macro_rules! intrinsics {
429363
#[naked]
430364
$(#[$($attr)*])*
431365
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
432-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
366+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
433367
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
434368
$($body)*
435369
}
@@ -495,7 +429,7 @@ macro_rules! intrinsics {
495429
pub mod $name {
496430
$(#[$($attr)*])*
497431
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
498-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
432+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
499433
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
500434
super::$name($($argname),*)
501435
}
@@ -521,7 +455,7 @@ macro_rules! intrinsics {
521455
pub mod $name {
522456
$(#[$($attr)*])*
523457
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
524-
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
458+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
525459
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
526460
super::$name($($argname),*)
527461
}

src/math.rs

-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ macro_rules! no_mangle {
77
($(fn $fun:ident($($iid:ident : $ity:ty),+) -> $oty:ty;)+) => {
88
intrinsics! {
99
$(
10-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
1110
pub extern "C" fn $fun($($iid: $ity),+) -> $oty {
1211
self::libm::$fun($($iid),+)
1312
}
@@ -92,14 +91,12 @@ no_mangle! {
9291
}
9392

9493
intrinsics! {
95-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
9694
pub extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 {
9795
let r = self::libm::lgamma_r(x);
9896
*s = r.1;
9997
r.0
10098
}
10199

102-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
103100
pub extern "C" fn lgammaf_r(x: f32, s: &mut i32) -> f32 {
104101
let r = self::libm::lgammaf_r(x);
105102
*s = r.1;

src/mem/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ use core::ops::{BitOr, Shl};
2020
mod impls;
2121

2222
intrinsics! {
23-
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
2423
#[mem_builtin]
2524
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
2625
impls::copy_forward(dest, src, n);
2726
dest
2827
}
2928

30-
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
3129
#[mem_builtin]
3230
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
3331
let delta = (dest as usize).wrapping_sub(src as usize);
@@ -41,26 +39,22 @@ intrinsics! {
4139
dest
4240
}
4341

44-
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
4542
#[mem_builtin]
4643
pub unsafe extern "C" fn memset(s: *mut u8, c: crate::mem::c_int, n: usize) -> *mut u8 {
4744
impls::set_bytes(s, c as u8, n);
4845
s
4946
}
5047

51-
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
5248
#[mem_builtin]
5349
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
5450
impls::compare_bytes(s1, s2, n)
5551
}
5652

57-
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
5853
#[mem_builtin]
5954
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
6055
memcmp(s1, s2, n)
6156
}
6257

63-
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
6458
#[mem_builtin]
6559
pub unsafe extern "C" fn strlen(s: *const core::ffi::c_char) -> usize {
6660
impls::c_string_length(s)

0 commit comments

Comments
 (0)