On 21/12/2022 11:03, SuHsueyu wrote: > I used clang and gcc to generate the obj files, and then readelf these > two files, I found that there is a problem with the machine item, the > machine item generated by clang is "Linux BPF", and the one generated > by gcc is my physical machine architecture "x86-64", which makes the > check of "ehdr->e_machine != EM_BPF" fail. > This seems to be a problem with my not specifying a compilation option > similar to clang target when compiling with gcc. > > On Wed, Dec 21, 2022 at 8:34 AM Andrii Nakryiko > <andrii.nakryiko@xxxxxxxxx> wrote: >> >> On Tue, Dec 20, 2022 at 3:56 AM SuHsueyu <anolasc13@xxxxxxxxx> wrote: >>> >>> Hello, I use gcc 12.1.0 to compile a source file: >>> t.c >>> struct t { >>> int a:2; >>> int b:3; >>> int c:2; >>> } g; >>> with gcc -c -gbtf t.c >>> and try to use libbpf API btf__parse_split, bpf_object__open, and >>> bpf_object__open to parse and load into the kernel, but it failed with >>> "libbpf: elf: /path/to/t.o is not a valid eBPF object file". >> >> if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) >> >> This check is failing in libbpf. So check which of those two are not >> set appropriately. cc Jose to point where to report GCC-BPF specific >> issues. >> >>> >>> Is it wrong for me to do so? Due to some constraint, I cannot use >>> clang but gcc. How to parse and load gcc compiled object file with >>> libbpf? As mentioned https://gcc.gnu.org/wiki/BPFBackEnd has some details; I used those to install gcc-bpf-unknown-none/binutils-bpf-unknown-none (package names differ for different distros so use the wiki page to find the ones you need) and ran $ bpf-unknown-none-gcc t.c -c -o t.o -gbtf $ bpftool btf dump file t.o [1] STRUCT 't' size=4 vlen=3 'a' type_id=2 bits_offset=0 bitfield_size=2 'b' type_id=2 bits_offset=2 bitfield_size=3 'c' type_id=2 bits_offset=5 bitfield_size=2 [2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED [3] VAR 'g' type_id=1, linkage=global [4] DATASEC '.bss' size=0 vlen=1 type_id=3 offset=0 size=4 (VAR 'g') $ file t.o t.o: ELF 64-bit LSB relocatable, eBPF, version 1 (SYSV), not stripped Hope this helps, Alan