1

I am working on a project with eBPF code with one file and it is getting quite long, I was hoping I could split it into multiple files. I am trying to build multiple files into bpf object files and then link them together into one object file. This is the Makefile I am using,

KDIR ?= /lib/modules/$(shell uname -r)/build
CLANG ?= clang
LLC ?= llc
ARCH := $(subst x86_64,x86,$(shell arch))

BIN := a.o
CLANG_FLAGS = -I. -I$(KDIR)/arch/$(ARCH)/include \
    -I$(KDIR)/arch/$(ARCH)/include/generated \
    -I$(KDIR)/include \
    -I$(KDIR)/arch/$(ARCH)/include/uapi \
    -I$(KDIR)/include/uapi \
    -include $(KDIR)/include/linux/kconfig.h \
    -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
    -D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \
    -Wno-gnu-variable-sized-type-not-at-end \
    -Wno-address-of-packed-member -Wno-tautological-compare \
    -O2 -emit-llvm

all: $(BIN)

a.o: program.c program.h
    $(CLANG) $(CLANG_FLAGS) -g -c $< -o - | \
    $(LLC) -march=bpf -mcpu=$(CPU) -filetype=obj -o $@

clean:
    rm -f *.o

How can I modify this Makefile to compile and link eBPF object file from multiple C files?

imawful
  • 69
  • 5
  • The question at the end, "How can I make multiple eBPF programs [...]?" hardly matches the question title, "Compiling [...] multiple files into a single bpf object file", at all. What are you trying to build, programs or object files? How many are you trying to build, one or multiple? How many sources do you actually have, one or several? – John Bollinger Mar 07 '23 at 21:11
  • Note: generally speaking, each source file maps to a separate object file. It is common to link multiple object files into a single *program*, or to combine them into a single *library*, but it is vanishingly rare to combine multiple object files into another *object file*. If that's really what you think you want to do, then why? – John Bollinger Mar 07 '23 at 21:14
  • I suppose you could `cat` all the .c-files and compile it. But putting all the object files in an archive file (.a) is the conventional approach. – Andreas Mar 08 '23 at 07:07
  • I have edited the question to make it more clear. So in this [article](https://reviews.llvm.org/D101336), they have explained how to link different object files, but they have initially used the target=bpf flag to compile to object files. I am not compiling that way, I am doing this to compile - `clang -O2 -emit-llvm -c my_bpf_program.c -o - | \ llc -march=bpf -mcpu=probe -filetype=obj -o my_bpf_objfile.o`. This takes the output of clang and pipes it to llc. So I was wondering how I can perform linking of object files in this case. – imawful Mar 08 '23 at 07:58

1 Answers1

2

Linking eBPF code from different object files into a single one is not typically supported by your compiler, because there's some handling specific to eBPF programs to do, but it's supported by libbpf (since v0.4.0), and by bpftool (v5.13) that relies on this library. In particular, you can use the following command to generate a single object files containing eBPF bytecode from multiple objects:

$ bpftool gen object <output_file> <input_file> [<input_file> ...]

See relevant bpftool documentation for more details.

Qeole
  • 8,284
  • 1
  • 24
  • 52