Unified · Split
| @@ -76,6 +76,7 @@ int tcc_relocate(TCCState *s1) · 75 unmodified lines | ||
| 76 | 76 | int tcc_run(TCCState *s1, int argc, char **argv) |
| 77 | 77 | { |
| 78 | 78 | int (*prog_main)(int, char **); |
| 79 | + int ret; | |
| 79 | 80 | |
| 80 | 81 | if (tcc_relocate(s1) < 0) |
| 81 | 82 | return -1; |
| @@ -83,38 +84,30 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 1 unmodified line | ||
| 83 | 84 | prog_main = tcc_get_symbol_err(s1, "main"); |
| 84 | 85 | |
| 85 | 86 | #ifdef CONFIG_TCC_BACKTRACE |
| 86 | − if (s1->do_debug) | |
| 87 | + if (s1->do_debug) { | |
| 87 | 88 | set_exception_handler(); |
| 89 | + rt_prog_main = prog_main; | |
| 90 | + } | |
| 88 | 91 | #endif |
| 89 | 92 | |
| 90 | 93 | #ifdef CONFIG_TCC_BCHECK |
| 91 | 94 | if (s1->do_bounds_check) { |
| 92 | 95 | void (*bound_init)(void); |
| 93 | 96 | void (*bound_exit)(void); |
| 94 | − int ret; | |
| 95 | 97 | /* set error function */ |
| 96 | 98 | rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); |
| 97 | − rt_prog_main = prog_main; | |
| 98 | 99 | /* XXX: use .init section so that it also work in binary ? */ |
| 99 | 100 | bound_init = tcc_get_symbol_err(s1, "__bound_init"); |
| 100 | 101 | bound_exit = tcc_get_symbol_err(s1, "__bound_exit"); |
| 101 | 102 | bound_init(); |
| 102 | 103 | ret = (*prog_main)(argc, argv); |
| 103 | 104 | bound_exit(); |
| 104 | − return ret; | |
| 105 | − } | |
| 106 | −#endif | |
| 107 | − | |
| 108 | −#ifdef TCC_TARGET_PE | |
| 109 | − { | |
| 110 | − unsigned char *p = tcc_get_symbol(s1, "tinyc_no_getbp"); | |
| 111 | − if (p) *p = 0; | |
| 112 | − } | |
| 105 | + } else | |
| 113 | 106 | #endif |
| 114 | − return (*prog_main)(argc, argv); | |
| 107 | + ret = (*prog_main)(argc, argv); | |
| 108 | + return ret; | |
| 115 | 109 | } |
| 116 | 110 | |
| 117 | − | |
| 118 | 111 | /* relocate code. Return -1 on error, required size if ptr is NULL, |
| 119 | 112 | otherwise copy code into buffer passed by the caller */ |
| 120 | 113 | static int tcc_relocate_ex(TCCState *s1, void *ptr) |
| @@ -370,9 +363,10 @@ static void rt_error(ucontext_t *uc, const char *fmt, ...) · 249 unmodified lines | ||
| 370 | 363 | uplong pc; |
| 371 | 364 | int i; |
| 372 | 365 | |
| 373 | − va_start(ap, fmt); | |
| 374 | 366 | fprintf(stderr, "Runtime error: "); |
| 367 | + va_start(ap, fmt); | |
| 375 | 368 | vfprintf(stderr, fmt, ap); |
| 369 | + va_end(ap); | |
| 376 | 370 | fprintf(stderr, "\n"); |
| 377 | 371 | |
| 378 | 372 | for(i=0;i<num_callers;i++) { |
| @@ -382,8 +376,6 @@ static void rt_error(ucontext_t *uc, const char *fmt, ...) · 3 unmodified lines | ||
| 382 | 376 | if (pc == (uplong)rt_prog_main && pc) |
| 383 | 377 | break; |
| 384 | 378 | } |
| 385 | − exit(255); | |
| 386 | − va_end(ap); | |
| 387 | 379 | } |
| 388 | 380 | |
| 389 | 381 | /* ------------------------------------------------------------- */ |
| @@ -581,18 +573,27 @@ static int rt_get_caller_pc(unsigned long *paddr, · 191 unmodified lines | ||
| 581 | 573 | |
| 582 | 574 | static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info) |
| 583 | 575 | { |
| 584 | − CONTEXT *uc = ex_info->ContextRecord; | |
| 585 | −/* | |
| 586 | 576 | EXCEPTION_RECORD *er = ex_info->ExceptionRecord; |
| 587 | − printf("CPU exception: code=%08lx addr=%p\n", | |
| 588 | − er->ExceptionCode, er->ExceptionAddress); | |
| 589 | −*/ | |
| 590 | − if (rt_bound_error_msg && *rt_bound_error_msg) | |
| 591 | − rt_error(uc, *rt_bound_error_msg); | |
| 592 | − else | |
| 593 | − rt_error(uc, "dereferencing invalid pointer"); | |
| 594 | − exit(255); | |
| 595 | − //return EXCEPTION_CONTINUE_SEARCH; | |
| 577 | + CONTEXT *uc = ex_info->ContextRecord; | |
| 578 | + switch (er->ExceptionCode) { | |
| 579 | + case EXCEPTION_ACCESS_VIOLATION: | |
| 580 | + if (rt_bound_error_msg && *rt_bound_error_msg) | |
| 581 | + rt_error(uc, *rt_bound_error_msg); | |
| 582 | + else | |
| 583 | + rt_error(uc, "access violation"); | |
| 584 | + break; | |
| 585 | + case EXCEPTION_STACK_OVERFLOW: | |
| 586 | + rt_error(uc, "stack overflow"); | |
| 587 | + break; | |
| 588 | + case EXCEPTION_INT_DIVIDE_BY_ZERO: | |
| 589 | + rt_error(uc, "division by zero"); | |
| 590 | + break; | |
| 591 | + default: | |
| 592 | + rt_error(uc, "exception caught"); | |
| 593 | + break; | |
| 594 | + } | |
| 595 | + exit(-1); | |
| 596 | + return EXCEPTION_CONTINUE_SEARCH; | |
| 596 | 597 | } |
| 597 | 598 | |
| 598 | 599 | /* Generate a stack backtrace when a CPU exception occurs. */ |