[PATCH 5/5] selftests/sgx: Enclave freestanding compilation + separate linker options.

Jo Van Bulck posted 5 patches 2 years, 6 months ago
[PATCH 5/5] selftests/sgx: Enclave freestanding compilation + separate linker options.
Posted by Jo Van Bulck 2 years, 6 months ago
Fixes "'linker' input unused [-Wunused-command-line-argument]" errors when
compiling with clang.

Additionally pass -ffreestanding to prohibit memset/memcpy stdlib calls for
optimized enclave code.

Signed-off-by: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
---
 tools/testing/selftests/sgx/Makefile | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/sgx/Makefile b/tools/testing/selftests/sgx/Makefile
index c5483445ba28..aff419615462 100644
--- a/tools/testing/selftests/sgx/Makefile
+++ b/tools/testing/selftests/sgx/Makefile
@@ -12,9 +12,11 @@ OBJCOPY := $(CROSS_COMPILE)objcopy
 endif
 
 INCLUDES := -I$(top_srcdir)/tools/include
-HOST_CFLAGS := -Wall -Werror -g $(INCLUDES) -fPIC -z noexecstack
-ENCL_CFLAGS := -Wall -Werror -static -nostdlib -nostartfiles -fPIE \
-	       -fno-stack-protector -mrdrnd $(INCLUDES)
+HOST_CFLAGS := -Wall -Werror -g $(INCLUDES) -fPIC
+HOST_LDFLAGS := -z noexecstack -lcrypto
+ENCL_CFLAGS := -Wall -Werror -static -nostdlib -ffreestanding \
+	       -nostartfiles -fPIE -fno-stack-protector -mrdrnd $(INCLUDES)
+ENCL_LDFLAGS := -z noexecstack -Wl,--build-id=none
 
 TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx
 TEST_FILES := $(OUTPUT)/test_encl.elf
@@ -28,7 +30,7 @@ $(OUTPUT)/test_sgx: $(OUTPUT)/main.o \
 		    $(OUTPUT)/sigstruct.o \
 		    $(OUTPUT)/call.o \
 		    $(OUTPUT)/sign_key.o
-	$(CC) $(HOST_CFLAGS) -o $@ $^ -lcrypto
+	$(CC) $(HOST_CFLAGS) -o $@ $^ $(HOST_LDFLAGS)
 
 $(OUTPUT)/main.o: main.c
 	$(CC) $(HOST_CFLAGS) -c $< -o $@
@@ -46,7 +48,7 @@ $(OUTPUT)/sign_key.o: sign_key.S
 	$(CC) $(HOST_CFLAGS) -c $< -o $@
 
 $(OUTPUT)/test_encl.elf: test_encl.lds test_encl.c test_encl_bootstrap.S
-	$(CC) $(ENCL_CFLAGS) -T $^ -o $@ -Wl,--build-id=none
+	$(CC) $(ENCL_CFLAGS) -T $^ -o $@ $(ENCL_LDFLAGS)
 
 EXTRA_CLEAN := \
 	$(OUTPUT)/test_encl.elf \
-- 
2.34.1
Re: [PATCH 5/5] selftests/sgx: Enclave freestanding compilation + separate linker options.
Posted by Jarkko Sakkinen 2 years, 6 months ago
On Mon Jul 24, 2023 at 4:58 PM UTC, Jo Van Bulck wrote:
> Fixes "'linker' input unused [-Wunused-command-line-argument]" errors when
> compiling with clang.
>
> Additionally pass -ffreestanding to prohibit memset/memcpy stdlib calls for
> optimized enclave code.

Should be split into two patches. Please describe the motivation for the
second paragraph in the patch, which adds '-ffreestanding'.


BR, Jarkko
Re: [PATCH 5/5] selftests/sgx: Enclave freestanding compilation + separate linker options.
Posted by Jo Van Bulck 2 years, 6 months ago
On 28.07.23 21:22, Jarkko Sakkinen wrote:
> Should be split into two patches. 

Thanks, will do in the next patch revision.

> Please describe the motivation for the
> second paragraph in the patch, which adds '-ffreestanding'.

Even when passing -nostdlib, the compiler still assumes memset and 
memcpy are present [1].

I found that, when not passing '-ffreestanding', clang seems to optimize 
away the existing memcpy/memset implementations and errors with:

/bin/ld: test_encl.o: in function `do_encl_init_tcs_page':
test_encl.c:(.text+0x17e): undefined reference to `memset'

I will add this information in the next patch revision.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90701

Best,
Jo