Auto merge of #85276 - Bobo1239:more_dso_local, r=nagisa

Set dso_local for more items

Related to https://github.com/rust-lang/rust/pull/83592. (cc `@nagisa)`

Noticed that on x86_64 with `relocation-model: static` `R_X86_64_GOTPCREL` relocations were still generated in some cases. (related: https://github.com/Rust-for-Linux/linux/issues/135; Rust-for-Linux needs these fixes to successfully build)

First time doing anything with LLVM so not sure whether this is correct but the following are some of the things I've tried to convince myself.

## C equivalent

Example from clang which also sets `dso_local` in these cases:
`clang-12 -fno-PIC -S -emit-llvm test.c`
```C
extern int A;

int* a() {
    return &A;
}

int B;

int* b() {
    return &B;
}
```
```
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

`@A` = external dso_local global i32, align 4
`@B` = dso_local global i32 0, align 4

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32* `@a()` #0 {
  ret i32* `@A`
}

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32* `@b()` #0 {
  ret i32* `@B`
}

attributes #0 = { noinline nounwind optnone uwtable "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project/ b978a93635b584db380274d7c8963c73989944a1)"}
```
`clang-12 -fno-PIC -c test.c`
`objdump test.o -r`:
```
test.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
0000000000000006 R_X86_64_64       A
0000000000000016 R_X86_64_64       B

RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE
0000000000000020 R_X86_64_PC32     .text
0000000000000040 R_X86_64_PC32     .text+0x0000000000000010
```

## Comparison to pre-LLVM 12 output

`rustc --emit=obj,llvm-ir --target=x86_64-unknown-none-linuxkernel --crate-type rlib test.rs`
```Rust
#![feature(no_core, lang_items)]
#![no_core]

#[lang="sized"]
trait Sized {}

#[lang="sync"]
trait Sync {}

#[lang = "drop_in_place"]
pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) {}

impl Sync for i32 {}

pub static STATIC: i32 = 32;

extern {
    pub static EXT_STATIC: i32;
}

pub fn a() -> &'static i32 {
    &STATIC
}
pub fn b() -> &'static i32 {
    unsafe {&EXT_STATIC}
}
```
`objdump test.o -r`
nightly-2021-02-20 (rustc target is `x86_64-linux-kernel`):
```
RELOCATION RECORDS FOR [.text._ZN4test1a17h1024ba65f3424175E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      _ZN4test6STATIC17h3adc41a83746c9ffE

RELOCATION RECORDS FOR [.text._ZN4test1b17h86a6a80c1190ac8dE]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      EXT_STATIC
```
nightly-2021-05-10:
```
RELOCATION RECORDS FOR [.text._ZN4test1a17he846f03bf37b2d20E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_GOTPCREL  _ZN4test6STATIC17h5a059515bf3d4968E-0x0000000000000004

RELOCATION RECORDS FOR [.text._ZN4test1b17h7e0f7f80fbd91125E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_GOTPCREL  EXT_STATIC-0x0000000000000004
```
This PR:
```
RELOCATION RECORDS FOR [.text._ZN4test1a17he846f03bf37b2d20E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      _ZN4test6STATIC17h5a059515bf3d4968E

RELOCATION RECORDS FOR [.text._ZN4test1b17h7e0f7f80fbd91125E]:
OFFSET           TYPE              VALUE
0000000000000007 R_X86_64_32S      EXT_STATIC
```
This commit is contained in:
bors 2021-05-19 07:25:17 +00:00
commit be8450eec8
4 changed files with 76 additions and 5 deletions

View File

@ -218,3 +218,27 @@ pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
Visibility::Protected => llvm::Visibility::Protected,
}
}
pub fn linkage_from_llvm(linkage: llvm::Linkage) -> Linkage {
match linkage {
llvm::Linkage::ExternalLinkage => Linkage::External,
llvm::Linkage::AvailableExternallyLinkage => Linkage::AvailableExternally,
llvm::Linkage::LinkOnceAnyLinkage => Linkage::LinkOnceAny,
llvm::Linkage::LinkOnceODRLinkage => Linkage::LinkOnceODR,
llvm::Linkage::WeakAnyLinkage => Linkage::WeakAny,
llvm::Linkage::WeakODRLinkage => Linkage::WeakODR,
llvm::Linkage::AppendingLinkage => Linkage::Appending,
llvm::Linkage::InternalLinkage => Linkage::Internal,
llvm::Linkage::PrivateLinkage => Linkage::Private,
llvm::Linkage::ExternalWeakLinkage => Linkage::ExternalWeak,
llvm::Linkage::CommonLinkage => Linkage::Common,
}
}
pub fn visibility_from_llvm(linkage: llvm::Visibility) -> Visibility {
match linkage {
llvm::Visibility::Default => Visibility::Default,
llvm::Visibility::Hidden => Visibility::Hidden,
llvm::Visibility::Protected => Visibility::Protected,
}
}

View File

@ -17,6 +17,7 @@
use rustc_middle::ty::{self, Instance, Ty};
use rustc_middle::{bug, span_bug};
use rustc_target::abi::{AddressSpace, Align, HasDataLayout, LayoutOf, Primitive, Scalar, Size};
use rustc_target::spec::RelocModel;
use tracing::debug;
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
@ -282,6 +283,12 @@ impl CodegenCx<'ll, 'tcx> {
}
}
if self.tcx.sess.relocation_model() == RelocModel::Static {
unsafe {
llvm::LLVMRustSetDSOLocal(g, true);
}
}
self.instances.borrow_mut().insert(instance, g);
g
}
@ -363,6 +370,12 @@ fn codegen_static(&self, def_id: DefId, is_mutable: bool) {
set_global_alignment(&self, g, self.align_of(ty));
llvm::LLVMSetInitializer(g, v);
let linkage = base::linkage_from_llvm(llvm::LLVMRustGetLinkage(g));
let visibility = base::visibility_from_llvm(llvm::LLVMRustGetVisibility(g));
if self.should_assume_dso_local(linkage, visibility) {
llvm::LLVMRustSetDSOLocal(g, true);
}
// As an optimization, all shared statics which do not have interior
// mutability are placed into read-only memory.
if !is_mutable && self.type_is_freeze(ty) {

View File

@ -54,7 +54,7 @@ pub enum CallConv {
}
/// LLVMRustLinkage
#[derive(PartialEq)]
#[derive(Copy, Clone, PartialEq)]
#[repr(C)]
pub enum Linkage {
ExternalLinkage = 0,
@ -72,6 +72,7 @@ pub enum Linkage {
// LLVMRustVisibility
#[repr(C)]
#[derive(Copy, Clone)]
pub enum Visibility {
Default = 0,
Hidden = 1,

View File

@ -1,8 +1,8 @@
// min-llvm-version: 12.0.0
// needs-llvm-components: aarch64 x86
// revisions:X64 A64
// revisions:x64 A64
// assembly-output: emit-asm
// [X64] compile-flags: --target x86_64-unknown-linux-gnu -Crelocation-model=static
// [x64] compile-flags: --target x86_64-unknown-linux-gnu -Crelocation-model=static
// [A64] compile-flags: --target aarch64-unknown-linux-gnu -Crelocation-model=static
#![feature(no_core, lang_items)]
@ -15,14 +15,26 @@ trait Sized {}
#[lang="copy"]
trait Copy {}
#[lang="sync"]
trait Sync {}
#[lang = "drop_in_place"]
fn drop_in_place<T>(_: *mut T) {}
impl Copy for u8 {}
impl Sync for u8 {}
#[no_mangle]
pub static PIERIS: u8 = 42;
extern "C" {
static EXOCHORDA: *mut u8;
fn chaenomeles();
}
// CHECK-LABEL: banana:
// x64: movb chaenomeles, %{{[a,z]+}}
// x64: movb chaenomeles{{(\(%[a-z0-9]+\))?}}, %{{[a-z0-9]+}}
// A64: adrp [[REG:[a-z0-9]+]], chaenomeles
// A64-NEXT: ldrb {{[a-z0-9]+}}, {{\[}}[[REG]], :lo12:chaenomeles]
#[no_mangle]
@ -33,7 +45,7 @@ pub fn banana() -> u8 {
}
// CHECK-LABEL: peach:
// x64: movb banana, %{{[a,z]+}}
// x64: movb banana{{(\(%[a-z0-9]+\))?}}, %{{[a-z0-9]+}}
// A64: adrp [[REG2:[a-z0-9]+]], banana
// A64-NEXT: ldrb {{[a-z0-9]+}}, {{\[}}[[REG2]], :lo12:banana]
#[no_mangle]
@ -42,3 +54,24 @@ pub fn peach() -> u8 {
*(banana as *mut u8)
}
}
// CHECK-LABEL: mango:
// x64: movq EXOCHORDA{{(\(%[a-z0-9]+\))?}}, %[[REG:[a-z0-9]+]]
// x64-NEXT: movb (%[[REG]]), %{{[a-z0-9]+}}
// A64: adrp [[REG2:[a-z0-9]+]], EXOCHORDA
// A64-NEXT: ldr {{[a-z0-9]+}}, {{\[}}[[REG2]], :lo12:EXOCHORDA]
#[no_mangle]
pub fn mango() -> u8 {
unsafe {
*EXOCHORDA
}
}
// CHECK-LABEL: orange:
// x64: mov{{l|absq}} $PIERIS, %{{[a-z0-9]+}}
// A64: adrp [[REG2:[a-z0-9]+]], PIERIS
// A64-NEXT: add {{[a-z0-9]+}}, [[REG2]], :lo12:PIERIS
#[no_mangle]
pub fn orange() -> &'static u8 {
&PIERIS
}