Add BPF target

This change adds the bpfel-unknown-none and bpfeb-unknown-none targets
which can be used to generate little endian and big endian BPF
This commit is contained in:
Alessandro Decina 2020-11-30 19:41:57 +00:00
parent 92418ce65a
commit 12e70929d6
18 changed files with 250 additions and 3 deletions

View file

@ -67,6 +67,7 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
LinkerFlavor::Msvc => "link.exe",
LinkerFlavor::Lld(_) => "lld",
LinkerFlavor::PtxLinker => "rust-ptx-linker",
LinkerFlavor::BpfLinker => "bpf-linker",
}),
flavor,
)),

View file

@ -989,6 +989,7 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
LinkerFlavor::Msvc => "link.exe",
LinkerFlavor::Lld(_) => "lld",
LinkerFlavor::PtxLinker => "rust-ptx-linker",
LinkerFlavor::BpfLinker => "bpf-linker",
}),
flavor,
)),

View file

@ -84,6 +84,7 @@ impl LinkerInfo {
LinkerFlavor::PtxLinker => {
Box::new(PtxLinker { cmd, sess, info: self }) as Box<dyn Linker>
}
LinkerFlavor::BpfLinker => Box::new(BpfLinker { cmd, sess, info: self }) as Box<dyn Linker>
}
}
}
@ -1431,3 +1432,124 @@ impl<'a> Linker for PtxLinker<'a> {
fn linker_plugin_lto(&mut self) {}
}
pub struct BpfLinker<'a> {
cmd: Command,
sess: &'a Session,
info: &'a LinkerInfo,
}
impl<'a> Linker for BpfLinker<'a> {
fn cmd(&mut self) -> &mut Command {
&mut self.cmd
}
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
fn link_rlib(&mut self, path: &Path) {
self.cmd.arg(path);
}
fn link_whole_rlib(&mut self, path: &Path) {
self.cmd.arg(path);
}
fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path);
}
fn debuginfo(&mut self, _strip: Strip) {
self.cmd.arg("--debug");
}
fn add_object(&mut self, path: &Path) {
self.cmd.arg(path);
}
fn optimize(&mut self) {
self.cmd.arg(match self.sess.opts.optimize {
OptLevel::No => "-O0",
OptLevel::Less => "-O1",
OptLevel::Default => "-O2",
OptLevel::Aggressive => "-O3",
OptLevel::Size => "-Os",
OptLevel::SizeMin => "-Oz",
});
}
fn output_filename(&mut self, path: &Path) {
self.cmd.arg("-o").arg(path);
}
fn finalize(&mut self) {
self.cmd.arg("--cpu").arg(match self.sess.opts.cg.target_cpu {
Some(ref s) => s,
None => &self.sess.target.options.cpu,
});
}
fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {
panic!("external dylibs not supported")
}
fn link_rust_dylib(&mut self, _lib: Symbol, _path: &Path) {
panic!("external dylibs not supported")
}
fn link_staticlib(&mut self, _lib: Symbol, _verbatim: bool) {
panic!("staticlibs not supported")
}
fn link_whole_staticlib(&mut self, _lib: Symbol, _verbatim: bool, _search_path: &[PathBuf]) {
panic!("staticlibs not supported")
}
fn framework_path(&mut self, _path: &Path) {
panic!("frameworks not supported")
}
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
panic!("frameworks not supported")
}
fn full_relro(&mut self) {}
fn partial_relro(&mut self) {}
fn no_relro(&mut self) {}
fn gc_sections(&mut self, _keep_metadata: bool) {}
fn no_gc_sections(&mut self) {}
fn pgo_gen(&mut self) {}
fn no_crt_objects(&mut self) {}
fn no_default_libraries(&mut self) {}
fn control_flow_guard(&mut self) {}
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
let path = tmpdir.join("symbols");
let res: io::Result<()> = try {
let mut f = BufWriter::new(File::create(&path)?);
for sym in self.info.exports[&crate_type].iter() {
writeln!(f, "{}", sym)?;
}
};
if let Err(e) = res {
self.sess.fatal(&format!("failed to write symbols file: {}", e));
} else {
self.cmd.arg("--export-symbols").arg(&path);
}
}
fn subsystem(&mut self, _subsystem: &str) {}
fn group_start(&mut self) {}
fn group_end(&mut self) {}
fn linker_plugin_lto(&mut self) {}
}

View file

@ -86,6 +86,7 @@ fn main() {
"nvptx",
"hexagon",
"riscv",
"bpf",
];
let required_components = &[

View file

@ -167,4 +167,12 @@ pub fn initialize_available_targets() {
LLVMInitializeWebAssemblyAsmPrinter,
LLVMInitializeWebAssemblyAsmParser
);
init_target!(
llvm_component = "bpf",
LLVMInitializeBPFTargetInfo,
LLVMInitializeBPFTarget,
LLVMInitializeBPFTargetMC,
LLVMInitializeBPFAsmPrinter,
LLVMInitializeBPFAsmParser
);
}

View file

@ -0,0 +1,31 @@
// see BPFCallingConv.td
use crate::abi::call::{ArgAbi, FnAbi};
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
if ret.layout.is_aggregate() || ret.layout.size.bits() > 64 {
ret.make_indirect();
} else {
ret.extend_integer_width_to(64);
}
}
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
if arg.layout.is_aggregate() || arg.layout.size.bits() > 64 {
arg.make_indirect();
} else {
arg.extend_integer_width_to(64);
}
}
pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
if !fn_abi.ret.is_ignore() {
classify_ret(&mut fn_abi.ret);
}
for arg in &mut fn_abi.args {
if arg.is_ignore() {
continue;
}
classify_arg(arg);
}
}

View file

@ -6,6 +6,7 @@ mod aarch64;
mod amdgpu;
mod arm;
mod avr;
mod bpf;
mod hexagon;
mod mips;
mod mips64;
@ -654,6 +655,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
}
}
"asmjs" => wasm::compute_c_abi_info(cx, self),
"bpfel" | "bpfeb" => bpf::compute_abi_info(self),
a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),
}

View file

@ -0,0 +1,37 @@
use crate::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, TargetOptions};
use crate::{abi::Endian, spec::abi::Abi};
pub fn opts(endian: Endian) -> TargetOptions {
TargetOptions {
endian,
linker_flavor: LinkerFlavor::BpfLinker,
atomic_cas: false,
executables: true,
dynamic_linking: true,
no_builtins: true,
panic_strategy: PanicStrategy::Abort,
position_independent_executables: true,
merge_functions: MergeFunctions::Disabled,
obj_is_bitcode: true,
requires_lto: false,
singlethread: true,
max_atomic_width: Some(64),
unsupported_abis: vec![
Abi::Cdecl,
Abi::Stdcall { unwind: false },
Abi::Stdcall { unwind: true },
Abi::Fastcall,
Abi::Vectorcall,
Abi::Thiscall { unwind: false },
Abi::Thiscall { unwind: true },
Abi::Aapcs,
Abi::Win64,
Abi::SysV64,
Abi::PtxKernel,
Abi::Msp430Interrupt,
Abi::X86Interrupt,
Abi::AmdGpuKernel,
],
..Default::default()
}
}

View file

@ -0,0 +1,12 @@
use crate::spec::Target;
use crate::{abi::Endian, spec::bpf_base};
pub fn target() -> Target {
Target {
llvm_target: "bpfeb".to_string(),
data_layout: "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128".to_string(),
pointer_width: 64,
arch: "bpfeb".to_string(),
options: bpf_base::opts(Endian::Big),
}
}

View file

@ -0,0 +1,12 @@
use crate::spec::Target;
use crate::{abi::Endian, spec::bpf_base};
pub fn target() -> Target {
Target {
llvm_target: "bpfel".to_string(),
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128".to_string(),
pointer_width: 64,
arch: "bpfel".to_string(),
options: bpf_base::opts(Endian::Little),
}
}

View file

@ -57,6 +57,7 @@ mod apple_base;
mod apple_sdk_base;
mod arm_base;
mod avr_gnu_base;
mod bpf_base;
mod dragonfly_base;
mod freebsd_base;
mod fuchsia_base;
@ -93,6 +94,7 @@ pub enum LinkerFlavor {
Msvc,
Lld(LldFlavor),
PtxLinker,
BpfLinker,
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
@ -161,6 +163,7 @@ flavor_mappings! {
((LinkerFlavor::Ld), "ld"),
((LinkerFlavor::Msvc), "msvc"),
((LinkerFlavor::PtxLinker), "ptx-linker"),
((LinkerFlavor::BpfLinker), "bpf-linker"),
((LinkerFlavor::Lld(LldFlavor::Wasm)), "wasm-ld"),
((LinkerFlavor::Lld(LldFlavor::Ld64)), "ld64.lld"),
((LinkerFlavor::Lld(LldFlavor::Ld)), "ld.lld"),
@ -897,6 +900,9 @@ supported_targets! {
("aarch64_be-unknown-linux-gnu", aarch64_be_unknown_linux_gnu),
("aarch64-unknown-linux-gnu_ilp32", aarch64_unknown_linux_gnu_ilp32),
("aarch64_be-unknown-linux-gnu_ilp32", aarch64_be_unknown_linux_gnu_ilp32),
("bpfeb-unknown-none", bpfeb_unknown_none),
("bpfel-unknown-none", bpfel_unknown_none),
}
/// Everything `rustc` knows about how to compile for a specific target.

View file

@ -268,7 +268,9 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
if builder.no_std(target) == Some(true) {
let mut features = "compiler-builtins-mem".to_string();
features.push_str(compiler_builtins_c_feature);
if !target.starts_with("bpf") {
features.push_str(compiler_builtins_c_feature);
}
// for no-std targets we only compile a few no_std crates
cargo

View file

@ -236,7 +236,7 @@ impl Step for Llvm {
Some(s) => s,
None => {
"AArch64;ARM;Hexagon;MSP430;Mips;NVPTX;PowerPC;RISCV;\
Sparc;SystemZ;WebAssembly;X86"
Sparc;SystemZ;WebAssembly;X86;BPF"
}
};

View file

@ -306,5 +306,6 @@ pub fn use_host_linker(target: TargetSelection) -> bool {
|| target.contains("wasm32")
|| target.contains("nvptx")
|| target.contains("fortanix")
|| target.contains("fuchsia"))
|| target.contains("fuchsia")
|| target.contains("bpf"))
}

View file

@ -234,6 +234,8 @@ flavor. Valid options are:
* `ptx-linker`: use
[`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker) for Nvidia
NVPTX GPGPU support.
* `bpf-linker`: use
[`bpf-linker`](https://github.com/alessandrod/bpf-linker) for eBPF support.
* `wasm-ld`: use the [`wasm-ld`](https://lld.llvm.org/WebAssembly.html)
executable, a port of LLVM `lld` for WebAssembly.
* `ld64.lld`: use the LLVM `lld` executable with the [`-flavor darwin`

View file

@ -85,6 +85,8 @@ static TARGETS: &[&str] = &[
"armv7r-none-eabihf",
"armv7s-apple-ios",
"asmjs-unknown-emscripten",
"bpfeb-unknown-none",
"bpfel-unknown-none",
"i386-apple-ios",
"i586-pc-windows-msvc",
"i586-unknown-linux-gnu",

View file

@ -1835,6 +1835,7 @@ impl<'test> TestCx<'test> {
|| self.config.target.contains("nvptx")
|| self.is_vxworks_pure_static()
|| self.config.target.contains("sgx")
|| self.config.target.contains("bpf")
{
// We primarily compile all auxiliary libraries as dynamic libraries
// to avoid code size bloat and large binaries as much as possible
@ -2310,6 +2311,10 @@ impl<'test> TestCx<'test> {
// No extra flags needed.
}
Some("bpf-linker") => {
rustc.arg("-Clink-args=--emit=asm");
}
Some(_) => self.fatal("unknown 'assembly-output' header"),
None => self.fatal("missing 'assembly-output' header"),
}

View file

@ -48,6 +48,8 @@ const ARCH_TABLE: &[(&str, &str)] = &[
("armv7s", "arm"),
("asmjs", "asmjs"),
("avr", "avr"),
("bpfeb", "bpfeb"),
("bpfel", "bpfel"),
("hexagon", "hexagon"),
("i386", "x86"),
("i586", "x86"),