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