@@ -25,6 +25,12 @@ 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
+ /// 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.
33
+ ///
28
34
/// This macro is structured to be invoked with a bunch of functions that looks
29
35
/// like:
30
36
///
@@ -218,6 +224,7 @@ macro_rules! intrinsics {
218
224
#[ cfg( all( any( windows, target_os = "uefi" ) , target_arch = "x86_64" ) ) ]
219
225
pub mod $name {
220
226
#[ cfg_attr( not( feature = "mangled-names" ) , no_mangle) ]
227
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
221
228
pub extern $abi fn $name( $( $argname: $ty) ,* )
222
229
-> :: macros:: win64_128bit_abi_hack:: U64x2
223
230
{
@@ -258,6 +265,7 @@ macro_rules! intrinsics {
258
265
#[ cfg( target_arch = "arm" ) ]
259
266
pub mod $name {
260
267
#[ cfg_attr( not( feature = "mangled-names" ) , no_mangle) ]
268
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
261
269
pub extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
262
270
super :: $name( $( $argname) ,* )
263
271
}
@@ -266,7 +274,7 @@ macro_rules! intrinsics {
266
274
#[ cfg( target_arch = "arm" ) ]
267
275
pub mod $alias {
268
276
#[ cfg_attr( not( feature = "mangled-names" ) , no_mangle) ]
269
- #[ cfg_attr( all( not( windows) , not( target_vendor="apple" ) ) , linkage = "weak" ) ]
277
+ #[ cfg_attr( any ( all( not( windows) , not( target_vendor="apple" ) , feature = "weak-intrinsics ") ) , linkage = "weak" ) ]
270
278
pub extern "aapcs" fn $alias( $( $argname: $ty) ,* ) $( -> $ret) ? {
271
279
super :: $name( $( $argname) ,* )
272
280
}
@@ -283,6 +291,38 @@ macro_rules! intrinsics {
283
291
intrinsics!( $( $rest) * ) ;
284
292
) ;
285
293
294
+ // Explicit weak linkage gets dropped when weak-intrinsics is on since it
295
+ // will be added unconditionally to all intrinsics and would conflict
296
+ // otherwise.
297
+ (
298
+ #[ linkage = "weak" ]
299
+ $( #[ $( $attr: tt) * ] ) *
300
+ pub unsafe extern $abi: tt fn $name: ident( $( $argname: ident: $ty: ty) ,* ) $( -> $ret: ty) ? {
301
+ $( $body: tt) *
302
+ }
303
+
304
+ $( $rest: tt) *
305
+ ) => (
306
+ #[ cfg( feature = "weak-intrinsics" ) ]
307
+ intrinsics! {
308
+ $( #[ $( $attr) * ] ) *
309
+ pub extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
310
+ $( $body) *
311
+ }
312
+ }
313
+
314
+ #[ cfg( not( feature = "weak-intrinsics" ) ) ]
315
+ intrinsics! {
316
+ $( #[ $( $attr) * ] ) *
317
+ #[ linkage = "weak" ]
318
+ pub extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
319
+ $( $body) *
320
+ }
321
+ }
322
+
323
+ intrinsics!( $( $rest) * ) ;
324
+ ) ;
325
+
286
326
// C mem* functions are only generated when the "mem" feature is enabled.
287
327
(
288
328
#[ mem_builtin]
@@ -302,6 +342,7 @@ macro_rules! intrinsics {
302
342
pub mod $name {
303
343
$( #[ $( $attr) * ] ) *
304
344
#[ cfg_attr( not( feature = "mangled-names" ) , no_mangle) ]
345
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
305
346
pub unsafe extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
306
347
super :: $name( $( $argname) ,* )
307
348
}
@@ -325,6 +366,7 @@ macro_rules! intrinsics {
325
366
#[ naked]
326
367
$( #[ $( $attr) * ] ) *
327
368
#[ cfg_attr( not( feature = "mangled-names" ) , no_mangle) ]
369
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
328
370
pub unsafe extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
329
371
$( $body) *
330
372
}
@@ -391,6 +433,7 @@ macro_rules! intrinsics {
391
433
pub mod $name {
392
434
$( #[ $( $attr) * ] ) *
393
435
#[ cfg_attr( not( feature = "mangled-names" ) , no_mangle) ]
436
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
394
437
pub extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
395
438
super :: $name( $( $argname) ,* )
396
439
}
@@ -416,6 +459,7 @@ macro_rules! intrinsics {
416
459
pub mod $name {
417
460
$( #[ $( $attr) * ] ) *
418
461
#[ cfg_attr( not( feature = "mangled-names" ) , no_mangle) ]
462
+ #[ cfg_attr( feature = "weak-intrinsics" , linkage = "weak" ) ]
419
463
pub unsafe extern $abi fn $name( $( $argname: $ty) ,* ) $( -> $ret) ? {
420
464
super :: $name( $( $argname) ,* )
421
465
}
0 commit comments