riscv64-tok.h: update with more instructions from the spec
[tinycc.git] / tccrun.c
blobd373abca2ebcaa9f605ca75947f6a5b88348a26f
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 int 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 close(fd);
104 if (ptr == MAP_FAILED || prx == MAP_FAILED)
105 return tcc_error_noabort("tccrun: could not map memory");
106 ptr_diff = (char*)prx - (char*)ptr;
107 //printf("map %p %p %p\n", ptr, prx, (void*)ptr_diff);
109 #else
110 ptr = tcc_malloc(size);
111 #endif
112 if (tcc_relocate_ex(s1, ptr, ptr_diff))
113 return -1;
114 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
115 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr);
116 return 0;
119 ST_FUNC void tcc_run_free(TCCState *s1)
121 int i;
123 for (i = 0; i < s1->nb_runtime_mem; i += 2) {
124 unsigned size = (unsigned)(addr_t)s1->runtime_mem[i];
125 void *ptr = s1->runtime_mem[i+1];
126 #ifdef HAVE_SELINUX
127 munmap(ptr, size * 2);
128 #else
129 /* unprotect memory to make it usable for malloc again */
130 set_pages_executable(s1, 2, ptr, size);
131 #ifdef _WIN64
132 win64_del_function_table(*(void**)ptr);
133 #endif
134 tcc_free(ptr);
135 #endif
137 tcc_free(s1->runtime_mem);
140 static void run_cdtors(TCCState *s1, const char *start, const char *end,
141 int argc, char **argv, char **envp)
143 void **a = (void **)get_sym_addr(s1, start, 0, 0);
144 void **b = (void **)get_sym_addr(s1, end, 0, 0);
145 while (a != b)
146 ((void(*)(int, char **, char **))*a++)(argc, argv, envp);
149 #define NR_AT_EXIT 32
151 static struct exit_context {
152 int exit_called;
153 int nr_exit;
154 void (*exitfunc[NR_AT_EXIT])(int, void *);
155 void *exitarg[NR_AT_EXIT];
156 #ifndef CONFIG_TCC_BACKTRACE
157 jmp_buf run_jmp_buf;
158 #endif
159 } g_exit_context;
161 static void init_exit(void)
163 struct exit_context *e = &g_exit_context;
165 e->exit_called = 0;
166 e->nr_exit = 0;
169 static void call_exit(int ret)
171 struct exit_context *e = &g_exit_context;
173 while (e->nr_exit) {
174 e->nr_exit--;
175 e->exitfunc[e->nr_exit](ret, e->exitarg[e->nr_exit]);
179 static int rt_atexit(void (*function)(void))
181 struct exit_context *e = &g_exit_context;
183 if (e->nr_exit < NR_AT_EXIT) {
184 e->exitfunc[e->nr_exit] = (void (*)(int, void *))function;
185 e->exitarg[e->nr_exit++] = NULL;
186 return 0;
188 return 1;
191 static int rt_on_exit(void (*function)(int, void *), void *arg)
193 struct exit_context *e = &g_exit_context;
195 if (e->nr_exit < NR_AT_EXIT) {
196 e->exitfunc[e->nr_exit] = function;
197 e->exitarg[e->nr_exit++] = arg;
198 return 0;
200 return 1;
203 static void run_exit(int code)
205 struct exit_context *e = &g_exit_context;
207 e->exit_called = 1;
208 #ifdef CONFIG_TCC_BACKTRACE
209 longjmp((&g_rtctxt)->jmp_buf, code ? code : 256);
210 #else
211 longjmp(e->run_jmp_buf, code ? code : 256);
212 #endif
215 /* launch the compiled program with the given arguments */
216 LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
218 int (*prog_main)(int, char **, char **), ret;
219 #ifdef CONFIG_TCC_BACKTRACE
220 rt_context *rc = &g_rtctxt;
221 #endif
223 #if defined(__APPLE__) || defined(__FreeBSD__)
224 char **envp = NULL;
225 #elif defined(__OpenBSD__) || defined(__NetBSD__)
226 extern char **environ;
227 char **envp = environ;
228 #else
229 char **envp = environ;
230 #endif
232 s1->runtime_main = s1->nostdlib ? "_start" : "main";
233 if ((s1->dflag & 16) && (addr_t)-1 == get_sym_addr(s1, s1->runtime_main, 0, 1))
234 return 0;
235 tcc_add_symbol(s1, "exit", run_exit);
236 tcc_add_symbol(s1, "atexit", rt_atexit);
237 tcc_add_symbol(s1, "on_exit", rt_on_exit);
238 if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
239 return -1;
240 prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1);
241 if ((addr_t)-1 == (addr_t)prog_main)
242 return -1;
244 #ifdef CONFIG_TCC_BACKTRACE
245 memset(rc, 0, sizeof *rc);
246 if (s1->do_debug) {
247 void *p;
248 if (s1->dwarf) {
249 rc->dwarf_line = dwarf_line_section->data;
250 rc->dwarf_line_end = dwarf_line_section->data + dwarf_line_section->data_offset;
251 if (dwarf_line_str_section)
252 rc->dwarf_line_str = dwarf_line_str_section->data;
254 else
256 rc->stab_sym = (Stab_Sym *)stab_section->data;
257 rc->stab_sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
258 rc->stab_str = (char *)stab_section->link->data;
260 rc->dwarf = s1->dwarf;
261 rc->esym_start = (ElfW(Sym) *)(symtab_section->data);
262 rc->esym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
263 rc->elf_str = (char *)symtab_section->link->data;
264 #if PTR_SIZE == 8
265 rc->prog_base = text_section->sh_addr & 0xffffffff00000000ULL;
266 #if defined TCC_TARGET_MACHO
267 if (s1->dwarf)
268 rc->prog_base = (addr_t) -1;
269 #else
270 #endif
271 #endif
272 rc->top_func = tcc_get_symbol(s1, "main");
273 rc->num_callers = s1->rt_num_callers;
274 rc->do_jmp = 1;
275 if ((p = tcc_get_symbol(s1, "__rt_error")))
276 *(void**)p = _rt_error;
277 #ifdef CONFIG_TCC_BCHECK
278 if (s1->do_bounds_check) {
279 rc->bounds_start = (void*)bounds_section->sh_addr;
280 if ((p = tcc_get_symbol(s1, "__bound_init")))
281 ((void(*)(void*,int))p)(rc->bounds_start, 1);
283 #endif
284 set_exception_handler();
286 #endif
288 errno = 0; /* clean errno value */
289 fflush(stdout);
290 fflush(stderr);
291 init_exit();
292 /* These aren't C symbols, so don't need leading underscore handling. */
293 run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);
294 #ifdef CONFIG_TCC_BACKTRACE
295 if (!(ret = setjmp(rc->jmp_buf)))
296 #else
297 if (!(ret = setjmp((&g_exit_context)->run_jmp_buf)))
298 #endif
300 ret = prog_main(argc, argv, envp);
302 run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);
303 call_exit(ret);
304 if ((s1->dflag & 16) && ret)
305 fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
306 if ((s1->dflag & 16) == 0 && (&g_exit_context)->exit_called)
307 exit(ret);
308 return ret;
311 #define DEBUG_RUNMEN 0
313 /* enable rx/ro/rw permissions */
314 #define CONFIG_RUNMEM_RO 1
316 #if CONFIG_RUNMEM_RO
317 # define PAGE_ALIGN PAGESIZE
318 #elif defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
319 /* To avoid that x86 processors would reload cached instructions
320 each time when data is written in the near, we need to make
321 sure that code and data do not share the same 64 byte unit */
322 # define PAGE_ALIGN 64
323 #else
324 # define PAGE_ALIGN 1
325 #endif
327 /* relocate code. Return -1 on error, required size if ptr is NULL,
328 otherwise copy code into buffer passed by the caller */
329 static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff)
331 Section *s;
332 unsigned offset, length, align, max_align, i, k, f;
333 unsigned n, copy;
334 addr_t mem, addr;
336 if (NULL == ptr) {
337 s1->nb_errors = 0;
338 #ifdef TCC_TARGET_PE
339 pe_output_file(s1, NULL);
340 #else
341 tcc_add_runtime(s1);
342 resolve_common_syms(s1);
343 build_got_entries(s1, 0);
344 #endif
345 if (s1->nb_errors)
346 return -1;
349 offset = max_align = 0, mem = (addr_t)ptr;
350 #ifdef _WIN64
351 offset += sizeof (void*); /* space for function_table pointer */
352 #endif
353 copy = 0;
354 redo:
355 for (k = 0; k < 3; ++k) { /* 0:rx, 1:ro, 2:rw sections */
356 n = 0; addr = 0;
357 for(i = 1; i < s1->nb_sections; i++) {
358 static const char shf[] = {
359 SHF_ALLOC|SHF_EXECINSTR, SHF_ALLOC, SHF_ALLOC|SHF_WRITE
361 s = s1->sections[i];
362 if (shf[k] != (s->sh_flags & (SHF_ALLOC|SHF_WRITE|SHF_EXECINSTR)))
363 continue;
364 length = s->data_offset;
365 if (copy) {
366 if (addr == 0)
367 addr = s->sh_addr;
368 n = (s->sh_addr - addr) + length;
369 ptr = (void*)s->sh_addr;
370 if (k == 0)
371 ptr = (void*)(s->sh_addr - ptr_diff);
372 if (NULL == s->data || s->sh_type == SHT_NOBITS)
373 memset(ptr, 0, length);
374 else
375 memcpy(ptr, s->data, length);
376 #ifdef _WIN64
377 if (s == s1->uw_pdata)
378 *(void**)mem = win64_add_function_table(s1);
379 #endif
380 if (s->data) {
381 tcc_free(s->data);
382 s->data = NULL;
383 s->data_allocated = 0;
385 s->data_offset = 0;
386 continue;
388 align = s->sh_addralign - 1;
389 if (++n == 1 && align < (PAGE_ALIGN - 1))
390 align = (PAGE_ALIGN - 1);
391 if (max_align < align)
392 max_align = align;
393 addr = k ? mem : mem + ptr_diff;
394 offset += -(addr + offset) & align;
395 s->sh_addr = mem ? addr + offset : 0;
396 offset += length;
397 #if DEBUG_RUNMEN
398 if (mem)
399 printf("%d: %-16s %p len %04x align %04x\n",
400 k, s->name, (void*)s->sh_addr, length, align + 1);
401 #endif
403 if (copy) { /* set permissions */
404 if (k == 0 && ptr_diff)
405 continue; /* not with HAVE_SELINUX */
406 f = k;
407 #if !CONFIG_RUNMEM_RO
408 if (f != 0)
409 continue;
410 f = 3; /* change only SHF_EXECINSTR to rwx */
411 #endif
412 #if DEBUG_RUNMEN
413 printf("protect %d %p %04x\n", f, (void*)addr, n);
414 #endif
415 if (n) {
416 if (set_pages_executable(s1, f, (void*)addr, n))
417 return -1;
422 if (copy)
423 return 0;
425 /* relocate symbols */
426 relocate_syms(s1, s1->symtab, !(s1->nostdlib));
427 if (s1->nb_errors)
428 return -1;
429 if (0 == mem)
430 return offset + max_align;
432 #ifdef TCC_TARGET_PE
433 s1->pe_imagebase = mem;
434 #endif
436 /* relocate sections */
437 #ifndef TCC_TARGET_PE
438 relocate_plt(s1);
439 #endif
440 relocate_sections(s1);
441 copy = 1;
442 goto redo;
445 /* ------------------------------------------------------------- */
446 /* allow to run code in memory */
448 static int set_pages_executable(TCCState *s1, int mode, void *ptr, unsigned long length)
450 #ifdef _WIN32
451 static const unsigned char protect[] = {
452 PAGE_EXECUTE_READ,
453 PAGE_READONLY,
454 PAGE_READWRITE,
455 PAGE_EXECUTE_READWRITE
457 DWORD old;
458 if (!VirtualProtect(ptr, length, protect[mode], &old))
459 return -1;
460 return 0;
461 #else
462 static const unsigned char protect[] = {
463 PROT_READ | PROT_EXEC,
464 PROT_READ,
465 PROT_READ | PROT_WRITE,
466 PROT_READ | PROT_WRITE | PROT_EXEC
468 addr_t start, end;
469 start = (addr_t)ptr & ~(PAGESIZE - 1);
470 end = (addr_t)ptr + length;
471 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
472 if (mprotect((void *)start, end - start, protect[mode]))
473 return tcc_error_noabort("mprotect failed: did you mean to configure --with-selinux?");
474 /* XXX: BSD sometimes dump core with bad system call */
475 # if (defined TCC_TARGET_ARM && !TARGETOS_BSD) || defined TCC_TARGET_ARM64
476 if (mode == 0 || mode == 3) {
477 void __clear_cache(void *beginning, void *end);
478 __clear_cache(ptr, (char *)ptr + length);
480 # endif
481 return 0;
482 #endif
485 #ifdef _WIN64
486 static void *win64_add_function_table(TCCState *s1)
488 void *p = NULL;
489 if (s1->uw_pdata) {
490 p = (void*)s1->uw_pdata->sh_addr;
491 RtlAddFunctionTable(
492 (RUNTIME_FUNCTION*)p,
493 s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
494 s1->pe_imagebase
496 s1->uw_pdata = NULL;
498 return p;
501 static void win64_del_function_table(void *p)
503 if (p) {
504 RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p);
507 #endif
508 #endif //ndef CONFIG_TCC_BACKTRACE_ONLY
509 /* ------------------------------------------------------------- */
510 #ifdef CONFIG_TCC_BACKTRACE
512 static int rt_vprintf(const char *fmt, va_list ap)
514 int ret = vfprintf(stderr, fmt, ap);
515 fflush(stderr);
516 return ret;
519 static int rt_printf(const char *fmt, ...)
521 va_list ap;
522 int r;
523 va_start(ap, fmt);
524 r = rt_vprintf(fmt, ap);
525 va_end(ap);
526 return r;
529 static char *rt_elfsym(rt_context *rc, addr_t wanted_pc, addr_t *func_addr)
531 ElfW(Sym) *esym;
532 for (esym = rc->esym_start + 1; esym < rc->esym_end; ++esym) {
533 int type = ELFW(ST_TYPE)(esym->st_info);
534 if ((type == STT_FUNC || type == STT_GNU_IFUNC)
535 && wanted_pc >= esym->st_value
536 && wanted_pc < esym->st_value + esym->st_size) {
537 *func_addr = esym->st_value;
538 return rc->elf_str + esym->st_name;
541 return NULL;
545 /* print the position in the source file of PC value 'pc' by reading
546 the stabs debug information */
547 static addr_t rt_printline (rt_context *rc, addr_t wanted_pc,
548 const char *msg, const char *skip)
550 char func_name[128];
551 addr_t func_addr, last_pc, pc;
552 const char *incl_files[INCLUDE_STACK_SIZE];
553 int incl_index, last_incl_index, len, last_line_num, i;
554 const char *str, *p;
555 Stab_Sym *sym;
557 next:
558 func_name[0] = '\0';
559 func_addr = 0;
560 incl_index = 0;
561 last_pc = (addr_t)-1;
562 last_line_num = 1;
563 last_incl_index = 0;
565 for (sym = rc->stab_sym + 1; sym < rc->stab_sym_end; ++sym) {
566 str = rc->stab_str + sym->n_strx;
567 pc = sym->n_value;
569 switch(sym->n_type) {
570 case N_SLINE:
571 if (func_addr)
572 goto rel_pc;
573 case N_SO:
574 case N_SOL:
575 goto abs_pc;
576 case N_FUN:
577 if (sym->n_strx == 0) /* end of function */
578 goto rel_pc;
579 abs_pc:
580 #if PTR_SIZE == 8
581 /* Stab_Sym.n_value is only 32bits */
582 pc += rc->prog_base;
583 #endif
584 goto check_pc;
585 rel_pc:
586 pc += func_addr;
587 check_pc:
588 if (pc >= wanted_pc && wanted_pc >= last_pc)
589 goto found;
590 break;
593 switch(sym->n_type) {
594 /* function start or end */
595 case N_FUN:
596 if (sym->n_strx == 0)
597 goto reset_func;
598 p = strchr(str, ':');
599 if (0 == p || (len = p - str + 1, len > sizeof func_name))
600 len = sizeof func_name;
601 pstrcpy(func_name, len, str);
602 func_addr = pc;
603 break;
604 /* line number info */
605 case N_SLINE:
606 last_pc = pc;
607 last_line_num = sym->n_desc;
608 last_incl_index = incl_index;
609 break;
610 /* include files */
611 case N_BINCL:
612 if (incl_index < INCLUDE_STACK_SIZE)
613 incl_files[incl_index++] = str;
614 break;
615 case N_EINCL:
616 if (incl_index > 1)
617 incl_index--;
618 break;
619 /* start/end of translation unit */
620 case N_SO:
621 incl_index = 0;
622 if (sym->n_strx) {
623 /* do not add path */
624 len = strlen(str);
625 if (len > 0 && str[len - 1] != '/')
626 incl_files[incl_index++] = str;
628 reset_func:
629 func_name[0] = '\0';
630 func_addr = 0;
631 last_pc = (addr_t)-1;
632 break;
633 /* alternative file name (from #line or #include directives) */
634 case N_SOL:
635 if (incl_index)
636 incl_files[incl_index-1] = str;
637 break;
641 func_name[0] = '\0';
642 func_addr = 0;
643 last_incl_index = 0;
644 /* we try symtab symbols (no line number info) */
645 p = rt_elfsym(rc, wanted_pc, &func_addr);
646 if (p) {
647 pstrcpy(func_name, sizeof func_name, p);
648 goto found;
650 if ((rc = rc->next))
651 goto next;
652 found:
653 i = last_incl_index;
654 if (i > 0) {
655 str = incl_files[--i];
656 if (skip[0] && strstr(str, skip))
657 return (addr_t)-1;
658 rt_printf("%s:%d: ", str, last_line_num);
659 } else
660 rt_printf("%08llx : ", (long long)wanted_pc);
661 rt_printf("%s %s", msg, func_name[0] ? func_name : "???");
662 #if 0
663 if (--i >= 0) {
664 rt_printf(" (included from ");
665 for (;;) {
666 rt_printf("%s", incl_files[i]);
667 if (--i < 0)
668 break;
669 rt_printf(", ");
671 rt_printf(")");
673 #endif
674 return func_addr;
677 /* ------------------------------------------------------------- */
678 /* rt_printline - dwarf version */
680 #define MAX_128 ((8 * sizeof (long long) + 6) / 7)
682 #define DIR_TABLE_SIZE (64)
683 #define FILE_TABLE_SIZE (512)
685 #define dwarf_read_1(ln,end) \
686 ((ln) < (end) ? *(ln)++ : 0)
687 #define dwarf_read_2(ln,end) \
688 ((ln) + 2 < (end) ? (ln) += 2, read16le((ln) - 2) : 0)
689 #define dwarf_read_4(ln,end) \
690 ((ln) + 4 < (end) ? (ln) += 4, read32le((ln) - 4) : 0)
691 #define dwarf_read_8(ln,end) \
692 ((ln) + 8 < (end) ? (ln) += 8, read64le((ln) - 8) : 0)
693 #define dwarf_ignore_type(ln, end) /* timestamp/size/md5/... */ \
694 switch (entry_format[j].form) { \
695 case DW_FORM_data1: (ln) += 1; break; \
696 case DW_FORM_data2: (ln) += 2; break; \
697 case DW_FORM_data4: (ln) += 3; break; \
698 case DW_FORM_data8: (ln) += 8; break; \
699 case DW_FORM_data16: (ln) += 16; break; \
700 case DW_FORM_udata: dwarf_read_uleb128(&(ln), (end)); break; \
701 default: goto next_line; \
704 static unsigned long long
705 dwarf_read_uleb128(unsigned char **ln, unsigned char *end)
707 unsigned char *cp = *ln;
708 unsigned long long retval = 0;
709 int i;
711 for (i = 0; i < MAX_128; i++) {
712 unsigned long long byte = dwarf_read_1(cp, end);
714 retval |= (byte & 0x7f) << (i * 7);
715 if ((byte & 0x80) == 0)
716 break;
718 *ln = cp;
719 return retval;
722 static long long
723 dwarf_read_sleb128(unsigned char **ln, unsigned char *end)
725 unsigned char *cp = *ln;
726 long long retval = 0;
727 int i;
729 for (i = 0; i < MAX_128; i++) {
730 unsigned long long byte = dwarf_read_1(cp, end);
732 retval |= (byte & 0x7f) << (i * 7);
733 if ((byte & 0x80) == 0) {
734 if ((byte & 0x40) && (i + 1) * 7 < 64)
735 retval |= -1LL << ((i + 1) * 7);
736 break;
739 *ln = cp;
740 return retval;
743 static addr_t rt_printline_dwarf (rt_context *rc, addr_t wanted_pc,
744 const char *msg, const char *skip)
746 unsigned char *ln;
747 unsigned char *cp;
748 unsigned char *end;
749 unsigned char *opcode_length;
750 unsigned long long size;
751 unsigned int length;
752 unsigned char version;
753 unsigned int min_insn_length;
754 unsigned int max_ops_per_insn;
755 int line_base;
756 unsigned int line_range;
757 unsigned int opcode_base;
758 unsigned int opindex;
759 unsigned int col;
760 unsigned int i;
761 unsigned int j;
762 unsigned int len;
763 unsigned long long value;
764 struct {
765 unsigned int type;
766 unsigned int form;
767 } entry_format[256];
768 unsigned int dir_size;
769 #if 0
770 char *dirs[DIR_TABLE_SIZE];
771 #endif
772 unsigned int filename_size;
773 struct dwarf_filename_struct {
774 unsigned int dir_entry;
775 char *name;
776 } filename_table[FILE_TABLE_SIZE];
777 addr_t last_pc;
778 addr_t pc;
779 addr_t func_addr;
780 int line;
781 char *filename;
782 char *function;
784 next:
785 ln = rc->dwarf_line;
786 while (ln < rc->dwarf_line_end) {
787 dir_size = 0;
788 filename_size = 0;
789 last_pc = 0;
790 pc = 0;
791 func_addr = 0;
792 line = 1;
793 filename = NULL;
794 function = NULL;
795 length = 4;
796 size = dwarf_read_4(ln, rc->dwarf_line_end);
797 if (size == 0xffffffffu) // dwarf 64
798 length = 8, size = dwarf_read_8(ln, rc->dwarf_line_end);
799 end = ln + size;
800 if (end < ln || end > rc->dwarf_line_end)
801 break;
802 version = dwarf_read_2(ln, end);
803 if (version >= 5)
804 ln += length + 2; // address size, segment selector, prologue Length
805 else
806 ln += length; // prologue Length
807 min_insn_length = dwarf_read_1(ln, end);
808 if (version >= 4)
809 max_ops_per_insn = dwarf_read_1(ln, end);
810 else
811 max_ops_per_insn = 1;
812 ln++; // Initial value of 'is_stmt'
813 line_base = dwarf_read_1(ln, end);
814 line_base |= line_base >= 0x80 ? ~0xff : 0;
815 line_range = dwarf_read_1(ln, end);
816 opcode_base = dwarf_read_1(ln, end);
817 opcode_length = ln;
818 ln += opcode_base - 1;
819 opindex = 0;
820 if (version >= 5) {
821 col = dwarf_read_1(ln, end);
822 for (i = 0; i < col; i++) {
823 entry_format[i].type = dwarf_read_uleb128(&ln, end);
824 entry_format[i].form = dwarf_read_uleb128(&ln, end);
826 dir_size = dwarf_read_uleb128(&ln, end);
827 for (i = 0; i < dir_size; i++) {
828 for (j = 0; j < col; j++) {
829 if (entry_format[j].type == DW_LNCT_path) {
830 if (entry_format[j].form != DW_FORM_line_strp)
831 goto next_line;
832 #if 0
833 value = length == 4 ? dwarf_read_4(ln, end)
834 : dwarf_read_8(ln, end);
835 if (i < DIR_TABLE_SIZE)
836 dirs[i] = (char *)rc->dwarf_line_str + value;
837 #else
838 length == 4 ? dwarf_read_4(ln, end)
839 : dwarf_read_8(ln, end);
840 #endif
842 else
843 dwarf_ignore_type(ln, end);
846 col = dwarf_read_1(ln, end);
847 for (i = 0; i < col; i++) {
848 entry_format[i].type = dwarf_read_uleb128(&ln, end);
849 entry_format[i].form = dwarf_read_uleb128(&ln, end);
851 filename_size = dwarf_read_uleb128(&ln, end);
852 for (i = 0; i < filename_size; i++)
853 for (j = 0; j < col; j++) {
854 if (entry_format[j].type == DW_LNCT_path) {
855 if (entry_format[j].form != DW_FORM_line_strp)
856 goto next_line;
857 value = length == 4 ? dwarf_read_4(ln, end)
858 : dwarf_read_8(ln, end);
859 if (i < FILE_TABLE_SIZE)
860 filename_table[i].name =
861 (char *)rc->dwarf_line_str + value;
863 else if (entry_format[j].type == DW_LNCT_directory_index) {
864 switch (entry_format[j].form) {
865 case DW_FORM_data1: value = dwarf_read_1(ln, end); break;
866 case DW_FORM_data2: value = dwarf_read_2(ln, end); break;
867 case DW_FORM_data4: value = dwarf_read_4(ln, end); break;
868 case DW_FORM_udata: value = dwarf_read_uleb128(&ln, end); break;
869 default: goto next_line;
871 if (i < FILE_TABLE_SIZE)
872 filename_table[i].dir_entry = value;
874 else
875 dwarf_ignore_type(ln, end);
878 else {
879 while ((dwarf_read_1(ln, end))) {
880 #if 0
881 if (++dir_size < DIR_TABLE_SIZE)
882 dirs[dir_size - 1] = (char *)ln - 1;
883 #endif
884 while (dwarf_read_1(ln, end)) {}
886 while ((dwarf_read_1(ln, end))) {
887 if (++filename_size < FILE_TABLE_SIZE) {
888 filename_table[filename_size - 1].name = (char *)ln - 1;
889 while (dwarf_read_1(ln, end)) {}
890 filename_table[filename_size - 1].dir_entry =
891 dwarf_read_uleb128(&ln, end);
893 else {
894 while (dwarf_read_1(ln, end)) {}
895 dwarf_read_uleb128(&ln, end);
897 dwarf_read_uleb128(&ln, end); // time
898 dwarf_read_uleb128(&ln, end); // size
901 if (filename_size >= 1)
902 filename = filename_table[0].name;
903 while (ln < end) {
904 last_pc = pc;
905 i = dwarf_read_1(ln, end);
906 if (i >= opcode_base) {
907 if (max_ops_per_insn == 1)
908 pc += ((i - opcode_base) / line_range) * min_insn_length;
909 else {
910 pc += (opindex + (i - opcode_base) / line_range) /
911 max_ops_per_insn * min_insn_length;
912 opindex = (opindex + (i - opcode_base) / line_range) %
913 max_ops_per_insn;
915 i = (int)((i - opcode_base) % line_range) + line_base;
916 check_pc:
917 if (pc >= wanted_pc && wanted_pc >= last_pc)
918 goto found;
919 line += i;
921 else {
922 switch (i) {
923 case 0:
924 len = dwarf_read_uleb128(&ln, end);
925 cp = ln;
926 ln += len;
927 if (len == 0)
928 goto next_line;
929 switch (dwarf_read_1(cp, end)) {
930 case DW_LNE_end_sequence:
931 break;
932 case DW_LNE_set_address:
933 #if PTR_SIZE == 4
934 pc = dwarf_read_4(cp, end);
935 #else
936 pc = dwarf_read_8(cp, end);
937 #endif
938 #if defined TCC_TARGET_MACHO
939 if (rc->prog_base != (addr_t) -1)
940 pc += rc->prog_base;
941 #endif
942 opindex = 0;
943 break;
944 case DW_LNE_define_file: /* deprecated */
945 if (++filename_size < FILE_TABLE_SIZE) {
946 filename_table[filename_size - 1].name = (char *)ln - 1;
947 while (dwarf_read_1(ln, end)) {}
948 filename_table[filename_size - 1].dir_entry =
949 dwarf_read_uleb128(&ln, end);
951 else {
952 while (dwarf_read_1(ln, end)) {}
953 dwarf_read_uleb128(&ln, end);
955 dwarf_read_uleb128(&ln, end); // time
956 dwarf_read_uleb128(&ln, end); // size
957 break;
958 case DW_LNE_hi_user - 1:
959 function = (char *)cp;
960 func_addr = pc;
961 break;
962 default:
963 break;
965 break;
966 case DW_LNS_advance_pc:
967 if (max_ops_per_insn == 1)
968 pc += dwarf_read_uleb128(&ln, end) * min_insn_length;
969 else {
970 unsigned long long off = dwarf_read_uleb128(&ln, end);
972 pc += (opindex + off) / max_ops_per_insn *
973 min_insn_length;
974 opindex = (opindex + off) % max_ops_per_insn;
976 i = 0;
977 goto check_pc;
978 case DW_LNS_advance_line:
979 line += dwarf_read_sleb128(&ln, end);
980 break;
981 case DW_LNS_set_file:
982 i = dwarf_read_uleb128(&ln, end);
983 i -= i > 0 && version < 5;
984 if (i < FILE_TABLE_SIZE && i < filename_size)
985 filename = filename_table[i].name;
986 break;
987 case DW_LNS_const_add_pc:
988 if (max_ops_per_insn == 1)
989 pc += ((255 - opcode_base) / line_range) * min_insn_length;
990 else {
991 unsigned int off = (255 - opcode_base) / line_range;
993 pc += ((opindex + off) / max_ops_per_insn) *
994 min_insn_length;
995 opindex = (opindex + off) % max_ops_per_insn;
997 i = 0;
998 goto check_pc;
999 case DW_LNS_fixed_advance_pc:
1000 i = dwarf_read_2(ln, end);
1001 pc += i;
1002 opindex = 0;
1003 i = 0;
1004 goto check_pc;
1005 default:
1006 for (j = 0; j < opcode_length[i - 1]; j++)
1007 dwarf_read_uleb128 (&ln, end);
1008 break;
1012 next_line:
1013 ln = end;
1016 filename = NULL;
1017 func_addr = 0;
1018 /* we try symtab symbols (no line number info) */
1019 function = rt_elfsym(rc, wanted_pc, &func_addr);
1020 if (function)
1021 goto found;
1022 if ((rc = rc->next))
1023 goto next;
1024 found:
1025 if (filename) {
1026 if (skip[0] && strstr(filename, skip))
1027 return (addr_t)-1;
1028 rt_printf("%s:%d: ", filename, line);
1030 else
1031 rt_printf("0x%08llx : ", (long long)wanted_pc);
1032 rt_printf("%s %s", msg, function ? function : "???");
1033 return (addr_t)func_addr;
1035 /* ------------------------------------------------------------- */
1037 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level);
1039 static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap)
1041 rt_context *rc = &g_rtctxt;
1042 addr_t pc = 0;
1043 char skip[100];
1044 int i, level, ret, n;
1045 const char *a, *b, *msg;
1047 if (fp) {
1048 /* we're called from tcc_backtrace. */
1049 rc->fp = (addr_t)fp;
1050 rc->ip = (addr_t)ip;
1051 msg = "";
1052 } else {
1053 /* we're called from signal/exception handler */
1054 msg = "RUNTIME ERROR: ";
1057 skip[0] = 0;
1058 /* If fmt is like "^file.c^..." then skip calls from 'file.c' */
1059 if (fmt[0] == '^' && (b = strchr(a = fmt + 1, fmt[0]))) {
1060 memcpy(skip, a, b - a), skip[b - a] = 0;
1061 fmt = b + 1;
1064 n = rc->num_callers ? rc->num_callers : 6;
1065 for (i = level = 0; level < n; i++) {
1066 ret = rt_get_caller_pc(&pc, rc, i);
1067 a = "%s";
1068 if (ret != -1) {
1069 if (rc->dwarf)
1070 pc = rt_printline_dwarf(rc, pc, level ? "by" : "at", skip);
1071 else
1072 pc = rt_printline(rc, pc, level ? "by" : "at", skip);
1073 if (pc == (addr_t)-1)
1074 continue;
1075 a = ": %s";
1077 if (level == 0) {
1078 rt_printf(a, msg);
1079 rt_vprintf(fmt, ap);
1080 } else if (ret == -1)
1081 break;
1082 rt_printf("\n");
1083 if (ret == -1 || (pc == (addr_t)rc->top_func && pc))
1084 break;
1085 ++level;
1088 rc->ip = rc->fp = 0;
1089 return 0;
1092 /* emit a run time error at position 'pc' */
1093 static int rt_error(const char *fmt, ...)
1095 va_list ap;
1096 int ret;
1097 va_start(ap, fmt);
1098 ret = _rt_error(0, 0, fmt, ap);
1099 va_end(ap);
1100 return ret;
1103 static void rt_exit(int code)
1105 rt_context *rc = &g_rtctxt;
1106 if (rc->do_jmp)
1107 longjmp(rc->jmp_buf, code ? code : 256);
1108 exit(code);
1111 /* ------------------------------------------------------------- */
1113 #ifndef _WIN32
1114 # include <signal.h>
1115 # ifndef __OpenBSD__
1116 # include <sys/ucontext.h>
1117 # endif
1118 #else
1119 # define ucontext_t CONTEXT
1120 #endif
1122 /* translate from ucontext_t* to internal rt_context * */
1123 static void rt_getcontext(ucontext_t *uc, rt_context *rc)
1125 #if defined _WIN64
1126 rc->ip = uc->Rip;
1127 rc->fp = uc->Rbp;
1128 rc->sp = uc->Rsp;
1129 #elif defined _WIN32
1130 rc->ip = uc->Eip;
1131 rc->fp = uc->Ebp;
1132 rc->sp = uc->Esp;
1133 #elif defined __i386__
1134 # if defined(__APPLE__)
1135 rc->ip = uc->uc_mcontext->__ss.__eip;
1136 rc->fp = uc->uc_mcontext->__ss.__ebp;
1137 # elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1138 rc->ip = uc->uc_mcontext.mc_eip;
1139 rc->fp = uc->uc_mcontext.mc_ebp;
1140 # elif defined(__dietlibc__)
1141 rc->ip = uc->uc_mcontext.eip;
1142 rc->fp = uc->uc_mcontext.ebp;
1143 # elif defined(__NetBSD__)
1144 rc->ip = uc->uc_mcontext.__gregs[_REG_EIP];
1145 rc->fp = uc->uc_mcontext.__gregs[_REG_EBP];
1146 # elif defined(__OpenBSD__)
1147 rc->ip = uc->sc_eip;
1148 rc->fp = uc->sc_ebp;
1149 # elif !defined REG_EIP && defined EIP /* fix for glibc 2.1 */
1150 rc->ip = uc->uc_mcontext.gregs[EIP];
1151 rc->fp = uc->uc_mcontext.gregs[EBP];
1152 # else
1153 rc->ip = uc->uc_mcontext.gregs[REG_EIP];
1154 rc->fp = uc->uc_mcontext.gregs[REG_EBP];
1155 # endif
1156 #elif defined(__x86_64__)
1157 # if defined(__APPLE__)
1158 rc->ip = uc->uc_mcontext->__ss.__rip;
1159 rc->fp = uc->uc_mcontext->__ss.__rbp;
1160 # elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1161 rc->ip = uc->uc_mcontext.mc_rip;
1162 rc->fp = uc->uc_mcontext.mc_rbp;
1163 # elif defined(__NetBSD__)
1164 rc->ip = uc->uc_mcontext.__gregs[_REG_RIP];
1165 rc->fp = uc->uc_mcontext.__gregs[_REG_RBP];
1166 # elif defined(__OpenBSD__)
1167 rc->ip = uc->sc_rip;
1168 rc->fp = uc->sc_rbp;
1169 # else
1170 rc->ip = uc->uc_mcontext.gregs[REG_RIP];
1171 rc->fp = uc->uc_mcontext.gregs[REG_RBP];
1172 # endif
1173 #elif defined(__arm__) && defined(__NetBSD__)
1174 rc->ip = uc->uc_mcontext.__gregs[_REG_PC];
1175 rc->fp = uc->uc_mcontext.__gregs[_REG_FP];
1176 #elif defined(__arm__) && defined(__OpenBSD__)
1177 rc->ip = uc->sc_pc;
1178 rc->fp = uc->sc_r11;
1179 #elif defined(__arm__) && defined(__FreeBSD__)
1180 rc->ip = uc->uc_mcontext.__gregs[_REG_PC];
1181 rc->fp = uc->uc_mcontext.__gregs[_REG_FP];
1182 #elif defined(__arm__)
1183 rc->ip = uc->uc_mcontext.arm_pc;
1184 rc->fp = uc->uc_mcontext.arm_fp;
1185 #elif defined(__aarch64__) && defined(__APPLE__)
1186 // see:
1187 // /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/mach/arm/_structs.h
1188 rc->ip = uc->uc_mcontext->__ss.__pc;
1189 rc->fp = uc->uc_mcontext->__ss.__fp;
1190 #elif defined(__aarch64__) && defined(__FreeBSD__)
1191 rc->ip = uc->uc_mcontext.mc_gpregs.gp_elr; /* aka REG_PC */
1192 rc->fp = uc->uc_mcontext.mc_gpregs.gp_x[29];
1193 #elif defined(__aarch64__) && defined(__NetBSD__)
1194 rc->ip = uc->uc_mcontext.__gregs[_REG_PC];
1195 rc->fp = uc->uc_mcontext.__gregs[_REG_FP];
1196 #elif defined(__aarch64__) && defined(__OpenBSD__)
1197 rc->ip = uc->sc_elr;
1198 rc->fp = uc->sc_x[29];
1199 #elif defined(__aarch64__)
1200 rc->ip = uc->uc_mcontext.pc;
1201 rc->fp = uc->uc_mcontext.regs[29];
1202 #elif defined(__riscv) && defined(__OpenBSD__)
1203 rc->ip = uc->sc_sepc;
1204 rc->fp = uc->sc_s[0];
1205 #elif defined(__riscv)
1206 rc->ip = uc->uc_mcontext.__gregs[REG_PC];
1207 rc->fp = uc->uc_mcontext.__gregs[REG_S0];
1208 #endif
1211 /* ------------------------------------------------------------- */
1212 #ifndef _WIN32
1213 /* signal handler for fatal errors */
1214 static void sig_error(int signum, siginfo_t *siginf, void *puc)
1216 rt_context *rc = &g_rtctxt;
1217 rt_getcontext(puc, rc);
1219 switch(signum) {
1220 case SIGFPE:
1221 switch(siginf->si_code) {
1222 case FPE_INTDIV:
1223 case FPE_FLTDIV:
1224 rt_error("division by zero");
1225 break;
1226 default:
1227 rt_error("floating point exception");
1228 break;
1230 break;
1231 case SIGBUS:
1232 case SIGSEGV:
1233 rt_error("invalid memory access");
1234 break;
1235 case SIGILL:
1236 rt_error("illegal instruction");
1237 break;
1238 case SIGABRT:
1239 rt_error("abort() called");
1240 break;
1241 default:
1242 rt_error("caught signal %d", signum);
1243 break;
1245 rt_exit(255);
1248 #ifndef SA_SIGINFO
1249 # define SA_SIGINFO 0x00000004u
1250 #endif
1252 /* Generate a stack backtrace when a CPU exception occurs. */
1253 static void set_exception_handler(void)
1255 struct sigaction sigact;
1256 /* install TCC signal handlers to print debug info on fatal
1257 runtime errors */
1258 sigemptyset (&sigact.sa_mask);
1259 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
1260 #if 0//def SIGSTKSZ // this causes signals not to work at all on some (older) linuxes
1261 sigact.sa_flags |= SA_ONSTACK;
1262 #endif
1263 sigact.sa_sigaction = sig_error;
1264 sigemptyset(&sigact.sa_mask);
1265 sigaction(SIGFPE, &sigact, NULL);
1266 sigaction(SIGILL, &sigact, NULL);
1267 sigaction(SIGSEGV, &sigact, NULL);
1268 sigaction(SIGBUS, &sigact, NULL);
1269 sigaction(SIGABRT, &sigact, NULL);
1270 #if 0//def SIGSTKSZ
1271 /* This allows stack overflow to be reported instead of a SEGV */
1273 stack_t ss;
1274 static unsigned char stack[SIGSTKSZ] __attribute__((aligned(16)));
1276 ss.ss_sp = stack;
1277 ss.ss_size = SIGSTKSZ;
1278 ss.ss_flags = 0;
1279 sigaltstack(&ss, NULL);
1281 #endif
1284 #else /* WIN32 */
1286 /* signal handler for fatal errors */
1287 static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
1289 rt_context *rc = &g_rtctxt;
1290 unsigned code;
1291 rt_getcontext(ex_info->ContextRecord, rc);
1293 switch (code = ex_info->ExceptionRecord->ExceptionCode) {
1294 case EXCEPTION_ACCESS_VIOLATION:
1295 rt_error("invalid memory access");
1296 break;
1297 case EXCEPTION_STACK_OVERFLOW:
1298 rt_error("stack overflow");
1299 break;
1300 case EXCEPTION_INT_DIVIDE_BY_ZERO:
1301 rt_error("division by zero");
1302 break;
1303 case EXCEPTION_BREAKPOINT:
1304 case EXCEPTION_SINGLE_STEP:
1305 rc->ip = *(addr_t*)rc->sp;
1306 rt_error("breakpoint/single-step exception:");
1307 return EXCEPTION_CONTINUE_SEARCH;
1308 default:
1309 rt_error("caught exception %08x", code);
1310 break;
1312 if (rc->do_jmp)
1313 rt_exit(255);
1314 return EXCEPTION_EXECUTE_HANDLER;
1317 /* Generate a stack backtrace when a CPU exception occurs. */
1318 static void set_exception_handler(void)
1320 SetUnhandledExceptionFilter(cpu_exception_handler);
1323 #endif
1325 /* ------------------------------------------------------------- */
1326 /* return the PC at frame level 'level'. Return negative if not found */
1327 #if defined(__i386__) || defined(__x86_64__)
1328 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1330 addr_t ip, fp;
1331 if (level == 0) {
1332 ip = rc->ip;
1333 } else {
1334 ip = 0;
1335 fp = rc->fp;
1336 while (--level) {
1337 /* XXX: check address validity with program info */
1338 if (fp <= 0x1000)
1339 break;
1340 fp = ((addr_t *)fp)[0];
1342 if (fp > 0x1000)
1343 ip = ((addr_t *)fp)[1];
1345 if (ip <= 0x1000)
1346 return -1;
1347 *paddr = ip;
1348 return 0;
1351 #elif defined(__arm__)
1352 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1354 /* XXX: only supports linux/bsd */
1355 #if !defined(__linux__) && \
1356 !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
1357 return -1;
1358 #else
1359 if (level == 0) {
1360 *paddr = rc->ip;
1361 } else {
1362 addr_t fp = rc->fp;
1363 while (--level)
1364 fp = ((addr_t *)fp)[0];
1365 *paddr = ((addr_t *)fp)[2];
1367 return 0;
1368 #endif
1371 #elif defined(__aarch64__)
1372 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1374 if (level == 0) {
1375 *paddr = rc->ip;
1376 } else {
1377 addr_t *fp = (addr_t*)rc->fp;
1378 while (--level)
1379 fp = (addr_t *)fp[0];
1380 *paddr = fp[1];
1382 return 0;
1385 #elif defined(__riscv)
1386 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1388 if (level == 0) {
1389 *paddr = rc->ip;
1390 } else {
1391 addr_t *fp = (addr_t*)rc->fp;
1392 while (--level && fp >= (addr_t*)0x1000)
1393 fp = (addr_t *)fp[-2];
1394 if (fp < (addr_t*)0x1000)
1395 return -1;
1396 *paddr = fp[-1];
1398 return 0;
1401 #else
1402 #warning add arch specific rt_get_caller_pc()
1403 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
1405 return -1;
1408 #endif
1409 #endif /* CONFIG_TCC_BACKTRACE */
1410 /* ------------------------------------------------------------- */
1411 #ifdef CONFIG_TCC_STATIC
1413 /* dummy function for profiling */
1414 ST_FUNC void *dlopen(const char *filename, int flag)
1416 return NULL;
1419 ST_FUNC void dlclose(void *p)
1423 ST_FUNC const char *dlerror(void)
1425 return "error";
1428 typedef struct TCCSyms {
1429 char *str;
1430 void *ptr;
1431 } TCCSyms;
1434 /* add the symbol you want here if no dynamic linking is done */
1435 static TCCSyms tcc_syms[] = {
1436 #if !defined(CONFIG_TCCBOOT)
1437 #define TCCSYM(a) { #a, &a, },
1438 TCCSYM(printf)
1439 TCCSYM(fprintf)
1440 TCCSYM(fopen)
1441 TCCSYM(fclose)
1442 #undef TCCSYM
1443 #endif
1444 { NULL, NULL },
1447 ST_FUNC void *dlsym(void *handle, const char *symbol)
1449 TCCSyms *p;
1450 p = tcc_syms;
1451 while (p->str != NULL) {
1452 if (!strcmp(p->str, symbol))
1453 return p->ptr;
1454 p++;
1456 return NULL;
1459 #endif /* CONFIG_TCC_STATIC */
1460 #endif /* TCC_IS_NATIVE */
1461 /* ------------------------------------------------------------- */