Skip to content

Commit 01b6be0

Browse files
authored
Rollup merge of rust-lang#66957 - parthsane:pvs/ftx_lld_linker, r=alexcrichton
Change Linker for x86_64-fortanix-unknown-sgx target to rust-lld Changed linker for `x86_64-fortanix-unknown-sgx` target to `rust-lld` This change needed the RelaxELFRelocations flag to be set for it to work correctly r? @jethrogb
2 parents 08e7fac + 54b2060 commit 01b6be0

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

src/librustc_codegen_llvm/back/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_fea
167167
let emit_stack_size_section = sess.opts.debugging_opts.emit_stack_sizes;
168168

169169
let asm_comments = sess.asm_comments();
170-
170+
let relax_elf_relocations = sess.target.target.options.relax_elf_relocations;
171171
Arc::new(move || {
172172
let tm = unsafe {
173173
llvm::LLVMRustCreateTargetMachine(
@@ -183,6 +183,7 @@ pub fn target_machine_factory(sess: &Session, optlvl: config::OptLevel, find_fea
183183
singlethread,
184184
asm_comments,
185185
emit_stack_size_section,
186+
relax_elf_relocations,
186187
)
187188
};
188189

src/librustc_codegen_llvm/llvm/ffi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,8 @@ extern "C" {
17021702
TrapUnreachable: bool,
17031703
Singlethread: bool,
17041704
AsmComments: bool,
1705-
EmitStackSizeSection: bool)
1705+
EmitStackSizeSection: bool,
1706+
RelaxELFRelocations: bool)
17061707
-> Option<&'static mut TargetMachine>;
17071708
pub fn LLVMRustDisposeTargetMachine(T: &'static mut TargetMachine);
17081709
pub fn LLVMRustAddBuilderLibraryInfo(PMB: &'a PassManagerBuilder,

src/librustc_codegen_ssa/back/linker.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ impl<'a> Linker for GccLinker<'a> {
398398

399399
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
400400
// Symbol visibility in object files typically takes care of this.
401-
if crate_type == CrateType::Executable {
401+
if crate_type == CrateType::Executable &&
402+
self.sess.target.target.options.override_export_symbols.is_none() {
402403
return;
403404
}
404405

src/librustc_target/spec/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ pub struct TargetOptions {
803803

804804
/// LLVM ABI name, corresponds to the '-mabi' parameter available in multilib C compilers
805805
pub llvm_abiname: String,
806+
807+
/// Whether or not RelaxElfRelocation flag will be passed to the linker
808+
pub relax_elf_relocations: bool,
806809
}
807810

808811
impl Default for TargetOptions {
@@ -890,6 +893,7 @@ impl Default for TargetOptions {
890893
merge_functions: MergeFunctions::Aliases,
891894
target_mcount: "mcount".to_string(),
892895
llvm_abiname: "".to_string(),
896+
relax_elf_relocations: false,
893897
}
894898
}
895899
}
@@ -1207,6 +1211,7 @@ impl Target {
12071211
key!(merge_functions, MergeFunctions)?;
12081212
key!(target_mcount);
12091213
key!(llvm_abiname);
1214+
key!(relax_elf_relocations, bool);
12101215

12111216
if let Some(array) = obj.find("abi-blacklist").and_then(Json::as_array) {
12121217
for name in array.iter().filter_map(|abi| abi.as_string()) {
@@ -1426,6 +1431,7 @@ impl ToJson for Target {
14261431
target_option_val!(merge_functions);
14271432
target_option_val!(target_mcount);
14281433
target_option_val!(llvm_abiname);
1434+
target_option_val!(relax_elf_relocations);
14291435

14301436
if default.abi_blacklist != self.options.abi_blacklist {
14311437
d.insert("abi-blacklist".to_string(), self.options.abi_blacklist.iter()
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
use std::iter;
22

3-
use super::{LinkerFlavor, PanicStrategy, Target, TargetOptions};
3+
use super::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions};
44

55
pub fn target() -> Result<Target, String> {
66
const PRE_LINK_ARGS: &[&str] = &[
7-
"-Wl,--as-needed",
8-
"-Wl,-z,noexecstack",
9-
"-m64",
10-
"-fuse-ld=gold",
11-
"-nostdlib",
12-
"-shared",
13-
"-Wl,-e,sgx_entry",
14-
"-Wl,-Bstatic",
15-
"-Wl,--gc-sections",
16-
"-Wl,-z,text",
17-
"-Wl,-z,norelro",
18-
"-Wl,--rosegment",
19-
"-Wl,--no-undefined",
20-
"-Wl,--error-unresolved-symbols",
21-
"-Wl,--no-undefined-version",
22-
"-Wl,-Bsymbolic",
23-
"-Wl,--export-dynamic",
7+
"--as-needed",
8+
"--eh-frame-hdr",
9+
"-z" , "noexecstack",
10+
"-e","sgx_entry",
11+
"-Bstatic",
12+
"--gc-sections",
13+
"-z","text",
14+
"-z","norelro",
15+
"--no-undefined",
16+
"--error-unresolved-symbols",
17+
"--no-undefined-version",
18+
"-Bsymbolic",
19+
"--export-dynamic",
2420
// The following symbols are needed by libunwind, which is linked after
2521
// libstd. Make sure they're included in the link.
26-
"-Wl,-u,__rust_abort",
27-
"-Wl,-u,__rust_c_alloc",
28-
"-Wl,-u,__rust_c_dealloc",
29-
"-Wl,-u,__rust_print_err",
30-
"-Wl,-u,__rust_rwlock_rdlock",
31-
"-Wl,-u,__rust_rwlock_unlock",
32-
"-Wl,-u,__rust_rwlock_wrlock",
22+
"-u","__rust_abort",
23+
"-u","__rust_c_alloc",
24+
"-u","__rust_c_dealloc",
25+
"-u","__rust_print_err",
26+
"-u","__rust_rwlock_rdlock",
27+
"-u","__rust_rwlock_unlock",
28+
"-u","__rust_rwlock_wrlock"
3329
];
3430

3531
const EXPORT_SYMBOLS: &[&str] = &[
@@ -50,18 +46,20 @@ pub fn target() -> Result<Target, String> {
5046
dynamic_linking: false,
5147
executables: true,
5248
linker_is_gnu: true,
49+
linker: Some("rust-lld".to_owned()),
5350
max_atomic_width: Some(64),
5451
panic_strategy: PanicStrategy::Unwind,
5552
cpu: "x86-64".into(),
5653
features: "+rdrnd,+rdseed".into(),
5754
position_independent_executables: true,
5855
pre_link_args: iter::once((
59-
LinkerFlavor::Gcc,
56+
LinkerFlavor::Lld(LldFlavor::Ld),
6057
PRE_LINK_ARGS.iter().cloned().map(String::from).collect(),
6158
))
6259
.collect(),
6360
post_link_objects: vec!["libunwind.a".into()],
6461
override_export_symbols: Some(EXPORT_SYMBOLS.iter().cloned().map(String::from).collect()),
62+
relax_elf_relocations: true,
6563
..Default::default()
6664
};
6765
Ok(Target {
@@ -74,7 +72,7 @@ pub fn target() -> Result<Target, String> {
7472
target_vendor: "fortanix".into(),
7573
data_layout: "e-m:e-i64:64-f80:128-n8:16:32:64-S128".into(),
7674
arch: "x86_64".into(),
77-
linker_flavor: LinkerFlavor::Gcc,
75+
linker_flavor: LinkerFlavor::Lld(LldFlavor::Ld),
7876
options: opts,
7977
})
8078
}

src/rustllvm/PassWrapper.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
393393
bool TrapUnreachable,
394394
bool Singlethread,
395395
bool AsmComments,
396-
bool EmitStackSizeSection) {
396+
bool EmitStackSizeSection,
397+
bool RelaxELFRelocations) {
397398

398399
auto OptLevel = fromRust(RustOptLevel);
399400
auto RM = fromRust(RustReloc);
@@ -418,6 +419,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
418419
Options.MCOptions.AsmVerbose = AsmComments;
419420
Options.MCOptions.PreserveAsmComments = AsmComments;
420421
Options.MCOptions.ABIName = ABIStr;
422+
Options.RelaxELFRelocations = RelaxELFRelocations;
421423

422424
if (TrapUnreachable) {
423425
// Tell LLVM to codegen `unreachable` into an explicit trap instruction.

0 commit comments

Comments
 (0)