It's wrong running the loader post prepare because gen_loader records the BTF load, map creation and extern/CO-RE relocation steps while the object is being prepared. If it is enabled after bpf_object__prepare(), those steps are never recorded. Suggested-by: Andrii Nakryiko Signed-off-by: Andrey Grodzovsky --- tools/lib/bpf/libbpf.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8fcb8cf39cc0..6bb5887090cd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10698,6 +10698,9 @@ int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts) return libbpf_err(-EFAULT); if (!OPTS_VALID(opts, gen_loader_opts)) return libbpf_err(-EINVAL); + if (obj->state >= OBJ_PREPARED) + return libbpf_err(-EINVAL); + gen = calloc(1, sizeof(*gen)); if (!gen) return libbpf_err(-ENOMEM); -- 2.34.1