Unified · Split
| @@ -2,6 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | # Tiny C Compiler Makefile |
| 3 | 3 | # |
| 4 | 4 | prefix=/usr/local |
| 5 | +manpath=$(prefix)/man | |
| 5 | 6 | |
| 6 | 7 | CFLAGS=-O2 -g -Wall |
| 7 | 8 | LIBS=-ldl |
| @@ -117,7 +118,7 @@ bcheck.o: bcheck.c · 109 unmodified lines | ||
| 117 | 118 | |
| 118 | 119 | install: tcc libtcc1.o bcheck.o |
| 119 | 120 | $(INSTALL) -m755 tcc $(prefix)/bin |
| 120 | − $(INSTALL) tcc.1 $(prefix)/man/man1 | |
| 121 | + $(INSTALL) tcc.1 $(manpath)/man1 | |
| 121 | 122 | mkdir -p $(prefix)/lib/tcc |
| 122 | 123 | mkdir -p $(prefix)/lib/tcc/include |
| 123 | 124 | $(INSTALL) -m644 libtcc1.o bcheck.o $(prefix)/lib/tcc |
| @@ -63,10 +63,16 @@ int tcc_add_library(TCCState *s, const char *libraryname); · 62 unmodified lines | ||
| 63 | 63 | /* add a symbol to the compiled program */ |
| 64 | 64 | int tcc_add_symbol(TCCState *s, const char *name, unsigned long val); |
| 65 | 65 | |
| 66 | −/* output an executable file */ | |
| 66 | +/* output an executable, library or object file */ | |
| 67 | 67 | int tcc_output_file(TCCState *s, const char *filename); |
| 68 | 68 | |
| 69 | 69 | /* link and run main() function and return its value */ |
| 70 | 70 | int tcc_run(TCCState *s, int argc, char **argv); |
| 71 | 71 | |
| 72 | +/* do all relocations (needed before using tcc_get_symbol()) */ | |
| 73 | +void tcc_relocate(TCCState *s); | |
| 74 | + | |
| 75 | +/* return symbol value or error */ | |
| 76 | +void *tcc_get_symbol(TCCState *s, const char *name); | |
| 77 | + | |
| 72 | 78 | #endif |
| @@ -23,10 +23,8 @@ char my_program[] = · 22 unmodified lines | ||
| 23 | 23 | " return fib(n-1) + fib(n-2);\n" |
| 24 | 24 | "}\n" |
| 25 | 25 | "\n" |
| 26 | −"int main(int argc, char **argv)\n" | |
| 26 | +"int foo(int n)\n" | |
| 27 | 27 | "{\n" |
| 28 | −" int n;\n" | |
| 29 | −" n = atoi(argv[1]);\n" | |
| 30 | 28 | " printf(\"Hello World!\\n\");\n" |
| 31 | 29 | " printf(\"fib(%d) = %d\\n\", n, fib(n));\n" |
| 32 | 30 | " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n" |
| @@ -36,7 +34,7 @@ char my_program[] = · 3 unmodified lines | ||
| 36 | 34 | int main(int argc, char **argv) |
| 37 | 35 | { |
| 38 | 36 | TCCState *s; |
| 39 | − char *args[3]; | |
| 37 | + int (*func)(int); | |
| 40 | 38 | |
| 41 | 39 | s = tcc_new(); |
| 42 | 40 | if (!s) { |
| @@ -54,11 +52,11 @@ int main(int argc, char **argv) · 11 unmodified lines | ||
| 54 | 52 | with tcc_add_dll(() and using its symbols directly. */ |
| 55 | 53 | tcc_add_symbol(s, "add", (unsigned long)&add); |
| 56 | 54 | |
| 57 | − args[0] = ""; | |
| 58 | − args[1] = "32"; | |
| 59 | − args[2] = NULL; | |
| 55 | + tcc_relocate(s); | |
| 60 | 56 | |
| 61 | − tcc_run(s, 2, args); | |
| 57 | + func = tcc_get_symbol(s, "foo"); | |
| 58 | + | |
| 59 | + func(32); | |
| 62 | 60 | |
| 63 | 61 | tcc_delete(s); |
| 64 | 62 | return 0; |
| @@ -7070,11 +7070,10 @@ static void sig_error(int signum, siginfo_t *siginf, void *puc) · 7069 unmodified lines | ||
| 7070 | 7070 | } |
| 7071 | 7071 | #endif |
| 7072 | 7072 | |
| 7073 | −/* launch the compiled program with the given arguments */ | |
| 7074 | −int tcc_run(TCCState *s1, int argc, char **argv) | |
| 7073 | +/* do all relocations (needed before using tcc_get_symbol()) */ | |
| 7074 | +void tcc_relocate(TCCState *s1) | |
| 7075 | 7075 | { |
| 7076 | 7076 | Section *s; |
| 7077 | − int (*prog_main)(int, char **); | |
| 7078 | 7077 | int i; |
| 7079 | 7078 | |
| 7080 | 7079 | tcc_add_runtime(s1); |
| @@ -7104,8 +7103,16 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 23 unmodified lines | ||
| 7104 | 7103 | if (s->reloc) |
| 7105 | 7104 | relocate_section(s1, s); |
| 7106 | 7105 | } |
| 7106 | +} | |
| 7107 | + | |
| 7108 | +/* launch the compiled program with the given arguments */ | |
| 7109 | +int tcc_run(TCCState *s1, int argc, char **argv) | |
| 7110 | +{ | |
| 7111 | + int (*prog_main)(int, char **); | |
| 7112 | + | |
| 7113 | + tcc_relocate(s1); | |
| 7107 | 7114 | |
| 7108 | − prog_main = (void *)get_elf_sym_val("main"); | |
| 7115 | + prog_main = tcc_get_symbol(s1, "main"); | |
| 7109 | 7116 | |
| 7110 | 7117 | if (do_debug) { |
| 7111 | 7118 | #ifdef WIN32 |
| @@ -7131,11 +7138,11 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 19 unmodified lines | ||
| 7131 | 7138 | void **bound_error_func; |
| 7132 | 7139 | |
| 7133 | 7140 | /* set error function */ |
| 7134 | − bound_error_func = (void **)get_elf_sym_val("__bound_error_func"); | |
| 7141 | + bound_error_func = (void **)tcc_get_symbol(s1, "__bound_error_func"); | |
| 7135 | 7142 | *bound_error_func = rt_error; |
| 7136 | 7143 | |
| 7137 | 7144 | /* XXX: use .init section so that it also work in binary ? */ |
| 7138 | − bound_init = (void *)get_elf_sym_val("__bound_init"); | |
| 7145 | + bound_init = (void *)tcc_get_symbol(s1, "__bound_init"); | |
| 7139 | 7146 | bound_init(); |
| 7140 | 7147 | } |
| 7141 | 7148 | #endif |
| @@ -154,7 +154,7 @@ static int find_elf_sym(Section *s, const char *name) · 153 unmodified lines | ||
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /* return elf symbol value or error */ |
| 157 | −static unsigned long get_elf_sym_val(const char *name) | |
| 157 | +void *tcc_get_symbol(TCCState *s, const char *name) | |
| 158 | 158 | { |
| 159 | 159 | int sym_index; |
| 160 | 160 | Elf32_Sym *sym; |
| @@ -163,7 +163,7 @@ static unsigned long get_elf_sym_val(const char *name) · 2 unmodified lines | ||
| 163 | 163 | if (!sym_index) |
| 164 | 164 | error("%s not defined", name); |
| 165 | 165 | sym = &((Elf32_Sym *)symtab_section->data)[sym_index]; |
| 166 | − return sym->st_value; | |
| 166 | + return (void *)sym->st_value; | |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /* add an elf symbol : check if it is already defined and patch |
| @@ -1294,7 +1294,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1124 unmodified lines | ||
| 1294 | 1294 | |
| 1295 | 1295 | /* get entry point address */ |
| 1296 | 1296 | if (file_type == TCC_OUTPUT_EXE) |
| 1297 | − ehdr.e_entry = get_elf_sym_val("_start"); | |
| 1297 | + ehdr.e_entry = (unsigned long)tcc_get_symbol(s1, "_start"); | |
| 1298 | 1298 | else |
| 1299 | 1299 | ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */ |
| 1300 | 1300 | } |