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.
This commit is contained in:
seyko 2015-04-10 07:37:31 +03:00
parent 5cd4393a54
commit 7e7e6148fd
7 changed files with 70 additions and 39 deletions

View File

@ -89,7 +89,7 @@ $(ARM_FPA_LD_CROSS)_LINK = arm-fpa-ld-tcc$(EXESUF)
$(ARM_VFP_CROSS)_LINK = arm-vfp-tcc$(EXESUF) $(ARM_VFP_CROSS)_LINK = arm-vfp-tcc$(EXESUF)
$(ARM_EABI_CROSS)_LINK = arm-eabi-tcc$(EXESUF) $(ARM_EABI_CROSS)_LINK = arm-eabi-tcc$(EXESUF)
ifeq ($(HOST_OS),Windows) ifeq ($(TARGETOS),Windows)
ifeq ($(ARCH),i386) ifeq ($(ARCH),i386)
PROGS:=$($(WIN32_CROSS)_LINK) PROGS:=$($(WIN32_CROSS)_LINK)
$($(WIN32_CROSS)_LINK)_TCC = yes $($(WIN32_CROSS)_LINK)_TCC = yes
@ -100,7 +100,7 @@ $($(WIN64_CROSS)_LINK)_TCC = yes
endif endif
endif endif
ifeq ($(HOST_OS),Linux) ifeq ($(TARGETOS),Linux)
ifeq ($(ARCH),i386) ifeq ($(ARCH),i386)
PROGS:=$($(I386_CROSS)_LINK) PROGS:=$($(I386_CROSS)_LINK)
$($(I386_CROSS)_LINK)_TCC = yes $($(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 \ 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 lib/arm64/libtcc1.a
LIBTCC1=libtcc1.a LIBTCC1=libtcc1.a
BCHECK=yes
else ifeq ($(ARCH),x86-64) else ifeq ($(ARCH),x86-64)
ifeq ($(TARGETOS),Darwin) ifeq ($(TARGETOS),Darwin)
NATIVE_FILES=$(X86_64_FILES) NATIVE_FILES=$(X86_64_FILES)
@ -291,10 +290,6 @@ install: $(PROGS) $(TCCLIBS) $(TCCDOCS)
ifneq ($(LIBTCC1),) ifneq ($(LIBTCC1),)
mkdir -p "$(tccdir)/$(ARCH)" mkdir -p "$(tccdir)/$(ARCH)"
$(INSTALL) -m644 $(LIBTCC1) "$(tccdir)/$(ARCH)" $(INSTALL) -m644 $(LIBTCC1) "$(tccdir)/$(ARCH)"
endif
ifneq ($(BCHECK),)
-mkdir -p "$(tccdir)/$(ARCH)"
$(INSTALL) -m644 lib/$(ARCH)/bcheck.o "$(tccdir)/$(ARCH)"
endif endif
$(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include" $(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include"
mkdir -p "$(libdir)" mkdir -p "$(libdir)"
@ -315,7 +310,6 @@ ifdef CONFIG_CROSS
ifneq ($(HOST_OS),Darwin) ifneq ($(HOST_OS),Darwin)
mkdir -p "$(tccdir)/arm64" mkdir -p "$(tccdir)/arm64"
$(INSTALL) -m644 lib/arm64/libtcc1.a "$(tccdir)/arm64" $(INSTALL) -m644 lib/arm64/libtcc1.a "$(tccdir)/arm64"
$(INSTALL) -m644 lib/i386/bcheck.o "$(tccdir)/i386"
endif endif
$(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386" $(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386"
$(INSTALL) -m644 lib/x86_64/libtcc1.a "$(tccdir)/x86-64" $(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)/doc"
mkdir -p "$(tccdir)/libtcc" mkdir -p "$(tccdir)/libtcc"
$(INSTALLBIN) -m755 $(PROGS) "$(tccdir)" $(INSTALLBIN) -m755 $(PROGS) "$(tccdir)"
$(INSTALLBIN) -m755 tcc.exe "$(tccdir)"
$(INSTALL) -m644 $(LIBTCC1) $(top_srcdir)/win32/lib/*.def "$(tccdir)/lib" $(INSTALL) -m644 $(LIBTCC1) $(top_srcdir)/win32/lib/*.def "$(tccdir)/lib"
cp -r $(top_srcdir)/win32/include/. "$(tccdir)/include" cp -r $(top_srcdir)/win32/include/. "$(tccdir)/include"
cp -r $(top_srcdir)/win32/examples/. "$(tccdir)/examples" cp -r $(top_srcdir)/win32/examples/. "$(tccdir)/examples"
$(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include" $(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 tcc-doc.html $(top_srcdir)/win32/tcc-win32.txt "$(tccdir)/doc"
$(INSTALL) -m644 $(top_srcdir)/libtcc.h $(LIBTCC_EXTRA) "$(tccdir)/libtcc" $(INSTALL) -m644 $(top_srcdir)/libtcc.h $(LIBTCC_EXTRA) "$(tccdir)/libtcc"
$(INSTALL) -m644 $(LIBTCC) $(tccdir) $(INSTALL) -m644 $(LIBTCC) "$(tccdir)"
ifdef CONFIG_CROSS ifdef CONFIG_CROSS
mkdir -p "$(tccdir)/lib/32" mkdir -p "$(tccdir)/lib/32"
mkdir -p "$(tccdir)/lib/64" mkdir -p "$(tccdir)/lib/64"
@ -385,6 +380,9 @@ clean:
rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.log \ rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.log \
*.exe a.out tags TAGS libtcc_test$(EXESUF) tcc$(EXESUF) *.exe a.out tags TAGS libtcc_test$(EXESUF) tcc$(EXESUF)
-rm -r $(ARCH) arm64 -rm -r $(ARCH) arm64
ifeq ($(HOST_OS),Linux)
-rm -r ./C:
endif
-rm *-tcc$(EXESUF) -rm *-tcc$(EXESUF)
$(MAKE) -C tests $@ $(MAKE) -C tests $@
ifneq ($(LIBTCC1),) ifneq ($(LIBTCC1),)

15
configure vendored
View File

@ -47,7 +47,12 @@ tcc_elfinterp=""
tcc_lddir= tcc_lddir=
confvars= confvars=
cpu= cpu=
host_os=`uname` host_os=`uname`
case $host_os in
MINGW32*) host_os=Windows; ;;
*) ;;
esac
# OS specific # OS specific
targetos=`uname` targetos=`uname`
@ -170,13 +175,13 @@ for opt do
;; ;;
--enable-gprof) gprof="yes" --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" --enable-cross) build_cross="yes"
;; ;;
@ -210,7 +215,7 @@ classify_cpu "$cpu"
# Checking for CFLAGS # Checking for CFLAGS
if test -z "$CFLAGS"; then if test -z "$CFLAGS"; then
CFLAGS="-Wall -g -O2" CFLAGS="-Wall -g -O0"
fi fi
if test "$mingw32" = "yes" ; then if test "$mingw32" = "yes" ; then

View File

@ -32,9 +32,9 @@ ifndef TARGET # native library
endif endif
endif endif
endif endif
BCHECK_O = bcheck.o
endif endif
BCHECK_O = bcheck.o
DIR = $(TARGET) DIR = $(TARGET)
native : ../libtcc1.a native : ../libtcc1.a

View File

@ -150,6 +150,7 @@ static BoundEntry *__bound_find_region(BoundEntry *e1, void *p)
static void bound_error(const char *fmt, ...) static void bound_error(const char *fmt, ...)
{ {
__bound_error_msg = fmt; __bound_error_msg = fmt;
fprintf(stderr,"%s %s: %s\n", __FILE__, __FUNCTION__, fmt);
*(int *)0 = 0; /* force a runtime error */ *(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; size_t addr = (size_t)p;
BoundEntry *e; BoundEntry *e;
__bound_init();
#if defined(BOUND_DEBUG) #if defined(BOUND_DEBUG)
printf("%s %s: 0x%x %d\n", __FILE__, __FUNCTION__, (int)p, offset); printf("%s %s: 0x%x %d\n", __FILE__, __FUNCTION__, (int)p, offset);
#endif #endif
@ -179,9 +183,7 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset)
} }
addr += offset; addr += offset;
if (addr >= e->size) { if (addr >= e->size) {
#if defined(BOUND_DEBUG) fprintf(stderr,"%s %s: %p is outside of the region\n", __FILE__, __FUNCTION__, p + offset);
printf("%s %s: 0x%zx is outside of the region\n", __FILE__, __FUNCTION__, p + offset);
#endif
return INVALID_POINTER; /* return an invalid pointer */ return INVALID_POINTER; /* return an invalid pointer */
} }
return p + offset; return p + offset;
@ -195,6 +197,7 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \
size_t addr = (size_t)p; \ size_t addr = (size_t)p; \
BoundEntry *e; \ BoundEntry *e; \
\ \
__bound_init(); \
e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \ e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
e = (BoundEntry *)((char *)e + \ e = (BoundEntry *)((char *)e + \
((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \ ((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 = (size_t)p - e->start; \
} \ } \
addr += offset + dsize; \ 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 INVALID_POINTER; /* return an invalid pointer */ \
} \
return p + offset; \ return p + offset; \
} }
@ -228,7 +233,7 @@ void FASTCALL __bound_local_new(void *p1)
{ {
size_t addr, size, fp, *p = p1; size_t addr, size, fp, *p = p1;
#ifdef BOUND_DEBUG #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 #endif
GET_CALLER_FP(fp); GET_CALLER_FP(fp);
for(;;) { for(;;) {
@ -364,6 +369,12 @@ void __bound_init(void)
size_t start, size; size_t start, size;
size_t *p; size_t *p;
static int inited;
if (inited)
return;
inited = 1;
#ifdef BOUND_DEBUG #ifdef BOUND_DEBUG
fprintf(stderr, "%s, %s() start\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s, %s() start\n", __FILE__, __FUNCTION__);
#endif #endif
@ -444,7 +455,7 @@ void __bound_main_arg(void **p)
void *start = p; void *start = p;
while (*p++); while (*p++);
#ifdef BOUND_DEBUG #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); __FILE__, __FUNCTION__, (void *) p - start);
#endif #endif
__bound_new_region(start, (void *) p - start); __bound_new_region(start, (void *) p - start);
@ -482,8 +493,10 @@ void __bound_new_region(void *p, size_t size)
BoundEntry *page, *e, *e2; BoundEntry *page, *e, *e2;
size_t t1_start, t1_end, i, t2_start, t2_end; size_t t1_start, t1_end, i, t2_start, t2_end;
__bound_init();
#ifdef BOUND_DEBUG #ifdef BOUND_DEBUG
fprintf(stderr, "%s, %s(%p, %zx) start\n", fprintf(stderr, "%s, %s(%p, %p) start\n",
__FILE__, __FUNCTION__, p, size); __FILE__, __FUNCTION__, p, size);
#endif #endif
@ -595,6 +608,8 @@ int __bound_delete_region(void *p)
BoundEntry *page, *e, *e2; BoundEntry *page, *e, *e2;
size_t t1_start, t1_end, t2_start, t2_end, i; size_t t1_start, t1_end, t2_start, t2_end, i;
__bound_init();
#ifdef BOUND_DEBUG #ifdef BOUND_DEBUG
fprintf(stderr, "%s %s() start\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s %s() start\n", __FILE__, __FUNCTION__);
#endif #endif
@ -757,7 +772,7 @@ void *__bound_malloc(size_t size, const void *caller)
return NULL; return NULL;
#ifdef BOUND_DEBUG #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); __FILE__, __FUNCTION__, ptr, size);
#endif #endif
__bound_new_region(ptr, size); __bound_new_region(ptr, size);
@ -791,7 +806,7 @@ void *__bound_memalign(size_t size, size_t align, const void *caller)
return NULL; return NULL;
#ifdef BOUND_DEBUG #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); __FILE__, __FUNCTION__, ptr, size);
#endif #endif
__bound_new_region(ptr, size); __bound_new_region(ptr, size);

View File

@ -1390,6 +1390,17 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
tcc_add_crt(s, "crti.o"); tcc_add_crt(s, "crti.o");
} }
#endif #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; return 0;
} }

View File

@ -1569,9 +1569,6 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1)
{ {
#ifdef CONFIG_TCC_BCHECK #ifdef CONFIG_TCC_BCHECK
addr_t *ptr; addr_t *ptr;
Section *init_section;
unsigned char *pinit;
int sym_index;
if (0 == s1->do_bounds_check) if (0 == s1->do_bounds_check)
return; return;
@ -1585,19 +1582,23 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1)
#ifdef TCC_TARGET_I386 #ifdef TCC_TARGET_I386
if (s1->output_type != TCC_OUTPUT_MEMORY) { if (s1->output_type != TCC_OUTPUT_MEMORY) {
/* add 'call __bound_init()' in .init section */ /* add 'call __bound_init()' in .init section */
init_section = find_section(s1, ".init");
pinit = section_ptr_add(init_section, 5); /* XXX not called on MSYS, reason is unknown. For this
pinit[0] = 0xe8; case a call to __bound_init is performed in bcheck.c
put32(pinit + 1, -4); when __bound_ptr_add, __bound_new_region,
sym_index = find_elf_sym(symtab_section, "__bound_init"); __bound_delete_region called */
if (!sym_index) {
tcc_add_support(s1, "bcheck.o"); int sym_index = find_elf_sym(symtab_section, "__bound_init");
sym_index = find_elf_sym(symtab_section, "__bound_init"); if (sym_index) {
if (!sym_index) Section *init_section = find_section(s1, ".init");
tcc_error("__bound_init not defined"); unsigned char *pinit = section_ptr_add(init_section, 5);
} pinit[0] = 0xe8;
put_elf_reloc(symtab_section, init_section, put32(pinit + 1, -4);
put_elf_reloc(symtab_section, init_section,
init_section->data_offset - 4, R_386_PC32, sym_index); init_section->data_offset - 4, R_386_PC32, sym_index);
}
else
tcc_warning("__bound_init not defined");
} }
#endif #endif
#endif #endif

View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#define NB_ITS 1000000 #define NB_ITS 1000000
//#define NB_ITS 1 //#define NB_ITS 1