From 7e7e6148fdb4adbda936f80b5d4ac3d738908d95 Mon Sep 17 00:00:00 2001 From: seyko Date: Fri, 10 Apr 2015 07:37:31 +0300 Subject: [PATCH] fix installation amd bcheck for Windows * define targetos=Windows when --enable-tcc32-mingw, --enable-cygwin, ... * use TARGETOS insteed HOST_OS when selecting PROGS * use "$(tccdir)" insteed $(tccdir) on install (spaces in path) * install tcc.exe too * produce bcheck.o when cross-compiling too (lib/Makefile) * force bcheck.o linking by compiling inside tcc_set_output_type() a dummy program with local array. Otherwise bcheck.o may be not linked. * replace %xz format specifier with %p in bcheck (don't supported on Windows) * call a __bound_init when __bound_ptr_add, __bound_ptr_indir, __bound_new_region, __bound_delete_region called. This is because a __bound_init inside ".init" section is not called on Windows for unknown reason. * print on stderr a message when an illegal pointer is returned: there is no segmentation violation on Windows for a program compiled with "tcc -b" * remove "C:" subdir on clean if $HOST_OS = "Linux" * default CFLAGS="-Wall -g -O0" insteed CFLAGS="-Wall -g -O2" to speed up compilation and more precise debugging. --- Makefile | 16 +++++++--------- configure | 15 ++++++++++----- lib/Makefile | 2 +- lib/bcheck.c | 33 ++++++++++++++++++++++++--------- libtcc.c | 11 +++++++++++ tccelf.c | 31 ++++++++++++++++--------------- tests/boundtest.c | 1 + 7 files changed, 70 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index d70af996..20459789 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,7 @@ $(ARM_FPA_LD_CROSS)_LINK = arm-fpa-ld-tcc$(EXESUF) $(ARM_VFP_CROSS)_LINK = arm-vfp-tcc$(EXESUF) $(ARM_EABI_CROSS)_LINK = arm-eabi-tcc$(EXESUF) -ifeq ($(HOST_OS),Windows) +ifeq ($(TARGETOS),Windows) ifeq ($(ARCH),i386) PROGS:=$($(WIN32_CROSS)_LINK) $($(WIN32_CROSS)_LINK)_TCC = yes @@ -100,7 +100,7 @@ $($(WIN64_CROSS)_LINK)_TCC = yes endif endif -ifeq ($(HOST_OS),Linux) +ifeq ($(TARGETOS),Linux) ifeq ($(ARCH),i386) PROGS:=$($(I386_CROSS)_LINK) $($(I386_CROSS)_LINK)_TCC = yes @@ -140,7 +140,6 @@ PROGS_CROSS=$($(X64_CROSS)_LINK) $($(WIN32_CROSS)_LINK) $($(WIN64_CROSS)_LINK) $ LIBTCC1_CROSS=lib/i386-win/libtcc1.a lib/x86_64-win/libtcc1.a lib/i386/libtcc1.a lib/x86_64/libtcc1.a \ lib/arm64/libtcc1.a LIBTCC1=libtcc1.a -BCHECK=yes else ifeq ($(ARCH),x86-64) ifeq ($(TARGETOS),Darwin) NATIVE_FILES=$(X86_64_FILES) @@ -292,10 +291,6 @@ ifneq ($(LIBTCC1),) mkdir -p "$(tccdir)/$(ARCH)" $(INSTALL) -m644 $(LIBTCC1) "$(tccdir)/$(ARCH)" endif -ifneq ($(BCHECK),) - -mkdir -p "$(tccdir)/$(ARCH)" - $(INSTALL) -m644 lib/$(ARCH)/bcheck.o "$(tccdir)/$(ARCH)" -endif $(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include" mkdir -p "$(libdir)" $(INSTALL) -m644 $(LIBTCC) "$(libdir)" @@ -315,7 +310,6 @@ ifdef CONFIG_CROSS ifneq ($(HOST_OS),Darwin) mkdir -p "$(tccdir)/arm64" $(INSTALL) -m644 lib/arm64/libtcc1.a "$(tccdir)/arm64" - $(INSTALL) -m644 lib/i386/bcheck.o "$(tccdir)/i386" endif $(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386" $(INSTALL) -m644 lib/x86_64/libtcc1.a "$(tccdir)/x86-64" @@ -346,13 +340,14 @@ install: $(PROGS) $(TCCLIBS) $(TCCDOCS) mkdir -p "$(tccdir)/doc" mkdir -p "$(tccdir)/libtcc" $(INSTALLBIN) -m755 $(PROGS) "$(tccdir)" + $(INSTALLBIN) -m755 tcc.exe "$(tccdir)" $(INSTALL) -m644 $(LIBTCC1) $(top_srcdir)/win32/lib/*.def "$(tccdir)/lib" cp -r $(top_srcdir)/win32/include/. "$(tccdir)/include" cp -r $(top_srcdir)/win32/examples/. "$(tccdir)/examples" $(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include" $(INSTALL) -m644 tcc-doc.html $(top_srcdir)/win32/tcc-win32.txt "$(tccdir)/doc" $(INSTALL) -m644 $(top_srcdir)/libtcc.h $(LIBTCC_EXTRA) "$(tccdir)/libtcc" - $(INSTALL) -m644 $(LIBTCC) $(tccdir) + $(INSTALL) -m644 $(LIBTCC) "$(tccdir)" ifdef CONFIG_CROSS mkdir -p "$(tccdir)/lib/32" mkdir -p "$(tccdir)/lib/64" @@ -385,6 +380,9 @@ clean: rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.log \ *.exe a.out tags TAGS libtcc_test$(EXESUF) tcc$(EXESUF) -rm -r $(ARCH) arm64 +ifeq ($(HOST_OS),Linux) + -rm -r ./C: +endif -rm *-tcc$(EXESUF) $(MAKE) -C tests $@ ifneq ($(LIBTCC1),) diff --git a/configure b/configure index 69089fe5..5b9b9c17 100755 --- a/configure +++ b/configure @@ -47,7 +47,12 @@ tcc_elfinterp="" tcc_lddir= confvars= cpu= + host_os=`uname` +case $host_os in + MINGW32*) host_os=Windows; ;; + *) ;; +esac # OS specific targetos=`uname` @@ -170,13 +175,13 @@ for opt do ;; --enable-gprof) gprof="yes" ;; - --enable-mingw32) mingw32="yes" ; cross_prefix="i686-pc-mingw32-" ; cpu=x86 + --enable-mingw32) mingw32="yes" ; cross_prefix="i686-pc-mingw32-" ; cpu=x86; targetos=Windows; ;; - --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; cpu=x86 + --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; cpu=x86; targetos=Windows; ;; - --enable-tcc32-mingw) mingw32="yes" ; cross_prefix="i386-win-" ; cpu=x86; cc=tcc + --enable-tcc32-mingw) mingw32="yes" ; cross_prefix="i386-win-" ; cpu=x86; cc=tcc; targetos=Windows; ;; - --enable-tcc64-mingw) mingw32="yes" ; cross_prefix="x86_64-win-" ; cpu=x86_64; cc=tcc + --enable-tcc64-mingw) mingw32="yes" ; cross_prefix="x86_64-win-" ; cpu=x86_64; cc=tcc; targetos=Windows; ;; --enable-cross) build_cross="yes" ;; @@ -210,7 +215,7 @@ classify_cpu "$cpu" # Checking for CFLAGS if test -z "$CFLAGS"; then - CFLAGS="-Wall -g -O2" + CFLAGS="-Wall -g -O0" fi if test "$mingw32" = "yes" ; then diff --git a/lib/Makefile b/lib/Makefile index 90b32865..aa3984b2 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -32,9 +32,9 @@ ifndef TARGET # native library endif endif endif - BCHECK_O = bcheck.o endif +BCHECK_O = bcheck.o DIR = $(TARGET) native : ../libtcc1.a diff --git a/lib/bcheck.c b/lib/bcheck.c index 6f47f808..7dcb36e1 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -150,6 +150,7 @@ static BoundEntry *__bound_find_region(BoundEntry *e1, void *p) static void bound_error(const char *fmt, ...) { __bound_error_msg = fmt; + fprintf(stderr,"%s %s: %s\n", __FILE__, __FUNCTION__, fmt); *(int *)0 = 0; /* force a runtime error */ } @@ -164,6 +165,9 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) { size_t addr = (size_t)p; BoundEntry *e; + + __bound_init(); + #if defined(BOUND_DEBUG) printf("%s %s: 0x%x %d\n", __FILE__, __FUNCTION__, (int)p, offset); #endif @@ -179,9 +183,7 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) } addr += offset; if (addr >= e->size) { - #if defined(BOUND_DEBUG) - printf("%s %s: 0x%zx is outside of the region\n", __FILE__, __FUNCTION__, p + offset); - #endif + fprintf(stderr,"%s %s: %p is outside of the region\n", __FILE__, __FUNCTION__, p + offset); return INVALID_POINTER; /* return an invalid pointer */ } return p + offset; @@ -195,6 +197,7 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ size_t addr = (size_t)p; \ BoundEntry *e; \ \ + __bound_init(); \ e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \ e = (BoundEntry *)((char *)e + \ ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \ @@ -205,8 +208,10 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ addr = (size_t)p - e->start; \ } \ addr += offset + dsize; \ - if (addr > e->size) \ + if (addr > e->size) { \ + fprintf(stderr,"%s %s: %p is outside of the region\n", __FILE__, __FUNCTION__, p + offset); \ return INVALID_POINTER; /* return an invalid pointer */ \ + } \ return p + offset; \ } @@ -228,7 +233,7 @@ void FASTCALL __bound_local_new(void *p1) { size_t addr, size, fp, *p = p1; #ifdef BOUND_DEBUG - fprintf(stderr, "%s, %s start p1=%zx *p1=%zx\n", __FILE__, __FUNCTION__, p, *p); + fprintf(stderr, "%s, %s start p1=%p *p1=%p\n", __FILE__, __FUNCTION__, p, *p); #endif GET_CALLER_FP(fp); for(;;) { @@ -364,6 +369,12 @@ void __bound_init(void) size_t start, size; size_t *p; + static int inited; + if (inited) + return; + + inited = 1; + #ifdef BOUND_DEBUG fprintf(stderr, "%s, %s() start\n", __FILE__, __FUNCTION__); #endif @@ -444,7 +455,7 @@ void __bound_main_arg(void **p) void *start = p; while (*p++); #ifdef BOUND_DEBUG - fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n", + fprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n", __FILE__, __FUNCTION__, (void *) p - start); #endif __bound_new_region(start, (void *) p - start); @@ -482,8 +493,10 @@ void __bound_new_region(void *p, size_t size) BoundEntry *page, *e, *e2; size_t t1_start, t1_end, i, t2_start, t2_end; + __bound_init(); + #ifdef BOUND_DEBUG - fprintf(stderr, "%s, %s(%p, %zx) start\n", + fprintf(stderr, "%s, %s(%p, %p) start\n", __FILE__, __FUNCTION__, p, size); #endif @@ -595,6 +608,8 @@ int __bound_delete_region(void *p) BoundEntry *page, *e, *e2; size_t t1_start, t1_end, t2_start, t2_end, i; + __bound_init(); + #ifdef BOUND_DEBUG fprintf(stderr, "%s %s() start\n", __FILE__, __FUNCTION__); #endif @@ -757,7 +772,7 @@ void *__bound_malloc(size_t size, const void *caller) return NULL; #ifdef BOUND_DEBUG - fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n", + fprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n", __FILE__, __FUNCTION__, ptr, size); #endif __bound_new_region(ptr, size); @@ -791,7 +806,7 @@ void *__bound_memalign(size_t size, size_t align, const void *caller) return NULL; #ifdef BOUND_DEBUG - fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n", + fprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n", __FILE__, __FUNCTION__, ptr, size); #endif __bound_new_region(ptr, size); diff --git a/libtcc.c b/libtcc.c index d5bf555a..4cea4a80 100644 --- a/libtcc.c +++ b/libtcc.c @@ -1390,6 +1390,17 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) tcc_add_crt(s, "crti.o"); } #endif + +#ifdef CONFIG_TCC_BCHECK + if (s->do_bounds_check && (output_type == TCC_OUTPUT_EXE)) + { + /* XXX force a bcheck.o linking by compiling a function with a local array. + Otherwise bcheck.o may be not linked. */ + + if (tcc_compile_string(s, "static void __bound_check_dummy_func(){int v[1];}") == -1) + tcc_warning("compiling __bound_check_dummy_func failed"); + } +#endif return 0; } diff --git a/tccelf.c b/tccelf.c index 5ca5089f..ac914562 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1569,9 +1569,6 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) { #ifdef CONFIG_TCC_BCHECK addr_t *ptr; - Section *init_section; - unsigned char *pinit; - int sym_index; if (0 == s1->do_bounds_check) return; @@ -1585,19 +1582,23 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) #ifdef TCC_TARGET_I386 if (s1->output_type != TCC_OUTPUT_MEMORY) { /* add 'call __bound_init()' in .init section */ - init_section = find_section(s1, ".init"); - pinit = section_ptr_add(init_section, 5); - pinit[0] = 0xe8; - put32(pinit + 1, -4); - sym_index = find_elf_sym(symtab_section, "__bound_init"); - if (!sym_index) { - tcc_add_support(s1, "bcheck.o"); - sym_index = find_elf_sym(symtab_section, "__bound_init"); - if (!sym_index) - tcc_error("__bound_init not defined"); - } - put_elf_reloc(symtab_section, init_section, + + /* XXX not called on MSYS, reason is unknown. For this + case a call to __bound_init is performed in bcheck.c + when __bound_ptr_add, __bound_new_region, + __bound_delete_region called */ + + int sym_index = find_elf_sym(symtab_section, "__bound_init"); + if (sym_index) { + Section *init_section = find_section(s1, ".init"); + unsigned char *pinit = section_ptr_add(init_section, 5); + pinit[0] = 0xe8; + put32(pinit + 1, -4); + put_elf_reloc(symtab_section, init_section, init_section->data_offset - 4, R_386_PC32, sym_index); + } + else + tcc_warning("__bound_init not defined"); } #endif #endif diff --git a/tests/boundtest.c b/tests/boundtest.c index c2b29532..eb95a963 100644 --- a/tests/boundtest.c +++ b/tests/boundtest.c @@ -1,5 +1,6 @@ #include #include +#include #define NB_ITS 1000000 //#define NB_ITS 1 -- 2.11.4.GIT