Skip to content

Commit 076762c

Browse files
theihorKernel Patches Daemon
authored andcommitted
resolve_btfids: introduce enum btf_id_kind
Instead of using multiple flags, make struct btf_id tagged with an enum value indicating its kind in the context of resolve_btfids. Signed-off-by: Ihor Solodrai <[email protected]>
1 parent 2d9454d commit 076762c

File tree

1 file changed

+41
-20
lines changed
  • tools/bpf/resolve_btfids

1 file changed

+41
-20
lines changed

tools/bpf/resolve_btfids/main.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,22 @@
9898
# error "Unknown machine endianness!"
9999
#endif
100100

101+
enum btf_id_kind {
102+
BTF_ID_KIND_NONE,
103+
BTF_ID_KIND_SYM,
104+
BTF_ID_KIND_SET,
105+
BTF_ID_KIND_SET8
106+
};
107+
101108
struct btf_id {
102109
struct rb_node rb_node;
103110
char *name;
104111
union {
105112
int id;
106113
int cnt;
107114
};
108-
int addr_cnt;
109-
bool is_set;
110-
bool is_set8;
115+
enum btf_id_kind kind:8;
116+
int addr_cnt:8;
111117
Elf64_Addr addr[ADDR_CNT];
112118
};
113119

@@ -260,26 +266,33 @@ static char *get_id(const char *prefix_end)
260266
return id;
261267
}
262268

263-
static struct btf_id *add_set(struct object *obj, char *name, bool is_set8)
269+
static struct btf_id *add_set(struct object *obj, char *name, enum btf_id_kind kind)
264270
{
265271
/*
266272
* __BTF_ID__set__name
267273
* name = ^
268274
* id = ^
269275
*/
270-
char *id = name + (is_set8 ? sizeof(BTF_SET8 "__") : sizeof(BTF_SET "__")) - 1;
276+
int prefixlen = kind == BTF_ID_KIND_SET8 ? sizeof(BTF_SET8 "__") : sizeof(BTF_SET "__");
277+
char *id = name + prefixlen - 1;
271278
int len = strlen(name);
279+
struct btf_id *btf_id;
272280

273281
if (id >= name + len) {
274282
pr_err("FAILED to parse set name: %s\n", name);
275283
return NULL;
276284
}
277285

278-
return btf_id__add(&obj->sets, id, true);
286+
btf_id = btf_id__add(&obj->sets, id, true);
287+
if (btf_id)
288+
btf_id->kind = kind;
289+
290+
return btf_id;
279291
}
280292

281293
static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
282294
{
295+
struct btf_id *btf_id;
283296
char *id;
284297

285298
id = get_id(name + size);
@@ -288,7 +301,10 @@ static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
288301
return NULL;
289302
}
290303

291-
return btf_id__add(root, id, false);
304+
btf_id = btf_id__add(root, id, false);
305+
btf_id->kind = BTF_ID_KIND_SYM;
306+
307+
return btf_id;
292308
}
293309

294310
/* Older libelf.h and glibc elf.h might not yet define the ELF compression types. */
@@ -491,28 +507,24 @@ static int symbols_collect(struct object *obj)
491507
id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
492508
/* set8 */
493509
} else if (!strncmp(prefix, BTF_SET8, sizeof(BTF_SET8) - 1)) {
494-
id = add_set(obj, prefix, true);
510+
id = add_set(obj, prefix, BTF_ID_KIND_SET8);
495511
/*
496512
* SET8 objects store list's count, which is encoded
497513
* in symbol's size, together with 'cnt' field hence
498514
* that - 1.
499515
*/
500-
if (id) {
516+
if (id)
501517
id->cnt = sym.st_size / sizeof(uint64_t) - 1;
502-
id->is_set8 = true;
503-
}
504518
/* set */
505519
} else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
506-
id = add_set(obj, prefix, false);
520+
id = add_set(obj, prefix, BTF_ID_KIND_SET);
507521
/*
508522
* SET objects store list's count, which is encoded
509523
* in symbol's size, together with 'cnt' field hence
510524
* that - 1.
511525
*/
512-
if (id) {
526+
if (id)
513527
id->cnt = sym.st_size / sizeof(int) - 1;
514-
id->is_set = true;
515-
}
516528
} else {
517529
pr_err("FAILED unsupported prefix %s\n", prefix);
518530
return -1;
@@ -643,7 +655,7 @@ static int id_patch(struct object *obj, struct btf_id *id)
643655
int i;
644656

645657
/* For set, set8, id->id may be 0 */
646-
if (!id->id && !id->is_set && !id->is_set8) {
658+
if (!id->id && id->kind == BTF_ID_KIND_SYM) {
647659
pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name);
648660
warnings++;
649661
}
@@ -696,6 +708,7 @@ static int sets_patch(struct object *obj)
696708
{
697709
Elf_Data *data = obj->efile.idlist;
698710
struct rb_node *next;
711+
int cnt;
699712

700713
next = rb_first(&obj->sets);
701714
while (next) {
@@ -715,11 +728,15 @@ static int sets_patch(struct object *obj)
715728
return -1;
716729
}
717730

718-
if (id->is_set) {
731+
switch (id->kind) {
732+
case BTF_ID_KIND_SET:
719733
set = data->d_buf + off;
734+
cnt = set->cnt;
720735
qsort(set->ids, set->cnt, sizeof(set->ids[0]), cmp_id);
721-
} else {
736+
break;
737+
case BTF_ID_KIND_SET8:
722738
set8 = data->d_buf + off;
739+
cnt = set8->cnt;
723740
/*
724741
* Make sure id is at the beginning of the pairs
725742
* struct, otherwise the below qsort would not work.
@@ -744,10 +761,14 @@ static int sets_patch(struct object *obj)
744761
bswap_32(set8->pairs[i].flags);
745762
}
746763
}
764+
break;
765+
case BTF_ID_KIND_SYM:
766+
default:
767+
pr_err("Unexpected btf_id_kind %d for set '%s'\n", id->kind, id->name);
768+
return -1;
747769
}
748770

749-
pr_debug("sorting addr %5lu: cnt %6d [%s]\n",
750-
off, id->is_set ? set->cnt : set8->cnt, id->name);
771+
pr_debug("sorting addr %5lu: cnt %6d [%s]\n", off, cnt, id->name);
751772

752773
next = rb_next(next);
753774
}

0 commit comments

Comments
 (0)