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+
101108struct 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
281293static 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,11 @@ 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+ if (btf_id )
306+ btf_id -> kind = BTF_ID_KIND_SYM ;
307+
308+ return btf_id ;
292309}
293310
294311/* Older libelf.h and glibc elf.h might not yet define the ELF compression types. */
@@ -491,28 +508,24 @@ static int symbols_collect(struct object *obj)
491508 id = add_symbol (& obj -> funcs , prefix , sizeof (BTF_FUNC ) - 1 );
492509 /* set8 */
493510 } else if (!strncmp (prefix , BTF_SET8 , sizeof (BTF_SET8 ) - 1 )) {
494- id = add_set (obj , prefix , true );
511+ id = add_set (obj , prefix , BTF_ID_KIND_SET8 );
495512 /*
496513 * SET8 objects store list's count, which is encoded
497514 * in symbol's size, together with 'cnt' field hence
498515 * that - 1.
499516 */
500- if (id ) {
517+ if (id )
501518 id -> cnt = sym .st_size / sizeof (uint64_t ) - 1 ;
502- id -> is_set8 = true;
503- }
504519 /* set */
505520 } else if (!strncmp (prefix , BTF_SET , sizeof (BTF_SET ) - 1 )) {
506- id = add_set (obj , prefix , false );
521+ id = add_set (obj , prefix , BTF_ID_KIND_SET );
507522 /*
508523 * SET objects store list's count, which is encoded
509524 * in symbol's size, together with 'cnt' field hence
510525 * that - 1.
511526 */
512- if (id ) {
527+ if (id )
513528 id -> cnt = sym .st_size / sizeof (int ) - 1 ;
514- id -> is_set = true;
515- }
516529 } else {
517530 pr_err ("FAILED unsupported prefix %s\n" , prefix );
518531 return -1 ;
@@ -643,7 +656,7 @@ static int id_patch(struct object *obj, struct btf_id *id)
643656 int i ;
644657
645658 /* For set, set8, id->id may be 0 */
646- if (!id -> id && ! id -> is_set && ! id -> is_set8 ) {
659+ if (!id -> id && id -> kind == BTF_ID_KIND_SYM ) {
647660 pr_err ("WARN: resolve_btfids: unresolved symbol %s\n" , id -> name );
648661 warnings ++ ;
649662 }
@@ -696,6 +709,7 @@ static int sets_patch(struct object *obj)
696709{
697710 Elf_Data * data = obj -> efile .idlist ;
698711 struct rb_node * next ;
712+ int cnt ;
699713
700714 next = rb_first (& obj -> sets );
701715 while (next ) {
@@ -715,11 +729,15 @@ static int sets_patch(struct object *obj)
715729 return -1 ;
716730 }
717731
718- if (id -> is_set ) {
732+ switch (id -> kind ) {
733+ case BTF_ID_KIND_SET :
719734 set = data -> d_buf + off ;
735+ cnt = set -> cnt ;
720736 qsort (set -> ids , set -> cnt , sizeof (set -> ids [0 ]), cmp_id );
721- } else {
737+ break ;
738+ case BTF_ID_KIND_SET8 :
722739 set8 = data -> d_buf + off ;
740+ cnt = set8 -> cnt ;
723741 /*
724742 * Make sure id is at the beginning of the pairs
725743 * struct, otherwise the below qsort would not work.
@@ -744,10 +762,14 @@ static int sets_patch(struct object *obj)
744762 bswap_32 (set8 -> pairs [i ].flags );
745763 }
746764 }
765+ break ;
766+ case BTF_ID_KIND_SYM :
767+ default :
768+ pr_err ("Unexpected btf_id_kind %d for set '%s'\n" , id -> kind , id -> name );
769+ return -1 ;
747770 }
748771
749- pr_debug ("sorting addr %5lu: cnt %6d [%s]\n" ,
750- off , id -> is_set ? set -> cnt : set8 -> cnt , id -> name );
772+ pr_debug ("sorting addr %5lu: cnt %6d [%s]\n" , off , cnt , id -> name );
751773
752774 next = rb_next (next );
753775 }
0 commit comments