From b42cb16b65ed237e6ec30f65d4f9647561515cb8 Mon Sep 17 00:00:00 2001
From: grischka <grischka>
Date: Wed, 5 Oct 2016 18:34:17 +0200
Subject: [PATCH] Misc. fixes

Makefile :
- do not 'uninstall' peoples /usr/local/doc entirely
libtcc.c :
- MEM_DEBUG : IDE-friendly output "file:line: ..."
- always ELF for objects
tccgen.c :
- fix memory leak in new switch code
- move static 'in_sizeof' out of function
profiling :
- define 'static' to empty
resolve_sym() :
- replace by dlsym()

win32/64: fix R_XXX_RELATIVE fixme
- was fixed for i386 already in
  8e4d64be2fc1d707c7800c5096f6e0ea22cbd
- do not -Lsystemdir if compiling to .o
---
 Makefile | 15 +++++++--------
 libtcc.c | 22 +++++++++++++---------
 tcc.h    |  9 ++++++---
 tccelf.c | 18 +++++++++++++++---
 tccgen.c |  3 ++-
 tccrun.c |  9 +--------
 6 files changed, 44 insertions(+), 32 deletions(-)

diff --git a/Makefile b/Makefile
index 5fa9659c..59df0051 100644
--- a/Makefile
+++ b/Makefile
@@ -18,10 +18,6 @@ else
  endif
 endif
 
-CFLAGS_P = $(CFLAGS) -pg -static -DCONFIG_TCC_STATIC
-LIBS_P=
-LDFLAGS_P = $(LDFLAGS)
-
 LIBTCC = libtcc.a
 LIBTCC1 = libtcc1.a
 LINK_LIBTCC =
@@ -50,6 +46,10 @@ ifeq ($(TARGETOS),Darwin)
  export MACOSX_DEPLOYMENT_TARGET:=10.2
 endif
 
+CFLAGS_P = $(CFLAGS) -pg -static -DCONFIG_TCC_STATIC -DTCC_PROFILE
+LIBS_P= $(LIBS)
+LDFLAGS_P = $(LDFLAGS)
+
 CONFIG_$(ARCH) = yes
 NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386
 NATIVE_DEFINES_$(CONFIG_x86-64) += -DTCC_TARGET_X86_64
@@ -136,8 +136,8 @@ endif
 all: $(PROGS) $(TCCLIBS) $(TCCDOCS)
 
 # Host Tiny C Compiler
-tcc$(EXESUF): tcc.c $(LIBTCC)
-	$(CC) -o $@ $^ $(LIBS) $(NATIVE_DEFINES) $(CFLAGS) $(LDFLAGS) $(LINK_LIBTCC)
+tcc$(EXESUF): tcc.o $(LIBTCC)
+	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LIBS) $(LINK_LIBTCC)
 
 # Cross Tiny C Compilers
 %-tcc$(EXESUF): tcc.c
@@ -182,7 +182,6 @@ else
 LIBTCC_OBJ = libtcc.o
 LIBTCC_INC = $(NATIVE_FILES)
 libtcc.o : NATIVE_DEFINES += -DONE_SOURCE
-tcc.o : NATIVE_DEFINES += -DONE_SOURCE
 endif
 
 $(LIBTCC_OBJ) tcc.o : %.o : %.c $(LIBTCC_INC)
@@ -269,7 +268,7 @@ uninstall:
 	rm -fv "$(libdir)/$(LIBTCC)" "$(includedir)/libtcc.h"
 	rm -fv $(libdir)/libtcc.so*
 	rm -rv "$(tccdir)"
-	rm -rv "$(docdir)"
+	rm -fv "$(docdir)/tcc-doc.html"
 else
 # on windows
 install: $(PROGS) $(TCCLIBS) $(TCCDOCS)
diff --git a/libtcc.c b/libtcc.c
index 068a0fcc..4e69b4b2 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -116,7 +116,7 @@ static void tcc_add_systemdir(TCCState *s)
 }
 #endif
 
-#if defined TCC_IS_NATIVE && !defined CONFIG_TCC_STATIC
+#if defined TCC_IS_NATIVE
 static void dlclose(void *p)
 {
     FreeLibrary((HMODULE)p);
@@ -412,7 +412,7 @@ PUB_FUNC void tcc_memstats(int bench)
             mem_cur_size, mem_max_size);
 
         while (header) {
-            fprintf(stderr, "  file %s, line %u: %u bytes\n",
+            fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
                 header->file_name, header->line_num, header->size);
             header = header->next;
         }
@@ -1504,14 +1504,21 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
 {
     s->output_type = output_type;
 
+    /* always elf for objects */
+    if (output_type == TCC_OUTPUT_OBJ)
+        s->output_format = TCC_OUTPUT_FORMAT_ELF;
+
+    if (s->char_is_unsigned)
+        tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
+
     if (!s->nostdinc) {
         /* default include paths */
         /* -isystem paths have already been handled */
         tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
     }
 
-    /* if bound checking, then add corresponding sections */
 #ifdef CONFIG_TCC_BCHECK
+    /* if bound checking, then add corresponding sections */
     if (s->do_bounds_check) {
         /* define symbol */
         tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
@@ -1522,11 +1529,6 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
                                       SHT_PROGBITS, SHF_ALLOC);
     }
 #endif
-
-    if (s->char_is_unsigned) {
-        tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
-    }
-
     /* add debug sections */
     if (s->do_debug) {
         /* stab symbols */
@@ -1540,9 +1542,11 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
     }
 
     tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
+
 #ifdef TCC_TARGET_PE
 # ifdef _WIN32
-    tcc_add_systemdir(s);
+    if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
+        tcc_add_systemdir(s);
 # endif
 #else
     /* add libc crt1/crti objects */
diff --git a/tcc.h b/tcc.h
index 49fde3bd..aeb0204e 100644
--- a/tcc.h
+++ b/tcc.h
@@ -76,6 +76,7 @@
 #  pragma warning (disable : 4018)  // signed/unsigned mismatch
 #  pragma warning (disable : 4146)  // unary minus operator applied to unsigned type, result still unsigned
 # endif
+# undef CONFIG_TCC_STATIC
 #endif
 
 #ifndef O_BINARY
@@ -1052,6 +1053,10 @@ static inline int toup(int c)
 # define PUB_FUNC
 #endif
 
+#ifdef TCC_PROFILE /* profile all functions */
+# define static
+#endif
+
 #ifdef ONE_SOURCE
 #define ST_INLN static inline
 #define ST_FUNC static
@@ -1559,9 +1564,7 @@ ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
 ST_FUNC void *dlopen(const char *filename, int flag);
 ST_FUNC void dlclose(void *p);
 ST_FUNC const char *dlerror(void);
-ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
-#elif !defined _WIN32
-ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
+ST_FUNC void *dlsym(int flag, const char *symbol);
 #endif
 
 #ifdef CONFIG_TCC_BACKTRACE
diff --git a/tccelf.c b/tccelf.c
index 23c7e9b7..e6468b2a 100644
--- a/tccelf.c
+++ b/tccelf.c
@@ -450,7 +450,7 @@ ST_FUNC void relocate_syms(TCCState *s1, int do_resolve)
 #if defined TCC_IS_NATIVE && !defined _WIN32
                 void *addr;
                 name = (char *) symtab_section->link->data + sym->st_name;
-                addr = resolve_sym(s1, name);
+                addr = dlsym(RTLD_DEFAULT, name);
                 if (addr) {
                     sym->st_value = (addr_t)addr;
 #ifdef DEBUG_RELOC
@@ -576,15 +576,18 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
                 goto output_file;
             write16le(ptr, read16le(ptr) + val - addr);
             break;
-        case R_386_RELATIVE:
-            /* do nothing */
+
+#ifdef TCC_TARGET_PE
+        case R_386_RELATIVE: /* handled in pe_relocate_rva() */
             break;
+#endif
         case R_386_COPY:
             /* This reloction must copy initialized data from the library
             to the program .bss segment. Currently made like for ARM
             (to remove noise of defaukt case). Is this true? 
             */
             break;
+
         default:
             fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
                 type, (unsigned)addr, ptr, (unsigned)val);
@@ -773,6 +776,10 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
             /* Nothing to do.  Normally used to indicate a dependency
                on a certain symbol (like for exception handling under EABI).  */
             break;
+#ifdef TCC_TARGET_PE
+	case R_ARM_RELATIVE: /* handled in pe_relocate_rva() */
+	    break;
+#endif
         default:
             fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
                 type, (unsigned)addr, ptr, (unsigned)val);
@@ -973,6 +980,11 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s)
             /* we load the got offset */
             write32le(ptr, read32le(ptr) + s1->sym_attrs[sym_index].got_offset);
             break;
+#ifdef TCC_TARGET_PE
+	case R_X86_64_RELATIVE: /* handled in pe_relocate_rva() */
+	    break;
+#endif
+
 #else
 #error unsupported processor
 #endif
diff --git a/tccgen.c b/tccgen.c
index 8144dc04..c33cc811 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -54,6 +54,7 @@ ST_DATA Sym *define_stack;
 ST_DATA Sym *global_label_stack;
 ST_DATA Sym *local_label_stack;
 static int local_scope;
+static int in_sizeof;
 
 ST_DATA int vlas_in_scope; /* number of VLAs that are currently in scope */
 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
@@ -3762,7 +3763,6 @@ ST_FUNC void unary(void)
     CType type;
     Sym *s;
     AttributeDef ad;
-    static int in_sizeof = 0;
 
     sizeof_caller = in_sizeof;
     in_sizeof = 0;
@@ -5235,6 +5235,7 @@ static void block(int *bsym, int *csym, int is_expr)
         gcase(sw.p, sw.n, c, &a);
         if (sw.def_sym)
             gjmp_addr(sw.def_sym);
+        dynarray_reset(&sw.p, &sw.n);
         cur_switch = saved;
         /* break label */
         gsym(a);
diff --git a/tccrun.c b/tccrun.c
index 3a6a82db..4903f903 100644
--- a/tccrun.c
+++ b/tccrun.c
@@ -748,7 +748,7 @@ static TCCSyms tcc_syms[] = {
     { NULL, NULL },
 };
 
-ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol)
+ST_FUNC void *dlsym(int flag, const char *symbol)
 {
     TCCSyms *p;
     p = tcc_syms;
@@ -760,13 +760,6 @@ ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol)
     return NULL;
 }
 
-#elif !defined(_WIN32)
-
-ST_FUNC void *resolve_sym(TCCState *s1, const char *sym)
-{
-    return dlsym(RTLD_DEFAULT, sym);
-}
-
 #endif /* CONFIG_TCC_STATIC */
 #endif /* TCC_IS_NATIVE */
 /* ------------------------------------------------------------- */