Skip to content

Commit 0527ef1

Browse files
humendaatopia
authored andcommitted
Add L4Bender as linker variant
1 parent 38bc9b9 commit 0527ef1

File tree

3 files changed

+135
-14
lines changed

3 files changed

+135
-14
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ impl LinkerInfo {
7777
as Box<dyn Linker>
7878
}
7979

80+
LinkerFlavor::L4Bender => {
81+
Box::new(L4Bender {
82+
cmd,
83+
sess,
84+
hinted_static: false,
85+
}) as Box<Linker>
86+
},
8087
LinkerFlavor::Lld(LldFlavor::Wasm) => {
8188
Box::new(WasmLd::new(cmd, sess, self)) as Box<dyn Linker>
8289
}
@@ -1284,6 +1291,127 @@ impl<'a> Linker for WasmLd<'a> {
12841291
}
12851292
}
12861293

1294+
/// Linker shepherd script for L4Re (Fiasco)
1295+
pub struct L4Bender<'a> {
1296+
cmd: Command,
1297+
sess: &'a Session,
1298+
hinted_static: bool,
1299+
}
1300+
1301+
impl<'a> Linker for L4Bender<'a> {
1302+
fn link_dylib(&mut self, lib: &str) {
1303+
self.link_staticlib(lib); // do not support dynamic linking for now
1304+
}
1305+
fn link_staticlib(&mut self, lib: &str) {
1306+
self.hint_static();
1307+
self.cmd.arg(format!("-PC{}", lib));
1308+
}
1309+
fn link_rlib(&mut self, lib: &Path) {
1310+
self.hint_static();
1311+
self.cmd.arg(lib);
1312+
}
1313+
fn include_path(&mut self, path: &Path) {
1314+
self.cmd.arg("-L").arg(path);
1315+
}
1316+
fn framework_path(&mut self, _: &Path) {
1317+
bug!("Frameworks are not supported on L4Re!");
1318+
}
1319+
fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
1320+
fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
1321+
// not sure about pie on L4Re
1322+
fn position_independent_executable(&mut self) { }
1323+
fn no_position_independent_executable(&mut self) { }
1324+
fn full_relro(&mut self) { self.cmd.arg("-z,relro,-z,now"); }
1325+
fn partial_relro(&mut self) { self.cmd.arg("-z,relro"); }
1326+
fn no_relro(&mut self) { self.cmd.arg("-z,norelro"); }
1327+
fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
1328+
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
1329+
1330+
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) { self.link_dylib(lib); }
1331+
1332+
fn link_framework(&mut self, _: &str) {
1333+
bug!("Frameworks not supported on L4Re.");
1334+
}
1335+
1336+
// Here we explicitly ask that the entire archive is included into the
1337+
// result artifact. For more details see #15460, but the gist is that
1338+
// the linker will strip away any unused objects in the archive if we
1339+
// don't otherwise explicitly reference them. This can occur for
1340+
// libraries which are just providing bindings, libraries with generic
1341+
// functions, etc.
1342+
fn link_whole_staticlib(&mut self, lib: &str, _: &[PathBuf]) {
1343+
self.hint_static();
1344+
self.cmd.arg("--whole-archive");
1345+
self.cmd.arg("-l").arg(lib);
1346+
self.cmd.arg("--no-whole-archive");
1347+
}
1348+
1349+
fn link_whole_rlib(&mut self, lib: &Path) {
1350+
self.hint_static();
1351+
self.cmd.arg("--whole-archive").arg(lib).arg("--no-whole-archive");
1352+
}
1353+
1354+
fn gc_sections(&mut self, keep_metadata: bool) {
1355+
if !keep_metadata {
1356+
self.cmd.arg("--gc-sections");
1357+
}
1358+
}
1359+
1360+
fn optimize(&mut self) {
1361+
self.cmd.arg("-O2");
1362+
}
1363+
1364+
fn pgo_gen(&mut self) { }
1365+
1366+
fn debuginfo(&mut self) {
1367+
// for documentation, see GccLinker.debuginfo()
1368+
match self.sess.opts.debuginfo {
1369+
DebugInfoLevel::NoDebugInfo => {
1370+
match self.sess.opts.debugging_opts.strip_debuginfo_if_disabled {
1371+
Some(true) => { self.cmd.arg("-S"); },
1372+
_ => {},
1373+
}
1374+
},
1375+
_ => {},
1376+
};
1377+
}
1378+
1379+
fn no_default_libraries(&mut self) {
1380+
self.cmd.arg("-nostdlib");
1381+
}
1382+
1383+
fn build_dylib(&mut self, _: &Path) {
1384+
bug!("not implemented");
1385+
}
1386+
1387+
fn export_symbols(&mut self, _: &Path, _: CrateType) {
1388+
bug!("Not implemented");
1389+
}
1390+
1391+
fn subsystem(&mut self, subsystem: &str) {
1392+
self.cmd.arg(&format!("--subsystem,{}", subsystem));
1393+
}
1394+
1395+
fn finalize(&mut self) -> Command {
1396+
self.hint_static(); // Reset to default before returning the composed command line.
1397+
let mut cmd = Command::new("");
1398+
::std::mem::swap(&mut cmd, &mut self.cmd);
1399+
cmd
1400+
}
1401+
1402+
fn group_start(&mut self) { self.cmd.arg("--start-group"); }
1403+
fn group_end(&mut self) { self.cmd.arg("--end-group"); }
1404+
}
1405+
1406+
impl<'a> L4Bender<'a> {
1407+
fn hint_static(&mut self) {
1408+
if !self.hinted_static {
1409+
self.cmd.arg("-static");
1410+
self.hinted_static = true;
1411+
}
1412+
}
1413+
}
1414+
12871415
fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
12881416
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
12891417
return exports.clone();
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
use crate::spec::{LinkerFlavor, PanicStrategy, TargetOptions};
22
//use std::process::Command;
33

4-
// Use GCC to locate code for crt* libraries from the host, not from L4Re. Note
5-
// that a few files also come from L4Re, for these, the function shouldn't be
6-
// used. This uses GCC for the location of the file, but GCC is required for L4Re anyway.
7-
//fn get_path_or(filename: &str) -> String {
8-
// let child = Command::new("gcc")
9-
// .arg(format!("-print-file-name={}", filename)).output()
10-
// .expect("Failed to execute GCC");
11-
// String::from_utf8(child.stdout)
12-
// .expect("Couldn't read path from GCC").trim().into()
13-
//}
14-
154
pub fn opts() -> TargetOptions {
165
TargetOptions {
176
os: "l4re".to_string(),
187
env: "uclibc".to_string(),
198
linker_flavor: LinkerFlavor::Ld,
209
executables: true,
2110
panic_strategy: PanicStrategy::Abort,
22-
linker: Some("ld".to_string()),
23-
linker_is_gnu: false,
24-
families: vec!["unix".to_string()],
11+
linker: Some("l4-bender".to_string()),
12+
pre_link_args: args,
13+
os_family: Some("unix".to_string()),
2514
..Default::default()
2615
}
2716
}

compiler/rustc_target/src/spec/x86_64_unknown_l4re_uclibc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ pub fn target() -> Target {
1111
data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
1212
.to_string(),
1313
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,
1418
options: base,
1519
}
1620
}

0 commit comments

Comments
 (0)