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 |
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
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
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 --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
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(-)