-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
di: btf: only exported symbols (bpf programs) should have linkage=global
We only want exported symbols (programs marked as #[no_mangle]) to have linkage=global. This avoid issues like: Global function write() doesn't return scalar. Only those are supported. verification time 18 usec stack depth 0+0 ... The error above happens when aya-log's WriteBuf::write doesn't get inlined, and ends up having linkage=global. Global functions are verified independently from their callers, so the verifier has less context, and as a result globals are harder to verify. In clang one makes a function global by not making it `static`. That model doesn't work for rust, where all symbols exported by dependencies are non-static, and therefore end up being emitted as global. This commit implements a custom pass which marks functions as linkage=static unless they've been explicitly exported. Fixes aya-rs/aya#808
- Loading branch information
1 parent
d11bf49
commit 4214888
Showing
5 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// no-prefer-dynamic | ||
// compile-flags: --crate-type rlib -C debuginfo=2 | ||
#![no_std] | ||
|
||
#[inline(never)] | ||
pub fn dep_public_symbol() -> u8 { | ||
// read_volatile stops LTO inlining the function in the calling crate | ||
unsafe { core::ptr::read_volatile(0 as *const u8) } | ||
} | ||
|
||
#[no_mangle] | ||
pub fn dep_no_mangle() -> u8 { | ||
dep_public_symbol() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// no-prefer-dynamic | ||
// compile-flags: --crate-type rlib | ||
#![no_std] | ||
|
||
#[panic_handler] | ||
fn panic_impl(_: &core::panic::PanicInfo) -> ! { | ||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// assembly-output: bpf-linker | ||
// compile-flags: --crate-type bin -C link-arg=--emit=obj -C debuginfo=2 -C link-arg=--log-level=debug -C link-arg=--log-file=/tmp/linker-lol.log | ||
#![no_std] | ||
#![no_main] | ||
|
||
// aux-build: loop-panic-handler.rs | ||
extern crate loop_panic_handler; | ||
|
||
// aux-build: dep-exports.rs | ||
extern crate dep_exports as dep; | ||
|
||
pub use dep::dep_public_symbol as local_re_exported; | ||
|
||
#[no_mangle] | ||
fn local_no_mangle() -> u8 { | ||
local_public(1, 2) | ||
} | ||
|
||
#[inline(never)] | ||
pub fn local_public(_arg1: u32, _arg2: u32) -> u8 { | ||
// bind v so we create a debug variable which needs its scope to be fixed | ||
let v = dep::dep_public_symbol(); | ||
// call inline functions so we get inlinedAt scopes to be fixed | ||
inline_function_1(v) + inline_function_2(v) | ||
} | ||
|
||
#[inline(always)] | ||
fn inline_function_1(v: u8) -> u8 { | ||
unsafe { core::ptr::read_volatile(v as *const u8) } | ||
} | ||
|
||
#[inline(always)] | ||
fn inline_function_2(v: u8) -> u8 { | ||
inline_function_1(v) | ||
} | ||
|
||
// #[no_mangle] functions keep linkage=global | ||
// CHECK: FUNC 'local_no_mangle' type_id={{[0-9]+}} linkage=global | ||
|
||
// check that parameter names are preserved | ||
// CHECK: FUNC_PROTO | ||
// CHECK-NEXT: _arg1 | ||
// CHECK-NEXT: _arg2 | ||
|
||
// public functions get static linkage | ||
// CHECK: FUNC '{{.*}}local_public{{.*}}' type_id={{[0-9]+}} linkage=static | ||
// CHECK: FUNC '{{.*}}dep_public_symbol{{.*}}' type_id={{[0-9]+}} linkage=static | ||
|
||
// #[no_mangle] is honored for dep functions | ||
// CHECK: FUNC 'dep_no_mangle' type_id={{[0-9]+}} linkage=global |