Skip to content

Commit 9a94f27

Browse files
Jakub Kicinskiborkmann
authored andcommitted
tools: libbpf: restore the ability to load programs from .text section
libbpf used to be able to load programs from the default section called '.text'. It's not very common to leave sections unnamed, but if it happens libbpf will fail to load the programs reporting -EINVAL from the kernel. The -EINVAL comes from bpf_obj_name_cpy() because since 48cca7e ("libbpf: add support for bpf_call") libbpf does not resolve program names for programs in '.text', defaulting to '.text'. '.text', however, does not pass the (isalnum(*src) || *src == '_') check in bpf_obj_name_cpy(). With few extra lines of code we can limit the pseudo call assumptions only to objects which actually contain code relocations. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 9aba361 commit 9a94f27

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ struct bpf_object {
234234
size_t nr_maps;
235235

236236
bool loaded;
237+
bool has_pseudo_calls;
237238

238239
/*
239240
* Information when doing elf related work. Only valid if fd
@@ -400,10 +401,6 @@ bpf_object__init_prog_names(struct bpf_object *obj)
400401
const char *name = NULL;
401402

402403
prog = &obj->programs[pi];
403-
if (prog->idx == obj->efile.text_shndx) {
404-
name = ".text";
405-
goto skip_search;
406-
}
407404

408405
for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
409406
si++) {
@@ -426,12 +423,15 @@ bpf_object__init_prog_names(struct bpf_object *obj)
426423
}
427424
}
428425

426+
if (!name && prog->idx == obj->efile.text_shndx)
427+
name = ".text";
428+
429429
if (!name) {
430430
pr_warning("failed to find sym for prog %s\n",
431431
prog->section_name);
432432
return -EINVAL;
433433
}
434-
skip_search:
434+
435435
prog->name = strdup(name);
436436
if (!prog->name) {
437437
pr_warning("failed to allocate memory for prog sym %s\n",
@@ -981,6 +981,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
981981
prog->reloc_desc[i].type = RELO_CALL;
982982
prog->reloc_desc[i].insn_idx = insn_idx;
983983
prog->reloc_desc[i].text_off = sym.st_value;
984+
obj->has_pseudo_calls = true;
984985
continue;
985986
}
986987

@@ -1426,14 +1427,20 @@ bpf_program__load(struct bpf_program *prog,
14261427
return err;
14271428
}
14281429

1430+
static bool bpf_program__is_function_storage(struct bpf_program *prog,
1431+
struct bpf_object *obj)
1432+
{
1433+
return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
1434+
}
1435+
14291436
static int
14301437
bpf_object__load_progs(struct bpf_object *obj)
14311438
{
14321439
size_t i;
14331440
int err;
14341441

14351442
for (i = 0; i < obj->nr_programs; i++) {
1436-
if (obj->programs[i].idx == obj->efile.text_shndx)
1443+
if (bpf_program__is_function_storage(&obj->programs[i], obj))
14371444
continue;
14381445
err = bpf_program__load(&obj->programs[i],
14391446
obj->license,
@@ -2247,7 +2254,7 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
22472254
bpf_program__set_expected_attach_type(prog,
22482255
expected_attach_type);
22492256

2250-
if (prog->idx != obj->efile.text_shndx && !first_prog)
2257+
if (!bpf_program__is_function_storage(prog, obj) && !first_prog)
22512258
first_prog = prog;
22522259
}
22532260

0 commit comments

Comments
 (0)