From 02642bc94cb65a59a9eb95b8214bf66960ecde6e Mon Sep 17 00:00:00 2001
From: grischka <grischka>
Date: Wed, 19 Oct 2016 19:15:04 +0200
Subject: [PATCH] lib/libtcc1.c: cleanup

- remove #include dependencies from libtcc1.c
  for easier cross compilation
- clear_cache only on ARM
- error-message for mprotect failure
---
 i386-gen.c    | 19 +++----------------
 lib/libtcc1.c | 50 +++++++++++++++-----------------------------------
 tcc.h         |  2 +-
 tccrun.c      | 13 ++++++-------
 tcctok.h      |  3 ---
 5 files changed, 25 insertions(+), 62 deletions(-)

diff --git a/i386-gen.c b/i386-gen.c
index 9cc2f8e1..eaea9a3f 100644
--- a/i386-gen.c
+++ b/i386-gen.c
@@ -1025,8 +1025,7 @@ ST_FUNC void gen_cvt_itof(int t)
 /* convert fp to int 't' type */
 ST_FUNC void gen_cvt_ftoi(int t)
 {
- #ifndef COMMIT_4ad186c5ef61_IS_FIXED
-    /* a good version but it takes a more time to execute */
+#if 1
     gv(RC_FLOAT);
     save_reg(TREG_EAX);
     save_reg(TREG_EDX);
@@ -1034,19 +1033,7 @@ ST_FUNC void gen_cvt_ftoi(int t)
     vtop->r = TREG_EAX; /* mark reg as used */
     if (t == VT_LLONG)
         vtop->r2 = TREG_EDX;
- #else
-    /* a new version with a bug: t2a = 44100312 */
-    /*
-    #include<stdio.h>
-    int main() {
-	int t1 = 176401255;
-	float f = 0.25f;
-	int t2a = (int)(t1 * f); // must be 44100313
-	int t2b = (int)(t1 * (float)0.25f);
-	printf("t2a=%d t2b=%d \n",t2a,t2b);
-	return 0;
-    }
-    */
+#else
     int bt = vtop->type.t & VT_BTYPE;
     if (bt == VT_FLOAT)
         vpush_global_sym(&func_old_type, TOK___fixsfdi);
@@ -1059,7 +1046,7 @@ ST_FUNC void gen_cvt_ftoi(int t)
     vpushi(0);
     vtop->r = REG_IRET;
     vtop->r2 = REG_LRET;
- #endif
+#endif
 }
 
 /* convert from one floating point type to another */
diff --git a/lib/libtcc1.c b/lib/libtcc1.c
index a5fee7b9..3b6d6180 100644
--- a/lib/libtcc1.c
+++ b/lib/libtcc1.c
@@ -28,8 +28,6 @@ the Free Software Foundation, 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  
 */
 
-#include <stdint.h>
-
 #define W_TYPE_SIZE   32
 #define BITS_PER_UNIT 8
 
@@ -480,7 +478,6 @@ long long __ashldi3(long long a, int b)
 #endif
 }
 
-#ifndef COMMIT_4ad186c5ef61_IS_FIXED
 long long __tcc_cvt_ftol(long double x)
 {
     unsigned c0, c1;
@@ -492,7 +489,6 @@ long long __tcc_cvt_ftol(long double x)
     __asm__ __volatile__ ("fldcw %0" : : "m" (c0));
     return ret;
 }
-#endif
 
 #endif /* !__x86_64__ */
 
@@ -638,24 +634,24 @@ long long __fixxfdi (long double a1)
 #if defined(TCC_TARGET_X86_64) && !defined(_WIN64)
 
 #ifndef __TINYC__
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# undef __va_start
+# undef __va_arg
+# undef __va_copy
+# undef __va_end
 #else
-/* Avoid including stdlib.h because it is not easily available when
-   cross compiling */
-#include <stddef.h> /* size_t definition is needed for a x86_64-tcc to parse memset() */
-extern void *malloc(unsigned long long);
-extern void *memset(void *s, int c, size_t n);
-extern void free(void*);
+/* Avoid include files, they may not be available when cross compiling */
+extern void *memset(void *s, int c, __SIZE_TYPE__ n);
 extern void abort(void);
 #endif
 
+/* This should be in sync with our include/stdarg.h */
 enum __va_arg_type {
     __va_gen_reg, __va_float_reg, __va_stack
 };
 
-//This should be in sync with the declaration on our include/stdarg.h
 /* GCC compatible definition of va_list. */
 typedef struct {
     unsigned int gp_offset;
@@ -667,11 +663,6 @@ typedef struct {
     char *reg_save_area;
 } __va_list_struct;
 
-#undef __va_start
-#undef __va_arg
-#undef __va_copy
-#undef __va_end
-
 void __va_start(__va_list_struct *ap, void *fp)
 {
     memset(ap, 0, sizeof(__va_list_struct));
@@ -705,33 +696,25 @@ void *__va_arg(__va_list_struct *ap,
     case __va_stack:
     use_overflow_area:
         ap->overflow_arg_area += size;
-        ap->overflow_arg_area = (char*)((intptr_t)(ap->overflow_arg_area + align - 1) & -(intptr_t)align);
+        ap->overflow_arg_area = (char*)((long long)(ap->overflow_arg_area + align - 1) & -align);
         return ap->overflow_arg_area - size;
 
-    default:
+    default: /* should never happen */
 #ifndef __TINYC__
         fprintf(stderr, "unknown ABI type for __va_arg\n");
 #endif
         abort();
     }
 }
-
 #endif /* __x86_64__ */
 
-/* Flushing for tccrun */
-#if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_I386)
-
-void __clear_cache(void *beginning, void *end)
-{
-}
-
-#elif defined(TCC_TARGET_ARM)
-
+#ifdef TCC_TARGET_ARM
 #define _GNU_SOURCE
 #include <unistd.h>
 #include <sys/syscall.h>
 #include <stdio.h>
 
+/* Flushing for tccrun */
 void __clear_cache(void *beginning, void *end)
 {
 /* __ARM_NR_cacheflush is kernel private and should not be used in user space.
@@ -747,7 +730,4 @@ void __clear_cache(void *beginning, void *end)
              "ret");
 #endif
 }
-
-#else
-#warning __clear_cache not defined for this architecture, avoid using tcc -run
-#endif
+#endif /* arm */
diff --git a/tcc.h b/tcc.h
index bd4fee41..e536f6c5 100644
--- a/tcc.h
+++ b/tcc.h
@@ -1312,6 +1312,7 @@ ST_DATA Section *last_text_section; /* to handle .previous asm directive */
 /* bound check related sections */
 ST_DATA Section *bounds_section; /* contains global data bound description */
 ST_DATA Section *lbounds_section; /* contains local data bound description */
+ST_FUNC void tccelf_bounds_new(TCCState *s);
 #endif
 /* symbol sections */
 ST_DATA Section *symtab_section, *strtab_section;
@@ -1320,7 +1321,6 @@ ST_DATA Section *stab_section, *stabstr_section;
 
 ST_FUNC void tccelf_new(TCCState *s);
 ST_FUNC void tccelf_delete(TCCState *s);
-ST_FUNC void tccelf_bounds_new(TCCState *s);
 ST_FUNC void tccelf_stab_new(TCCState *s);
 
 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
diff --git a/tccrun.c b/tccrun.c
index 15a2d76c..3e32de4c 100644
--- a/tccrun.c
+++ b/tccrun.c
@@ -255,7 +255,6 @@ static void set_pages_executable(void *ptr, unsigned long length)
     unsigned long old_protect;
     VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
 #else
-    extern void __clear_cache(void *beginning, void *end);
 #ifndef PAGESIZE
 # define PAGESIZE 4096
 #endif
@@ -263,12 +262,12 @@ static void set_pages_executable(void *ptr, unsigned long length)
     start = (addr_t)ptr & ~(PAGESIZE - 1);
     end = (addr_t)ptr + length;
     end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
-    mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
-  #ifndef __PCC__
-    __clear_cache(ptr, (char *)ptr + length);
-  #else
-    /* pcc 1.2.0.DEVEL 20141206 don't have such proc */
-  #endif
+    if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))
+        tcc_error("mprotect failed: did you mean to configure --with-selinux?");
+#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
+    { extern void __clear_cache(void *beginning, void *end);
+      __clear_cache(ptr, (char *)ptr + length); }
+#endif
 #endif
 }
 
diff --git a/tcctok.h b/tcctok.h
index 228b055f..7480ac77 100644
--- a/tcctok.h
+++ b/tcctok.h
@@ -242,10 +242,7 @@
      DEF(TOK___fixsfdi, "__fixsfdi")
      DEF(TOK___fixdfdi, "__fixdfdi")
      DEF(TOK___fixxfdi, "__fixxfdi")
-
- #ifndef COMMIT_4ad186c5ef61_IS_FIXED
      DEF(TOK___tcc_cvt_ftol, "__tcc_cvt_ftol")
- #endif
 #endif
 
 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64