@@ -27,7 +27,10 @@ fn relocate_field() {
27
27
"# ,
28
28
relocation_code : r#"
29
29
__u8 memory[] = {1, 2, 3, 4};
30
- __u32 value = BPF_CORE_READ((struct foo *)&memory, c);
30
+ struct foo *ptr = (struct foo *) &memory;
31
+ bpf_probe_read_kernel(&value,
32
+ sizeof(__u8),
33
+ __builtin_preserve_access_index(&ptr->c));
31
34
"# ,
32
35
}
33
36
. build ( )
@@ -46,7 +49,8 @@ fn relocate_enum() {
46
49
enum foo { D = 4 } e1;
47
50
"# ,
48
51
relocation_code : r#"
49
- __u32 value = bpf_core_enum_value(enum foo, D);
52
+ #define BPF_ENUMVAL_VALUE 1
53
+ value = __builtin_preserve_enum_value(*(typeof(enum foo) *)D, BPF_ENUMVAL_VALUE);
50
54
"# ,
51
55
}
52
56
. build ( )
@@ -68,8 +72,10 @@ fn relocate_pointer() {
68
72
"# ,
69
73
relocation_code : r#"
70
74
__u8 memory[] = {42, 0, 0, 0, 0, 0, 0, 0};
71
- struct foo *f = BPF_CORE_READ((struct bar *)&memory, f);
72
- __u32 value = ((__u64) f);
75
+ struct bar* ptr = (struct bar *) &memory;
76
+ bpf_probe_read_kernel(&value,
77
+ sizeof(void *),
78
+ __builtin_preserve_access_index(&ptr->f));
73
79
"# ,
74
80
}
75
81
. build ( )
@@ -114,27 +120,29 @@ impl RelocationTest {
114
120
r#"
115
121
#include <linux/bpf.h>
116
122
117
- #include <bpf/bpf_core_read.h>
118
- #include <bpf/bpf_helpers.h>
119
- #include <bpf/bpf_tracing.h>
123
+ static long (*bpf_map_update_elem)(void *map, const void *key, const void *value, __u64 flags) = (void *) 2;
124
+ static long (*bpf_probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 113;
120
125
121
126
{local_definition}
122
127
123
128
struct {{
124
- __uint(type, BPF_MAP_TYPE_ARRAY);
125
- __type(key, __u32);
126
- __type(value, __u32);
127
- __uint(max_entries, 1);
128
- }} output_map SEC(".maps");
129
+ int (*type)[BPF_MAP_TYPE_ARRAY];
130
+ __u32 *key;
131
+ __u32 *value;
132
+ int (*max_entries)[1];
133
+ }} output_map
134
+ __attribute__((section(".maps"), used));
129
135
130
- SEC("tracepoint/bpf_prog") int bpf_prog(void *ctx) {{
136
+ __attribute__((section("tracepoint/bpf_prog"), used))
137
+ int bpf_prog(void *ctx) {{
131
138
__u32 key = 0;
139
+ __u32 value = 0;
132
140
{relocation_code}
133
141
bpf_map_update_elem(&output_map, &key, &value, BPF_ANY);
134
142
return 0;
135
143
}}
136
144
137
- char _license[] SEC( "license") = "GPL";
145
+ char _license[] __attribute__((section( "license"), used) ) = "GPL";
138
146
"#
139
147
) )
140
148
. context ( "Failed to compile eBPF program" ) ?;
@@ -157,12 +165,11 @@ impl RelocationTest {
157
165
r#"
158
166
#include <linux/bpf.h>
159
167
160
- #include <bpf/bpf_core_read.h>
161
- #include <bpf/bpf_helpers.h>
162
- #include <bpf/bpf_tracing.h>
168
+ static long (*bpf_probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 113;
163
169
164
170
{target_btf}
165
171
int main() {{
172
+ __u32 value = 0;
166
173
// This is needed to make sure to emit BTF for the defined types,
167
174
// it could be dead code eliminated if we don't.
168
175
{relocation_code};
@@ -195,12 +202,6 @@ fn compile(source_code: &str) -> Result<(TempDir, PathBuf)> {
195
202
Command :: new ( "clang" )
196
203
. current_dir ( & tmp_dir)
197
204
. args ( [ "-c" , "-g" , "-O2" , "-target" , "bpf" ] )
198
- // NOTE: these tests depend on libbpf, LIBBPF_INCLUDE must point its headers.
199
- // This is set automatically by the integration-test xtask.
200
- . args ( [
201
- "-I" ,
202
- & std:: env:: var ( "LIBBPF_INCLUDE" ) . context ( "LIBBPF_INCLUDE not set" ) ?,
203
- ] )
204
205
. arg ( & source)
205
206
. status ( )
206
207
. context ( "Failed to run clang" ) ?
0 commit comments