2009-12-20 00:10:26 +03:00
|
|
|
/*
|
2009-12-20 03:53:49 +03:00
|
|
|
* TCC - Tiny C Compiler - Support for -run switch
|
2009-12-20 00:10:26 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
#include "tcc.h"
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-03-05 23:15:56 +04:00
|
|
|
/* only native compiler supports -run */
|
|
|
|
#ifdef TCC_IS_NATIVE
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
ST_DATA int rt_num_callers = 6;
|
|
|
|
ST_DATA const char **rt_bound_error_msg;
|
|
|
|
ST_DATA void *rt_prog_main;
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define ucontext_t CONTEXT
|
|
|
|
#endif
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
static void set_pages_executable(void *ptr, unsigned long length);
|
|
|
|
static void set_exception_handler(void);
|
2013-02-04 19:08:06 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level);
|
2009-12-20 00:22:43 +03:00
|
|
|
static void rt_error(ucontext_t *uc, const char *fmt, ...);
|
|
|
|
static int tcc_relocate_ex(TCCState *s1, void *ptr);
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-03-03 20:12:06 +04:00
|
|
|
#ifdef _WIN64
|
2011-07-14 21:09:49 +04:00
|
|
|
static void win64_add_function_table(TCCState *s1);
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
/* Do all relocations (needed before using tcc_get_symbol())
|
|
|
|
Returns -1 on error. */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-09-01 13:33:34 +04:00
|
|
|
LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
int ret;
|
2012-09-01 13:33:34 +04:00
|
|
|
|
|
|
|
if (TCC_RELOCATE_AUTO != ptr)
|
|
|
|
return tcc_relocate_ex(s1, ptr);
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
ret = tcc_relocate_ex(s1, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2010-04-24 14:28:54 +04:00
|
|
|
#ifdef HAVE_SELINUX
|
2013-02-12 22:13:28 +04:00
|
|
|
{ /* Use mmap instead of malloc for Selinux. Ref:
|
|
|
|
http://www.gnu.org/s/libc/manual/html_node/File-Size.html */
|
|
|
|
|
|
|
|
char tmpfname[] = "/tmp/.tccrunXXXXXX";
|
|
|
|
int fd = mkstemp (tmpfname);
|
|
|
|
|
|
|
|
s1->mem_size = ret;
|
|
|
|
unlink (tmpfname);
|
|
|
|
ftruncate (fd, s1->mem_size);
|
|
|
|
|
|
|
|
s1->write_mem = mmap (NULL, ret, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if (s1->write_mem == MAP_FAILED)
|
|
|
|
tcc_error("/tmp not writeable");
|
|
|
|
|
|
|
|
s1->runtime_mem = mmap (NULL, ret, PROT_READ|PROT_EXEC,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if (s1->runtime_mem == MAP_FAILED)
|
|
|
|
tcc_error("/tmp not executable");
|
|
|
|
|
|
|
|
ret = tcc_relocate_ex(s1, s1->write_mem);
|
2010-04-24 14:28:54 +04:00
|
|
|
}
|
|
|
|
#else
|
2013-02-12 22:13:28 +04:00
|
|
|
s1->runtime_mem = tcc_malloc(ret);
|
|
|
|
ret = tcc_relocate_ex(s1, s1->runtime_mem);
|
2010-04-24 14:28:54 +04:00
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
return ret;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* launch the compiled program with the given arguments */
|
2011-08-11 18:55:30 +04:00
|
|
|
LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
int (*prog_main)(int, char **);
|
2010-09-15 15:43:40 +04:00
|
|
|
int ret;
|
2010-04-24 14:28:54 +04:00
|
|
|
|
2012-09-01 13:33:34 +04:00
|
|
|
if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
|
2009-12-20 00:22:43 +03:00
|
|
|
return -1;
|
2010-04-24 14:28:54 +04:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
prog_main = tcc_get_symbol_err(s1, "main");
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2010-09-15 15:43:40 +04:00
|
|
|
if (s1->do_debug) {
|
2009-12-20 00:22:43 +03:00
|
|
|
set_exception_handler();
|
2010-09-15 15:43:40 +04:00
|
|
|
rt_prog_main = prog_main;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (s1->do_bounds_check) {
|
|
|
|
void (*bound_init)(void);
|
|
|
|
void (*bound_exit)(void);
|
|
|
|
/* set error function */
|
|
|
|
rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
|
|
|
|
/* XXX: use .init section so that it also work in binary ? */
|
|
|
|
bound_init = tcc_get_symbol_err(s1, "__bound_init");
|
|
|
|
bound_exit = tcc_get_symbol_err(s1, "__bound_exit");
|
|
|
|
bound_init();
|
|
|
|
ret = (*prog_main)(argc, argv);
|
|
|
|
bound_exit();
|
2010-09-15 15:43:40 +04:00
|
|
|
} else
|
2009-12-20 00:40:28 +03:00
|
|
|
#endif
|
2010-09-15 15:43:40 +04:00
|
|
|
ret = (*prog_main)(argc, argv);
|
|
|
|
return ret;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
Section *s;
|
|
|
|
unsigned long offset, length;
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t mem;
|
2009-12-20 00:22:43 +03:00
|
|
|
int i;
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (NULL == ptr) {
|
2009-12-20 00:22:43 +03:00
|
|
|
s1->nb_errors = 0;
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
pe_output_file(s1, NULL);
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
2009-12-20 00:22:43 +03:00
|
|
|
tcc_add_runtime(s1);
|
|
|
|
relocate_common_syms();
|
|
|
|
tcc_add_linker_symbols(s1);
|
|
|
|
build_got_entries(s1);
|
|
|
|
#endif
|
|
|
|
if (s1->nb_errors)
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2013-02-04 19:08:06 +04:00
|
|
|
offset = 0, mem = (addr_t)ptr;
|
2009-12-20 00:22:43 +03:00
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (0 == (s->sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
|
|
|
length = s->data_offset;
|
|
|
|
s->sh_addr = mem ? (mem + offset + 15) & ~15 : 0;
|
|
|
|
offset = (offset + length + 15) & ~15;
|
|
|
|
}
|
|
|
|
offset += 16;
|
|
|
|
|
|
|
|
/* relocate symbols */
|
|
|
|
relocate_syms(s1, 1);
|
|
|
|
if (s1->nb_errors)
|
|
|
|
return -1;
|
|
|
|
|
2013-02-04 19:08:06 +04:00
|
|
|
#ifdef TCC_HAS_RUNTIME_PLTGOT
|
2009-12-20 00:22:43 +03:00
|
|
|
s1->runtime_plt_and_got_offset = 0;
|
|
|
|
s1->runtime_plt_and_got = (char *)(mem + offset);
|
|
|
|
/* double the size of the buffer for got and plt entries
|
|
|
|
XXX: calculate exact size for them? */
|
|
|
|
offset *= 2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (0 == mem)
|
|
|
|
return offset;
|
|
|
|
|
|
|
|
/* relocate each section */
|
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (s->reloc)
|
|
|
|
relocate_section(s1, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (0 == (s->sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
|
|
|
length = s->data_offset;
|
|
|
|
// printf("%-12s %08x %04x\n", s->name, s->sh_addr, length);
|
2013-02-04 19:08:06 +04:00
|
|
|
ptr = (void*)s->sh_addr;
|
2009-12-20 00:22:43 +03:00
|
|
|
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(ptr, length);
|
|
|
|
}
|
|
|
|
|
2013-02-04 19:08:06 +04:00
|
|
|
#ifdef TCC_HAS_RUNTIME_PLTGOT
|
2009-12-20 00:22:43 +03:00
|
|
|
set_pages_executable(s1->runtime_plt_and_got,
|
|
|
|
s1->runtime_plt_and_got_offset);
|
|
|
|
#endif
|
2011-07-14 21:09:49 +04:00
|
|
|
|
2012-03-03 20:12:06 +04:00
|
|
|
#ifdef _WIN64
|
2011-07-14 21:09:49 +04:00
|
|
|
win64_add_function_table(s1);
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
return 0;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
/* allow to run code in memory */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
static void set_pages_executable(void *ptr, unsigned long length)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
unsigned long old_protect;
|
|
|
|
VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
|
|
|
|
#else
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifndef PAGESIZE
|
|
|
|
# define PAGESIZE 4096
|
|
|
|
#endif
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t start, end;
|
|
|
|
start = (addr_t)ptr & ~(PAGESIZE - 1);
|
|
|
|
end = (addr_t)ptr + length;
|
2009-12-20 00:22:43 +03:00
|
|
|
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
|
|
|
|
mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
|
2013-03-19 17:03:15 +04:00
|
|
|
__clear_cache(ptr, ptr + length);
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
ST_FUNC void tcc_set_num_callers(int n)
|
2011-08-11 18:55:30 +04:00
|
|
|
{
|
|
|
|
rt_num_callers = n;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
/* print the position in the source file of PC value 'pc' by reading
|
|
|
|
the stabs debug information */
|
2013-02-04 19:08:06 +04:00
|
|
|
static addr_t rt_printline(addr_t wanted_pc, const char *msg)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
char func_name[128], last_func_name[128];
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t func_addr, last_pc, pc;
|
2009-12-20 00:10:26 +03:00
|
|
|
const char *incl_files[INCLUDE_STACK_SIZE];
|
|
|
|
int incl_index, len, last_line_num, i;
|
|
|
|
const char *str, *p;
|
|
|
|
|
2010-09-15 15:43:09 +04:00
|
|
|
Stab_Sym *stab_sym = NULL, *stab_sym_end, *sym;
|
|
|
|
int stab_len = 0;
|
|
|
|
char *stab_str = NULL;
|
|
|
|
|
|
|
|
if (stab_section) {
|
|
|
|
stab_len = stab_section->data_offset;
|
|
|
|
stab_sym = (Stab_Sym *)stab_section->data;
|
|
|
|
stab_str = stabstr_section->data;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
incl_index = 0;
|
|
|
|
last_func_name[0] = '\0';
|
2013-02-04 19:08:06 +04:00
|
|
|
last_pc = (addr_t)-1;
|
2009-12-20 00:10:26 +03:00
|
|
|
last_line_num = 1;
|
2010-09-15 15:43:09 +04:00
|
|
|
|
|
|
|
if (!stab_sym)
|
|
|
|
goto no_stabs;
|
|
|
|
|
|
|
|
stab_sym_end = (Stab_Sym*)((char*)stab_sym + stab_len);
|
|
|
|
for (sym = stab_sym + 1; sym < stab_sym_end; ++sym) {
|
2009-12-20 00:10:26 +03:00
|
|
|
switch(sym->n_type) {
|
|
|
|
/* function start or end */
|
|
|
|
case N_FUN:
|
|
|
|
if (sym->n_strx == 0) {
|
|
|
|
/* we test if between last line and end of function */
|
|
|
|
pc = sym->n_value + func_addr;
|
|
|
|
if (wanted_pc >= last_pc && wanted_pc < pc)
|
|
|
|
goto found;
|
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
} else {
|
2010-09-15 15:43:09 +04:00
|
|
|
str = stab_str + sym->n_strx;
|
2009-12-20 00:10:26 +03:00
|
|
|
p = strchr(str, ':');
|
|
|
|
if (!p) {
|
|
|
|
pstrcpy(func_name, sizeof(func_name), str);
|
|
|
|
} else {
|
|
|
|
len = p - str;
|
|
|
|
if (len > sizeof(func_name) - 1)
|
|
|
|
len = sizeof(func_name) - 1;
|
|
|
|
memcpy(func_name, str, len);
|
|
|
|
func_name[len] = '\0';
|
|
|
|
}
|
|
|
|
func_addr = sym->n_value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* line number info */
|
|
|
|
case N_SLINE:
|
|
|
|
pc = sym->n_value + func_addr;
|
|
|
|
if (wanted_pc >= last_pc && wanted_pc < pc)
|
|
|
|
goto found;
|
|
|
|
last_pc = pc;
|
|
|
|
last_line_num = sym->n_desc;
|
|
|
|
/* XXX: slow! */
|
|
|
|
strcpy(last_func_name, func_name);
|
|
|
|
break;
|
|
|
|
/* include files */
|
|
|
|
case N_BINCL:
|
2010-09-15 15:43:09 +04:00
|
|
|
str = stab_str + sym->n_strx;
|
2009-12-20 00:10:26 +03:00
|
|
|
add_incl:
|
|
|
|
if (incl_index < INCLUDE_STACK_SIZE) {
|
|
|
|
incl_files[incl_index++] = str;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case N_EINCL:
|
|
|
|
if (incl_index > 1)
|
|
|
|
incl_index--;
|
|
|
|
break;
|
|
|
|
case N_SO:
|
|
|
|
if (sym->n_strx == 0) {
|
|
|
|
incl_index = 0; /* end of translation unit */
|
|
|
|
} else {
|
2010-09-15 15:43:09 +04:00
|
|
|
str = stab_str + sym->n_strx;
|
2009-12-20 00:10:26 +03:00
|
|
|
/* do not add path */
|
|
|
|
len = strlen(str);
|
|
|
|
if (len > 0 && str[len - 1] != '/')
|
|
|
|
goto add_incl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 15:43:09 +04:00
|
|
|
no_stabs:
|
2009-12-20 00:10:26 +03:00
|
|
|
/* second pass: we try symtab symbols (no line number info) */
|
|
|
|
incl_index = 0;
|
2010-09-15 15:43:09 +04:00
|
|
|
if (symtab_section)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
ElfW(Sym) *sym, *sym_end;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
|
|
|
|
for(sym = (ElfW(Sym) *)symtab_section->data + 1;
|
|
|
|
sym < sym_end;
|
|
|
|
sym++) {
|
|
|
|
type = ELFW(ST_TYPE)(sym->st_info);
|
2010-10-10 18:23:25 +04:00
|
|
|
if (type == STT_FUNC || type == STT_GNU_IFUNC) {
|
2009-12-20 00:10:26 +03:00
|
|
|
if (wanted_pc >= sym->st_value &&
|
|
|
|
wanted_pc < sym->st_value + sym->st_size) {
|
|
|
|
pstrcpy(last_func_name, sizeof(last_func_name),
|
|
|
|
strtab_section->data + sym->st_name);
|
|
|
|
func_addr = sym->st_value;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* did not find any info: */
|
2010-09-15 15:42:52 +04:00
|
|
|
fprintf(stderr, "%s %p ???\n", msg, (void*)wanted_pc);
|
|
|
|
fflush(stderr);
|
2009-12-20 00:10:26 +03:00
|
|
|
return 0;
|
|
|
|
found:
|
2010-09-15 15:42:52 +04:00
|
|
|
i = incl_index;
|
|
|
|
if (i > 0)
|
|
|
|
fprintf(stderr, "%s:%d: ", incl_files[--i], last_line_num);
|
|
|
|
fprintf(stderr, "%s %p", msg, (void*)wanted_pc);
|
|
|
|
if (last_func_name[0] != '\0')
|
2009-12-20 00:10:26 +03:00
|
|
|
fprintf(stderr, " %s()", last_func_name);
|
2010-09-15 15:42:52 +04:00
|
|
|
if (--i >= 0) {
|
|
|
|
fprintf(stderr, " (included from ");
|
|
|
|
for (;;) {
|
|
|
|
fprintf(stderr, "%s", incl_files[i]);
|
|
|
|
if (--i < 0)
|
|
|
|
break;
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
fprintf(stderr, ")");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2010-09-15 15:42:52 +04:00
|
|
|
fflush(stderr);
|
2009-12-20 00:10:26 +03:00
|
|
|
return func_addr;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* emit a run time error at position 'pc' */
|
|
|
|
static void rt_error(ucontext_t *uc, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t pc;
|
2009-12-20 00:22:43 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stderr, "Runtime error: ");
|
2010-09-15 15:43:40 +04:00
|
|
|
va_start(ap, fmt);
|
2009-12-20 00:22:43 +03:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2010-09-15 15:43:40 +04:00
|
|
|
va_end(ap);
|
2009-12-20 00:22:43 +03:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
for(i=0;i<rt_num_callers;i++) {
|
2009-12-20 00:22:43 +03:00
|
|
|
if (rt_get_caller_pc(&pc, uc, i) < 0)
|
|
|
|
break;
|
2010-09-15 15:42:52 +04:00
|
|
|
pc = rt_printline(pc, i ? "by" : "at");
|
2013-02-04 19:08:06 +04:00
|
|
|
if (pc == (addr_t)rt_prog_main && pc)
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
/* signal handler for fatal errors */
|
|
|
|
static void sig_error(int signum, siginfo_t *siginf, void *puc)
|
|
|
|
{
|
|
|
|
ucontext_t *uc = puc;
|
|
|
|
|
|
|
|
switch(signum) {
|
|
|
|
case SIGFPE:
|
|
|
|
switch(siginf->si_code) {
|
|
|
|
case FPE_INTDIV:
|
|
|
|
case FPE_FLTDIV:
|
|
|
|
rt_error(uc, "division by zero");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_error(uc, "floating point exception");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGBUS:
|
|
|
|
case SIGSEGV:
|
|
|
|
if (rt_bound_error_msg && *rt_bound_error_msg)
|
|
|
|
rt_error(uc, *rt_bound_error_msg);
|
|
|
|
else
|
|
|
|
rt_error(uc, "dereferencing invalid pointer");
|
|
|
|
break;
|
|
|
|
case SIGILL:
|
|
|
|
rt_error(uc, "illegal instruction");
|
|
|
|
break;
|
|
|
|
case SIGABRT:
|
|
|
|
rt_error(uc, "abort() called");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_error(uc, "caught signal %d", signum);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifndef SA_SIGINFO
|
|
|
|
# define SA_SIGINFO 0x00000004u
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* Generate a stack backtrace when a CPU exception occurs. */
|
|
|
|
static void set_exception_handler(void)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
/* install TCC signal handlers to print debug info on fatal
|
|
|
|
runtime errors */
|
|
|
|
sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
|
|
|
|
sigact.sa_sigaction = sig_error;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigaction(SIGFPE, &sigact, NULL);
|
|
|
|
sigaction(SIGILL, &sigact, NULL);
|
|
|
|
sigaction(SIGSEGV, &sigact, NULL);
|
|
|
|
sigaction(SIGBUS, &sigact, NULL);
|
|
|
|
sigaction(SIGABRT, &sigact, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#ifdef __i386__
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
/* fix for glibc 2.1 */
|
|
|
|
#ifndef REG_EIP
|
|
|
|
#define REG_EIP EIP
|
|
|
|
#define REG_EBP EBP
|
|
|
|
#endif
|
|
|
|
|
2013-02-04 20:24:03 +04:00
|
|
|
/* return the PC at frame level 'level'. Return negative if not found */
|
2013-02-04 19:08:06 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2013-02-04 20:24:03 +04:00
|
|
|
addr_t fp;
|
2009-12-20 00:10:26 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (level == 0) {
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
*paddr = uc->uc_mcontext->__ss.__eip;
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-12-20 00:10:26 +03:00
|
|
|
*paddr = uc->uc_mcontext.mc_eip;
|
|
|
|
#elif defined(__dietlibc__)
|
|
|
|
*paddr = uc->uc_mcontext.eip;
|
|
|
|
#else
|
|
|
|
*paddr = uc->uc_mcontext.gregs[REG_EIP];
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
} else {
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
fp = uc->uc_mcontext->__ss.__ebp;
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-12-20 00:10:26 +03:00
|
|
|
fp = uc->uc_mcontext.mc_ebp;
|
|
|
|
#elif defined(__dietlibc__)
|
|
|
|
fp = uc->uc_mcontext.ebp;
|
|
|
|
#else
|
|
|
|
fp = uc->uc_mcontext.gregs[REG_EBP];
|
|
|
|
#endif
|
|
|
|
for(i=1;i<level;i++) {
|
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000 || fp >= 0xc0000000)
|
|
|
|
return -1;
|
2013-02-04 20:24:03 +04:00
|
|
|
fp = ((addr_t *)fp)[0];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2013-02-04 20:24:03 +04:00
|
|
|
*paddr = ((addr_t *)fp)[1];
|
2009-12-20 00:10:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#elif defined(__x86_64__)
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2013-02-04 20:24:03 +04:00
|
|
|
/* return the PC at frame level 'level'. Return negative if not found */
|
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2013-02-04 20:24:03 +04:00
|
|
|
addr_t fp;
|
2009-12-20 00:10:26 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (level == 0) {
|
|
|
|
/* XXX: only support linux */
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
*paddr = uc->uc_mcontext->__ss.__rip;
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-12-20 00:10:26 +03:00
|
|
|
*paddr = uc->uc_mcontext.mc_rip;
|
|
|
|
#else
|
|
|
|
*paddr = uc->uc_mcontext.gregs[REG_RIP];
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
} else {
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
fp = uc->uc_mcontext->__ss.__rbp;
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-12-20 00:10:26 +03:00
|
|
|
fp = uc->uc_mcontext.mc_rbp;
|
|
|
|
#else
|
|
|
|
fp = uc->uc_mcontext.gregs[REG_RBP];
|
|
|
|
#endif
|
|
|
|
for(i=1;i<level;i++) {
|
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000)
|
|
|
|
return -1;
|
2013-02-04 20:24:03 +04:00
|
|
|
fp = ((addr_t *)fp)[0];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2013-02-04 20:24:03 +04:00
|
|
|
*paddr = ((addr_t *)fp)[1];
|
2009-12-20 00:10:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2010-05-14 16:22:32 +04:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#elif defined(__arm__)
|
|
|
|
|
|
|
|
/* return the PC at frame level 'level'. Return negative if not found */
|
2013-02-04 20:24:03 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2010-05-14 16:22:32 +04:00
|
|
|
{
|
2013-02-04 20:24:03 +04:00
|
|
|
addr_t fp, sp;
|
2010-05-14 16:22:32 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (level == 0) {
|
|
|
|
/* XXX: only supports linux */
|
|
|
|
#if defined(__linux__)
|
|
|
|
*paddr = uc->uc_mcontext.arm_pc;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
#if defined(__linux__)
|
|
|
|
fp = uc->uc_mcontext.arm_fp;
|
|
|
|
sp = uc->uc_mcontext.arm_sp;
|
|
|
|
if (sp < 0x1000)
|
|
|
|
sp = 0x1000;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
/* XXX: specific to tinycc stack frames */
|
|
|
|
if (fp < sp + 12 || fp & 3)
|
|
|
|
return -1;
|
|
|
|
for(i = 1; i < level; i++) {
|
2013-02-04 20:24:03 +04:00
|
|
|
sp = ((addr_t *)fp)[-2];
|
2010-05-14 16:22:32 +04:00
|
|
|
if (sp < fp || sp - fp > 16 || sp & 3)
|
|
|
|
return -1;
|
2013-02-04 20:24:03 +04:00
|
|
|
fp = ((addr_t *)fp)[-3];
|
2010-05-14 16:22:32 +04:00
|
|
|
if (fp <= sp || fp - sp < 12 || fp & 3)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* XXX: check address validity with program info */
|
2013-02-04 20:24:03 +04:00
|
|
|
*paddr = ((addr_t *)fp)[-1];
|
2010-05-14 16:22:32 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
#warning add arch specific rt_get_caller_pc()
|
2013-02-04 20:24:03 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* !__i386__ */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#else /* WIN32 */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
EXCEPTION_RECORD *er = ex_info->ExceptionRecord;
|
2010-09-15 15:43:40 +04:00
|
|
|
CONTEXT *uc = ex_info->ContextRecord;
|
|
|
|
switch (er->ExceptionCode) {
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
if (rt_bound_error_msg && *rt_bound_error_msg)
|
|
|
|
rt_error(uc, *rt_bound_error_msg);
|
|
|
|
else
|
|
|
|
rt_error(uc, "access violation");
|
|
|
|
break;
|
|
|
|
case EXCEPTION_STACK_OVERFLOW:
|
|
|
|
rt_error(uc, "stack overflow");
|
|
|
|
break;
|
|
|
|
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
|
|
|
rt_error(uc, "division by zero");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_error(uc, "exception caught");
|
|
|
|
break;
|
|
|
|
}
|
2013-02-05 17:27:38 +04:00
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* Generate a stack backtrace when a CPU exception occurs. */
|
|
|
|
static void set_exception_handler(void)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
SetUnhandledExceptionFilter(cpu_exception_handler);
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2012-03-03 20:12:06 +04:00
|
|
|
#ifdef _WIN64
|
2011-07-14 21:09:49 +04:00
|
|
|
static void win64_add_function_table(TCCState *s1)
|
|
|
|
{
|
|
|
|
RtlAddFunctionTable(
|
2013-02-04 19:08:06 +04:00
|
|
|
(RUNTIME_FUNCTION*)s1->uw_pdata->sh_addr,
|
2011-07-14 21:09:49 +04:00
|
|
|
s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
|
2013-02-04 19:08:06 +04:00
|
|
|
text_section->sh_addr
|
2011-07-14 21:09:49 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* return the PC at frame level 'level'. Return non zero if not found */
|
2013-02-04 19:08:06 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, CONTEXT *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t fp, pc;
|
2009-12-20 00:10:26 +03:00
|
|
|
int i;
|
2010-09-15 15:43:25 +04:00
|
|
|
#ifdef _WIN64
|
|
|
|
pc = uc->Rip;
|
|
|
|
fp = uc->Rbp;
|
|
|
|
#else
|
|
|
|
pc = uc->Eip;
|
|
|
|
fp = uc->Ebp;
|
|
|
|
#endif
|
|
|
|
if (level > 0) {
|
|
|
|
for(i=1;i<level;i++) {
|
2009-12-20 00:22:43 +03:00
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000 || fp >= 0xc0000000)
|
|
|
|
return -1;
|
2013-02-04 19:08:06 +04:00
|
|
|
fp = ((addr_t*)fp)[0];
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
2013-02-04 19:08:06 +04:00
|
|
|
pc = ((addr_t*)fp)[1];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2010-09-15 15:43:25 +04:00
|
|
|
*paddr = pc;
|
|
|
|
return 0;
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
#endif /* CONFIG_TCC_BACKTRACE */
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#ifdef CONFIG_TCC_STATIC
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* dummy function for profiling */
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void *dlopen(const char *filename, int flag)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
return NULL;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void dlclose(void *p)
|
2009-12-20 00:11:12 +03:00
|
|
|
{
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
|
|
|
|
ST_FUNC const char *dlerror(void)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
return "error";
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
typedef struct TCCSyms {
|
|
|
|
char *str;
|
|
|
|
void *ptr;
|
|
|
|
} TCCSyms;
|
2009-12-20 00:10:26 +03:00
|
|
|
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* add the symbol you want here if no dynamic linking is done */
|
|
|
|
static TCCSyms tcc_syms[] = {
|
|
|
|
#if !defined(CONFIG_TCCBOOT)
|
2013-02-12 22:13:28 +04:00
|
|
|
#define TCCSYM(a) { #a, &a, },
|
2009-12-20 00:22:43 +03:00
|
|
|
TCCSYM(printf)
|
|
|
|
TCCSYM(fprintf)
|
|
|
|
TCCSYM(fopen)
|
|
|
|
TCCSYM(fclose)
|
2013-02-12 22:13:28 +04:00
|
|
|
#undef TCCSYM
|
2009-12-20 00:10:26 +03:00
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
TCCSyms *p;
|
|
|
|
p = tcc_syms;
|
|
|
|
while (p->str != NULL) {
|
|
|
|
if (!strcmp(p->str, symbol))
|
|
|
|
return p->ptr;
|
|
|
|
p++;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
#elif !defined(_WIN32)
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void *resolve_sym(TCCState *s1, const char *sym)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
return dlsym(RTLD_DEFAULT, sym);
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* CONFIG_TCC_STATIC */
|
2013-02-04 19:08:06 +04:00
|
|
|
#endif /* TCC_IS_NATIVE */
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|