@@ -25,12 +25,11 @@ macro_rules! public_test_dep {
25
25
/// platforms need and elsewhere in this library it just looks like normal Rust
26
26
/// code.
27
27
///
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.
34
33
///
35
34
/// This macro is structured to be invoked with a bunch of functions that looks
36
35
/// like:
@@ -54,6 +53,10 @@ macro_rules! public_test_dep {
54
53
///
55
54
/// A quick overview of attributes supported right now are:
56
55
///
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.
57
60
/// * `maybe_use_optimized_c_shim` - indicates that the Rust implementation is
58
61
/// ignored if an optimized C version was compiled.
59
62
/// * `aapcs_on_arm` - forces the ABI of the function to be `"aapcs"` on ARM and
@@ -128,6 +131,67 @@ macro_rules! intrinsics {
128
131
intrinsics!( $( $rest) * ) ;
129
132
) ;
130
133
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
+
131
195
// Right now there's a bunch of architecture-optimized intrinsics in the
132
196
// stock compiler-rt implementation. Not all of these have been ported over
133
197
// to Rust yet so when the `c` feature of this crate is enabled we fall back
@@ -150,7 +214,7 @@ macro_rules! intrinsics {
150
214
$( $rest: tt) *
151
215
) => (
152
216
#[ cfg( $name = "optimized-c" ) ]
153
- #[ cfg_attr( all ( not ( windows ) , not ( target_vendor = "apple" ) ) , linkage = "weak" ) ]
217
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
154
218
pub $( unsafe $( $empty) ? ) ? extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
155
219
extern $abi {
156
220
fn $name( $( $argname: $ty) ,* ) $( -> $ret) ?;
@@ -250,14 +314,15 @@ macro_rules! intrinsics {
250
314
) => (
251
315
#[ cfg( all( any( windows, target_os = "uefi" ) , target_arch = "x86_64" ) ) ]
252
316
$( #[ $( $attr) * ] ) *
317
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
253
318
pub extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
254
319
$( $body) *
255
320
}
256
321
257
322
#[ cfg( all( any( windows, target_os = "uefi" ) , target_arch = "x86_64" ) ) ]
258
323
pub mod $name {
259
324
#[ 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" ) ]
261
326
pub extern $abi fn $name( $( $argname: $ty) ,* )
262
327
-> $crate:: macros:: win64_128bit_abi_hack:: U64x2
263
328
{
@@ -298,7 +363,7 @@ macro_rules! intrinsics {
298
363
#[ cfg( target_arch = "arm" ) ]
299
364
pub mod $name {
300
365
#[ 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" ) ]
302
367
pub extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
303
368
super :: $name( $( $argname) ,* )
304
369
}
@@ -307,7 +372,7 @@ macro_rules! intrinsics {
307
372
#[ cfg( target_arch = "arm" ) ]
308
373
pub mod $alias {
309
374
#[ 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" ) ]
311
376
pub extern "aapcs" fn $alias( $( $argname: $ty) ,* ) $( -> $ret) ? {
312
377
super :: $name( $( $argname) ,* )
313
378
}
@@ -373,7 +438,7 @@ macro_rules! intrinsics {
373
438
pub mod $name {
374
439
$( #[ $( $attr) * ] ) *
375
440
#[ 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" ) ]
377
442
pub unsafe extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
378
443
super :: $name( $( $argname) ,* )
379
444
}
@@ -397,7 +462,7 @@ macro_rules! intrinsics {
397
462
#[ naked]
398
463
$( #[ $( $attr) * ] ) *
399
464
#[ 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" ) ]
401
466
pub unsafe extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
402
467
$( $body) *
403
468
}
@@ -463,7 +528,7 @@ macro_rules! intrinsics {
463
528
pub mod $name {
464
529
$( #[ $( $attr) * ] ) *
465
530
#[ 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" ) ]
467
532
pub extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
468
533
super :: $name( $( $argname) ,* )
469
534
}
@@ -489,7 +554,7 @@ macro_rules! intrinsics {
489
554
pub mod $name {
490
555
$( #[ $( $attr) * ] ) *
491
556
#[ 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" ) ]
493
558
pub unsafe extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
494
559
super :: $name( $( $argname) ,* )
495
560
}
0 commit comments