Unified · Split
| @@ -25,7 +25,6 @@ · 24 unmodified lines | ||
| 25 | 25 | #include <math.h> |
| 26 | 26 | #include <unistd.h> |
| 27 | 27 | #include <signal.h> |
| 28 | −#include <malloc.h> | |
| 29 | 28 | #include <unistd.h> |
| 30 | 29 | #include <fcntl.h> |
| 31 | 30 | #ifndef WIN32 |
| @@ -307,6 +306,9 @@ int tcc_ext = 1; · 275 unmodified lines | ||
| 307 | 306 | /* if true, static linking is performed */ |
| 308 | 307 | int static_link = 0; |
| 309 | 308 | |
| 309 | +/* give the path of the tcc libraries */ | |
| 310 | +static const char *tcc_lib_path = CONFIG_TCC_PREFIX "/lib/tcc"; | |
| 311 | + | |
| 310 | 312 | struct TCCState { |
| 311 | 313 | int output_type; |
| 312 | 314 | }; |
| @@ -586,6 +588,8 @@ void type_to_str(char *buf, int buf_size, · 273 unmodified lines | ||
| 586 | 588 | int t, const char *varstr); |
| 587 | 589 | char *get_tok_str(int v, CValue *cv); |
| 588 | 590 | Sym *external_sym(int v, int u, int r); |
| 591 | +static Sym *get_sym_ref(int t, Section *sec, | |
| 592 | + unsigned long offset, unsigned long size); | |
| 589 | 593 | |
| 590 | 594 | /* section generation */ |
| 591 | 595 | void put_extern_sym(Sym *sym, Section *section, unsigned long value); |
| @@ -613,10 +617,6 @@ static inline int is_float(int t) · 21 unmodified lines | ||
| 613 | 617 | return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT; |
| 614 | 618 | } |
| 615 | 619 | |
| 616 | −#ifdef CONFIG_TCC_BCHECK | |
| 617 | −#include "bcheck.c" | |
| 618 | −#endif | |
| 619 | − | |
| 620 | 620 | #ifdef TCC_TARGET_I386 |
| 621 | 621 | #include "i386-gen.c" |
| 622 | 622 | #endif |
| @@ -2494,18 +2494,27 @@ void vpushi(int v) · 1871 unmodified lines | ||
| 2494 | 2494 | vsetc(VT_INT, VT_CONST, &cval); |
| 2495 | 2495 | } |
| 2496 | 2496 | |
| 2497 | −/* push a reference to a section offset by adding a dummy symbol */ | |
| 2498 | −void vpush_ref(int t, Section *sec, unsigned long offset) | |
| 2497 | +/* Return a static symbol pointing to a section */ | |
| 2498 | +static Sym *get_sym_ref(int t, Section *sec, | |
| 2499 | + unsigned long offset, unsigned long size) | |
| 2499 | 2500 | { |
| 2500 | 2501 | int v; |
| 2501 | 2502 | Sym *sym; |
| 2502 | − CValue cval; | |
| 2503 | 2503 | |
| 2504 | 2504 | v = anon_sym++; |
| 2505 | 2505 | sym = sym_push1(&global_stack, v, t | VT_STATIC, 0); |
| 2506 | 2506 | sym->r = VT_CONST | VT_SYM; |
| 2507 | 2507 | put_extern_sym(sym, sec, offset); |
| 2508 | − cval.sym = sym; | |
| 2508 | + return sym; | |
| 2509 | +} | |
| 2510 | + | |
| 2511 | +/* push a reference to a section offset by adding a dummy symbol */ | |
| 2512 | +/* XXX: add size */ | |
| 2513 | +void vpush_ref(int t, Section *sec, unsigned long offset) | |
| 2514 | +{ | |
| 2515 | + CValue cval; | |
| 2516 | + | |
| 2517 | + cval.sym = get_sym_ref(t, sec, offset, 0); | |
| 2509 | 2518 | vsetc(t, VT_CONST | VT_SYM, &cval); |
| 2510 | 2519 | } |
| 2511 | 2520 | |
| @@ -6397,7 +6406,8 @@ void put_extern_sym(Sym *sym, Section *section, unsigned long value) · 3885 unmodified lines | ||
| 6397 | 6406 | int sym_type, sym_bind, sh_num, info; |
| 6398 | 6407 | Elf32_Sym *esym; |
| 6399 | 6408 | const char *name; |
| 6400 | − | |
| 6409 | + char buf[32]; | |
| 6410 | + | |
| 6401 | 6411 | if (section) |
| 6402 | 6412 | sh_num = section->sh_num; |
| 6403 | 6413 | else |
| @@ -6411,7 +6421,30 @@ void put_extern_sym(Sym *sym, Section *section, unsigned long value) · 7 unmodified lines | ||
| 6411 | 6421 | sym_bind = STB_LOCAL; |
| 6412 | 6422 | else |
| 6413 | 6423 | sym_bind = STB_GLOBAL; |
| 6424 | + | |
| 6414 | 6425 | name = get_tok_str(sym->v, NULL); |
| 6426 | +#ifdef CONFIG_TCC_BCHECK | |
| 6427 | + if (do_bounds_check) { | |
| 6428 | + /* if bound checking is activated, we change some function | |
| 6429 | + names by adding the "__bound" prefix */ | |
| 6430 | + switch(sym->v) { | |
| 6431 | + case TOK_malloc: | |
| 6432 | + case TOK_free: | |
| 6433 | + case TOK_realloc: | |
| 6434 | + case TOK_memalign: | |
| 6435 | + case TOK_calloc: | |
| 6436 | + case TOK_memcpy: | |
| 6437 | + case TOK_memmove: | |
| 6438 | + case TOK_memset: | |
| 6439 | + case TOK_strlen: | |
| 6440 | + case TOK_strcpy: | |
| 6441 | + strcpy(buf, "__bound_"); | |
| 6442 | + strcat(buf, name); | |
| 6443 | + name = buf; | |
| 6444 | + break; | |
| 6445 | + } | |
| 6446 | + } | |
| 6447 | +#endif | |
| 6415 | 6448 | info = ELF32_ST_INFO(sym_bind, sym_type); |
| 6416 | 6449 | sym->c = add_elf_sym(symtab_section, value, 0, info, sh_num, name); |
| 6417 | 6450 | } else { |
| @@ -6576,14 +6609,6 @@ static void relocate_common_syms(void) · 158 unmodified lines | ||
| 6576 | 6609 | |
| 6577 | 6610 | static void *resolve_sym(const char *sym) |
| 6578 | 6611 | { |
| 6579 | −#ifdef CONFIG_TCC_BCHECK | |
| 6580 | − if (do_bounds_check) { | |
| 6581 | − void *ptr; | |
| 6582 | − ptr = bound_resolve_sym(sym); | |
| 6583 | − if (ptr) | |
| 6584 | − return ptr; | |
| 6585 | − } | |
| 6586 | −#endif | |
| 6587 | 6612 | return dlsym(NULL, sym); |
| 6588 | 6613 | } |
| 6589 | 6614 | |
| @@ -6867,7 +6892,44 @@ static void put_dt(Section *dynamic, int dt, unsigned long val) · 277 unmodified lines | ||
| 6867 | 6892 | /* add tcc runtime libraries */ |
| 6868 | 6893 | static void tcc_add_runtime(TCCState *s1) |
| 6869 | 6894 | { |
| 6870 | − tcc_add_file(s1, CONFIG_TCC_PREFIX "/lib/tcc/libtcc1.o"); | |
| 6895 | + char buf[1024]; | |
| 6896 | + | |
| 6897 | + snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "libtcc1.o"); | |
| 6898 | + tcc_add_file(s1, buf); | |
| 6899 | +#ifdef CONFIG_TCC_BCHECK | |
| 6900 | + if (do_bounds_check) { | |
| 6901 | + /* XXX: add an object file to do that */ | |
| 6902 | + *(int *)bounds_section->data_ptr = 0; | |
| 6903 | + bounds_section->data_ptr += sizeof(int); | |
| 6904 | + add_elf_sym(symtab_section, 0, 0, | |
| 6905 | + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | |
| 6906 | + bounds_section->sh_num, "__bounds_start"); | |
| 6907 | + add_elf_sym(symtab_section, (long)&rt_error, 0, | |
| 6908 | + ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), | |
| 6909 | + SHN_ABS, "rt_error"); | |
| 6910 | + /* add bound check code */ | |
| 6911 | + snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o"); | |
| 6912 | + tcc_add_file(s1, buf); | |
| 6913 | + } | |
| 6914 | +#endif | |
| 6915 | + /* add libc if not memory output */ | |
| 6916 | + if (s1->output_type != TCC_OUTPUT_MEMORY) { | |
| 6917 | + tcc_add_library(s1, "c"); | |
| 6918 | + tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o"); | |
| 6919 | + } | |
| 6920 | + /* add various standard linker symbols */ | |
| 6921 | + add_elf_sym(symtab_section, | |
| 6922 | + text_section->data_ptr - text_section->data, 0, | |
| 6923 | + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | |
| 6924 | + text_section->sh_num, "_etext"); | |
| 6925 | + add_elf_sym(symtab_section, | |
| 6926 | + data_section->data_ptr - data_section->data, 0, | |
| 6927 | + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | |
| 6928 | + data_section->sh_num, "_edata"); | |
| 6929 | + add_elf_sym(symtab_section, | |
| 6930 | + bss_section->data_ptr - bss_section->data, 0, | |
| 6931 | + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | |
| 6932 | + bss_section->sh_num, "_end"); | |
| 6871 | 6933 | } |
| 6872 | 6934 | |
| 6873 | 6935 | /* add dynamic sections so that the executable is dynamically linked */ |
| @@ -6898,12 +6960,8 @@ int tcc_output_file(TCCState *s1, const char *filename) · 24 unmodified lines | ||
| 6898 | 6960 | |
| 6899 | 6961 | file_type = s1->output_type; |
| 6900 | 6962 | |
| 6901 | − /* add libc crtn object */ | |
| 6902 | − if (file_type != TCC_OUTPUT_OBJ) { | |
| 6963 | + if (file_type != TCC_OUTPUT_OBJ) | |
| 6903 | 6964 | tcc_add_runtime(s1); |
| 6904 | − tcc_add_library(s1, "c"); | |
| 6905 | − tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o"); | |
| 6906 | − } | |
| 6907 | 6965 | |
| 6908 | 6966 | interp = NULL; |
| 6909 | 6967 | dynamic = NULL; |
| @@ -8012,6 +8070,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 1102 unmodified lines | ||
| 8012 | 8070 | { |
| 8013 | 8071 | Section *s; |
| 8014 | 8072 | int (*prog_main)(int, char **); |
| 8073 | + void (*bound_init)(void); | |
| 8015 | 8074 | int i; |
| 8016 | 8075 | |
| 8017 | 8076 | tcc_add_runtime(s1); |
| @@ -8056,18 +8115,11 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 38 unmodified lines | ||
| 8056 | 8115 | |
| 8057 | 8116 | #ifdef CONFIG_TCC_BCHECK |
| 8058 | 8117 | if (do_bounds_check) { |
| 8059 | − int *p, *p_end; | |
| 8060 | − __bound_init(); | |
| 8061 | − /* add all known static regions */ | |
| 8062 | − p = (int *)bounds_section->data; | |
| 8063 | − p_end = (int *)bounds_section->data_ptr; | |
| 8064 | − while (p < p_end) { | |
| 8065 | − __bound_new_region((void *)p[0], p[1]); | |
| 8066 | − p += 2; | |
| 8067 | − } | |
| 8118 | + /* XXX: use .init section so that it also work in binary ? */ | |
| 8119 | + bound_init = (void *)get_elf_sym_val("__bound_init"); | |
| 8120 | + bound_init(); | |
| 8068 | 8121 | } |
| 8069 | 8122 | #endif |
| 8070 | − | |
| 8071 | 8123 | return (*prog_main)(argc, argv); |
| 8072 | 8124 | } |
| 8073 | 8125 | |
| @@ -8283,6 +8335,14 @@ int tcc_add_library(TCCState *s, const char *libraryname) · 209 unmodified lines | ||
| 8283 | 8335 | return -1; |
| 8284 | 8336 | } |
| 8285 | 8337 | |
| 8338 | +int tcc_add_symbol(TCCState *s, const char *name, unsigned long val) | |
| 8339 | +{ | |
| 8340 | + add_elf_sym(symtab_section, val, 0, | |
| 8341 | + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), | |
| 8342 | + SHN_ABS, name); | |
| 8343 | + return 0; | |
| 8344 | +} | |
| 8345 | + | |
| 8286 | 8346 | int tcc_set_output_type(TCCState *s, int output_type) |
| 8287 | 8347 | { |
| 8288 | 8348 | s->output_type = output_type; |
| @@ -8302,16 +8362,17 @@ int tcc_set_output_type(TCCState *s, int output_type) · 13 unmodified lines | ||
| 8302 | 8362 | void help(void) |
| 8303 | 8363 | { |
| 8304 | 8364 | printf("tcc version 0.9.8 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" |
| 8305 | − "usage: tcc [-c] [-o outfile] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" | |
| 8365 | + "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" | |
| 8306 | 8366 | " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n" |
| 8307 | 8367 | " [--] infile1 [infile2... --] [infile_args...]\n" |
| 8308 | 8368 | "\n" |
| 8309 | 8369 | "General options:\n" |
| 8310 | 8370 | " -c compile only - generate an object file\n" |
| 8311 | − " -o outfile set output filename (NOT WORKING YET)\n" | |
| 8312 | − " -bench output compilation statistics\n" | |
| 8371 | + " -o outfile set output filename\n" | |
| 8313 | 8372 | " -- allows multiples input files if no -o option given. Also\n" |
| 8314 | 8373 | " separate input files from runtime arguments\n" |
| 8374 | + " -Bdir set tcc internal library path\n" | |
| 8375 | + " -bench output compilation statistics\n" | |
| 8315 | 8376 | "Preprocessor options:\n" |
| 8316 | 8377 | " -Idir add include path 'dir'\n" |
| 8317 | 8378 | " -Dsym[=val] define 'sym' with value 'val'\n" |
| @@ -8325,7 +8386,7 @@ void help(void) · 7 unmodified lines | ||
| 8325 | 8386 | " -Ldir add library path 'dir'\n" |
| 8326 | 8387 | " -llib link with dynamic library 'lib'\n" |
| 8327 | 8388 | " -shared generate a shared library (NOT WORKING YET)\n" |
| 8328 | − " -static static linking (NOT WORKING YET)\n" | |
| 8389 | + " -static static linking\n" | |
| 8329 | 8390 | ); |
| 8330 | 8391 | } |
| 8331 | 8392 | |
| @@ -8376,6 +8437,9 @@ int main(int argc, char **argv) · 44 unmodified lines | ||
| 8376 | 8437 | tcc_undefine_symbol(s, r + 2); |
| 8377 | 8438 | } else if (r[1] == 'L') { |
| 8378 | 8439 | tcc_add_library_path(s, r + 2); |
| 8440 | + } else if (r[1] == 'B') { | |
| 8441 | + /* set tcc utilities path (mainly for tcc development) */ | |
| 8442 | + tcc_lib_path = r + 2; | |
| 8379 | 8443 | } else if (r[1] == 'l') { |
| 8380 | 8444 | dynarray_add((void ***)&libraries, &nb_libraries, r + 2); |
| 8381 | 8445 | } else if (!strcmp(r + 1, "bench")) { |
| @@ -8436,6 +8500,14 @@ int main(int argc, char **argv) · 54 unmodified lines | ||
| 8436 | 8500 | if (outfile && output_type == TCC_OUTPUT_MEMORY) |
| 8437 | 8501 | output_type = TCC_OUTPUT_EXE; |
| 8438 | 8502 | |
| 8503 | + /* warning if not supported features */ | |
| 8504 | + if (output_type == TCC_OUTPUT_DLL) | |
| 8505 | + warning("dll output is currently not supported"); | |
| 8506 | +#ifdef CONFIG_TCC_BCHECK | |
| 8507 | + if (do_bounds_check && output_type != TCC_OUTPUT_MEMORY) | |
| 8508 | + warning("bounds checking is currently only supported for in-memory execution"); | |
| 8509 | +#endif | |
| 8510 | + | |
| 8439 | 8511 | tcc_set_output_type(s, output_type); |
| 8440 | 8512 | |
| 8441 | 8513 | tcc_add_file(s, argv[optind]); |