summaryrefslogtreecommitdiff
path: root/tools/lib/bpf/linker.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/lib/bpf/linker.c')
-rw-r--r--tools/lib/bpf/linker.c37
1 files changed, 28 insertions, 9 deletions
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 5e0aa2f2c0ca..46b16cbdcda3 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -94,6 +94,7 @@ struct dst_sec {
int sec_sym_idx;
/* section's DATASEC variable info, emitted on BTF finalization */
+ bool has_btf;
int sec_var_cnt;
struct btf_var_secinfo *sec_vars;
@@ -1436,6 +1437,16 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
continue;
dst_sec = &linker->secs[src_sec->dst_id];
+ /* Mark section as having BTF regardless of the presence of
+ * variables. In some cases compiler might generate empty BTF
+ * with no variables information. E.g., when promoting local
+ * array/structure variable initial values and BPF object
+ * file otherwise has no read-only static variables in
+ * .rodata. We need to preserve such empty BTF and just set
+ * correct section size.
+ */
+ dst_sec->has_btf = true;
+
t = btf__type_by_id(obj->btf, src_sec->sec_type_id);
src_var = btf_var_secinfos(t);
n = btf_vlen(t);
@@ -1717,7 +1728,7 @@ static int finalize_btf(struct bpf_linker *linker)
for (i = 1; i < linker->sec_cnt; i++) {
struct dst_sec *sec = &linker->secs[i];
- if (!sec->sec_var_cnt)
+ if (!sec->has_btf)
continue;
id = btf__add_datasec(btf, sec->sec_name, sec->sec_sz);
@@ -1895,8 +1906,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i];
sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
- if (sz < 0)
- return sz;
+ if (sz < 0) {
+ err = sz;
+ goto out;
+ }
cur += sz;
}
@@ -1910,8 +1923,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i];
sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
- if (sz < 0)
- return sz;
+ if (sz < 0) {
+ err = sz;
+ goto out;
+ }
cur += sz;
}
@@ -1925,8 +1940,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i];
sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
- if (sz < 0)
- return sz;
+ if (sz < 0) {
+ err = sz;
+ goto out;
+ }
cur += sz;
}
@@ -1937,8 +1954,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
if (err) {
linker->btf_ext = NULL;
pr_warn("failed to parse final .BTF.ext data: %d\n", err);
- return err;
+ goto out;
}
- return 0;
+out:
+ free(data);
+ return err;
}