This patch added BPF support to LLD. BPF
program does not have a defined entry point.
But LLD can still help do linking different
BPF objects into one object.
The simplest way is to link different objects
in a relocatable way.
clang -target bpf -g -O2 -c t1.c t2.c
ld.lld -r t1.o t2.o -o final.o
The drawback is that .BTF section is not deduplicated
as lld just simply merged all .BTF sections and
didn't do deduplication so a post-linker tool
is needed to do deduplication.
But we can build a relocable object file
through LTO. LTO puts all IRs in the same module so
deduplication is automatically done by the compiler.
clang -target bpf -flto -O2 -g -c t1.c -o t1.bc
clang -target bpf -flto -O2 -g -c t2.c -o t2.bc
ld.lld -r t1.bc t2.bc -o final.o
Currently, lld unconditionally emits a section per function/datum
with LTO. The bpf loader library, libbpf, expects only
one .text or .data section. Some other bpf loader tools may
have similar assumptions. To make downstream bpf tools less
complicated, this patch also introduced a target callback function
emitFuncDataSections() which target can set whether it wants to
emit sections per function or datum. For BPF target,
the function will return true if a relocatable object is
to be generated.
Another change is needed for relocation for static function/variables.
LLVM bpf target has written the offset of the relocation symbol in
the instruction, so there is no need for further section-level
relocation writing into the instruction buffer.