Fix test90 for 32 bits targets
[tinycc.git] / tccrun.c
blob8e851ee2954c07e541123eab17c08e0a082fc500
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 #ifdef CONFIG_TCC_BACKTRACE
27 typedef struct rt_context
29 /* --> tccelf.c:tcc_add_btstub wants those below in that order: */
30 union {
31 struct {
32 Stab_Sym *stab_sym, *stab_sym_end;
33 char *stab_str;
35 struct {
36 unsigned char *dwarf_line, *dwarf_line_end, *dwarf_line_str;
39 addr_t dwarf;
40 ElfW(Sym) *esym_start, *esym_end;
41 char *elf_str;
42 addr_t prog_base;
43 void *bounds_start;
44 struct rt_context *next;
45 /* <-- */
46 int num_callers;
47 addr_t ip, fp, sp;
48 void *top_func;
49 jmp_buf jmp_buf;
50 char do_jmp;
51 } rt_context;
53 static rt_context g_rtctxt;
54 static void set_exception_handler(void);
55 static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);
56 static void rt_exit(int code);
57 #endif /* CONFIG_TCC_BACKTRACE */
59 /* defined when included from lib/bt-exe.c */
60 #ifndef CONFIG_TCC_BACKTRACE_ONLY
62 #ifndef _WIN32
63 # include <sys/mman.h>
64 #endif
66 static void set_pages_executable(TCCState *s1, int mode, void *ptr, unsigned long length);
67 static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff);
69 #ifdef _WIN64
70 static void *win64_add_function_table(TCCState *s1);
71 static void win64_del_function_table(void *);
72 #endif
74 /* ------------------------------------------------------------- */
75 /* Do all relocations (needed before using tcc_get_symbol())
76 Returns -1 on error. */
78 LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
80 int size;
81 addr_t ptr_diff = 0;
83 if (TCC_RELOCATE_AUTO != ptr)
84 return tcc_relocate_ex(s1, ptr, 0);
86 size = tcc_relocate_ex(s1, NULL, 0);
87 if (size < 0)
88 return -1;
90 #ifdef HAVE_SELINUX
92 /* Using mmap instead of malloc */
93 void *prx;
94 char tmpfname[] = "/tmp/.tccrunXXXXXX";
95 int fd = mkstemp(tmpfname);
96 unlink(tmpfname);
97 ftruncate(fd, size);
99 size = (size + (PAGESIZE-1)) & ~(PAGESIZE-1);
100 ptr = mmap(NULL, size * 2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
101 /* mmap RX memory at a fixed distance */
102 prx = mmap((char*)ptr + size, size, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_FIXED, fd, 0);
103 if (ptr == MAP_FAILED || prx == MAP_FAILED)
104 tcc_error("tccrun: could not map memory");
105 ptr_diff = (char*)prx - (char*)ptr;
106 close(fd);
107 //printf("map %p %p %p\n", ptr, prx, (void*)ptr_diff);
109 #else
110 ptr = tcc_malloc(size);
111 #endif
112 tcc_relocate_ex(s1, ptr, ptr_diff); /* no more errors expected */
113 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
114 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr);
115 return 0;
118 ST_FUNC void tcc_run_free(TCCState *s1)
120 int i;
122 for (i = 0; i < s1->nb_runtime_mem; i += 2) {
123 unsigned size = (unsigned)(addr_t)s1->runtime_mem[i];
124 void *ptr = s1->runtime_mem[i+1];
125 #ifdef HAVE_SELINUX
126 munmap(ptr, size * 2);
127 #else
128 /* unprotect memory to make it usable for malloc again */
129 set_pages_executable(s1, 2, ptr, size);
130 #ifdef _WIN64
131 win64_del_function_table(*(void**)ptr);
132 #endif
133 tcc_free(ptr);
134 #endif
136 tcc_free(s1->runtime_mem);
139 static void run_cdtors(TCCState *s1, const char *start, const char *end,
140 int argc, char **argv, char **envp)
142 void **a = (void **)get_sym_addr(s1, start, 0, 0);
143 void **b = (void **)get_sym_addr(s1, end, 0, 0);
144 while (a != b)
145 ((void(*)(int, char **, char **))*a++)(argc, argv, envp);
148 #define NR_AT_EXIT 32
150 static struct exit_context {
151 int exit_called;
152 int nr_exit;
153 void (*exitfunc[NR_AT_EXIT])(int, void *);
154 void *exitarg[NR_AT_EXIT];
155 #ifndef CONFIG_TCC_BACKTRACE
156 jmp_buf run_jmp_buf;
157 #endif
158 } g_exit_context;
160 static void init_exit(void)
162 struct exit_context *e = &g_exit_context;
164 e->exit_called = 0;
165 e->nr_exit = 0;
168 static void call_exit(int ret)
170 struct exit_context *e = &g_exit_context;
172 while (e->nr_exit) {
173 e->nr_exit--;
174 e->exitfunc[e->nr_exit](ret, e->exitarg[e->nr_exit]);
178 static int rt_atexit(void (*function)(void))
180 struct exit_context *e = &g_exit_context;
182 if (e->nr_exit < NR_AT_EXIT) {
183 e->exitfunc[e->nr_exit] = (void (*)(int, void *))function;
184 e->exitarg[e->nr_exit++] = NULL;
185 return 0;
187 return 1;
190 static int rt_on_exit(void (*function)(int, void *), void *arg)
192 struct exit_context *e = &g_exit_context;
194 if (e->nr_exit < NR_AT_EXIT) {
195 e->exitfunc[e->nr_exit] = function;
196 e->exitarg[e->nr_exit++] = arg;
197 return 0;
199 return 1;
202 static void run_exit(int code)
204 struct exit_context *e = &g_exit_context;
206 e->exit_called = 1;
207 #ifdef CONFIG_TCC_BACKTRACE
208 longjmp((&g_rtctxt)->jmp_buf, code ? code : 256);
209 #else
210 longjmp(e->run_jmp_buf, code ? code : 256);
211 #endif
214 /* launch the compiled program with the given arguments */
215 LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
217 int (*prog_main)(int, char **, char **), ret;
218 #ifdef CONFIG_TCC_BACKTRACE
219 rt_context *rc = &g_rtctxt;
220 #endif
222 #if defined(__APPLE__) || defined(__FreeBSD__)
223 char **envp = NULL;
224 #elif defined(__OpenBSD__) || defined(__NetBSD__)
225 extern char **environ;
226 char **envp = environ;
227 #else
228 char **envp = environ;
229 #endif
231 s1->runtime_main = s1->nostdlib ? "_start" : "main";
232 if ((s1->dflag & 16) && (addr_t)-1 == get_sym_addr(s1, s1->runtime_main, 0, 1))
233 return 0;
234 tcc_add_symbol(s1, "exit", run_exit);
235 tcc_add_symbol(s1, "atexit", rt_atexit);
236 tcc_add_symbol(s1, "on_exit", rt_on_exit);
237 if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
238 return -1;
239 prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1);
241 #ifdef CONFIG_TCC_BACKTRACE
242 memset(rc, 0, sizeof *rc);
243 if (s1->do_debug) {
244 void *p;
245 if (s1->dwarf) {
246 rc->dwarf_line = dwarf_line_section->data;
247 rc->dwarf_line_end = dwarf_line_section->data + dwarf_line_section->data_offset;
248 if (dwarf_line_str_section)
249 rc->dwarf_line_str = dwarf_line_str_section->data;
251 else
253 rc->stab_sym = (Stab_Sym *)stab_section->data;
254 rc->stab_sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
255 rc->stab_str = (char *)stab_section->link->data;
257 rc->dwarf = s1->dwarf;
258 rc->esym_start = (ElfW(Sym) *)(symtab_section->data);
259 rc->esym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
260 rc->elf_str = (char *)symtab_section->link->data;
261 #if PTR_SIZE == 8
262 rc->prog_base = text_section->sh_addr & 0xffffffff00000000ULL;
263 #if defined TCC_TARGET_MACHO
264 if (s1->dwarf)
265 rc->prog_base = (addr_t) -1;
266 #else
267 #endif
268 #endif
269 rc->top_func = tcc_get_symbol(s1, "main");
270 rc->num_callers = s1->rt_num_callers;
271 rc->do_jmp = 1;
272 if ((p = tcc_get_symbol(s1, "__rt_error")))
273 *(void**)p = _rt_error;
274 #ifdef CONFIG_TCC_BCHECK
275 if (s1->do_bounds_check) {
276 rc->bounds_start = (void*)bounds_section->sh_addr;
277 if ((p = tcc_get_symbol(s1, "__bound_init")))
278 ((void(*)(void*,int))p)(rc->bounds_start, 1);
280 #endif
281 set_exception_handler();
283 #endif
285 errno = 0; /* clean errno value */
286 fflush(stdout);
287 fflush(stderr);
288 init_exit();
289 /* These aren't C symbols, so don't need leading underscore handling. */
290 run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);
291 #ifdef CONFIG_TCC_BACKTRACE
292 if (!(ret = setjmp(rc->jmp_buf)))
293 #else
294 if (!(ret = setjmp((&g_exit_context)->run_jmp_buf)))
295 #endif
297 ret = prog_main(argc, argv, envp);
299 run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);
300 call_exit(ret);
301 if ((s1->dflag & 16) && ret)
302 fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
303 if ((s1->dflag & 16) == 0 && (&g_exit_context)->exit_called)
304 exit(ret);
305 return ret;
308 #define DEBUG_RUNMEN 0
310 /* enable rx/ro/rw permissions */
311 #define CONFIG_RUNMEM_RO 1
313 #if CONFIG_RUNMEM_RO
314 # define PAGE_ALIGN PAGESIZE
315 #elif defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
316 /* To avoid that x86 processors would reload cached instructions
317 each time when data is written in the near, we need to make
318 sure that code and data do not share the same 64 byte unit */
319 # define PAGE_ALIGN 64
320 #else
321 # define PAGE_ALIGN 1
322 #endif
324 /* relocate code. Return -1 on error, required size if ptr is NULL,
325 otherwise copy code into buffer passed by the caller */
326 static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff)
328 Section *s;
329 unsigned offset, length, align, max_align, i, k, f;
330 unsigned n, copy;
331 addr_t mem, addr;
333 if (NULL == ptr) {
334 s1->nb_errors = 0;
335 #ifdef TCC_TARGET_PE
336 pe_output_file(s1, NULL);
337 #else
338 tcc_add_runtime(s1);
339 resolve_common_syms(s1);
340 build_got_entries(s1, 0);
341 #endif
342 if (s1->nb_errors)
343 return -1;
346 offset = max_align = 0, mem = (addr_t)ptr;
347 #ifdef _WIN64
348 offset += sizeof (void*); /* space for function_table pointer */
349 #endif
350 copy = 0;
351 redo:
352 for (k = 0; k < 3; ++k) { /* 0:rx, 1:ro, 2:rw sections */
353 n = 0; addr = 0;
354 for(i = 1; i < s1->nb_sections; i++) {
355 static const char shf[] = {
356 SHF_ALLOC|SHF_EXECINSTR, SHF_ALLOC, SHF_ALLOC|SHF_WRITE
358 s = s1->sections[i];
359 if (shf[k] != (s->sh_flags & (SHF_ALLOC|SHF_WRITE|SHF_EXECINSTR)))
360 continue;
361 length = s->data_offset;
362 if (copy) {
363 if (addr == 0)
364 addr = s->sh_addr;
365 n = (s->sh_addr - addr) + length;
366 ptr = (void*)s->sh_addr;
367 if (k == 0)
368 ptr = (void*)(s->sh_addr - ptr_diff);
369 if (NULL == s->data || s->sh_type == SHT_NOBITS)
370 memset(ptr, 0, length);
371 else
372 memcpy(ptr, s->data, length);
373 #ifdef _WIN64
374 if (s == s1->uw_pdata)
375 *(void**)mem = win64_add_function_table(s1);
376 #endif
377 if (s->data) {
378 tcc_free(s->data);
379 s->data = NULL;
380 s->data_allocated = 0;
382 s->data_offset = 0;
383 continue;
385 align = s->sh_addralign - 1;
386 if (++n == 1 && align < (PAGE_ALIGN - 1))
387 align = (PAGE_ALIGN - 1);
388 if (max_align < align)
389 max_align = align;
390 addr = k ? mem : mem + ptr_diff;
391 offset += -(addr + offset) & align;
392 s->sh_addr = mem ? addr + offset : 0;
393 offset += length;
394 #if DEBUG_RUNMEN
395 if (mem)
396 printf("%d: %-16s %p len %04x align %04x\n",
397 k, s->name, (void*)s->sh_addr, length, align + 1);
398 #endif
400 if (copy) { /* set permissions */
401 if (k == 0 && ptr_diff)
402 continue; /* not with HAVE_SELINUX */
403 f = k;
404 #if !CONFIG_RUNMEM_RO
405 if (f != 0)
406 continue;
407 f = 3; /* change only SHF_EXECINSTR to rwx */
408 #endif
409 #if DEBUG_RUNMEN
410 printf("protect %d %p %04x\n", f, (void*)addr, n);
411 #endif
412 if (n)
413 set_pages_executable(s1, f, (void*)addr, n);
417 if (copy)
418 return 0;
420 /* relocate symbols */
421 relocate_syms(s1, s1->symtab, !(s1->nostdlib));
422 if (s1->nb_errors)
423 return -1;
424 if (0 == mem)
425 return offset + max_align;
427 #ifdef TCC_TARGET_PE
428 s1->pe_imagebase = mem;
429 #endif
431 /* relocate sections */
432 #ifndef TCC_TARGET_PE
433 relocate_plt(s1);
434 #endif
435 relocate_sections(s1);
436 copy = 1;
437 goto redo;
440 /* ------------------------------------------------------------- */
441 /* allow to run code in memory */
443 static void set_pages_executable(TCCState *s1, int mode, void *ptr, unsigned long length)
445 #ifdef _WIN32
446 static const unsigned char protect[] = {
447 PAGE_EXECUTE_READ,
448 PAGE_READONLY,
449 PAGE_READWRITE,
450 PAGE_EXECUTE_READWRITE
452 DWORD old;
453 VirtualProtect(ptr, length, protect[mode], &old);
454 #else
455 static const unsigned char protect[] = {
456 PROT_READ | PROT_EXEC,
457 PROT_READ,
458 PROT_READ | PROT_WRITE,
459 PROT_READ | PROT_WRITE | PROT_EXEC
461 addr_t start, end;
462 start = (addr_t)ptr & ~(PAGESIZE - 1);
463 end = (addr_t)ptr + length;
464 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
465 if (mprotect((void *)start, end - start, protect[mode]))
466 tcc_error("mprotect failed: did you mean to configure --with-selinux?");
468 /* XXX: BSD sometimes dump core with bad system call */
469 # if (defined TCC_TARGET_ARM && !TARGETOS_BSD) || defined TCC_TARGET_ARM64
470 if (mode == 0 || mode == 3) {
471 void __clear_cache(void *beginning, void *end);
472 __clear_cache(ptr, (char *)ptr + length);
474 # endif
476 #endif
479 #ifdef _WIN64
480 static void *win64_add_function_table(TCCState *s1)
482 void *p = NULL;
483 if (s1->uw_pdata) {
484 p = (void*)s1->uw_pdata->sh_addr;
485 RtlAddFunctionTable(
486 (RUNTIME_FUNCTION*)p,
487 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
488 s1->pe_imagebase
490 s1->uw_pdata = NULL;
492 return p;
495 static void win64_del_function_table(void *p)
497 if (p) {
498 RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p);
501 #endif
502 #endif //ndef CONFIG_TCC_BACKTRACE_ONLY
503 /* ------------------------------------------------------------- */
504 #ifdef CONFIG_TCC_BACKTRACE
506 static int rt_vprintf(const char *fmt, va_list ap)
508 int ret = vfprintf(stderr, fmt, ap);
509 fflush(stderr);
510 return ret;
513 static int rt_printf(const char *fmt, ...)
515 va_list ap;
516 int r;
517 va_start(ap, fmt);
518 r = rt_vprintf(fmt, ap);
519 va_end(ap);
520 return r;
523 static char *rt_elfsym(rt_context *rc, addr_t wanted_pc, addr_t *func_addr)
525 ElfW(Sym) *esym;
526 for (esym = rc->esym_start + 1; esym < rc->esym_end; ++esym) {
527 int type = ELFW(ST_TYPE)(esym->st_info);
528 if ((type == STT_FUNC || type == STT_GNU_IFUNC)
529 && wanted_pc >= esym->st_value
530 && wanted_pc < esym->st_value + esym->st_size) {
531 *func_addr = esym->st_value;
532 return rc->elf_str + esym->st_name;
535 return NULL;
538 #define INCLUDE_STACK_SIZE 32
540 /* print the position in the source file of PC value 'pc' by reading
541 the stabs debug information */
542 static addr_t rt_printline (rt_context *rc, addr_t wanted_pc,
543 const char *msg, const char *skip)
545 char func_name[128];
546 addr_t func_addr, last_pc, pc;
547 const char *incl_files[INCLUDE_STACK_SIZE];
548 int incl_index, last_incl_index, len, last_line_num, i;
549 const char *str, *p;
550 Stab_Sym *sym;
552 next:
553 func_name[0] = '\0';
554 func_addr = 0;
555 incl_index = 0;
556 last_pc = (addr_t)-1;
557 last_line_num = 1;
558 last_incl_index = 0;
560 for (sym = rc->stab_sym + 1; sym < rc->stab_sym_end; ++sym) {
561 str = rc->stab_str + sym->n_strx;
562 pc = sym->n_value;
564 switch(sym->n_type) {
565 case N_SLINE:
566 if (func_addr)
567 goto rel_pc;
568 case N_SO:
569 case N_SOL:
570 goto abs_pc;
571 case N_FUN:
572 if (sym->n_strx == 0) /* end of function */
573 goto rel_pc;
574 abs_pc:
575 #if PTR_SIZE == 8
576 /* Stab_Sym.n_value is only 32bits */
577 pc += rc->prog_base;
578 #endif
579 goto check_pc;
580 rel_pc:
581 pc += func_addr;
582 check_pc:
583 if (pc >= wanted_pc && wanted_pc >= last_pc)
584 goto found;
585 break;
588 switch(sym->n_type) {
589 /* function start or end */
590 case N_FUN:
591 if (sym->n_strx == 0)
592 goto reset_func;
593 p = strchr(str, ':');
594 if (0 == p || (len = p - str + 1, len > sizeof func_name))
595 len = sizeof func_name;
596 pstrcpy(func_name, len, str);
597 func_addr = pc;
598 break;
599 /* line number info */
600 case N_SLINE:
601 last_pc = pc;
602 last_line_num = sym->n_desc;
603 last_incl_index = incl_index;
604 break;
605 /* include files */
606 case N_BINCL:
607 if (incl_index < INCLUDE_STACK_SIZE)
608 incl_files[incl_index++] = str;
609 break;
610 case N_EINCL:
611 if (incl_index > 1)
612 incl_index--;
613 break;
614 /* start/end of translation unit */
615 case N_SO:
616 incl_index = 0;
617 if (sym->n_strx) {
618 /* do not add path */
619 len = strlen(str);
620 if (len > 0 && str[len - 1] != '/')
621 incl_files[incl_index++] = str;
623 reset_func:
624 func_name[0] = '\0';
625 func_addr = 0;
626 last_pc = (addr_t)-1;
627 break;
628 /* alternative file name (from #line or #include directives) */
629 case N_SOL:
630 if (incl_index)
631 incl_files[incl_index-1] = str;
632 break;
636 func_name[0] = '\0';
637 func_addr = 0;
638 last_incl_index = 0;
639 /* we try symtab symbols (no line number info) */
640 p = rt_elfsym(rc, wanted_pc, &func_addr);
641 if (p) {
642 pstrcpy(func_name, sizeof func_name, p);
643 goto found;
645 if ((rc = rc->next))
646 goto next;
647 found:
648 i = last_incl_index;
649 if (i > 0) {
650 str = incl_files[--i];
651 if (skip[0] && strstr(str, skip))
652 return (addr_t)-1;
653 rt_printf("%s:%d: ", str, last_line_num);
654 } else
655 rt_printf("%08llx : ", (long long)wanted_pc);
656 rt_printf("%s %s", msg, func_name[0] ? func_name : "???");
657 #if 0
658 if (--i >= 0) {
659 rt_printf(" (included from ");
660 for (;;) {
661 rt_printf("%s", incl_files[i]);
662 if (--i < 0)
663 break;
664 rt_printf(", ");
666 rt_printf(")");
668 #endif
669 return func_addr;
672 /* ------------------------------------------------------------- */
673 /* rt_printline - dwarf version */
675 #define MAX_128 ((8 * sizeof (long long) + 6) / 7)
677 #define DIR_TABLE_SIZE (64)
678 #define FILE_TABLE_SIZE (512)
680 #define dwarf_read_1(ln,end) \
681 ((ln) < (end) ? *(ln)++ : 0)
682 #define dwarf_read_2(ln,end) \
683 ((ln) + 2 < (end) ? (ln) += 2, read16le((ln) - 2) : 0)
684 #define dwarf_read_4(ln,end) \
685 ((ln) + 4 < (end) ? (ln) += 4, read32le((ln) - 4) : 0)
686 #define dwarf_read_8(ln,end) \
687 ((ln) + 8 < (end) ? (ln) += 8, read64le((ln) - 8) : 0)
688 #define dwarf_ignore_type(ln, end) /* timestamp/size/md5/... */ \
689 switch (entry_format[j].form) { \
690 case DW_FORM_data1: (ln) += 1; break; \
691 case DW_FORM_data2: (ln) += 2; break; \
692 case DW_FORM_data4: (ln) += 3; break; \
693 case DW_FORM_data8: (ln) += 8; break; \
694 case DW_FORM_data16: (ln) += 16; break; \
695 case DW_FORM_udata: dwarf_read_uleb128(&(ln), (end)); break; \
696 default: goto next_line; \
699 static unsigned long long
700 dwarf_read_uleb128(unsigned char **ln, unsigned char *end)
702 unsigned char *cp = *ln;
703 unsigned long long retval = 0;
704 int i;
706 for (i = 0; i < MAX_128; i++) {
707 unsigned long long byte = dwarf_read_1(cp, end);
709 retval |= (byte & 0x7f) << (i * 7);
710 if ((byte & 0x80) == 0)
711 break;
713 *ln = cp;
714 return retval;
717 static long long
718 dwarf_read_sleb128(unsigned char **ln, unsigned char *end)
720 unsigned char *cp = *ln;
721 long long retval = 0;
722 int i;
724 for (i = 0; i < MAX_128; i++) {
725 unsigned long long byte = dwarf_read_1(cp, end);
727 retval |= (byte & 0x7f) << (i * 7);
728 if ((byte & 0x80) == 0) {
729 if ((byte & 0x40) && (i + 1) * 7 < 64)
730 retval |= -1LL << ((i + 1) * 7);
731 break;
734 *ln = cp;
735 return retval;
738 static addr_t rt_printline_dwarf (rt_context *rc, addr_t wanted_pc,
739 const char *msg, const char *skip)
741 unsigned char *ln;
742 unsigned char *cp;
743 unsigned char *end;
744 unsigned char *opcode_length;
745 unsigned long long size;
746 unsigned int length;
747 unsigned char version;
748 unsigned int min_insn_length;
749 unsigned int max_ops_per_insn;
750 int line_base;
751 unsigned int line_range;
752 unsigned int opcode_base;
753 unsigned int opindex;
754 unsigned int col;
755 unsigned int i;
756 unsigned int j;
757 unsigned int len;
758 unsigned long long value;
759 struct {
760 unsigned int type;
761 unsigned int form;
762 } entry_format[256];
763 unsigned int dir_size;
764 #if 0
765 char *dirs[DIR_TABLE_SIZE];
766 #endif
767 unsigned int filename_size;
768 struct dwarf_filename_struct {
769 unsigned int dir_entry;
770 char *name;
771 } filename_table[FILE_TABLE_SIZE];
772 addr_t last_pc;
773 addr_t pc;
774 addr_t func_addr;
775 int line;
776 char *filename;
777 char *function;
779 next:
780 ln = rc->dwarf_line;
781 while (ln < rc->dwarf_line_end) {
782 dir_size = 0;
783 filename_size = 0;
784 last_pc = 0;
785 pc = 0;
786 func_addr = 0;
787 line = 1;
788 filename = NULL;
789 function = NULL;
790 length = 4;
791 size = dwarf_read_4(ln, rc->dwarf_line_end);
792 if (size == 0xffffffffu) // dwarf 64
793 length = 8, size = dwarf_read_8(ln, rc->dwarf_line_end);
794 end = ln + size;
795 if (end < ln || end > rc->dwarf_line_end)
796 break;
797 version = dwarf_read_2(ln, end);
798 if (version >= 5)
799 ln += length + 2; // address size, segment selector, prologue Length
800 else
801 ln += length; // prologue Length
802 min_insn_length = dwarf_read_1(ln, end);
803 if (version >= 4)
804 max_ops_per_insn = dwarf_read_1(ln, end);
805 else
806 max_ops_per_insn = 1;
807 ln++; // Initial value of 'is_stmt'
808 line_base = dwarf_read_1(ln, end);
809 line_base |= line_base >= 0x80 ? ~0xff : 0;
810 line_range = dwarf_read_1(ln, end);
811 opcode_base = dwarf_read_1(ln, end);
812 opcode_length = ln;
813 ln += opcode_base - 1;
814 opindex = 0;
815 if (version >= 5) {
816 col = dwarf_read_1(ln, end);
817 for (i = 0; i < col; i++) {
818 entry_format[i].type = dwarf_read_uleb128(&ln, end);
819 entry_format[i].form = dwarf_read_uleb128(&ln, end);
821 dir_size = dwarf_read_uleb128(&ln, end);
822 for (i = 0; i < dir_size; i++) {
823 for (j = 0; j < col; j++) {
824 if (entry_format[j].type == DW_LNCT_path) {
825 if (entry_format[j].form != DW_FORM_line_strp)
826 goto next_line;
827 #if 0
828 value = length == 4 ? dwarf_read_4(ln, end)
829 : dwarf_read_8(ln, end);
830 if (i < DIR_TABLE_SIZE)
831 dirs[i] = (char *)rc->dwarf_line_str + value;
832 #else
833 length == 4 ? dwarf_read_4(ln, end)
834 : dwarf_read_8(ln, end);
835 #endif
837 else
838 dwarf_ignore_type(ln, end);
841 col = dwarf_read_1(ln, end);
842 for (i = 0; i < col; i++) {
843 entry_format[i].type = dwarf_read_uleb128(&ln, end);
844 entry_format[i].form = dwarf_read_uleb128(&ln, end);
846 filename_size = dwarf_read_uleb128(&ln, end);
847 for (i = 0; i < filename_size; i++)
848 for (j = 0; j < col; j++) {
849 if (entry_format[j].type == DW_LNCT_path) {
850 if (entry_format[j].form != DW_FORM_line_strp)
851 goto next_line;
852 value = length == 4 ? dwarf_read_4(ln, end)
853 : dwarf_read_8(ln, end);
854 if (i < FILE_TABLE_SIZE)
855 filename_table[i].name =
856 (char *)rc->dwarf_line_str + value;
858 else if (entry_format[j].type == DW_LNCT_directory_index) {
859 switch (entry_format[j].form) {
860 case DW_FORM_data1: value = dwarf_read_1(ln, end); break;
861 case DW_FORM_data2: value = dwarf_read_2(ln, end); break;
862 case DW_FORM_data4: value = dwarf_read_4(ln, end); break;
863 case DW_FORM_udata: value = dwarf_read_uleb128(&ln, end); break;
864 default: goto next_line;
866 if (i < FILE_TABLE_SIZE)
867 filename_table[i].dir_entry = value;
869 else
870 dwarf_ignore_type(ln, end);
873 else {
874 while ((dwarf_read_1(ln, end))) {
875 #if 0
876 if (++dir_size < DIR_TABLE_SIZE)
877 dirs[dir_size - 1] = (char *)ln - 1;
878 #endif
879 while (dwarf_read_1(ln, end)) {}
881 while ((dwarf_read_1(ln, end))) {
882 if (++filename_size < FILE_TABLE_SIZE) {
883 filename_table[filename_size - 1].name = (char *)ln - 1;
884 while (dwarf_read_1(ln, end)) {}
885 filename_table[filename_size - 1].dir_entry =
886 dwarf_read_uleb128(&ln, end);
888 else {
889 while (dwarf_read_1(ln, end)) {}
890 dwarf_read_uleb128(&ln, end);
892 dwarf_read_uleb128(&ln, end); // time
893 dwarf_read_uleb128(&ln, end); // size
896 if (filename_size >= 1)
897 filename = filename_table[0].name;
898 while (ln < end) {
899 last_pc = pc;
900 i = dwarf_read_1(ln, end);
901 if (i >= opcode_base) {
902 if (max_ops_per_insn == 1)
903 pc += ((i - opcode_base) / line_range) * min_insn_length;
904 else {
905 pc += (opindex + (i - opcode_base) / line_range) /
906 max_ops_per_insn * min_insn_length;
907 opindex = (opindex + (i - opcode_base) / line_range) %
908 max_ops_per_insn;
910 i = (int)((i - opcode_base) % line_range) + line_base;
911 check_pc:
912 if (pc >= wanted_pc && wanted_pc >= last_pc)
913 goto found;
914 line += i;
916 else {
917 switch (i) {
918 case 0:
919 len = dwarf_read_uleb128(&ln, end);
920 cp = ln;
921 ln += len;
922 if (len == 0)
923 goto next_line;
924 switch (dwarf_read_1(cp, end)) {
925 case DW_LNE_end_sequence:
926 break;
927 case DW_LNE_set_address:
928 #if PTR_SIZE == 4
929 pc = dwarf_read_4(cp, end);
930 #else
931 pc = dwarf_read_8(cp, end);
932 #endif
933 #if defined TCC_TARGET_MACHO
934 if (rc->prog_base != (addr_t) -1)
935 pc += rc->prog_base;
936 #endif
937 opindex = 0;
938 break;
939 case DW_LNE_define_file: /* deprecated */
940 if (++filename_size < FILE_TABLE_SIZE) {
941 filename_table[filename_size - 1].name = (char *)ln - 1;
942 while (dwarf_read_1(ln, end)) {}
943 filename_table[filename_size - 1].dir_entry =
944 dwarf_read_uleb128(&ln, end);
946 else {
947 while (dwarf_read_1(ln, end)) {}
948 dwarf_read_uleb128(&ln, end);
950 dwarf_read_uleb128(&ln, end); // time
951 dwarf_read_uleb128(&ln, end); // size
952 break;
953 case DW_LNE_hi_user - 1:
954 function = (char *)cp;
955 func_addr = pc;
956 break;
957 default:
958 break;
960 break;
961 case DW_LNS_advance_pc:
962 if (max_ops_per_insn == 1)
963 pc += dwarf_read_uleb128(&ln, end) * min_insn_length;
964 else {
965 unsigned long long off = dwarf_read_uleb128(&ln, end);
967 pc += (opindex + off) / max_ops_per_insn *
968 min_insn_length;
969 opindex = (opindex + off) % max_ops_per_insn;
971 i = 0;
972 goto check_pc;
973 case DW_LNS_advance_line:
974 line += dwarf_read_sleb128(&ln, end);
975 break;
976 case DW_LNS_set_file:
977 i = dwarf_read_uleb128(&ln, end);
978 i -= i > 0 && version < 5;
979 if (i < FILE_TABLE_SIZE && i < filename_size)
980 filename = filename_table[i].name;
981 break;
982 case DW_LNS_const_add_pc:
983 if (max_ops_per_insn == 1)
984 pc += ((255 - opcode_base) / line_range) * min_insn_length;
985 else {
986 unsigned int off = (255 - opcode_base) / line_range;
988 pc += ((opindex + off) / max_ops_per_insn) *
989 min_insn_length;
990 opindex = (opindex + off) % max_ops_per_insn;
992 i = 0;
993 goto check_pc;
994 case DW_LNS_fixed_advance_pc:
995 i = dwarf_read_2(ln, end);
996 pc += i;
997 opindex = 0;
998 i = 0;
999 goto check_pc;
1000 default:
1001 for (j = 0; j < opcode_length[i - 1]; j++)
1002 dwarf_read_uleb128 (&ln, end);
1003 break;
1007 next_line:
1008 ln = end;
1011 filename = NULL;
1012 func_addr = 0;
1013 /* we try symtab symbols (no line number info) */
1014 function = rt_elfsym(rc, wanted_pc, &func_addr);
1015 if (function)
1016 goto found;
1017 if ((rc = rc->next))
1018 goto next;
1019 found:
1020 if (filename) {
1021 if (skip[0] && strstr(filename, skip))
1022 return (addr_t)-1;
1023 rt_printf("%s:%d: ", filename, line);
1025 else
1026 rt_printf("0x%08llx : ", (long long)wanted_pc);
1027 rt_printf("%s %s", msg, function ? function : "???");
1028 return (addr_t)func_addr;
1030 /* ------------------------------------------------------------- */
1032 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level);
1034 static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap)
1036 rt_context *rc = &g_rtctxt;
1037 addr_t pc = 0;
1038 char skip[100];
1039 int i, level, ret, n;
1040 const char *a, *b, *msg;
1042 if (fp) {
1043 /* we're called from tcc_backtrace. */
1044 rc->fp = (addr_t)fp;
1045 rc->ip = (addr_t)ip;
1046 msg = "";
1047 } else {
1048 /* we're called from signal/exception handler */
1049 msg = "RUNTIME ERROR: ";
1052 skip[0] = 0;
1053 /* If fmt is like "^file.c^..." then skip calls from 'file.c' */
1054 if (fmt[0] == '^' && (b = strchr(a = fmt + 1, fmt[0]))) {
1055 memcpy(skip, a, b - a), skip[b - a] = 0;
1056 fmt = b + 1;
1059 n = rc->num_callers ? rc->num_callers : 6;
1060 for (i = level = 0; level < n; i++) {
1061 ret = rt_get_caller_pc(&pc, rc, i);
1062 a = "%s";
1063 if (ret != -1) {
1064 if (rc->dwarf)
1065 pc = rt_printline_dwarf(rc, pc, level ? "by" : "at", skip);
1066 else
1067 pc = rt_printline(rc, pc, level ? "by" : "at", skip);
1068 if (pc == (addr_t)-1)
1069 continue;
1070 a = ": %s";
1072 if (level == 0) {
1073 rt_printf(a, msg);
1074 rt_vprintf(fmt, ap);
1075 } else if (ret == -1)
1076 break;
1077 rt_printf("\n");
1078 if (ret == -1 || (pc == (addr_t)rc->top_func && pc))
1079 break;
1080 ++level;
1083 rc->ip = rc->fp = 0;
1084 return 0;
1087 /* emit a run time error at position 'pc' */
1088 static int rt_error(const char *fmt, ...)
1090 va_list ap;
1091 int ret;
1092 va_start(ap, fmt);
1093 ret = _rt_error(0, 0, fmt, ap);
1094 va_end(ap);
1095 return ret;
1098 static void rt_exit(int code)
1100 rt_context *rc = &g_rtctxt;
1101 if (rc->do_jmp)
1102 longjmp(rc->jmp_buf, code ? code : 256);
1103 exit(code);
1106 /* ------------------------------------------------------------- */
1108 #ifndef _WIN32
1109 # include <signal.h>
1110 # ifndef __OpenBSD__
1111 # include <sys/ucontext.h>
1112 # endif
1113 #else
1114 # define ucontext_t CONTEXT
1115 #endif
1117 /* translate from ucontext_t* to internal rt_context * */
1118 static void rt_getcontext(ucontext_t *uc, rt_context *rc)
1120 #if defined _WIN64
1121 rc->ip = uc->Rip;
1122 rc->fp = uc->Rbp;
1123 rc->sp = uc->Rsp;
1124 #elif defined _WIN32
1125 rc->ip = uc->Eip;
1126 rc->fp = uc->Ebp;
1127 rc->sp = uc->Esp;
1128 #elif defined __i386__
1129 # if defined(__APPLE__)
1130 rc->ip = uc->uc_mcontext->__ss.__eip;
1131 rc->fp = uc->uc_mcontext->__ss.__ebp;
1132 # elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1133 rc->ip = uc->uc_mcontext.mc_eip;
1134 rc->fp = uc->uc_mcontext.mc_ebp;
1135 # elif defined(__dietlibc__)
1136 rc->ip = uc->uc_mcontext.eip;
1137 rc->fp = uc->uc_mcontext.ebp;
1138 # elif defined(__NetBSD__)
1139 rc->ip = uc->uc_mcontext.__gregs[_REG_EIP];
1140 rc->fp = uc->uc_mcontext.__gregs[_REG_EBP];
1141 # elif defined(__OpenBSD__)
1142 rc->ip = uc->sc_eip;
1143 rc->fp = uc->sc_ebp;
1144 # elif !defined REG_EIP && defined EIP /* fix for glibc 2.1 */
1145 rc->ip = uc->uc_mcontext.gregs[EIP];
1146 rc->fp = uc->uc_mcontext.gregs[EBP];
1147 # else
1148 rc->ip = uc->uc_mcontext.gregs[REG_EIP];
1149 rc->fp = uc->uc_mcontext.gregs[REG_EBP];
1150 # endif
1151 #elif defined(__x86_64__)
1152 # if defined(__APPLE__)
1153 rc->ip = uc->uc_mcontext->__ss.__rip;
1154 rc->fp = uc->uc_mcontext->__ss.__rbp;
1155 # elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1156 rc->ip = uc->uc_mcontext.mc_rip;
1157 rc->fp = uc->uc_mcontext.mc_rbp;
1158 # elif defined(__NetBSD__)
1159 rc->ip = uc->uc_mcontext.__gregs[_REG_RIP];
1160 rc->fp = uc->uc_mcontext.__gregs[_REG_RBP];
1161 # elif defined(__OpenBSD__)
1162 rc->ip = uc->sc_rip;
1163 rc->fp = uc->sc_rbp;
1164 # else
1165 rc->ip = uc->uc_mcontext.gregs[REG_RIP];
1166 rc->fp = uc->uc_mcontext.gregs[REG_RBP];
1167 # endif
1168 #elif defined(__arm__) && defined(__NetBSD__)
1169 rc->ip = uc->uc_mcontext.__gregs[_REG_PC];
1170 rc->fp = uc->uc_mcontext.__gregs[_REG_FP];
1171 #elif defined(__arm__) && defined(__OpenBSD__)
1172 rc->ip = uc->sc_pc;
1173 rc->fp = uc->sc_r11;
1174 #elif defined(__arm__) && defined(__FreeBSD__)
1175 rc->ip = uc->uc_mcontext.__gregs[_REG_PC];
1176 rc->fp = uc->uc_mcontext.__gregs[_REG_FP];
1177 #elif defined(__arm__)
1178 rc->ip = uc->uc_mcontext.arm_pc;
1179 rc->fp = uc->uc_mcontext.arm_fp;
1180 #elif defined(__aarch64__) && defined(__APPLE__)
1181 // see:
1182 // /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/mach/arm/_structs.h
1183 rc->ip = uc->uc_mcontext->__ss.__pc;
1184 rc->fp = uc->uc_mcontext->__ss.__fp;
1185 #elif defined(__aarch64__) && defined(__FreeBSD__)
1186 rc->ip = uc->uc_mcontext.mc_gpregs.gp_elr; /* aka REG_PC */
1187 rc->fp = uc->uc_mcontext.mc_gpregs.gp_x[29];
1188 #elif defined(__aarch64__) && defined(__NetBSD__)
1189 rc->ip = uc->uc_mcontext.__gregs[_REG_PC];
1190 rc->fp = uc->uc_mcontext.__gregs[_REG_FP];
1191 #elif defined(__aarch64__) && defined(__OpenBSD__)
1192 rc->ip = uc->sc_elr;
1193 rc->fp = uc->sc_x[29];
1194 #elif defined(__aarch64__)
1195 rc->ip = uc->uc_mcontext.pc;
1196 rc->fp = uc->uc_mcontext.regs[29];
1197 #elif defined(__riscv) && defined(__OpenBSD__)
1198 rc->ip = uc->sc_sepc;
1199 rc->fp = uc->sc_s[0];
1200 #elif defined(__riscv)
1201 rc->ip = uc->uc_mcontext.__gregs[REG_PC];
1202 rc->fp = uc->uc_mcontext.__gregs[REG_S0];
1203 #endif
1206 /* ------------------------------------------------------------- */
1207 #ifndef _WIN32
1208 /* signal handler for fatal errors */
1209 static void sig_error(int signum, siginfo_t *siginf, void *puc)
1211 rt_context *rc = &g_rtctxt;
1212 rt_getcontext(puc, rc);
1214 switch(signum) {
1215 case SIGFPE:
1216 switch(siginf->si_code) {
1217 case FPE_INTDIV:
1218 case FPE_FLTDIV:
1219 rt_error("division by zero");
1220 break;
1221 default:
1222 rt_error("floating point exception");
1223 break;
1225 break;
1226 case SIGBUS:
1227 case SIGSEGV:
1228 rt_error("invalid memory access");
1229 break;
1230 case SIGILL:
1231 rt_error("illegal instruction");
1232 break;
1233 case SIGABRT:
1234 rt_error("abort() called");
1235 break;
1236 default:
1237 rt_error("caught signal %d", signum);
1238 break;
1240 rt_exit(255);
1243 #ifndef SA_SIGINFO
1244 # define SA_SIGINFO 0x00000004u
1245 #endif
1247 /* Generate a stack backtrace when a CPU exception occurs. */
1248 static void set_exception_handler(void)
1250 struct sigaction sigact;
1251 /* install TCC signal handlers to print debug info on fatal
1252 runtime errors */
1253 sigemptyset (&sigact.sa_mask);
1254 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
1255 #if 0//def SIGSTKSZ // this causes signals not to work at all on some (older) linuxes
1256 sigact.sa_flags |= SA_ONSTACK;
1257 #endif
1258 sigact.sa_sigaction = sig_error;
1259 sigemptyset(&sigact.sa_mask);
1260 sigaction(SIGFPE, &sigact, NULL);
1261 sigaction(SIGILL, &sigact, NULL);
1262 sigaction(SIGSEGV, &sigact, NULL);
1263 sigaction(SIGBUS, &sigact, NULL);
1264 sigaction(SIGABRT, &sigact, NULL);
1265 #if 0//def SIGSTKSZ
1266 /* This allows stack overflow to be reported instead of a SEGV */
1268 stack_t ss;
1269 static unsigned char stack[SIGSTKSZ] __attribute__((aligned(16)));
1271 ss.ss_sp = stack;
1272 ss.ss_size = SIGSTKSZ;
1273 ss.ss_flags = 0;
1274 sigaltstack(&ss, NULL);
1276 #endif
1279 #else /* WIN32 */
1281 /* signal handler for fatal errors */
1282 static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
1284 rt_context *rc = &g_rtctxt;
1285 unsigned code;
1286 rt_getcontext(ex_info->ContextRecord, rc);
1288 switch (code = ex_info->ExceptionRecord->ExceptionCode) {
1289 case EXCEPTION_ACCESS_VIOLATION:
1290 rt_error("invalid memory access");
1291 break;
1292 case EXCEPTION_STACK_OVERFLOW:
1293 rt_error("stack overflow");
1294 break;
1295 case EXCEPTION_INT_DIVIDE_BY_ZERO:
1296 rt_error("division by zero");
1297 break;
1298 case EXCEPTION_BREAKPOINT:
1299 case EXCEPTION_SINGLE_STEP:
1300 rc->ip = *(addr_t*)rc->sp;
1301 rt_error("breakpoint/single-step exception:");
1302 return EXCEPTION_CONTINUE_SEARCH;
1303 default:
1304 rt_error("caught exception %08x", code);
1305 break;
1307 if (rc->do_jmp)
1308 rt_exit(255);
1309 return EXCEPTION_EXECUTE_HANDLER;
1312 /* Generate a stack backtrace when a CPU exception occurs. */
1313 static void set_exception_handler(void)
1315 SetUnhandledExceptionFilter(cpu_exception_handler);
1318 #endif
1320 /* ------------------------------------------------------------- */
1321 /* return the PC at frame level 'level'. Return negative if not found */
1322 #if defined(__i386__) || defined(__x86_64__)
1323 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1325 addr_t ip, fp;
1326 if (level == 0) {
1327 ip = rc->ip;
1328 } else {
1329 ip = 0;
1330 fp = rc->fp;
1331 while (--level) {
1332 /* XXX: check address validity with program info */
1333 if (fp <= 0x1000)
1334 break;
1335 fp = ((addr_t *)fp)[0];
1337 if (fp > 0x1000)
1338 ip = ((addr_t *)fp)[1];
1340 if (ip <= 0x1000)
1341 return -1;
1342 *paddr = ip;
1343 return 0;
1346 #elif defined(__arm__)
1347 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1349 /* XXX: only supports linux/bsd */
1350 #if !defined(__linux__) && \
1351 !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
1352 return -1;
1353 #else
1354 if (level == 0) {
1355 *paddr = rc->ip;
1356 } else {
1357 addr_t fp = rc->fp;
1358 while (--level)
1359 fp = ((addr_t *)fp)[0];
1360 *paddr = ((addr_t *)fp)[2];
1362 return 0;
1363 #endif
1366 #elif defined(__aarch64__)
1367 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1369 if (level == 0) {
1370 *paddr = rc->ip;
1371 } else {
1372 addr_t *fp = (addr_t*)rc->fp;
1373 while (--level)
1374 fp = (addr_t *)fp[0];
1375 *paddr = fp[1];
1377 return 0;
1380 #elif defined(__riscv)
1381 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1383 if (level == 0) {
1384 *paddr = rc->ip;
1385 } else {
1386 addr_t *fp = (addr_t*)rc->fp;
1387 while (--level && fp >= (addr_t*)0x1000)
1388 fp = (addr_t *)fp[-2];
1389 if (fp < (addr_t*)0x1000)
1390 return -1;
1391 *paddr = fp[-1];
1393 return 0;
1396 #else
1397 #warning add arch specific rt_get_caller_pc()
1398 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1400 return -1;
1403 #endif
1404 #endif /* CONFIG_TCC_BACKTRACE */
1405 /* ------------------------------------------------------------- */
1406 #ifdef CONFIG_TCC_STATIC
1408 /* dummy function for profiling */
1409 ST_FUNC void *dlopen(const char *filename, int flag)
1411 return NULL;
1414 ST_FUNC void dlclose(void *p)
1418 ST_FUNC const char *dlerror(void)
1420 return "error";
1423 typedef struct TCCSyms {
1424 char *str;
1425 void *ptr;
1426 } TCCSyms;
1429 /* add the symbol you want here if no dynamic linking is done */
1430 static TCCSyms tcc_syms[] = {
1431 #if !defined(CONFIG_TCCBOOT)
1432 #define TCCSYM(a) { #a, &a, },
1433 TCCSYM(printf)
1434 TCCSYM(fprintf)
1435 TCCSYM(fopen)
1436 TCCSYM(fclose)
1437 #undef TCCSYM
1438 #endif
1439 { NULL, NULL },
1442 ST_FUNC void *dlsym(void *handle, const char *symbol)
1444 TCCSyms *p;
1445 p = tcc_syms;
1446 while (p->str != NULL) {
1447 if (!strcmp(p->str, symbol))
1448 return p->ptr;
1449 p++;
1451 return NULL;
1454 #endif /* CONFIG_TCC_STATIC */
1455 #endif /* TCC_IS_NATIVE */
1456 /* ------------------------------------------------------------- */