| @@ -20,75 +20,167 @@ · 19 unmodified lines |
| 20 | 20 | |
| 21 | 21 | /* tccrun.c - support for tcc -run */ |
| 22 | 22 | |
| 23 | +#ifdef _WIN32 |
| 24 | +#define ucontext_t CONTEXT |
| 25 | +#endif |
| 23 | 26 | |
| 24 | | −/********************************************************/ |
| 25 | | −#ifdef CONFIG_TCC_STATIC |
| 27 | +static void set_pages_executable(void *ptr, unsigned long length); |
| 28 | +static void set_exception_handler(void); |
| 29 | +static int rt_get_caller_pc(unsigned long *paddr, ucontext_t *uc, int level); |
| 30 | +static void rt_error(ucontext_t *uc, const char *fmt, ...); |
| 31 | +static int tcc_relocate_ex(TCCState *s1, void *ptr); |
| 26 | 32 | |
| 27 | | −#define RTLD_LAZY 0x001 |
| 28 | | −#define RTLD_NOW 0x002 |
| 29 | | −#define RTLD_GLOBAL 0x100 |
| 30 | | −#define RTLD_DEFAULT NULL |
| 33 | +/* ------------------------------------------------------------- */ |
| 34 | +/* Do all relocations (needed before using tcc_get_symbol()) |
| 35 | + Returns -1 on error. */ |
| 31 | 36 | |
| 32 | | −/* dummy function for profiling */ |
| 33 | | −void *dlopen(const char *filename, int flag) |
| 37 | +int tcc_relocate(TCCState *s1) |
| 34 | 38 | { |
| 35 | | − return NULL; |
| 36 | | −} |
| 39 | + int ret; |
| 37 | 40 | |
| 38 | | −void dlclose(void *p) |
| 39 | | −{ |
| 41 | + ret = tcc_relocate_ex(s1, NULL); |
| 42 | + if (-1 != ret) { |
| 43 | + s1->runtime_mem = tcc_malloc(ret); |
| 44 | + ret = tcc_relocate_ex(s1, s1->runtime_mem); |
| 45 | + } |
| 46 | + return ret; |
| 40 | 47 | } |
| 41 | 48 | |
| 42 | | −const char *dlerror(void) |
| 49 | +/* launch the compiled program with the given arguments */ |
| 50 | +int tcc_run(TCCState *s1, int argc, char **argv) |
| 43 | 51 | { |
| 44 | | − return "error"; |
| 45 | | −} |
| 52 | + int (*prog_main)(int, char **); |
| 46 | 53 | |
| 47 | | −typedef struct TCCSyms { |
| 48 | | − char *str; |
| 49 | | − void *ptr; |
| 50 | | −} TCCSyms; |
| 54 | + if (tcc_relocate(s1) < 0) |
| 55 | + return -1; |
| 51 | 56 | |
| 52 | | −#define TCCSYM(a) { #a, &a, }, |
| 57 | + prog_main = tcc_get_symbol_err(s1, "main"); |
| 53 | 58 | |
| 54 | | −/* add the symbol you want here if no dynamic linking is done */ |
| 55 | | −static TCCSyms tcc_syms[] = { |
| 56 | | −#if !defined(CONFIG_TCCBOOT) |
| 57 | | − TCCSYM(printf) |
| 58 | | − TCCSYM(fprintf) |
| 59 | | − TCCSYM(fopen) |
| 60 | | − TCCSYM(fclose) |
| 59 | +#ifdef CONFIG_TCC_BACKTRACE |
| 60 | + if (s1->do_debug) |
| 61 | + set_exception_handler(); |
| 61 | 62 | #endif |
| 62 | | − { NULL, NULL }, |
| 63 | | −}; |
| 64 | 63 | |
| 65 | | −void *resolve_sym(TCCState *s1, const char *symbol) |
| 66 | | −{ |
| 67 | | − TCCSyms *p; |
| 68 | | − p = tcc_syms; |
| 69 | | − while (p->str != NULL) { |
| 70 | | − if (!strcmp(p->str, symbol)) |
| 71 | | − return p->ptr; |
| 72 | | − p++; |
| 64 | +#ifdef CONFIG_TCC_BCHECK |
| 65 | + if (s1->do_bounds_check) { |
| 66 | + void (*bound_init)(void); |
| 67 | + void (*bound_exit)(void); |
| 68 | + int ret; |
| 69 | + /* set error function */ |
| 70 | + rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); |
| 71 | + rt_prog_main = prog_main; |
| 72 | + /* XXX: use .init section so that it also work in binary ? */ |
| 73 | + bound_init = tcc_get_symbol_err(s1, "__bound_init"); |
| 74 | + bound_exit = tcc_get_symbol_err(s1, "__bound_exit"); |
| 75 | + bound_init(); |
| 76 | + ret = (*prog_main)(argc, argv); |
| 77 | + bound_exit(); |
| 78 | + return ret; |
| 73 | 79 | } |
| 74 | | − return NULL; |
| 80 | +#endif |
| 81 | + return (*prog_main)(argc, argv); |
| 75 | 82 | } |
| 76 | 83 | |
| 77 | | −#elif defined(_WIN32) |
| 78 | | −#define dlclose FreeLibrary |
| 79 | 84 | |
| 85 | +/* relocate code. Return -1 on error, required size if ptr is NULL, |
| 86 | + otherwise copy code into buffer passed by the caller */ |
| 87 | +static int tcc_relocate_ex(TCCState *s1, void *ptr) |
| 88 | +{ |
| 89 | + Section *s; |
| 90 | + unsigned long offset, length; |
| 91 | + uplong mem; |
| 92 | + int i; |
| 93 | + |
| 94 | + if (0 == s1->runtime_added) { |
| 95 | + s1->runtime_added = 1; |
| 96 | + s1->nb_errors = 0; |
| 97 | +#ifdef TCC_TARGET_PE |
| 98 | + pe_output_file(s1, NULL); |
| 80 | 99 | #else |
| 81 | | −#include <dlfcn.h> |
| 100 | + tcc_add_runtime(s1); |
| 101 | + relocate_common_syms(); |
| 102 | + tcc_add_linker_symbols(s1); |
| 103 | + build_got_entries(s1); |
| 104 | +#endif |
| 105 | + if (s1->nb_errors) |
| 106 | + return -1; |
| 107 | + } |
| 82 | 108 | |
| 83 | | −void *resolve_sym(TCCState *s1, const char *sym) |
| 84 | | −{ |
| 85 | | − return dlsym(RTLD_DEFAULT, sym); |
| 109 | + offset = 0, mem = (uplong)ptr; |
| 110 | + for(i = 1; i < s1->nb_sections; i++) { |
| 111 | + s = s1->sections[i]; |
| 112 | + if (0 == (s->sh_flags & SHF_ALLOC)) |
| 113 | + continue; |
| 114 | + length = s->data_offset; |
| 115 | + s->sh_addr = mem ? (mem + offset + 15) & ~15 : 0; |
| 116 | + offset = (offset + length + 15) & ~15; |
| 117 | + } |
| 118 | + offset += 16; |
| 119 | + |
| 120 | + /* relocate symbols */ |
| 121 | + relocate_syms(s1, 1); |
| 122 | + if (s1->nb_errors) |
| 123 | + return -1; |
| 124 | + |
| 125 | +#if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE |
| 126 | + s1->runtime_plt_and_got_offset = 0; |
| 127 | + s1->runtime_plt_and_got = (char *)(mem + offset); |
| 128 | + /* double the size of the buffer for got and plt entries |
| 129 | + XXX: calculate exact size for them? */ |
| 130 | + offset *= 2; |
| 131 | +#endif |
| 132 | + |
| 133 | + if (0 == mem) |
| 134 | + return offset; |
| 135 | + |
| 136 | + /* relocate each section */ |
| 137 | + for(i = 1; i < s1->nb_sections; i++) { |
| 138 | + s = s1->sections[i]; |
| 139 | + if (s->reloc) |
| 140 | + relocate_section(s1, s); |
| 141 | + } |
| 142 | + |
| 143 | + for(i = 1; i < s1->nb_sections; i++) { |
| 144 | + s = s1->sections[i]; |
| 145 | + if (0 == (s->sh_flags & SHF_ALLOC)) |
| 146 | + continue; |
| 147 | + length = s->data_offset; |
| 148 | + // printf("%-12s %08x %04x\n", s->name, s->sh_addr, length); |
| 149 | + ptr = (void*)(uplong)s->sh_addr; |
| 150 | + if (NULL == s->data || s->sh_type == SHT_NOBITS) |
| 151 | + memset(ptr, 0, length); |
| 152 | + else |
| 153 | + memcpy(ptr, s->data, length); |
| 154 | + /* mark executable sections as executable in memory */ |
| 155 | + if (s->sh_flags & SHF_EXECINSTR) |
| 156 | + set_pages_executable(ptr, length); |
| 157 | + } |
| 158 | + |
| 159 | +#if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE |
| 160 | + set_pages_executable(s1->runtime_plt_and_got, |
| 161 | + s1->runtime_plt_and_got_offset); |
| 162 | +#endif |
| 163 | + return 0; |
| 86 | 164 | } |
| 87 | 165 | |
| 88 | | −#endif /* defined CONFIG_TCC_STATIC */ |
| 166 | +/* ------------------------------------------------------------- */ |
| 167 | +/* allow to run code in memory */ |
| 89 | 168 | |
| 90 | | −/********************************************************/ |
| 169 | +static void set_pages_executable(void *ptr, unsigned long length) |
| 170 | +{ |
| 171 | +#ifdef _WIN32 |
| 172 | + unsigned long old_protect; |
| 173 | + VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect); |
| 174 | +#else |
| 175 | + unsigned long start, end; |
| 176 | + start = (unsigned long)ptr & ~(PAGESIZE - 1); |
| 177 | + end = (unsigned long)ptr + length; |
| 178 | + end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); |
| 179 | + mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 180 | +#endif |
| 181 | +} |
| 91 | 182 | |
| 183 | +/* ------------------------------------------------------------- */ |
| 92 | 184 | #ifdef CONFIG_TCC_BACKTRACE |
| 93 | 185 | |
| 94 | 186 | /* print the position in the source file of PC value 'pc' by reading |
| @@ -215,7 +307,92 @@ static unsigned long rt_printline(unsigned long wanted_pc) · 120 unmodified lines |
| 215 | 307 | return func_addr; |
| 216 | 308 | } |
| 217 | 309 | |
| 310 | +/* emit a run time error at position 'pc' */ |
| 311 | +static void rt_error(ucontext_t *uc, const char *fmt, ...) |
| 312 | +{ |
| 313 | + va_list ap; |
| 314 | + unsigned long pc; |
| 315 | + int i; |
| 316 | + |
| 317 | + va_start(ap, fmt); |
| 318 | + fprintf(stderr, "Runtime error: "); |
| 319 | + vfprintf(stderr, fmt, ap); |
| 320 | + fprintf(stderr, "\n"); |
| 321 | + |
| 322 | + for(i=0;i<num_callers;i++) { |
| 323 | + if (rt_get_caller_pc(&pc, uc, i) < 0) |
| 324 | + break; |
| 325 | + if (i == 0) |
| 326 | + fprintf(stderr, "at "); |
| 327 | + else |
| 328 | + fprintf(stderr, "by "); |
| 329 | + pc = rt_printline(pc); |
| 330 | + if (pc == (unsigned long)rt_prog_main && pc) |
| 331 | + break; |
| 332 | + } |
| 333 | + exit(255); |
| 334 | + va_end(ap); |
| 335 | +} |
| 336 | + |
| 337 | +/* ------------------------------------------------------------- */ |
| 338 | +#ifndef _WIN32 |
| 339 | + |
| 340 | +/* signal handler for fatal errors */ |
| 341 | +static void sig_error(int signum, siginfo_t *siginf, void *puc) |
| 342 | +{ |
| 343 | + ucontext_t *uc = puc; |
| 344 | + |
| 345 | + switch(signum) { |
| 346 | + case SIGFPE: |
| 347 | + switch(siginf->si_code) { |
| 348 | + case FPE_INTDIV: |
| 349 | + case FPE_FLTDIV: |
| 350 | + rt_error(uc, "division by zero"); |
| 351 | + break; |
| 352 | + default: |
| 353 | + rt_error(uc, "floating point exception"); |
| 354 | + break; |
| 355 | + } |
| 356 | + break; |
| 357 | + case SIGBUS: |
| 358 | + case SIGSEGV: |
| 359 | + if (rt_bound_error_msg && *rt_bound_error_msg) |
| 360 | + rt_error(uc, *rt_bound_error_msg); |
| 361 | + else |
| 362 | + rt_error(uc, "dereferencing invalid pointer"); |
| 363 | + break; |
| 364 | + case SIGILL: |
| 365 | + rt_error(uc, "illegal instruction"); |
| 366 | + break; |
| 367 | + case SIGABRT: |
| 368 | + rt_error(uc, "abort() called"); |
| 369 | + break; |
| 370 | + default: |
| 371 | + rt_error(uc, "caught signal %d", signum); |
| 372 | + break; |
| 373 | + } |
| 374 | + exit(255); |
| 375 | +} |
| 376 | + |
| 377 | +/* Generate a stack backtrace when a CPU exception occurs. */ |
| 378 | +static void set_exception_handler(void) |
| 379 | +{ |
| 380 | + struct sigaction sigact; |
| 381 | + /* install TCC signal handlers to print debug info on fatal |
| 382 | + runtime errors */ |
| 383 | + sigact.sa_flags = SA_SIGINFO | SA_RESETHAND; |
| 384 | + sigact.sa_sigaction = sig_error; |
| 385 | + sigemptyset(&sigact.sa_mask); |
| 386 | + sigaction(SIGFPE, &sigact, NULL); |
| 387 | + sigaction(SIGILL, &sigact, NULL); |
| 388 | + sigaction(SIGSEGV, &sigact, NULL); |
| 389 | + sigaction(SIGBUS, &sigact, NULL); |
| 390 | + sigaction(SIGABRT, &sigact, NULL); |
| 391 | +} |
| 392 | + |
| 393 | +/* ------------------------------------------------------------- */ |
| 218 | 394 | #ifdef __i386__ |
| 395 | + |
| 219 | 396 | /* fix for glibc 2.1 */ |
| 220 | 397 | #ifndef REG_EIP |
| 221 | 398 | #define REG_EIP EIP |
| @@ -223,8 +400,7 @@ static unsigned long rt_printline(unsigned long wanted_pc) · 1 unmodified line |
| 223 | 400 | #endif |
| 224 | 401 | |
| 225 | 402 | /* return the PC at frame level 'level'. Return non zero if not found */ |
| 226 | | −static int rt_get_caller_pc(unsigned long *paddr, |
| 227 | | − ucontext_t *uc, int level) |
| 403 | +static int rt_get_caller_pc(unsigned long *paddr, ucontext_t *uc, int level) |
| 228 | 404 | { |
| 229 | 405 | unsigned long fp; |
| 230 | 406 | int i; |
| @@ -256,7 +432,10 @@ static int rt_get_caller_pc(unsigned long *paddr, · 25 unmodified lines |
| 256 | 432 | return 0; |
| 257 | 433 | } |
| 258 | 434 | } |
| 435 | + |
| 436 | +/* ------------------------------------------------------------- */ |
| 259 | 437 | #elif defined(__x86_64__) |
| 438 | + |
| 260 | 439 | /* return the PC at frame level 'level'. Return non zero if not found */ |
| 261 | 440 | static int rt_get_caller_pc(unsigned long *paddr, |
| 262 | 441 | ucontext_t *uc, int level) |
| @@ -288,236 +467,136 @@ static int rt_get_caller_pc(unsigned long *paddr, · 25 unmodified lines |
| 288 | 467 | return 0; |
| 289 | 468 | } |
| 290 | 469 | } |
| 470 | + |
| 471 | +/* ------------------------------------------------------------- */ |
| 291 | 472 | #else |
| 473 | + |
| 292 | 474 | #warning add arch specific rt_get_caller_pc() |
| 293 | 475 | static int rt_get_caller_pc(unsigned long *paddr, |
| 294 | 476 | ucontext_t *uc, int level) |
| 295 | 477 | { |
| 296 | 478 | return -1; |
| 297 | 479 | } |
| 298 | | −#endif |
| 299 | 480 | |
| 300 | | −/* emit a run time error at position 'pc' */ |
| 301 | | −void rt_error(ucontext_t *uc, const char *fmt, ...) |
| 302 | | −{ |
| 303 | | − va_list ap; |
| 304 | | − unsigned long pc; |
| 305 | | − int i; |
| 481 | +#endif /* !__i386__ */ |
| 306 | 482 | |
| 307 | | − va_start(ap, fmt); |
| 308 | | − fprintf(stderr, "Runtime error: "); |
| 309 | | − vfprintf(stderr, fmt, ap); |
| 310 | | − fprintf(stderr, "\n"); |
| 311 | | − for(i=0;i<num_callers;i++) { |
| 312 | | − if (rt_get_caller_pc(&pc, uc, i) < 0) |
| 313 | | − break; |
| 314 | | − if (i == 0) |
| 315 | | − fprintf(stderr, "at "); |
| 316 | | − else |
| 317 | | − fprintf(stderr, "by "); |
| 318 | | − pc = rt_printline(pc); |
| 319 | | − if (pc == rt_prog_main && pc) |
| 320 | | − break; |
| 321 | | − } |
| 322 | | − exit(255); |
| 323 | | − va_end(ap); |
| 324 | | −} |
| 483 | +/* ------------------------------------------------------------- */ |
| 484 | +#else /* WIN32 */ |
| 325 | 485 | |
| 326 | | −/* signal handler for fatal errors */ |
| 327 | | −static void sig_error(int signum, siginfo_t *siginf, void *puc) |
| 486 | +static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info) |
| 328 | 487 | { |
| 329 | | − ucontext_t *uc = puc; |
| 330 | | − |
| 331 | | − switch(signum) { |
| 332 | | − case SIGFPE: |
| 333 | | − switch(siginf->si_code) { |
| 334 | | − case FPE_INTDIV: |
| 335 | | − case FPE_FLTDIV: |
| 336 | | − rt_error(uc, "division by zero"); |
| 337 | | − break; |
| 338 | | − default: |
| 339 | | − rt_error(uc, "floating point exception"); |
| 340 | | − break; |
| 341 | | − } |
| 342 | | − break; |
| 343 | | − case SIGBUS: |
| 344 | | − case SIGSEGV: |
| 345 | | − if (rt_bound_error_msg && *rt_bound_error_msg) |
| 346 | | − rt_error(uc, *rt_bound_error_msg); |
| 347 | | − else |
| 348 | | − rt_error(uc, "dereferencing invalid pointer"); |
| 349 | | − break; |
| 350 | | − case SIGILL: |
| 351 | | − rt_error(uc, "illegal instruction"); |
| 352 | | − break; |
| 353 | | − case SIGABRT: |
| 354 | | − rt_error(uc, "abort() called"); |
| 355 | | − break; |
| 356 | | − default: |
| 357 | | − rt_error(uc, "caught signal %d", signum); |
| 358 | | − break; |
| 359 | | − } |
| 488 | + CONTEXT *uc = ex_info->ContextRecord; |
| 489 | +/* |
| 490 | + EXCEPTION_RECORD *er = ex_info->ExceptionRecord; |
| 491 | + printf("CPU exception: code=%08lx addr=%p\n", |
| 492 | + er->ExceptionCode, er->ExceptionAddress); |
| 493 | +*/ |
| 494 | + if (rt_bound_error_msg && *rt_bound_error_msg) |
| 495 | + rt_error(uc, *rt_bound_error_msg); |
| 496 | + else |
| 497 | + rt_error(uc, "dereferencing invalid pointer"); |
| 360 | 498 | exit(255); |
| 499 | + //return EXCEPTION_CONTINUE_SEARCH; |
| 361 | 500 | } |
| 362 | 501 | |
| 363 | | −#endif /* defined CONFIG_TCC_BACKTRACE */ |
| 364 | | − |
| 365 | | −/********************************************************/ |
| 366 | | − |
| 367 | | −void set_pages_executable(void *ptr, unsigned long length) |
| 502 | +/* Generate a stack backtrace when a CPU exception occurs. */ |
| 503 | +static void set_exception_handler(void) |
| 368 | 504 | { |
| 369 | | −#ifdef _WIN32 |
| 370 | | − unsigned long old_protect; |
| 371 | | − VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect); |
| 372 | | −#else |
| 373 | | − unsigned long start, end; |
| 374 | | − start = (unsigned long)ptr & ~(PAGESIZE - 1); |
| 375 | | − end = (unsigned long)ptr + length; |
| 376 | | − end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); |
| 377 | | − mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 378 | | −#endif |
| 505 | + SetUnhandledExceptionFilter(cpu_exception_handler); |
| 379 | 506 | } |
| 380 | 507 | |
| 381 | | −/* relocate code. Return -1 on error, required size if ptr is NULL, |
| 382 | | − otherwise copy code into buffer passed by the caller */ |
| 383 | | −static int tcc_relocate_ex(TCCState *s1, void *ptr) |
| 508 | +/* return the PC at frame level 'level'. Return non zero if not found */ |
| 509 | +static int rt_get_caller_pc(unsigned long *paddr, |
| 510 | + CONTEXT *uc, int level) |
| 384 | 511 | { |
| 385 | | − Section *s; |
| 386 | | − unsigned long offset, length; |
| 387 | | − uplong mem; |
| 512 | + unsigned long fp; |
| 388 | 513 | int i; |
| 389 | 514 | |
| 390 | | − if (0 == s1->runtime_added) { |
| 391 | | − s1->runtime_added = 1; |
| 392 | | − s1->nb_errors = 0; |
| 393 | | −#ifdef TCC_TARGET_PE |
| 394 | | − pe_output_file(s1, NULL); |
| 395 | | −#else |
| 396 | | − tcc_add_runtime(s1); |
| 397 | | − relocate_common_syms(); |
| 398 | | − tcc_add_linker_symbols(s1); |
| 399 | | − build_got_entries(s1); |
| 400 | | −#endif |
| 401 | | − if (s1->nb_errors) |
| 402 | | − return -1; |
| 403 | | − } |
| 404 | | − |
| 405 | | − offset = 0, mem = (uplong)ptr; |
| 406 | | − for(i = 1; i < s1->nb_sections; i++) { |
| 407 | | − s = s1->sections[i]; |
| 408 | | − if (0 == (s->sh_flags & SHF_ALLOC)) |
| 409 | | − continue; |
| 410 | | − length = s->data_offset; |
| 411 | | − s->sh_addr = mem ? (mem + offset + 15) & ~15 : 0; |
| 412 | | − offset = (offset + length + 15) & ~15; |
| 515 | + if (level == 0) { |
| 516 | + *paddr = uc->Eip; |
| 517 | + return 0; |
| 518 | + } else { |
| 519 | + fp = uc->Ebp; |
| 520 | + for(i=1;i<level;i++) { |
| 521 | + /* XXX: check address validity with program info */ |
| 522 | + if (fp <= 0x1000 || fp >= 0xc0000000) |
| 523 | + return -1; |
| 524 | + fp = ((unsigned long *)fp)[0]; |
| 525 | + } |
| 526 | + *paddr = ((unsigned long *)fp)[1]; |
| 527 | + return 0; |
| 413 | 528 | } |
| 414 | | − offset += 16; |
| 415 | | − |
| 416 | | − /* relocate symbols */ |
| 417 | | − relocate_syms(s1, 1); |
| 418 | | − if (s1->nb_errors) |
| 419 | | − return -1; |
| 420 | | − |
| 421 | | −#ifndef TCC_TARGET_PE |
| 422 | | −#ifdef TCC_TARGET_X86_64 |
| 423 | | − s1->runtime_plt_and_got_offset = 0; |
| 424 | | − s1->runtime_plt_and_got = (char *)(mem + offset); |
| 425 | | − /* double the size of the buffer for got and plt entries |
| 426 | | − XXX: calculate exact size for them? */ |
| 427 | | − offset *= 2; |
| 428 | | −#endif |
| 429 | | −#endif |
| 529 | +} |
| 430 | 530 | |
| 431 | | − if (0 == mem) |
| 432 | | − return offset; |
| 531 | +#endif /* _WIN32 */ |
| 532 | +#endif /* CONFIG_TCC_BACKTRACE */ |
| 533 | +/* ------------------------------------------------------------- */ |
| 433 | 534 | |
| 434 | | − /* relocate each section */ |
| 435 | | − for(i = 1; i < s1->nb_sections; i++) { |
| 436 | | − s = s1->sections[i]; |
| 437 | | − if (s->reloc) |
| 438 | | − relocate_section(s1, s); |
| 439 | | − } |
| 535 | +#ifdef CONFIG_TCC_STATIC |
| 440 | 536 | |
| 441 | | − for(i = 1; i < s1->nb_sections; i++) { |
| 442 | | − s = s1->sections[i]; |
| 443 | | − if (0 == (s->sh_flags & SHF_ALLOC)) |
| 444 | | − continue; |
| 445 | | − length = s->data_offset; |
| 446 | | − // printf("%-12s %08x %04x\n", s->name, s->sh_addr, length); |
| 447 | | − ptr = (void*)(uplong)s->sh_addr; |
| 448 | | − if (NULL == s->data || s->sh_type == SHT_NOBITS) |
| 449 | | − memset(ptr, 0, length); |
| 450 | | − else |
| 451 | | − memcpy(ptr, s->data, length); |
| 452 | | − /* mark executable sections as executable in memory */ |
| 453 | | − if (s->sh_flags & SHF_EXECINSTR) |
| 454 | | − set_pages_executable(ptr, length); |
| 455 | | − } |
| 537 | +#define RTLD_LAZY 0x001 |
| 538 | +#define RTLD_NOW 0x002 |
| 539 | +#define RTLD_GLOBAL 0x100 |
| 540 | +#define RTLD_DEFAULT NULL |
| 456 | 541 | |
| 457 | | −#ifndef TCC_TARGET_PE |
| 458 | | −#ifdef TCC_TARGET_X86_64 |
| 459 | | − set_pages_executable(s1->runtime_plt_and_got, |
| 460 | | − s1->runtime_plt_and_got_offset); |
| 461 | | −#endif |
| 462 | | −#endif |
| 463 | | − return 0; |
| 542 | +/* dummy function for profiling */ |
| 543 | +void *dlopen(const char *filename, int flag) |
| 544 | +{ |
| 545 | + return NULL; |
| 464 | 546 | } |
| 465 | 547 | |
| 466 | | −/* Do all relocations (needed before using tcc_get_symbol()) |
| 467 | | − Returns -1 on error. */ |
| 468 | | −int tcc_relocate(TCCState *s1) |
| 548 | +void dlclose(void *p) |
| 469 | 549 | { |
| 470 | | − int ret = tcc_relocate_ex(s1, NULL); |
| 471 | | − if (-1 == ret) |
| 472 | | − return ret; |
| 473 | | − s1->runtime_mem = tcc_malloc(ret); |
| 474 | | − return tcc_relocate_ex(s1, s1->runtime_mem); |
| 475 | 550 | } |
| 476 | 551 | |
| 477 | | −/* launch the compiled program with the given arguments */ |
| 478 | | −int tcc_run(TCCState *s1, int argc, char **argv) |
| 552 | +const char *dlerror(void) |
| 479 | 553 | { |
| 480 | | − int (*prog_main)(int, char **); |
| 554 | + return "error"; |
| 555 | +} |
| 481 | 556 | |
| 482 | | − if (tcc_relocate(s1) < 0) |
| 483 | | − return -1; |
| 557 | +typedef struct TCCSyms { |
| 558 | + char *str; |
| 559 | + void *ptr; |
| 560 | +} TCCSyms; |
| 484 | 561 | |
| 485 | | − prog_main = tcc_get_symbol_err(s1, "main"); |
| 562 | +#define TCCSYM(a) { #a, &a, }, |
| 486 | 563 | |
| 487 | | − if (s1->do_debug) { |
| 488 | | −#ifdef CONFIG_TCC_BACKTRACE |
| 489 | | − struct sigaction sigact; |
| 490 | | − /* install TCC signal handlers to print debug info on fatal |
| 491 | | − runtime errors */ |
| 492 | | − sigact.sa_flags = SA_SIGINFO | SA_RESETHAND; |
| 493 | | − sigact.sa_sigaction = sig_error; |
| 494 | | − sigemptyset(&sigact.sa_mask); |
| 495 | | − sigaction(SIGFPE, &sigact, NULL); |
| 496 | | − sigaction(SIGILL, &sigact, NULL); |
| 497 | | − sigaction(SIGSEGV, &sigact, NULL); |
| 498 | | − sigaction(SIGBUS, &sigact, NULL); |
| 499 | | − sigaction(SIGABRT, &sigact, NULL); |
| 500 | | −#else |
| 501 | | − error("debug mode not available"); |
| 564 | +/* add the symbol you want here if no dynamic linking is done */ |
| 565 | +static TCCSyms tcc_syms[] = { |
| 566 | +#if !defined(CONFIG_TCCBOOT) |
| 567 | + TCCSYM(printf) |
| 568 | + TCCSYM(fprintf) |
| 569 | + TCCSYM(fopen) |
| 570 | + TCCSYM(fclose) |
| 502 | 571 | #endif |
| 572 | + { NULL, NULL }, |
| 573 | +}; |
| 574 | + |
| 575 | +void *resolve_sym(TCCState *s1, const char *symbol) |
| 576 | +{ |
| 577 | + TCCSyms *p; |
| 578 | + p = tcc_syms; |
| 579 | + while (p->str != NULL) { |
| 580 | + if (!strcmp(p->str, symbol)) |
| 581 | + return p->ptr; |
| 582 | + p++; |
| 503 | 583 | } |
| 584 | + return NULL; |
| 585 | +} |
| 504 | 586 | |
| 505 | | −#ifdef CONFIG_TCC_BCHECK |
| 506 | | − if (s1->do_bounds_check) { |
| 507 | | − void (*bound_init)(void); |
| 508 | | − void (*bound_exit)(void); |
| 509 | | − /* set error function */ |
| 510 | | − rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); |
| 511 | | − rt_prog_main = (unsigned long)prog_main; |
| 512 | | − /* XXX: use .init section so that it also work in binary ? */ |
| 513 | | − bound_init = tcc_get_symbol_err(s1, "__bound_init"); |
| 514 | | − bound_exit = tcc_get_symbol_err(s1, "__bound_exit"); |
| 515 | | − bound_init(); |
| 516 | | − ret = (*prog_main)(argc, argv); |
| 517 | | − bound_exit(); |
| 518 | | − } else |
| 519 | | −#endif |
| 520 | | − return (*prog_main)(argc, argv); |
| 587 | +#elif defined(_WIN32) |
| 588 | + |
| 589 | +#define dlclose FreeLibrary |
| 590 | + |
| 591 | +#else |
| 592 | + |
| 593 | +#include <dlfcn.h> |
| 594 | + |
| 595 | +void *resolve_sym(TCCState *s1, const char *sym) |
| 596 | +{ |
| 597 | + return dlsym(RTLD_DEFAULT, sym); |
| 521 | 598 | } |
| 522 | 599 | |
| 523 | | −/********************************************************/ |
| 600 | +#endif /* CONFIG_TCC_STATIC */ |
| 601 | + |
| 602 | +/* ------------------------------------------------------------- */ |