Skeletons: Support cross-compiling towards architectures with different endianness
TL;DR: When cross-compiling, host little-endian bootstrap bpftool cannot open a big-endian ELF to generate a skeleton from it and build target big-endian bpftool.
Long version: Currently, bpftool's Makefile compiles the skeleton-related programs (skeletons/*.bpf.c) without paying attention to the target architecture. When cross-compiling, say on a host with LE for a target with BE, this leads to runtime failure on "bpftool prog show", because bpftool cannot load the LE bytecode on the BE target machine. This is Christophe's output here.
So the first fix is to make the Makefile aware of the target endianness somehow, and to build this ELF with target endianness. But this is not enough, because when (host) boostrap bpftool opens the ELF to generate the skeleton from it before building the final (target) bpftool binary, then bpf_object__check_endianness() in libbpf refuses to open the ELF if endianness is not the same as on the host as seen here.
The way I see it, we'd need to make sure libbpf can work with ELFs of a different endianness -- assuming that's doable -- and to pass it an option to tell whether LE or BE is expected for a given ELF. Which in turn would require bootstrap bpftool to be aware of the target endianness.
https://lore.kernel.org/bpf/[email protected]/
Cc @chantra, just FYI - this is the issue that we discussed earlier today.
https://lore.kernel.org/bpf/[email protected]/t/#u
That patch series was merged with my latest revision. Hopefully the years of cross-endian trauma can now begin to heal... 🤕
Looking forward to the next libbpf 1.5.0 and bpftool 7.5.0 releases! 🙌