BPF static linking
Subsystems (7)
Andrii Nakryiko
maintainerv3 cover BPF static linking
This patch set adds new libbpf APIs and their bpftool integration that allows to perform static linking of BPF object files. Currently no extern resolution across object files is performed. This is going to be the focus of the follow up patches. But, given amount of code and logic necessary to perform just basic functionality of linking together mostly independent BPF object files, it was decided to land basic BPF linker code and logic first and extend it afterwards.
The motivation for BPF static linking is to provide the functionality that is naturally assumed for user-space development process: ability to structure application's code without artificial restrictions of having all the code and data (variables and maps) inside a single source code file.
This enables better engineering practices of splitting code into well-encapsulated parts. It provides ability to hide internal state from other parts of the code base through static variables and maps. It is also a first steps towards having generic reusable BPF libraries.
Please see individual patches (mostly #6 and #7) for more details. Patch #10 passes all test_progs' individual BPF .o files through BPF static linker, which is supposed to be a no-op operation, so is essentially validating that BPF static linker doesn't produce corrupted ELF object files. Patch #11 adds Makefile infra to be able to specify multi-file BPF object files and adds the first multi-file test to validate correctness.
v2->v3:
- added F(F(F(X))) = F(F(X)) test for all linked BPF object files (Alexei);
- used reallocarray() more consistently in few places (Alexei);
- improved bash completions for gen object (Quentin);
- dropped .bpfo extension, but had to add optional name OBJECT_FILE parameter (path #8) to gen skeleton command to specify desired object name during skeleton generation;
- fixed bug with not skipping license and version DATASECs in 2nd and later object files, resulting in symbol lookup failure on subsequent linking passes;v1->v2:
- extracted struct strset to manage unique set of strings both for BTF and
ELF SYMTAB (patch #4, refactors btf and btf_dedup logic as well) (Alexei);
- fixed bugs in bpftool gen command; renamed it to gen object, added BASH completions and extended/updated man page (Quentin).
Andrii Nakryiko (12):
libbpf: expose btf_type_by_id() internally
libbpf: generalize BTF and BTF.ext type ID and strings iteration
libbpf: rename internal memory-management helpers
libbpf: extract internal set-of-strings datastructure APIs
libbpf: add generic BTF type shallow copy API
libbpf: add BPF static linker APIs
libbpf: add BPF static linker BTF and BTF.ext support
bpftool: add ability to specify custom skeleton object name
bpftool: add gen object command to perform BPF static linking selftests/bpf: re-generate vmlinux.h and BPF skeletons if bpftool
changed
selftests/bpf: pass all BPF .o's through BPF static linker selftests/bpf: add multi-file statically linked BPF object file test
.../bpf/bpftool/Documentation/bpftool-gen.rst | 78 +- tools/bpf/bpftool/bash-completion/bpftool | 17 +- tools/bpf/bpftool/gen.c | 70 +- tools/lib/bpf/Build | 2 +- tools/lib/bpf/btf.c | 714 +++--- tools/lib/bpf/btf.h | 2 + tools/lib/bpf/btf_dump.c | 8 +- tools/lib/bpf/libbpf.c | 15 +- tools/lib/bpf/libbpf.h | 13 + tools/lib/bpf/libbpf.map | 5 + tools/lib/bpf/libbpf_internal.h | 38 +- tools/lib/bpf/linker.c | 1941 +++++++++++++++++ tools/lib/bpf/strset.c | 176 ++ tools/lib/bpf/strset.h | 21 + tools/testing/selftests/bpf/Makefile | 27 +- .../selftests/bpf/prog_tests/static_linked.c | 40 + .../selftests/bpf/progs/test_static_linked1.c | 30 + .../selftests/bpf/progs/test_static_linked2.c | 31 + 18 files changed, 2805 insertions(+), 423 deletions(-) create mode 100644 tools/lib/bpf/linker.c create mode 100644 tools/lib/bpf/strset.c create mode 100644 tools/lib/bpf/strset.h create mode 100644 tools/testing/selftests/bpf/prog_tests/static_linked.c create mode 100644 tools/testing/selftests/bpf/progs/test_static_linked1.c create mode 100644 tools/testing/selftests/bpf/progs/test_static_linked2.c
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 2/12 libbpf: generalize BTF and BTF.ext type ID and strings iteration
Extract and generalize the logic to iterate BTF type ID and string offset fields within BTF types and .BTF.ext data. Expose this internally in libbpf for re-use by bpf_linker.
Additionally, complete strings deduplication handling for BTF.ext (e.g., CO-RE access strings), which was previously missing. There previously was no case of deduplicating .BTF.ext data, but bpf_linker is going to use it.
--- tools/lib/bpf/btf.c | 393 ++++++++++++++++++-------------- tools/lib/bpf/libbpf_internal.h | 7 + 2 files changed, 228 insertions(+), 172 deletions(-)
tools/lib/bpf/btf.c +221 -172
@@ -3155,95 +3155,28 @@ static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,return d; } -typedef int (*str_off_fn_t )(__u32 *str_off_ptr,void *ctx); -/* * Iterate over all possible places in .BTF and .BTF.ext that can reference * string and pass pointer to it to a provided callback `fn`. */ -static int btf_for_each_str_off (struct btf_dedup *d,str_off_fn_t fn,void *ctx) +static int btf_for_each_str_off (struct btf_dedup *d,str_off_visit_fn fn,void *ctx) { -void *line_data_cur, *line_data_end; -int i, j, r, rec_size; -struct btf_type *t; +int i, r;for (i =0 ; i < d->btf->nr_types; i++) { - t =btf_type_by_id (d->btf, d->btf->start_id + i); - r =fn (&t->name_off, ctx); +struct btf_type *t =btf_type_by_id (d->btf, d->btf->start_id + i); + + r =btf_type_visit_str_offs (t, fn, ctx);if (r)return r; - -switch (btf_kind (t)) { -case BTF_KIND_STRUCT : -case BTF_KIND_UNION : { -struct btf_member *m =btf_members (t); -__u16 vlen =btf_vlen (t); - -for (j =0 ; j < vlen; j++) { - r =fn (&m->name_off, ctx); -if (r) -return r; - m++; - } -break ; - } -case BTF_KIND_ENUM : { -struct btf_enum *m =btf_enum (t); -__u16 vlen =btf_vlen (t); - -for (j =0 ; j < vlen; j++) { - r =fn (&m->name_off, ctx); -if (r) -return r; - m++; - } -break ; - } -case BTF_KIND_FUNC_PROTO : { -struct btf_param *m =btf_params (t); -__u16 vlen =btf_vlen (t); - -for (j =0 ; j < vlen; j++) { - r =fn (&m->name_off, ctx); -if (r) -return r; - m++; - } -break ; - } -default : -break ; - } }if (!d->btf_ext)return 0 ; - line_data_cur = d->btf_ext->line_info.info; - line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len; - rec_size = d->btf_ext->line_info.rec_size; - -while (line_data_cur < line_data_end) { -struct btf_ext_info_sec *sec = line_data_cur; -struct bpf_line_info_min *line_info; -__u32 num_info = sec->num_info; - - r =fn (&sec->sec_name_off, ctx); -if (r) -return r; - - line_data_cur +=sizeof (struct btf_ext_info_sec ); -for (i =0 ; i < num_info; i++) { - line_info = line_data_cur; - r =fn (&line_info->file_name_off, ctx); -if (r) -return r; - r =fn (&line_info->line_off, ctx); -if (r) -return r; - line_data_cur += rec_size; - } - } + r =btf_ext_visit_str_offs (d->btf_ext, fn, ctx); +if (r) +return r;return 0 ; }
@@ -4498,15 +4431,18 @@ static int btf_dedup_compact_types(struct btf_dedup *d)* then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map, * which is populated during compaction phase. */ -static int btf_dedup_remap_type_id (struct btf_dedup *d,__u32 type_id) +static int btf_dedup_remap_type_id (__u32 *type_id,void *ctx) { +struct btf_dedup *d = ctx;__u32 resolved_type_id, new_type_id; - resolved_type_id =resolve_type_id (d, type_id); + resolved_type_id =resolve_type_id (d, *type_id); new_type_id = d->hypot_map[resolved_type_id];if (new_type_id >BTF_MAX_NR_TYPES )return -EINVAL ; -return new_type_id; + + *type_id = new_type_id; +return 0 ; }/*
@@ -4519,109 +4455,25 @@ static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)* referenced from any BTF type (e.g., struct fields, func proto args, etc) to * their final deduped type IDs. */ -static int btf_dedup_remap_type (struct btf_dedup *d,__u32 type_id) +static int btf_dedup_remap_types (struct btf_dedup *d) { -struct btf_type *t =btf_type_by_id (d->btf, type_id);int i, r; -switch (btf_kind (t)) { -case BTF_KIND_INT : -case BTF_KIND_ENUM : -case BTF_KIND_FLOAT : -break ; - -case BTF_KIND_FWD : -case BTF_KIND_CONST : -case BTF_KIND_VOLATILE : -case BTF_KIND_RESTRICT : -case BTF_KIND_PTR : -case BTF_KIND_TYPEDEF : -case BTF_KIND_FUNC : -case BTF_KIND_VAR : - r =btf_dedup_remap_type_id (d, t->type); -if (r <0 ) -return r; - t->type = r; -break ; - -case BTF_KIND_ARRAY : { -struct btf_array *arr_info =btf_array (t); - - r =btf_dedup_remap_type_id (d, arr_info->type); -if (r <0 ) -return r; - arr_info->type = r; - r =btf_dedup_remap_type_id (d, arr_info->index_type); -if (r <0 ) -return r; - arr_info->index_type = r; -break ; - } - -case BTF_KIND_STRUCT : -case BTF_KIND_UNION : { -struct btf_member *member =btf_members (t); -__u16 vlen =btf_vlen (t); - -for (i =0 ; i < vlen; i++) { - r =btf_dedup_remap_type_id (d, member->type); -if (r <0 ) -return r; - member->type = r; - member++; - } -break ; - } - -case BTF_KIND_FUNC_PROTO : { -struct btf_param *param =btf_params (t); -__u16 vlen =btf_vlen (t); +for (i =0 ; i < d->btf->nr_types; i++) { +struct btf_type *t =btf_type_by_id (d->btf, d->btf->start_id + i); - r =btf_dedup_remap_type_id (d, t->type); -if (r <0 ) + r =btf_type_visit_type_ids (t, btf_dedup_remap_type_id, d); +if (r)return r; - t->type = r; - -for (i =0 ; i < vlen; i++) { - r =btf_dedup_remap_type_id (d, param->type); -if (r <0 ) -return r; - param->type = r; - param++; - } -break ; - } - -case BTF_KIND_DATASEC : { -struct btf_var_secinfo *var =btf_var_secinfos (t); -__u16 vlen =btf_vlen (t); - -for (i =0 ; i < vlen; i++) { - r =btf_dedup_remap_type_id (d, var->type); -if (r <0 ) -return r; - var->type = r; - var++; - } -break ; - } - -default : -return -EINVAL ; } -return 0 ; -} +if (!d->btf_ext) +return 0 ; -static int btf_dedup_remap_types (struct btf_dedup *d) -{ -int i, r; + r =btf_ext_visit_type_ids (d->btf_ext, btf_dedup_remap_type_id, d); +if (r) +return r; -for (i =0 ; i < d->btf->nr_types; i++) { - r =btf_dedup_remap_type (d, d->btf->start_id + i); -if (r <0 ) -return r; - }return 0 ; }
@@ -4675,3 +4527,200 @@ struct btf *libbpf_find_kernel_btf(void)pr_warn ("failed to find valid kernel BTF\n" );return ERR_PTR (-ESRCH ); } + +int btf_type_visit_type_ids (struct btf_type *t,type_id_visit_fn visit,void *ctx) +{ +int i, n, err; + +switch (btf_kind (t)) { +case BTF_KIND_INT : +case BTF_KIND_FLOAT : +case BTF_KIND_ENUM : +return 0 ; + +case BTF_KIND_FWD : +case BTF_KIND_CONST : +case BTF_KIND_VOLATILE : +case BTF_KIND_RESTRICT : +case BTF_KIND_PTR : +case BTF_KIND_TYPEDEF : +case BTF_KIND_FUNC : +case BTF_KIND_VAR : +return visit (&t->type, ctx); + +case BTF_KIND_ARRAY : { +struct btf_array *a =btf_array (t); + + err =visit (&a->type, ctx); + err = err ?:visit (&a->index_type, ctx); +return err; + } + +case BTF_KIND_STRUCT : +case BTF_KIND_UNION : { +struct btf_member *m =btf_members (t); + +for (i =0 , n =btf_vlen (t); i < n; i++, m++) { + err =visit (&m->type, ctx); +if (err) +return err; + } +return 0 ; + } + +case BTF_KIND_FUNC_PROTO : { +struct btf_param *m =btf_params (t); + + err =visit (&t->type, ctx); +if (err) +return err; +for (i =0 , n =btf_vlen (t); i < n; i++, m++) { + err =visit (&m->type, ctx); +if (err) +return err; + } +return 0 ; + } + +case BTF_KIND_DATASEC : { +struct btf_var_secinfo *m =btf_var_secinfos (t); + +for (i =0 , n =btf_vlen (t); i < n; i++, m++) { + err =visit (&m->type, ctx); +if (err) +return err; + } +return 0 ; + } + +default : +return -EINVAL ; + } +} + +int btf_type_visit_str_offs (struct btf_type *t,str_off_visit_fn visit,void *ctx) +{ +int i, n, err; + + err =visit (&t->name_off, ctx); +if (err) +return err; + +switch (btf_kind (t)) { +case BTF_KIND_STRUCT : +case BTF_KIND_UNION : { +struct btf_member *m =btf_members (t); + +for (i =0 , n =btf_vlen (t); i < n; i++, m++) { + err =visit (&m->name_off, ctx); +if (err) +return err; + } +break ; + } +case BTF_KIND_ENUM : { +struct btf_enum *m =btf_enum (t); + +for (i =0 , n =btf_vlen (t); i < n; i++, m++) { + err =visit (&m->name_off, ctx); +if (err) +return err; + } +break ; + } +case BTF_KIND_FUNC_PROTO : { +struct btf_param *m =btf_params (t); + +for (i =0 , n =btf_vlen (t); i < n; i++, m++) { + err =visit (&m->name_off, ctx); +if (err) +return err; + } +break ; + } +default : +break ; + } + +return 0 ; +} + +int btf_ext_visit_type_ids (struct btf_ext *btf_ext,type_id_visit_fn visit,void *ctx) +{ +const struct btf_ext_info *seg; +struct btf_ext_info_sec *sec; +int i, err; + + seg = &btf_ext->func_info; +for_each_btf_ext_sec (seg, sec) { +struct bpf_func_info_min *rec; + +for_each_btf_ext_rec (seg, sec, i, rec) { + err =visit (&rec->type_id, ctx); +if (err <0 ) +return err; + } + } + + seg = &btf_ext->core_relo_info; +for_each_btf_ext_sec (seg, sec) { +struct bpf_core_relo *rec; + +for_each_btf_ext_rec (seg, sec, i, rec) { + err =visit (&rec->type_id, ctx); +if (err <0 ) +return err; + } + } + +return 0 ; +} + +int btf_ext_visit_str_offs (struct btf_ext *btf_ext,str_off_visit_fn visit,void *ctx) +{ +const struct btf_ext_info *seg; +struct btf_ext_info_sec *sec; +int i, err; + + seg = &btf_ext->func_info; +for_each_btf_ext_sec (seg, sec) { + err =visit (&sec->sec_name_off, ctx); +if (err) +return err; + } + + seg = &btf_ext->line_info; +for_each_btf_ext_sec (seg, sec) { +struct bpf_line_info_min *rec; + + err =visit (&sec->sec_name_off, ctx); +if (err) +return err; + +for_each_btf_ext_rec (seg, sec, i, rec) { + err =visit (&rec->file_name_off, ctx); +if (err) +return err; + err =visit (&rec->line_off, ctx); +if (err) +return err; + } + } + + seg = &btf_ext->core_relo_info; +for_each_btf_ext_sec (seg, sec) { +struct bpf_core_relo *rec; + + err =visit (&sec->sec_name_off, ctx); +if (err) +return err; + +for_each_btf_ext_rec (seg, sec, i, rec) { + err =visit (&rec->access_str_off, ctx); +if (err) +return err; + } + } + +return 0 ; +}
tools/lib/bpf/libbpf_internal.h +7 -0
@@ -356,4 +356,11 @@ struct bpf_core_relo { enum bpf_core_relo_kind kind; }; +typedef int (*type_id_visit_fn )(__u32 *type_id,void *ctx); +typedef int (*str_off_visit_fn )(__u32 *str_off,void *ctx); +int btf_type_visit_type_ids (struct btf_type *t,type_id_visit_fn visit,void *ctx); +int btf_type_visit_str_offs (struct btf_type *t,str_off_visit_fn visit,void *ctx); +int btf_ext_visit_type_ids (struct btf_ext *btf_ext,type_id_visit_fn visit,void *ctx); +int btf_ext_visit_str_offs (struct btf_ext *btf_ext,str_off_visit_fn visit,void *ctx); +#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 1/12 libbpf: expose btf_type_by_id() internally
In reply to Andrii Nakryikobtf_type_by_id() is internal-only convenience API returning non-const pointer to struct btf_type. Expose it outside of btf.c for re-use.
--- tools/lib/bpf/btf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)
tools/lib/bpf/btf.c +1 -1
@@ -435,7 +435,7 @@ const struct btf *btf__base_btf(const struct btf *btf) }/* internal helper returning non-const pointer to a type */ -static struct btf_type *btf_type_by_id (struct btf *btf,__u32 type_id) +struct btf_type *btf_type_by_id (struct btf *btf,__u32 type_id) {if (type_id ==0 )return &btf_void;
tools/lib/bpf/libbpf_internal.h +5 -0
@@ -107,6 +107,11 @@ static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)return realloc (ptr, total); } +struct btf ; +struct btf_type ; + +struct btf_type *btf_type_by_id (struct btf *btf,__u32 type_id); +void *btf_add_mem (void **data,size_t *cap_cnt,size_t elem_sz,size_t cur_cnt,size_t max_cnt,size_t add_cnt);int btf_ensure_mem (void **data,size_t *cap_cnt,size_t elem_sz,size_t need_cnt);
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 3/12 libbpf: rename internal memory-management helpers
In reply to Andrii NakryikoRename btf_add_mem() and btf_ensure_mem() helpers that abstract away details of dynamically resizable memory to use libbpf_ prefix, as they are not BTF-specific. No functional changes.
--- tools/lib/bpf/btf.c | 24 ++++++++++++------------ tools/lib/bpf/btf_dump.c | 8 ++++---- tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/libbpf_internal.h | 6 +++--- 4 files changed, 21 insertions(+), 21 deletions(-)
tools/lib/bpf/btf.c +12 -12
@@ -142,8 +142,8 @@ static inline __u64 ptr_to_u64(const void *ptr)* On success, memory pointer to the beginning of unused memory is returned. * On error, NULL is returned. */ -void *btf_add_mem (void **data,size_t *cap_cnt,size_t elem_sz, -size_t cur_cnt,size_t max_cnt,size_t add_cnt) +void *libbpf_add_mem (void **data,size_t *cap_cnt,size_t elem_sz, +size_t cur_cnt,size_t max_cnt,size_t add_cnt) {size_t new_cnt;void *new_data;
@@ -179,14 +179,14 @@ void *btf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,/* Ensure given dynamically allocated memory region has enough allocated space * to accommodate *need_cnt* elements of size *elem_sz* bytes each */ -int btf_ensure_mem (void **data,size_t *cap_cnt,size_t elem_sz,size_t need_cnt) +int libbpf_ensure_mem (void **data,size_t *cap_cnt,size_t elem_sz,size_t need_cnt) {void *p;if (need_cnt <= *cap_cnt)return 0 ; - p =btf_add_mem (data, cap_cnt, elem_sz, *cap_cnt,SIZE_MAX , need_cnt - *cap_cnt); + p =libbpf_add_mem (data, cap_cnt, elem_sz, *cap_cnt,SIZE_MAX , need_cnt - *cap_cnt);if (!p)return -ENOMEM ;
@@ -197,8 +197,8 @@ static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off) {__u32 *p; - p =btf_add_mem ((void **)&btf->type_offs, &btf->type_offs_cap,sizeof (__u32), - btf->nr_types,BTF_MAX_NR_TYPES ,1 ); + p =libbpf_add_mem ((void **)&btf->type_offs, &btf->type_offs_cap,sizeof (__u32), + btf->nr_types,BTF_MAX_NR_TYPES ,1 );if (!p)return -ENOMEM ;
@@ -1586,8 +1586,8 @@ static int btf_ensure_modifiable(struct btf *btf)static void *btf_add_str_mem (struct btf *btf,size_t add_sz) { -return btf_add_mem (&btf->strs_data, &btf->strs_data_cap,1 , - btf->hdr->str_len,BTF_MAX_STR_OFFSET , add_sz); +return libbpf_add_mem (&btf->strs_data, &btf->strs_data_cap,1 , + btf->hdr->str_len,BTF_MAX_STR_OFFSET , add_sz); }/* Find an offset in BTF string section that corresponds to a given string *s*.
@@ -1683,8 +1683,8 @@ int btf__add_str(struct btf *btf, const char *s)static void *btf_add_type_mem (struct btf *btf,size_t add_sz) { -return btf_add_mem (&btf->types_data, &btf->types_data_cap,1 , - btf->hdr->type_len,UINT_MAX , add_sz); +return libbpf_add_mem (&btf->types_data, &btf->types_data_cap,1 , + btf->hdr->type_len,UINT_MAX , add_sz); }static __u32 btf_type_info (int kind,int vlen,int kflag)
@@ -3208,7 +3208,7 @@ static int strs_dedup_remap_str_off(__u32 *str_off_ptr, void *ctx) len =strlen (s) +1 ; new_off = d->strs_len; - p =btf_add_mem (&d->strs_data, &d->strs_cap,1 , new_off,BTF_MAX_STR_OFFSET , len); + p =libbpf_add_mem (&d->strs_data, &d->strs_cap,1 , new_off,BTF_MAX_STR_OFFSET , len);if (!p)return -ENOMEM ;
@@ -3264,7 +3264,7 @@ static int btf_dedup_strings(struct btf_dedup *d) }if (!d->btf->base_btf) { - s =btf_add_mem (&d->strs_data, &d->strs_cap,1 , d->strs_len,BTF_MAX_STR_OFFSET ,1 ); + s =libbpf_add_mem (&d->strs_data, &d->strs_cap,1 , d->strs_len,BTF_MAX_STR_OFFSET ,1 );if (!s)return -ENOMEM ;/* initial empty string */
tools/lib/bpf/btf_dump.c +4 -4
@@ -166,11 +166,11 @@ static int btf_dump_resize(struct btf_dump *d)if (last_id <= d->last_id)return 0 ; -if (btf_ensure_mem ((void **)&d->type_states, &d->type_states_cap, -sizeof (*d->type_states), last_id +1 )) +if (libbpf_ensure_mem ((void **)&d->type_states, &d->type_states_cap, +sizeof (*d->type_states), last_id +1 ))return -ENOMEM ; -if (btf_ensure_mem ((void **)&d->cached_names, &d->cached_names_cap, -sizeof (*d->cached_names), last_id +1 )) +if (libbpf_ensure_mem ((void **)&d->cached_names, &d->cached_names_cap, +sizeof (*d->cached_names), last_id +1 ))return -ENOMEM ;if (d->last_id ==0 ) {
tools/lib/bpf/libbpf.c +2 -2
@@ -4867,8 +4867,8 @@ static int load_module_btfs(struct bpf_object *obj) goto err_out; } - err =btf_ensure_mem ((void **)&obj->btf_modules, &obj->btf_module_cap, -sizeof (*obj->btf_modules), obj->btf_module_cnt +1 ); + err =libbpf_ensure_mem ((void **)&obj->btf_modules, &obj->btf_module_cap, +sizeof (*obj->btf_modules), obj->btf_module_cnt +1 );if (err) goto err_out;
tools/lib/bpf/libbpf_internal.h +3 -3
@@ -112,9 +112,9 @@ struct btf_type;struct btf_type *btf_type_by_id (struct btf *btf,__u32 type_id); -void *btf_add_mem (void **data,size_t *cap_cnt,size_t elem_sz, -size_t cur_cnt,size_t max_cnt,size_t add_cnt); -int btf_ensure_mem (void **data,size_t *cap_cnt,size_t elem_sz,size_t need_cnt); +void *libbpf_add_mem (void **data,size_t *cap_cnt,size_t elem_sz, +size_t cur_cnt,size_t max_cnt,size_t add_cnt); +int libbpf_ensure_mem (void **data,size_t *cap_cnt,size_t elem_sz,size_t need_cnt); static inline bool libbpf_validate_opts(const char *opts, size_t opts_sz, size_t user_sz,
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 4/12 libbpf: extract internal set-of-strings datastructure APIs
In reply to Andrii NakryikoExtract BTF logic for maintaining a set of strings data structure, used for BTF strings section construction in writable mode, into separate re-usable API. This data structure is going to be used by bpf_linker to maintains ELF STRTAB section, which has the same layout as BTF strings section.
--- tools/lib/bpf/Build | 2 +- tools/lib/bpf/btf.c | 255 ++++++++++------------------------------- tools/lib/bpf/strset.c | 176 ++++++++++++++++++++++++++++ tools/lib/bpf/strset.h | 21 ++++ 4 files changed, 259 insertions(+), 195 deletions(-) create mode 100644 tools/lib/bpf/strset.c create mode 100644 tools/lib/bpf/strset.h
tools/lib/bpf/Build +1 -1
@@ -1,3 +1,3 @@ libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \ netlink.o bpf_prog_linfo.o libbpf_probes.o xsk.o hashmap.o \ - btf_dump.o ringbuf.o + btf_dump.o ringbuf.o strset.o
tools/lib/bpf/btf.c +61 -194
417 lines not on this page · raw message
tools/lib/bpf/strset.c new file +176 -0
176 lines not on this page · raw message
tools/lib/bpf/strset.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ + +/* Copyright (c) 2021 Facebook */ +#ifndef __LIBBPF_STRSET_H +#define __LIBBPF_STRSET_H + +#include <stdbool.h> +#include <stddef.h> + +struct strset ; + +struct strset *strset__new (size_t max_data_sz,const char *init_data,size_t init_data_sz); +void strset__free (struct strset *set); + +const char *strset__data (const struct strset *set); +size_t strset__data_size (const struct strset *set); + +int strset__find_str (struct strset *set,const char *s); +int strset__add_str (struct strset *set,const char *s); + +#endif /* __LIBBPF_STRSET_H */
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 6/12 libbpf: add BPF static linker APIs
In reply to Andrii NakryikoIntroduce BPF static linker APIs to libbpf. BPF static linker allows to perform static linking of multiple BPF object files into a single combined resulting object file, preserving all the BPF programs, maps, global variables, etc.
Data sections (.bss, .data, .rodata, .maps, maps, etc) with the same name are concatenated together. Similarly, code sections are also concatenated. All the symbols and ELF relocations are also concatenated in their respective ELF sections and are adjusted accordingly to the new object file layout.
Static variables and functions are handled correctly as well, adjusting BPF instructions offsets to reflect new variable/function offset within the combined ELF section. Such relocations are referencing STT_SECTION symbols and that stays intact.
Data sections in different files can have different alignment requirements, so that is taken care of as well, adjusting sizes and offsets as necessary to satisfy both old and new alignment requirements.
DWARF data sections are stripped out, currently. As well as LLLVM_ADDRSIG section, which is ignored by libbpf in bpf_object__open() anyways. So, in a way, BPF static linker is an analogue to llvm-strip -g, which is a pretty nice property, especially if resulting .o file is then used to generate BPF skeleton.
Original string sections are ignored and instead we construct our own set of unique strings using libbpf-internal struct strset API.
To reduce the size of the patch, all the .BTF and .BTF.ext processing was moved into a separate patch.
The high-level API consists of just 4 functions:
- bpf_linker__new() creates an instance of BPF static linker. It accepts
output filename and (currently empty) options struct;
- bpf_linker__add_file() takes input filename and appends it to the already processed ELF data; it can be called multiple times, one for each BPF ELF object file that needs to be linked in;
- bpf_linker__finalize() needs to be called to dump final ELF contents into the output file, specified when bpf_linker was created; after bpf_linker__finalize() is called, no more bpf_linker__add_file() and bpf_linker__finalize() calls are allowed, they will return error;
- regardless of whether bpf_linker__finalize() was called or not, bpf_linker__free() will free up all the used resources.
Currently, BPF static linker doesn't resolve cross-object file references (extern variables and/or functions). This will be added in the follow up patch set.
--- tools/lib/bpf/Build | 2 +- tools/lib/bpf/libbpf.c | 11 +- tools/lib/bpf/libbpf.h | 13 + tools/lib/bpf/libbpf.map | 4 + tools/lib/bpf/libbpf_internal.h | 20 + tools/lib/bpf/linker.c | 1176 +++++++++++++++++++++++++++++++ 6 files changed, 1215 insertions(+), 11 deletions(-) create mode 100644 tools/lib/bpf/linker.c
tools/lib/bpf/Build +1 -1
@@ -1,3 +1,3 @@ libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \ netlink.o bpf_prog_linfo.o libbpf_probes.o xsk.o hashmap.o \ - btf_dump.o ringbuf.o strset.o + btf_dump.o ringbuf.o strset.o linker.o
tools/lib/bpf/libbpf.c +1 -10
@@ -55,10 +55,6 @@#include "libbpf_internal.h" #include "hashmap.h" -#ifndef EM_BPF -#define EM_BPF 247 -#endif -#ifndef BPF_FS_MAGIC #define BPF_FS_MAGIC 0xcafe4a11#endif
@@ -1134,11 +1130,6 @@ static void bpf_object__elf_finish(struct bpf_object *obj) obj->efile.obj_buf_sz =0 ; } -/* if libelf is old and doesn't support mmap(), fall back to read() */ -#ifndef ELF_C_READ_MMAP -#define ELF_C_READ_MMAP ELF_C_READ -#endif -static int bpf_object__elf_init (struct bpf_object *obj) {int err =0 ;
@@ -2807,7 +2798,7 @@ static bool ignore_elf_section(GElf_Shdr *hdr, const char *name)return true;/* ignore .llvm_addrsig section as well */ -if (hdr->sh_type ==0x6FFF4C03 /* SHT_LLVM_ADDRSIG */ ) +if (hdr->sh_type ==SHT_LLVM_ADDRSIG )return true;/* no subprograms will lead to an empty .text section, ignore it */
tools/lib/bpf/libbpf.h +13 -0
@@ -760,6 +760,19 @@ enum libbpf_tristate { TRI_MODULE = 2, }; +struct bpf_linker_opts { +/* size of this struct, for forward/backward compatiblity */ +size_t sz; +}; +#define bpf_linker_opts__last_field sz + +struct bpf_linker ; + +LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts); +LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filename); +LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); +LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); + #ifdef __cplusplus }/* extern "C" */ #endif
tools/lib/bpf/libbpf.map +4 -0
@@ -355,4 +355,8 @@ LIBBPF_0.4.0 { global: btf__add_float; btf__add_type; + bpf_linker__add_file; + bpf_linker__finalize; + bpf_linker__free; + bpf_linker__new; } LIBBPF_0.3.0;
tools/lib/bpf/libbpf_internal.h +20 -0
@@ -20,6 +20,26 @@#include "libbpf.h" +#ifndef EM_BPF +#define EM_BPF 247 +#endif + +#ifndef R_BPF_64_64 +#define R_BPF_64_64 1 +#endif +#ifndef R_BPF_64_32 +#define R_BPF_64_32 10 +#endif + +#ifndef SHT_LLVM_ADDRSIG +#define SHT_LLVM_ADDRSIG 0x6FFF4C03 +#endif + +/* if libelf is old and doesn't support mmap(), fall back to read() */ +#ifndef ELF_C_READ_MMAP +#define ELF_C_READ_MMAP ELF_C_READ +#endif +#define BTF_INFO_ENC (kind, kind_flag, vlen) \ ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))#define BTF_TYPE_ENC (name, info, size_or_type) (name), (info), (size_or_type)
tools/lib/bpf/linker.c new file +1176 -0
1,176 lines not on this page · raw message
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 5/12 libbpf: add generic BTF type shallow copy API
In reply to Andrii NakryikoAdd btf__add_type() API that performs shallow copy of a given BTF type from the source BTF into the destination BTF. All the information and type IDs are preserved, but all the strings encountered are added into the destination BTF and corresponding offsets are rewritten. BTF type IDs are assumed to be correct or such that will be (somehow) modified afterwards.
--- tools/lib/bpf/btf.c | 48 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 51 insertions(+)
tools/lib/bpf/btf.c +48 -0
@@ -1629,6 +1629,54 @@ static int btf_commit_type(struct btf *btf, int data_sz)return btf->start_id + btf->nr_types -1 ; } +struct btf_pipe { +const struct btf *src; +struct btf *dst; +}; + +static int btf_rewrite_str (__u32 *str_off,void *ctx) +{ +struct btf_pipe *p = ctx; +int off; + +if (!*str_off)/* nothing to do for empty strings */ +return 0 ; + + off =btf__add_str (p->dst,btf__str_by_offset (p->src, *str_off)); +if (off <0 ) +return off; + + *str_off = off; +return 0 ; +} + +int btf__add_type (struct btf *btf,const struct btf *src_btf,const struct btf_type *src_type) +{ +struct btf_pipe p = { .src = src_btf, .dst = btf }; +struct btf_type *t; +int sz, err; + + sz =btf_type_size (src_type); +if (sz <0 ) +return sz; + +/* deconstruct BTF, if necessary, and invalidate raw_data */ +if (btf_ensure_modifiable (btf)) +return -ENOMEM ; + + t =btf_add_type_mem (btf, sz); +if (!t) +return -ENOMEM ; + +memcpy (t, src_type, sz); + + err =btf_type_visit_str_offs (t, btf_rewrite_str, &p); +if (err) +return err; + +return btf_commit_type (btf, sz); +} +/* * Append new BTF_KIND_INT type with: * - *name* - non-empty, non-NULL type name;
tools/lib/bpf/btf.h +2 -0
@@ -93,6 +93,8 @@ LIBBPF_API struct btf *libbpf_find_kernel_btf(void); LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s); +LIBBPF_API int btf__add_type(struct btf *btf, const struct btf *src_btf, +const struct btf_type *src_type); LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding); LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);
tools/lib/bpf/libbpf.map +1 -0
@@ -354,4 +354,5 @@ LIBBPF_0.3.0 { LIBBPF_0.4.0 { global: btf__add_float; + btf__add_type; } LIBBPF_0.3.0;
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 8/12 bpftool: add ability to specify custom skeleton object name
In reply to Andrii NakryikoAdd optional name OBJECT_NAME parameter to gen skeleton command to override default object name, normally derived from input file name. This allows much more flexibility during build time.
--- .../bpf/bpftool/Documentation/bpftool-gen.rst | 13 +++++---- tools/bpf/bpftool/bash-completion/bpftool | 11 +++++++- tools/bpf/bpftool/gen.c | 27 +++++++++++++++++-- 3 files changed, 43 insertions(+), 8 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-gen.rst +8 -5
@@ -19,7 +19,7 @@ SYNOPSIS GEN COMMANDS ============= -| **bpftool** **gen skeleton** *FILE* +| **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*] | **bpftool** **gen help** DESCRIPTION
@@ -75,10 +75,13 @@ DESCRIPTION specific maps, programs, etc. As part of skeleton, few custom functions are generated. - Each of them is prefixed with object name, derived from - object file name. I.e., if BPF object file name is - **example.o**, BPF object name will be **example**. The - following custom functions are provided in such case: + Each of them is prefixed with object name. Object name can + either be derived from object file name, i.e., if BPF object + file name is **example.o**, BPF object name will be + **example**. Object name can be also specified explicitly + through **name** *OBJECT_NAME* parameter. The following + custom functions are provided (assuming **example** as + the object name): - **example__open** and **example__open_opts**. These functions are used to instantiate skeleton. It
tools/bpf/bpftool/bash-completion/bpftool +10 -1
@@ -982,7 +982,16 @@ _bpftool() gen) case $command in skeleton) - _filedir + case $prev in + $command) + _filedir + return 0 + ;; + *) + _bpftool_once_attr 'name' + return 0 + ;; + esac ;; *) [[ $prev == $object ]] && \
tools/bpf/bpftool/gen.c +25 -2
@@ -288,6 +288,28 @@ static int do_skeleton(int argc, char **argv) } file =GET_ARG (); +while (argc) { +if (!REQ_ARGS (2 )) +return -1 ; + +if (is_prefix (*argv,"name" )) { +NEXT_ARG (); + +if (obj_name[0 ] !='\0' ) { +p_err ("object name already specified" ); +return -1 ; + } + +strncpy (obj_name, *argv,MAX_OBJ_NAME_LEN -1 ); + obj_name[MAX_OBJ_NAME_LEN -1 ] ='\0' ; + }else { +p_err ("unknown arg %s" , *argv); +return -1 ; + } + +NEXT_ARG (); + } +if (argc) {p_err ("extra unknown arguments" );return -1 ;
@@ -310,7 +332,8 @@ static int do_skeleton(int argc, char **argv)p_err ("failed to mmap() %s: %s" , file,strerror (errno)); goto out; } -get_obj_name (obj_name, file); +if (obj_name[0 ] =='\0' ) +get_obj_name (obj_name, file); opts.object_name = obj_name; obj =bpf_object__open_mem (obj_data, file_sz, &opts);if (IS_ERR (obj)) {
@@ -599,7 +622,7 @@ static int do_help(int argc, char **argv) }fprintf (stderr, -"Usage: %1$s %2$s skeleton FILE\n" +"Usage: %1$s %2$s skeleton FILE [name OBJECT_NAME]\n" " %1$s %2$s help\n" "\n" " " HELP_SPEC_OPTIONS "\n"
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 9/12 bpftool: add `gen object` command to perform BPF static linking
In reply to Andrii NakryikoAdd bpftool gen object <output-file> <input_file>... command to statically link multiple BPF ELF object files into a single output BPF ELF object file.
This patch also updates bash completions and man page. Man page gets a short section on gen object command, but also updates the skeleton example to show off workflow for BPF application with two .bpf.c files, compiled individually with Clang, then resulting object files are linked together with gen object, and then final object file is used to generate usable BPF skeleton. This should help new users understand realistic workflow w.r.t. compiling mutli-file BPF application.
--- .../bpf/bpftool/Documentation/bpftool-gen.rst | 65 +++++++++++++++---- tools/bpf/bpftool/bash-completion/bpftool | 6 +- tools/bpf/bpftool/gen.c | 45 ++++++++++++- 3 files changed, 100 insertions(+), 16 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-gen.rst +51 -14
@@ -14,16 +14,37 @@ SYNOPSIS *OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] } - *COMMAND* := { **skeleton** | **help** } + *COMMAND* := { **object** | **skeleton** | **help** } GEN COMMANDS ============= +| **bpftool** **gen object** *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...] | **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*] | **bpftool** **gen help** DESCRIPTION =========== + **bpftool gen object** *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...] + Statically link (combine) together one or more *INPUT_FILE*'s + into a single resulting *OUTPUT_FILE*. All the files involved + are BPF ELF object files. + + The rules of BPF static linking are mostly the same as for + user-space object files, but in addition to combining data + and instruction sections, .BTF and .BTF.ext (if present in + any of the input files) data are combined together. .BTF + data is deduplicated, so all the common types across + *INPUT_FILE*'s will only be represented once in the resulting + BTF information. + + BPF static linking allows to partition BPF source code into + individually compiled files that are then linked into + a single resulting BPF object file, which can be used to + generated BPF skeleton (with **gen skeleton** command) or + passed directly into **libbpf** (using **bpf_object__open()** + family of APIs). + **bpftool gen skeleton** *FILE* Generate BPF skeleton C header file for a given *FILE*.
@@ -133,26 +154,19 @@ OPTIONS EXAMPLES ======== -**$ cat example.c** +**$ cat example1.bpf.c** :: #include <stdbool.h> #include <linux/ptrace.h> #include <linux/bpf.h> - #include "bpf_helpers.h" + #include <bpf/bpf_helpers.h> const volatile int param1 = 42; bool global_flag = true; struct { int x; } data = {}; - struct { - __uint(type, BPF_MAP_TYPE_HASH); - __uint(max_entries, 128); - __type(key, int); - __type(value, long); - } my_map SEC(".maps"); - SEC("raw_tp/sys_enter") int handle_sys_enter(struct pt_regs *ctx) {
@@ -164,6 +178,21 @@ EXAMPLES return 0; } +**$ cat example2.bpf.c** + +:: + + #include <linux/ptrace.h> + #include <linux/bpf.h> + #include <bpf/bpf_helpers.h> + + struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 128); + __type(key, int); + __type(value, long); + } my_map SEC(".maps"); + SEC("raw_tp/sys_exit") int handle_sys_exit(struct pt_regs *ctx) {
@@ -173,9 +202,17 @@ EXAMPLES } This is example BPF application with two BPF programs and a mix of BPF maps -and global variables. +and global variables. Source code is split across two source code files. -**$ bpftool gen skeleton example.o** +**$ clang -target bpf -g example1.bpf.c -o example1.bpf.o** +**$ clang -target bpf -g example2.bpf.c -o example2.bpf.o** +**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o** + +This set of commands compiles *example1.bpf.c* and *example2.bpf.c* +individually and then statically links respective object files into the final +BPF ELF object file *example.bpf.o*. + +**$ bpftool gen skeleton example.bpf.o name example | tee example.skel.h** ::
@@ -230,7 +267,7 @@ and global variables. #endif /* __EXAMPLE_SKEL_H__ */ -**$ cat example_user.c** +**$ cat example.c** ::
@@ -273,7 +310,7 @@ and global variables. return err; } -**# ./example_user** +**# ./example** ::
tools/bpf/bpftool/bash-completion/bpftool +5 -1
@@ -981,6 +981,10 @@ _bpftool() ;; gen) case $command in + object) + _filedir + return 0 + ;; skeleton) case $prev in $command)
@@ -995,7 +999,7 @@ _bpftool() ;; *) [[ $prev == $object ]] && \ - COMPREPLY=( $( compgen -W 'skeleton help' -- "$cur" ) ) + COMPREPLY=( $( compgen -W 'object skeleton help' -- "$cur" ) ) ;; esac ;;
tools/bpf/bpftool/gen.c +44 -1
63 lines not on this page · raw message
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 11/12 selftests/bpf: pass all BPF .o's through BPF static linker
In reply to Andrii NakryikoPass all individual BPF object files (generated from progs/*.c) through bpftool gen object command to validate that BPF static linker doesn't corrupt them.
As an additional sanity checks, validate that passing resulting object files through linker again results in identical ELF files. Exact same ELF contents can be guaranteed only after two passes, as after the first pass ELF sections order changes, and thus .BTF.ext data sections order changes. That, in turn, means that strings are added into the final BTF string sections in different order, so .BTF strings data might not be exactly the same. But doing another round of linking afterwards should result in the identical ELF file, which is checked with additional diff command.
--- tools/testing/selftests/bpf/Makefile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/Makefile +6 -5
@@ -355,12 +355,13 @@ $(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \ $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \ $(TRUNNER_BPF_CFLAGS)) -$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \ - $(TRUNNER_OUTPUT)/%.o \ - $(BPFTOOL) \ - | $(TRUNNER_OUTPUT) +$(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) - $(Q)$$(BPFTOOL) gen skeleton $$< > $$@ + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked.o) $$< + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked.o) + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) + $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) + $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked.o) name $$(notdir $$(<:.o=)) > $$@ endif # ensure we set up tests.h header generation rule just once
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 7/12 libbpf: add BPF static linker BTF and BTF.ext support
In reply to Andrii NakryikoAdd .BTF and .BTF.ext static linking logic.
When multiple BPF object files are linked together, their respective .BTF and .BTF.ext sections are merged together. BTF types are not just concatenated, but also deduplicated. .BTF.ext data is grouped by type (func info, line info, core_relos) and target section names, and then all the records are concatenated together, preserving their relative order. All the BTF type ID references and string offsets are updated as necessary, to take into account possibly deduplicated strings and types.
BTF DATASEC types are handled specially. Their respective var_secinfos are accumulated separately in special per-section data and then final DATASEC types are emitted at the very end during bpf_linker__finalize() operation, just before emitting final ELF output file.
BTF data can also provide "section annotations" for some extern variables. Such concept is missing in ELF, but BTF will have DATASEC types for such special extern datasections (e.g., .kconfig, .ksyms). Such sections are called "ephemeral" internally. Internally linker will keep metadata for each such section, collecting variables information, but those sections won't be emitted into the final ELF file.
Also, given LLVM/Clang during compilation emits BTF DATASECS that are incomplete, missing section size and variable offsets for static variables, BPF static linker will initially fix up such DATASECs, using ELF symbols data. The final DATASECs will preserve section sizes and all variable offsets. This is handled correctly by libbpf already, so won't cause any new issues. On the other hand, it's actually a nice property to have a complete BTF data without runtime adjustments done during bpf_object__open() by libbpf. In that sense, BPF static linker is also a BTF normalizer.
--- tools/lib/bpf/linker.c | 769 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 767 insertions(+), 2 deletions(-)
tools/lib/bpf/linker.c +767 -2
@@ -32,12 +32,17 @@ struct src_sec {int dst_off;/* whether section is omitted from the final ELF file */ bool skipped; +/* whether section is an ephemeral section, not mapped to an ELF section */ +bool ephemeral;/* ELF info */ size_t sec_idx;Elf_Scn *scn;Elf64_Shdr *shdr;Elf_Data *data; + +/* corresponding BTF DATASEC type ID */ +int sec_type_id; };struct src_obj {
@@ -49,12 +54,24 @@ struct src_obj {/* SYMTAB section index */ size_t symtab_sec_idx; -/* List of sections. Slot zero is unused. */ +struct btf *btf; +struct btf_ext *btf_ext; + +/* List of sections (including ephemeral). Slot zero is unused. */ struct src_sec *secs;int sec_cnt;/* mapping of symbol indices from src to dst ELF */ int *sym_map; +/* mapping from the src BTF type IDs to dst ones */ +int *btf_type_map; +}; + +/* single .BTF.ext data section */ +struct btf_ext_sec_data { +size_t rec_cnt; +__u32 rec_sz; +void *recs; };struct dst_sec {
@@ -75,6 +92,15 @@ struct dst_sec {/* corresponding STT_SECTION symbol index in SYMTAB */ int sec_sym_idx; + +/* section's DATASEC variable info, emitted on BTF finalization */ +int sec_var_cnt; +struct btf_var_secinfo *sec_vars; + +/* section's .BTF.ext data */ +struct btf_ext_sec_data func_info; +struct btf_ext_sec_data line_info; +struct btf_ext_sec_data core_relo_info; };struct bpf_linker {
@@ -90,6 +116,9 @@ struct bpf_linker {struct strset *strtab_strs;/* STRTAB unique strings */ size_t strtab_sec_idx;/* STRTAB section index */ size_t symtab_sec_idx;/* SYMTAB section index */ + +struct btf *btf; +struct btf_ext *btf_ext; };#define pr_warn_elf (fmt, ...) \
@@ -101,9 +130,17 @@ static int init_output_elf(struct bpf_linker *linker, const char *file);static int linker_load_obj_file (struct bpf_linker *linker,const char *filename,struct src_obj *obj);static int linker_sanity_check_elf (struct src_obj *obj); +static int linker_sanity_check_btf (struct src_obj *obj); +static int linker_sanity_check_btf_ext (struct src_obj *obj); +static int linker_fixup_btf (struct src_obj *obj);static int linker_append_sec_data (struct bpf_linker *linker,struct src_obj *obj);static int linker_append_elf_syms (struct bpf_linker *linker,struct src_obj *obj);static int linker_append_elf_relos (struct bpf_linker *linker,struct src_obj *obj); +static int linker_append_btf (struct bpf_linker *linker,struct src_obj *obj); +static int linker_append_btf_ext (struct bpf_linker *linker,struct src_obj *obj); + +static int finalize_btf (struct bpf_linker *linker); +static int finalize_btf_ext (struct bpf_linker *linker);void bpf_linker__free (struct bpf_linker *linker) {
@@ -122,11 +159,19 @@ void bpf_linker__free(struct bpf_linker *linker)strset__free (linker->strtab_strs); +btf__free (linker->btf); +btf_ext__free (linker->btf_ext); +for (i =1 ; i < linker->sec_cnt; i++) {struct dst_sec *sec = &linker->secs[i];free (sec->sec_name);free (sec->raw_data); +free (sec->sec_vars); + +free (sec->func_info.recs); +free (sec->line_info.recs); +free (sec->core_relo_info.recs); }free (linker->secs);
@@ -335,6 +380,12 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) sec->shdr->sh_addralign =8 ; sec->shdr->sh_entsize =sizeof (Elf64_Sym); +/* .BTF */ + linker->btf =btf__new_empty (); + err =libbpf_get_error (linker->btf); +if (err) +return err; +/* add the special all-zero symbol */ init_sym =add_new_sym (linker,NULL );if (!init_sym)
@@ -362,8 +413,13 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename) err = err ?:linker_append_sec_data (linker, &obj); err = err ?:linker_append_elf_syms (linker, &obj); err = err ?:linker_append_elf_relos (linker, &obj); + err = err ?:linker_append_btf (linker, &obj); + err = err ?:linker_append_btf_ext (linker, &obj);/* free up src_obj resources */ +free (obj.btf_type_map); +btf__free (obj.btf); +btf_ext__free (obj.btf_ext);free (obj.secs);free (obj.sym_map);if (obj.elf)
@@ -555,10 +611,22 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,break ;case SHT_PROGBITS :if (strcmp (sec_name,BTF_ELF_SEC ) ==0 ) { + obj->btf =btf__new (data->d_buf, shdr->sh_size); + err =libbpf_get_error (obj->btf); +if (err) { +pr_warn ("failed to parse .BTF from %s: %d\n" , filename, err); +return err; + } sec->skipped = true;continue ; }if (strcmp (sec_name,BTF_EXT_ELF_SEC ) ==0 ) { + obj->btf_ext =btf_ext__new (data->d_buf, shdr->sh_size); + err =libbpf_get_error (obj->btf_ext); +if (err) { +pr_warn ("failed to parse .BTF.ext from '%s': %d\n" , filename, err); +return err; + } sec->skipped = true;continue ; }
@@ -580,6 +648,9 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, } err = err ?:linker_sanity_check_elf (obj); + err = err ?:linker_sanity_check_btf (obj); + err = err ?:linker_sanity_check_btf_ext (obj); + err = err ?:linker_fixup_btf (obj);return err; }
@@ -753,6 +824,69 @@ static int linker_sanity_check_elf(struct src_obj *obj)return 0 ; } +static int check_btf_type_id (__u32 *type_id,void *ctx) +{ +struct btf *btf = ctx; + +if (*type_id >btf__get_nr_types (btf)) +return -EINVAL ; + +return 0 ; +} + +static int check_btf_str_off (__u32 *str_off,void *ctx) +{ +struct btf *btf = ctx; +const char *s; + + s =btf__str_by_offset (btf, *str_off); + +if (!s) +return -EINVAL ; + +return 0 ; +} + +static int linker_sanity_check_btf (struct src_obj *obj) +{ +struct btf_type *t; +int i, n, err =0 ; + +if (!obj->btf) +return 0 ; + + n =btf__get_nr_types (obj->btf); +for (i =1 ; i <= n; i++) { + t =btf_type_by_id (obj->btf, i); + + err = err ?:btf_type_visit_type_ids (t, check_btf_type_id, obj->btf); + err = err ?:btf_type_visit_str_offs (t, check_btf_str_off, obj->btf); +if (err) +return err; + } + +return 0 ; +} + +static int linker_sanity_check_btf_ext (struct src_obj *obj) +{ +int err =0 ; + +if (!obj->btf_ext) +return 0 ; + +/* can't use .BTF.ext without .BTF */ +if (!obj->btf) +return -EINVAL ; + + err = err ?:btf_ext_visit_type_ids (obj->btf_ext, check_btf_type_id, obj->btf); + err = err ?:btf_ext_visit_str_offs (obj->btf_ext, check_btf_str_off, obj->btf); +if (err) +return err; + +return 0 ; +} +static int init_sec (struct bpf_linker *linker,struct dst_sec *dst_sec,struct src_sec *src_sec) {Elf_Scn *scn;
@@ -763,6 +897,10 @@ static int init_sec(struct bpf_linker *linker, struct dst_sec *dst_sec, struct s dst_sec->sec_sz =0 ; dst_sec->sec_idx =0 ; +/* ephemeral sections are just thin section shells lacking most parts */ +if (src_sec->ephemeral) +return 0 ; + scn =elf_newscn (linker->elf);if (!scn)return -1 ;
@@ -891,12 +1029,15 @@ static bool is_data_sec(struct src_sec *sec) {if (!sec || sec->skipped)return false; +/* ephemeral sections are data sections, e.g., .kconfig, .ksyms */ +if (sec->ephemeral) +return true;return sec->shdr->sh_type ==SHT_PROGBITS || sec->shdr->sh_type ==SHT_NOBITS ; }static bool is_relo_sec (struct src_sec *sec) { -if (!sec || sec->skipped) +if (!sec || sec->skipped || sec->ephemeral)return false;return sec->shdr->sh_type ==SHT_REL ; }
@@ -945,6 +1086,9 @@ static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj/* record mapped section index */ src_sec->dst_id = dst_sec->id; +if (src_sec->ephemeral) +continue ; + err =extend_sec (dst_sec, src_sec);if (err)return err;
@@ -1120,6 +1264,339 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obreturn 0 ; } +static struct src_sec *find_src_sec_by_name (struct src_obj *obj,const char *sec_name) +{ +struct src_sec *sec; +int i; + +for (i =1 ; i < obj->sec_cnt; i++) { + sec = &obj->secs[i]; + +if (strcmp (sec->sec_name, sec_name) ==0 ) +return sec; + } + +return NULL ; +} + +static Elf64_Sym *find_sym_by_name (struct src_obj *obj,size_t sec_idx, +int sym_type,const char *sym_name) +{ +struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx]; +Elf64_Sym *sym = symtab->data->d_buf; +int i, n = symtab->shdr->sh_size / symtab->shdr->sh_entsize; +int str_sec_idx = symtab->shdr->sh_link; +const char *name; + +for (i =0 ; i < n; i++, sym++) { +if (sym->st_shndx != sec_idx) +continue ; +if (ELF64_ST_TYPE (sym->st_info) != sym_type) +continue ; + + name =elf_strptr (obj->elf, str_sec_idx, sym->st_name); +if (!name) +return NULL ; + +if (strcmp (sym_name, name) !=0 ) +continue ; + +return sym; + } + +return NULL ; +} + +static int linker_fixup_btf (struct src_obj *obj) +{ +const char *sec_name; +struct src_sec *sec; +int i, j, n, m; + + n =btf__get_nr_types (obj->btf); +for (i =1 ; i <= n; i++) { +struct btf_var_secinfo *vi; +struct btf_type *t; + + t =btf_type_by_id (obj->btf, i); +if (btf_kind (t) !=BTF_KIND_DATASEC ) +continue ; + + sec_name =btf__str_by_offset (obj->btf, t->name_off); + sec =find_src_sec_by_name (obj, sec_name); +if (sec) { +/* record actual section size, unless ephemeral */ +if (sec->shdr) + t->size = sec->shdr->sh_size; + }else { +/* BTF can have some sections that are not represented +* in ELF, e.g., .kconfig and .ksyms, which are used +* for special extern variables. Here we'll +* pre-create "section shells" for them to be able to +* keep track of extra per-section metadata later +* (e.g., BTF variables). +*/ + sec =add_src_sec (obj, sec_name); +if (!sec) +return -ENOMEM ; + + sec->ephemeral = true; + sec->sec_idx =0 ;/* will match UNDEF shndx in ELF */ + } + +/* remember ELF section and its BTF type ID match */ + sec->sec_type_id = i; + +/* fix up variable offsets */ + vi =btf_var_secinfos (t); +for (j =0 , m =btf_vlen (t); j < m; j++, vi++) { +const struct btf_type *vt =btf__type_by_id (obj->btf, vi->type); +const char *var_name =btf__str_by_offset (obj->btf, vt->name_off); +int var_linkage =btf_var (vt)->linkage; +Elf64_Sym *sym; + +/* no need to patch up static or extern vars */ +if (var_linkage !=BTF_VAR_GLOBAL_ALLOCATED ) +continue ; + + sym =find_sym_by_name (obj, sec->sec_idx,STT_OBJECT , var_name); +if (!sym) { +pr_warn ("failed to find symbol for variable '%s' in section '%s'\n" , var_name, sec_name); +return -ENOENT ; + } + + vi->offset = sym->st_value; + } + } + +return 0 ; +} + +static int remap_type_id (__u32 *type_id,void *ctx) +{ +int *id_map = ctx; + + *type_id = id_map[*type_id]; + +return 0 ; +} + +static int linker_append_btf (struct bpf_linker *linker,struct src_obj *obj) +{ +const struct btf_type *t; +int i, j, n, start_id, id; + +if (!obj->btf) +return 0 ; + + start_id =btf__get_nr_types (linker->btf) +1 ; + n =btf__get_nr_types (obj->btf); + + obj->btf_type_map =calloc (n +1 ,sizeof (int )); +if (!obj->btf_type_map) +return -ENOMEM ; + +for (i =1 ; i <= n; i++) { + t =btf__type_by_id (obj->btf, i); + +/* DATASECs are handled specially below */ +if (btf_kind (t) ==BTF_KIND_DATASEC ) +continue ; + + id =btf__add_type (linker->btf, obj->btf, t); +if (id <0 ) { +pr_warn ("failed to append BTF type #%d from file '%s'\n" , i, obj->filename); +return id; + } + + obj->btf_type_map[i] = id; + } + +/* remap all the types except DATASECs */ + n =btf__get_nr_types (linker->btf); +for (i = start_id; i <= n; i++) { +struct btf_type *dst_t =btf_type_by_id (linker->btf, i); + +if (btf_type_visit_type_ids (dst_t, remap_type_id, obj->btf_type_map)) +return -EINVAL ; + } + +/* append DATASEC info */ +for (i =1 ; i < obj->sec_cnt; i++) { +struct src_sec *src_sec; +struct dst_sec *dst_sec; +const struct btf_var_secinfo *src_var; +struct btf_var_secinfo *dst_var; + + src_sec = &obj->secs[i]; +if (!src_sec->sec_type_id || src_sec->skipped) +continue ; + dst_sec = &linker->secs[src_sec->dst_id]; + + t =btf__type_by_id (obj->btf, src_sec->sec_type_id); + src_var =btf_var_secinfos (t); + n =btf_vlen (t); +for (j =0 ; j < n; j++, src_var++) { +void *sec_vars = dst_sec->sec_vars; + + sec_vars =libbpf_reallocarray (sec_vars, + dst_sec->sec_var_cnt +1 , +sizeof (*dst_sec->sec_vars)); +if (!sec_vars) +return -ENOMEM ; + + dst_sec->sec_vars = sec_vars; + dst_sec->sec_var_cnt++; + + dst_var = &dst_sec->sec_vars[dst_sec->sec_var_cnt -1 ]; + dst_var->type = obj->btf_type_map[src_var->type]; + dst_var->size = src_var->size; + dst_var->offset = src_sec->dst_off + src_var->offset; + } + } + +return 0 ; +} + +static void *add_btf_ext_rec (struct btf_ext_sec_data *ext_data,const void *src_rec) +{ +void *tmp; + + tmp =libbpf_reallocarray (ext_data->recs, ext_data->rec_cnt +1 , ext_data->rec_sz); +if (!tmp) +return NULL ; + ext_data->recs = tmp; + + tmp += ext_data->rec_cnt * ext_data->rec_sz; +memcpy (tmp, src_rec, ext_data->rec_sz); + + ext_data->rec_cnt++; + +return tmp; +} + +static int linker_append_btf_ext (struct bpf_linker *linker,struct src_obj *obj) +{ +const struct btf_ext_info_sec *ext_sec; +const char *sec_name, *s; +struct src_sec *src_sec; +struct dst_sec *dst_sec; +int rec_sz, str_off, i; + +if (!obj->btf_ext) +return 0 ; + + rec_sz = obj->btf_ext->func_info.rec_size; +for_each_btf_ext_sec (&obj->btf_ext->func_info, ext_sec) { +struct bpf_func_info_min *src_rec, *dst_rec; + + sec_name =btf__name_by_offset (obj->btf, ext_sec->sec_name_off); + src_sec =find_src_sec_by_name (obj, sec_name); +if (!src_sec) { +pr_warn ("can't find section '%s' referenced from .BTF.ext\n" , sec_name); +return -EINVAL ; + } + dst_sec = &linker->secs[src_sec->dst_id]; + +if (dst_sec->func_info.rec_sz ==0 ) + dst_sec->func_info.rec_sz = rec_sz; +if (dst_sec->func_info.rec_sz != rec_sz) { +pr_warn ("incompatible .BTF.ext record sizes for section '%s'\n" , sec_name); +return -EINVAL ; + } + +for_each_btf_ext_rec (&obj->btf_ext->func_info, ext_sec, i, src_rec) { + dst_rec =add_btf_ext_rec (&dst_sec->func_info, src_rec); +if (!dst_rec) +return -ENOMEM ; + + dst_rec->insn_off += src_sec->dst_off; + dst_rec->type_id = obj->btf_type_map[dst_rec->type_id]; + } + } + + rec_sz = obj->btf_ext->line_info.rec_size; +for_each_btf_ext_sec (&obj->btf_ext->line_info, ext_sec) { +struct bpf_line_info_min *src_rec, *dst_rec; + + sec_name =btf__name_by_offset (obj->btf, ext_sec->sec_name_off); + src_sec =find_src_sec_by_name (obj, sec_name); +if (!src_sec) { +pr_warn ("can't find section '%s' referenced from .BTF.ext\n" , sec_name); +return -EINVAL ; + } + dst_sec = &linker->secs[src_sec->dst_id]; + +if (dst_sec->line_info.rec_sz ==0 ) + dst_sec->line_info.rec_sz = rec_sz; +if (dst_sec->line_info.rec_sz != rec_sz) { +pr_warn ("incompatible .BTF.ext record sizes for section '%s'\n" , sec_name); +return -EINVAL ; + } + +for_each_btf_ext_rec (&obj->btf_ext->line_info, ext_sec, i, src_rec) { + dst_rec =add_btf_ext_rec (&dst_sec->line_info, src_rec); +if (!dst_rec) +return -ENOMEM ; + + dst_rec->insn_off += src_sec->dst_off; + + s =btf__str_by_offset (obj->btf, src_rec->file_name_off); + str_off =btf__add_str (linker->btf, s); +if (str_off <0 ) +return -ENOMEM ; + dst_rec->file_name_off = str_off; + + s =btf__str_by_offset (obj->btf, src_rec->line_off); + str_off =btf__add_str (linker->btf, s); +if (str_off <0 ) +return -ENOMEM ; + dst_rec->line_off = str_off; + +/* dst_rec->line_col is fine */ + } + } + + rec_sz = obj->btf_ext->core_relo_info.rec_size; +for_each_btf_ext_sec (&obj->btf_ext->core_relo_info, ext_sec) { +struct bpf_core_relo *src_rec, *dst_rec; + + sec_name =btf__name_by_offset (obj->btf, ext_sec->sec_name_off); + src_sec =find_src_sec_by_name (obj, sec_name); +if (!src_sec) { +pr_warn ("can't find section '%s' referenced from .BTF.ext\n" , sec_name); +return -EINVAL ; + } + dst_sec = &linker->secs[src_sec->dst_id]; + +if (dst_sec->core_relo_info.rec_sz ==0 ) + dst_sec->core_relo_info.rec_sz = rec_sz; +if (dst_sec->core_relo_info.rec_sz != rec_sz) { +pr_warn ("incompatible .BTF.ext record sizes for section '%s'\n" , sec_name); +return -EINVAL ; + } + +for_each_btf_ext_rec (&obj->btf_ext->core_relo_info, ext_sec, i, src_rec) { + dst_rec =add_btf_ext_rec (&dst_sec->core_relo_info, src_rec); +if (!dst_rec) +return -ENOMEM ; + + dst_rec->insn_off += src_sec->dst_off; + dst_rec->type_id = obj->btf_type_map[dst_rec->type_id]; + + s =btf__str_by_offset (obj->btf, src_rec->access_str_off); + str_off =btf__add_str (linker->btf, s); +if (str_off <0 ) +return -ENOMEM ; + dst_rec->access_str_off = str_off; + +/* dst_rec->kind is fine */ + } + } + +return 0 ; +} +int bpf_linker__finalize (struct bpf_linker *linker) {struct dst_sec *sec;
@@ -1130,6 +1607,10 @@ int bpf_linker__finalize(struct bpf_linker *linker)if (!linker->elf)return -EINVAL ; + err =finalize_btf (linker); +if (err) +return err; +/* Finalize strings */ strs_sz =strset__data_size (linker->strtab_strs); strs =strset__data (linker->strtab_strs);
@@ -1149,6 +1630,10 @@ int bpf_linker__finalize(struct bpf_linker *linker)if (sec->sec_idx == linker->strtab_sec_idx)continue ; +/* special ephemeral sections (.ksyms, .kconfig, etc) */ +if (!sec->scn) +continue ; + sec->data->d_buf = sec->raw_data; }
@@ -1174,3 +1659,283 @@ int bpf_linker__finalize(struct bpf_linker *linker)return 0 ; } + +static int emit_elf_data_sec (struct bpf_linker *linker,const char *sec_name, +size_t align,const void *raw_data,size_t raw_sz) +{ +Elf_Scn *scn; +Elf_Data *data; +Elf64_Shdr *shdr; +int name_off; + + name_off =strset__add_str (linker->strtab_strs, sec_name); +if (name_off <0 ) +return name_off; + + scn =elf_newscn (linker->elf); +if (!scn) +return -ENOMEM ; + data =elf_newdata (scn); +if (!data) +return -ENOMEM ; + shdr =elf64_getshdr (scn); +if (!shdr) +return -EINVAL ; + + shdr->sh_name = name_off; + shdr->sh_type =SHT_PROGBITS ; + shdr->sh_flags =0 ; + shdr->sh_size = raw_sz; + shdr->sh_link =0 ; + shdr->sh_info =0 ; + shdr->sh_addralign = align; + shdr->sh_entsize =0 ; + + data->d_type =ELF_T_BYTE ; + data->d_size = raw_sz; + data->d_buf = (void *)raw_data; + data->d_align = align; + data->d_off =0 ; + +return 0 ; +} + +static int finalize_btf (struct bpf_linker *linker) +{ +struct btf *btf = linker->btf; +const void *raw_data; +int i, j, id, err; +__u32 raw_sz; + +/* bail out if no BTF data was produced */ +if (btf__get_nr_types (linker->btf) ==0 ) +return 0 ; + +for (i =1 ; i < linker->sec_cnt; i++) { +struct dst_sec *sec = &linker->secs[i]; + +if (!sec->sec_var_cnt) +continue ; + + id =btf__add_datasec (btf, sec->sec_name, sec->sec_sz); +if (id <0 ) { +pr_warn ("failed to add consolidated BTF type for datasec '%s': %d\n" , + sec->sec_name, id); +return id; + } + +for (j =0 ; j < sec->sec_var_cnt; j++) { +struct btf_var_secinfo *vi = &sec->sec_vars[j]; + +if (btf__add_datasec_var_info (btf, vi->type, vi->offset, vi->size)) +return -EINVAL ; + } + } + + err =finalize_btf_ext (linker); +if (err) { +pr_warn (".BTF.ext generation failed: %d\n" , err); +return err; + } + + err =btf__dedup (linker->btf, linker->btf_ext,NULL ); +if (err) { +pr_warn ("BTF dedup failed: %d\n" , err); +return err; + } + +/* Emit .BTF section */ + raw_data =btf__get_raw_data (linker->btf, &raw_sz); +if (!raw_data) +return -ENOMEM ; + + err =emit_elf_data_sec (linker,BTF_ELF_SEC ,8 , raw_data, raw_sz); +if (err) { +pr_warn ("failed to write out .BTF ELF section: %d\n" , err); +return err; + } + +/* Emit .BTF.ext section */ +if (linker->btf_ext) { + raw_data =btf_ext__get_raw_data (linker->btf_ext, &raw_sz); +if (!raw_data) +return -ENOMEM ; + + err =emit_elf_data_sec (linker,BTF_EXT_ELF_SEC ,8 , raw_data, raw_sz); +if (err) { +pr_warn ("failed to write out .BTF.ext ELF section: %d\n" , err); +return err; + } + } + +return 0 ; +} + +static int emit_btf_ext_data (struct bpf_linker *linker,void *output, +const char *sec_name,struct btf_ext_sec_data *sec_data) +{ +struct btf_ext_info_sec *sec_info; +void *cur = output; +int str_off; +size_t sz; + +if (!sec_data->rec_cnt) +return 0 ; + + str_off =btf__add_str (linker->btf, sec_name); +if (str_off <0 ) +return -ENOMEM ; + + sec_info = cur; + sec_info->sec_name_off = str_off; + sec_info->num_info = sec_data->rec_cnt; + cur +=sizeof (struct btf_ext_info_sec ); + + sz = sec_data->rec_cnt * sec_data->rec_sz; +memcpy (cur, sec_data->recs, sz); + cur += sz; + +return cur - output; +} + +static int finalize_btf_ext (struct bpf_linker *linker) +{ +size_t funcs_sz =0 , lines_sz =0 , core_relos_sz =0 , total_sz =0 ; +size_t func_rec_sz =0 , line_rec_sz =0 , core_relo_rec_sz =0 ; +struct btf_ext_header *hdr; +void *data, *cur; +int i, err, sz; + +/* validate that all sections have the same .BTF.ext record sizes +* and calculate total data size for each type of data (func info, +* line info, core relos) +*/ +for (i =1 ; i < linker->sec_cnt; i++) { +struct dst_sec *sec = &linker->secs[i]; + +if (sec->func_info.rec_cnt) { +if (func_rec_sz ==0 ) + func_rec_sz = sec->func_info.rec_sz; +if (func_rec_sz != sec->func_info.rec_sz) { +pr_warn ("mismatch in func_info record size %zu != %u\n" , + func_rec_sz, sec->func_info.rec_sz); +return -EINVAL ; + } + + funcs_sz +=sizeof (struct btf_ext_info_sec ) + func_rec_sz * sec->func_info.rec_cnt; + } +if (sec->line_info.rec_cnt) { +if (line_rec_sz ==0 ) + line_rec_sz = sec->line_info.rec_sz; +if (line_rec_sz != sec->line_info.rec_sz) { +pr_warn ("mismatch in line_info record size %zu != %u\n" , + line_rec_sz, sec->line_info.rec_sz); +return -EINVAL ; + } + + lines_sz +=sizeof (struct btf_ext_info_sec ) + line_rec_sz * sec->line_info.rec_cnt; + } +if (sec->core_relo_info.rec_cnt) { +if (core_relo_rec_sz ==0 ) + core_relo_rec_sz = sec->core_relo_info.rec_sz; +if (core_relo_rec_sz != sec->core_relo_info.rec_sz) { +pr_warn ("mismatch in core_relo_info record size %zu != %u\n" , + core_relo_rec_sz, sec->core_relo_info.rec_sz); +return -EINVAL ; + } + + core_relos_sz +=sizeof (struct btf_ext_info_sec ) + core_relo_rec_sz * sec->core_relo_info.rec_cnt; + } + } + +if (!funcs_sz && !lines_sz && !core_relos_sz) +return 0 ; + + total_sz +=sizeof (struct btf_ext_header ); +if (funcs_sz) { + funcs_sz +=sizeof (__u32);/* record size prefix */ + total_sz += funcs_sz; + } +if (lines_sz) { + lines_sz +=sizeof (__u32);/* record size prefix */ + total_sz += lines_sz; + } +if (core_relos_sz) { + core_relos_sz +=sizeof (__u32);/* record size prefix */ + total_sz += core_relos_sz; + } + + cur = data =calloc (1 , total_sz); +if (!data) +return -ENOMEM ; + + hdr = cur; + hdr->magic =BTF_MAGIC ; + hdr->version =BTF_VERSION ; + hdr->flags =0 ; + hdr->hdr_len =sizeof (struct btf_ext_header ); + cur +=sizeof (struct btf_ext_header ); + +/* All offsets are in bytes relative to the end of this header */ + hdr->func_info_off =0 ; + hdr->func_info_len = funcs_sz; + hdr->line_info_off = funcs_sz; + hdr->line_info_len = lines_sz; + hdr->core_relo_off = funcs_sz + lines_sz;; + hdr->core_relo_len = core_relos_sz; + +if (funcs_sz) { + *(__u32 *)cur = func_rec_sz; + cur +=sizeof (__u32); + +for (i =1 ; i < linker->sec_cnt; i++) { +struct dst_sec *sec = &linker->secs[i]; + + sz =emit_btf_ext_data (linker, cur, sec->sec_name, &sec->func_info); +if (sz <0 ) +return sz; + + cur += sz; + } + } + +if (lines_sz) { + *(__u32 *)cur = line_rec_sz; + cur +=sizeof (__u32); + +for (i =1 ; i < linker->sec_cnt; i++) { +struct dst_sec *sec = &linker->secs[i]; + + sz =emit_btf_ext_data (linker, cur, sec->sec_name, &sec->line_info); +if (sz <0 ) +return sz; + + cur += sz; + } + } + +if (core_relos_sz) { + *(__u32 *)cur = core_relo_rec_sz; + cur +=sizeof (__u32); + +for (i =1 ; i < linker->sec_cnt; i++) { +struct dst_sec *sec = &linker->secs[i]; + + sz =emit_btf_ext_data (linker, cur, sec->sec_name, &sec->core_relo_info); +if (sz <0 ) +return sz; + + cur += sz; + } + } + + linker->btf_ext =btf_ext__new (data, total_sz); + err =libbpf_get_error (linker->btf_ext); +if (err) { + linker->btf_ext =NULL ; +pr_warn ("failed to parse final .BTF.ext data: %d\n" , err); +return err; + } + +return 0 ; +}
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 10/12 selftests/bpf: re-generate vmlinux.h and BPF skeletons if bpftool changed
In reply to Andrii NakryikoTrigger vmlinux.h and BPF skeletons re-generation if detected that bpftool was re-compiled. Otherwise full make clean is required to get updated skeletons, if bpftool is modified.
--- tools/testing/selftests/bpf/Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +3 -2
@@ -232,7 +232,7 @@ $(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \ DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers endif -$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) | $(BPFTOOL) $(INCLUDE_DIR) +$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR) ifeq ($(VMLINUX_H),) $(call msg,GEN,,$@) $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
@@ -357,7 +357,8 @@ $(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \ $(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h: \ $(TRUNNER_OUTPUT)/%.o \ - | $(BPFTOOL) $(TRUNNER_OUTPUT) + $(BPFTOOL) \ + | $(TRUNNER_OUTPUT) $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) $(Q)$$(BPFTOOL) gen skeleton $$< > $$@ endif
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v3 12/12 selftests/bpf: add multi-file statically linked BPF object file test
In reply to Andrii NakryikoAdd Makefile infra to specify multi-file BPF object files (and derivative skeletons). Add first selftest validating BPF static linker can merge together successfully two independent BPF object files and resulting object and skeleton are correct and usable.
Use the same F(F(F(X))) = F(F(X)) identity test on linked object files as for the case of single BPF object files.
--- tools/testing/selftests/bpf/Makefile | 21 ++++++++-- .../selftests/bpf/prog_tests/static_linked.c | 40 +++++++++++++++++++ .../selftests/bpf/progs/test_static_linked1.c | 30 ++++++++++++++ .../selftests/bpf/progs/test_static_linked2.c | 31 ++++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/static_linked.c create mode 100644 tools/testing/selftests/bpf/progs/test_static_linked1.c create mode 100644 tools/testing/selftests/bpf/progs/test_static_linked2.c
tools/testing/selftests/bpf/Makefile +18 -3
@@ -303,6 +303,10 @@ endef SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c +LINKED_SKELS := test_static_linked.skel.h + +test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o + # Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on # $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. # Parameters:
@@ -323,6 +327,7 @@ TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS) TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ $$(filter-out $(SKEL_BLACKLIST), \ $$(TRUNNER_BPF_SRCS))) +TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS)) TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS) # Evaluate rules now with extra TRUNNER_XXX variables above already defined
@@ -357,11 +362,20 @@ $(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \ $(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) - $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked.o) $$< - $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked.o) + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$< + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o) $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) - $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked.o) name $$(notdir $$(<:.o=)) > $$@ + $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@ + +$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT) + $$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.o)) + $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps)) + $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o) + $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o) + $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) + $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) + $(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@ endif # ensure we set up tests.h header generation rule just once
@@ -383,6 +397,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ $(TRUNNER_EXTRA_HDRS) \ $(TRUNNER_BPF_OBJS) \ $(TRUNNER_BPF_SKELS) \ + $(TRUNNER_BPF_SKELS_LINKED) \ $$(BPFOBJ) | $(TRUNNER_OUTPUT) $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
tools/testing/selftests/bpf/prog_tests/static_linked.c new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */ + +#include <test_progs.h> +#include "test_static_linked.skel.h" + +void test_static_linked (void ) +{ +int err; +struct test_static_linked * skel; + + skel =test_static_linked__open (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; + + skel->rodata->rovar1 =1 ; + skel->bss->static_var1 =2 ; + skel->bss->static_var11 =3 ; + + skel->rodata->rovar2 =4 ; + skel->bss->static_var2 =5 ; + skel->bss->static_var22 =6 ; + + err =test_static_linked__load (skel); +if (!ASSERT_OK (err,"skel_load" )) + goto cleanup; + + err =test_static_linked__attach (skel); +if (!ASSERT_OK (err,"skel_attach" )) + goto cleanup; + +/* trigger */ +usleep (1 ); + +ASSERT_EQ (skel->bss->var1,1 *2 +2 +3 ,"var1" ); +ASSERT_EQ (skel->bss->var2,4 *3 +5 +6 ,"var2" ); + +cleanup: +test_static_linked__destroy (skel); +}
tools/testing/selftests/bpf/progs/test_static_linked1.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +/* 8-byte aligned .bss */ +static volatile long static_var1; +static volatile int static_var11; +int var1 =0 ; +/* 4-byte aligned .rodata */ +const volatile int rovar1; + +/* same "subprog" name in both files */ +static __noinline int subprog(int x) +{ +/* but different formula */ +return x *2 ; +} + +SEC("raw_tp/sys_enter") +int handler1 (const void *ctx) +{ + var1 =subprog (rovar1) + static_var1 + static_var11; + +return 0 ; +} + +char LICENSE[] SEC("license") = "GPL"; +int VERSION SEC("version") = 1;
tools/testing/selftests/bpf/progs/test_static_linked2.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +/* 4-byte aligned .bss */ +static volatile int static_var2; +static volatile int static_var22; +int var2 =0 ; +/* 8-byte aligned .rodata */ +const volatile long rovar2; + +/* same "subprog" name in both files */ +static __noinline int subprog(int x) +{ +/* but different formula */ +return x *3 ; +} + +SEC("raw_tp/sys_enter") +int handler2 (const void *ctx) +{ + var2 =subprog (rovar2) + static_var2 + static_var22; + +return 0 ; +} + +/* different name and/or type of the variable doesn't matter */ +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = 1;
Signature · 1 line
2.30.2