You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that this has peculiar semantics, see #156468.
In particular, unlike normal statics, the local global variable we generate for this indirection does not have a globally unique address (rust-lang/miri#5015, #156467).
These are very LLVM specific, it's unclear how applicable they are to other backends.
Beyond external or weak, I've never seen a reason to use the other attributes.
These linkage methods are platform-specific and aren't guaranteed to work everywhere.
That being said, it's the only way to do weak symbols on Linux and it's also convenient for exporting a known symbol without worrying about privacy on the Rust side of things. I would personally want to reduce the set of accepted linkage types and then state "well of course linkage is platform-specific!"
Which prints 0 @ 5617765adaa8 @ 0x5617765ad890 meaning that the A seen by the rust code contains the address of A and not A itself. It's also easy to get LLVM to abort with this.
I think we should at least work on stabilising the weak and extern_weak linkage. Both are very useful and a fairly widespread linkage options that seem to be supported (unlike many other options currently exposed) by at least all of the tier 1 targets.
To give an example of a use-case for weak linkage in Rust code (I need weak linkage for libloading), one would be for global statics unique even between multiple versions of the same crate. Consider that, currently, if a binary links log = ^0.3 and log = ^0.4 some way, or another, it will end up having two distinct global loggers. This could trivially be resolved with some use of the weak linkage option (as it ensures – at link time – only one instance of global with the same name).
That being said, #[linkage] should always infect whatever it applies to with unsafety. Consider for example these two crates:
when linked together, all uses of FOO as seen from the newer-version crate would be UB if the linker happened to choose to link-in the value from the older version. This is functionally a transmute without size checks.
Reacted by Michael Bradshaw, Mateusz Mikuła, Luo Jia / Zhouqi Jiang, Js2xxx, Trevor Gross, Mads Marquart, qouteall and Mavity the Madity
extern_weak is broken in such a dubious way that even though I pointed out two years ago that it was being used incorrectly in the stdlib, the bug still hasn't been fixed.
PS: The address of __dso_handle isn't actually too significant in __cxa_thread_atexit_impl and &__dso_handle is only a few bytes off.
PSS: Wow, looks like I already explained this above (also two years ago). Maybe the NSA is trying to keep this potential remote code execution unfixed. 🤔
Activity
aturon commentedon Nov 5, 2015
cc @alexcrichton
alexcrichton commentedon Nov 5, 2015
Currently this translates to the various linkage models of LLVM, for example the values of the attribute can be:
Some worries about this are:
externalorweak, I've never seen a reason to use the other attributes.That being said, it's the only way to do weak symbols on Linux and it's also convenient for exporting a known symbol without worrying about privacy on the Rust side of things. I would personally want to reduce the set of accepted linkage types and then state "well of course linkage is platform-specific!"
aturon commentedon Nov 5, 2015
cc #29629
mahkoh commentedon Jan 11, 2016
This feature is fundamentally broken. Consider the following code:
Which prints
0 @ 5617765adaa8 @ 0x5617765ad890meaning that theAseen by the rust code contains the address ofAand notAitself. It's also easy to get LLVM to abort with this.mahkoh commentedon Jan 11, 2016
This attribute is also used incorrectly here.
brson commentedon Jun 1, 2017
#18804 needs to be resolved before stabilization.
nagisa commentedon Jan 6, 2018
I think we should at least work on stabilising the
weakandextern_weaklinkage. Both are very useful and a fairly widespread linkage options that seem to be supported (unlike many other options currently exposed) by at least all of the tier 1 targets.To give an example of a use-case for
weaklinkage in Rust code (I need weak linkage for libloading), one would be for global statics unique even between multiple versions of the same crate. Consider that, currently, if a binary linkslog = ^0.3andlog = ^0.4some way, or another, it will end up having two distinct global loggers. This could trivially be resolved with some use of theweaklinkage option (as it ensures – at link time – only one instance of global with the same name).That being said,
#[linkage]should always infect whatever it applies to with unsafety. Consider for example these two crates:when linked together, all uses of
FOOas seen from the newer-version crate would be UB if the linker happened to choose to link-in the value from the older version. This is functionally a transmute without size checks.mahkoh commentedon Jan 13, 2018
extern_weak is broken in such a dubious way that even though I pointed out two years ago that it was being used incorrectly in the stdlib, the bug still hasn't been fixed.
gcc and clang handle this attribute correctly.
PS: The address of __dso_handle isn't actually too significant in __cxa_thread_atexit_impl and &__dso_handle is only a few bytes off.
PSS: Wow, looks like I already explained this above (also two years ago). Maybe the NSA is trying to keep this potential remote code execution unfixed. 🤔
38 remaining items
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
tgross35 commentedon Sep 28, 2026
Link to Zulip discussion #general > Question(s) about making an RFC
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of rust-lang#163405 - bjorn3:less_linkage_options, r=lqd
Unrolled build for #163405 in rollup 163609
Rollup merge of #163405 - bjorn3:less_linkage_options, r=lqd
Rollup merge of #163405 - bjorn3:less_linkage_options, r=lqd