Skip to content

Commit 42538d8

Browse files
committed
Revert "Apply weak attributes to all intrinsics"
This reverts commit 7f9c937.
1 parent 6de6be7 commit 42538d8

File tree

5 files changed

+117
-14
lines changed

5 files changed

+117
-14
lines changed

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ rustc-dep-of-std = ['compiler-builtins', 'core']
7070
# are not normally public but are required by the `testcrate`
7171
public-test-deps = []
7272

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

src/arm.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 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)]
2324
#[naked]
2425
#[cfg(not(target_env = "msvc"))]
2526
pub unsafe extern "C" fn __aeabi_uidivmod() {
@@ -35,6 +36,7 @@ intrinsics! {
3536
);
3637
}
3738

39+
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
3840
#[naked]
3941
pub unsafe extern "C" fn __aeabi_uldivmod() {
4042
core::arch::asm!(
@@ -51,6 +53,7 @@ intrinsics! {
5153
);
5254
}
5355

56+
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
5457
#[naked]
5558
pub unsafe extern "C" fn __aeabi_idivmod() {
5659
core::arch::asm!(
@@ -64,6 +67,7 @@ intrinsics! {
6467
);
6568
}
6669

70+
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
6771
#[naked]
6872
pub unsafe extern "C" fn __aeabi_ldivmod() {
6973
core::arch::asm!(
@@ -80,13 +84,17 @@ intrinsics! {
8084
);
8185
}
8286

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

91+
#[weak]
8592
#[cfg(not(target_os = "ios"))]
8693
pub unsafe extern "aapcs" fn __aeabi_memcpy(dest: *mut u8, src: *const u8, n: usize) {
8794
crate::mem::memcpy(dest, src, n);
8895
}
8996

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

115+
#[weak]
107116
#[cfg(not(target_os = "ios"))]
108117
pub unsafe extern "aapcs" fn __aeabi_memcpy8(dest: *mut u8, src: *const u8, n: usize) {
109118
__aeabi_memcpy4(dest, src, n);
110119
}
111120

121+
#[weak]
112122
#[cfg(not(target_os = "ios"))]
113123
pub unsafe extern "aapcs" fn __aeabi_memmove(dest: *mut u8, src: *const u8, n: usize) {
114124
crate::mem::memmove(dest, src, n);
115125
}
116126

127+
#[weak]
117128
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
118129
pub unsafe extern "aapcs" fn __aeabi_memmove4(dest: *mut u8, src: *const u8, n: usize) {
119130
__aeabi_memmove(dest, src, n);
120131
}
121132

133+
#[weak]
122134
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
123135
pub unsafe extern "aapcs" fn __aeabi_memmove8(dest: *mut u8, src: *const u8, n: usize) {
124136
__aeabi_memmove(dest, src, n);
125137
}
126138

139+
#[weak]
127140
#[cfg(not(target_os = "ios"))]
128141
pub unsafe extern "aapcs" fn __aeabi_memset(dest: *mut u8, n: usize, c: i32) {
129142
// Note the different argument order
130143
crate::mem::memset(dest, c, n);
131144
}
132145

146+
#[weak]
133147
#[cfg(not(target_os = "ios"))]
134148
pub unsafe extern "aapcs" fn __aeabi_memset4(dest: *mut u8, n: usize, c: i32) {
135149
let mut dest = dest as *mut u32;
@@ -147,21 +161,25 @@ intrinsics! {
147161
__aeabi_memset(dest as *mut u8, n, byte as i32);
148162
}
149163

164+
#[weak]
150165
#[cfg(not(target_os = "ios"))]
151166
pub unsafe extern "aapcs" fn __aeabi_memset8(dest: *mut u8, n: usize, c: i32) {
152167
__aeabi_memset4(dest, n, c);
153168
}
154169

170+
#[weak]
155171
#[cfg(not(target_os = "ios"))]
156172
pub unsafe extern "aapcs" fn __aeabi_memclr(dest: *mut u8, n: usize) {
157173
__aeabi_memset(dest, n, 0);
158174
}
159175

176+
#[weak]
160177
#[cfg(not(any(target_os = "ios", target_env = "msvc")))]
161178
pub unsafe extern "aapcs" fn __aeabi_memclr4(dest: *mut u8, n: usize) {
162179
__aeabi_memset4(dest, n, 0);
163180
}
164181

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

src/macros.rs

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +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-
/// All intrinsics functions are marked with #[linkage = "weak"] when
29-
/// `not(windows) and not(target_vendor = "apple")`.
30-
/// `weak` linkage attribute is used so that these functions can be replaced
31-
/// by another implementation at link time. This is particularly useful for mixed
32-
/// Rust/C++ binaries that want to use the C++ intrinsics, otherwise linking against
33-
/// the Rust stdlib will replace those from the compiler-rt library.
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.
3433
///
3534
/// This macro is structured to be invoked with a bunch of functions that looks
3635
/// like:
@@ -54,6 +53,10 @@ macro_rules! public_test_dep {
5453
///
5554
/// A quick overview of attributes supported right now are:
5655
///
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.
5760
/// * `maybe_use_optimized_c_shim` - indicates that the Rust implementation is
5861
/// ignored if an optimized C version was compiled.
5962
/// * `aapcs_on_arm` - forces the ABI of the function to be `"aapcs"` on ARM and
@@ -128,6 +131,67 @@ macro_rules! intrinsics {
128131
intrinsics!($($rest)*);
129132
);
130133

134+
// Explicit weak linkage gets dropped when weak-intrinsics is on since it
135+
// will be added unconditionally to all intrinsics and would conflict
136+
// otherwise.
137+
(
138+
#[weak]
139+
$(#[$($attr:tt)*])*
140+
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
141+
$($body:tt)*
142+
}
143+
144+
$($rest:tt)*
145+
) => (
146+
#[cfg(feature = "weak-intrinsics")]
147+
intrinsics! {
148+
$(#[$($attr)*])*
149+
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
150+
$($body)*
151+
}
152+
}
153+
154+
#[cfg(not(feature = "weak-intrinsics"))]
155+
intrinsics! {
156+
$(#[$($attr)*])*
157+
#[linkage = "weak"]
158+
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
159+
$($body)*
160+
}
161+
}
162+
163+
intrinsics!($($rest)*);
164+
);
165+
// Same as above but for unsafe.
166+
(
167+
#[weak]
168+
$(#[$($attr:tt)*])*
169+
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
170+
$($body:tt)*
171+
}
172+
173+
$($rest:tt)*
174+
) => (
175+
#[cfg(feature = "weak-intrinsics")]
176+
intrinsics! {
177+
$(#[$($attr)*])*
178+
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
179+
$($body)*
180+
}
181+
}
182+
183+
#[cfg(not(feature = "weak-intrinsics"))]
184+
intrinsics! {
185+
$(#[$($attr)*])*
186+
#[linkage = "weak"]
187+
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
188+
$($body)*
189+
}
190+
}
191+
192+
intrinsics!($($rest)*);
193+
);
194+
131195
// Right now there's a bunch of architecture-optimized intrinsics in the
132196
// stock compiler-rt implementation. Not all of these have been ported over
133197
// to Rust yet so when the `c` feature of this crate is enabled we fall back
@@ -150,7 +214,7 @@ macro_rules! intrinsics {
150214
$($rest:tt)*
151215
) => (
152216
#[cfg($name = "optimized-c")]
153-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
217+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
154218
pub $(unsafe $($empty)? )? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
155219
extern $abi {
156220
fn $name($($argname: $ty),*) $(-> $ret)?;
@@ -250,14 +314,15 @@ macro_rules! intrinsics {
250314
) => (
251315
#[cfg(all(any(windows, target_os = "uefi"), target_arch = "x86_64"))]
252316
$(#[$($attr)*])*
317+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
253318
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
254319
$($body)*
255320
}
256321

257322
#[cfg(all(any(windows, target_os = "uefi"), target_arch = "x86_64"))]
258323
pub mod $name {
259324
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
260-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
325+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
261326
pub extern $abi fn $name( $($argname: $ty),* )
262327
-> $crate::macros::win64_128bit_abi_hack::U64x2
263328
{
@@ -298,7 +363,7 @@ macro_rules! intrinsics {
298363
#[cfg(target_arch = "arm")]
299364
pub mod $name {
300365
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
301-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
366+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
302367
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
303368
super::$name($($argname),*)
304369
}
@@ -307,7 +372,7 @@ macro_rules! intrinsics {
307372
#[cfg(target_arch = "arm")]
308373
pub mod $alias {
309374
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
310-
#[cfg_attr(all(not(windows), not(target_vendor="apple")), linkage = "weak")]
375+
#[cfg_attr(any(all(not(windows), not(target_vendor="apple")), feature = "weak-intrinsics"), linkage = "weak")]
311376
pub extern "aapcs" fn $alias( $($argname: $ty),* ) $(-> $ret)? {
312377
super::$name($($argname),*)
313378
}
@@ -373,7 +438,7 @@ macro_rules! intrinsics {
373438
pub mod $name {
374439
$(#[$($attr)*])*
375440
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
376-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
441+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
377442
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
378443
super::$name($($argname),*)
379444
}
@@ -397,7 +462,7 @@ macro_rules! intrinsics {
397462
#[naked]
398463
$(#[$($attr)*])*
399464
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
400-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
465+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
401466
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
402467
$($body)*
403468
}
@@ -463,7 +528,7 @@ macro_rules! intrinsics {
463528
pub mod $name {
464529
$(#[$($attr)*])*
465530
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
466-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
531+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
467532
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
468533
super::$name($($argname),*)
469534
}
@@ -489,7 +554,7 @@ macro_rules! intrinsics {
489554
pub mod $name {
490555
$(#[$($attr)*])*
491556
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
492-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
557+
#[cfg_attr(feature = "weak-intrinsics", linkage = "weak")]
493558
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
494559
super::$name($($argname),*)
495560
}

src/math.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ macro_rules! no_mangle {
99
($(fn $fun:ident($($iid:ident : $ity:ty),+) -> $oty:ty;)+) => {
1010
intrinsics! {
1111
$(
12+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
1213
pub extern "C" fn $fun($($iid: $ity),+) -> $oty {
1314
self::libm::$fun($($iid),+)
1415
}
@@ -93,12 +94,14 @@ no_mangle! {
9394
}
9495

9596
intrinsics! {
97+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
9698
pub extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 {
9799
let r = self::libm::lgamma_r(x);
98100
*s = r.1;
99101
r.0
100102
}
101103

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

src/mem/mod.rs

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

2222
intrinsics! {
23+
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
2324
#[mem_builtin]
2425
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
2526
impls::copy_forward(dest, src, n);
2627
dest
2728
}
2829

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

44+
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
4245
#[mem_builtin]
4346
pub unsafe extern "C" fn memset(s: *mut u8, c: crate::mem::c_int, n: usize) -> *mut u8 {
4447
impls::set_bytes(s, c as u8, n);
4548
s
4649
}
4750

51+
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
4852
#[mem_builtin]
4953
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
5054
impls::compare_bytes(s1, s2, n)
5155
}
5256

57+
#[cfg_attr(not(all(target_os = "windows", target_env = "gnu")), weak)]
5358
#[mem_builtin]
5459
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
5560
memcmp(s1, s2, n)
5661
}
5762

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

0 commit comments

Comments
 (0)