diff mbox series

[v3,9/9] selftests/sgx: Specify freestanding environment for enclave compilation

Message ID 20230819094332.8535-10-jo.vanbulck@cs.kuleuven.be (mailing list archive)
State New, archived
Headers show
Series selftests/sgx: Fix compilation errors | expand

Commit Message

Jo Van Bulck Aug. 19, 2023, 9:43 a.m. UTC
Use -ffreestanding to assert the enclave compilation targets a
freestanding environment (i.e., without "main" or standard libraries).
This fixes clang reporting "undefined reference to `memset'" after
erroneously optimizing away the provided memset/memcpy implementations.

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

Comments

Jarkko Sakkinen Aug. 22, 2023, 10:14 a.m. UTC | #1
On Sat Aug 19, 2023 at 12:43 PM EEST, Jo Van Bulck wrote:
> Use -ffreestanding to assert the enclave compilation targets a
> freestanding environment (i.e., without "main" or standard libraries).
> This fixes clang reporting "undefined reference to `memset'" after
> erroneously optimizing away the provided memset/memcpy implementations.
>
> Signed-off-by: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
> ---
>  tools/testing/selftests/sgx/Makefile | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/sgx/Makefile b/tools/testing/selftests/sgx/Makefile
> index 2de970f72..19a07e890 100644
> --- a/tools/testing/selftests/sgx/Makefile
> +++ b/tools/testing/selftests/sgx/Makefile
> @@ -14,8 +14,8 @@ endif
>  INCLUDES := -I$(top_srcdir)/tools/include
>  HOST_CFLAGS := -Wall -Werror -g $(INCLUDES) -fPIC
>  HOST_LDFLAGS := -z noexecstack -lcrypto
> -ENCL_CFLAGS := -Wall -Werror -static-pie -nostdlib -nostartfiles -fPIE \
> -	       -fno-stack-protector -mrdrnd $(INCLUDES)
> +ENCL_CFLAGS := -Wall -Werror -static-pie -nostdlib -ffreestanding -fPIE \
> +		-nostartfiles -fno-stack-protector -mrdrnd $(INCLUDES)
>  ENCL_LDFLAGS := -Wl,-T,test_encl.lds,--build-id=none
>  
>  TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx
> -- 
> 2.25.1

Do you still need nostdfiles and nostartfiles with freestanding?

BR, Jarkko
Jo Van Bulck Aug. 23, 2023, 12:57 p.m. UTC | #2
On 22.08.23 12:14, Jarkko Sakkinen wrote:
> Do you still need nostdfiles and nostartfiles with freestanding?

Thanks, good question. I tested that compiling with only -ffreestanding 
yields:

/* snipped */
/usr/local/bin/ld: 
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/rcrt1.o: in 
function `_start':
(.text+0x24): undefined reference to `main'
/* snipped */

So we definitely still need -nostartfiles to prevent the compiler/linker 
from introducing the standard system startup functions. However, in my 
understanding, -nostdlib (which is what I assume you mean with 
nostdfiles) already implies the individual options -nodefaultlibs and 
-nostartfiles.

Thus, we definitely still need -nostartfiles and I'm not 100% sure we 
don't need -nostdlib (though it compiles fine for me with only 
-nostartfiles). Gcc only specifies:

-ffreestanding
Assert that compilation targets a freestanding environment.  This 
implies -fno-builtin. A freestanding environment is one in which the 
standard library may not exist, and program startup may not necessarily 
be at "main".

Bottom line: I suggest to keep -nostdlib to be sure and remove 
-nostartfiles (as it is redundant). I'll include this in the next  patch 
iteration.

Best,
Jo
Jarkko Sakkinen Aug. 23, 2023, 5:31 p.m. UTC | #3
On Wed Aug 23, 2023 at 3:57 PM EEST, Jo Van Bulck wrote:
> On 22.08.23 12:14, Jarkko Sakkinen wrote:
> > Do you still need nostdfiles and nostartfiles with freestanding?
>
> Thanks, good question. I tested that compiling with only -ffreestanding 
> yields:
>
> /* snipped */
> /usr/local/bin/ld: 
> /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/rcrt1.o: in 
> function `_start':
> (.text+0x24): undefined reference to `main'
> /* snipped */
>
> So we definitely still need -nostartfiles to prevent the compiler/linker 
> from introducing the standard system startup functions. However, in my 
> understanding, -nostdlib (which is what I assume you mean with 
> nostdfiles) already implies the individual options -nodefaultlibs and 
> -nostartfiles.
>
> Thus, we definitely still need -nostartfiles and I'm not 100% sure we 
> don't need -nostdlib (though it compiles fine for me with only 
> -nostartfiles). Gcc only specifies:
>
> -ffreestanding
> Assert that compilation targets a freestanding environment.  This 
> implies -fno-builtin. A freestanding environment is one in which the 
> standard library may not exist, and program startup may not necessarily 
> be at "main".
>
> Bottom line: I suggest to keep -nostdlib to be sure and remove 
> -nostartfiles (as it is redundant). I'll include this in the next  patch 
> iteration.
>
> Best,
> Jo

OK, cool, sounds like plan.

BR, Jarkko
diff mbox series

Patch

diff --git a/tools/testing/selftests/sgx/Makefile b/tools/testing/selftests/sgx/Makefile
index 2de970f72..19a07e890 100644
--- a/tools/testing/selftests/sgx/Makefile
+++ b/tools/testing/selftests/sgx/Makefile
@@ -14,8 +14,8 @@  endif
 INCLUDES := -I$(top_srcdir)/tools/include
 HOST_CFLAGS := -Wall -Werror -g $(INCLUDES) -fPIC
 HOST_LDFLAGS := -z noexecstack -lcrypto
-ENCL_CFLAGS := -Wall -Werror -static-pie -nostdlib -nostartfiles -fPIE \
-	       -fno-stack-protector -mrdrnd $(INCLUDES)
+ENCL_CFLAGS := -Wall -Werror -static-pie -nostdlib -ffreestanding -fPIE \
+		-nostartfiles -fno-stack-protector -mrdrnd $(INCLUDES)
 ENCL_LDFLAGS := -Wl,-T,test_encl.lds,--build-id=none
 
 TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx