Skip to content

Commit 660d993

Browse files
committed
adapt L4Bender implementation
- Fix style errors. - L4-bender does not yet support dynamic linking. - Stack unwinding is not yet supported for x86_64-unknown-l4re-uclibc. For now, just abort on panics. - Use GNU-style linker options where possible. As suggested by review: - Use standard GNU-style ld syntax for relro flags. - Use standard GNU-style optimization flags and logic. - Use standard GNU-style ld syntax for --subsystem. - Don't read environment variables in L4Bender linker. Thanks to CARGO_ENCODED_RUSTFLAGS introduced in rust-lang#9601, l4-bender's arguments can now be passed from the L4Re build system without resorting to custom parsing of environment variables.
1 parent d984287 commit 660d993

File tree

4 files changed

+74
-89
lines changed

4 files changed

+74
-89
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 62 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::command::Command;
33
use super::symbol_export;
44
use rustc_span::symbol::sym;
55

6-
use std::env;
76
use std::ffi::{OsStr, OsString};
87
use std::fs::{self, File};
98
use std::io::prelude::*;
@@ -150,9 +149,7 @@ pub fn get_linker<'a>(
150149

151150
LinkerFlavor::BpfLinker => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
152151

153-
LinkerFlavor::L4Bender => {
154-
Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>
155-
},
152+
LinkerFlavor::L4Bender => Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>,
156153
}
157154
}
158155

@@ -1367,10 +1364,10 @@ pub struct L4Bender<'a> {
13671364
}
13681365

13691366
impl<'a> Linker for L4Bender<'a> {
1370-
fn link_dylib(&mut self, _lib: Symbol) {
1371-
panic!("dylibs not supported yet")
1367+
fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {
1368+
bug!("dylibs are not supported on L4Re");
13721369
}
1373-
fn link_staticlib(&mut self, lib: Symbol) {
1370+
fn link_staticlib(&mut self, lib: Symbol, _verbatim: bool) {
13741371
self.hint_static();
13751372
self.cmd.arg(format!("-PC{}", lib));
13761373
}
@@ -1382,36 +1379,44 @@ impl<'a> Linker for L4Bender<'a> {
13821379
self.cmd.arg("-L").arg(path);
13831380
}
13841381
fn framework_path(&mut self, _: &Path) {
1385-
bug!("Frameworks are not supported on L4Re!");
1386-
}
1387-
fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
1388-
fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
1389-
// not sure about pie on L4Re
1390-
fn position_independent_executable(&mut self) { }
1391-
fn no_position_independent_executable(&mut self) { }
1392-
fn full_relro(&mut self) { self.cmd.arg("-z,relro,-z,now"); }
1393-
fn partial_relro(&mut self) { self.cmd.arg("-z,relro"); }
1394-
fn no_relro(&mut self) { self.cmd.arg("-z,norelro"); }
1395-
fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
1382+
bug!("frameworks are not supported on L4Re");
1383+
}
1384+
fn output_filename(&mut self, path: &Path) {
1385+
self.cmd.arg("-o").arg(path);
1386+
}
1387+
1388+
fn add_object(&mut self, path: &Path) {
1389+
self.cmd.arg(path);
1390+
}
1391+
1392+
fn full_relro(&mut self) {
1393+
self.cmd.arg("-zrelro");
1394+
self.cmd.arg("-znow");
1395+
}
1396+
1397+
fn partial_relro(&mut self) {
1398+
self.cmd.arg("-zrelro");
1399+
}
1400+
1401+
fn no_relro(&mut self) {
1402+
self.cmd.arg("-znorelro");
1403+
}
1404+
13961405
fn cmd(&mut self) -> &mut Command {
13971406
&mut self.cmd
13981407
}
13991408

1409+
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1410+
14001411
fn link_rust_dylib(&mut self, _: Symbol, _: &Path) {
14011412
panic!("Rust dylibs not supported");
14021413
}
14031414

1404-
fn link_framework(&mut self, _: Symbol) {
1405-
bug!("Frameworks not supported on L4Re.");
1415+
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
1416+
bug!("frameworks not supported on L4Re");
14061417
}
14071418

1408-
// Here we explicitly ask that the entire archive is included into the
1409-
// result artifact. For more details see #15460, but the gist is that
1410-
// the linker will strip away any unused objects in the archive if we
1411-
// don't otherwise explicitly reference them. This can occur for
1412-
// libraries which are just providing bindings, libraries with generic
1413-
// functions, etc.
1414-
fn link_whole_staticlib(&mut self, lib: Symbol, _: &[PathBuf]) {
1419+
fn link_whole_staticlib(&mut self, lib: Symbol, _verbatim: bool, _search_path: &[PathBuf]) {
14151420
self.hint_static();
14161421
self.cmd.arg("--whole-archive").arg(format!("-l{}", lib));
14171422
self.cmd.arg("--no-whole-archive");
@@ -1428,17 +1433,28 @@ impl<'a> Linker for L4Bender<'a> {
14281433
}
14291434
}
14301435

1436+
fn no_gc_sections(&mut self) {
1437+
self.cmd.arg("--no-gc-sections");
1438+
}
1439+
14311440
fn optimize(&mut self) {
1432-
self.cmd.arg("-O2");
1441+
// GNU-style linkers support optimization with -O. GNU ld doesn't
1442+
// need a numeric argument, but other linkers do.
1443+
if self.sess.opts.optimize == config::OptLevel::Default
1444+
|| self.sess.opts.optimize == config::OptLevel::Aggressive
1445+
{
1446+
self.cmd.arg("-O1");
1447+
}
14331448
}
14341449

1435-
fn pgo_gen(&mut self) { }
1450+
fn pgo_gen(&mut self) {}
14361451

14371452
fn debuginfo(&mut self, strip: Strip) {
14381453
match strip {
14391454
Strip::None => {}
14401455
Strip::Debuginfo => {
1441-
self.cmd().arg("--strip-debug"); }
1456+
self.cmd().arg("--strip-debug");
1457+
}
14421458
Strip::Symbols => {
14431459
self.cmd().arg("--strip-all");
14441460
}
@@ -1449,72 +1465,38 @@ impl<'a> Linker for L4Bender<'a> {
14491465
self.cmd.arg("-nostdlib");
14501466
}
14511467

1452-
fn build_dylib(&mut self, _: &Path) {
1453-
bug!("not implemented");
1454-
}
1455-
1456-
fn export_symbols(&mut self, _: &Path, _: CrateType) {
1468+
fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[String]) {
14571469
// ToDo, not implemented, copy from GCC
1470+
self.sess.warn("exporting symbols not implemented yet for L4Bender");
14581471
return;
14591472
}
14601473

14611474
fn subsystem(&mut self, subsystem: &str) {
1462-
self.cmd.arg(&format!("--subsystem,{}", subsystem));
1475+
self.cmd.arg(&format!("--subsystem {}", subsystem));
14631476
}
14641477

1465-
fn finalize(&mut self) {
1478+
fn reset_per_library_state(&mut self) {
14661479
self.hint_static(); // Reset to default before returning the composed command line.
14671480
}
14681481

1469-
fn group_start(&mut self) { self.cmd.arg("--start-group"); }
1470-
fn group_end(&mut self) { self.cmd.arg("--end-group"); }
1471-
fn linker_plugin_lto(&mut self) {
1472-
// do nothing
1473-
}
1474-
fn control_flow_guard(&mut self) {
1475-
self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
1482+
fn group_start(&mut self) {
1483+
self.cmd.arg("--start-group");
14761484
}
14771485

1478-
fn no_crt_objects(&mut self) { }
1479-
}
1486+
fn group_end(&mut self) {
1487+
self.cmd.arg("--end-group");
1488+
}
14801489

1481-
impl<'a> L4Bender<'a> {
1482-
pub fn new(mut cmd: Command, sess: &'a Session) -> L4Bender<'a> {
1483-
if let Ok(l4bender_args) = env::var("L4_BENDER_ARGS") {
1484-
L4Bender::split_cmd_args(&mut cmd, &l4bender_args);
1485-
}
1490+
fn linker_plugin_lto(&mut self) {}
14861491

1487-
cmd.arg("--"); // separate direct l4-bender args from linker args
1492+
fn control_flow_guard(&mut self) {}
14881493

1489-
L4Bender {
1490-
cmd: cmd,
1491-
sess: sess,
1492-
hinted_static: false,
1493-
}
1494-
}
1494+
fn no_crt_objects(&mut self) {}
1495+
}
14951496

1496-
/// This parses a shell-escaped string and unquotes the arguments. It doesn't attempt to
1497-
/// completely understand shell, but should instead allow passing arguments like
1498-
/// `-Dlinker="ld -m x86_64"`, and a copy without quotes, but spaces preserved, is added as an
1499-
/// argument to the given Command. This means that constructs as \" are not understood, so
1500-
/// quote wisely.
1501-
fn split_cmd_args(cmd: &mut Command, shell_args: &str) {
1502-
let mut arg = String::new();
1503-
let mut quoted = false;
1504-
for character in shell_args.chars() {
1505-
match character {
1506-
' ' if !quoted => {
1507-
cmd.arg(&arg);
1508-
arg.clear();
1509-
},
1510-
'"' | '\'' => quoted = !quoted,
1511-
_ => arg.push(character),
1512-
};
1513-
}
1514-
if arg.len() > 0 {
1515-
cmd.arg(&arg);
1516-
arg.clear();
1517-
}
1497+
impl<'a> L4Bender<'a> {
1498+
pub fn new(cmd: Command, sess: &'a Session) -> L4Bender<'a> {
1499+
L4Bender { cmd: cmd, sess: sess, hinted_static: false }
15181500
}
15191501

15201502
fn hint_static(&mut self) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, PanicStrategy, TargetOptions};
1+
use crate::spec::{LinkerFlavor, PanicStrategy, TargetOptions};
22
use std::default::Default;
33

44
pub fn opts() -> TargetOptions {
55
TargetOptions {
66
os: "l4re".to_string(),
77
env: "uclibc".to_string(),
8-
linker_flavor: LinkerFlavor::Ld,
8+
linker_flavor: LinkerFlavor::L4Bender,
99
executables: true,
1010
panic_strategy: PanicStrategy::Abort,
1111
linker: Some("l4-bender".to_string()),
12-
pre_link_args: args,
13-
os_family: Some("unix".to_string()),
12+
linker_is_gnu: false,
13+
families: vec!["unix".to_string()],
1414
..Default::default()
1515
}
1616
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
use crate::spec::Target;
1+
use crate::spec::{PanicStrategy, Target};
22

33
pub fn target() -> Target {
44
let mut base = super::l4re_base::opts();
55
base.cpu = "x86-64".to_string();
66
base.max_atomic_width = Some(64);
7+
base.crt_static_allows_dylibs = false;
8+
base.dynamic_linking = false;
9+
base.panic_strategy = PanicStrategy::Abort;
710

811
Target {
912
llvm_target: "x86_64-unknown-l4re-uclibc".to_string(),
1013
pointer_width: 64,
1114
data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
1215
.to_string(),
1316
arch: "x86_64".to_string(),
14-
target_os: "l4re".to_string(),
15-
target_env: "uclibc".to_string(),
16-
target_vendor: "unknown".to_string(),
17-
linker_flavor: LinkerFlavor::L4Bender,
1817
options: base,
1918
}
2019
}

library/panic_unwind/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ cfg_if::cfg_if! {
3939
} else if #[cfg(target_os = "hermit")] {
4040
#[path = "hermit.rs"]
4141
mod real_imp;
42+
} else if #[cfg(target_os = "l4re")] {
43+
// L4Re is unix family but does not yet support unwinding.
44+
#[path = "dummy.rs"]
45+
mod real_imp;
4246
} else if #[cfg(target_env = "msvc")] {
4347
#[path = "seh.rs"]
4448
mod real_imp;

0 commit comments

Comments
 (0)