From 9c5bb16447e3d6cf5493c4802f3e3a1939430a27 Mon Sep 17 00:00:00 2001 From: grischka Date: Sat, 1 Oct 2016 20:47:36 +0200 Subject: [PATCH] Revert part of "fix installation amd bcheck for Windows" tccelf.c : force linking bcheck by adding elf symbol __bound_init bcheck.c : use (size_t)1 for x86_64 Fixes 7e7e6148fdb4adbda936f80b5d4ac3d738908d95 --- lib/alloca86_64-bt.S | 2 +- lib/bcheck.c | 62 +++++++++++++++++++++++++++++----------------------- libtcc.c | 10 --------- tccelf.c | 40 +++++++++++---------------------- tccgen.c | 13 ----------- tccpe.c | 2 +- tccrun.c | 20 +++++++---------- tcctok.h | 1 - 8 files changed, 58 insertions(+), 92 deletions(-) diff --git a/lib/alloca86_64-bt.S b/lib/alloca86_64-bt.S index ab8629da..1f196bb2 100644 --- a/lib/alloca86_64-bt.S +++ b/lib/alloca86_64-bt.S @@ -31,7 +31,7 @@ p3: #else pop %rdx mov %rdi,%rax - movl %rax,%rsi # size, a second parm to the __bound_new_region + mov %rax,%rsi # size, a second parm to the __bound_new_region add $15,%rax and $-16,%rax diff --git a/lib/bcheck.c b/lib/bcheck.c index 829e33d6..756c539b 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -59,12 +59,12 @@ #define BOUND_T3_BITS (sizeof(size_t)*8 - BOUND_T1_BITS - BOUND_T2_BITS) #define BOUND_E_BITS (sizeof(size_t)) -#define BOUND_T1_SIZE (1 << BOUND_T1_BITS) -#define BOUND_T2_SIZE (1 << BOUND_T2_BITS) -#define BOUND_T3_SIZE (1 << BOUND_T3_BITS) +#define BOUND_T1_SIZE ((size_t)1 << BOUND_T1_BITS) +#define BOUND_T2_SIZE ((size_t)1 << BOUND_T2_BITS) +#define BOUND_T3_SIZE ((size_t)1 << BOUND_T3_BITS) #define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS) -#define BOUND_T23_SIZE (1 << BOUND_T23_BITS) +#define BOUND_T23_SIZE ((size_t)1 << BOUND_T23_BITS) /* this pointer is generated when bound check is incorrect */ @@ -157,7 +157,7 @@ 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 */ + *(void **)0 = 0; /* force a runtime error */ } static void bound_alloc_error(void) @@ -172,9 +172,10 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) size_t addr = (size_t)p; BoundEntry *e; - __bound_init(); + dprintf(stderr, "%s %s: %p %x\n", + __FILE__, __FUNCTION__, p, (unsigned)offset); - dprintf(stderr, "%s %s: %p %p\n", __FILE__, __FUNCTION__, p, offset); + __bound_init(); e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; e = (BoundEntry *)((char *)e + @@ -187,7 +188,8 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) } addr += offset; if (addr >= e->size) { - fprintf(stderr,"%s %s: %p is outside of the region\n", __FILE__, __FUNCTION__, p + offset); + 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; @@ -201,7 +203,8 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ size_t addr = (size_t)p; \ BoundEntry *e; \ \ - dprintf(stderr, "%s %s: %p %p start\n", __FILE__, __FUNCTION__, p, offset); \ + dprintf(stderr, "%s %s: %p %x start\n", \ + __FILE__, __FUNCTION__, p, (unsigned)offset); \ \ __bound_init(); \ e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \ @@ -215,10 +218,12 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ } \ addr += offset + dsize; \ if (addr > e->size) { \ - fprintf(stderr,"%s %s: %p is outside of the region\n", __FILE__, __FUNCTION__, p + offset); \ + fprintf(stderr,"%s %s: %p is outside of the region\n", \ + __FILE__, __FUNCTION__, p + offset); \ return INVALID_POINTER; /* return an invalid pointer */ \ } \ - dprintf(stderr, "%s %s: return p+offset = %p\n", __FILE__, __FUNCTION__, p + offset); \ + dprintf(stderr, "%s %s: return p+offset = %p\n", \ + __FILE__, __FUNCTION__, p + offset); \ return p + offset; \ } @@ -456,14 +461,15 @@ void __bound_main_arg(void **p) void *start = p; while (*p++); - dprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n", - __FILE__, __FUNCTION__, (void *) p - start); + dprintf(stderr, "%s, %s calling __bound_new_region(%p %x)\n", + __FILE__, __FUNCTION__, start, (unsigned)((void *)p - start)); __bound_new_region(start, (void *) p - start); } void __bound_exit(void) { + dprintf(stderr, "%s, %s()\n", __FILE__, __FUNCTION__); restore_malloc_hooks(); } @@ -494,10 +500,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(); + dprintf(stderr, "%s, %s(%p, %x) start\n", + __FILE__, __FUNCTION__, p, (unsigned)size); - dprintf(stderr, "%s, %s(%p, %p) start\n", - __FILE__, __FUNCTION__, p, size); + __bound_init(); start = (size_t)p; end = start + size; @@ -557,8 +563,7 @@ void __bound_new_region(void *p, size_t size) } /* delete a region */ -static inline void delete_region(BoundEntry *e, - void *p, size_t empty_size) +static inline void delete_region(BoundEntry *e, void *p, size_t empty_size) { size_t addr; BoundEntry *e1; @@ -606,10 +611,10 @@ int __bound_delete_region(void *p) BoundEntry *page, *e, *e2; size_t t1_start, t1_end, t2_start, t2_end, i; - __bound_init(); - dprintf(stderr, "%s %s() start\n", __FILE__, __FUNCTION__); + __bound_init(); + start = (size_t)p; t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS); t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) & @@ -765,8 +770,8 @@ void *__bound_malloc(size_t size, const void *caller) if (!ptr) return NULL; - dprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n", - __FILE__, __FUNCTION__, ptr, size); + dprintf(stderr, "%s, %s calling __bound_new_region(%p, %x)\n", + __FILE__, __FUNCTION__, ptr, (unsigned)size); __bound_new_region(ptr, size); return ptr; @@ -798,8 +803,8 @@ void *__bound_memalign(size_t size, size_t align, const void *caller) if (!ptr) return NULL; - dprintf(stderr, "%s, %s calling __bound_new_region(%p, %p)\n", - __FILE__, __FUNCTION__, ptr, size); + dprintf(stderr, "%s, %s calling __bound_new_region(%p, %x)\n", + __FILE__, __FUNCTION__, ptr, (unsigned)size); __bound_new_region(ptr, size); return ptr; @@ -892,7 +897,8 @@ void *__bound_memcpy(void *dst, const void *src, size_t size) { void* p; - dprintf(stderr, "%s %s: start, dst=%p src=%p size=%p\n", __FILE__, __FUNCTION__, dst, src, size); + dprintf(stderr, "%s %s: start, dst=%p src=%p size=%x\n", + __FILE__, __FUNCTION__, dst, src, (unsigned)size); __bound_check(dst, size); __bound_check(src, size); @@ -942,9 +948,11 @@ char *__bound_strcpy(char *dst, const char *src) size_t len; void *p; - dprintf(stderr, "%s %s: strcpy start, dst=%p src=%p\n", __FILE__, __FUNCTION__, dst, src); + dprintf(stderr, "%s %s: strcpy start, dst=%p src=%p\n", + __FILE__, __FUNCTION__, dst, src); len = __bound_strlen(src); p = __bound_memcpy(dst, src, len + 1); - dprintf(stderr, "%s %s: strcpy end, p=%p\n", __FILE__, __FUNCTION__, dst, src, p); + dprintf(stderr, "%s %s: strcpy end, p = %p\n", + __FILE__, __FUNCTION__, p); return p; } diff --git a/libtcc.c b/libtcc.c index c092a04a..71e8c2f3 100644 --- a/libtcc.c +++ b/libtcc.c @@ -1674,16 +1674,6 @@ 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)) - { - /* force a bcheck.o linking */ - addr_t func = TOK___bound_init; - Sym *sym = external_global_sym(func, &func_old_type, 0); - if (!sym->c) - put_extern_sym(sym, NULL, 0, 0); - } -#endif if (s->normalize_inc_dirs) tcc_normalize_inc_dirs(s); return 0; diff --git a/tccelf.c b/tccelf.c index d47027b8..71e62aff 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1580,35 +1580,29 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) { #ifdef CONFIG_TCC_BCHECK addr_t *ptr; + int sym_index; if (0 == s1->do_bounds_check) return; - /* XXX: add an object file to do that */ ptr = section_ptr_add(bounds_section, sizeof(*ptr)); *ptr = 0; add_elf_sym(symtab_section, 0, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, bounds_section->sh_num, "__bounds_start"); + /* pull bcheck.o from libtcc1.a */ + sym_index = add_elf_sym(symtab_section, 0, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + SHN_UNDEF, "__bound_init"); if (s1->output_type != TCC_OUTPUT_MEMORY) { /* add 'call __bound_init()' in .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; - write32le(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"); + Section *init_section = find_section(s1, ".init"); + unsigned char *pinit = section_ptr_add(init_section, 5); + pinit[0] = 0xe8; + write32le(pinit + 1, -4); + put_elf_reloc(symtab_section, init_section, + init_section->data_offset - 4, R_386_PC32, sym_index); + /* R_386_PC32 = R_X86_64_PC32 = 2 */ } #endif } @@ -1616,8 +1610,8 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) /* add tcc runtime libraries */ ST_FUNC void tcc_add_runtime(TCCState *s1) { + tcc_add_bcheck(s1); tcc_add_pragma_libs(s1); - /* add libc */ if (!s1->nostdlib) { tcc_add_library(s1, "c"); @@ -1627,14 +1621,6 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) } #endif tcc_add_support(s1, "libtcc1.a"); - } - - /* tcc_add_bcheck tries to relocate a call to __bound_init in _init so - libtcc1.a must be loaded before for __bound_init to be defined and - crtn.o must be loaded after to not finalize _init too early. */ - tcc_add_bcheck(s1); - - if (!s1->nostdlib) { /* add crt end if not memory output */ if (s1->output_type != TCC_OUTPUT_MEMORY) tcc_add_crt(s1, "crtn.o"); diff --git a/tccgen.c b/tccgen.c index e3f2a691..598ba88d 100644 --- a/tccgen.c +++ b/tccgen.c @@ -6179,19 +6179,6 @@ static void gen_function(Sym *sym) gfunc_prolog(&sym->type); local_scope = 0; -#ifdef CONFIG_TCC_BCHECK - if (tcc_state->do_bounds_check && !strcmp(funcname, "main")) { - int i; - Sym *sym; - for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) { - if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD) - break; - vpush_global_sym(&func_old_type, TOK___bound_main_arg); - vset(&sym->type, sym->r, sym->c); - gfunc_call(1); - } - } -#endif rsym = 0; block(NULL, NULL, 0); gsym(rsym); diff --git a/tccpe.c b/tccpe.c index 83acc2f8..3e15d913 100644 --- a/tccpe.c +++ b/tccpe.c @@ -1809,8 +1809,8 @@ ST_FUNC int pe_output_file(TCCState * s1, const char *filename) pe.filename = filename; pe.s1 = s1; - pe_add_runtime(s1, &pe); tcc_add_bcheck(s1); + pe_add_runtime(s1, &pe); relocate_common_syms(); /* assign bss adresses */ tcc_add_linker_symbols(s1); diff --git a/tccrun.c b/tccrun.c index 9ee70e43..3a6a82db 100644 --- a/tccrun.c +++ b/tccrun.c @@ -92,11 +92,9 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr) LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) { int (*prog_main)(int, char **); - int ret; if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0) return -1; - prog_main = tcc_get_symbol_err(s1, s1->runtime_main); #ifdef CONFIG_TCC_BACKTRACE @@ -106,13 +104,15 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) } #endif + errno = 0; /* clean errno value */ + #ifdef CONFIG_TCC_BCHECK if (s1->do_bounds_check) { void (*bound_init)(void); void (*bound_exit)(void); void (*bound_new_region)(void *p, addr_t size); int (*bound_delete_region)(void *p); - int i; + int i, ret; /* set error function */ rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); @@ -121,28 +121,24 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) bound_exit = tcc_get_symbol_err(s1, "__bound_exit"); bound_new_region = tcc_get_symbol_err(s1, "__bound_new_region"); bound_delete_region = tcc_get_symbol_err(s1, "__bound_delete_region"); + bound_init(); /* mark argv area as valid */ bound_new_region(argv, argc*sizeof(argv[0])); for (i=0; i