Skip to contentSkip to conversation

bpf/selftests: Allow vmtest.sh to build statically linked test_progs.

PATCH Superseded KP Singh 3 participants 6 messages Mar 4, 2022 – Mar 7, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

KP Singh

author
#0 lore raw

PATCH 1/1 bpf/selftests: Allow vmtest.sh to build statically linked test_progs.

Dynamic linking when compiling on the host can cause issues when the libc version does not match the one in the VM image. Allow the user to use static compilation when this issue arises:

Before:
./vmtest.sh -- ./test_progs -t test_ima
./test_progs: /usr/lib/libc.so.6: version `GLIBC_2.33' not found (required by ./test_progs)

After:

TRUNNER_LDFLAGS=-static ./vmtest.sh -- ./test_progs -t test_ima test_ima:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Not using static as the default as some distros may not have dependent static libraries.

---
 tools/testing/selftests/bpf/Makefile  | 4 ++--
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +2 -2
index fe12b4f5fe20..2473c9b0cb2e 100644
@@ -162,7 +162,7 @@ $(MAKE_DIRS):
 
 $(OUTPUT)/%.o: %.c
 	$(call msg,CC,,$@)
-	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
+	$(Q)$(CC) $(CFLAGS) $(TRUNNER_LDFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
 
 $(OUTPUT)/%:%.c
 	$(call msg,BINARY,,$@)
@@ -468,7 +468,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
 			     $(RESOLVE_BTFIDS)				\
 			     | $(TRUNNER_BINARY)-extras
 	$$(call msg,BINARY,,$$@)
-	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+	$(Q)$$(CC) $$(CFLAGS) $(TRUNNER_LDFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
 	$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
 	$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool
 
tools/testing/selftests/bpf/vmtest.sh +1 -1
index e0bb04a97e10..a8bf6ceb3d06 100755
@@ -155,7 +155,7 @@ update_selftests()
 	local selftests_dir="${kernel_checkout}/tools/testing/selftests/bpf"
 
 	cd "${selftests_dir}"
-	${make_command}
+	${make_command} TRUNNER_LDFLAGS=-static
 
 	# Mount the image and copy the selftests to the image.
 	mount_image
Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
author
#1 lore raw
Quoted 52 lines · KP Singh

Dynamic linking when compiling on the host can cause issues when the libc version does not match the one in the VM image. Allow the user to use static compilation when this issue arises:

Before:
./vmtest.sh -- ./test_progs -t test_ima
./test_progs: /usr/lib/libc.so.6: version `GLIBC_2.33' not found (required by ./test_progs)

After:

TRUNNER_LDFLAGS=-static ./vmtest.sh -- ./test_progs -t test_ima test_ima:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Not using static as the default as some distros may not have dependent static libraries.

---
 tools/testing/selftests/bpf/Makefile  | 4 ++--
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +2 -2
index fe12b4f5fe20..2473c9b0cb2e 100644
@@ -162,7 +162,7 @@ $(MAKE_DIRS):
 
 $(OUTPUT)/%.o: %.c
        $(call msg,CC,,$@)
-       $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
+       $(Q)$(CC) $(CFLAGS) $(TRUNNER_LDFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
 
 $(OUTPUT)/%:%.c
        $(call msg,BINARY,,$@)
@@ -468,7 +468,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)                   \
                             $(RESOLVE_BTFIDS)                          \
                             | $(TRUNNER_BINARY)-extras
        $$(call msg,BINARY,,$$@)
-       $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+       $(Q)$$(CC) $$(CFLAGS) $(TRUNNER_LDFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
        $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
        $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool
 
tools/testing/selftests/bpf/vmtest.sh +0 -0
index e0bb04a97e10..a8bf6ceb3d06 100755

tools/testing/selftests/bpf/vmtest.sh update_selftests() · line 158

Quoted 5 lines
tools/testing/selftests/bpf/vmtest.sh +1 -1
@@ -155,7 +155,7 @@ update_selftests()
        local selftests_dir="${kernel_checkout}/tools/testing/selftests/bpf"
 
        cd "${selftests_dir}"
-       ${make_command}
+       ${make_command} TRUNNER_LDFLAGS=-static

This is not what you want to do KP, amend your commit so that -static is not the default.

Okay, resending.

Quoted 6 lines
tools/testing/selftests/bpf/vmtest.sh +0 -1
        # Mount the image and copy the selftests to the image.
        mount_image
--

2.35.1.616.g0bdcbb4464-goog

#2 lore raw
In reply to KP Singh
Quoted 52 lines · KP Singh

Dynamic linking when compiling on the host can cause issues when the libc version does not match the one in the VM image. Allow the user to use static compilation when this issue arises:

Before:
./vmtest.sh -- ./test_progs -t test_ima
./test_progs: /usr/lib/libc.so.6: version `GLIBC_2.33' not found (required by ./test_progs)

After:

TRUNNER_LDFLAGS=-static ./vmtest.sh -- ./test_progs -t test_ima test_ima:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Not using static as the default as some distros may not have dependent static libraries.

---
 tools/testing/selftests/bpf/Makefile  | 4 ++--
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +2 -2
index fe12b4f5fe20..2473c9b0cb2e 100644
@@ -162,7 +162,7 @@ $(MAKE_DIRS):
 
 $(OUTPUT)/%.o: %.c
        $(call msg,CC,,$@)
-       $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
+       $(Q)$(CC) $(CFLAGS) $(TRUNNER_LDFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
 
 $(OUTPUT)/%:%.c
        $(call msg,BINARY,,$@)
@@ -468,7 +468,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)                   \
                             $(RESOLVE_BTFIDS)                          \
                             | $(TRUNNER_BINARY)-extras
        $$(call msg,BINARY,,$$@)
-       $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+       $(Q)$$(CC) $$(CFLAGS) $(TRUNNER_LDFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
        $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
        $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool
 
tools/testing/selftests/bpf/vmtest.sh +0 -0
index e0bb04a97e10..a8bf6ceb3d06 100755

tools/testing/selftests/bpf/vmtest.sh update_selftests() · line 158

Quoted 5 lines
tools/testing/selftests/bpf/vmtest.sh +1 -1
@@ -155,7 +155,7 @@ update_selftests()
        local selftests_dir="${kernel_checkout}/tools/testing/selftests/bpf"
 
        cd "${selftests_dir}"
-       ${make_command}
+       ${make_command} TRUNNER_LDFLAGS=-static

In the commit message you mentioned we are not making it default for everyone, Yet here making it default?
Also, do we need to add a new TRUNNER_LDFLAGS ? Why not just LDFLAGS, we also use CFLAGS. Thinking ahead if we want add ASAN support later, maybe add a switch to vmtest.sh for choosing flavor to run would be better.

Quoted 6 lines
tools/testing/selftests/bpf/vmtest.sh +0 -1
        # Mount the image and copy the selftests to the image.
        mount_image
--

2.35.1.616.g0bdcbb4464-goog

author
#3 lore raw
Quoted 56 lines · On Fri, Mar 4, 2022 at 5:57 PM [email protected] <[email protected]> wrote:
Quoted 54 lines · KP Singh

Dynamic linking when compiling on the host can cause issues when the libc version does not match the one in the VM image. Allow the user to use static compilation when this issue arises:

Before:
./vmtest.sh -- ./test_progs -t test_ima
./test_progs: /usr/lib/libc.so.6: version `GLIBC_2.33' not found (required by ./test_progs)

After:

TRUNNER_LDFLAGS=-static ./vmtest.sh -- ./test_progs -t test_ima test_ima:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Not using static as the default as some distros may not have dependent static libraries.

---
 tools/testing/selftests/bpf/Makefile  | 4 ++--
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +2 -2
index fe12b4f5fe20..2473c9b0cb2e 100644
@@ -162,7 +162,7 @@ $(MAKE_DIRS):
 
 $(OUTPUT)/%.o: %.c
        $(call msg,CC,,$@)
-       $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
+       $(Q)$(CC) $(CFLAGS) $(TRUNNER_LDFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
 
 $(OUTPUT)/%:%.c
        $(call msg,BINARY,,$@)
@@ -468,7 +468,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)                   \
                             $(RESOLVE_BTFIDS)                          \
                             | $(TRUNNER_BINARY)-extras
        $$(call msg,BINARY,,$$@)
-       $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+       $(Q)$$(CC) $$(CFLAGS) $(TRUNNER_LDFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
        $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
        $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool
 
tools/testing/selftests/bpf/vmtest.sh +0 -0
index e0bb04a97e10..a8bf6ceb3d06 100755
@@ -155,7 +155,7 @@ update_selftests()
        local selftests_dir="${kernel_checkout}/tools/testing/selftests/bpf"
 
Quoted 6 lines

tools/testing/selftests/bpf/vmtest.sh update_selftests() · line 158

Quoted 3 lines
tools/testing/selftests/bpf/vmtest.sh +1 -1
        cd "${selftests_dir}"
-       ${make_command}
+       ${make_command} TRUNNER_LDFLAGS=-static

In the commit message you mentioned we are not making it default for everyone, Yet here making it default?

I have already sent a v2, maybe you missed that

https://lore.kernel.org/bpf/[email protected]/T/#u

after noticing my mistake

https://lore.kernel.org/bpf/CAJygYd1uwX05w3-+avZfz9d2a=8OD7VTMEv8Uo9AHLHrnu=k+Q@mail.gmail.com/T/#m1a3eedc831d8b27e5639b1d57e3af36a2b20f449

Quoted 2 lines

Also, do we need to add a new TRUNNER_LDFLAGS ? Why not just LDFLAGS, we also use CFLAGS. Thinking ahead if we want add ASAN support later,

Simple LDFLAGS and CFLAGS (not TRUNNER_*) did not work for me for some reason.

Quoted 2 lines

maybe add a switch to vmtest.sh for choosing flavor to run would be better.

I am not sure if we want to add a switch for everything. But I don't mind if folks think it would be useful.

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/vmtest.sh +0 -1
        # Mount the image and copy the selftests to the image.
        mount_image
--

2.35.1.616.g0bdcbb4464-goog

#4 lore raw
Quoted 69 lines · KP Singh
Quoted 56 lines · On Fri, Mar 4, 2022 at 5:57 PM [email protected] <[email protected]> wrote:
Quoted 54 lines · KP Singh

Dynamic linking when compiling on the host can cause issues when the libc version does not match the one in the VM image. Allow the user to use static compilation when this issue arises:

Before:
./vmtest.sh -- ./test_progs -t test_ima
./test_progs: /usr/lib/libc.so.6: version `GLIBC_2.33' not found (required by ./test_progs)

After:

TRUNNER_LDFLAGS=-static ./vmtest.sh -- ./test_progs -t test_ima test_ima:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Not using static as the default as some distros may not have dependent static libraries.

---
 tools/testing/selftests/bpf/Makefile  | 4 ++--
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +2 -2
index fe12b4f5fe20..2473c9b0cb2e 100644
@@ -162,7 +162,7 @@ $(MAKE_DIRS):
 
 $(OUTPUT)/%.o: %.c
        $(call msg,CC,,$@)
-       $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
+       $(Q)$(CC) $(CFLAGS) $(TRUNNER_LDFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
 
 $(OUTPUT)/%:%.c
        $(call msg,BINARY,,$@)
@@ -468,7 +468,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)                   \
                             $(RESOLVE_BTFIDS)                          \
                             | $(TRUNNER_BINARY)-extras
        $$(call msg,BINARY,,$$@)
-       $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+       $(Q)$$(CC) $$(CFLAGS) $(TRUNNER_LDFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
        $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
        $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool
 
tools/testing/selftests/bpf/vmtest.sh +0 -0
index e0bb04a97e10..a8bf6ceb3d06 100755
@@ -155,7 +155,7 @@ update_selftests()
        local selftests_dir="${kernel_checkout}/tools/testing/selftests/bpf"
 
Quoted 6 lines

tools/testing/selftests/bpf/vmtest.sh update_selftests() · line 158

Quoted 3 lines
tools/testing/selftests/bpf/vmtest.sh +1 -1
        cd "${selftests_dir}"
-       ${make_command}
+       ${make_command} TRUNNER_LDFLAGS=-static

In the commit message you mentioned we are not making it default for everyone, Yet here making it default?

I have already sent a v2, maybe you missed that

https://lore.kernel.org/bpf/[email protected]/T/#u

Quoted 8 lines

after noticing my mistake

https://lore.kernel.org/bpf/CAJygYd1uwX05w3-+avZfz9d2a=8OD7VTMEv8Uo9AHLHrnu=k+Q@mail.gmail.com/T/#m1a3eedc831d8b27e5639b1d57e3af36a2b20f449

Quoted 2 lines

Also, do we need to add a new TRUNNER_LDFLAGS ? Why not just LDFLAGS, we also use CFLAGS. Thinking ahead if we want add ASAN support later,

Simple LDFLAGS and CFLAGS (not TRUNNER_*) did not work for me for some reason.

I think LDFLAGS are not wired properly everywhere. But have you tried

LDLIBS=-static ./vmtest.sh ?

In my case I can't get libcap.a to be installed in my system, so compilation never completely succeeds, but I think it should get you what you want

Quoted 15 lines
Quoted 3 lines

maybe add a switch to vmtest.sh for choosing flavor to run would be better.

I am not sure if we want to add a switch for everything. But I don't mind if folks think it would be useful.

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/vmtest.sh +0 -1
        # Mount the image and copy the selftests to the image.
        mount_image
--

2.35.1.616.g0bdcbb4464-goog

author
#5 lore raw
Quoted 75 lines · Andrii Nakryiko
Quoted 73 lines · KP Singh
Quoted 56 lines · On Fri, Mar 4, 2022 at 5:57 PM [email protected] <[email protected]> wrote:
Quoted 54 lines · KP Singh

Dynamic linking when compiling on the host can cause issues when the libc version does not match the one in the VM image. Allow the user to use static compilation when this issue arises:

Before:
./vmtest.sh -- ./test_progs -t test_ima
./test_progs: /usr/lib/libc.so.6: version `GLIBC_2.33' not found (required by ./test_progs)

After:

TRUNNER_LDFLAGS=-static ./vmtest.sh -- ./test_progs -t test_ima test_ima:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Not using static as the default as some distros may not have dependent static libraries.

---
 tools/testing/selftests/bpf/Makefile  | 4 ++--
 tools/testing/selftests/bpf/vmtest.sh | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/Makefile +2 -2
index fe12b4f5fe20..2473c9b0cb2e 100644
@@ -162,7 +162,7 @@ $(MAKE_DIRS):
 
 $(OUTPUT)/%.o: %.c
        $(call msg,CC,,$@)
-       $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
+       $(Q)$(CC) $(CFLAGS) $(TRUNNER_LDFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
 
 $(OUTPUT)/%:%.c
        $(call msg,BINARY,,$@)
@@ -468,7 +468,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)                   \
                             $(RESOLVE_BTFIDS)                          \
                             | $(TRUNNER_BINARY)-extras
        $$(call msg,BINARY,,$$@)
-       $(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+       $(Q)$$(CC) $$(CFLAGS) $(TRUNNER_LDFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
        $(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
        $(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool
 
tools/testing/selftests/bpf/vmtest.sh +0 -0
index e0bb04a97e10..a8bf6ceb3d06 100755
@@ -155,7 +155,7 @@ update_selftests()
        local selftests_dir="${kernel_checkout}/tools/testing/selftests/bpf"
 
Quoted 6 lines

tools/testing/selftests/bpf/vmtest.sh update_selftests() · line 158

Quoted 3 lines
tools/testing/selftests/bpf/vmtest.sh +1 -1
        cd "${selftests_dir}"
-       ${make_command}
+       ${make_command} TRUNNER_LDFLAGS=-static

In the commit message you mentioned we are not making it default for everyone, Yet here making it default?

I have already sent a v2, maybe you missed that

https://lore.kernel.org/bpf/[email protected]/T/#u

after noticing my mistake

https://lore.kernel.org/bpf/CAJygYd1uwX05w3-+avZfz9d2a=8OD7VTMEv8Uo9AHLHrnu=k+Q@mail.gmail.com/T/#m1a3eedc831d8b27e5639b1d57e3af36a2b20f449

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Also, do we need to add a new TRUNNER_LDFLAGS ? Why not just LDFLAGS, we also use CFLAGS. Thinking ahead if we want add ASAN support later,

Simple LDFLAGS and CFLAGS (not TRUNNER_*) did not work for me for some reason.

I think LDFLAGS are not wired properly everywhere. But have you tried

LDLIBS=-static ./vmtest.sh ?

LDLIBS=-static ./vmtest.sh -j 72 -- ./test_progs -t test_ima

works for me. Sending a docs patch instead.

Quoted 20 lines

In my case I can't get libcap.a to be installed in my system, so compilation never completely succeeds, but I think it should get you what you want

Quoted 15 lines
Quoted 3 lines

maybe add a switch to vmtest.sh for choosing flavor to run would be better.

I am not sure if we want to add a switch for everything. But I don't mind if folks think it would be useful.

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/vmtest.sh +0 -1
        # Mount the image and copy the selftests to the image.
        mount_image
--

2.35.1.616.g0bdcbb4464-goog