Skip to content

Commit

Permalink
Update linker commands
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte committed Jan 28, 2025
1 parent 203227a commit 6b37bfc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,15 @@ fn add_link_script(cmd: &mut dyn Linker, sess: &Session, tmpdir: &Path, crate_ty
let file_name = ["rustc", &sess.target.llvm_target, "linkfile.ld"].join("-");

let path = tmpdir.join(file_name);

let script = if sess.target.arch == "sbf" &&
sess.opts.cg.target_cpu.as_ref()
.unwrap_or(&sess.target.cpu.as_ref().to_string()) == "v3" {
&sess.target.sbf_v3_linker_script.as_ref().unwrap()
} else {
script
};

if let Err(error) = fs::write(&path, script.as_ref()) {
sess.dcx().emit_fatal(errors::LinkScriptWriteFailure { path, error });
}
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,12 @@ impl<'a> GccLinker<'a> {
} else {
self.linker_arg("--entry=entrypoint");
}
if self.sess.opts.cg.target_cpu.as_ref().unwrap_or(&self.sess.target.cpu.as_ref().to_string()) == "sbfv2"
{
self.linker_arg("--section-start=.text=0x100000000");
self.linker_arg("--pack-dyn-relocs=relr");
if self.sess.opts.cg.target_cpu.as_ref()
.unwrap_or(&self.sess.target.cpu.as_ref().to_string()) == "v3" {
self.linker_arg("-Bsymbolic");
if self.sess.opts.debuginfo == DebugInfo::None {
self.linker_arg("--strip-all");
}
}
}
}
Expand Down
61 changes: 57 additions & 4 deletions compiler/rustc_target/src/spec/base/sbf_base.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::abi::Endian;
use crate::spec::{Cc, cvs, LinkerFlavor, Lld, PanicStrategy, TargetOptions};

pub fn opts() -> TargetOptions {
let linker_script = r"
const V0_LINKER_SCRIPT: &str = r"
PHDRS
{
text PT_LOAD ;
Expand All @@ -28,9 +27,62 @@ SECTIONS
}
}
";

const V3_LINKER_SCRIPT: &str = r"
SECTIONS
{
.text 0x000000000 : {
*(.text*)
} :text
.rodata 0x100000000 : {
*(.rodata*)
*(.data.rel.ro*)
BYTE(0);
. = ALIGN(8);
} :rodata
.bss.stack 0x200000000 (NOLOAD) : {
_stack_start = .;
. = . + 0x1000;
_stack_end = .;
. = ALIGN(8);
} :stack
.bss.heap 0x300000000 (NOLOAD) : {
_heap_start = .;
. = . + 0x1000;
_heap_end = .;
. = ALIGN(8);
} :heap
.dynsym 0xFFFFFFFF00000000 : {
*(.dynsym)
. = ALIGN(8);
} :dynsym
.strtab : { *(.strtab) } :other
.dynstr : { *(.dynstr) } :other
/DISCARD/ : {
*(.comment*)
*(.eh_frame*)
*(*hash*)
*(.bss*)
*(.data*)
*(.rel.dyn*)
*(.dynamic)
}
}
PHDRS
{
text PT_LOAD FLAGS(1);
rodata PT_LOAD FLAGS(4);
stack PT_GNU_STACK FLAGS(6);
heap PT_LOAD FLAGS(6);
dynsym PT_NULL FLAGS(0);
other PT_NULL FLAGS(0);
}
";

pub fn opts() -> TargetOptions {
let pre_link_args = TargetOptions::link_args(
LinkerFlavor::Gnu(Cc::No, Lld::No),
&["--threads=1", "-z", "notext"],
&["--threads=1", "-z", "notext", "-Bdynamic"],
);

TargetOptions {
Expand All @@ -45,7 +97,7 @@ SECTIONS
env: "".into(),
executables: true,
families: cvs!["solana"],
link_script: Some(linker_script.into()),
link_script: Some(V0_LINKER_SCRIPT.into()),
linker: Some("rust-lld".into()),
linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes),
main_needs_argc_argv: false,
Expand All @@ -60,6 +112,7 @@ SECTIONS
singlethread: true,
vendor: "solana".into(),
c_enum_min_bits: Some(32),
sbf_v3_link_script: Some(V3_LINKER_SCRIPT.into()),
.. Default::default()
}
}
6 changes: 6 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,9 @@ pub struct TargetOptions {

/// Whether the target supports XRay instrumentation.
pub supports_xray: bool,

/// SBFv3 linker script
pub sbf_v3_link_script: Option<StaticCow<str>>,
}

/// Add arguments for the given flavor and also for its "twin" flavors
Expand Down Expand Up @@ -2524,6 +2527,7 @@ impl Default for TargetOptions {
entry_name: "main".into(),
entry_abi: Conv::C,
supports_xray: false,
sbf_v3_link_script: None,
}
}
}
Expand Down Expand Up @@ -3255,6 +3259,7 @@ impl Target {
key!(entry_name);
key!(entry_abi, Conv)?;
key!(supports_xray, bool);
key!(sbf_v3_link_script, optional);

if base.is_builtin {
// This can cause unfortunate ICEs later down the line.
Expand Down Expand Up @@ -3512,6 +3517,7 @@ impl ToJson for Target {
target_option_val!(entry_name);
target_option_val!(entry_abi);
target_option_val!(supports_xray);
target_option_val!(sbf_v3_link_script);

// Serializing `-Clink-self-contained` needs a dynamic key to support the
// backwards-compatible variants.
Expand Down

0 comments on commit 6b37bfc

Please sign in to comment.