/* * TCC - Tiny C Compiler - Support for -run switch * * Copyright (c) 2001-2004 Fabrice Bellard * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tcc.h" /* only native compiler supports -run */#ifdef TCC_IS_NATIVE #ifdef CONFIG_TCC_BACKTRACEtypedef struct rt_context{ /* --> tccelf.c:tcc_add_btstub wants those below in that order: */ Stab_Sym *stab_sym, *stab_sym_end; char *stab_str; ElfW(Sym) *esym_start, *esym_end; char *elf_str; addr_t prog_base; void *bounds_start; struct rt_context *next; /* <-- */ int num_callers; addr_t ip, fp, sp; void *top_func; jmp_buf jmp_buf; char do_jmp;} rt_context; static rt_context g_rtctxt;static void set_exception_handler(void);static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);static void rt_exit(int code);#endif /* CONFIG_TCC_BACKTRACE */ /* defined when included from lib/bt-exe.c */#ifndef CONFIG_TCC_BACKTRACE_ONLY #ifndef _WIN32# include <sys/mman.h>#endif static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length);static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff); #ifdef _WIN64static void *win64_add_function_table(TCCState *s1);static void win64_del_function_table(void *);#endif /* ------------------------------------------------------------- *//* Do all relocations (needed before using tcc_get_symbol()) Returns -1 on error. */ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr){ int size; addr_t ptr_diff = 0; if (TCC_RELOCATE_AUTO != ptr) return tcc_relocate_ex(s1, ptr, 0); size = tcc_relocate_ex(s1, NULL, 0); if (size < 0) return -1; #ifdef HAVE_SELINUX{ /* Using mmap instead of malloc */ void *prx; char tmpfname[] = "/tmp/.tccrunXXXXXX"; int fd = mkstemp(tmpfname); unlink(tmpfname); ftruncate(fd, size); ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); prx = mmap (NULL, size, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED || prx == MAP_FAILED) tcc_error("tccrun: could not map memory"); dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size); dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, prx); ptr_diff = (char*)prx - (char*)ptr; close(fd);}#else ptr = tcc_malloc(size);#endif tcc_relocate_ex(s1, ptr, ptr_diff); /* no more errors expected */ dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr); return 0;} ST_FUNC void tcc_run_free(TCCState *s1){ int i; for (i = 0; i < s1->nb_runtime_mem; ++i) {#ifdef HAVE_SELINUX unsigned size = (unsigned)(addr_t)s1->runtime_mem[i++]; munmap(s1->runtime_mem[i++], size); munmap(s1->runtime_mem[i], size);#else#ifdef _WIN64 win64_del_function_table(*(void**)s1->runtime_mem[i]);#endif tcc_free(s1->runtime_mem[i]);#endif } tcc_free(s1->runtime_mem);} static void run_cdtors(TCCState *s1, const char *start, const char *end, int argc, char **argv, char **envp){ void **a = (void **)get_sym_addr(s1, start, 0, 0); void **b = (void **)get_sym_addr(s1, end, 0, 0); while (a != b) ((void(*)(int, char **, char **))*a++)(argc, argv, envp);} /* launch the compiled program with the given arguments */LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv){ int (*prog_main)(int, char **, char **), ret;#ifdef CONFIG_TCC_BACKTRACE rt_context *rc = &g_rtctxt;#endif# if defined(__APPLE__) char **envp = NULL;#else char **envp = environ;#endif s1->runtime_main = s1->nostdlib ? "_start" : "main"; if ((s1->dflag & 16) && (addr_t)-1 == get_sym_addr(s1, s1->runtime_main, 0, 1)) return 0;#ifdef CONFIG_TCC_BACKTRACE if (s1->do_debug) tcc_add_symbol(s1, "exit", rt_exit);#endif if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0) return -1; prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1); #ifdef CONFIG_TCC_BACKTRACE memset(rc, 0, sizeof *rc); if (s1->do_debug) { void *p; rc->stab_sym = (Stab_Sym *)stab_section->data; rc->stab_sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset); rc->stab_str = (char *)stab_section->link->data; rc->esym_start = (ElfW(Sym) *)(symtab_section->data); rc->esym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset); rc->elf_str = (char *)symtab_section->link->data;#if PTR_SIZE == 8 rc->prog_base = text_section->sh_addr & 0xffffffff00000000ULL;#endif rc->top_func = tcc_get_symbol(s1, "main"); rc->num_callers = s1->rt_num_callers; rc->do_jmp = 1; if ((p = tcc_get_symbol(s1, "__rt_error"))) *(void**)p = _rt_error;#ifdef CONFIG_TCC_BCHECK if (s1->do_bounds_check) { if ((p = tcc_get_symbol(s1, "__bound_init"))) ((void(*)(void*, int))p)(bounds_section->data, 1); }#endif set_exception_handler(); }#endif errno = 0; /* clean errno value */ fflush(stdout); fflush(stderr); /* These aren't C symbols, so don't need leading underscore handling. */ run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);#ifdef CONFIG_TCC_BACKTRACE if (!rc->do_jmp || !(ret = setjmp(rc->jmp_buf)))#endif { ret = prog_main(argc, argv, envp); } run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL); if ((s1->dflag & 16) && ret) fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp); return ret;} #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64/* To avoid that x86 processors would reload cached instructions each time when data is written in the near, we need to make sure that code and data do not share the same 64 byte unit */ #define RUN_SECTION_ALIGNMENT 63#else #define RUN_SECTION_ALIGNMENT 0#endif /* relocate code. Return -1 on error, required size if ptr is NULL, otherwise copy code into buffer passed by the caller */static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff){ Section *s; unsigned offset, length, align, max_align, i, k, f; addr_t mem, addr; if (NULL == ptr) { s1->nb_errors = 0;#ifdef TCC_TARGET_PE pe_output_file(s1, NULL);#else tcc_add_runtime(s1); resolve_common_syms(s1); build_got_entries(s1);#endif if (s1->nb_errors) return -1; } offset = max_align = 0, mem = (addr_t)ptr;#ifdef _WIN64 offset += sizeof (void*); /* space for function_table pointer */#endif for (k = 0; k < 2; ++k) { f = 0, addr = k ? mem : mem + ptr_diff; for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; if (0 == (s->sh_flags & SHF_ALLOC)) continue; if (k != !(s->sh_flags & SHF_EXECINSTR)) continue; align = s->sh_addralign - 1; if (++f == 1 && align < RUN_SECTION_ALIGNMENT) align = RUN_SECTION_ALIGNMENT; if (max_align < align) max_align = align; offset += -(addr + offset) & align; s->sh_addr = mem ? addr + offset : 0; offset += s->data_offset;#if 0 if (mem) printf("%-16s %p len %04x align %2d\n", s->name, (void*)s->sh_addr, (unsigned)s->data_offset, align + 1);#endif } } /* relocate symbols */ relocate_syms(s1, s1->symtab, !(s1->nostdlib)); if (s1->nb_errors) return -1; if (0 == mem) return offset + max_align; #ifdef TCC_TARGET_PE s1->pe_imagebase = mem;#endif /* relocate each section */ for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; if (s->reloc) relocate_section(s1, s); }#if !defined(TCC_TARGET_PE) || defined(TCC_TARGET_MACHO) relocate_plt(s1);#endif for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; if (0 == (s->sh_flags & SHF_ALLOC)) continue; length = s->data_offset; ptr = (void*)s->sh_addr; if (s->sh_flags & SHF_EXECINSTR) ptr = (char*)((addr_t)ptr - ptr_diff); if (NULL == s->data || s->sh_type == SHT_NOBITS) memset(ptr, 0, length); else memcpy(ptr, s->data, length); /* mark executable sections as executable in memory */ if (s->sh_flags & SHF_EXECINSTR) set_pages_executable(s1, (char*)((addr_t)ptr + ptr_diff), length); } #ifdef _WIN64 *(void**)mem = win64_add_function_table(s1);#endif return 0;} /* ------------------------------------------------------------- *//* allow to run code in memory */ static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length){#ifdef _WIN32 unsigned long old_protect; VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);#else void __clear_cache(void *beginning, void *end);# ifndef HAVE_SELINUX addr_t start, end;# ifndef PAGESIZE# define PAGESIZE 4096# endif start = (addr_t)ptr & ~(PAGESIZE - 1); end = (addr_t)ptr + length; end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC)) tcc_error("mprotect failed: did you mean to configure --with-selinux?");# endif# if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 __clear_cache(ptr, (char *)ptr + length);# endif#endif} #ifdef _WIN64static void *win64_add_function_table(TCCState *s1){ void *p = NULL; if (s1->uw_pdata) { p = (void*)s1->uw_pdata->sh_addr; RtlAddFunctionTable( (RUNTIME_FUNCTION*)p, s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION), s1->pe_imagebase ); s1->uw_pdata = NULL; } return p;} static void win64_del_function_table(void *p){ if (p) { RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p); }}#endif#endif //ndef CONFIG_TCC_BACKTRACE_ONLY/* ------------------------------------------------------------- */#ifdef CONFIG_TCC_BACKTRACE static int rt_vprintf(const char *fmt, va_list ap){ int ret = vfprintf(stderr, fmt, ap); fflush(stderr); return ret;} static int rt_printf(const char *fmt, ...){ va_list ap; int r; va_start(ap, fmt); r = rt_vprintf(fmt, ap); va_end(ap); return r;} #define INCLUDE_STACK_SIZE 32 /* print the position in the source file of PC value 'pc' by reading the stabs debug information */static addr_t rt_printline (rt_context *rc, addr_t wanted_pc, const char *msg, const char *skip){ char func_name[128]; addr_t func_addr, last_pc, pc; const char *incl_files[INCLUDE_STACK_SIZE]; int incl_index, last_incl_index, len, last_line_num, i; const char *str, *p; ElfW(Sym) *esym; Stab_Sym *sym; next: func_name[0] = '\0'; func_addr = 0; incl_index = 0; last_pc = (addr_t)-1; last_line_num = 1; last_incl_index = 0; for (sym = rc->stab_sym + 1; sym < rc->stab_sym_end; ++sym) { str = rc->stab_str + sym->n_strx; pc = sym->n_value; switch(sym->n_type) { case N_SLINE: if (func_addr) goto rel_pc; case N_SO: case N_SOL: goto abs_pc; case N_FUN: if (sym->n_strx == 0) /* end of function */ goto rel_pc; abs_pc:#if PTR_SIZE == 8 /* Stab_Sym.n_value is only 32bits */ pc += rc->prog_base;#endif goto check_pc; rel_pc: pc += func_addr; check_pc: if (pc >= wanted_pc && wanted_pc >= last_pc) goto found; break; } switch(sym->n_type) { /* function start or end */ case N_FUN: if (sym->n_strx == 0) goto reset_func; p = strchr(str, ':'); if (0 == p || (len = p - str + 1, len > sizeof func_name)) len = sizeof func_name; pstrcpy(func_name, len, str); func_addr = pc; break; /* line number info */ case N_SLINE: last_pc = pc; last_line_num = sym->n_desc; last_incl_index = incl_index; break; /* include files */ case N_BINCL: if (incl_index < INCLUDE_STACK_SIZE) incl_files[incl_index++] = str; break; case N_EINCL: if (incl_index > 1) incl_index--; break; /* start/end of translation unit */ case N_SO: incl_index = 0; if (sym->n_strx) { /* do not add path */ len = strlen(str); if (len > 0 && str[len - 1] != '/') incl_files[incl_index++] = str; } reset_func: func_name[0] = '\0'; func_addr = 0; last_pc = (addr_t)-1; break; /* alternative file name (from #line or #include directives) */ case N_SOL: if (incl_index) incl_files[incl_index-1] = str; break; } } func_name[0] = '\0'; func_addr = 0; last_incl_index = 0; /* we try symtab symbols (no line number info) */ for (esym = rc->esym_start + 1; esym < rc->esym_end; ++esym) { int type = ELFW(ST_TYPE)(esym->st_info); if (type == STT_FUNC || type == STT_GNU_IFUNC) { if (wanted_pc >= esym->st_value && wanted_pc < esym->st_value + esym->st_size) { pstrcpy(func_name, sizeof(func_name), rc->elf_str + esym->st_name); func_addr = esym->st_value; goto found; } } } if ((rc = rc->next)) goto next; found:Lines 1–500 of 915. Load more
/* * TCC - Tiny C Compiler - Support for -run switch * * Copyright (c) 2001-2004 Fabrice Bellard * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "tcc.h" /* only native compiler supports -run */ #ifdef TCC_IS_NATIVE #ifdef CONFIG_TCC_BACKTRACE typedef struct rt_context { /* --> tccelf.c:tcc_add_btstub wants those below in that order: */ Stab_Sym *stab_sym, *stab_sym_end; char *stab_str; ElfW(Sym) *esym_start, *esym_end; char *elf_str; addr_t prog_base; void *bounds_start; struct rt_context *next; /* <-- */ int num_callers; addr_t ip, fp, sp; void *top_func; jmp_buf jmp_buf; char do_jmp; } rt_context; static rt_context g_rtctxt; static void set_exception_handler(void); static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap); static void rt_exit(int code); #endif /* CONFIG_TCC_BACKTRACE */ /* defined when included from lib/bt-exe.c */ #ifndef CONFIG_TCC_BACKTRACE_ONLY #ifndef _WIN32 # include <sys/mman.h> #endif static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length); static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff); #ifdef _WIN64 static void *win64_add_function_table(TCCState *s1); static void win64_del_function_table(void *); #endif /* ------------------------------------------------------------- */ /* Do all relocations (needed before using tcc_get_symbol()) Returns -1 on error. */ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr) { int size; addr_t ptr_diff = 0; if (TCC_RELOCATE_AUTO != ptr) return tcc_relocate_ex(s1, ptr, 0); size = tcc_relocate_ex(s1, NULL, 0); if (size < 0) return -1; #ifdef HAVE_SELINUX { /* Using mmap instead of malloc */ void *prx; char tmpfname[] = "/tmp/.tccrunXXXXXX"; int fd = mkstemp(tmpfname); unlink(tmpfname); ftruncate(fd, size); ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); prx = mmap (NULL, size, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED || prx == MAP_FAILED) tcc_error("tccrun: could not map memory"); dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size); dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, prx); ptr_diff = (char*)prx - (char*)ptr; close(fd); } #else ptr = tcc_malloc(size); #endif tcc_relocate_ex(s1, ptr, ptr_diff); /* no more errors expected */ dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr); return 0; } ST_FUNC void tcc_run_free(TCCState *s1) { int i; for (i = 0; i < s1->nb_runtime_mem; ++i) { #ifdef HAVE_SELINUX unsigned size = (unsigned)(addr_t)s1->runtime_mem[i++]; munmap(s1->runtime_mem[i++], size); munmap(s1->runtime_mem[i], size); #else #ifdef _WIN64 win64_del_function_table(*(void**)s1->runtime_mem[i]); #endif tcc_free(s1->runtime_mem[i]); #endif } tcc_free(s1->runtime_mem); } static void run_cdtors(TCCState *s1, const char *start, const char *end, int argc, char **argv, char **envp) { void **a = (void **)get_sym_addr(s1, start, 0, 0); void **b = (void **)get_sym_addr(s1, end, 0, 0); while (a != b) ((void(*)(int, char **, char **))*a++)(argc, argv, envp); } /* launch the compiled program with the given arguments */ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) { int (*prog_main)(int, char **, char **), ret; #ifdef CONFIG_TCC_BACKTRACE rt_context *rc = &g_rtctxt; #endif # if defined(__APPLE__) char **envp = NULL; #else char **envp = environ; #endif s1->runtime_main = s1->nostdlib ? "_start" : "main"; if ((s1->dflag & 16) && (addr_t)-1 == get_sym_addr(s1, s1->runtime_main, 0, 1)) return 0; #ifdef CONFIG_TCC_BACKTRACE if (s1->do_debug) tcc_add_symbol(s1, "exit", rt_exit); #endif if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0) return -1; prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1); #ifdef CONFIG_TCC_BACKTRACE memset(rc, 0, sizeof *rc); if (s1->do_debug) { void *p; rc->stab_sym = (Stab_Sym *)stab_section->data; rc->stab_sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset); rc->stab_str = (char *)stab_section->link->data; rc->esym_start = (ElfW(Sym) *)(symtab_section->data); rc->esym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset); rc->elf_str = (char *)symtab_section->link->data; #if PTR_SIZE == 8 rc->prog_base = text_section->sh_addr & 0xffffffff00000000ULL; #endif rc->top_func = tcc_get_symbol(s1, "main"); rc->num_callers = s1->rt_num_callers; rc->do_jmp = 1; if ((p = tcc_get_symbol(s1, "__rt_error"))) *(void**)p = _rt_error; #ifdef CONFIG_TCC_BCHECK if (s1->do_bounds_check) { if ((p = tcc_get_symbol(s1, "__bound_init"))) ((void(*)(void*, int))p)(bounds_section->data, 1); } #endif set_exception_handler(); } #endif errno = 0; /* clean errno value */ fflush(stdout); fflush(stderr); /* These aren't C symbols, so don't need leading underscore handling. */ run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp); #ifdef CONFIG_TCC_BACKTRACE if (!rc->do_jmp || !(ret = setjmp(rc->jmp_buf))) #endif { ret = prog_main(argc, argv, envp); } run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL); if ((s1->dflag & 16) && ret) fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp); return ret; } #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 /* To avoid that x86 processors would reload cached instructions each time when data is written in the near, we need to make sure that code and data do not share the same 64 byte unit */ #define RUN_SECTION_ALIGNMENT 63 #else #define RUN_SECTION_ALIGNMENT 0 #endif /* relocate code. Return -1 on error, required size if ptr is NULL, otherwise copy code into buffer passed by the caller */ static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff) { Section *s; unsigned offset, length, align, max_align, i, k, f; addr_t mem, addr; if (NULL == ptr) { s1->nb_errors = 0; #ifdef TCC_TARGET_PE pe_output_file(s1, NULL); #else tcc_add_runtime(s1); resolve_common_syms(s1); build_got_entries(s1); #endif if (s1->nb_errors) return -1; } offset = max_align = 0, mem = (addr_t)ptr; #ifdef _WIN64 offset += sizeof (void*); /* space for function_table pointer */ #endif for (k = 0; k < 2; ++k) { f = 0, addr = k ? mem : mem + ptr_diff; for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; if (0 == (s->sh_flags & SHF_ALLOC)) continue; if (k != !(s->sh_flags & SHF_EXECINSTR)) continue; align = s->sh_addralign - 1; if (++f == 1 && align < RUN_SECTION_ALIGNMENT) align = RUN_SECTION_ALIGNMENT; if (max_align < align) max_align = align; offset += -(addr + offset) & align; s->sh_addr = mem ? addr + offset : 0; offset += s->data_offset; #if 0 if (mem) printf("%-16s %p len %04x align %2d\n", s->name, (void*)s->sh_addr, (unsigned)s->data_offset, align + 1); #endif } } /* relocate symbols */ relocate_syms(s1, s1->symtab, !(s1->nostdlib)); if (s1->nb_errors) return -1; if (0 == mem) return offset + max_align; #ifdef TCC_TARGET_PE s1->pe_imagebase = mem; #endif /* relocate each section */ for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; if (s->reloc) relocate_section(s1, s); } #if !defined(TCC_TARGET_PE) || defined(TCC_TARGET_MACHO) relocate_plt(s1); #endif for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; if (0 == (s->sh_flags & SHF_ALLOC)) continue; length = s->data_offset; ptr = (void*)s->sh_addr; if (s->sh_flags & SHF_EXECINSTR) ptr = (char*)((addr_t)ptr - ptr_diff); if (NULL == s->data || s->sh_type == SHT_NOBITS) memset(ptr, 0, length); else memcpy(ptr, s->data, length); /* mark executable sections as executable in memory */ if (s->sh_flags & SHF_EXECINSTR) set_pages_executable(s1, (char*)((addr_t)ptr + ptr_diff), length); } #ifdef _WIN64 *(void**)mem = win64_add_function_table(s1); #endif return 0; } /* ------------------------------------------------------------- */ /* allow to run code in memory */ static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length) { #ifdef _WIN32 unsigned long old_protect; VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect); #else void __clear_cache(void *beginning, void *end); # ifndef HAVE_SELINUX addr_t start, end; # ifndef PAGESIZE # define PAGESIZE 4096 # endif start = (addr_t)ptr & ~(PAGESIZE - 1); end = (addr_t)ptr + length; end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC)) tcc_error("mprotect failed: did you mean to configure --with-selinux?"); # endif # if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 __clear_cache(ptr, (char *)ptr + length); # endif #endif } #ifdef _WIN64 static void *win64_add_function_table(TCCState *s1) { void *p = NULL; if (s1->uw_pdata) { p = (void*)s1->uw_pdata->sh_addr; RtlAddFunctionTable( (RUNTIME_FUNCTION*)p, s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION), s1->pe_imagebase ); s1->uw_pdata = NULL; } return p; } static void win64_del_function_table(void *p) { if (p) { RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p); } } #endif #endif //ndef CONFIG_TCC_BACKTRACE_ONLY /* ------------------------------------------------------------- */ #ifdef CONFIG_TCC_BACKTRACE static int rt_vprintf(const char *fmt, va_list ap) { int ret = vfprintf(stderr, fmt, ap); fflush(stderr); return ret; } static int rt_printf(const char *fmt, ...) { va_list ap; int r; va_start(ap, fmt); r = rt_vprintf(fmt, ap); va_end(ap); return r; } #define INCLUDE_STACK_SIZE 32 /* print the position in the source file of PC value 'pc' by reading the stabs debug information */ static addr_t rt_printline (rt_context *rc, addr_t wanted_pc, const char *msg, const char *skip) { char func_name[128]; addr_t func_addr, last_pc, pc; const char *incl_files[INCLUDE_STACK_SIZE]; int incl_index, last_incl_index, len, last_line_num, i; const char *str, *p; ElfW(Sym) *esym; Stab_Sym *sym; next: func_name[0] = '\0'; func_addr = 0; incl_index = 0; last_pc = (addr_t)-1; last_line_num = 1; last_incl_index = 0; for (sym = rc->stab_sym + 1; sym < rc->stab_sym_end; ++sym) { str = rc->stab_str + sym->n_strx; pc = sym->n_value; switch(sym->n_type) { case N_SLINE: if (func_addr) goto rel_pc; case N_SO: case N_SOL: goto abs_pc; case N_FUN: if (sym->n_strx == 0) /* end of function */ goto rel_pc; abs_pc: #if PTR_SIZE == 8 /* Stab_Sym.n_value is only 32bits */ pc += rc->prog_base; #endif goto check_pc; rel_pc: pc += func_addr; check_pc: if (pc >= wanted_pc && wanted_pc >= last_pc) goto found; break; } switch(sym->n_type) { /* function start or end */ case N_FUN: if (sym->n_strx == 0) goto reset_func; p = strchr(str, ':'); if (0 == p || (len = p - str + 1, len > sizeof func_name)) len = sizeof func_name; pstrcpy(func_name, len, str); func_addr = pc; break; /* line number info */ case N_SLINE: last_pc = pc; last_line_num = sym->n_desc; last_incl_index = incl_index; break; /* include files */ case N_BINCL: if (incl_index < INCLUDE_STACK_SIZE) incl_files[incl_index++] = str; break; case N_EINCL: if (incl_index > 1) incl_index--; break; /* start/end of translation unit */ case N_SO: incl_index = 0; if (sym->n_strx) { /* do not add path */ len = strlen(str); if (len > 0 && str[len - 1] != '/') incl_files[incl_index++] = str; } reset_func: func_name[0] = '\0'; func_addr = 0; last_pc = (addr_t)-1; break; /* alternative file name (from #line or #include directives) */ case N_SOL: if (incl_index) incl_files[incl_index-1] = str; break; } } func_name[0] = '\0'; func_addr = 0; last_incl_index = 0; /* we try symtab symbols (no line number info) */ for (esym = rc->esym_start + 1; esym < rc->esym_end; ++esym) { int type = ELFW(ST_TYPE)(esym->st_info); if (type == STT_FUNC || type == STT_GNU_IFUNC) { if (wanted_pc >= esym->st_value && wanted_pc < esym->st_value + esym->st_size) { pstrcpy(func_name, sizeof(func_name), rc->elf_str + esym->st_name); func_addr = esym->st_value; goto found; } } } if ((rc = rc->next)) goto next; found: