Hi all --

Attached for review/testing is a diff that enables TCC to work on
Solaris/Illumos x86. Tested on an OpenIndiana (Illumos distro) machine,
where all tests pass when running TCC as an i386 compiler.
Solaris/Illumos is a multiarch environment. Modern Illumos is a 64-bit
kernel only, but 32-bit binaries work just fine; the Illumos people care
a lot about backwards compatibility.

Illumos is interesting because it is a variant of early Solaris 11, with
about a decade of divergence from Oracle. The way I set up support is to
declare Illumos effectively a sub TARGETOS of Solaris.

To build and test, you will want to ensure that the GNU tools are found
first in your PATH. This is because TCC assumes GNU install and GNU
diff, but on Illumos Sun install and Sun diff are found first with the
default PATH. You can fix this by running `export
PATH=/usr/gnu/bin:/usr/bin:/usr/sbin:/sbin` before building TCC. You'll
also need to run the configure script with `bash configure` as /bin/sh
on Illumos is not smart enough to run the configure script.

As mentioned, all tests pass on Illumos i386. In order to run the tests,
you need to rebuild TCC with itself. This is because GCC on Illumos
unconditionally declares itself to be amd64, even when in i386 mode. As
such, TCC built with GCC fails the tests because TCC thinks it's a cross
compiler and the -run test errors out because of this. Once you rebuild
TCC with itself, it'll correctly think it's a native compiler and run
all the tests.

I believe all the bits for amd64 support are there and good to go.
However, there is a linker issue that prevents binaries from working
correctly. The message you receive is:
ld.so.1: <exe name>: fatal: relocation error: file <exe name>: symbol
__start_crt_compiler: referenced symbol not found

However, you get what I think is the real problem if you run `ldd -d` on
the binary:
relocation R_AMD64_COPY offset invalid: _DYNAMIC: offset=0xa00a08 lies
outside memory image; relocation discarded

If you can help fix this, that would be great. With that said, the i386
version of TCC ought to be enough for the vast majority of use cases for
TCC on Solaris/Illumos, so I am sharing the diff now.

Is it worth waiting around for testing or should I just go ahead and
push this to mob?

Thanks.

~Brian
diff --git a/Makefile b/Makefile
index b52194a..df0ea27 100644
--- a/Makefile
+++ b/Makefile
@@ -91,6 +91,8 @@ NATIVE_DEFINES_$(CONFIG_arm64) += -DTCC_TARGET_ARM64
 NATIVE_DEFINES_$(CONFIG_riscv64) += -DTCC_TARGET_RISCV64
 NATIVE_DEFINES_$(CONFIG_BSD) += -DTARGETOS_$(TARGETOS)
 NATIVE_DEFINES_$(CONFIG_Android) += -DTARGETOS_ANDROID
+NATIVE_DEFINES_$(CONFIG_SunOS) += -DTARGETOS_SunOS
+NATIVE_DEFINES_$(CONFIG_Illumos) += -DTARGETOS_SunOS -DTARGETOS_Illumos
 NATIVE_DEFINES_$(CONFIG_pie) += -DCONFIG_TCC_PIE
 NATIVE_DEFINES_$(CONFIG_pic) += -DCONFIG_TCC_PIC
 NATIVE_DEFINES_no_$(CONFIG_new_macho) += -DCONFIG_NEW_MACHO=0
diff --git a/configure b/configure
index 6ec98f3..63227ff 100755
--- a/configure
+++ b/configure
@@ -61,6 +61,9 @@ targetos=`uname`
 if test "$targetos" = "Linux" ; then
   test "$(uname -o)" = "Android" && targetos=Android
 fi
+if test "$targetos" = "SunOS" ; then
+  test "$(uname -o)" = "illumos" && targetos=Illumos
+fi
 
 case $targetos in
   Darwin)
@@ -99,6 +102,12 @@ case $targetos in
       tcc_switches="-Wl,-rpath=$sysroot/lib,-section-alignment=0x1000"
     fi
     ;;
+  SunOS)
+    confvars="$confvars SunOS"
+    ;;
+  Illumos)
+    confvars="$confvars Illumos"
+    ;;
   *)
     ;;
 esac
@@ -388,6 +397,9 @@ if test -z "$cross_prefix" ; then
           if test -f "/usr/lib64/crti.o" ; then
             tcc_lddir="lib64"
           fi
+          if test -f "/usr/lib/amd64/crti.o" ; then
+            tcc_lddir="lib/amd64"
+          fi
         fi
       fi
 
diff --git a/conftest.c b/conftest.c
index ad7275c..7d883ba 100644
--- a/conftest.c
+++ b/conftest.c
@@ -20,6 +20,8 @@ const char *platform_macros[] = {
     "__FreeBSD_kernel__",   "TARGETOS_FreeBSD_kernel",
     "__OpenBSD__",          "TARGETOS_OpenBSD",
     "__NetBSD__",           "TARGETOS_NetBSD",
+    "__illumos__",          "TARGETOS_Illumos",
+    "__sun__",              "TARGETOS_SunOS",
     "__linux__",            "TARGETOS_Linux",
     "__ANDROID__",          "TARGETOS_ANDROID",
 
@@ -200,6 +202,10 @@ int main(int argc, char **argv)
 # define TRIPLET_OS "netbsd"
 #elif defined(__OpenBSD__)
 # define TRIPLET_OS "openbsd"
+#elif defined(__illumos__)
+# define TRIPLET_OS "illumos"
+#elif defined(__sun__)
+# define TRIPLET_OS "solaris"
 #elif defined(_WIN32)
 # define TRIPLET_OS "win32"
 #elif defined(__APPLE__)
diff --git a/include/tccdefs.h b/include/tccdefs.h
index f6c25a4..a606288 100644
--- a/include/tccdefs.h
+++ b/include/tccdefs.h
@@ -60,6 +60,14 @@
 #if defined _WIN32
     #define __WCHAR_TYPE__ unsigned short
     #define __WINT_TYPE__ unsigned short
+#elif defined __sun__
+    #ifdef __ILP32__
+        #define __WCHAR_TYPE__ long
+        #define __WINT_TYPE__ long
+    #else
+        #define __WCHAR_TYPE__ int
+        #define __WINT_TYPE__ int
+    #endif
 #elif defined __linux__
     #define __WCHAR_TYPE__ int
     #define __WINT_TYPE__ unsigned int
@@ -115,6 +123,13 @@
     #define __GNUC__ 4
     #define _ANSI_LIBRARY 1
 
+#elif defined __sun__
+    #define __GNUC__ 4
+    #define __GNUC_MINOR__ 3
+    #define __GNUC_PATCHLEVEL__ 3
+    #define _Pragma(x)
+    #define __ELF__ 1
+
 #elif defined __APPLE__
     /* emulate APPLE-GCC to make libc's headerfiles compile: */
     #define __GNUC__ 4   /* darwin emits warning on GCC<4 */
diff --git a/lib/bcheck.c b/lib/bcheck.c
index b8dd1c5..d339e1c 100644
--- a/lib/bcheck.c
+++ b/lib/bcheck.c
@@ -32,6 +32,10 @@
 #include <malloc.h>
 #endif
 
+#if defined(__sun__)
+#include <thread.h>
+#endif
+
 #if !defined(_WIN32)
 #include <unistd.h>
 #include <sys/syscall.h>
@@ -154,14 +158,14 @@ static pthread_spinlock_t bounds_spin;
 #define HAVE_SIGNAL            (1)
 #define HAVE_SIGACTION         (1)
 #define HAVE_FORK              (1)
-#if !defined(__APPLE__) && defined(__GNUC__) && (defined(__i386__) || 
defined(__x86_64__))
+#if !defined(__APPLE__) && !defined(__sun__) && defined(__GNUC__) && 
(defined(__i386__) || defined(__x86_64__))
 #define HAVE_TLS_FUNC          (0)
 #define HAVE_TLS_VAR           (1)
 #else
 #define HAVE_TLS_FUNC          (1)
 #define HAVE_TLS_VAR           (0)
 #endif
-#if defined TCC_MUSL || defined __ANDROID__
+#if defined TCC_MUSL || defined __ANDROID__ || defined __sun__
 # undef HAVE_CTYPE
 #endif
 #endif
@@ -232,6 +236,9 @@ typedef struct alloca_list_struct {
 #elif  defined(__NetBSD__)
 #define BOUND_TID_TYPE         pid_t
 #define BOUND_GET_TID(id)      id = syscall (SYS__lwp_self)
+#elif defined(__sun__)
+#define BOUND_TID_TYPE         pid_t
+#define BOUND_GET_TID(id)      id = thr_self()
 #elif defined(__linux__)
 #define BOUND_TID_TYPE         pid_t
 #define BOUND_GET_TID(id)      id = syscall (SYS_gettid)
@@ -1180,7 +1187,7 @@ void __attribute__((destructor)) __bound_exit(void)
     if (inited) {
 #if !defined(_WIN32) && !defined(__APPLE__) && !defined TCC_MUSL && \
     !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
-    !defined(__ANDROID__)
+    !defined(__ANDROID__) && !defined(__sun__)
         if (print_heap) {
             extern void __libc_freeres (void);
             __libc_freeres ();
diff --git a/tcc.c b/tcc.c
index 0c6f6ac..881d4dd 100644
--- a/tcc.c
+++ b/tcc.c
@@ -189,6 +189,10 @@ static const char version[] =
         " OpenBSD"
 #elif TARGETOS_NetBSD
         " NetBSD"
+#elif TARGETOS_Illumos
+        " Illumos"
+#elif TARGETOS_SunOS
+        " Solaris"
 #else
         " Linux"
 #endif
diff --git a/tcc.h b/tcc.h
index 78b0174..cf53484 100644
--- a/tcc.h
+++ b/tcc.h
@@ -312,6 +312,12 @@ extern long double strtold (const char *__nptr, char 
**__endptr);
 #  define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
 # elif TARGETOS_OpenBSD
 #  define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.so"
+# elif TARGETOS_SunOS
+#  ifdef TCC_TARGET_X86_64
+#    define CONFIG_TCC_ELFINTERP "/lib/amd64/ld.so.1"
+#  else
+#    define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
+#  endif
 # elif defined __GNU__
 #  define CONFIG_TCC_ELFINTERP "/lib/ld.so"
 # elif defined(TCC_TARGET_PE)
diff --git a/tccpp.c b/tccpp.c
index b95c63d..dac14ac 100644
--- a/tccpp.c
+++ b/tccpp.c
@@ -3597,6 +3597,17 @@ static const char * const target_os_defs =
     "__NetBSD__\0"
 # elif TARGETOS_OpenBSD
     "__OpenBSD__\0"
+# elif TARGETOS_SunOS
+    "__sun\0"
+    "sun\0"
+    "__sun__\0"
+#  ifdef TARGETOS_Illumos
+    "__illumos__\0"
+#  endif
+#  ifdef TCC_TARGET_X86_64
+    "__amd64\0" /* Headers depend on __amd64 to find ISA, __amd64__ won't work 
*/
+    "__x86_64\0" /* This one too */
+#  endif
 # else
     "__linux__\0"
     "__linux\0"
diff --git a/tccrun.c b/tccrun.c
index d373abc..95d9403 100644
--- a/tccrun.c
+++ b/tccrun.c
@@ -222,7 +222,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
 
 #if defined(__APPLE__) || defined(__FreeBSD__)
     char **envp = NULL;
-#elif defined(__OpenBSD__) || defined(__NetBSD__)
+#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__sun__)
     extern char **environ;
     char **envp = environ;
 #else
@@ -1115,6 +1115,9 @@ static void rt_exit(int code)
 # ifndef __OpenBSD__
 #  include <sys/ucontext.h>
 # endif
+# ifdef __sun__
+#  include <sys/regset.h>
+# endif
 #else
 # define ucontext_t CONTEXT
 #endif
diff --git a/tests/Makefile b/tests/Makefile
index a46c852..4aa2ed7 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -284,6 +284,7 @@ CROSS-TGTS = \
  i386 \
  i386-win32 \
  i386-OpenBSD \
+ i386-Illumos \
  x86_64 \
  x86_64-win32 \
  x86_64-osx \
_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to