Skip to content

Commit 2d9454d

Browse files
theihorKernel Patches Daemon
authored andcommitted
resolve_btfids: factor out load_btf()
Increase the lifetime of parsed BTF in resolve_btfids by factoring load_btf() routine out of symbols_resolve() and storing the base_btf and btf pointers in the struct object. Signed-off-by: Ihor Solodrai <[email protected]>
1 parent bf7f9d4 commit 2d9454d

File tree

1 file changed

+34
-13
lines changed
  • tools/bpf/resolve_btfids

1 file changed

+34
-13
lines changed

tools/bpf/resolve_btfids/main.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ struct object {
116116
const char *btf_path;
117117
const char *base_btf_path;
118118

119+
struct btf *btf;
120+
struct btf *base_btf;
121+
119122
struct {
120123
int fd;
121124
Elf *elf;
@@ -529,24 +532,18 @@ static int symbols_collect(struct object *obj)
529532
return 0;
530533
}
531534

532-
static int symbols_resolve(struct object *obj)
535+
static int load_btf(struct object *obj)
533536
{
534-
int nr_typedefs = obj->nr_typedefs;
535-
int nr_structs = obj->nr_structs;
536-
int nr_unions = obj->nr_unions;
537-
int nr_funcs = obj->nr_funcs;
538-
struct btf *base_btf = NULL;
539-
int err, type_id;
540-
struct btf *btf;
541-
__u32 nr_types;
537+
struct btf *base_btf = NULL, *btf = NULL;
538+
int err;
542539

543540
if (obj->base_btf_path) {
544541
base_btf = btf__parse(obj->base_btf_path, NULL);
545542
err = libbpf_get_error(base_btf);
546543
if (err) {
547544
pr_err("FAILED: load base BTF from %s: %s\n",
548545
obj->base_btf_path, strerror(-err));
549-
return -1;
546+
goto out_err;
550547
}
551548
}
552549

@@ -555,9 +552,30 @@ static int symbols_resolve(struct object *obj)
555552
if (err) {
556553
pr_err("FAILED: load BTF from %s: %s\n",
557554
obj->btf_path ?: obj->path, strerror(-err));
558-
goto out;
555+
goto out_err;
559556
}
560557

558+
obj->base_btf = base_btf;
559+
obj->btf = btf;
560+
561+
return 0;
562+
563+
out_err:
564+
btf__free(base_btf);
565+
btf__free(btf);
566+
return err;
567+
}
568+
569+
static int symbols_resolve(struct object *obj)
570+
{
571+
int nr_typedefs = obj->nr_typedefs;
572+
int nr_structs = obj->nr_structs;
573+
int nr_unions = obj->nr_unions;
574+
int nr_funcs = obj->nr_funcs;
575+
struct btf *btf = obj->btf;
576+
int err, type_id;
577+
__u32 nr_types;
578+
561579
err = -1;
562580
nr_types = btf__type_cnt(btf);
563581

@@ -615,8 +633,6 @@ static int symbols_resolve(struct object *obj)
615633

616634
err = 0;
617635
out:
618-
btf__free(base_btf);
619-
btf__free(btf);
620636
return err;
621637
}
622638

@@ -824,6 +840,9 @@ int main(int argc, const char **argv)
824840
if (symbols_collect(&obj))
825841
goto out;
826842

843+
if (load_btf(&obj))
844+
goto out;
845+
827846
if (symbols_resolve(&obj))
828847
goto out;
829848

@@ -833,6 +852,8 @@ int main(int argc, const char **argv)
833852
if (!(fatal_warnings && warnings))
834853
err = 0;
835854
out:
855+
btf__free(obj.base_btf);
856+
btf__free(obj.btf);
836857
if (obj.efile.elf) {
837858
elf_end(obj.efile.elf);
838859
close(obj.efile.fd);

0 commit comments

Comments
 (0)