From a728c4e0a15d3822cfb283a4764c2d01018a788a Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Sat, 9 Aug 2014 18:44:13 +0200
Subject: Add Haiku support.


diff --git a/Makefile b/Makefile
index 705b585..bcf562e 100644
--- a/Makefile
+++ b/Makefile
@@ -36,12 +36,12 @@ ifdef CONFIG_WIN64
 CONFIG_WIN32=yes
 endif
 
-ifndef CONFIG_WIN32
-LIBS=-lm
-ifndef CONFIG_NOLDL
-LIBS+=-ldl
-endif
-endif
+#ifndef CONFIG_WIN32
+#LIBS=-lm
+#ifndef CONFIG_NOLDL
+#LIBS+=-ldl
+#endif
+#endif
 
 # make libtcc as static or dynamic library?
 ifdef DISABLE_STATIC
diff --git a/lib/bcheck.c b/lib/bcheck.c
index 54124b9..7af69ff 100644
--- a/lib/bcheck.c
+++ b/lib/bcheck.c
@@ -42,7 +42,7 @@
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
     || defined(__DragonFly__) || defined(__dietlibc__) \
     || defined(__UCLIBC__) || defined(__OpenBSD__) \
-    || defined(_WIN32) || defined(TCC_UCLIBC)
+    || defined(_WIN32) || defined(TCC_UCLIBC) || defined(__HAIKU__)
 #warning Bound checking does not support malloc (etc.) in this environment.
 #undef CONFIG_TCC_MALLOC_HOOKS
 #undef HAVE_MEMALIGN
diff --git a/tccrun.c b/tccrun.c
index 50178a8..c67bebb 100644
--- a/tccrun.c
+++ b/tccrun.c
@@ -481,7 +481,7 @@ static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
         *paddr = uc->uc_mcontext->__ss.__eip;
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
         *paddr = uc->uc_mcontext.mc_eip;
-#elif defined(__dietlibc__)
+#elif defined(__dietlibc__) || defined(__HAIKU__)
         *paddr = uc->uc_mcontext.eip;
 #else
         *paddr = uc->uc_mcontext.gregs[REG_EIP];
@@ -492,7 +492,7 @@ static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
         fp = uc->uc_mcontext->__ss.__ebp;
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
         fp = uc->uc_mcontext.mc_ebp;
-#elif defined(__dietlibc__)
+#elif defined(__dietlibc__) || defined(__HAIKU__)
         fp = uc->uc_mcontext.ebp;
 #else
         fp = uc->uc_mcontext.gregs[REG_EBP];
-- 
2.7.0


From 47578a5c04b702ee055ced9dcd29f5eb7f1b2165 Mon Sep 17 00:00:00 2001
From: Leorize <alaviss@users.noreply.github.com>
Date: Sun, 3 Dec 2017 11:51:08 +0700
Subject: tcc.h: ucontext_t is in <signal.h>


diff --git a/tcc.h b/tcc.h
index 2c2936c..ec75502 100644
--- a/tcc.h
+++ b/tcc.h
@@ -43,7 +43,11 @@
 #ifndef _WIN32
 # include <unistd.h>
 # include <sys/time.h>
-# include <sys/ucontext.h>
+# ifndef __HAIKU__
+#  include <sys/ucontext.h>
+# else
+#  include <signal.h>
+# endif
 # include <sys/mman.h>
 # ifndef CONFIG_TCC_STATIC
 #  include <dlfcn.h>
-- 
2.7.0


From 99dfbd1a1ea61dd7a2fa6b2a64b4f3cef082d3c2 Mon Sep 17 00:00:00 2001
From: Leorize <alaviss@users.noreply.github.com>
Date: Sun, 3 Dec 2017 11:52:06 +0700
Subject: libtcc: add support for haiku start files


diff --git a/libtcc.c b/libtcc.c
index af0fb7f..e5d12bb 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -1346,8 +1346,15 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
     if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
         !s->nostdlib) {
         if (output_type != TCC_OUTPUT_DLL)
+# ifdef __HAIKU__
+            tcc_add_crt(s, "start_dyn.o");
+# else
             tcc_add_crt(s, "crt1.o");
+# endif
         tcc_add_crt(s, "crti.o");
+# ifdef __HAIKU__
+        tcc_add_crt(s, "init_term_dyn.o");
+# endif
     }
 #endif
     return 0;
-- 
2.7.0


From ddb0f5247fd5d09647154fe15000c2b74112f77e Mon Sep 17 00:00:00 2001
From: Leorize <alaviss@users.noreply.github.com>
Date: Sun, 3 Dec 2017 11:52:50 +0700
Subject: tccelf: haiku use libroot instead of libc


diff --git a/tccelf.c b/tccelf.c
index 500ea18..bdc4c9c 100644
--- a/tccelf.c
+++ b/tccelf.c
@@ -1360,7 +1360,11 @@ ST_FUNC void tcc_add_runtime(TCCState *s1)
 
     /* add libc */
     if (!s1->nostdlib) {
+#ifdef __HAIKU__
+        tcc_add_library(s1, "root");
+#else
         tcc_add_library(s1, "c");
+#endif
 #ifdef CONFIG_USE_LIBGCC
         tcc_add_file(s1, TCC_LIBGCC);
 #elif !defined WITHOUT_LIBTCC
-- 
2.7.0


From a7a81c2967a49730c1d55c54e2d80022aad959ac Mon Sep 17 00:00:00 2001
From: Leorize <alaviss@users.noreply.github.com>
Date: Sun, 3 Dec 2017 17:31:00 +0700
Subject: libtcc: define Haiku specific macros


diff --git a/libtcc.c b/libtcc.c
index e5d12bb..0660c51 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -930,6 +930,10 @@ LIBTCCAPI TCCState *tcc_new(void)
     tcc_define_symbol(s, "__i386__", NULL);
     tcc_define_symbol(s, "__i386", NULL);
     tcc_define_symbol(s, "i386", NULL);
+# if defined(__HAIKU__)
+    tcc_define_symbol(s, "__INTEL__", NULL);
+    tcc_define_symbol(s, "__X86__", NULL);
+# endif
 #elif defined(TCC_TARGET_X86_64)
     tcc_define_symbol(s, "__x86_64__", NULL);
 #elif defined(TCC_TARGET_ARM)
@@ -964,6 +968,9 @@ LIBTCCAPI TCCState *tcc_new(void)
 # if defined(__FreeBSD_kernel__)
     tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
 # endif
+# if defined(__HAIKU__)
+    tcc_define_symbol(s, "__HAIKU__", NULL);
+# endif
 #endif
 
     /* TinyCC & gcc defines */
-- 
2.7.0

