Skip to content

Commit c1cccec

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Reject static maps
Static maps never really worked with libbpf, because all such maps were always silently resolved to the very first map. Detect static maps (both legacy and BTF-defined) and report user-friendly error. Tested locally by switching few maps (legacy and BTF-defined) in selftests to static ones and verifying that now libbpf rejects them loudly. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9e9b451 commit c1cccec

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,6 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
17951795
if (!symbols)
17961796
return -EINVAL;
17971797

1798-
17991798
scn = elf_sec_by_idx(obj, obj->efile.maps_shndx);
18001799
data = elf_sec_data(obj, scn);
18011800
if (!scn || !data) {
@@ -1855,6 +1854,12 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
18551854
return -LIBBPF_ERRNO__FORMAT;
18561855
}
18571856

1857+
if (GELF_ST_TYPE(sym.st_info) == STT_SECTION
1858+
|| GELF_ST_BIND(sym.st_info) == STB_LOCAL) {
1859+
pr_warn("map '%s' (legacy): static maps are not supported\n", map_name);
1860+
return -ENOTSUP;
1861+
}
1862+
18581863
map->libbpf_type = LIBBPF_MAP_UNSPEC;
18591864
map->sec_idx = sym.st_shndx;
18601865
map->sec_offset = sym.st_value;
@@ -2262,6 +2267,16 @@ static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def
22622267
pr_debug("map '%s': found inner map definition.\n", map->name);
22632268
}
22642269

2270+
static const char *btf_var_linkage_str(__u32 linkage)
2271+
{
2272+
switch (linkage) {
2273+
case BTF_VAR_STATIC: return "static";
2274+
case BTF_VAR_GLOBAL_ALLOCATED: return "global";
2275+
case BTF_VAR_GLOBAL_EXTERN: return "extern";
2276+
default: return "unknown";
2277+
}
2278+
}
2279+
22652280
static int bpf_object__init_user_btf_map(struct bpf_object *obj,
22662281
const struct btf_type *sec,
22672282
int var_idx, int sec_idx,
@@ -2294,10 +2309,9 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,
22942309
map_name, btf_kind_str(var));
22952310
return -EINVAL;
22962311
}
2297-
if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
2298-
var_extra->linkage != BTF_VAR_STATIC) {
2299-
pr_warn("map '%s': unsupported var linkage %u.\n",
2300-
map_name, var_extra->linkage);
2312+
if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
2313+
pr_warn("map '%s': unsupported map linkage %s.\n",
2314+
map_name, btf_var_linkage_str(var_extra->linkage));
23012315
return -EOPNOTSUPP;
23022316
}
23032317

0 commit comments

Comments
 (0)