Commit b7d7f3e1 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Simplify BTF initialization logic

Current implementation of bpf_object's BTF initialization is very convoluted
and thus prone to errors. It doesn't have to be like that. This patch
simplifies it significantly.

This code also triggered static analysis issues over logically dead code due
to redundant error checks. This simplification should fix that as well.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200117060801.1311525-3-andriin@fb.com
parent bc0eb9a3
...@@ -2297,16 +2297,18 @@ static int bpf_object__init_btf(struct bpf_object *obj, ...@@ -2297,16 +2297,18 @@ static int bpf_object__init_btf(struct bpf_object *obj,
Elf_Data *btf_data, Elf_Data *btf_data,
Elf_Data *btf_ext_data) Elf_Data *btf_ext_data)
{ {
bool btf_required = bpf_object__is_btf_mandatory(obj); int err = -ENOENT;
int err = 0;
if (btf_data) { if (btf_data) {
obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
if (IS_ERR(obj->btf)) { if (IS_ERR(obj->btf)) {
err = PTR_ERR(obj->btf);
obj->btf = NULL;
pr_warn("Error loading ELF section %s: %d.\n", pr_warn("Error loading ELF section %s: %d.\n",
BTF_ELF_SEC, err); BTF_ELF_SEC, err);
goto out; goto out;
} }
err = 0;
} }
if (btf_ext_data) { if (btf_ext_data) {
if (!obj->btf) { if (!obj->btf) {
...@@ -2324,18 +2326,9 @@ static int bpf_object__init_btf(struct bpf_object *obj, ...@@ -2324,18 +2326,9 @@ static int bpf_object__init_btf(struct bpf_object *obj,
} }
} }
out: out:
if (err || IS_ERR(obj->btf)) { if (err && bpf_object__is_btf_mandatory(obj)) {
if (btf_required)
err = err ? : PTR_ERR(obj->btf);
else
err = 0;
if (!IS_ERR_OR_NULL(obj->btf))
btf__free(obj->btf);
obj->btf = NULL;
}
if (btf_required && !obj->btf) {
pr_warn("BTF is required, but is missing or corrupted.\n"); pr_warn("BTF is required, but is missing or corrupted.\n");
return err == 0 ? -ENOENT : err; return err;
} }
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment