From 4197ee0a928bd84e123734a87b1b755b86b9d1f8 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Mon, 3 Aug 2020 21:40:37 +0200 Subject: some work on noreturn and exit/Halt --- ecomp-c/asm-i386.c | 4 ++-- ecomp-c/build.sh | 4 +++- ecomp-c/builtin_unreachable.c | 8 ++++++++ ecomp-c/ec.c | 2 +- ecomp-c/libc-freestanding.c | 17 +++++++++++++++-- 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 ecomp-c/builtin_unreachable.c diff --git a/ecomp-c/asm-i386.c b/ecomp-c/asm-i386.c index 83904c5..9b303c8 100644 --- a/ecomp-c/asm-i386.c +++ b/ecomp-c/asm-i386.c @@ -146,12 +146,12 @@ static void Err( char *s, va_list args ) fflush( stderr ); } -static void Halt( int code ) +__attribute__((noreturn)) static void Halt( int code ) { exit( code ); } -static void Abort( char *s, ... ) +__attribute__((noreturn)) static void Abort( char *s, ... ) { va_list args; va_start( args, s ); diff --git a/ecomp-c/build.sh b/ecomp-c/build.sh index 1fce90b..1d7551e 100755 --- a/ecomp-c/build.sh +++ b/ecomp-c/build.sh @@ -13,7 +13,7 @@ MODULES+=("$BINARY.c") case "${COMPILER}" in gcc) - CFLAGS="-m32 -march=i386 -Werror -Wno-noreturn -Wall -pedantic -std=c89 -x c" + CFLAGS="-m32 -march=i386 -Wall -pedantic -Werror -std=c89 -x c" ;; clang) CFLAGS="-m32 -march=i386 -Werror -Wall -pedantic -std=c89 -x c" @@ -62,10 +62,12 @@ case "${COMPILER}:${MODE}" in ;; pcc:freestanding) CFLAGS+=" -ffreestanding -nostdlib -Wl,-emain" + MODULES+=("builtin_unreachable.c") ;; tcc:freestanding) CFLAGS+=" -fno-bultin -nostdlib" MODULES+=("_start-stub.c") + MODULES+=("builtin_unreachable.c") ;; *:hosted) CFLAGS+=" -lbsd" diff --git a/ecomp-c/builtin_unreachable.c b/ecomp-c/builtin_unreachable.c new file mode 100644 index 0000000..4ba239e --- /dev/null +++ b/ecomp-c/builtin_unreachable.c @@ -0,0 +1,8 @@ +/* + * __builtin_unreachable for pcc/tcc marking exit in + * libc-freestanding.c syscall1noret + */ + +void __builtin_unreachable( void ) +{ +} diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c index bc0944e..82775e0 100644 --- a/ecomp-c/ec.c +++ b/ecomp-c/ec.c @@ -133,7 +133,7 @@ static void Err( char *s, va_list args ) fflush( stderr ); } -static void Halt( int code ) +__attribute__((noreturn)) static void Halt( int code ) { exit( code ); } diff --git a/ecomp-c/libc-freestanding.c b/ecomp-c/libc-freestanding.c index f37aa83..35f2e02 100644 --- a/ecomp-c/libc-freestanding.c +++ b/ecomp-c/libc-freestanding.c @@ -138,6 +138,19 @@ static __attribute__((noinline)) int syscall1( int id, int arg0 ) return retval; } +void __builtin_unreachable( void ); + +static __attribute__((noinline)) __attribute__((noreturn)) void syscall1noret( int id, int arg0 ) +{ + __asm__ volatile( "\ + push %%ebx\n\ + mov %0, %%eax\n\ + mov %1, %%ebx\n\ + int $0x80\n\ + pop %%ebx\n" : : "m"( id ), "m"( arg0 ) ); + __builtin_unreachable( ); +} + static __attribute__((noinline)) int syscall3( int id, int arg0, int arg1, int arg2 ) { int retval; @@ -164,9 +177,9 @@ static __attribute__((noinline)) int syscall3( int id, int arg0, int arg1, int a return retval; } -void exit( int status ) +__attribute__((noreturn)) void exit( int status ) { - syscall1( SYSCALL_EXIT, status ); + syscall1noret( SYSCALL_EXIT, status ); } static void strreverse( char *s ) -- cgit v1.2.3-54-g00ecf