Skip to content

Commit 12e7092

Browse files
committed
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
1 parent 92418ce commit 12e7092

File tree

18 files changed

+250
-3
lines changed

18 files changed

+250
-3
lines changed

compiler/rustc_codegen_cranelift/src/toolchain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
6767
LinkerFlavor::Msvc => "link.exe",
6868
LinkerFlavor::Lld(_) => "lld",
6969
LinkerFlavor::PtxLinker => "rust-ptx-linker",
70+
LinkerFlavor::BpfLinker => "bpf-linker",
7071
}),
7172
flavor,
7273
)),

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
989989
LinkerFlavor::Msvc => "link.exe",
990990
LinkerFlavor::Lld(_) => "lld",
991991
LinkerFlavor::PtxLinker => "rust-ptx-linker",
992+
LinkerFlavor::BpfLinker => "bpf-linker",
992993
}),
993994
flavor,
994995
)),

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl LinkerInfo {
8484
LinkerFlavor::PtxLinker => {
8585
Box::new(PtxLinker { cmd, sess, info: self }) as Box<dyn Linker>
8686
}
87+
LinkerFlavor::BpfLinker => Box::new(BpfLinker { cmd, sess, info: self }) as Box<dyn Linker>
8788
}
8889
}
8990
}
@@ -1431,3 +1432,124 @@ impl<'a> Linker for PtxLinker<'a> {
14311432

14321433
fn linker_plugin_lto(&mut self) {}
14331434
}
1435+
1436+
pub struct BpfLinker<'a> {
1437+
cmd: Command,
1438+
sess: &'a Session,
1439+
info: &'a LinkerInfo,
1440+
}
1441+
1442+
impl<'a> Linker for BpfLinker<'a> {
1443+
fn cmd(&mut self) -> &mut Command {
1444+
&mut self.cmd
1445+
}
1446+
1447+
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
1448+
1449+
fn link_rlib(&mut self, path: &Path) {
1450+
self.cmd.arg(path);
1451+
}
1452+
1453+
fn link_whole_rlib(&mut self, path: &Path) {
1454+
self.cmd.arg(path);
1455+
}
1456+
1457+
fn include_path(&mut self, path: &Path) {
1458+
self.cmd.arg("-L").arg(path);
1459+
}
1460+
1461+
fn debuginfo(&mut self, _strip: Strip) {
1462+
self.cmd.arg("--debug");
1463+
}
1464+
1465+
fn add_object(&mut self, path: &Path) {
1466+
self.cmd.arg(path);
1467+
}
1468+
1469+
fn optimize(&mut self) {
1470+
self.cmd.arg(match self.sess.opts.optimize {
1471+
OptLevel::No => "-O0",
1472+
OptLevel::Less => "-O1",
1473+
OptLevel::Default => "-O2",
1474+
OptLevel::Aggressive => "-O3",
1475+
OptLevel::Size => "-Os",
1476+
OptLevel::SizeMin => "-Oz",
1477+
});
1478+
}
1479+
1480+
fn output_filename(&mut self, path: &Path) {
1481+
self.cmd.arg("-o").arg(path);
1482+
}
1483+
1484+
fn finalize(&mut self) {
1485+
self.cmd.arg("--cpu").arg(match self.sess.opts.cg.target_cpu {
1486+
Some(ref s) => s,
1487+
None => &self.sess.target.options.cpu,
1488+
});
1489+
}
1490+
1491+
fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {
1492+
panic!("external dylibs not supported")
1493+
}
1494+
1495+
fn link_rust_dylib(&mut self, _lib: Symbol, _path: &Path) {
1496+
panic!("external dylibs not supported")
1497+
}
1498+
1499+
fn link_staticlib(&mut self, _lib: Symbol, _verbatim: bool) {
1500+
panic!("staticlibs not supported")
1501+
}
1502+
1503+
fn link_whole_staticlib(&mut self, _lib: Symbol, _verbatim: bool, _search_path: &[PathBuf]) {
1504+
panic!("staticlibs not supported")
1505+
}
1506+
1507+
fn framework_path(&mut self, _path: &Path) {
1508+
panic!("frameworks not supported")
1509+
}
1510+
1511+
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
1512+
panic!("frameworks not supported")
1513+
}
1514+
1515+
fn full_relro(&mut self) {}
1516+
1517+
fn partial_relro(&mut self) {}
1518+
1519+
fn no_relro(&mut self) {}
1520+
1521+
fn gc_sections(&mut self, _keep_metadata: bool) {}
1522+
1523+
fn no_gc_sections(&mut self) {}
1524+
1525+
fn pgo_gen(&mut self) {}
1526+
1527+
fn no_crt_objects(&mut self) {}
1528+
1529+
fn no_default_libraries(&mut self) {}
1530+
1531+
fn control_flow_guard(&mut self) {}
1532+
1533+
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
1534+
let path = tmpdir.join("symbols");
1535+
let res: io::Result<()> = try {
1536+
let mut f = BufWriter::new(File::create(&path)?);
1537+
for sym in self.info.exports[&crate_type].iter() {
1538+
writeln!(f, "{}", sym)?;
1539+
}
1540+
};
1541+
if let Err(e) = res {
1542+
self.sess.fatal(&format!("failed to write symbols file: {}", e));
1543+
} else {
1544+
self.cmd.arg("--export-symbols").arg(&path);
1545+
}
1546+
}
1547+
1548+
fn subsystem(&mut self, _subsystem: &str) {}
1549+
1550+
fn group_start(&mut self) {}
1551+
1552+
fn group_end(&mut self) {}
1553+
1554+
fn linker_plugin_lto(&mut self) {}
1555+
}

compiler/rustc_llvm/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ fn main() {
8686
"nvptx",
8787
"hexagon",
8888
"riscv",
89+
"bpf",
8990
];
9091

9192
let required_components = &[

compiler/rustc_llvm/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,12 @@ pub fn initialize_available_targets() {
167167
LLVMInitializeWebAssemblyAsmPrinter,
168168
LLVMInitializeWebAssemblyAsmParser
169169
);
170+
init_target!(
171+
llvm_component = "bpf",
172+
LLVMInitializeBPFTargetInfo,
173+
LLVMInitializeBPFTarget,
174+
LLVMInitializeBPFTargetMC,
175+
LLVMInitializeBPFAsmPrinter,
176+
LLVMInitializeBPFAsmParser
177+
);
170178
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// see BPFCallingConv.td
2+
use crate::abi::call::{ArgAbi, FnAbi};
3+
4+
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
5+
if ret.layout.is_aggregate() || ret.layout.size.bits() > 64 {
6+
ret.make_indirect();
7+
} else {
8+
ret.extend_integer_width_to(64);
9+
}
10+
}
11+
12+
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
13+
if arg.layout.is_aggregate() || arg.layout.size.bits() > 64 {
14+
arg.make_indirect();
15+
} else {
16+
arg.extend_integer_width_to(64);
17+
}
18+
}
19+
20+
pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
21+
if !fn_abi.ret.is_ignore() {
22+
classify_ret(&mut fn_abi.ret);
23+
}
24+
25+
for arg in &mut fn_abi.args {
26+
if arg.is_ignore() {
27+
continue;
28+
}
29+
classify_arg(arg);
30+
}
31+
}

compiler/rustc_target/src/abi/call/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod aarch64;
66
mod amdgpu;
77
mod arm;
88
mod avr;
9+
mod bpf;
910
mod hexagon;
1011
mod mips;
1112
mod mips64;
@@ -654,6 +655,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
654655
}
655656
}
656657
"asmjs" => wasm::compute_c_abi_info(cx, self),
658+
"bpfel" | "bpfeb" => bpf::compute_abi_info(self),
657659
a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),
658660
}
659661

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, TargetOptions};
2+
use crate::{abi::Endian, spec::abi::Abi};
3+
4+
pub fn opts(endian: Endian) -> TargetOptions {
5+
TargetOptions {
6+
endian,
7+
linker_flavor: LinkerFlavor::BpfLinker,
8+
atomic_cas: false,
9+
executables: true,
10+
dynamic_linking: true,
11+
no_builtins: true,
12+
panic_strategy: PanicStrategy::Abort,
13+
position_independent_executables: true,
14+
merge_functions: MergeFunctions::Disabled,
15+
obj_is_bitcode: true,
16+
requires_lto: false,
17+
singlethread: true,
18+
max_atomic_width: Some(64),
19+
unsupported_abis: vec![
20+
Abi::Cdecl,
21+
Abi::Stdcall { unwind: false },
22+
Abi::Stdcall { unwind: true },
23+
Abi::Fastcall,
24+
Abi::Vectorcall,
25+
Abi::Thiscall { unwind: false },
26+
Abi::Thiscall { unwind: true },
27+
Abi::Aapcs,
28+
Abi::Win64,
29+
Abi::SysV64,
30+
Abi::PtxKernel,
31+
Abi::Msp430Interrupt,
32+
Abi::X86Interrupt,
33+
Abi::AmdGpuKernel,
34+
],
35+
..Default::default()
36+
}
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::spec::Target;
2+
use crate::{abi::Endian, spec::bpf_base};
3+
4+
pub fn target() -> Target {
5+
Target {
6+
llvm_target: "bpfeb".to_string(),
7+
data_layout: "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128".to_string(),
8+
pointer_width: 64,
9+
arch: "bpfeb".to_string(),
10+
options: bpf_base::opts(Endian::Big),
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::spec::Target;
2+
use crate::{abi::Endian, spec::bpf_base};
3+
4+
pub fn target() -> Target {
5+
Target {
6+
llvm_target: "bpfel".to_string(),
7+
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128".to_string(),
8+
pointer_width: 64,
9+
arch: "bpfel".to_string(),
10+
options: bpf_base::opts(Endian::Little),
11+
}
12+
}

compiler/rustc_target/src/spec/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod apple_base;
5757
mod apple_sdk_base;
5858
mod arm_base;
5959
mod avr_gnu_base;
60+
mod bpf_base;
6061
mod dragonfly_base;
6162
mod freebsd_base;
6263
mod fuchsia_base;
@@ -93,6 +94,7 @@ pub enum LinkerFlavor {
9394
Msvc,
9495
Lld(LldFlavor),
9596
PtxLinker,
97+
BpfLinker,
9698
}
9799

98100
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
@@ -161,6 +163,7 @@ flavor_mappings! {
161163
((LinkerFlavor::Ld), "ld"),
162164
((LinkerFlavor::Msvc), "msvc"),
163165
((LinkerFlavor::PtxLinker), "ptx-linker"),
166+
((LinkerFlavor::BpfLinker), "bpf-linker"),
164167
((LinkerFlavor::Lld(LldFlavor::Wasm)), "wasm-ld"),
165168
((LinkerFlavor::Lld(LldFlavor::Ld64)), "ld64.lld"),
166169
((LinkerFlavor::Lld(LldFlavor::Ld)), "ld.lld"),
@@ -897,6 +900,9 @@ supported_targets! {
897900
("aarch64_be-unknown-linux-gnu", aarch64_be_unknown_linux_gnu),
898901
("aarch64-unknown-linux-gnu_ilp32", aarch64_unknown_linux_gnu_ilp32),
899902
("aarch64_be-unknown-linux-gnu_ilp32", aarch64_be_unknown_linux_gnu_ilp32),
903+
904+
("bpfeb-unknown-none", bpfeb_unknown_none),
905+
("bpfel-unknown-none", bpfel_unknown_none),
900906
}
901907

902908
/// Everything `rustc` knows about how to compile for a specific target.

src/bootstrap/compile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
268268

269269
if builder.no_std(target) == Some(true) {
270270
let mut features = "compiler-builtins-mem".to_string();
271-
features.push_str(compiler_builtins_c_feature);
271+
if !target.starts_with("bpf") {
272+
features.push_str(compiler_builtins_c_feature);
273+
}
272274

273275
// for no-std targets we only compile a few no_std crates
274276
cargo

src/bootstrap/native.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl Step for Llvm {
236236
Some(s) => s,
237237
None => {
238238
"AArch64;ARM;Hexagon;MSP430;Mips;NVPTX;PowerPC;RISCV;\
239-
Sparc;SystemZ;WebAssembly;X86"
239+
Sparc;SystemZ;WebAssembly;X86;BPF"
240240
}
241241
};
242242

src/bootstrap/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,5 +306,6 @@ pub fn use_host_linker(target: TargetSelection) -> bool {
306306
|| target.contains("wasm32")
307307
|| target.contains("nvptx")
308308
|| target.contains("fortanix")
309-
|| target.contains("fuchsia"))
309+
|| target.contains("fuchsia")
310+
|| target.contains("bpf"))
310311
}

src/doc/rustc/src/codegen-options/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ flavor. Valid options are:
234234
* `ptx-linker`: use
235235
[`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker) for Nvidia
236236
NVPTX GPGPU support.
237+
* `bpf-linker`: use
238+
[`bpf-linker`](https://github.com/alessandrod/bpf-linker) for eBPF support.
237239
* `wasm-ld`: use the [`wasm-ld`](https://lld.llvm.org/WebAssembly.html)
238240
executable, a port of LLVM `lld` for WebAssembly.
239241
* `ld64.lld`: use the LLVM `lld` executable with the [`-flavor darwin`

src/tools/build-manifest/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ static TARGETS: &[&str] = &[
8585
"armv7r-none-eabihf",
8686
"armv7s-apple-ios",
8787
"asmjs-unknown-emscripten",
88+
"bpfeb-unknown-none",
89+
"bpfel-unknown-none",
8890
"i386-apple-ios",
8991
"i586-pc-windows-msvc",
9092
"i586-unknown-linux-gnu",

src/tools/compiletest/src/runtest.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,7 @@ impl<'test> TestCx<'test> {
18351835
|| self.config.target.contains("nvptx")
18361836
|| self.is_vxworks_pure_static()
18371837
|| self.config.target.contains("sgx")
1838+
|| self.config.target.contains("bpf")
18381839
{
18391840
// We primarily compile all auxiliary libraries as dynamic libraries
18401841
// to avoid code size bloat and large binaries as much as possible
@@ -2310,6 +2311,10 @@ impl<'test> TestCx<'test> {
23102311
// No extra flags needed.
23112312
}
23122313

2314+
Some("bpf-linker") => {
2315+
rustc.arg("-Clink-args=--emit=asm");
2316+
}
2317+
23132318
Some(_) => self.fatal("unknown 'assembly-output' header"),
23142319
None => self.fatal("missing 'assembly-output' header"),
23152320
}

src/tools/compiletest/src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const ARCH_TABLE: &[(&str, &str)] = &[
4848
("armv7s", "arm"),
4949
("asmjs", "asmjs"),
5050
("avr", "avr"),
51+
("bpfeb", "bpfeb"),
52+
("bpfel", "bpfel"),
5153
("hexagon", "hexagon"),
5254
("i386", "x86"),
5355
("i586", "x86"),

0 commit comments

Comments
 (0)