Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: add documentation to compare clang "-target bpf" and default target

The added documentation explains how generated codes may differ
between clang bpf target and default target, and when to use
each target.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

authored by

Yonghong Song and committed by
Daniel Borkmann
6215ea6b 65073a67

+31
+31
Documentation/bpf/bpf_devel_QA.txt
··· 516 516 By the way, the BPF kernel selftests run with -mcpu=probe for better 517 517 test coverage. 518 518 519 + Q: In some cases clang flag "-target bpf" is used but in other cases the 520 + default clang target, which matches the underlying architecture, is used. 521 + What is the difference and when I should use which? 522 + 523 + A: Although LLVM IR generation and optimization try to stay architecture 524 + independent, "-target <arch>" still has some impact on generated code: 525 + 526 + - BPF program may recursively include header file(s) with file scope 527 + inline assembly codes. The default target can handle this well, 528 + while bpf target may fail if bpf backend assembler does not 529 + understand these assembly codes, which is true in most cases. 530 + 531 + - When compiled without -g, additional elf sections, e.g., 532 + .eh_frame and .rela.eh_frame, may be present in the object file 533 + with default target, but not with bpf target. 534 + 535 + - The default target may turn a C switch statement into a switch table 536 + lookup and jump operation. Since the switch table is placed 537 + in the global readonly section, the bpf program will fail to load. 538 + The bpf target does not support switch table optimization. 539 + The clang option "-fno-jump-tables" can be used to disable 540 + switch table generation. 541 + 542 + You should use default target when: 543 + 544 + - Your program includes a header file, e.g., ptrace.h, which eventually 545 + pulls in some header files containing file scope host assembly codes. 546 + - You can add "-fno-jump-tables" to work around the switch table issue. 547 + 548 + Otherwise, you can use bpf target. 549 + 519 550 Happy BPF hacking!