diff mbox series

[bpf-next] bpftool: Clean up HOST_CFLAGS, HOST_LDFLAGS for bootstrap bpftool

Message ID 20240320014103.45641-1-qmo@kernel.org (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [bpf-next] bpftool: Clean up HOST_CFLAGS, HOST_LDFLAGS for bootstrap bpftool | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 6 maintainers not CCed: quentin@isovalent.com llvm@lists.linux.dev morbo@google.com nathan@kernel.org justinstitt@google.com ndesaulniers@google.com
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 27 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Quentin Monnet March 20, 2024, 1:41 a.m. UTC
Bpftool's Makefile uses $(HOST_CFLAGS) to build the bootstrap version of
bpftool, in order to pick the flags for the host (where we run the
bootstrap version) and not for the target system (where we plan to run
the full bpftool binary). But we pass too much information through this
variable.

In particular, we set HOST_CFLAGS by copying most of the $(CFLAGS); but
we do this after the feature detection for bpftool, which means that
$(CFLAGS), hence $(HOST_CFLAGS), contain all macro definitions for using
the different optional features. For example, -DHAVE_LLVM_SUPPORT may be
passed to the $(HOST_CFLAGS), even though the LLVM disassembler is not
used in the bootstrap version, and the related library may even be
missing for the host architecture.

A similar thing happens with the $(LDFLAGS), that we use unchanged for
linking the bootstrap version even though they may contains flags to
link against additional libraries.

To address the $(HOST_CFLAGS) issue, we move the definition of
$(HOST_CFLAGS) earlier in the Makefile, before the $(CFLAGS) update
resulting from the feature probing - none of which being relevant to the
bootstrap version. To clean up the $(LDFLAGS) for the bootstrap version,
we introduce a dedicated $(HOST_LDFLAGS) variable that we base on
$(LDFLAGS), before the feature probing as well.

On my setup, the following macro and libraries are removed from the
compiler invocation to build bpftool after this patch:

  -DUSE_LIBCAP
  -DHAVE_LLVM_SUPPORT
  -I/usr/lib/llvm-17/include
  -D_GNU_SOURCE
  -D__STDC_CONSTANT_MACROS
  -D__STDC_FORMAT_MACROS
  -D__STDC_LIMIT_MACROS
  -lLLVM-17
  -L/usr/lib/llvm-17/lib

Another advantage of cleaning up these flags is that displaying
available features with "bpftool version" becomes more accurate for the
bootstrap bpftool, and no longer reflects the features detected (and
available only) for the final binary.

Cc: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Quentin Monnet <qmo@kernel.org>
---
 tools/bpf/bpftool/Makefile | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Jiri Olsa March 20, 2024, 8:51 a.m. UTC | #1
On Wed, Mar 20, 2024 at 01:41:03AM +0000, Quentin Monnet wrote:
> Bpftool's Makefile uses $(HOST_CFLAGS) to build the bootstrap version of
> bpftool, in order to pick the flags for the host (where we run the
> bootstrap version) and not for the target system (where we plan to run
> the full bpftool binary). But we pass too much information through this
> variable.
> 
> In particular, we set HOST_CFLAGS by copying most of the $(CFLAGS); but
> we do this after the feature detection for bpftool, which means that
> $(CFLAGS), hence $(HOST_CFLAGS), contain all macro definitions for using
> the different optional features. For example, -DHAVE_LLVM_SUPPORT may be
> passed to the $(HOST_CFLAGS), even though the LLVM disassembler is not
> used in the bootstrap version, and the related library may even be
> missing for the host architecture.
> 
> A similar thing happens with the $(LDFLAGS), that we use unchanged for
> linking the bootstrap version even though they may contains flags to
> link against additional libraries.
> 
> To address the $(HOST_CFLAGS) issue, we move the definition of
> $(HOST_CFLAGS) earlier in the Makefile, before the $(CFLAGS) update
> resulting from the feature probing - none of which being relevant to the
> bootstrap version. To clean up the $(LDFLAGS) for the bootstrap version,
> we introduce a dedicated $(HOST_LDFLAGS) variable that we base on
> $(LDFLAGS), before the feature probing as well.
> 
> On my setup, the following macro and libraries are removed from the
> compiler invocation to build bpftool after this patch:
> 
>   -DUSE_LIBCAP
>   -DHAVE_LLVM_SUPPORT
>   -I/usr/lib/llvm-17/include
>   -D_GNU_SOURCE
>   -D__STDC_CONSTANT_MACROS
>   -D__STDC_FORMAT_MACROS
>   -D__STDC_LIMIT_MACROS
>   -lLLVM-17
>   -L/usr/lib/llvm-17/lib
> 
> Another advantage of cleaning up these flags is that displaying
> available features with "bpftool version" becomes more accurate for the
> bootstrap bpftool, and no longer reflects the features detected (and
> available only) for the final binary.
> 
> Cc: Jean-Philippe Brucker <jean-philippe@linaro.org>
> Signed-off-by: Quentin Monnet <qmo@kernel.org>

got some fuzz when applying it, but other than that lgtm

Acked-by: Jiri Olsa <jolsa@kernel.org>

jirka

> ---
>  tools/bpf/bpftool/Makefile | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
> index e9154ace80ff..972f8d727130 100644
> --- a/tools/bpf/bpftool/Makefile
> +++ b/tools/bpf/bpftool/Makefile
> @@ -89,6 +89,10 @@ ifneq ($(EXTRA_LDFLAGS),)
>  LDFLAGS += $(EXTRA_LDFLAGS)
>  endif
>  
> +HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
> +		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
> +HOST_LDFLAGS := $(LDFLAGS)
> +
>  INSTALL ?= install
>  RM ?= rm -f
>  
> @@ -178,9 +182,6 @@ ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
>    SRCS := $(filter-out jit_disasm.c,$(SRCS))
>  endif
>  
> -HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
> -		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
> -
>  BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
>  
>  BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
> @@ -238,7 +239,7 @@ $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
>  	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
>  
>  $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
> -	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
> +	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
>  
>  $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
>  	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
> -- 
> 2.34.1
>
Andrii Nakryiko March 21, 2024, 6:55 p.m. UTC | #2
On Wed, Mar 20, 2024 at 1:51 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Wed, Mar 20, 2024 at 01:41:03AM +0000, Quentin Monnet wrote:
> > Bpftool's Makefile uses $(HOST_CFLAGS) to build the bootstrap version of
> > bpftool, in order to pick the flags for the host (where we run the
> > bootstrap version) and not for the target system (where we plan to run
> > the full bpftool binary). But we pass too much information through this
> > variable.
> >
> > In particular, we set HOST_CFLAGS by copying most of the $(CFLAGS); but
> > we do this after the feature detection for bpftool, which means that
> > $(CFLAGS), hence $(HOST_CFLAGS), contain all macro definitions for using
> > the different optional features. For example, -DHAVE_LLVM_SUPPORT may be
> > passed to the $(HOST_CFLAGS), even though the LLVM disassembler is not
> > used in the bootstrap version, and the related library may even be
> > missing for the host architecture.
> >
> > A similar thing happens with the $(LDFLAGS), that we use unchanged for
> > linking the bootstrap version even though they may contains flags to
> > link against additional libraries.
> >
> > To address the $(HOST_CFLAGS) issue, we move the definition of
> > $(HOST_CFLAGS) earlier in the Makefile, before the $(CFLAGS) update
> > resulting from the feature probing - none of which being relevant to the
> > bootstrap version. To clean up the $(LDFLAGS) for the bootstrap version,
> > we introduce a dedicated $(HOST_LDFLAGS) variable that we base on
> > $(LDFLAGS), before the feature probing as well.
> >
> > On my setup, the following macro and libraries are removed from the
> > compiler invocation to build bpftool after this patch:
> >
> >   -DUSE_LIBCAP
> >   -DHAVE_LLVM_SUPPORT
> >   -I/usr/lib/llvm-17/include
> >   -D_GNU_SOURCE
> >   -D__STDC_CONSTANT_MACROS
> >   -D__STDC_FORMAT_MACROS
> >   -D__STDC_LIMIT_MACROS
> >   -lLLVM-17
> >   -L/usr/lib/llvm-17/lib
> >
> > Another advantage of cleaning up these flags is that displaying
> > available features with "bpftool version" becomes more accurate for the
> > bootstrap bpftool, and no longer reflects the features detected (and
> > available only) for the final binary.
> >
> > Cc: Jean-Philippe Brucker <jean-philippe@linaro.org>
> > Signed-off-by: Quentin Monnet <qmo@kernel.org>
>
> got some fuzz when applying it, but other than that lgtm
>
> Acked-by: Jiri Olsa <jolsa@kernel.org>
>

this was applied, but PW bot didn't notice it. I marked it as accepted
in patchworks, just FYI.

> jirka
>
> > ---
> >  tools/bpf/bpftool/Makefile | 9 +++++----
> >  1 file changed, 5 insertions(+), 4 deletions(-)
> >
> > diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
> > index e9154ace80ff..972f8d727130 100644
> > --- a/tools/bpf/bpftool/Makefile
> > +++ b/tools/bpf/bpftool/Makefile
> > @@ -89,6 +89,10 @@ ifneq ($(EXTRA_LDFLAGS),)
> >  LDFLAGS += $(EXTRA_LDFLAGS)
> >  endif
> >
> > +HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
> > +             $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
> > +HOST_LDFLAGS := $(LDFLAGS)
> > +
> >  INSTALL ?= install
> >  RM ?= rm -f
> >
> > @@ -178,9 +182,6 @@ ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
> >    SRCS := $(filter-out jit_disasm.c,$(SRCS))
> >  endif
> >
> > -HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
> > -             $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
> > -
> >  BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
> >
> >  BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
> > @@ -238,7 +239,7 @@ $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
> >       $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
> >
> >  $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
> > -     $(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
> > +     $(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
> >
> >  $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
> >       $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
> > --
> > 2.34.1
> >
Quentin Monnet March 22, 2024, 2:16 p.m. UTC | #3
2024-03-21 18:55 UTC+0000 ~ Andrii Nakryiko <andrii.nakryiko@gmail.com>
> On Wed, Mar 20, 2024 at 1:51 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>>
>> On Wed, Mar 20, 2024 at 01:41:03AM +0000, Quentin Monnet wrote:
>>> Bpftool's Makefile uses $(HOST_CFLAGS) to build the bootstrap version of
>>> bpftool, in order to pick the flags for the host (where we run the
>>> bootstrap version) and not for the target system (where we plan to run
>>> the full bpftool binary). But we pass too much information through this
>>> variable.
>>>
>>> In particular, we set HOST_CFLAGS by copying most of the $(CFLAGS); but
>>> we do this after the feature detection for bpftool, which means that
>>> $(CFLAGS), hence $(HOST_CFLAGS), contain all macro definitions for using
>>> the different optional features. For example, -DHAVE_LLVM_SUPPORT may be
>>> passed to the $(HOST_CFLAGS), even though the LLVM disassembler is not
>>> used in the bootstrap version, and the related library may even be
>>> missing for the host architecture.
>>>
>>> A similar thing happens with the $(LDFLAGS), that we use unchanged for
>>> linking the bootstrap version even though they may contains flags to
>>> link against additional libraries.
>>>
>>> To address the $(HOST_CFLAGS) issue, we move the definition of
>>> $(HOST_CFLAGS) earlier in the Makefile, before the $(CFLAGS) update
>>> resulting from the feature probing - none of which being relevant to the
>>> bootstrap version. To clean up the $(LDFLAGS) for the bootstrap version,
>>> we introduce a dedicated $(HOST_LDFLAGS) variable that we base on
>>> $(LDFLAGS), before the feature probing as well.
>>>
>>> On my setup, the following macro and libraries are removed from the
>>> compiler invocation to build bpftool after this patch:
>>>
>>>   -DUSE_LIBCAP
>>>   -DHAVE_LLVM_SUPPORT
>>>   -I/usr/lib/llvm-17/include
>>>   -D_GNU_SOURCE
>>>   -D__STDC_CONSTANT_MACROS
>>>   -D__STDC_FORMAT_MACROS
>>>   -D__STDC_LIMIT_MACROS
>>>   -lLLVM-17
>>>   -L/usr/lib/llvm-17/lib
>>>
>>> Another advantage of cleaning up these flags is that displaying
>>> available features with "bpftool version" becomes more accurate for the
>>> bootstrap bpftool, and no longer reflects the features detected (and
>>> available only) for the final binary.
>>>
>>> Cc: Jean-Philippe Brucker <jean-philippe@linaro.org>
>>> Signed-off-by: Quentin Monnet <qmo@kernel.org>
>>
>> got some fuzz when applying it, but other than that lgtm
>>
>> Acked-by: Jiri Olsa <jolsa@kernel.org>
>>
> 
> this was applied, but PW bot didn't notice it. I marked it as accepted
> in patchworks, just FYI.

Thanks for the notice!
Quentin
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
index e9154ace80ff..972f8d727130 100644
--- a/tools/bpf/bpftool/Makefile
+++ b/tools/bpf/bpftool/Makefile
@@ -89,6 +89,10 @@  ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+HOST_LDFLAGS := $(LDFLAGS)
+
 INSTALL ?= install
 RM ?= rm -f
 
@@ -178,9 +182,6 @@  ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
   SRCS := $(filter-out jit_disasm.c,$(SRCS))
 endif
 
-HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
-
 BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
 
 BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
@@ -238,7 +239,7 @@  $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
 	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
 
 $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
-	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
+	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
 
 $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
 	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@