On Thu, Aug 13, 2020 at 1:39 PM Andrii Nakryiko <andriin@xxxxxx> wrote: > > With libbpf and BTF it is pretty common to have libbpf built for one > architecture, while BTF information was generated for a different architecture > (typically, but not always, BPF). In such case, the size of a pointer might > differ betweem architectures. libbpf previously was always making an > assumption that pointer size for BTF is the same as native architecture > pointer size, but that breaks for cases where libbpf is built as 32-bit > library, while BTF is for 64-bit architecture. > > To solve this, add heuristic to determine pointer size by searching for `long` > or `unsigned long` integer type and using its size as a pointer size. Also, > allow to override the pointer size with a new API btf__set_pointer_size(), for > cases where application knows which pointer size should be used. User > application can check what libbpf "guessed" by looking at the result of > btf__pointer_size(). If it's not 0, then libbpf successfully determined a > pointer size, otherwise native arch pointer size will be used. > > For cases where BTF is parsed from ELF file, use ELF's class (32-bit or > 64-bit) to determine pointer size. > > Fixes: 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf") > Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion") > Signed-off-by: Andrii Nakryiko <andriin@xxxxxx> > --- > tools/lib/bpf/btf.c | 83 ++++++++++++++++++++++++++++++++++++++-- > tools/lib/bpf/btf.h | 2 + > tools/lib/bpf/btf_dump.c | 4 +- > tools/lib/bpf/libbpf.map | 2 + > 4 files changed, 87 insertions(+), 4 deletions(-) > [...] > > + switch (gelf_getclass(elf)) { > + case ELFCLASS32: > + btf__set_pointer_size(btf, 4); > + break; > + case ELFCLASS64: > + btf__set_pointer_size(btf, 8); > + break; > + default: > + pr_warn("failed to get ELF class (bitness) for %s\n", path); > + goto done; This is not right, it should have been a break, not sure what happened. I'll send v3, maybe the cover letter also won't go missing this time. > + } > + > if (btf_ext && btf_ext_data) { > *btf_ext = btf_ext__new(btf_ext_data->d_buf, > btf_ext_data->d_size); [...]