Skip to content

Commit 90e97f7

Browse files
Donglin PengKernel Patches Daemon
authored andcommitted
resolve_btfids: Refactor the sort_btf_by_name function
Preserve original relative order of anonymous or same-named types to improve the consistency. No functional changes. Cc: Andrii Nakryiko <[email protected]> Signed-off-by: Donglin Peng <[email protected]>
1 parent cecc6f5 commit 90e97f7

File tree

1 file changed

+11
-7
lines changed
  • tools/bpf/resolve_btfids

1 file changed

+11
-7
lines changed

tools/bpf/resolve_btfids/main.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,22 +1226,26 @@ static int cmp_type_names(const void *a, const void *b, void *priv)
12261226
const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);
12271227
const struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);
12281228
const char *na, *nb;
1229+
int r;
12291230

12301231
na = btf__str_by_offset(btf, ta->name_off);
12311232
nb = btf__str_by_offset(btf, tb->name_off);
1232-
return strcmp(na, nb);
1233+
r = strcmp(na, nb);
1234+
if (r != 0)
1235+
return r;
1236+
1237+
/* preserve original relative order of anonymous or same-named types */
1238+
return *(__u32 *)a < *(__u32 *)b ? -1 : 1;
12331239
}
12341240

12351241
static int sort_btf_by_name(struct btf *btf)
12361242
{
12371243
__u32 *permute_ids = NULL, *id_map = NULL;
12381244
int nr_types, i, err = 0;
1239-
__u32 start_id = 0, start_offs = 1, id;
1245+
__u32 start_id = 0, id;
12401246

1241-
if (btf__base_btf(btf)) {
1247+
if (btf__base_btf(btf))
12421248
start_id = btf__type_cnt(btf__base_btf(btf));
1243-
start_offs = 0;
1244-
}
12451249
nr_types = btf__type_cnt(btf) - start_id;
12461250

12471251
permute_ids = calloc(nr_types, sizeof(*permute_ids));
@@ -1259,8 +1263,8 @@ static int sort_btf_by_name(struct btf *btf)
12591263
for (i = 0, id = start_id; i < nr_types; i++, id++)
12601264
permute_ids[i] = id;
12611265

1262-
qsort_r(permute_ids + start_offs, nr_types - start_offs,
1263-
sizeof(*permute_ids), cmp_type_names, btf);
1266+
qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,
1267+
btf);
12641268

12651269
for (i = 0; i < nr_types; i++) {
12661270
id = permute_ids[i] - start_id;

0 commit comments

Comments
 (0)