From b6a0c483b7383fc42bf9a840adbbf987867043d2 Mon Sep 17 00:00:00 2001 From: Rodrigo Campos Date: Wed, 18 Oct 2023 14:42:16 +0200 Subject: [PATCH] libct/dmz: Support compiling on all arches When we added nolibc, we started using it unconditionally. But runc is currently being compiled on more arches than supported by nolibc, like MIPS. Let's compile using stdlib if the arch we are compiling on is not supported by nolibc. If compilation is broken in some arch, just removing it from the NOLIBC_GOARCHES variable should fix the compilation, as it will fallback to use the C stdlib. Signed-off-by: Rodrigo Campos --- libcontainer/dmz/Makefile | 18 +++++++++++++++--- libcontainer/dmz/README.md | 2 ++ libcontainer/dmz/_dmz.c | 8 ++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/libcontainer/dmz/Makefile b/libcontainer/dmz/Makefile index 57ff782bd..3c30db243 100644 --- a/libcontainer/dmz/Makefile +++ b/libcontainer/dmz/Makefile @@ -1,7 +1,19 @@ -# Get CC values for cross-compilation. +# Get GO, GOARCH and CC values for cross-compilation. include ../../cc_platform.mk -# We use the flags suggested in nolibc/nolibc.h, it makes the binary very small. +# List of GOARCH that nolibc supports, from: +# https://go.dev/doc/install/source#environment (with GOOS=linux) +# +# See nolibc supported arches in ./nolibc/arch-*.h +NOLIBC_GOARCHES := 386 amd64 arm arm64 loong64 ppc64le riscv64 s390x + +ifneq (,$(filter $(GOARCH), $(NOLIBC_GOARCHES))) + # We use the flags suggested in nolibc/nolibc.h, it makes the binary very small. + CFLAGS += -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib -lgcc +else + CFLAGS += -DRUNC_USE_STDLIB +endif + runc-dmz: _dmz.c - $(CC) $(CFLAGS) -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib -lgcc -static -o $@ $^ + $(CC) $(CFLAGS) -static -o $@ $^ $(STRIP) -gs $@ diff --git a/libcontainer/dmz/README.md b/libcontainer/dmz/README.md index 72d577019..3cfa913ff 100644 --- a/libcontainer/dmz/README.md +++ b/libcontainer/dmz/README.md @@ -13,4 +13,6 @@ The current version in that folder is from Linux 6.6-rc3 tag (556fb7131e03b02836 It also support all the architectures we support in runc. +If the GOARCH we use for compiling doesn't support nolibc, it fallbacks to using the C stdlib. + [nolibc-upstream]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/include/nolibc?h=v6.6-rc3 diff --git a/libcontainer/dmz/_dmz.c b/libcontainer/dmz/_dmz.c index 73ecf2541..2855e60d5 100644 --- a/libcontainer/dmz/_dmz.c +++ b/libcontainer/dmz/_dmz.c @@ -1,5 +1,9 @@ -#include "xstat.h" -#include "nolibc/nolibc.h" +#ifdef RUNC_USE_STDLIB +# include +#else +# include "xstat.h" +# include "nolibc/nolibc.h" +#endif extern char **environ;