@@ -25,6 +25,7 @@ GEN COMMANDS
25
25
26
26
| **bpftool** **gen object** *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...]
27
27
| **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*]
28
+ | **bpftool** **gen min_core_btf** *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...]
28
29
| **bpftool** **gen help**
29
30
30
31
DESCRIPTION
@@ -149,6 +150,26 @@ DESCRIPTION
149
150
(non-read-only) data from userspace, with same simplicity
150
151
as for BPF side.
151
152
153
+ **bpftool ** **gen min_core_btf ** *INPUT * *OUTPUT * *OBJECT * [*OBJECT *...]
154
+ Generate a minimum BTF file as *OUTPUT *, derived from a given
155
+ *INPUT * BTF file, containing all needed BTF types so one, or
156
+ more, given eBPF objects CO-RE relocations may be satisfied.
157
+
158
+ When kernels aren't compiled with CONFIG_DEBUG_INFO_BTF,
159
+ libbpf, when loading an eBPF object, has to rely on external
160
+ BTF files to be able to calculate CO-RE relocations.
161
+
162
+ Usually, an external BTF file is built from existing kernel
163
+ DWARF data using pahole. It contains all the types used by
164
+ its respective kernel image and, because of that, is big.
165
+
166
+ The min_core_btf feature builds smaller BTF files, customized
167
+ to one or multiple eBPF objects, so they can be distributed
168
+ together with an eBPF CO-RE based application, turning the
169
+ application portable to different kernel versions.
170
+
171
+ Check examples bellow for more information how to use it.
172
+
152
173
**bpftool gen help **
153
174
Print short help message.
154
175
@@ -215,7 +236,9 @@ This is example BPF application with two BPF programs and a mix of BPF maps
215
236
and global variables. Source code is split across two source code files.
216
237
217
238
**$ clang -target bpf -g example1.bpf.c -o example1.bpf.o **
239
+
218
240
**$ clang -target bpf -g example2.bpf.c -o example2.bpf.o **
241
+
219
242
**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o **
220
243
221
244
This set of commands compiles *example1.bpf.c * and *example2.bpf.c *
@@ -329,3 +352,70 @@ BPF ELF object file *example.bpf.o*.
329
352
my_static_var: 7
330
353
331
354
This is a stripped-out version of skeleton generated for above example code.
355
+
356
+ min_core_btf
357
+ ------------
358
+
359
+ **$ bpftool btf dump file 5.4.0-example.btf format raw **
360
+
361
+ ::
362
+
363
+ [1] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
364
+ [2] CONST '(anon)' type_id=1
365
+ [3] VOLATILE '(anon)' type_id=1
366
+ [4] ARRAY '(anon)' type_id=1 index_type_id=21 nr_elems=2
367
+ [5] PTR '(anon)' type_id=8
368
+ [6] CONST '(anon)' type_id=5
369
+ [7] INT 'char' size=1 bits_offset=0 nr_bits=8 encoding=(none)
370
+ [8] CONST '(anon)' type_id=7
371
+ [9] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
372
+ <long output>
373
+
374
+ **$ bpftool btf dump file one.bpf.o format raw **
375
+
376
+ ::
377
+
378
+ [1] PTR '(anon)' type_id=2
379
+ [2] STRUCT 'trace_event_raw_sys_enter' size=64 vlen=4
380
+ 'ent' type_id=3 bits_offset=0
381
+ 'id' type_id=7 bits_offset=64
382
+ 'args' type_id=9 bits_offset=128
383
+ '__data' type_id=12 bits_offset=512
384
+ [3] STRUCT 'trace_entry' size=8 vlen=4
385
+ 'type' type_id=4 bits_offset=0
386
+ 'flags' type_id=5 bits_offset=16
387
+ 'preempt_count' type_id=5 bits_offset=24
388
+ <long output>
389
+
390
+ **$ bpftool gen min_core_btf 5.4.0-example.btf 5.4.0-smaller.btf one.bpf.o **
391
+
392
+ **$ bpftool btf dump file 5.4.0-smaller.btf format raw **
393
+
394
+ ::
395
+
396
+ [1] TYPEDEF 'pid_t' type_id=6
397
+ [2] STRUCT 'trace_event_raw_sys_enter' size=64 vlen=1
398
+ 'args' type_id=4 bits_offset=128
399
+ [3] STRUCT 'task_struct' size=9216 vlen=2
400
+ 'pid' type_id=1 bits_offset=17920
401
+ 'real_parent' type_id=7 bits_offset=18048
402
+ [4] ARRAY '(anon)' type_id=5 index_type_id=8 nr_elems=6
403
+ [5] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
404
+ [6] TYPEDEF '__kernel_pid_t' type_id=8
405
+ [7] PTR '(anon)' type_id=3
406
+ [8] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
407
+ <end>
408
+
409
+ Now, the "5.4.0-smaller.btf" file may be used by libbpf as an external BTF file
410
+ when loading the "one.bpf.o" object into the "5.4.0-example" kernel. Note that
411
+ the generated BTF file won't allow other eBPF objects to be loaded, just the
412
+ ones given to min_core_btf.
413
+
414
+ ::
415
+
416
+ LIBBPF_OPTS(bpf_object_open_opts, opts, .btf_custom_path = "5.4.0-smaller.btf");
417
+ struct bpf_object *obj;
418
+
419
+ obj = bpf_object__open_file("one.bpf.o", &opts);
420
+
421
+ ...
0 commit comments