rust/compiler
bors be8450eec8 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
```
2021-05-19 07:25:17 +00:00
..
rustc use jemallocator in rustc/rustdoc 2021-04-04 14:10:26 -07:00
rustc_apfloat Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_arena Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_ast Implement Anonymous{Struct, Union} in the AST 2021-05-16 09:49:16 -05:00
rustc_ast_lowering Auto merge of #84767 - scottmcm:try_trait_actual, r=lcnr 2021-05-18 20:50:01 +00:00
rustc_ast_passes Stabilize extended_key_value_attributes 2021-05-18 01:01:36 -04:00
rustc_ast_pretty Implement Anonymous{Struct, Union} in the AST 2021-05-16 09:49:16 -05:00
rustc_attr Allow specifying alignment for functions 2021-04-05 17:36:51 -04:00
rustc_builtin_macros Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
rustc_codegen_cranelift Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_codegen_llvm Auto merge of #85276 - Bobo1239:more_dso_local, r=nagisa 2021-05-19 07:25:17 +00:00
rustc_codegen_ssa Rollup merge of #85274 - luqmana:linker-is-gnu-gc-sections, r=petrochenkov 2021-05-18 22:36:04 -04:00
rustc_data_structures Only compute Obligation cache_key once in register_obligation_at 2021-05-04 11:57:53 -07:00
rustc_driver Use () for analysis. 2021-05-12 14:01:16 +02:00
rustc_error_codes swap function order for better read flow 2021-05-13 13:22:24 +02:00
rustc_errors Stabilize extended_key_value_attributes 2021-05-18 01:01:36 -04:00
rustc_expand Auto merge of #83813 - cbeuw:remap-std, r=michaelwoerister 2021-05-12 11:05:56 +00:00
rustc_feature Rollup merge of #83366 - jyn514:stabilize-key-value-attrs, r=petrochenkov 2021-05-18 22:35:54 -04:00
rustc_fs_util Optimize away a fs::metadata call. 2021-01-06 08:33:15 -08:00
rustc_graphviz Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_hir Rollup merge of #83366 - jyn514:stabilize-key-value-attrs, r=petrochenkov 2021-05-18 22:35:54 -04:00
rustc_hir_pretty Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
rustc_incremental Use () for codegen queries. 2021-05-12 13:58:46 +02:00
rustc_index Change bitwise operator to more easily keep data in vector registers 2021-05-04 11:55:18 -04:00
rustc_infer Add comments to opportunistic resolve functions 2021-05-15 23:36:10 -04:00
rustc_interface Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_lexer Fix outdated crate names in compiler docs 2021-04-08 11:12:14 -05:00
rustc_lint Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_lint_defs Add additional migrations to handle auto-traits and clone traits 2021-05-06 14:17:59 -04:00
rustc_llvm Auto merge of #85190 - mati865:update-cc, r=Mark-Simulacrum 2021-05-14 04:12:40 +00:00
rustc_macros Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_metadata Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_middle Auto merge of #85437 - GuillaumeGomez:rollup-3jcirty, r=GuillaumeGomez 2021-05-18 14:48:53 +00:00
rustc_mir Address review comments 2021-05-17 21:31:05 -04:00
rustc_mir_build Remove remnants of BorrowOfPackedField 2021-05-17 19:32:58 +02:00
rustc_parse Stabilize extended_key_value_attributes 2021-05-18 01:01:36 -04:00
rustc_parse_format Rollup merge of #83348 - osa1:issue83344, r=jackh726 2021-03-28 01:33:13 +09:00
rustc_passes Rollup merge of #85339 - FabianWolff:issue-83893, r=varkor 2021-05-18 14:08:51 +02:00
rustc_plugin_impl Use () for plugin_registrar_fn. 2021-05-12 13:58:43 +02:00
rustc_privacy Use () for privacy. 2021-05-12 13:58:45 +02:00
rustc_query_impl Use () in dependency_formats. 2021-05-12 13:58:41 +02:00
rustc_query_system Show nicer error when an 'unstable fingerprints' error occurs 2021-05-10 17:43:51 -04:00
rustc_resolve Add support for const operands and options to global_asm! 2021-05-13 22:31:57 +01:00
rustc_save_analysis Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
rustc_serialize Auto merge of #83465 - michaelwoerister:safe-read_raw_bytes, r=cjgillot 2021-03-26 01:28:59 +00:00
rustc_session Auto merge of #83129 - LeSeulArtichaut:thir-unsafeck, r=nikomatsakis 2021-05-13 10:49:29 +00:00
rustc_span Auto merge of #84767 - scottmcm:try_trait_actual, r=lcnr 2021-05-18 20:50:01 +00:00
rustc_symbol_mangling Rollup merge of #83767 - camelid:mangle-v0-fix, r=nikomatsakis 2021-05-18 22:35:55 -04:00
rustc_target Rollup merge of #85274 - luqmana:linker-is-gnu-gc-sections, r=petrochenkov 2021-05-18 22:36:04 -04:00
rustc_trait_selection Rollup merge of #85369 - FabianWolff:issue-84973, r=jackh726 2021-05-18 14:08:55 +02:00
rustc_traits Auto merge of #83207 - oli-obk:valtree2, r=lcnr 2021-04-02 10:28:12 +00:00
rustc_ty_utils add new attribute rustc_insignificant_dtor and a query to check if a type has a significant drop 2021-05-14 22:57:33 -04:00
rustc_type_ir Make TypeFoldable::is_global() false when fresh tys/consts are present 2021-05-01 16:58:33 -04:00
rustc_typeck Rollup merge of #83767 - camelid:mangle-v0-fix, r=nikomatsakis 2021-05-18 22:35:55 -04:00