Skip to content

Commit 15728ad

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Fix BTF-defined map-in-map initialization on 32-bit host arches
Libbpf built in 32-bit mode should be careful about not conflating 64-bit BPF pointers in BPF ELF file and host architecture pointers. This patch fixes issue of incorrect initializating of map-in-map inner map slots due to such difference. Fixes: 646f02f ("libbpf: Add BTF-defined map-in-map support") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9028bbc commit 15728ad

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5195,7 +5195,8 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
51955195
static int bpf_object__collect_map_relos(struct bpf_object *obj,
51965196
GElf_Shdr *shdr, Elf_Data *data)
51975197
{
5198-
int i, j, nrels, new_sz, ptr_sz = sizeof(void *);
5198+
const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
5199+
int i, j, nrels, new_sz;
51995200
const struct btf_var_secinfo *vi = NULL;
52005201
const struct btf_type *sec, *var, *def;
52015202
const struct btf_member *member;
@@ -5244,7 +5245,7 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
52445245

52455246
vi = btf_var_secinfos(sec) + map->btf_var_idx;
52465247
if (vi->offset <= rel.r_offset &&
5247-
rel.r_offset + sizeof(void *) <= vi->offset + vi->size)
5248+
rel.r_offset + bpf_ptr_sz <= vi->offset + vi->size)
52485249
break;
52495250
}
52505251
if (j == obj->nr_maps) {
@@ -5280,17 +5281,20 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
52805281
return -EINVAL;
52815282

52825283
moff = rel.r_offset - vi->offset - moff;
5283-
if (moff % ptr_sz)
5284+
/* here we use BPF pointer size, which is always 64 bit, as we
5285+
* are parsing ELF that was built for BPF target
5286+
*/
5287+
if (moff % bpf_ptr_sz)
52845288
return -EINVAL;
5285-
moff /= ptr_sz;
5289+
moff /= bpf_ptr_sz;
52865290
if (moff >= map->init_slots_sz) {
52875291
new_sz = moff + 1;
5288-
tmp = realloc(map->init_slots, new_sz * ptr_sz);
5292+
tmp = realloc(map->init_slots, new_sz * host_ptr_sz);
52895293
if (!tmp)
52905294
return -ENOMEM;
52915295
map->init_slots = tmp;
52925296
memset(map->init_slots + map->init_slots_sz, 0,
5293-
(new_sz - map->init_slots_sz) * ptr_sz);
5297+
(new_sz - map->init_slots_sz) * host_ptr_sz);
52945298
map->init_slots_sz = new_sz;
52955299
}
52965300
map->init_slots[moff] = targ_map;

0 commit comments

Comments
 (0)