1
Fork 0

Use llvm.compiler.used insetad of llvm.used

The #[used] attribute explicitly only requires symbols to be
retained in object files, but allows the linker to drop them
if dead. This corresponds to llvm.compiler.used semantics.

The motivation to change this *now* is that https://reviews.llvm.org/D97448
starts emitting #[used] symbols into unique sections with
SHF_GNU_RETAIN flag. This triggers a bug in some version of gold,
resulting in the ARGV_INIT_ARRAY symbol part of the .init_array
section to be incorrectly placed.
This commit is contained in:
Nikita Popov 2021-08-07 17:04:32 +02:00
parent 154c8408e9
commit 7c015648dd
5 changed files with 16 additions and 8 deletions

View file

@ -71,8 +71,8 @@ pub struct CodegenCx<'ll, 'tcx> {
/// to constants.)
pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
/// Statics that will be placed in the llvm.used variable
/// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
/// Statics that will be placed in the llvm.compiler.used variable
/// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
pub used_statics: RefCell<Vec<&'ll Value>>,
/// Mapping of non-scalar types to llvm types and field remapping if needed.
@ -447,7 +447,13 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}
fn create_used_variable(&self) {
let name = cstr!("llvm.used");
// The semantics of #[used] in Rust only require the symbol to make it into the object
// file. It is explicitly allowed for the linker to strip the symbol if it is dead.
// As such, use llvm.compiler.used instead of llvm.used.
// Additionally, https://reviews.llvm.org/D97448 in LLVM 13 started emitting unique
// sections with SHF_GNU_RETAIN flag for llvm.used symbols, which may trigger bugs in
// some versions of the gold linker.
let name = cstr!("llvm.compiler.used");
let section = cstr!("llvm.metadata");
let array =
self.const_array(&self.type_ptr_to(self.type_i8()), &*self.used_statics.borrow());