tccgen: inline_functions double free fix
[tinycc.git] / tccrun.c
blob3e32de4c45ce3f4bddd989a273399f3e31494807
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 /* ------------------------------------------------------------- */
56 /* Do all relocations (needed before using tcc_get_symbol())
57 Returns -1 on error. */
59 LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
61 int size; void *mem;
63 if (TCC_RELOCATE_AUTO != ptr)
64 return tcc_relocate_ex(s1, ptr);
66 size = tcc_relocate_ex(s1, NULL);
67 if (size < 0)
68 return -1;
70 #ifdef HAVE_SELINUX
71 { /* Use mmap instead of malloc for Selinux. Ref:
72 http://www.gnu.org/s/libc/manual/html_node/File-Size.html */
74 char tmpfname[] = "/tmp/.tccrunXXXXXX";
75 int fd = mkstemp (tmpfname);
76 void *wr_mem;
78 unlink (tmpfname);
79 ftruncate (fd, size);
81 wr_mem = mmap (NULL, size, PROT_READ|PROT_WRITE,
82 MAP_SHARED, fd, 0);
83 if (wr_mem == MAP_FAILED)
84 tcc_error("/tmp not writeable");
85 mem = mmap (NULL, size, PROT_READ|PROT_EXEC,
86 MAP_SHARED, fd, 0);
87 if (mem == MAP_FAILED)
88 tcc_error("/tmp not executable");
90 tcc_relocate_ex(s1, wr_mem);
91 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
92 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, wr_mem);
93 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
95 #else
96 mem = tcc_malloc(size);
97 tcc_relocate_ex(s1, mem); /* no more errors expected */
98 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
99 #endif
100 return 0;
103 ST_FUNC void tcc_run_free(TCCState *s1)
105 int i;
107 for (i = 0; i < s1->nb_runtime_mem; ++i) {
108 #ifdef HAVE_SELINUX
109 int size = (int)(addr_t)s1->runtime_mem[i];
110 munmap(s1->runtime_mem[++i], size);
111 munmap(s1->runtime_mem[++i], size);
112 #else
113 # ifdef _WIN64
114 win64_del_function_table(*(void**)s1->runtime_mem[i]);
115 # endif
116 tcc_free(s1->runtime_mem[i]);
117 #endif
119 tcc_free(s1->runtime_mem);
122 /* launch the compiled program with the given arguments */
123 LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
125 int (*prog_main)(int, char **);
127 if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
128 return -1;
129 prog_main = tcc_get_symbol_err(s1, s1->runtime_main);
131 #ifdef CONFIG_TCC_BACKTRACE
132 if (s1->do_debug) {
133 set_exception_handler();
134 rt_prog_main = prog_main;
136 #endif
138 errno = 0; /* clean errno value */
140 #ifdef CONFIG_TCC_BCHECK
141 if (s1->do_bounds_check) {
142 void (*bound_init)(void);
143 void (*bound_exit)(void);
144 void (*bound_new_region)(void *p, addr_t size);
145 int (*bound_delete_region)(void *p);
146 int i, ret;
148 /* set error function */
149 rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
150 /* XXX: use .init section so that it also work in binary ? */
151 bound_init = tcc_get_symbol_err(s1, "__bound_init");
152 bound_exit = tcc_get_symbol_err(s1, "__bound_exit");
153 bound_new_region = tcc_get_symbol_err(s1, "__bound_new_region");
154 bound_delete_region = tcc_get_symbol_err(s1, "__bound_delete_region");
156 bound_init();
157 /* mark argv area as valid */
158 bound_new_region(argv, argc*sizeof(argv[0]));
159 for (i=0; i<argc; ++i)
160 bound_new_region(argv[i], strlen(argv[i]) + 1);
162 ret = (*prog_main)(argc, argv);
164 /* unmark argv area */
165 for (i=0; i<argc; ++i)
166 bound_delete_region(argv[i]);
167 bound_delete_region(argv);
168 bound_exit();
169 return ret;
171 #endif
172 return (*prog_main)(argc, argv);
175 /* relocate code. Return -1 on error, required size if ptr is NULL,
176 otherwise copy code into buffer passed by the caller */
177 static int tcc_relocate_ex(TCCState *s1, void *ptr)
179 Section *s;
180 unsigned long offset, length;
181 addr_t mem;
182 int i;
184 if (NULL == ptr) {
185 s1->nb_errors = 0;
186 #ifdef TCC_TARGET_PE
187 pe_output_file(s1, NULL);
188 #else
189 tcc_add_runtime(s1);
190 relocate_common_syms();
191 tcc_add_linker_symbols(s1);
192 build_got_entries(s1);
193 #endif
194 if (s1->nb_errors)
195 return -1;
198 offset = 0, mem = (addr_t)ptr;
199 #ifdef _WIN64
200 offset += sizeof (void*);
201 #endif
202 for(i = 1; i < s1->nb_sections; i++) {
203 s = s1->sections[i];
204 if (0 == (s->sh_flags & SHF_ALLOC))
205 continue;
206 offset = (offset + 15) & ~15;
207 s->sh_addr = mem ? mem + offset : 0;
208 offset += s->data_offset;
211 /* relocate symbols */
212 relocate_syms(s1, 1);
213 if (s1->nb_errors)
214 return -1;
216 if (0 == mem)
217 return offset;
219 /* relocate each section */
220 for(i = 1; i < s1->nb_sections; i++) {
221 s = s1->sections[i];
222 if (s->reloc)
223 relocate_section(s1, s);
225 relocate_plt(s1);
227 #ifdef _WIN64
228 *(void**)ptr = win64_add_function_table(s1);
229 #endif
231 for(i = 1; i < s1->nb_sections; i++) {
232 s = s1->sections[i];
233 if (0 == (s->sh_flags & SHF_ALLOC))
234 continue;
235 length = s->data_offset;
236 // printf("%-12s %08lx %04x\n", s->name, s->sh_addr, length);
237 ptr = (void*)s->sh_addr;
238 if (NULL == s->data || s->sh_type == SHT_NOBITS)
239 memset(ptr, 0, length);
240 else
241 memcpy(ptr, s->data, length);
242 /* mark executable sections as executable in memory */
243 if (s->sh_flags & SHF_EXECINSTR)
244 set_pages_executable(ptr, length);
246 return 0;
249 /* ------------------------------------------------------------- */
250 /* allow to run code in memory */
252 static void set_pages_executable(void *ptr, unsigned long length)
254 #ifdef _WIN32
255 unsigned long old_protect;
256 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
257 #else
258 #ifndef PAGESIZE
259 # define PAGESIZE 4096
260 #endif
261 addr_t start, end;
262 start = (addr_t)ptr & ~(PAGESIZE - 1);
263 end = (addr_t)ptr + length;
264 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
265 if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))
266 tcc_error("mprotect failed: did you mean to configure --with-selinux?");
267 #if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
268 { extern void __clear_cache(void *beginning, void *end);
269 __clear_cache(ptr, (char *)ptr + length); }
270 #endif
271 #endif
274 #ifdef _WIN64
275 static void *win64_add_function_table(TCCState *s1)
277 void *p = NULL;
278 int r;
279 if (s1->uw_pdata) {
280 p = (void*)s1->uw_pdata->sh_addr;
281 r = RtlAddFunctionTable(
282 (RUNTIME_FUNCTION*)p,
283 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
284 text_section->sh_addr
286 s1->uw_pdata = NULL;
288 return p;;
291 static void win64_del_function_table(void *p)
293 if (p) {
294 RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p);
297 #endif
299 /* ------------------------------------------------------------- */
300 #ifdef CONFIG_TCC_BACKTRACE
302 ST_FUNC void tcc_set_num_callers(int n)
304 rt_num_callers = n;
307 /* print the position in the source file of PC value 'pc' by reading
308 the stabs debug information */
309 static addr_t rt_printline(addr_t wanted_pc, const char *msg)
311 char func_name[128], last_func_name[128];
312 addr_t func_addr, last_pc, pc;
313 const char *incl_files[INCLUDE_STACK_SIZE];
314 int incl_index, len, last_line_num, i;
315 const char *str, *p;
317 Stab_Sym *stab_sym = NULL, *stab_sym_end, *sym;
318 int stab_len = 0;
319 char *stab_str = NULL;
321 if (stab_section) {
322 stab_len = stab_section->data_offset;
323 stab_sym = (Stab_Sym *)stab_section->data;
324 stab_str = (char *) stabstr_section->data;
327 func_name[0] = '\0';
328 func_addr = 0;
329 incl_index = 0;
330 last_func_name[0] = '\0';
331 last_pc = (addr_t)-1;
332 last_line_num = 1;
334 if (!stab_sym)
335 goto no_stabs;
337 stab_sym_end = (Stab_Sym*)((char*)stab_sym + stab_len);
338 for (sym = stab_sym + 1; sym < stab_sym_end; ++sym) {
339 switch(sym->n_type) {
340 /* function start or end */
341 case N_FUN:
342 if (sym->n_strx == 0) {
343 /* we test if between last line and end of function */
344 pc = sym->n_value + func_addr;
345 if (wanted_pc >= last_pc && wanted_pc < pc)
346 goto found;
347 func_name[0] = '\0';
348 func_addr = 0;
349 } else {
350 str = stab_str + sym->n_strx;
351 p = strchr(str, ':');
352 if (!p) {
353 pstrcpy(func_name, sizeof(func_name), str);
354 } else {
355 len = p - str;
356 if (len > sizeof(func_name) - 1)
357 len = sizeof(func_name) - 1;
358 memcpy(func_name, str, len);
359 func_name[len] = '\0';
361 func_addr = sym->n_value;
363 break;
364 /* line number info */
365 case N_SLINE:
366 pc = sym->n_value + func_addr;
367 if (wanted_pc >= last_pc && wanted_pc < pc)
368 goto found;
369 last_pc = pc;
370 last_line_num = sym->n_desc;
371 /* XXX: slow! */
372 strcpy(last_func_name, func_name);
373 break;
374 /* include files */
375 case N_BINCL:
376 str = stab_str + sym->n_strx;
377 add_incl:
378 if (incl_index < INCLUDE_STACK_SIZE) {
379 incl_files[incl_index++] = str;
381 break;
382 case N_EINCL:
383 if (incl_index > 1)
384 incl_index--;
385 break;
386 case N_SO:
387 if (sym->n_strx == 0) {
388 incl_index = 0; /* end of translation unit */
389 } else {
390 str = stab_str + sym->n_strx;
391 /* do not add path */
392 len = strlen(str);
393 if (len > 0 && str[len - 1] != '/')
394 goto add_incl;
396 break;
400 no_stabs:
401 /* second pass: we try symtab symbols (no line number info) */
402 incl_index = 0;
403 if (symtab_section)
405 ElfW(Sym) *sym, *sym_end;
406 int type;
408 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
409 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
410 sym < sym_end;
411 sym++) {
412 type = ELFW(ST_TYPE)(sym->st_info);
413 if (type == STT_FUNC || type == STT_GNU_IFUNC) {
414 if (wanted_pc >= sym->st_value &&
415 wanted_pc < sym->st_value + sym->st_size) {
416 pstrcpy(last_func_name, sizeof(last_func_name),
417 (char *) strtab_section->data + sym->st_name);
418 func_addr = sym->st_value;
419 goto found;
424 /* did not find any info: */
425 fprintf(stderr, "%s %p ???\n", msg, (void*)wanted_pc);
426 fflush(stderr);
427 return 0;
428 found:
429 i = incl_index;
430 if (i > 0)
431 fprintf(stderr, "%s:%d: ", incl_files[--i], last_line_num);
432 fprintf(stderr, "%s %p", msg, (void*)wanted_pc);
433 if (last_func_name[0] != '\0')
434 fprintf(stderr, " %s()", last_func_name);
435 if (--i >= 0) {
436 fprintf(stderr, " (included from ");
437 for (;;) {
438 fprintf(stderr, "%s", incl_files[i]);
439 if (--i < 0)
440 break;
441 fprintf(stderr, ", ");
443 fprintf(stderr, ")");
445 fprintf(stderr, "\n");
446 fflush(stderr);
447 return func_addr;
450 /* emit a run time error at position 'pc' */
451 static void rt_error(ucontext_t *uc, const char *fmt, ...)
453 va_list ap;
454 addr_t pc;
455 int i;
457 fprintf(stderr, "Runtime error: ");
458 va_start(ap, fmt);
459 vfprintf(stderr, fmt, ap);
460 va_end(ap);
461 fprintf(stderr, "\n");
463 for(i=0;i<rt_num_callers;i++) {
464 if (rt_get_caller_pc(&pc, uc, i) < 0)
465 break;
466 pc = rt_printline(pc, i ? "by" : "at");
467 if (pc == (addr_t)rt_prog_main && pc)
468 break;
472 /* ------------------------------------------------------------- */
473 #ifndef _WIN32
475 /* signal handler for fatal errors */
476 static void sig_error(int signum, siginfo_t *siginf, void *puc)
478 ucontext_t *uc = puc;
480 switch(signum) {
481 case SIGFPE:
482 switch(siginf->si_code) {
483 case FPE_INTDIV:
484 case FPE_FLTDIV:
485 rt_error(uc, "division by zero");
486 break;
487 default:
488 rt_error(uc, "floating point exception");
489 break;
491 break;
492 case SIGBUS:
493 case SIGSEGV:
494 if (rt_bound_error_msg && *rt_bound_error_msg)
495 rt_error(uc, *rt_bound_error_msg);
496 else
497 rt_error(uc, "dereferencing invalid pointer");
498 break;
499 case SIGILL:
500 rt_error(uc, "illegal instruction");
501 break;
502 case SIGABRT:
503 rt_error(uc, "abort() called");
504 break;
505 default:
506 rt_error(uc, "caught signal %d", signum);
507 break;
509 exit(255);
512 #ifndef SA_SIGINFO
513 # define SA_SIGINFO 0x00000004u
514 #endif
516 /* Generate a stack backtrace when a CPU exception occurs. */
517 static void set_exception_handler(void)
519 struct sigaction sigact;
520 /* install TCC signal handlers to print debug info on fatal
521 runtime errors */
522 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
523 sigact.sa_sigaction = sig_error;
524 sigemptyset(&sigact.sa_mask);
525 sigaction(SIGFPE, &sigact, NULL);
526 sigaction(SIGILL, &sigact, NULL);
527 sigaction(SIGSEGV, &sigact, NULL);
528 sigaction(SIGBUS, &sigact, NULL);
529 sigaction(SIGABRT, &sigact, NULL);
532 /* ------------------------------------------------------------- */
533 #ifdef __i386__
535 /* fix for glibc 2.1 */
536 #ifndef REG_EIP
537 #define REG_EIP EIP
538 #define REG_EBP EBP
539 #endif
541 /* return the PC at frame level 'level'. Return negative if not found */
542 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
544 addr_t fp;
545 int i;
547 if (level == 0) {
548 #if defined(__APPLE__)
549 *paddr = uc->uc_mcontext->__ss.__eip;
550 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
551 *paddr = uc->uc_mcontext.mc_eip;
552 #elif defined(__dietlibc__)
553 *paddr = uc->uc_mcontext.eip;
554 #elif defined(__NetBSD__)
555 *paddr = uc->uc_mcontext.__gregs[_REG_EIP];
556 #elif defined(__OpenBSD__)
557 *paddr = uc->sc_eip;
558 #else
559 *paddr = uc->uc_mcontext.gregs[REG_EIP];
560 #endif
561 return 0;
562 } else {
563 #if defined(__APPLE__)
564 fp = uc->uc_mcontext->__ss.__ebp;
565 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
566 fp = uc->uc_mcontext.mc_ebp;
567 #elif defined(__dietlibc__)
568 fp = uc->uc_mcontext.ebp;
569 #elif defined(__NetBSD__)
570 fp = uc->uc_mcontext.__gregs[_REG_EBP];
571 #elif defined(__OpenBSD__)
572 *paddr = uc->sc_ebp;
573 #else
574 fp = uc->uc_mcontext.gregs[REG_EBP];
575 #endif
576 for(i=1;i<level;i++) {
577 /* XXX: check address validity with program info */
578 if (fp <= 0x1000 || fp >= 0xc0000000)
579 return -1;
580 fp = ((addr_t *)fp)[0];
582 *paddr = ((addr_t *)fp)[1];
583 return 0;
587 /* ------------------------------------------------------------- */
588 #elif defined(__x86_64__)
590 /* return the PC at frame level 'level'. Return negative if not found */
591 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
593 addr_t fp;
594 int i;
596 if (level == 0) {
597 /* XXX: only support linux */
598 #if defined(__APPLE__)
599 *paddr = uc->uc_mcontext->__ss.__rip;
600 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
601 *paddr = uc->uc_mcontext.mc_rip;
602 #elif defined(__NetBSD__)
603 *paddr = uc->uc_mcontext.__gregs[_REG_RIP];
604 #else
605 *paddr = uc->uc_mcontext.gregs[REG_RIP];
606 #endif
607 return 0;
608 } else {
609 #if defined(__APPLE__)
610 fp = uc->uc_mcontext->__ss.__rbp;
611 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
612 fp = uc->uc_mcontext.mc_rbp;
613 #elif defined(__NetBSD__)
614 fp = uc->uc_mcontext.__gregs[_REG_RBP];
615 #else
616 fp = uc->uc_mcontext.gregs[REG_RBP];
617 #endif
618 for(i=1;i<level;i++) {
619 /* XXX: check address validity with program info */
620 if (fp <= 0x1000)
621 return -1;
622 fp = ((addr_t *)fp)[0];
624 *paddr = ((addr_t *)fp)[1];
625 return 0;
629 /* ------------------------------------------------------------- */
630 #elif defined(__arm__)
632 /* return the PC at frame level 'level'. Return negative if not found */
633 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
635 addr_t fp, sp;
636 int i;
638 if (level == 0) {
639 /* XXX: only supports linux */
640 #if defined(__linux__)
641 *paddr = uc->uc_mcontext.arm_pc;
642 #else
643 return -1;
644 #endif
645 return 0;
646 } else {
647 #if defined(__linux__)
648 fp = uc->uc_mcontext.arm_fp;
649 sp = uc->uc_mcontext.arm_sp;
650 if (sp < 0x1000)
651 sp = 0x1000;
652 #else
653 return -1;
654 #endif
655 /* XXX: specific to tinycc stack frames */
656 if (fp < sp + 12 || fp & 3)
657 return -1;
658 for(i = 1; i < level; i++) {
659 sp = ((addr_t *)fp)[-2];
660 if (sp < fp || sp - fp > 16 || sp & 3)
661 return -1;
662 fp = ((addr_t *)fp)[-3];
663 if (fp <= sp || fp - sp < 12 || fp & 3)
664 return -1;
666 /* XXX: check address validity with program info */
667 *paddr = ((addr_t *)fp)[-1];
668 return 0;
672 /* ------------------------------------------------------------- */
673 #elif defined(__aarch64__)
675 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
677 if (level < 0)
678 return -1;
679 else if (level == 0) {
680 *paddr = uc->uc_mcontext.pc;
681 return 0;
683 else {
684 addr_t *fp = (addr_t *)uc->uc_mcontext.regs[29];
685 int i;
686 for (i = 1; i < level; i++)
687 fp = (addr_t *)fp[0];
688 *paddr = fp[1];
689 return 0;
693 /* ------------------------------------------------------------- */
694 #else
696 #warning add arch specific rt_get_caller_pc()
697 static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
699 return -1;
702 #endif /* !__i386__ */
704 /* ------------------------------------------------------------- */
705 #else /* WIN32 */
707 static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
709 EXCEPTION_RECORD *er = ex_info->ExceptionRecord;
710 CONTEXT *uc = ex_info->ContextRecord;
711 switch (er->ExceptionCode) {
712 case EXCEPTION_ACCESS_VIOLATION:
713 if (rt_bound_error_msg && *rt_bound_error_msg)
714 rt_error(uc, *rt_bound_error_msg);
715 else
716 rt_error(uc, "access violation");
717 break;
718 case EXCEPTION_STACK_OVERFLOW:
719 rt_error(uc, "stack overflow");
720 break;
721 case EXCEPTION_INT_DIVIDE_BY_ZERO:
722 rt_error(uc, "division by zero");
723 break;
724 default:
725 rt_error(uc, "exception caught");
726 break;
728 return EXCEPTION_EXECUTE_HANDLER;
731 /* Generate a stack backtrace when a CPU exception occurs. */
732 static void set_exception_handler(void)
734 SetUnhandledExceptionFilter(cpu_exception_handler);
737 /* return the PC at frame level 'level'. Return non zero if not found */
738 static int rt_get_caller_pc(addr_t *paddr, CONTEXT *uc, int level)
740 addr_t fp, pc;
741 int i;
742 #ifdef _WIN64
743 pc = uc->Rip;
744 fp = uc->Rbp;
745 #else
746 pc = uc->Eip;
747 fp = uc->Ebp;
748 #endif
749 if (level > 0) {
750 for(i=1;i<level;i++) {
751 /* XXX: check address validity with program info */
752 if (fp <= 0x1000 || fp >= 0xc0000000)
753 return -1;
754 fp = ((addr_t*)fp)[0];
756 pc = ((addr_t*)fp)[1];
758 *paddr = pc;
759 return 0;
762 #endif /* _WIN32 */
763 #endif /* CONFIG_TCC_BACKTRACE */
764 /* ------------------------------------------------------------- */
765 #ifdef CONFIG_TCC_STATIC
767 /* dummy function for profiling */
768 ST_FUNC void *dlopen(const char *filename, int flag)
770 return NULL;
773 ST_FUNC void dlclose(void *p)
777 ST_FUNC const char *dlerror(void)
779 return "error";
782 typedef struct TCCSyms {
783 char *str;
784 void *ptr;
785 } TCCSyms;
788 /* add the symbol you want here if no dynamic linking is done */
789 static TCCSyms tcc_syms[] = {
790 #if !defined(CONFIG_TCCBOOT)
791 #define TCCSYM(a) { #a, &a, },
792 TCCSYM(printf)
793 TCCSYM(fprintf)
794 TCCSYM(fopen)
795 TCCSYM(fclose)
796 #undef TCCSYM
797 #endif
798 { NULL, NULL },
801 ST_FUNC void *dlsym(int flag, const char *symbol)
803 TCCSyms *p;
804 p = tcc_syms;
805 while (p->str != NULL) {
806 if (!strcmp(p->str, symbol))
807 return p->ptr;
808 p++;
810 return NULL;
813 #endif /* CONFIG_TCC_STATIC */
814 #endif /* TCC_IS_NATIVE */
815 /* ------------------------------------------------------------- */