From 22e044c33967b0755766bdaf5d8de0eed62cd41c Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Tue, 23 Mar 2021 07:46:58 -0700 Subject: [PATCH] Fix compile when using tcc >0.9.27 (#238) This allows ksh to be compiled with versions of tcc that define __dso_handle in libtcc1.a, i.e., versions as of this commit: https://repo.or.cz/tinycc.git/commit/dd60b20c Older versions of tcc still fail to compile ksh, although now they fail after reaching the libdll feature test. I'm not sure if fixing that is feasible since even if I hack out the failing libdll feature test, ksh fails to link with a '__dso_handle' error. src/lib/libast/comp/atexit.c, src/lib/libast/features/lib, src/lib/libast/vmalloc/vmexit.c: - From what I've been able to gather the only OSes with support for on_exit are Linux and SunOS 4. However, on_exit takes two arguments, so the macro that defines it as taking one argument is incorrect. Since Solaris (SunOS 5) no longer has this call and the macro breaks on Linux, the clean fix is to remove it (atexit(3) is used instead). src/lib/libast/include/ast.h: - When compiling with tcc on FreeBSD, pretend to be gcc 2.95.3 instead of gcc 9.3.0. This stops /usr/include/math.h from activating gcc 3.0+ math compiler builtins that don't exist on tcc, while still identifying as gcc which is needed to avoid other FreeBSD system header breakage. src/cmd/builtin/Mamfile, src/cmd/builtin/features/pty, src/lib/libdll/Mamfile, src/lib/libdll/features/dll: - tcc forbids combining the -c compiler flag with -l* linker flags. Use the -lm flag in the iffe feature tests instead of the Mamfiles. This avoids iffe combining -lm with the -c flag. src/lib/libast/vmalloc/malloc.c: - Fix failure to compile with -D_std_malloc. This patch is from OpenSUSE: https://build.opensuse.org/package/view_file/shells/ksh/ksh93-malloc-hook.dif As it turns out tcc needs this change to build ksh with -D_std_malloc, so it has been applied. Co-authored-by: Martijn Dekker Resolves: https://github.com/ksh93/ksh/issues/232 --- src/cmd/builtin/Mamfile | 2 +- src/cmd/builtin/features/pty | 2 +- src/lib/libast/comp/atexit.c | 6 +----- src/lib/libast/features/lib | 2 +- src/lib/libast/include/ast.h | 13 +++++++++++++ src/lib/libast/vmalloc/malloc.c | 4 ---- src/lib/libast/vmalloc/vmexit.c | 2 +- src/lib/libdll/Mamfile | 2 +- src/lib/libdll/features/dll | 2 +- 9 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/cmd/builtin/Mamfile b/src/cmd/builtin/Mamfile index 873ad99d04fc..20f5e7a2a093 100644 --- a/src/cmd/builtin/Mamfile +++ b/src/cmd/builtin/Mamfile @@ -37,7 +37,7 @@ make install meta FEATURE/pty features/%>FEATURE/% features/pty pty make features/pty done features/pty - exec - iffe ${IFFEFLAGS} -v -c "${CC} ${mam_cc_FLAGS} ${KSH_RELFLAGS} ${CCFLAGS} ${LDFLAGS} -lm" ref ${mam_cc_L+-L${INSTALLROOT}/lib} -I${PACKAGE_ast_INCLUDE} -I${INSTALLROOT}/include ${mam_libast} ${mam_libcmd} : run features/pty + exec - iffe ${IFFEFLAGS} -v -c "${CC} ${mam_cc_FLAGS} ${KSH_RELFLAGS} ${CCFLAGS} ${LDFLAGS}" ref ${mam_cc_L+-L${INSTALLROOT}/lib} -I${PACKAGE_ast_INCLUDE} -I${INSTALLROOT}/include ${mam_libast} ${mam_libcmd} : run features/pty done FEATURE/pty generated make ${PACKAGE_ast_INCLUDE}/ast_time.h implicit done ${PACKAGE_ast_INCLUDE}/ast_time.h diff --git a/src/cmd/builtin/features/pty b/src/cmd/builtin/features/pty index 612609b36f21..ab4a041ec530 100755 --- a/src/cmd/builtin/features/pty +++ b/src/cmd/builtin/features/pty @@ -13,7 +13,7 @@ lib openpty,_getpty,ptsname -lutil lib grantpt,unlockpt,posix_openpt stdlib.h lib cfmakeraw termios.h -tst - output{ +tst - -lm output{ #include #if _lib_ptsname #include diff --git a/src/lib/libast/comp/atexit.c b/src/lib/libast/comp/atexit.c index d0df2d524b3f..c29ce5a73551 100644 --- a/src/lib/libast/comp/atexit.c +++ b/src/lib/libast/comp/atexit.c @@ -34,11 +34,7 @@ NoN(atexit) #else -#if _lib_onexit || _lib_on_exit - -#if !_lib_onexit -#define onexit on_exit -#endif +#if _lib_onexit extern int onexit(void(*)(void)); diff --git a/src/lib/libast/features/lib b/src/lib/libast/features/lib index 522c4c3002a1..39410ad93937 100644 --- a/src/lib/libast/features/lib +++ b/src/lib/libast/features/lib @@ -29,7 +29,7 @@ lib getopt,getsubopt,getopt_long,getopt_long_only lib glob,index,iswblank,iswctype,killpg,link,localeconv,madvise lib mbtowc,mbrtowc,memalign,memchr,memcpy,memdup,memmove,memset lib mkdir,mkfifo,mktemp,mktime -lib mount,on_exit,onexit,opendir,pathconf +lib mount,onexit,opendir,pathconf lib readlink,remove,rename,rewinddir,rindex,rmdir,setlocale lib setpgid,setpgrp,setpgrp2,setreuid,setsid,setuid,sigaction lib sigprocmask,sigsetmask,sigunblock,sigvec,socketpair diff --git a/src/lib/libast/include/ast.h b/src/lib/libast/include/ast.h index 00aa5fd805aa..3ce8eccf0b67 100644 --- a/src/lib/libast/include/ast.h +++ b/src/lib/libast/include/ast.h @@ -78,6 +78,19 @@ struct _sfio_s; #endif #endif +/* + * tcc on FreeBSD: Avoid using nonexistent math + * builtins by pretending to be an ancient gcc. + */ +#if __TINYC__ && __GNUC__ >= 3 && __FreeBSD__ +#undef __GNUC__ +#undef __GNUC_MINOR__ +#undef __GNUC_PATCHLEVEL__ +#define __GNUC__ 2 +#define __GNUC_MINOR__ 95 +#define __GNUC_PATCHLEVEL__ 3 +#endif + /* * exit() support -- this matches shell exit codes */ diff --git a/src/lib/libast/vmalloc/malloc.c b/src/lib/libast/vmalloc/malloc.c index 8639740dc986..5b3708f21186 100644 --- a/src/lib/libast/vmalloc/malloc.c +++ b/src/lib/libast/vmalloc/malloc.c @@ -1040,12 +1040,8 @@ extern Void_t* F1(_ast_valloc, size_t,n) { return valloc(n); } #if !_UWIN -#if !_malloc_hook - #include -#endif - typedef struct mallinfo Mallinfo_t; typedef struct mstats Mstats_t; diff --git a/src/lib/libast/vmalloc/vmexit.c b/src/lib/libast/vmalloc/vmexit.c index c027fd56ca7b..7752e09676b2 100644 --- a/src/lib/libast/vmalloc/vmexit.c +++ b/src/lib/libast/vmalloc/vmexit.c @@ -93,7 +93,7 @@ int type; return type; } -#endif /* _lib_onexit || _lib_on_exit */ +#endif /* _lib_onexit */ #endif /*!PACKAGE_ast*/ diff --git a/src/lib/libdll/Mamfile b/src/lib/libdll/Mamfile index 0ef420e967b9..29d9ddb5e82f 100644 --- a/src/lib/libdll/Mamfile +++ b/src/lib/libdll/Mamfile @@ -154,7 +154,7 @@ make install done features/dll bind -ldl dontcare bind -last - exec - iffe ${IFFEFLAGS} -v -c "${CC} ${mam_cc_FLAGS} ${KSH_RELFLAGS} ${CCFLAGS} ${LDFLAGS} -lm" ref ${mam_cc_L+-L.} ${mam_cc_L+-L${INSTALLROOT}/lib} -I${PACKAGE_ast_INCLUDE} -I${INSTALLROOT}/include ${mam_libdl} ${mam_libast} : run features/dll + exec - iffe ${IFFEFLAGS} -v -c "${CC} ${mam_cc_FLAGS} ${KSH_RELFLAGS} ${CCFLAGS} ${LDFLAGS}" ref ${mam_cc_L+-L.} ${mam_cc_L+-L${INSTALLROOT}/lib} -I${PACKAGE_ast_INCLUDE} -I${INSTALLROOT}/include ${mam_libdl} ${mam_libast} : run features/dll done FEATURE/dll generated exec - cmp 2>/dev/null -s FEATURE/dll dlldefs.h || { rm -f dlldefs.h; silent test -d . || mkdir .; cp FEATURE/dll dlldefs.h; } done dlldefs.h generated diff --git a/src/lib/libdll/features/dll b/src/lib/libdll/features/dll index e5d5790f2bd4..86163b46e016 100644 --- a/src/lib/libdll/features/dll +++ b/src/lib/libdll/features/dll @@ -74,7 +74,7 @@ tst run{ esac echo "#define _DLL_NEXT_PATH \"$lib\"" }end -tst - output{ +tst - -lm output{ #include #if defined(__MVS__) && !defined(__SUSV3) #define __SUSV3 1