Commit 59f2a29c authored by Andrii Nakryiko's avatar Andrii Nakryiko

Merge branch 'libbpf: Add btf__type_cnt() and btf__raw_data() APIs'

Hengqi Chen says:

====================

Add btf__type_cnt() and btf__raw_data() APIs and deprecate
btf__get_nr_type() and btf__get_raw_data() since the old APIs
don't follow libbpf naming convention. Also update tools/selftests
to use these new APIs. This is part of effort towards libbpf v1.0

v1->v2:
 - Update commit message, deprecate the old APIs in libbpf v0.7 (Andrii)
 - Separate changes in tools/ to individual patches (Andrii)
====================
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents 1000298c 487ef148
...@@ -329,7 +329,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id, ...@@ -329,7 +329,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
printf("\n\ttype_id=%u offset=%u size=%u", printf("\n\ttype_id=%u offset=%u size=%u",
v->type, v->offset, v->size); v->type, v->offset, v->size);
if (v->type <= btf__get_nr_types(btf)) { if (v->type < btf__type_cnt(btf)) {
vt = btf__type_by_id(btf, v->type); vt = btf__type_by_id(btf, v->type);
printf(" (%s '%s')", printf(" (%s '%s')",
btf_kind_str[btf_kind_safe(btf_kind(vt))], btf_kind_str[btf_kind_safe(btf_kind(vt))],
...@@ -390,14 +390,14 @@ static int dump_btf_raw(const struct btf *btf, ...@@ -390,14 +390,14 @@ static int dump_btf_raw(const struct btf *btf,
} }
} else { } else {
const struct btf *base; const struct btf *base;
int cnt = btf__get_nr_types(btf); int cnt = btf__type_cnt(btf);
int start_id = 1; int start_id = 1;
base = btf__base_btf(btf); base = btf__base_btf(btf);
if (base) if (base)
start_id = btf__get_nr_types(base) + 1; start_id = btf__type_cnt(base);
for (i = start_id; i <= cnt; i++) { for (i = start_id; i < cnt; i++) {
t = btf__type_by_id(btf, i); t = btf__type_by_id(btf, i);
dump_btf_type(btf, i, t); dump_btf_type(btf, i, t);
} }
...@@ -440,9 +440,9 @@ static int dump_btf_c(const struct btf *btf, ...@@ -440,9 +440,9 @@ static int dump_btf_c(const struct btf *btf,
goto done; goto done;
} }
} else { } else {
int cnt = btf__get_nr_types(btf); int cnt = btf__type_cnt(btf);
for (i = 1; i <= cnt; i++) { for (i = 1; i < cnt; i++) {
err = btf_dump__dump_type(d, i); err = btf_dump__dump_type(d, i);
if (err) if (err)
goto done; goto done;
......
...@@ -211,7 +211,7 @@ static int codegen_datasec_def(struct bpf_object *obj, ...@@ -211,7 +211,7 @@ static int codegen_datasec_def(struct bpf_object *obj,
static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
{ {
struct btf *btf = bpf_object__btf(obj); struct btf *btf = bpf_object__btf(obj);
int n = btf__get_nr_types(btf); int n = btf__type_cnt(btf);
struct btf_dump *d; struct btf_dump *d;
struct bpf_map *map; struct bpf_map *map;
const struct btf_type *sec; const struct btf_type *sec;
...@@ -233,7 +233,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) ...@@ -233,7 +233,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
continue; continue;
sec = NULL; sec = NULL;
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
const struct btf_type *t = btf__type_by_id(btf, i); const struct btf_type *t = btf__type_by_id(btf, i);
const char *name; const char *name;
......
...@@ -502,12 +502,12 @@ static int symbols_resolve(struct object *obj) ...@@ -502,12 +502,12 @@ static int symbols_resolve(struct object *obj)
} }
err = -1; err = -1;
nr_types = btf__get_nr_types(btf); nr_types = btf__type_cnt(btf);
/* /*
* Iterate all the BTF types and search for collected symbol IDs. * Iterate all the BTF types and search for collected symbol IDs.
*/ */
for (type_id = 1; type_id <= nr_types; type_id++) { for (type_id = 1; type_id < nr_types; type_id++) {
const struct btf_type *type; const struct btf_type *type;
struct rb_root *root; struct rb_root *root;
struct btf_id *id; struct btf_id *id;
......
...@@ -57,7 +57,7 @@ struct btf { ...@@ -57,7 +57,7 @@ struct btf {
* representation is broken up into three independently allocated * representation is broken up into three independently allocated
* memory regions to be able to modify them independently. * memory regions to be able to modify them independently.
* raw_data is nulled out at that point, but can be later allocated * raw_data is nulled out at that point, but can be later allocated
* and cached again if user calls btf__get_raw_data(), at which point * and cached again if user calls btf__raw_data(), at which point
* raw_data will contain a contiguous copy of header, types, and * raw_data will contain a contiguous copy of header, types, and
* strings: * strings:
* *
...@@ -435,6 +435,11 @@ __u32 btf__get_nr_types(const struct btf *btf) ...@@ -435,6 +435,11 @@ __u32 btf__get_nr_types(const struct btf *btf)
return btf->start_id + btf->nr_types - 1; return btf->start_id + btf->nr_types - 1;
} }
__u32 btf__type_cnt(const struct btf *btf)
{
return btf->start_id + btf->nr_types;
}
const struct btf *btf__base_btf(const struct btf *btf) const struct btf *btf__base_btf(const struct btf *btf)
{ {
return btf->base_btf; return btf->base_btf;
...@@ -466,8 +471,8 @@ static int determine_ptr_size(const struct btf *btf) ...@@ -466,8 +471,8 @@ static int determine_ptr_size(const struct btf *btf)
if (btf->base_btf && btf->base_btf->ptr_sz > 0) if (btf->base_btf && btf->base_btf->ptr_sz > 0)
return btf->base_btf->ptr_sz; return btf->base_btf->ptr_sz;
n = btf__get_nr_types(btf); n = btf__type_cnt(btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
t = btf__type_by_id(btf, i); t = btf__type_by_id(btf, i);
if (!btf_is_int(t)) if (!btf_is_int(t))
continue; continue;
...@@ -684,12 +689,12 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id) ...@@ -684,12 +689,12 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id)
__s32 btf__find_by_name(const struct btf *btf, const char *type_name) __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
{ {
__u32 i, nr_types = btf__get_nr_types(btf); __u32 i, nr_types = btf__type_cnt(btf);
if (!strcmp(type_name, "void")) if (!strcmp(type_name, "void"))
return 0; return 0;
for (i = 1; i <= nr_types; i++) { for (i = 1; i < nr_types; i++) {
const struct btf_type *t = btf__type_by_id(btf, i); const struct btf_type *t = btf__type_by_id(btf, i);
const char *name = btf__name_by_offset(btf, t->name_off); const char *name = btf__name_by_offset(btf, t->name_off);
...@@ -703,12 +708,12 @@ __s32 btf__find_by_name(const struct btf *btf, const char *type_name) ...@@ -703,12 +708,12 @@ __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id, static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id,
const char *type_name, __u32 kind) const char *type_name, __u32 kind)
{ {
__u32 i, nr_types = btf__get_nr_types(btf); __u32 i, nr_types = btf__type_cnt(btf);
if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void")) if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
return 0; return 0;
for (i = start_id; i <= nr_types; i++) { for (i = start_id; i < nr_types; i++) {
const struct btf_type *t = btf__type_by_id(btf, i); const struct btf_type *t = btf__type_by_id(btf, i);
const char *name; const char *name;
...@@ -781,7 +786,7 @@ static struct btf *btf_new_empty(struct btf *base_btf) ...@@ -781,7 +786,7 @@ static struct btf *btf_new_empty(struct btf *base_btf)
if (base_btf) { if (base_btf) {
btf->base_btf = base_btf; btf->base_btf = base_btf;
btf->start_id = btf__get_nr_types(base_btf) + 1; btf->start_id = btf__type_cnt(base_btf);
btf->start_str_off = base_btf->hdr->str_len; btf->start_str_off = base_btf->hdr->str_len;
} }
...@@ -831,7 +836,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf) ...@@ -831,7 +836,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
if (base_btf) { if (base_btf) {
btf->base_btf = base_btf; btf->base_btf = base_btf;
btf->start_id = btf__get_nr_types(base_btf) + 1; btf->start_id = btf__type_cnt(base_btf);
btf->start_str_off = base_btf->hdr->str_len; btf->start_str_off = base_btf->hdr->str_len;
} }
...@@ -1224,7 +1229,7 @@ static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endi ...@@ -1224,7 +1229,7 @@ static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endi
return NULL; return NULL;
} }
const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size) const void *btf__raw_data(const struct btf *btf_ro, __u32 *size)
{ {
struct btf *btf = (struct btf *)btf_ro; struct btf *btf = (struct btf *)btf_ro;
__u32 data_sz; __u32 data_sz;
...@@ -1232,7 +1237,7 @@ const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size) ...@@ -1232,7 +1237,7 @@ const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian); data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
if (!data) if (!data)
return errno = -ENOMEM, NULL; return errno = ENOMEM, NULL;
btf->raw_size = data_sz; btf->raw_size = data_sz;
if (btf->swapped_endian) if (btf->swapped_endian)
...@@ -1243,6 +1248,9 @@ const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size) ...@@ -1243,6 +1248,9 @@ const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
return data; return data;
} }
__attribute__((alias("btf__raw_data")))
const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
const char *btf__str_by_offset(const struct btf *btf, __u32 offset) const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
{ {
if (offset < btf->start_str_off) if (offset < btf->start_str_off)
...@@ -1651,7 +1659,7 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf) ...@@ -1651,7 +1659,7 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
old_strs_len = btf->hdr->str_len; old_strs_len = btf->hdr->str_len;
data_sz = src_btf->hdr->type_len; data_sz = src_btf->hdr->type_len;
cnt = btf__get_nr_types(src_btf); cnt = btf__type_cnt(src_btf) - 1;
/* pre-allocate enough memory for new types */ /* pre-allocate enough memory for new types */
t = btf_add_type_mem(btf, data_sz); t = btf_add_type_mem(btf, data_sz);
...@@ -1968,7 +1976,7 @@ int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz) ...@@ -1968,7 +1976,7 @@ int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz)
static struct btf_type *btf_last_type(struct btf *btf) static struct btf_type *btf_last_type(struct btf *btf)
{ {
return btf_type_by_id(btf, btf__get_nr_types(btf)); return btf_type_by_id(btf, btf__type_cnt(btf) - 1);
} }
/* /*
...@@ -3174,7 +3182,7 @@ static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext, ...@@ -3174,7 +3182,7 @@ static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
goto done; goto done;
} }
type_cnt = btf__get_nr_types(btf) + 1; type_cnt = btf__type_cnt(btf);
d->map = malloc(sizeof(__u32) * type_cnt); d->map = malloc(sizeof(__u32) * type_cnt);
if (!d->map) { if (!d->map) {
err = -ENOMEM; err = -ENOMEM;
......
...@@ -132,7 +132,9 @@ LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, ...@@ -132,7 +132,9 @@ LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
const char *type_name); const char *type_name);
LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf, LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
const char *type_name, __u32 kind); const char *type_name, __u32 kind);
LIBBPF_DEPRECATED_SINCE(0, 7, "use btf__type_cnt() instead; note that btf__get_nr_types() == btf__type_cnt() - 1")
LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf); LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
LIBBPF_API __u32 btf__type_cnt(const struct btf *btf);
LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf); LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf);
LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf, LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
__u32 id); __u32 id);
...@@ -145,7 +147,9 @@ LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); ...@@ -145,7 +147,9 @@ LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id); LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
LIBBPF_API int btf__fd(const struct btf *btf); LIBBPF_API int btf__fd(const struct btf *btf);
LIBBPF_API void btf__set_fd(struct btf *btf, int fd); LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
LIBBPF_DEPRECATED_SINCE(0, 7, "use btf__raw_data() instead")
LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size); LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
LIBBPF_API const void *btf__raw_data(const struct btf *btf, __u32 *size);
LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset); LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
......
...@@ -188,7 +188,7 @@ struct btf_dump *btf_dump__new(const struct btf *btf, ...@@ -188,7 +188,7 @@ struct btf_dump *btf_dump__new(const struct btf *btf,
static int btf_dump_resize(struct btf_dump *d) static int btf_dump_resize(struct btf_dump *d)
{ {
int err, last_id = btf__get_nr_types(d->btf); int err, last_id = btf__type_cnt(d->btf) - 1;
if (last_id <= d->last_id) if (last_id <= d->last_id)
return 0; return 0;
...@@ -262,7 +262,7 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id) ...@@ -262,7 +262,7 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id)
{ {
int err, i; int err, i;
if (id > btf__get_nr_types(d->btf)) if (id >= btf__type_cnt(d->btf))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
err = btf_dump_resize(d); err = btf_dump_resize(d);
...@@ -294,11 +294,11 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id) ...@@ -294,11 +294,11 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id)
*/ */
static int btf_dump_mark_referenced(struct btf_dump *d) static int btf_dump_mark_referenced(struct btf_dump *d)
{ {
int i, j, n = btf__get_nr_types(d->btf); int i, j, n = btf__type_cnt(d->btf);
const struct btf_type *t; const struct btf_type *t;
__u16 vlen; __u16 vlen;
for (i = d->last_id + 1; i <= n; i++) { for (i = d->last_id + 1; i < n; i++) {
t = btf__type_by_id(d->btf, i); t = btf__type_by_id(d->btf, i);
vlen = btf_vlen(t); vlen = btf_vlen(t);
......
...@@ -2497,8 +2497,8 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, ...@@ -2497,8 +2497,8 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
return -EINVAL; return -EINVAL;
} }
nr_types = btf__get_nr_types(obj->btf); nr_types = btf__type_cnt(obj->btf);
for (i = 1; i <= nr_types; i++) { for (i = 1; i < nr_types; i++) {
t = btf__type_by_id(obj->btf, i); t = btf__type_by_id(obj->btf, i);
if (!btf_is_datasec(t)) if (!btf_is_datasec(t))
continue; continue;
...@@ -2579,7 +2579,7 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) ...@@ -2579,7 +2579,7 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
struct btf_type *t; struct btf_type *t;
int i, j, vlen; int i, j, vlen;
for (i = 1; i <= btf__get_nr_types(btf); i++) { for (i = 1; i < btf__type_cnt(btf); i++) {
t = (struct btf_type *)btf__type_by_id(btf, i); t = (struct btf_type *)btf__type_by_id(btf, i);
if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) { if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) {
...@@ -2763,9 +2763,9 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, ...@@ -2763,9 +2763,9 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
static int btf_finalize_data(struct bpf_object *obj, struct btf *btf) static int btf_finalize_data(struct bpf_object *obj, struct btf *btf)
{ {
int err = 0; int err = 0;
__u32 i, n = btf__get_nr_types(btf); __u32 i, n = btf__type_cnt(btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
struct btf_type *t = btf_type_by_id(btf, i); struct btf_type *t = btf_type_by_id(btf, i);
/* Loader needs to fix up some of the things compiler /* Loader needs to fix up some of the things compiler
...@@ -2905,8 +2905,8 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) ...@@ -2905,8 +2905,8 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) if (!prog->mark_btf_static || !prog_is_subprog(obj, prog))
continue; continue;
n = btf__get_nr_types(obj->btf); n = btf__type_cnt(obj->btf);
for (j = 1; j <= n; j++) { for (j = 1; j < n; j++) {
t = btf_type_by_id(obj->btf, j); t = btf_type_by_id(obj->btf, j);
if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL)
continue; continue;
...@@ -2926,7 +2926,7 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) ...@@ -2926,7 +2926,7 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
__u32 sz; __u32 sz;
/* clone BTF to sanitize a copy and leave the original intact */ /* clone BTF to sanitize a copy and leave the original intact */
raw_data = btf__get_raw_data(obj->btf, &sz); raw_data = btf__raw_data(obj->btf, &sz);
kern_btf = btf__new(raw_data, sz); kern_btf = btf__new(raw_data, sz);
err = libbpf_get_error(kern_btf); err = libbpf_get_error(kern_btf);
if (err) if (err)
...@@ -2939,7 +2939,7 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) ...@@ -2939,7 +2939,7 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
if (obj->gen_loader) { if (obj->gen_loader) {
__u32 raw_size = 0; __u32 raw_size = 0;
const void *raw_data = btf__get_raw_data(kern_btf, &raw_size); const void *raw_data = btf__raw_data(kern_btf, &raw_size);
if (!raw_data) if (!raw_data)
return -ENOMEM; return -ENOMEM;
...@@ -3350,8 +3350,8 @@ static int find_extern_btf_id(const struct btf *btf, const char *ext_name) ...@@ -3350,8 +3350,8 @@ static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
if (!btf) if (!btf)
return -ESRCH; return -ESRCH;
n = btf__get_nr_types(btf); n = btf__type_cnt(btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
t = btf__type_by_id(btf, i); t = btf__type_by_id(btf, i);
if (!btf_is_var(t) && !btf_is_func(t)) if (!btf_is_var(t) && !btf_is_func(t))
...@@ -3382,8 +3382,8 @@ static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) { ...@@ -3382,8 +3382,8 @@ static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
if (!btf) if (!btf)
return -ESRCH; return -ESRCH;
n = btf__get_nr_types(btf); n = btf__type_cnt(btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
t = btf__type_by_id(btf, i); t = btf__type_by_id(btf, i);
if (!btf_is_datasec(t)) if (!btf_is_datasec(t))
...@@ -3467,8 +3467,8 @@ static int find_int_btf_id(const struct btf *btf) ...@@ -3467,8 +3467,8 @@ static int find_int_btf_id(const struct btf *btf)
const struct btf_type *t; const struct btf_type *t;
int i, n; int i, n;
n = btf__get_nr_types(btf); n = btf__type_cnt(btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
t = btf__type_by_id(btf, i); t = btf__type_by_id(btf, i);
if (btf_is_int(t) && btf_int_bits(t) == 32) if (btf_is_int(t) && btf_int_bits(t) == 32)
...@@ -5076,8 +5076,8 @@ static int bpf_core_add_cands(struct bpf_core_cand *local_cand, ...@@ -5076,8 +5076,8 @@ static int bpf_core_add_cands(struct bpf_core_cand *local_cand,
size_t targ_essent_len; size_t targ_essent_len;
int n, i; int n, i;
n = btf__get_nr_types(targ_btf); n = btf__type_cnt(targ_btf);
for (i = targ_start_id; i <= n; i++) { for (i = targ_start_id; i < n; i++) {
t = btf__type_by_id(targ_btf, i); t = btf__type_by_id(targ_btf, i);
if (btf_kind(t) != btf_kind(local_cand->t)) if (btf_kind(t) != btf_kind(local_cand->t))
continue; continue;
...@@ -5252,7 +5252,7 @@ bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 l ...@@ -5252,7 +5252,7 @@ bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 l
err = bpf_core_add_cands(&local_cand, local_essent_len, err = bpf_core_add_cands(&local_cand, local_essent_len,
obj->btf_modules[i].btf, obj->btf_modules[i].btf,
obj->btf_modules[i].name, obj->btf_modules[i].name,
btf__get_nr_types(obj->btf_vmlinux) + 1, btf__type_cnt(obj->btf_vmlinux),
cands); cands);
if (err) if (err)
goto err_out; goto err_out;
......
...@@ -395,4 +395,6 @@ LIBBPF_0.6.0 { ...@@ -395,4 +395,6 @@ LIBBPF_0.6.0 {
bpf_object__prev_program; bpf_object__prev_program;
btf__add_btf; btf__add_btf;
btf__add_decl_tag; btf__add_decl_tag;
btf__raw_data;
btf__type_cnt;
} LIBBPF_0.5.0; } LIBBPF_0.5.0;
...@@ -920,7 +920,7 @@ static int check_btf_type_id(__u32 *type_id, void *ctx) ...@@ -920,7 +920,7 @@ static int check_btf_type_id(__u32 *type_id, void *ctx)
{ {
struct btf *btf = ctx; struct btf *btf = ctx;
if (*type_id > btf__get_nr_types(btf)) if (*type_id >= btf__type_cnt(btf))
return -EINVAL; return -EINVAL;
return 0; return 0;
...@@ -947,8 +947,8 @@ static int linker_sanity_check_btf(struct src_obj *obj) ...@@ -947,8 +947,8 @@ static int linker_sanity_check_btf(struct src_obj *obj)
if (!obj->btf) if (!obj->btf)
return 0; return 0;
n = btf__get_nr_types(obj->btf); n = btf__type_cnt(obj->btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
t = btf_type_by_id(obj->btf, 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_type_ids(t, check_btf_type_id, obj->btf);
...@@ -1658,8 +1658,8 @@ static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sy ...@@ -1658,8 +1658,8 @@ static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sy
return -EINVAL; return -EINVAL;
} }
n = btf__get_nr_types(obj->btf); n = btf__type_cnt(obj->btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
t = btf__type_by_id(obj->btf, i); t = btf__type_by_id(obj->btf, i);
/* some global and extern FUNCs and VARs might not be associated with any /* some global and extern FUNCs and VARs might not be associated with any
...@@ -2130,8 +2130,8 @@ static int linker_fixup_btf(struct src_obj *obj) ...@@ -2130,8 +2130,8 @@ static int linker_fixup_btf(struct src_obj *obj)
if (!obj->btf) if (!obj->btf)
return 0; return 0;
n = btf__get_nr_types(obj->btf); n = btf__type_cnt(obj->btf);
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
struct btf_var_secinfo *vi; struct btf_var_secinfo *vi;
struct btf_type *t; struct btf_type *t;
...@@ -2234,14 +2234,14 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj) ...@@ -2234,14 +2234,14 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
if (!obj->btf) if (!obj->btf)
return 0; return 0;
start_id = btf__get_nr_types(linker->btf) + 1; start_id = btf__type_cnt(linker->btf);
n = btf__get_nr_types(obj->btf); n = btf__type_cnt(obj->btf);
obj->btf_type_map = calloc(n + 1, sizeof(int)); obj->btf_type_map = calloc(n + 1, sizeof(int));
if (!obj->btf_type_map) if (!obj->btf_type_map)
return -ENOMEM; return -ENOMEM;
for (i = 1; i <= n; i++) { for (i = 1; i < n; i++) {
struct glob_sym *glob_sym = NULL; struct glob_sym *glob_sym = NULL;
t = btf__type_by_id(obj->btf, i); t = btf__type_by_id(obj->btf, i);
...@@ -2296,8 +2296,8 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj) ...@@ -2296,8 +2296,8 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
} }
/* remap all the types except DATASECs */ /* remap all the types except DATASECs */
n = btf__get_nr_types(linker->btf); n = btf__type_cnt(linker->btf);
for (i = start_id; i <= n; i++) { for (i = start_id; i < n; i++) {
struct btf_type *dst_t = btf_type_by_id(linker->btf, 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)) if (btf_type_visit_type_ids(dst_t, remap_type_id, obj->btf_type_map))
...@@ -2656,7 +2656,7 @@ static int finalize_btf(struct bpf_linker *linker) ...@@ -2656,7 +2656,7 @@ static int finalize_btf(struct bpf_linker *linker)
__u32 raw_sz; __u32 raw_sz;
/* bail out if no BTF data was produced */ /* bail out if no BTF data was produced */
if (btf__get_nr_types(linker->btf) == 0) if (btf__type_cnt(linker->btf) == 1)
return 0; return 0;
for (i = 1; i < linker->sec_cnt; i++) { for (i = 1; i < linker->sec_cnt; i++) {
...@@ -2693,7 +2693,7 @@ static int finalize_btf(struct bpf_linker *linker) ...@@ -2693,7 +2693,7 @@ static int finalize_btf(struct bpf_linker *linker)
} }
/* Emit .BTF section */ /* Emit .BTF section */
raw_data = btf__get_raw_data(linker->btf, &raw_sz); raw_data = btf__raw_data(linker->btf, &raw_sz);
if (!raw_data) if (!raw_data)
return -ENOMEM; return -ENOMEM;
......
...@@ -110,7 +110,7 @@ static int perf_env__fetch_btf(struct perf_env *env, ...@@ -110,7 +110,7 @@ static int perf_env__fetch_btf(struct perf_env *env,
u32 data_size; u32 data_size;
const void *data; const void *data;
data = btf__get_raw_data(btf, &data_size); data = btf__raw_data(btf, &data_size);
node = malloc(data_size + sizeof(struct btf_node)); node = malloc(data_size + sizeof(struct btf_node));
if (!node) if (!node)
......
...@@ -215,7 +215,7 @@ int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[]) ...@@ -215,7 +215,7 @@ int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[])
int i; int i;
bool ok = true; bool ok = true;
ASSERT_EQ(btf__get_nr_types(btf), nr_types, "btf_nr_types"); ASSERT_EQ(btf__type_cnt(btf) - 1, nr_types, "btf_nr_types");
for (i = 1; i <= nr_types; i++) { for (i = 1; i <= nr_types; i++) {
if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump")) if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump"))
...@@ -254,7 +254,7 @@ const char *btf_type_c_dump(const struct btf *btf) ...@@ -254,7 +254,7 @@ const char *btf_type_c_dump(const struct btf *btf)
return NULL; return NULL;
} }
for (i = 1; i <= btf__get_nr_types(btf); i++) { for (i = 1; i < btf__type_cnt(btf); i++) {
err = btf_dump__dump_type(d, i); err = btf_dump__dump_type(d, i);
if (err) { if (err) {
fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err); fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
......
...@@ -7274,8 +7274,8 @@ static void do_test_dedup(unsigned int test_num) ...@@ -7274,8 +7274,8 @@ static void do_test_dedup(unsigned int test_num)
goto done; goto done;
} }
test_btf_data = btf__get_raw_data(test_btf, &test_btf_size); test_btf_data = btf__raw_data(test_btf, &test_btf_size);
expect_btf_data = btf__get_raw_data(expect_btf, &expect_btf_size); expect_btf_data = btf__raw_data(expect_btf, &expect_btf_size);
if (CHECK(test_btf_size != expect_btf_size, if (CHECK(test_btf_size != expect_btf_size,
"test_btf_size:%u != expect_btf_size:%u", "test_btf_size:%u != expect_btf_size:%u",
test_btf_size, expect_btf_size)) { test_btf_size, expect_btf_size)) {
...@@ -7329,8 +7329,8 @@ static void do_test_dedup(unsigned int test_num) ...@@ -7329,8 +7329,8 @@ static void do_test_dedup(unsigned int test_num)
expect_str_cur += expect_len + 1; expect_str_cur += expect_len + 1;
} }
test_nr_types = btf__get_nr_types(test_btf); test_nr_types = btf__type_cnt(test_btf);
expect_nr_types = btf__get_nr_types(expect_btf); expect_nr_types = btf__type_cnt(expect_btf);
if (CHECK(test_nr_types != expect_nr_types, if (CHECK(test_nr_types != expect_nr_types,
"test_nr_types:%u != expect_nr_types:%u", "test_nr_types:%u != expect_nr_types:%u",
test_nr_types, expect_nr_types)) { test_nr_types, expect_nr_types)) {
...@@ -7338,7 +7338,7 @@ static void do_test_dedup(unsigned int test_num) ...@@ -7338,7 +7338,7 @@ static void do_test_dedup(unsigned int test_num)
goto done; goto done;
} }
for (i = 1; i <= test_nr_types; i++) { for (i = 1; i < test_nr_types; i++) {
const struct btf_type *test_type, *expect_type; const struct btf_type *test_type, *expect_type;
int test_size, expect_size; int test_size, expect_size;
......
...@@ -27,7 +27,7 @@ static struct btf_dump_test_case { ...@@ -27,7 +27,7 @@ static struct btf_dump_test_case {
static int btf_dump_all_types(const struct btf *btf, static int btf_dump_all_types(const struct btf *btf,
const struct btf_dump_opts *opts) const struct btf_dump_opts *opts)
{ {
size_t type_cnt = btf__get_nr_types(btf); size_t type_cnt = btf__type_cnt(btf);
struct btf_dump *d; struct btf_dump *d;
int err = 0, id; int err = 0, id;
...@@ -36,7 +36,7 @@ static int btf_dump_all_types(const struct btf *btf, ...@@ -36,7 +36,7 @@ static int btf_dump_all_types(const struct btf *btf,
if (err) if (err)
return err; return err;
for (id = 1; id <= type_cnt; id++) { for (id = 1; id < type_cnt; id++) {
err = btf_dump__dump_type(d, id); err = btf_dump__dump_type(d, id);
if (err) if (err)
goto done; goto done;
...@@ -171,7 +171,7 @@ void test_btf_dump_incremental(void) ...@@ -171,7 +171,7 @@ void test_btf_dump_incremental(void)
err = btf__add_field(btf, "x", 2, 0, 0); err = btf__add_field(btf, "x", 2, 0, 0);
ASSERT_OK(err, "field_ok"); ASSERT_OK(err, "field_ok");
for (i = 1; i <= btf__get_nr_types(btf); i++) { for (i = 1; i < btf__type_cnt(btf); i++) {
err = btf_dump__dump_type(d, i); err = btf_dump__dump_type(d, i);
ASSERT_OK(err, "dump_type_ok"); ASSERT_OK(err, "dump_type_ok");
} }
...@@ -210,7 +210,7 @@ void test_btf_dump_incremental(void) ...@@ -210,7 +210,7 @@ void test_btf_dump_incremental(void)
err = btf__add_field(btf, "s", 3, 32, 0); err = btf__add_field(btf, "s", 3, 32, 0);
ASSERT_OK(err, "field_ok"); ASSERT_OK(err, "field_ok");
for (i = 1; i <= btf__get_nr_types(btf); i++) { for (i = 1; i < btf__type_cnt(btf); i++) {
err = btf_dump__dump_type(d, i); err = btf_dump__dump_type(d, i);
ASSERT_OK(err, "dump_type_ok"); ASSERT_OK(err, "dump_type_ok");
} }
......
...@@ -32,7 +32,7 @@ void test_btf_endian() { ...@@ -32,7 +32,7 @@ void test_btf_endian() {
ASSERT_EQ(btf__endianness(btf), swap_endian, "endian"); ASSERT_EQ(btf__endianness(btf), swap_endian, "endian");
/* Get raw BTF data in non-native endianness... */ /* Get raw BTF data in non-native endianness... */
raw_data = btf__get_raw_data(btf, &raw_sz); raw_data = btf__raw_data(btf, &raw_sz);
if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted")) if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted"))
goto err_out; goto err_out;
...@@ -42,9 +42,9 @@ void test_btf_endian() { ...@@ -42,9 +42,9 @@ void test_btf_endian() {
goto err_out; goto err_out;
ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian"); ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian");
ASSERT_EQ(btf__get_nr_types(swap_btf), btf__get_nr_types(btf), "nr_types"); ASSERT_EQ(btf__type_cnt(swap_btf), btf__type_cnt(btf), "nr_types");
swap_raw_data = btf__get_raw_data(swap_btf, &swap_raw_sz); swap_raw_data = btf__raw_data(swap_btf, &swap_raw_sz);
if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data")) if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data"))
goto err_out; goto err_out;
...@@ -58,7 +58,7 @@ void test_btf_endian() { ...@@ -58,7 +58,7 @@ void test_btf_endian() {
/* swap it back to native endianness */ /* swap it back to native endianness */
btf__set_endianness(swap_btf, endian); btf__set_endianness(swap_btf, endian);
swap_raw_data = btf__get_raw_data(swap_btf, &swap_raw_sz); swap_raw_data = btf__raw_data(swap_btf, &swap_raw_sz);
if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data")) if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data"))
goto err_out; goto err_out;
...@@ -75,7 +75,7 @@ void test_btf_endian() { ...@@ -75,7 +75,7 @@ void test_btf_endian() {
swap_btf = NULL; swap_btf = NULL;
btf__set_endianness(btf, swap_endian); btf__set_endianness(btf, swap_endian);
raw_data = btf__get_raw_data(btf, &raw_sz); raw_data = btf__raw_data(btf, &raw_sz);
if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted")) if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted"))
goto err_out; goto err_out;
...@@ -85,7 +85,7 @@ void test_btf_endian() { ...@@ -85,7 +85,7 @@ void test_btf_endian() {
goto err_out; goto err_out;
ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian"); ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian");
ASSERT_EQ(btf__get_nr_types(swap_btf), btf__get_nr_types(btf), "nr_types"); ASSERT_EQ(btf__type_cnt(swap_btf), btf__type_cnt(btf), "nr_types");
/* the type should appear as if it was stored in native endianness */ /* the type should appear as if it was stored in native endianness */
t = btf__type_by_id(swap_btf, var_id); t = btf__type_by_id(swap_btf, var_id);
......
...@@ -72,7 +72,7 @@ void test_btf_split() { ...@@ -72,7 +72,7 @@ void test_btf_split() {
d = btf_dump__new(btf2, NULL, &opts, btf_dump_printf); d = btf_dump__new(btf2, NULL, &opts, btf_dump_printf);
if (!ASSERT_OK_PTR(d, "btf_dump__new")) if (!ASSERT_OK_PTR(d, "btf_dump__new"))
goto cleanup; goto cleanup;
for (i = 1; i <= btf__get_nr_types(btf2); i++) { for (i = 1; i < btf__type_cnt(btf2); i++) {
err = btf_dump__dump_type(d, i); err = btf_dump__dump_type(d, i);
ASSERT_OK(err, "dump_type_ok"); ASSERT_OK(err, "dump_type_ok");
} }
......
...@@ -112,7 +112,7 @@ void test_core_autosize(void) ...@@ -112,7 +112,7 @@ void test_core_autosize(void)
if (!ASSERT_OK_PTR(f, "btf_fdopen")) if (!ASSERT_OK_PTR(f, "btf_fdopen"))
goto cleanup; goto cleanup;
raw_data = btf__get_raw_data(btf, &raw_sz); raw_data = btf__raw_data(btf, &raw_sz);
if (!ASSERT_OK_PTR(raw_data, "raw_data")) if (!ASSERT_OK_PTR(raw_data, "raw_data"))
goto cleanup; goto cleanup;
written = fwrite(raw_data, 1, raw_sz, f); written = fwrite(raw_data, 1, raw_sz, f);
......
...@@ -381,7 +381,7 @@ static int setup_type_id_case_local(struct core_reloc_test_case *test) ...@@ -381,7 +381,7 @@ static int setup_type_id_case_local(struct core_reloc_test_case *test)
exp->local_anon_void_ptr = -1; exp->local_anon_void_ptr = -1;
exp->local_anon_arr = -1; exp->local_anon_arr = -1;
for (i = 1; i <= btf__get_nr_types(local_btf); i++) for (i = 1; i < btf__type_cnt(local_btf); i++)
{ {
t = btf__type_by_id(local_btf, i); t = btf__type_by_id(local_btf, i);
/* we are interested only in anonymous types */ /* we are interested only in anonymous types */
......
...@@ -106,9 +106,9 @@ static int resolve_symbols(void) ...@@ -106,9 +106,9 @@ static int resolve_symbols(void)
"Failed to load BTF from btf_data.o\n")) "Failed to load BTF from btf_data.o\n"))
return -1; return -1;
nr = btf__get_nr_types(btf); nr = btf__type_cnt(btf);
for (type_id = 1; type_id <= nr; type_id++) { for (type_id = 1; type_id < nr; type_id++) {
if (__resolve_symbol(btf, type_id)) if (__resolve_symbol(btf, type_id))
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment