Commit bbaf1ff0 authored by Fangrui Song's avatar Fangrui Song Committed by Daniel Borkmann
Browse files

bpf: Replace deprecated -target with --target= for Clang



The -target option has been deprecated since clang 3.4 in 2013. Therefore, use
the preferred --target=bpf form instead. This also matches how we use --target=
in scripts/Makefile.clang.

Signed-off-by: default avatarFangrui Song <maskray@google.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Acked-by: default avatarQuentin Monnet <quentin@isovalent.com>
Link: https://github.com/llvm/llvm-project/commit/274b6f0c87a6a1798de0a68135afc7f95def6277
Link: https://lore.kernel.org/bpf/20230624001856.1903733-1-maskray@google.com
parent da1a055d
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -635,12 +635,12 @@ test coverage.

Q: clang flag for target bpf?
-----------------------------
Q: In some cases clang flag ``-target bpf`` is used but in other cases the
Q: In some cases clang flag ``--target=bpf`` is used but in other cases the
default clang target, which matches the underlying architecture, is used.
What is the difference and when I should use which?

A: Although LLVM IR generation and optimization try to stay architecture
independent, ``-target <arch>`` still has some impact on generated code:
independent, ``--target=<arch>`` still has some impact on generated code:

- BPF program may recursively include header file(s) with file scope
  inline assembly codes. The default target can handle this well,
@@ -658,7 +658,7 @@ independent, ``-target <arch>`` still has some impact on generated code:
  The clang option ``-fno-jump-tables`` can be used to disable
  switch table generation.

- For clang ``-target bpf``, it is guaranteed that pointer or long /
- For clang ``--target=bpf``, it is guaranteed that pointer or long /
  unsigned long types will always have a width of 64 bit, no matter
  whether underlying clang binary or default target (or kernel) is
  32 bit. However, when native clang target is used, then it will
@@ -668,7 +668,7 @@ independent, ``-target <arch>`` still has some impact on generated code:
  while the BPF LLVM back end still operates in 64 bit. The native
  target is mostly needed in tracing for the case of walking ``pt_regs``
  or other kernel structures where CPU's register width matters.
  Otherwise, ``clang -target bpf`` is generally recommended.
  Otherwise, ``clang --target=bpf`` is generally recommended.

You should use default target when:

@@ -685,7 +685,7 @@ when:
  into these structures is verified by the BPF verifier and may result
  in verification failures if the native architecture is not aligned with
  the BPF architecture, e.g. 64-bit. An example of this is
  BPF_PROG_TYPE_SK_MSG require ``-target bpf``
  BPF_PROG_TYPE_SK_MSG require ``--target=bpf``


.. Links
+2 −2
Original line number Diff line number Diff line
@@ -990,7 +990,7 @@ format.::
    } g2;
    int main() { return 0; }
    int test() { return 0; }
    -bash-4.4$ clang -c -g -O2 -target bpf t2.c
    -bash-4.4$ clang -c -g -O2 --target=bpf t2.c
    -bash-4.4$ readelf -S t2.o
      ......
      [ 8] .BTF              PROGBITS         0000000000000000  00000247
@@ -1000,7 +1000,7 @@ format.::
      [10] .rel.BTF.ext      REL              0000000000000000  000007e0
           0000000000000040  0000000000000010          16     9     8
      ......
    -bash-4.4$ clang -S -g -O2 -target bpf t2.c
    -bash-4.4$ clang -S -g -O2 --target=bpf t2.c
    -bash-4.4$ cat t2.s
      ......
            .section        .BTF,"",@progbits
+3 −3
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ For example, for the following code::
    return g1 + g2 + l1 + l2;
  }

Compiled with ``clang -target bpf -O2 -c test.c``, the following is
Compiled with ``clang --target=bpf -O2 -c test.c``, the following is
the code with ``llvm-objdump -dr test.o``::

       0:       18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0 ll
@@ -157,7 +157,7 @@ and ``call`` instructions. For example::
    return gfunc(a, b) +  lfunc(a, b) + global;
  }

Compiled with ``clang -target bpf -O2 -c test.c``, we will have
Compiled with ``clang --target=bpf -O2 -c test.c``, we will have
following code with `llvm-objdump -dr test.o``::

  Disassembly of section .text:
@@ -203,7 +203,7 @@ The following is an example to show how R_BPF_64_ABS64 could be generated::
  int global() { return 0; }
  struct t { void *g; } gbl = { global };

Compiled with ``clang -target bpf -O2 -g -c test.c``, we will see a
Compiled with ``clang --target=bpf -O2 -g -c test.c``, we will see a
relocation below in ``.data`` section with command
``llvm-readelf -r test.o``::

+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ entrypoints.lskel.h: $(OUTPUT)/entrypoints.bpf.o | $(BPFTOOL)

$(OUTPUT)/entrypoints.bpf.o: entrypoints.bpf.c $(OUTPUT)/vmlinux.h $(BPFOBJ) | $(OUTPUT)
	$(call msg,BPF,$@)
	$(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES)			      \
	$(Q)$(CLANG) -g -O2 --target=bpf $(INCLUDES)			      \
		 -c $(filter %.c,$^) -o $@ &&				      \
	$(LLVM_STRIP) -g $@

+1 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ iterators.lskel-%.h: $(OUTPUT)/%/iterators.bpf.o | $(BPFTOOL)
$(OUTPUT)/%/iterators.bpf.o: iterators.bpf.c $(BPFOBJ) | $(OUTPUT)
	$(call msg,BPF,$@)
	$(Q)mkdir -p $(@D)
	$(Q)$(CLANG) -g -O2 -target bpf -m$* $(INCLUDES)		      \
	$(Q)$(CLANG) -g -O2 --target=bpf -m$* $(INCLUDES)		      \
		 -c $(filter %.c,$^) -o $@ &&				      \
	$(LLVM_STRIP) -g $@

Loading