public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE
@ 2023-10-25  6:19 Viktor Malik
  2023-10-25  6:19 ` [PATCH bpf-next 1/3] samples/bpf: Allow building with custom CFLAGS/LDFLAGS Viktor Malik
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Viktor Malik @ 2023-10-25  6:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Donald Zickus,
	Viktor Malik

Hi,

when trying to build samples/bpf as PIE in Fedora, we came across
several issues, mainly related to the way compiler/linker flags are
handled in samples/bpf/Makefile. The first 2 commits in this patchset
address these issues (see commit messages for details).

At the same time, this proposes to allow passing an already built
bpftool to samples/bpf/Makefile. The reason is to remove a redundant
build step but also because I was not able to find a correct combination
of build flags to build libbpf.a for samples/bpf/bpftool/ with -fPIE.

Viktor Malik (3):
  samples/bpf: Allow building with custom CFLAGS/LDFLAGS
  samples/bpf: Fix passing LDFLAGS to libbpf
  samples/bpf: Allow building with custom bpftool

 samples/bpf/Makefile | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

-- 
2.41.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH bpf-next 1/3] samples/bpf: Allow building with custom CFLAGS/LDFLAGS
  2023-10-25  6:19 [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE Viktor Malik
@ 2023-10-25  6:19 ` Viktor Malik
  2023-10-25  6:19 ` [PATCH bpf-next 2/3] samples/bpf: Fix passing LDFLAGS to libbpf Viktor Malik
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Viktor Malik @ 2023-10-25  6:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Donald Zickus,
	Viktor Malik

Currently, it is not possible to specify custom flags when building
samples/bpf. The flags are defined in TPROGS_CFLAGS/TPROGS_LDFLAGS
variables, however, when trying to override those from the make command,
compilation fails.

For example, when trying to build with PIE:

    $ make -C samples/bpf TPROGS_CFLAGS="-fpie" TPROGS_LDFLAGS="-pie"

This is because samples/bpf/Makefile updates these variables, especially
appends include paths to TPROGS_CFLAGS and these updates are overridden
by setting the variables from the make command.

This patch introduces variables TPROGS_USER_CFLAGS/TPROGS_USER_LDFLAGS
for this purpose, which can be set from the make command and their
values are propagated to TPROGS_CFLAGS/TPROGS_LDFLAGS.

Signed-off-by: Viktor Malik <vmalik@redhat.com>
---
 samples/bpf/Makefile | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 90af76fa9dd8..5a9805edec93 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -150,6 +150,9 @@ always-y += ibumad_kern.o
 always-y += hbm_out_kern.o
 always-y += hbm_edt_kern.o
 
+TPROGS_CFLAGS = $(TPROGS_USER_CFLAGS)
+TPROGS_LDFLAGS = $(TPROGS_USER_LDFLAGS)
+
 ifeq ($(ARCH), arm)
 # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
 # headers when arm instruction set identification is requested.
@@ -316,7 +319,7 @@ XDP_SAMPLE_CFLAGS += -Wall -O2 \
 		     -I$(LIBBPF_INCLUDE) \
 		     -I$(src)/../../tools/testing/selftests/bpf
 
-$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS)
+$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS) $(TPROGS_USER_CFLAGS)
 $(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
 # Override includes for trace_helpers.o because __must_check won't be defined
 # in our include path.
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH bpf-next 2/3] samples/bpf: Fix passing LDFLAGS to libbpf
  2023-10-25  6:19 [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE Viktor Malik
  2023-10-25  6:19 ` [PATCH bpf-next 1/3] samples/bpf: Allow building with custom CFLAGS/LDFLAGS Viktor Malik
@ 2023-10-25  6:19 ` Viktor Malik
  2023-10-25  6:19 ` [PATCH bpf-next 3/3] samples/bpf: Allow building with custom bpftool Viktor Malik
  2023-10-26 13:40 ` [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: Viktor Malik @ 2023-10-25  6:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Donald Zickus,
	Viktor Malik

samples/bpf/Makefile passes LDFLAGS=$(TPROGS_LDFLAGS) to libbpf build
without surrounding quotes, which may cause compilation errors when
passing custom TPROGS_USER_LDFLAGS.

For example:

    $ make -C samples/bpf/ TPROGS_USER_LDFLAGS="-Wl,--as-needed -specs=/usr/lib/gcc/x86_64-redhat-linux/13/libsanitizer.spec"
    make: Entering directory './samples/bpf'
    make -C ../../ M=./samples/bpf BPF_SAMPLES_PATH=./samples/bpf
    make[1]: Entering directory '.'
    make -C ./samples/bpf/../../tools/lib/bpf RM='rm -rf' EXTRA_CFLAGS="-Wall -O2 -Wmissing-prototypes -Wstrict-prototypes  -I./usr/include -I./tools/testing/selftests/bpf/ -I./samples/bpf/libbpf/include -I./tools/include -I./tools/perf -I./tools/lib -DHAVE_ATTR_TEST=0" \
            LDFLAGS=-Wl,--as-needed -specs=/usr/lib/gcc/x86_64-redhat-linux/13/libsanitizer.spec srctree=./samples/bpf/../../ \
            O= OUTPUT=./samples/bpf/libbpf/ DESTDIR=./samples/bpf/libbpf prefix= \
            ./samples/bpf/libbpf/libbpf.a install_headers
    make: invalid option -- 'c'
    make: invalid option -- '='
    make: invalid option -- '/'
    make: invalid option -- 'u'
    make: invalid option -- '/'
    [...]

Fix the error by properly quoting $(TPROGS_LDFLAGS).

Signed-off-by: Viktor Malik <vmalik@redhat.com>
Suggested-by: Donald Zickus <dzickus@redhat.com>
---
 samples/bpf/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 5a9805edec93..378b9f3e9321 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -254,7 +254,7 @@ clean:
 $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
 # Fix up variables inherited from Kbuild that tools/ build system won't like
 	$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
-		LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ \
+		LDFLAGS="$(TPROGS_LDFLAGS)" srctree=$(BPF_SAMPLES_PATH)/../../ \
 		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
 		$@ install_headers
 
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH bpf-next 3/3] samples/bpf: Allow building with custom bpftool
  2023-10-25  6:19 [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE Viktor Malik
  2023-10-25  6:19 ` [PATCH bpf-next 1/3] samples/bpf: Allow building with custom CFLAGS/LDFLAGS Viktor Malik
  2023-10-25  6:19 ` [PATCH bpf-next 2/3] samples/bpf: Fix passing LDFLAGS to libbpf Viktor Malik
@ 2023-10-25  6:19 ` Viktor Malik
  2023-10-26 13:40 ` [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: Viktor Malik @ 2023-10-25  6:19 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Donald Zickus,
	Viktor Malik

samples/bpf build its own bpftool boostrap to generate vmlinux.h as well
as some BPF objects. This is a redundant step if bpftool has been
already built, so update samples/bpf/Makefile such that it accepts a
path to bpftool passed via the BPFTOOL variable. The approach is
practically the same as tools/testing/selftests/bpf/Makefile uses.

Signed-off-by: Viktor Malik <vmalik@redhat.com>
---
 samples/bpf/Makefile | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 378b9f3e9321..933f6c3fe6b0 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -260,8 +260,9 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
 
 BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
 BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
-BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
-$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
+DEFAULT_BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
+BPFTOOL ?= $(DEFAULT_BPFTOOL)
+$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
 	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\
 		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
 
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE
  2023-10-25  6:19 [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE Viktor Malik
                   ` (2 preceding siblings ...)
  2023-10-25  6:19 ` [PATCH bpf-next 3/3] samples/bpf: Allow building with custom bpftool Viktor Malik
@ 2023-10-26 13:40 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-10-26 13:40 UTC (permalink / raw)
  To: Viktor Malik
  Cc: bpf, ast, daniel, andrii, martin.lau, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, dzickus

Hello:

This series was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Wed, 25 Oct 2023 08:19:11 +0200 you wrote:
> Hi,
> 
> when trying to build samples/bpf as PIE in Fedora, we came across
> several issues, mainly related to the way compiler/linker flags are
> handled in samples/bpf/Makefile. The first 2 commits in this patchset
> address these issues (see commit messages for details).
> 
> [...]

Here is the summary with links:
  - [bpf-next,1/3] samples/bpf: Allow building with custom CFLAGS/LDFLAGS
    https://git.kernel.org/bpf/bpf-next/c/870f09f1ba30
  - [bpf-next,2/3] samples/bpf: Fix passing LDFLAGS to libbpf
    https://git.kernel.org/bpf/bpf-next/c/f56bcfadf7d6
  - [bpf-next,3/3] samples/bpf: Allow building with custom bpftool
    https://git.kernel.org/bpf/bpf-next/c/37db10bc247d

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-10-26 13:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-25  6:19 [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE Viktor Malik
2023-10-25  6:19 ` [PATCH bpf-next 1/3] samples/bpf: Allow building with custom CFLAGS/LDFLAGS Viktor Malik
2023-10-25  6:19 ` [PATCH bpf-next 2/3] samples/bpf: Fix passing LDFLAGS to libbpf Viktor Malik
2023-10-25  6:19 ` [PATCH bpf-next 3/3] samples/bpf: Allow building with custom bpftool Viktor Malik
2023-10-26 13:40 ` [PATCH bpf-next 0/3] samples/bpf: Allow building as PIE patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox