On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@xxxxxxxxx> wrote: > > Allow bpf_object__open() to access files of either endianness, and convert > included BPF programs to native byte-order in-memory for introspection. > Loading BPF objects of non-native byte-order is still disallowed however. > > Signed-off-by: Tony Ambardar <tony.ambardar@xxxxxxxxx> > --- > tools/lib/bpf/libbpf.c | 49 +++++++++++++++++++++++++++------ > tools/lib/bpf/libbpf_internal.h | 11 ++++++++ > 2 files changed, 52 insertions(+), 8 deletions(-) > [...] > > + /* Validate ELF object endianness... */ > + if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB && > + ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { > + err = -LIBBPF_ERRNO__ENDIAN; > + pr_warn("elf: '%s' has unknown byte order\n", obj->path); > + goto errout; > + } > + /* and preserve outside lifetime of bpf_object_open() */ what does it mean "preserve outside lifetime" ? > + obj->byteorder = ehdr->e_ident[EI_DATA]; > + > + > + why so many empty lines?.. > if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { > pr_warn("elf: failed to get section names section index for %s: %s\n", > obj->path, elf_errmsg(-1)); [...] > err = bpf_object__elf_init(obj); > - err = err ? : bpf_object__check_endianness(obj); > err = err ? : bpf_object__elf_collect(obj); > err = err ? : bpf_object__collect_externs(obj); > err = err ? : bpf_object_fixup_btf(obj); > @@ -8500,6 +8529,10 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch > > if (obj->gen_loader) > bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); nit: add {} around if, both sides should either have or not have {} > + else if (!is_native_endianness(obj)) { > + pr_warn("object '%s' is not native endianness\n", obj->name); "object '%s': load is not supported in non-native endianness\n" > + return libbpf_err(-LIBBPF_ERRNO__ENDIAN); > + } > > err = bpf_object_prepare_token(obj); > err = err ? : bpf_object__probe_loading(obj); [...]