]> git.lizzy.rs Git - rust.git/commitdiff
Add BPF target
authorAlessandro Decina <alessandro.d@gmail.com>
Mon, 30 Nov 2020 19:41:57 +0000 (19:41 +0000)
committerAlessandro Decina <alessandro.d@gmail.com>
Sun, 23 May 2021 08:03:27 +0000 (18:03 +1000)
This change adds the bpfel-unknown-none and bpfeb-unknown-none targets
which can be used to generate little endian and big endian BPF

18 files changed:
compiler/rustc_codegen_cranelift/src/toolchain.rs
compiler/rustc_codegen_ssa/src/back/link.rs
compiler/rustc_codegen_ssa/src/back/linker.rs
compiler/rustc_llvm/build.rs
compiler/rustc_llvm/src/lib.rs
compiler/rustc_target/src/abi/call/bpf.rs [new file with mode: 0644]
compiler/rustc_target/src/abi/call/mod.rs
compiler/rustc_target/src/spec/bpf_base.rs [new file with mode: 0644]
compiler/rustc_target/src/spec/bpfeb_unknown_none.rs [new file with mode: 0644]
compiler/rustc_target/src/spec/bpfel_unknown_none.rs [new file with mode: 0644]
compiler/rustc_target/src/spec/mod.rs
src/bootstrap/compile.rs
src/bootstrap/native.rs
src/bootstrap/util.rs
src/doc/rustc/src/codegen-options/index.md
src/tools/build-manifest/src/main.rs
src/tools/compiletest/src/runtest.rs
src/tools/compiletest/src/util.rs

index 484a9b699a0aa41aa73c412610bae16e2155d456..49475fe2469edc191a7ce2a0f81a512973a4b21a 100644 (file)
@@ -67,6 +67,7 @@ fn infer_from(
                     LinkerFlavor::Msvc => "link.exe",
                     LinkerFlavor::Lld(_) => "lld",
                     LinkerFlavor::PtxLinker => "rust-ptx-linker",
+                    LinkerFlavor::BpfLinker => "bpf-linker",
                 }),
                 flavor,
             )),
index e330b5e703b1f449465c45b508d33b1abbca8fa6..8b280ab64018ec75300054e9b91bdd86193e6387 100644 (file)
@@ -989,6 +989,7 @@ fn infer_from(
                     LinkerFlavor::Msvc => "link.exe",
                     LinkerFlavor::Lld(_) => "lld",
                     LinkerFlavor::PtxLinker => "rust-ptx-linker",
+                    LinkerFlavor::BpfLinker => "bpf-linker",
                 }),
                 flavor,
             )),
index 4dc9a3f5e41be181199eb2e5b74b762705d557f8..35146d22e6896e611eb70cc55954545445e205ed 100644 (file)
@@ -84,6 +84,7 @@ pub fn to_linker<'a>(
             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 @@ fn group_end(&mut self) {}
 
     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) {}
+}
index 301ed639f3b511b578848a9dcf6de27de345d909..452d1b19a18a84b18e861736f51505a437a0170a 100644 (file)
@@ -86,6 +86,7 @@ fn main() {
         "nvptx",
         "hexagon",
         "riscv",
+        "bpf",
     ];
 
     let required_components = &[
index 555aefb192948303a13864b55a7f167453c3e252..122627eb500ada1b69f2d38924d63e9f4069dcc9 100644 (file)
@@ -167,4 +167,12 @@ fn init() { }
         LLVMInitializeWebAssemblyAsmPrinter,
         LLVMInitializeWebAssemblyAsmParser
     );
+    init_target!(
+        llvm_component = "bpf",
+        LLVMInitializeBPFTargetInfo,
+        LLVMInitializeBPFTarget,
+        LLVMInitializeBPFTargetMC,
+        LLVMInitializeBPFAsmPrinter,
+        LLVMInitializeBPFAsmParser
+    );
 }
diff --git a/compiler/rustc_target/src/abi/call/bpf.rs b/compiler/rustc_target/src/abi/call/bpf.rs
new file mode 100644 (file)
index 0000000..79af015
--- /dev/null
@@ -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);
+    }
+}
index 0cf2441d84e04c5655a2fedf31208e08691d696c..864b4fe5f08d738da194a0155f8fb5568f3b2760 100644 (file)
@@ -6,6 +6,7 @@
 mod amdgpu;
 mod arm;
 mod avr;
+mod bpf;
 mod hexagon;
 mod mips;
 mod mips64;
@@ -654,6 +655,7 @@ pub fn adjust_for_cabi<C>(&mut self, cx: &C, abi: spec::abi::Abi) -> Result<(),
                 }
             }
             "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)),
         }
 
diff --git a/compiler/rustc_target/src/spec/bpf_base.rs b/compiler/rustc_target/src/spec/bpf_base.rs
new file mode 100644 (file)
index 0000000..0853255
--- /dev/null
@@ -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()
+    }
+}
diff --git a/compiler/rustc_target/src/spec/bpfeb_unknown_none.rs b/compiler/rustc_target/src/spec/bpfeb_unknown_none.rs
new file mode 100644 (file)
index 0000000..cb3e4bb
--- /dev/null
@@ -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),
+    }
+}
diff --git a/compiler/rustc_target/src/spec/bpfel_unknown_none.rs b/compiler/rustc_target/src/spec/bpfel_unknown_none.rs
new file mode 100644 (file)
index 0000000..9284030
--- /dev/null
@@ -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),
+    }
+}
index f1bd8ff237d0a9f30f9f7101b94a3786228bcee8..52b0b64280268f62e4adbd6d011c8860f65e4d78 100644 (file)
@@ -57,6 +57,7 @@
 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 @@ pub fn desc(&self) -> &str {
     ((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 @@ fn $module() {
     ("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.
index 2676b3bf8e0059e467fa00c9779711ec2c368f84..3d35a32b3f1716996bfbdfdf0718211a31e26846 100644 (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
index 44c281efe22be0f21626d4875ed6c0a0a9a730f7..49bbcb4d6129d4aa9b54af49301b5e75ae4d0480 100644 (file)
@@ -236,7 +236,7 @@ fn run(self, builder: &Builder<'_>) -> PathBuf {
             Some(s) => s,
             None => {
                 "AArch64;ARM;Hexagon;MSP430;Mips;NVPTX;PowerPC;RISCV;\
-                     Sparc;SystemZ;WebAssembly;X86"
+                     Sparc;SystemZ;WebAssembly;X86;BPF"
             }
         };
 
index b4421a82714fcebd01bd65a7ed8ac9c70aa2215b..cd010b750aa7a94a321841338f5c42d841621298 100644 (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"))
 }
index 077c322bd773678c610be68221c1ace78894dca7..7d86d4b0acdbf0ab6ee5baa1f744cac0e6fb0ec4 100644 (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`
index 4e605caafe22e95cd74118f629c43b9ba23f9bad..1e19b7b21d8bf0fe387029852b8afa4d6df12add 100644 (file)
@@ -85,6 +85,8 @@
     "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",
index 54b079a3e861057422fe1e075d786b2c10517385..f5095486d1a79bf799cec25d85307dba7987b08b 100644 (file)
@@ -1835,6 +1835,7 @@ fn build_auxiliary(&self, source_path: &str, aux_dir: &Path) -> bool {
             || 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 @@ fn compile_test_and_save_assembly(&self) -> (ProcRes, PathBuf) {
                 // 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"),
         }
index 7dbd70948b84d87a1b834ed10b3dee992153760a..238f4f7ae66588f60912f5495159b7e8eb4f0892 100644 (file)
@@ -48,6 +48,8 @@
     ("armv7s", "arm"),
     ("asmjs", "asmjs"),
     ("avr", "avr"),
+    ("bpfeb", "bpfeb"),
+    ("bpfel", "bpfel"),
     ("hexagon", "hexagon"),
     ("i386", "x86"),
     ("i586", "x86"),