History

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.

seyko committed 11 years ago

Commit 7e7e6141 parent 5cd43937 files · +70 −39Browse files

Unified · Split

7 files
  1. Makefile +7 −9
  2. configure +10 −5
  3. lib/Makefile +1 −1
  4. lib/bcheck.c +24 −9
  5. libtcc.c +11 −0
  6. tccelf.c +16 −15
  7. tests/boundtest.c +1 −0
Makefile+7−9
@@ -89,7 +89,7 @@ $(ARM_FPA_LD_CROSS)_LINK = arm-fpa-ld-tcc$(EXESUF) · 88 unmodified lines
8989 $(ARM_VFP_CROSS)_LINK = arm-vfp-tcc$(EXESUF)
9090 $(ARM_EABI_CROSS)_LINK = arm-eabi-tcc$(EXESUF)
9191
92−ifeq ($(HOST_OS),Windows)
92+ifeq ($(TARGETOS),Windows)
9393 ifeq ($(ARCH),i386)
9494 PROGS:=$($(WIN32_CROSS)_LINK)
9595 $($(WIN32_CROSS)_LINK)_TCC = yes
@@ -100,7 +100,7 @@ $($(WIN64_CROSS)_LINK)_TCC = yes · 4 unmodified lines
100100 endif
101101 endif
102102
103−ifeq ($(HOST_OS),Linux)
103+ifeq ($(TARGETOS),Linux)
104104 ifeq ($(ARCH),i386)
105105 PROGS:=$($(I386_CROSS)_LINK)
106106 $($(I386_CROSS)_LINK)_TCC = yes
@@ -140,7 +140,6 @@ PROGS_CROSS=$($(X64_CROSS)_LINK) $($(WIN32_CROSS)_LINK) $($(WIN64_CROSS)_LINK) $ · 33 unmodified lines
140140 LIBTCC1_CROSS=lib/i386-win/libtcc1.a lib/x86_64-win/libtcc1.a lib/i386/libtcc1.a lib/x86_64/libtcc1.a \
141141 lib/arm64/libtcc1.a
142142 LIBTCC1=libtcc1.a
143−BCHECK=yes
144143 else ifeq ($(ARCH),x86-64)
145144 ifeq ($(TARGETOS),Darwin)
146145 NATIVE_FILES=$(X86_64_FILES)
@@ -291,10 +290,6 @@ install: $(PROGS) $(TCCLIBS) $(TCCDOCS) · 144 unmodified lines
291290 ifneq ($(LIBTCC1),)
292291 mkdir -p "$(tccdir)/$(ARCH)"
293292 $(INSTALL) -m644 $(LIBTCC1) "$(tccdir)/$(ARCH)"
294−endif
295−ifneq ($(BCHECK),)
296− -mkdir -p "$(tccdir)/$(ARCH)"
297− $(INSTALL) -m644 lib/$(ARCH)/bcheck.o "$(tccdir)/$(ARCH)"
298293 endif
299294 $(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include"
300295 mkdir -p "$(libdir)"
@@ -315,7 +310,6 @@ ifdef CONFIG_CROSS · 14 unmodified lines
315310 ifneq ($(HOST_OS),Darwin)
316311 mkdir -p "$(tccdir)/arm64"
317312 $(INSTALL) -m644 lib/arm64/libtcc1.a "$(tccdir)/arm64"
318− $(INSTALL) -m644 lib/i386/bcheck.o "$(tccdir)/i386"
319313 endif
320314 $(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386"
321315 $(INSTALL) -m644 lib/x86_64/libtcc1.a "$(tccdir)/x86-64"
@@ -346,13 +340,14 @@ install: $(PROGS) $(TCCLIBS) $(TCCDOCS) · 24 unmodified lines
346340 mkdir -p "$(tccdir)/doc"
347341 mkdir -p "$(tccdir)/libtcc"
348342 $(INSTALLBIN) -m755 $(PROGS) "$(tccdir)"
343+ $(INSTALLBIN) -m755 tcc.exe "$(tccdir)"
349344 $(INSTALL) -m644 $(LIBTCC1) $(top_srcdir)/win32/lib/*.def "$(tccdir)/lib"
350345 cp -r $(top_srcdir)/win32/include/. "$(tccdir)/include"
351346 cp -r $(top_srcdir)/win32/examples/. "$(tccdir)/examples"
352347 $(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include"
353348 $(INSTALL) -m644 tcc-doc.html $(top_srcdir)/win32/tcc-win32.txt "$(tccdir)/doc"
354349 $(INSTALL) -m644 $(top_srcdir)/libtcc.h $(LIBTCC_EXTRA) "$(tccdir)/libtcc"
355− $(INSTALL) -m644 $(LIBTCC) $(tccdir)
350+ $(INSTALL) -m644 $(LIBTCC) "$(tccdir)"
356351 ifdef CONFIG_CROSS
357352 mkdir -p "$(tccdir)/lib/32"
358353 mkdir -p "$(tccdir)/lib/64"
@@ -385,6 +380,9 @@ clean: · 26 unmodified lines
385380 rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.log \
386381 *.exe a.out tags TAGS libtcc_test$(EXESUF) tcc$(EXESUF)
387382 -rm -r $(ARCH) arm64
383+ifeq ($(HOST_OS),Linux)
384+ -rm -r ./C:
385+endif
388386 -rm *-tcc$(EXESUF)
389387 $(MAKE) -C tests $@
390388 ifneq ($(LIBTCC1),)
configure+10−5
@@ -47,7 +47,12 @@ tcc_elfinterp="" · 46 unmodified lines
4747 tcc_lddir=
4848 confvars=
4949 cpu=
50+
5051 host_os=`uname`
52+case $host_os in
53+ MINGW32*) host_os=Windows; ;;
54+ *) ;;
55+esac
5156
5257 # OS specific
5358 targetos=`uname`
@@ -170,13 +175,13 @@ for opt do · 116 unmodified lines
170175 ;;
171176 --enable-gprof) gprof="yes"
172177 ;;
173− --enable-mingw32) mingw32="yes" ; cross_prefix="i686-pc-mingw32-" ; cpu=x86
178+ --enable-mingw32) mingw32="yes" ; cross_prefix="i686-pc-mingw32-" ; cpu=x86; targetos=Windows;
174179 ;;
175− --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; cpu=x86
180+ --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; cpu=x86; targetos=Windows;
176181 ;;
177− --enable-tcc32-mingw) mingw32="yes" ; cross_prefix="i386-win-" ; cpu=x86; cc=tcc
182+ --enable-tcc32-mingw) mingw32="yes" ; cross_prefix="i386-win-" ; cpu=x86; cc=tcc; targetos=Windows;
178183 ;;
179− --enable-tcc64-mingw) mingw32="yes" ; cross_prefix="x86_64-win-" ; cpu=x86_64; cc=tcc
184+ --enable-tcc64-mingw) mingw32="yes" ; cross_prefix="x86_64-win-" ; cpu=x86_64; cc=tcc; targetos=Windows;
180185 ;;
181186 --enable-cross) build_cross="yes"
182187 ;;
@@ -210,7 +215,7 @@ classify_cpu "$cpu" · 27 unmodified lines
210215
211216 # Checking for CFLAGS
212217 if test -z "$CFLAGS"; then
213− CFLAGS="-Wall -g -O2"
218+ CFLAGS="-Wall -g -O0"
214219 fi
215220
216221 if test "$mingw32" = "yes" ; then
lib/Makefile+1−1
@@ -32,9 +32,9 @@ ifndef TARGET # native library · 31 unmodified lines
3232 endif
3333 endif
3434 endif
35− BCHECK_O = bcheck.o
3635 endif
3736
37+BCHECK_O = bcheck.o
3838 DIR = $(TARGET)
3939
4040 native : ../libtcc1.a
lib/bcheck.c+24−9
@@ -150,6 +150,7 @@ static BoundEntry *__bound_find_region(BoundEntry *e1, void *p) · 149 unmodified lines
150150 static void bound_error(const char *fmt, ...)
151151 {
152152 __bound_error_msg = fmt;
153+ fprintf(stderr,"%s %s: %s\n", __FILE__, __FUNCTION__, fmt);
153154 *(int *)0 = 0; /* force a runtime error */
154155 }
155156
@@ -164,6 +165,9 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) · 8 unmodified lines
164165 {
165166 size_t addr = (size_t)p;
166167 BoundEntry *e;
168+
169+ __bound_init();
170+
167171 #if defined(BOUND_DEBUG)
168172 printf("%s %s: 0x%x %d\n", __FILE__, __FUNCTION__, (int)p, offset);
169173 #endif
@@ -179,9 +183,7 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) · 9 unmodified lines
179183 }
180184 addr += offset;
181185 if (addr >= e->size) {
182− #if defined(BOUND_DEBUG)
183− printf("%s %s: 0x%zx is outside of the region\n", __FILE__, __FUNCTION__, p + offset);
184− #endif
186+ fprintf(stderr,"%s %s: %p is outside of the region\n", __FILE__, __FUNCTION__, p + offset);
185187 return INVALID_POINTER; /* return an invalid pointer */
186188 }
187189 return p + offset;
@@ -195,6 +197,7 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ · 7 unmodified lines
195197 size_t addr = (size_t)p; \
196198 BoundEntry *e; \
197199 \
200+ __bound_init(); \
198201 e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \
199202 e = (BoundEntry *)((char *)e + \
200203 ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \
@@ -205,8 +208,10 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ · 4 unmodified lines
205208 addr = (size_t)p - e->start; \
206209 } \
207210 addr += offset + dsize; \
208− if (addr > e->size) \
211+ if (addr > e->size) { \
212+ fprintf(stderr,"%s %s: %p is outside of the region\n", __FILE__, __FUNCTION__, p + offset); \
209213 return INVALID_POINTER; /* return an invalid pointer */ \
214+ } \
210215 return p + offset; \
211216 }
212217
@@ -228,7 +233,7 @@ void FASTCALL __bound_local_new(void *p1) · 15 unmodified lines
228233 {
229234 size_t addr, size, fp, *p = p1;
230235 #ifdef BOUND_DEBUG
231− fprintf(stderr, "%s, %s start p1=%zx *p1=%zx\n", __FILE__, __FUNCTION__, p, *p);
236+ fprintf(stderr, "%s, %s start p1=%p *p1=%p\n", __FILE__, __FUNCTION__, p, *p);
232237 #endif
233238 GET_CALLER_FP(fp);
234239 for(;;) {
@@ -364,6 +369,12 @@ void __bound_init(void) · 129 unmodified lines
364369 size_t start, size;
365370 size_t *p;
366371
372+ static int inited;
373+ if (inited)
374+ return;
375+
376+ inited = 1;
377+
367378 #ifdef BOUND_DEBUG
368379 fprintf(stderr, "%s, %s() start\n", __FILE__, __FUNCTION__);
369380 #endif
@@ -444,7 +455,7 @@ void __bound_main_arg(void **p) · 74 unmodified lines
444455 void *start = p;
445456 while (*p++);
446457 #ifdef BOUND_DEBUG
447− fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n",
458+ fprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n",
448459 __FILE__, __FUNCTION__, (void *) p - start);
449460 #endif
450461 __bound_new_region(start, (void *) p - start);
@@ -482,8 +493,10 @@ void __bound_new_region(void *p, size_t size) · 31 unmodified lines
482493 BoundEntry *page, *e, *e2;
483494 size_t t1_start, t1_end, i, t2_start, t2_end;
484495
496+ __bound_init();
497+
485498 #ifdef BOUND_DEBUG
486− fprintf(stderr, "%s, %s(%p, %zx) start\n",
499+ fprintf(stderr, "%s, %s(%p, %p) start\n",
487500 __FILE__, __FUNCTION__, p, size);
488501 #endif
489502
@@ -595,6 +608,8 @@ int __bound_delete_region(void *p) · 105 unmodified lines
595608 BoundEntry *page, *e, *e2;
596609 size_t t1_start, t1_end, t2_start, t2_end, i;
597610
611+ __bound_init();
612+
598613 #ifdef BOUND_DEBUG
599614 fprintf(stderr, "%s %s() start\n", __FILE__, __FUNCTION__);
600615 #endif
@@ -757,7 +772,7 @@ void *__bound_malloc(size_t size, const void *caller) · 156 unmodified lines
757772 return NULL;
758773
759774 #ifdef BOUND_DEBUG
760− fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n",
775+ fprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n",
761776 __FILE__, __FUNCTION__, ptr, size);
762777 #endif
763778 __bound_new_region(ptr, size);
@@ -791,7 +806,7 @@ void *__bound_memalign(size_t size, size_t align, const void *caller) · 27 unmodified lines
791806 return NULL;
792807
793808 #ifdef BOUND_DEBUG
794− fprintf(stderr, "%s, %s calling __bound_new_region(%p, %llx)\n",
809+ fprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n",
795810 __FILE__, __FUNCTION__, ptr, size);
796811 #endif
797812 __bound_new_region(ptr, size);
libtcc.c+11−0
@@ -1390,6 +1390,17 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) · 1389 unmodified lines
13901390 tcc_add_crt(s, "crti.o");
13911391 }
13921392 #endif
1393+
1394+#ifdef CONFIG_TCC_BCHECK
1395+ if (s->do_bounds_check && (output_type == TCC_OUTPUT_EXE))
1396+ {
1397+ /* XXX force a bcheck.o linking by compiling a function with a local array.
1398+ Otherwise bcheck.o may be not linked. */
1399+
1400+ if (tcc_compile_string(s, "static void __bound_check_dummy_func(){int v[1];}") == -1)
1401+ tcc_warning("compiling __bound_check_dummy_func failed");
1402+ }
1403+#endif
13931404 return 0;
13941405 }
13951406
tccelf.c+16−15
@@ -1569,9 +1569,6 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) · 1568 unmodified lines
15691569 {
15701570 #ifdef CONFIG_TCC_BCHECK
15711571 addr_t *ptr;
1572− Section *init_section;
1573− unsigned char *pinit;
1574− int sym_index;
15751572
15761573 if (0 == s1->do_bounds_check)
15771574 return;
@@ -1585,19 +1582,23 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) · 7 unmodified lines
15851582 #ifdef TCC_TARGET_I386
15861583 if (s1->output_type != TCC_OUTPUT_MEMORY) {
15871584 /* add 'call __bound_init()' in .init section */
1588− init_section = find_section(s1, ".init");
1589− pinit = section_ptr_add(init_section, 5);
1590− pinit[0] = 0xe8;
1591− put32(pinit + 1, -4);
1592− sym_index = find_elf_sym(symtab_section, "__bound_init");
1593− if (!sym_index) {
1594− tcc_add_support(s1, "bcheck.o");
1595− sym_index = find_elf_sym(symtab_section, "__bound_init");
1596− if (!sym_index)
1597− tcc_error("__bound_init not defined");
1598− }
1599− put_elf_reloc(symtab_section, init_section,
1585+
1586+ /* XXX not called on MSYS, reason is unknown. For this
1587+ case a call to __bound_init is performed in bcheck.c
1588+ when __bound_ptr_add, __bound_new_region,
1589+ __bound_delete_region called */
1590+
1591+ int sym_index = find_elf_sym(symtab_section, "__bound_init");
1592+ if (sym_index) {
1593+ Section *init_section = find_section(s1, ".init");
1594+ unsigned char *pinit = section_ptr_add(init_section, 5);
1595+ pinit[0] = 0xe8;
1596+ put32(pinit + 1, -4);
1597+ put_elf_reloc(symtab_section, init_section,
16001598 init_section->data_offset - 4, R_386_PC32, sym_index);
1599+ }
1600+ else
1601+ tcc_warning("__bound_init not defined");
16011602 }
16021603 #endif
16031604 #endif
tests/boundtest.c+1−0
@@ -1,5 +1,6 @@
11 #include <stdlib.h>
22 #include <stdio.h>
3+#include <string.h>
34
45 #define NB_ITS 1000000
56 //#define NB_ITS 1

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t