SECTION_ALIGNMENT -> RUN_SECTION_ALIGNMENT, and tweaks
[tinycc.git] / tccrun.c
blob7b533df2b159ea3f36fbbd425e0a6d70c1bbe482
1 /*
2 * TCC - Tiny C Compiler - Support for -run switch
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /* only native compiler supports -run */
24 #ifdef TCC_IS_NATIVE
26 #ifndef _WIN32
27 # include <sys/mman.h>
28 #endif
30 #ifdef CONFIG_TCC_BACKTRACE
31 # ifndef _WIN32
32 # include <signal.h>
33 # ifndef __OpenBSD__
34 # include <sys/ucontext.h>
35 # endif
36 # else
37 # define ucontext_t CONTEXT
38 # endif
39 ST_DATA int rt_num_callers = 6;
40 ST_DATA const char **rt_bound_error_msg;
41 ST_DATA void *rt_prog_main;
42 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level);
43 static void rt_error(ucontext_t *uc, const char *fmt, ...);
44 static void set_exception_handler(void);
45 #endif
47 static void set_pages_executable(void *ptr, unsigned long length);
48 static int tcc_relocate_ex(TCCState *s1, void *ptr);
50 #ifdef _WIN64
51 static void *win64_add_function_table(TCCState *s1);
52 static void win64_del_function_table(void *);
53 #endif
55 // #define HAVE_SELINUX
57 /* ------------------------------------------------------------- */
58 /* Do all relocations (needed before using tcc_get_symbol())
59 Returns -1 on error. */
61 LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
63 int size; void *mem;
65 if (TCC_RELOCATE_AUTO != ptr)
66 return tcc_relocate_ex(s1, ptr);
68 size = tcc_relocate_ex(s1, NULL);
69 if (size < 0)
70 return -1;
72 #ifdef HAVE_SELINUX
73 { /* Use mmap instead of malloc for Selinux. Ref:
74 http://www.gnu.org/s/libc/manual/html_node/File-Size.html */
76 char tmpfname[] = "/tmp/.tccrunXXXXXX";
77 int fd = mkstemp (tmpfname);
78 void *wr_mem;
80 unlink (tmpfname);
81 ftruncate (fd, size);
83 wr_mem = mmap (NULL, size, PROT_READ|PROT_WRITE,
84 MAP_SHARED, fd, 0);
85 if (wr_mem == MAP_FAILED)
86 tcc_error("/tmp not writeable");
87 mem = mmap (NULL, size, PROT_READ|PROT_EXEC,
88 MAP_SHARED, fd, 0);
89 if (mem == MAP_FAILED)
90 tcc_error("/tmp not executable");
92 tcc_relocate_ex(s1, wr_mem);
93 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
94 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, wr_mem);
95 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
97 #else
98 mem = tcc_malloc(size);
99 tcc_relocate_ex(s1, mem); /* no more errors expected */
100 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
101 #endif
102 return 0;
105 ST_FUNC void tcc_run_free(TCCState *s1)
107 int i;
109 for (i = 0; i < s1->nb_runtime_mem; ++i) {
110 #ifdef HAVE_SELINUX
111 int size = (int)(addr_t)s1->runtime_mem[i];
112 munmap(s1->runtime_mem[++i], size);
113 munmap(s1->runtime_mem[++i], size);
114 #else
115 # ifdef _WIN64
116 win64_del_function_table(*(void**)s1->runtime_mem[i]);
117 # endif
118 tcc_free(s1->runtime_mem[i]);
119 #endif
121 tcc_free(s1->runtime_mem);
124 /* launch the compiled program with the given arguments */
125 LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
127 int (*prog_main)(int, char **);
129 if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
130 return -1;
131 prog_main = tcc_get_symbol_err(s1, s1->runtime_main);
133 #ifdef CONFIG_TCC_BACKTRACE
134 if (s1->do_debug) {
135 set_exception_handler();
136 rt_prog_main = prog_main;
138 #endif
140 errno = 0; /* clean errno value */
142 #ifdef CONFIG_TCC_BCHECK
143 if (s1->do_bounds_check) {
144 void (*bound_init)(void);
145 void (*bound_exit)(void);
146 void (*bound_new_region)(void *p, addr_t size);
147 int (*bound_delete_region)(void *p);
148 int i, ret;
150 /* set error function */
151 rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
152 /* XXX: use .init section so that it also work in binary ? */
153 bound_init = tcc_get_symbol_err(s1, "__bound_init");
154 bound_exit = tcc_get_symbol_err(s1, "__bound_exit");
155 bound_new_region = tcc_get_symbol_err(s1, "__bound_new_region");
156 bound_delete_region = tcc_get_symbol_err(s1, "__bound_delete_region");
158 bound_init();
159 /* mark argv area as valid */
160 bound_new_region(argv, argc*sizeof(argv[0]));
161 for (i=0; i<argc; ++i)
162 bound_new_region(argv[i], strlen(argv[i]) + 1);
164 ret = (*prog_main)(argc, argv);
166 /* unmark argv area */
167 for (i=0; i<argc; ++i)
168 bound_delete_region(argv[i]);
169 bound_delete_region(argv);
170 bound_exit();
171 return ret;
173 #endif
174 return (*prog_main)(argc, argv);
177 #ifdef TCC_TARGET_X86_64
178 #define RUN_SECTION_ALIGNMENT 63
179 #else
180 #define RUN_SECTION_ALIGNMENT 15
181 #endif
183 /* relocate code. Return -1 on error, required size if ptr is NULL,
184 otherwise copy code into buffer passed by the caller */
185 static int tcc_relocate_ex(TCCState *s1, void *ptr)
187 Section *s;
188 unsigned long offset, length;
189 addr_t mem;
190 int i;
192 if (NULL == ptr) {
193 s1->nb_errors = 0;
194 #ifdef TCC_TARGET_PE
195 pe_output_file(s1, NULL);
196 #else
197 tcc_add_runtime(s1);
198 relocate_common_syms();
199 tcc_add_linker_symbols(s1);
200 build_got_entries(s1);
201 #endif
202 if (s1->nb_errors)
203 return -1;
206 offset = 0, mem = (addr_t)ptr;
207 mem += -(int)mem & RUN_SECTION_ALIGNMENT;
208 #ifdef _WIN64
209 offset += sizeof (void*);
210 #endif
211 for(i = 1; i < s1->nb_sections; i++) {
212 s = s1->sections[i];
213 if (0 == (s->sh_flags & SHF_ALLOC))
214 continue;
215 offset = (offset + RUN_SECTION_ALIGNMENT) & ~RUN_SECTION_ALIGNMENT;
216 s->sh_addr = mem ? mem + offset : 0;
217 offset += s->data_offset;
220 /* relocate symbols */
221 relocate_syms(s1, s1->symtab, 1);
222 if (s1->nb_errors)
223 return -1;
225 if (0 == mem)
226 return offset + RUN_SECTION_ALIGNMENT;
228 /* relocate each section */
229 for(i = 1; i < s1->nb_sections; i++) {
230 s = s1->sections[i];
231 if (s->reloc)
232 relocate_section(s1, s);
234 relocate_plt(s1);
236 #ifdef _WIN64
237 *(void**)ptr = win64_add_function_table(s1);
238 #endif
240 for(i = 1; i < s1->nb_sections; i++) {
241 s = s1->sections[i];
242 if (0 == (s->sh_flags & SHF_ALLOC))
243 continue;
244 length = s->data_offset;
245 // printf("%-12s %08lx %04x\n", s->name, s->sh_addr, length);
246 ptr = (void*)s->sh_addr;
247 if (NULL == s->data || s->sh_type == SHT_NOBITS)
248 memset(ptr, 0, length);
249 else
250 memcpy(ptr, s->data, length);
251 /* mark executable sections as executable in memory */
252 if (s->sh_flags & SHF_EXECINSTR)
253 set_pages_executable(ptr, length);
255 return 0;
258 /* ------------------------------------------------------------- */
259 /* allow to run code in memory */
261 static void set_pages_executable(void *ptr, unsigned long length)
263 #ifdef _WIN32
264 unsigned long old_protect;
265 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
266 #else
267 #ifndef PAGESIZE
268 # define PAGESIZE 4096
269 #endif
270 addr_t start, end;
271 start = (addr_t)ptr & ~(PAGESIZE - 1);
272 end = (addr_t)ptr + length;
273 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
274 if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))
275 tcc_error("mprotect failed: did you mean to configure --with-selinux?");
276 #if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
277 { extern void __clear_cache(void *beginning, void *end);
278 __clear_cache(ptr, (char *)ptr + length); }
279 #endif
280 #endif
283 #ifdef _WIN64
284 static void *win64_add_function_table(TCCState *s1)
286 void *p = NULL;
287 if (s1->uw_pdata) {
288 p = (void*)s1->uw_pdata->sh_addr;
289 RtlAddFunctionTable(
290 (RUNTIME_FUNCTION*)p,
291 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
292 text_section->sh_addr
294 s1->uw_pdata = NULL;
296 return p;;
299 static void win64_del_function_table(void *p)
301 if (p) {
302 RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p);
305 #endif
307 /* ------------------------------------------------------------- */
308 #ifdef CONFIG_TCC_BACKTRACE
310 ST_FUNC void tcc_set_num_callers(int n)
312 rt_num_callers = n;
315 /* print the position in the source file of PC value 'pc' by reading
316 the stabs debug information */
317 static addr_t rt_printline(addr_t wanted_pc, const char *msg)
319 char func_name[128], last_func_name[128];
320 addr_t func_addr, last_pc, pc;
321 const char *incl_files[INCLUDE_STACK_SIZE];
322 int incl_index, len, last_line_num, i;
323 const char *str, *p;
325 Stab_Sym *stab_sym = NULL, *stab_sym_end, *sym;
326 int stab_len = 0;
327 char *stab_str = NULL;
329 if (stab_section) {
330 stab_len = stab_section->data_offset;
331 stab_sym = (Stab_Sym *)stab_section->data;
332 stab_str = (char *) stabstr_section->data;
335 func_name[0] = '\0';
336 func_addr = 0;
337 incl_index = 0;
338 last_func_name[0] = '\0';
339 last_pc = (addr_t)-1;
340 last_line_num = 1;
342 if (!stab_sym)
343 goto no_stabs;
345 stab_sym_end = (Stab_Sym*)((char*)stab_sym + stab_len);
346 for (sym = stab_sym + 1; sym < stab_sym_end; ++sym) {
347 switch(sym->n_type) {
348 /* function start or end */
349 case N_FUN:
350 if (sym->n_strx == 0) {
351 /* we test if between last line and end of function */
352 pc = sym->n_value + func_addr;
353 if (wanted_pc >= last_pc && wanted_pc < pc)
354 goto found;
355 func_name[0] = '\0';
356 func_addr = 0;
357 } else {
358 str = stab_str + sym->n_strx;
359 p = strchr(str, ':');
360 if (!p) {
361 pstrcpy(func_name, sizeof(func_name), str);
362 } else {
363 len = p - str;
364 if (len > sizeof(func_name) - 1)
365 len = sizeof(func_name) - 1;
366 memcpy(func_name, str, len);
367 func_name[len] = '\0';
369 func_addr = sym->n_value;
371 break;
372 /* line number info */
373 case N_SLINE:
374 pc = sym->n_value + func_addr;
375 if (wanted_pc >= last_pc && wanted_pc < pc)
376 goto found;
377 last_pc = pc;
378 last_line_num = sym->n_desc;
379 /* XXX: slow! */
380 strcpy(last_func_name, func_name);
381 break;
382 /* include files */
383 case N_BINCL:
384 str = stab_str + sym->n_strx;
385 add_incl:
386 if (incl_index < INCLUDE_STACK_SIZE) {
387 incl_files[incl_index++] = str;
389 break;
390 case N_EINCL:
391 if (incl_index > 1)
392 incl_index--;
393 break;
394 case N_SO:
395 if (sym->n_strx == 0) {
396 incl_index = 0; /* end of translation unit */
397 } else {
398 str = stab_str + sym->n_strx;
399 /* do not add path */
400 len = strlen(str);
401 if (len > 0 && str[len - 1] != '/')
402 goto add_incl;
404 break;
408 no_stabs:
409 /* second pass: we try symtab symbols (no line number info) */
410 incl_index = 0;
411 if (symtab_section)
413 ElfW(Sym) *sym, *sym_end;
414 int type;
416 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
417 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
418 sym < sym_end;
419 sym++) {
420 type = ELFW(ST_TYPE)(sym->st_info);
421 if (type == STT_FUNC || type == STT_GNU_IFUNC) {
422 if (wanted_pc >= sym->st_value &&
423 wanted_pc < sym->st_value + sym->st_size) {
424 pstrcpy(last_func_name, sizeof(last_func_name),
425 (char *) strtab_section->data + sym->st_name);
426 func_addr = sym->st_value;
427 goto found;
432 /* did not find any info: */
433 fprintf(stderr, "%s %p ???\n", msg, (void*)wanted_pc);
434 fflush(stderr);
435 return 0;
436 found:
437 i = incl_index;
438 if (i > 0)
439 fprintf(stderr, "%s:%d: ", incl_files[--i], last_line_num);
440 fprintf(stderr, "%s %p", msg, (void*)wanted_pc);
441 if (last_func_name[0] != '\0')
442 fprintf(stderr, " %s()", last_func_name);
443 if (--i >= 0) {
444 fprintf(stderr, " (included from ");
445 for (;;) {
446 fprintf(stderr, "%s", incl_files[i]);
447 if (--i < 0)
448 break;
449 fprintf(stderr, ", ");
451 fprintf(stderr, ")");
453 fprintf(stderr, "\n");
454 fflush(stderr);
455 return func_addr;
458 /* emit a run time error at position 'pc' */
459 static void rt_error(ucontext_t *uc, const char *fmt, ...)
461 va_list ap;
462 addr_t pc;
463 int i;
465 fprintf(stderr, "Runtime error: ");
466 va_start(ap, fmt);
467 vfprintf(stderr, fmt, ap);
468 va_end(ap);
469 fprintf(stderr, "\n");
471 for(i=0;i<rt_num_callers;i++) {
472 if (rt_get_caller_pc(&pc, uc, i) < 0)
473 break;
474 pc = rt_printline(pc, i ? "by" : "at");
475 if (pc == (addr_t)rt_prog_main && pc)
476 break;
480 /* ------------------------------------------------------------- */
481 #ifndef _WIN32
483 /* signal handler for fatal errors */
484 static void sig_error(int signum, siginfo_t *siginf, void *puc)
486 ucontext_t *uc = puc;
488 switch(signum) {
489 case SIGFPE:
490 switch(siginf->si_code) {
491 case FPE_INTDIV:
492 case FPE_FLTDIV:
493 rt_error(uc, "division by zero");
494 break;
495 default:
496 rt_error(uc, "floating point exception");
497 break;
499 break;
500 case SIGBUS:
501 case SIGSEGV:
502 if (rt_bound_error_msg && *rt_bound_error_msg)
503 rt_error(uc, *rt_bound_error_msg);
504 else
505 rt_error(uc, "dereferencing invalid pointer");
506 break;
507 case SIGILL:
508 rt_error(uc, "illegal instruction");
509 break;
510 case SIGABRT:
511 rt_error(uc, "abort() called");
512 break;
513 default:
514 rt_error(uc, "caught signal %d", signum);
515 break;
517 exit(255);
520 #ifndef SA_SIGINFO
521 # define SA_SIGINFO 0x00000004u
522 #endif
524 /* Generate a stack backtrace when a CPU exception occurs. */
525 static void set_exception_handler(void)
527 struct sigaction sigact;
528 /* install TCC signal handlers to print debug info on fatal
529 runtime errors */
530 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
531 sigact.sa_sigaction = sig_error;
532 sigemptyset(&sigact.sa_mask);
533 sigaction(SIGFPE, &sigact, NULL);
534 sigaction(SIGILL, &sigact, NULL);
535 sigaction(SIGSEGV, &sigact, NULL);
536 sigaction(SIGBUS, &sigact, NULL);
537 sigaction(SIGABRT, &sigact, NULL);
540 /* ------------------------------------------------------------- */
541 #ifdef __i386__
543 /* fix for glibc 2.1 */
544 #ifndef REG_EIP
545 #define REG_EIP EIP
546 #define REG_EBP EBP
547 #endif
549 /* return the PC at frame level 'level'. Return negative if not found */
550 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
552 addr_t fp;
553 int i;
555 if (level == 0) {
556 #if defined(__APPLE__)
557 *paddr = uc->uc_mcontext->__ss.__eip;
558 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
559 *paddr = uc->uc_mcontext.mc_eip;
560 #elif defined(__dietlibc__)
561 *paddr = uc->uc_mcontext.eip;
562 #elif defined(__NetBSD__)
563 *paddr = uc->uc_mcontext.__gregs[_REG_EIP];
564 #elif defined(__OpenBSD__)
565 *paddr = uc->sc_eip;
566 #else
567 *paddr = uc->uc_mcontext.gregs[REG_EIP];
568 #endif
569 return 0;
570 } else {
571 #if defined(__APPLE__)
572 fp = uc->uc_mcontext->__ss.__ebp;
573 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
574 fp = uc->uc_mcontext.mc_ebp;
575 #elif defined(__dietlibc__)
576 fp = uc->uc_mcontext.ebp;
577 #elif defined(__NetBSD__)
578 fp = uc->uc_mcontext.__gregs[_REG_EBP];
579 #elif defined(__OpenBSD__)
580 *paddr = uc->sc_ebp;
581 #else
582 fp = uc->uc_mcontext.gregs[REG_EBP];
583 #endif
584 for(i=1;i<level;i++) {
585 /* XXX: check address validity with program info */
586 if (fp <= 0x1000 || fp >= 0xc0000000)
587 return -1;
588 fp = ((addr_t *)fp)[0];
590 *paddr = ((addr_t *)fp)[1];
591 return 0;
595 /* ------------------------------------------------------------- */
596 #elif defined(__x86_64__)
598 /* return the PC at frame level 'level'. Return negative if not found */
599 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
601 addr_t fp;
602 int i;
604 if (level == 0) {
605 /* XXX: only support linux */
606 #if defined(__APPLE__)
607 *paddr = uc->uc_mcontext->__ss.__rip;
608 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
609 *paddr = uc->uc_mcontext.mc_rip;
610 #elif defined(__NetBSD__)
611 *paddr = uc->uc_mcontext.__gregs[_REG_RIP];
612 #else
613 *paddr = uc->uc_mcontext.gregs[REG_RIP];
614 #endif
615 return 0;
616 } else {
617 #if defined(__APPLE__)
618 fp = uc->uc_mcontext->__ss.__rbp;
619 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
620 fp = uc->uc_mcontext.mc_rbp;
621 #elif defined(__NetBSD__)
622 fp = uc->uc_mcontext.__gregs[_REG_RBP];
623 #else
624 fp = uc->uc_mcontext.gregs[REG_RBP];
625 #endif
626 for(i=1;i<level;i++) {
627 /* XXX: check address validity with program info */
628 if (fp <= 0x1000)
629 return -1;
630 fp = ((addr_t *)fp)[0];
632 *paddr = ((addr_t *)fp)[1];
633 return 0;
637 /* ------------------------------------------------------------- */
638 #elif defined(__arm__)
640 /* return the PC at frame level 'level'. Return negative if not found */
641 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
643 addr_t fp, sp;
644 int i;
646 if (level == 0) {
647 /* XXX: only supports linux */
648 #if defined(__linux__)
649 *paddr = uc->uc_mcontext.arm_pc;
650 #else
651 return -1;
652 #endif
653 return 0;
654 } else {
655 #if defined(__linux__)
656 fp = uc->uc_mcontext.arm_fp;
657 sp = uc->uc_mcontext.arm_sp;
658 if (sp < 0x1000)
659 sp = 0x1000;
660 #else
661 return -1;
662 #endif
663 /* XXX: specific to tinycc stack frames */
664 if (fp < sp + 12 || fp & 3)
665 return -1;
666 for(i = 1; i < level; i++) {
667 sp = ((addr_t *)fp)[-2];
668 if (sp < fp || sp - fp > 16 || sp & 3)
669 return -1;
670 fp = ((addr_t *)fp)[-3];
671 if (fp <= sp || fp - sp < 12 || fp & 3)
672 return -1;
674 /* XXX: check address validity with program info */
675 *paddr = ((addr_t *)fp)[-1];
676 return 0;
680 /* ------------------------------------------------------------- */
681 #elif defined(__aarch64__)
683 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
685 if (level < 0)
686 return -1;
687 else if (level == 0) {
688 *paddr = uc->uc_mcontext.pc;
689 return 0;
691 else {
692 addr_t *fp = (addr_t *)uc->uc_mcontext.regs[29];
693 int i;
694 for (i = 1; i < level; i++)
695 fp = (addr_t *)fp[0];
696 *paddr = fp[1];
697 return 0;
701 /* ------------------------------------------------------------- */
702 #else
704 #warning add arch specific rt_get_caller_pc()
705 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
707 return -1;
710 #endif /* !__i386__ */
712 /* ------------------------------------------------------------- */
713 #else /* WIN32 */
715 static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
717 EXCEPTION_RECORD *er = ex_info->ExceptionRecord;
718 CONTEXT *uc = ex_info->ContextRecord;
719 switch (er->ExceptionCode) {
720 case EXCEPTION_ACCESS_VIOLATION:
721 if (rt_bound_error_msg && *rt_bound_error_msg)
722 rt_error(uc, *rt_bound_error_msg);
723 else
724 rt_error(uc, "access violation");
725 break;
726 case EXCEPTION_STACK_OVERFLOW:
727 rt_error(uc, "stack overflow");
728 break;
729 case EXCEPTION_INT_DIVIDE_BY_ZERO:
730 rt_error(uc, "division by zero");
731 break;
732 default:
733 rt_error(uc, "exception caught");
734 break;
736 return EXCEPTION_EXECUTE_HANDLER;
739 /* Generate a stack backtrace when a CPU exception occurs. */
740 static void set_exception_handler(void)
742 SetUnhandledExceptionFilter(cpu_exception_handler);
745 /* return the PC at frame level 'level'. Return non zero if not found */
746 static int rt_get_caller_pc(addr_t *paddr, CONTEXT *uc, int level)
748 addr_t fp, pc;
749 int i;
750 #ifdef _WIN64
751 pc = uc->Rip;
752 fp = uc->Rbp;
753 #else
754 pc = uc->Eip;
755 fp = uc->Ebp;
756 #endif
757 if (level > 0) {
758 for(i=1;i<level;i++) {
759 /* XXX: check address validity with program info */
760 if (fp <= 0x1000 || fp >= 0xc0000000)
761 return -1;
762 fp = ((addr_t*)fp)[0];
764 pc = ((addr_t*)fp)[1];
766 *paddr = pc;
767 return 0;
770 #endif /* _WIN32 */
771 #endif /* CONFIG_TCC_BACKTRACE */
772 /* ------------------------------------------------------------- */
773 #ifdef CONFIG_TCC_STATIC
775 /* dummy function for profiling */
776 ST_FUNC void *dlopen(const char *filename, int flag)
778 return NULL;
781 ST_FUNC void dlclose(void *p)
785 ST_FUNC const char *dlerror(void)
787 return "error";
790 typedef struct TCCSyms {
791 char *str;
792 void *ptr;
793 } TCCSyms;
796 /* add the symbol you want here if no dynamic linking is done */
797 static TCCSyms tcc_syms[] = {
798 #if !defined(CONFIG_TCCBOOT)
799 #define TCCSYM(a) { #a, &a, },
800 TCCSYM(printf)
801 TCCSYM(fprintf)
802 TCCSYM(fopen)
803 TCCSYM(fclose)
804 #undef TCCSYM
805 #endif
806 { NULL, NULL },
809 ST_FUNC void *dlsym(void *handle, const char *symbol)
811 TCCSyms *p;
812 p = tcc_syms;
813 while (p->str != NULL) {
814 if (!strcmp(p->str, symbol))
815 return p->ptr;
816 p++;
818 return NULL;
821 #endif /* CONFIG_TCC_STATIC */
822 #endif /* TCC_IS_NATIVE */
823 /* ------------------------------------------------------------- */