How to cross compile bpf targets for linux arm on Mac M-series laptop?

267 views Asked by At

I am trying to discover and run a simple eBPF application on a local Linux VM on my Mac M1 laptop. For better efficiency, I chose to run an arm-based Ubuntu on UTM(an opensource VMM).

However, I am having trouble with compiling eBPF elf files. The most common way that I found on various tech blogs to compile is

clang -I headers -O -target bpf -c bpf/xdp.c -o bpf/xdp.elf

Personally, I try to use Mac clang to cross compile with target bpf-linux-eabi, but seems like the clang in Mac does not support this kind of target

error: unable to create target: 'No available targets are compatible with triple "bpf-unknown-linux-eabi"'

And clang -print-targets shows that

  Registered Targets:
    aarch64    - AArch64 (little endian)
    aarch64_32 - AArch64 (little endian ILP32)
    aarch64_be - AArch64 (big endian)
    arm        - ARM
    arm64      - ARM64 (little endian)
    arm64_32   - ARM64 (little endian ILP32)
    armeb      - ARM (big endian)
    thumb      - Thumb
    thumbeb    - Thumb (big endian)
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64

Obviously no bpf target found. BTW The clang version of Mac Ventura is 14.0.

I also tried to find a Docker clang image, only linux/amd64 versions are available(I need linux/arm64).

Anyone help to figure out how to cross compile elf files on Mac? Either by Mac clang or docker clang. Thank you in advance.

1

There are 1 answers

0
Dylan Reimerink On

When clang is compiled you can chose which targets are included or not. Your distro likely compiled clang without BPF support so you might have to manually compile clang instead.

Here is the getting started page which instructions on how to do so https://clang.llvm.org/get_started.html

There are a bunch of knobs during compilation the important ones found here especially important in your case is to make sure BPF is included in LLVM_TARGETS_TO_BUILD which controls this list of compiled in target platforms.