14 个版本
0.9.13 | 2024 年 8 月 13 日 |
---|---|
0.9.12 | 2024 年 3 月 21 日 |
0.9.11 | 2024 年 2 月 18 日 |
0.9.10 | 2023 年 9 月 19 日 |
0.9.0 | 2020 年 12 月 1 日 |
Unix API 排名第 40
每月下载量 2,295
110KB
2K SLoC
BPF 链接器 🔗
bpf-linker 旨在简化构建现代 BPF 程序,同时仍然支持较旧、限制更严格的内核。
概述
bpf-linker 可以用于将多个 BPF 对象文件静态链接在一起,并可选择执行针对较旧内核所需的优化。它操作于 LLVM 位码,因此输入必须是位码文件 (.bc) 或嵌入位码的对象文件 (.o),可选地存储在 ar 归档 (.a) 中。
安装
链接器需要 LLVM 19。它可以使用与 rust 编译器相同的 LLVM,或者可以使用外部 LLVM 安装。
如果你的目标是 aarch64-unknown-linux-gnu
(即 Apple Silicon 上的 Linux),你将不得不使用 外部 LLVM 方法。
使用 rustc 提供的 LLVM
你只需运行
cargo install bpf-linker
使用外部 LLVM
在基于 Debian 的发行版上,你需要安装 llvm-19-dev
,libclang-19-dev
和 libpolly-19-dev
软件包。如果你的发行版没有它们,你可以从官方 LLVM 仓库 https://apt.llvm.org 获取。
在基于 rpm 的发行版上,你需要 llvm-devel
和 clang-devel
软件包。如果你的发行版没有它们,你可以从 Fedora Rawhide 获取。
一旦安装了 LLVM 19,你可以通过运行以下命令来安装链接器:
cargo install bpf-linker --no-default-features
如果你没有 cargo,你可以从 https://rustup.rs 或从你的发行版的软件包管理器获取。
使用方法
Rust
夜间版
要编译你的 eBPF 仓库,只需运行
cargo +nightly build --target=bpfel-unknown-none -Z build-std=core --release
如果你不想每次都传递 target
和 build-std
选项,你可以将它们放在仓库根目录下的 .cargo/config.toml
中
[build]
target = "bpfel-unknown-none"
[unstable]
build-std = ["core"]
(实验性)BTF 支持
要输出 BTF 调试信息,请设置以下 rustflags
-C debuginfo=2 -C link-arg=--btf
这些标志仅适用于 eBPF 目标(bpfeb-unknown-none
,bpfel-unknown-none
)。请确保您只为 eBPF crate 设置这些标志,而不是为用户空间 crate 设置!
当直接使用 cargo +nightly build
编译 eBPF crate 时,它们可以通过 RUSTFLAGS
环境变量来定义
RUSTFLAGS="-C debuginfo=1 -C link-arg=--btf" cargo +nightly build --target=bpfel-unknown-none -Z build-std=core --release
为了避免手动指定,您可以将它们放入 .cargo/config.toml
[build]
target = "bpfel-unknown-none"
rustflags = "-C debuginfo=1 -C link-arg=--btf"
[unstable]
build-std = ["core"]
之后,位于 target/bpfel-unknown-none/release
的 BPF 对象文件应该包含一个 BTF 部分。
Clang
有关如何使用 clang 与链接器的一个简单示例,请参阅 此 gist。在示例中 lib.c 被编译为静态库,然后通过 program.c 进行链接。Makefile 显示了如何编译 C 代码然后链接。
使用方法
bpf-linker
USAGE:
bpf-linker [FLAGS] [OPTIONS] --output <output> [--] [inputs]...
FLAGS:
--disable-expand-memcpy-in-order Disable passing --bpf-expand-memcpy-in-order to LLVM
--disable-memory-builtins Disble exporting memcpy, memmove, memset, memcmp and bcmp. Exporting those
is commonly needed when LLVM does not manage to expand memory intrinsics to
a sequence of loads and stores
-h, --help Prints help information
--ignore-inline-never Ignore `noinline`/`#[inline(never)]`. Useful when targeting kernels that
don't support function calls
--unroll-loops Try hard to unroll loops. Useful when targeting kernels that don't support
loops
-V, --version Prints version information
OPTIONS:
--cpu <cpu> Target BPF processor. Can be one of `generic`, `probe`, `v1`, `v2`, `v3` [default:
generic]
--cpu-features <features> Enable or disable CPU features. The available features are: alu32, dummy, dwarfris.
Use +feature to enable a feature, or -feature to disable it. For example --cpu-
features=+alu32,-dwarfris [default: ]
--dump-module <path> Dump the final IR module to the given `path` before generating the code
--emit <emit> Output type. Can be one of `llvm-bc`, `asm`, `llvm-ir`, `obj` [default: obj]
--export <symbols>... Comma separated list of symbols to export. See also `--export-symbols`
--export-symbols <path> Export the symbols specified in the file `path`. The symbols must be separated by
new lines
-L <libs>... Add a directory to the library search path
--llvm-args <args>... Extra command line arguments to pass to LLVM
--log-file <path> Output logs to the given `path`
--log-level <level> Set the log level. Can be one of `off`, `info`, `warn`, `debug`, `trace`
-O <optimize>... Optimization level. 0-3, s, or z [default: 2]
-o, --output <output> Write output to <output>
--target <target> LLVM target triple. When not provided, the target is inferred from the inputs
ARGS:
<inputs>... Input files. Can be object files or static libraries
许可证
bpf-linker 使用以下任一许可证:
- Apache 许可证 2.0 版(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证(LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
依赖项
~9–18MB
~237K SLoC