History

fixed command line help - added -B option - moved bcheck.c outside of tcc

bellard committed 24 years ago

Commit b12e91a1 parent 6cdecbe1 file · +110 −38Browse files

Unified · Split

tcc.c+110−38
@@ -25,7 +25,6 @@ · 24 unmodified lines
2525 #include <math.h>
2626 #include <unistd.h>
2727 #include <signal.h>
28−#include <malloc.h>
2928 #include <unistd.h>
3029 #include <fcntl.h>
3130 #ifndef WIN32
@@ -307,6 +306,9 @@ int tcc_ext = 1; · 275 unmodified lines
307306 /* if true, static linking is performed */
308307 int static_link = 0;
309308
309+/* give the path of the tcc libraries */
310+static const char *tcc_lib_path = CONFIG_TCC_PREFIX "/lib/tcc";
311+
310312 struct TCCState {
311313 int output_type;
312314 };
@@ -586,6 +588,8 @@ void type_to_str(char *buf, int buf_size, · 273 unmodified lines
586588 int t, const char *varstr);
587589 char *get_tok_str(int v, CValue *cv);
588590 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);
589593
590594 /* section generation */
591595 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
613617 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
614618 }
615619
616−#ifdef CONFIG_TCC_BCHECK
617−#include "bcheck.c"
618−#endif
619−
620620 #ifdef TCC_TARGET_I386
621621 #include "i386-gen.c"
622622 #endif
@@ -2494,18 +2494,27 @@ void vpushi(int v) · 1871 unmodified lines
24942494 vsetc(VT_INT, VT_CONST, &cval);
24952495 }
24962496
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)
24992500 {
25002501 int v;
25012502 Sym *sym;
2502− CValue cval;
25032503
25042504 v = anon_sym++;
25052505 sym = sym_push1(&global_stack, v, t | VT_STATIC, 0);
25062506 sym->r = VT_CONST | VT_SYM;
25072507 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);
25092518 vsetc(t, VT_CONST | VT_SYM, &cval);
25102519 }
25112520
@@ -6397,7 +6406,8 @@ void put_extern_sym(Sym *sym, Section *section, unsigned long value) · 3885 unmodified lines
63976406 int sym_type, sym_bind, sh_num, info;
63986407 Elf32_Sym *esym;
63996408 const char *name;
6400−
6409+ char buf[32];
6410+
64016411 if (section)
64026412 sh_num = section->sh_num;
64036413 else
@@ -6411,7 +6421,30 @@ void put_extern_sym(Sym *sym, Section *section, unsigned long value) · 7 unmodified lines
64116421 sym_bind = STB_LOCAL;
64126422 else
64136423 sym_bind = STB_GLOBAL;
6424+
64146425 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
64156448 info = ELF32_ST_INFO(sym_bind, sym_type);
64166449 sym->c = add_elf_sym(symtab_section, value, 0, info, sh_num, name);
64176450 } else {
@@ -6576,14 +6609,6 @@ static void relocate_common_syms(void) · 158 unmodified lines
65766609
65776610 static void *resolve_sym(const char *sym)
65786611 {
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
65876612 return dlsym(NULL, sym);
65886613 }
65896614
@@ -6867,7 +6892,44 @@ static void put_dt(Section *dynamic, int dt, unsigned long val) · 277 unmodified lines
68676892 /* add tcc runtime libraries */
68686893 static void tcc_add_runtime(TCCState *s1)
68696894 {
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");
68716933 }
68726934
68736935 /* 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
68986960
68996961 file_type = s1->output_type;
69006962
6901− /* add libc crtn object */
6902− if (file_type != TCC_OUTPUT_OBJ) {
6963+ if (file_type != TCC_OUTPUT_OBJ)
69036964 tcc_add_runtime(s1);
6904− tcc_add_library(s1, "c");
6905− tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o");
6906− }
69076965
69086966 interp = NULL;
69096967 dynamic = NULL;
@@ -8012,6 +8070,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 1102 unmodified lines
80128070 {
80138071 Section *s;
80148072 int (*prog_main)(int, char **);
8073+ void (*bound_init)(void);
80158074 int i;
80168075
80178076 tcc_add_runtime(s1);
@@ -8056,18 +8115,11 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 38 unmodified lines
80568115
80578116 #ifdef CONFIG_TCC_BCHECK
80588117 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();
80688121 }
80698122 #endif
8070−
80718123 return (*prog_main)(argc, argv);
80728124 }
80738125
@@ -8283,6 +8335,14 @@ int tcc_add_library(TCCState *s, const char *libraryname) · 209 unmodified lines
82838335 return -1;
82848336 }
82858337
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+
82868346 int tcc_set_output_type(TCCState *s, int output_type)
82878347 {
82888348 s->output_type = output_type;
@@ -8302,16 +8362,17 @@ int tcc_set_output_type(TCCState *s, int output_type) · 13 unmodified lines
83028362 void help(void)
83038363 {
83048364 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"
83068366 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
83078367 " [--] infile1 [infile2... --] [infile_args...]\n"
83088368 "\n"
83098369 "General options:\n"
83108370 " -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"
83138372 " -- allows multiples input files if no -o option given. Also\n"
83148373 " separate input files from runtime arguments\n"
8374+ " -Bdir set tcc internal library path\n"
8375+ " -bench output compilation statistics\n"
83158376 "Preprocessor options:\n"
83168377 " -Idir add include path 'dir'\n"
83178378 " -Dsym[=val] define 'sym' with value 'val'\n"
@@ -8325,7 +8386,7 @@ void help(void) · 7 unmodified lines
83258386 " -Ldir add library path 'dir'\n"
83268387 " -llib link with dynamic library 'lib'\n"
83278388 " -shared generate a shared library (NOT WORKING YET)\n"
8328− " -static static linking (NOT WORKING YET)\n"
8389+ " -static static linking\n"
83298390 );
83308391 }
83318392
@@ -8376,6 +8437,9 @@ int main(int argc, char **argv) · 44 unmodified lines
83768437 tcc_undefine_symbol(s, r + 2);
83778438 } else if (r[1] == 'L') {
83788439 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;
83798443 } else if (r[1] == 'l') {
83808444 dynarray_add((void ***)&libraries, &nb_libraries, r + 2);
83818445 } else if (!strcmp(r + 1, "bench")) {
@@ -8436,6 +8500,14 @@ int main(int argc, char **argv) · 54 unmodified lines
84368500 if (outfile && output_type == TCC_OUTPUT_MEMORY)
84378501 output_type = TCC_OUTPUT_EXE;
84388502
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+
84398511 tcc_set_output_type(s, output_type);
84408512
84418513 tcc_add_file(s, argv[optind]);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t