Unified · Split
| @@ -74,7 +74,7 @@ int tcc_add_library_path(TCCState *s, const char *pathname); · 73 unmodified lines | |||
| 74 | int tcc_add_library(TCCState *s, const char *libraryname); | 74 | int tcc_add_library(TCCState *s, const char *libraryname); |
| 75 | 75 | ||
| 76 | /* add a symbol to the compiled program */ | 76 | /* add a symbol to the compiled program */ |
| 77 | int tcc_add_symbol(TCCState *s, const char *name, unsigned long val); | 77 | int tcc_add_symbol(TCCState *s, const char *name, void *val); |
| 78 | 78 | ||
| 79 | /* output an executable, library or object file. DO NOT call | 79 | /* output an executable, library or object file. DO NOT call |
| 80 | tcc_relocate() before. */ | 80 | tcc_relocate() before. */ |
| @@ -89,8 +89,8 @@ int tcc_run(TCCState *s, int argc, char **argv); · 8 unmodified lines | |||
| 89 | returns -1 on error and required size if ptr is NULL */ | 89 | returns -1 on error and required size if ptr is NULL */ |
| 90 | int tcc_relocate(TCCState *s1, void *ptr); | 90 | int tcc_relocate(TCCState *s1, void *ptr); |
| 91 | 91 | ||
| 92 | /* return symbol value. return 0 if OK, -1 if symbol not found */ | 92 | /* return symbol value or NULL if not found */ |
| 93 | int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name); | 93 | void *tcc_get_symbol(TCCState *s, const char *name); |
| 94 | 94 | ||
| 95 | #ifdef __cplusplus | 95 | #ifdef __cplusplus |
| 96 | } | 96 | } |
| @@ -35,7 +35,6 @@ int main(int argc, char **argv) · 34 unmodified lines | |||
| 35 | { | 35 | { |
| 36 | TCCState *s; | 36 | TCCState *s; |
| 37 | int (*func)(int); | 37 | int (*func)(int); |
| 38 | unsigned long val; | ||
| 39 | void *mem; | 38 | void *mem; |
| 40 | int size; | 39 | int size; |
| 41 | 40 | ||
| @@ -50,10 +49,9 @@ int main(int argc, char **argv) · 8 unmodified lines | |||
| 50 | 49 | ||
| 51 | tcc_compile_string(s, my_program); | 50 | tcc_compile_string(s, my_program); |
| 52 | 51 | ||
| 53 | /* as a test, we add a symbol that the compiled program can be | 52 | /* as a test, we add a symbol that the compiled program can use. |
| 54 | linked with. You can have a similar result by opening a dll | 53 | You may also open a dll with tcc_add_dll() and use symbols from that */ |
| 55 | with tcc_add_dll(() and using its symbols directly. */ | 54 | tcc_add_symbol(s, "add", add); |
| 56 | tcc_add_symbol(s, "add", (unsigned long)&add); | ||
| 57 | 55 | ||
| 58 | /* get needed size of the code */ | 56 | /* get needed size of the code */ |
| 59 | size = tcc_relocate(s, NULL); | 57 | size = tcc_relocate(s, NULL); |
| @@ -65,8 +63,9 @@ int main(int argc, char **argv) · 5 unmodified lines | |||
| 65 | tcc_relocate(s, mem); | 63 | tcc_relocate(s, mem); |
| 66 | 64 | ||
| 67 | /* get entry symbol */ | 65 | /* get entry symbol */ |
| 68 | tcc_get_symbol(s, &val, "foo"); | 66 | func = tcc_get_symbol(s, "foo"); |
| 69 | func = (void *)val; | 67 | if (!func) |
| 68 | return 1; | ||
| 70 | 69 | ||
| 71 | /* delete the state */ | 70 | /* delete the state */ |
| 72 | tcc_delete(s); | 71 | tcc_delete(s); |
| @@ -10398,8 +10398,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 10397 unmodified lines | |||
| 10398 | void (*bound_init)(void); | 10398 | void (*bound_init)(void); |
| 10399 | 10399 | ||
| 10400 | /* set error function */ | 10400 | /* set error function */ |
| 10401 | rt_bound_error_msg = (void *)tcc_get_symbol_err(s1, | 10401 | rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); |
| 10402 | "__bound_error_msg"); | ||
| 10403 | 10402 | ||
| 10404 | /* XXX: use .init section so that it also work in binary ? */ | 10403 | /* XXX: use .init section so that it also work in binary ? */ |
| 10405 | bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init"); | 10404 | bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init"); |
| @@ -10793,9 +10792,9 @@ int tcc_add_library(TCCState *s, const char *libraryname) · 387 unmodified lines | |||
| 10793 | return -1; | 10792 | return -1; |
| 10794 | } | 10793 | } |
| 10795 | 10794 | ||
| 10796 | int tcc_add_symbol(TCCState *s, const char *name, unsigned long val) | 10795 | int tcc_add_symbol(TCCState *s, const char *name, void *val) |
| 10797 | { | 10796 | { |
| 10798 | add_elf_sym(symtab_section, val, 0, | 10797 | add_elf_sym(symtab_section, (unsigned long)val, 0, |
| 10799 | ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, | 10798 | ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, |
| 10800 | SHN_ABS, name); | 10799 | SHN_ABS, name); |
| 10801 | return 0; | 10800 | return 0; |
| @@ -169,25 +169,24 @@ static int find_elf_sym(Section *s, const char *name) · 168 unmodified lines | |||
| 169 | } | 169 | } |
| 170 | 170 | ||
| 171 | /* return elf symbol value or error */ | 171 | /* return elf symbol value or error */ |
| 172 | int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name) | 172 | void *tcc_get_symbol(TCCState *s, const char *name) |
| 173 | { | 173 | { |
| 174 | int sym_index; | 174 | int sym_index; |
| 175 | ElfW(Sym) *sym; | 175 | ElfW(Sym) *sym; |
| 176 | |||
| 177 | sym_index = find_elf_sym(symtab_section, name); | 176 | sym_index = find_elf_sym(symtab_section, name); |
| 178 | if (!sym_index) | 177 | if (!sym_index) |
| 179 | return -1; | 178 | return NULL; |
| 180 | sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; | 179 | sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; |
| 181 | *pval = sym->st_value; | 180 | return (void*)sym->st_value; |
| 182 | return 0; | ||
| 183 | } | 181 | } |
| 184 | 182 | ||
| 185 | void *tcc_get_symbol_err(TCCState *s, const char *name) | 183 | void *tcc_get_symbol_err(TCCState *s, const char *name) |
| 186 | { | 184 | { |
| 187 | unsigned long val; | 185 | void *sym; |
| 188 | if (tcc_get_symbol(s, &val, name) < 0) | 186 | sym = tcc_get_symbol(s, name); |
| 187 | if (!sym) | ||
| 189 | error("%s not defined", name); | 188 | error("%s not defined", name); |
| 190 | return (void *)val; | 189 | return sym; |
| 191 | } | 190 | } |
| 192 | 191 | ||
| 193 | /* add an elf symbol : check if it is already defined and patch | 192 | /* add an elf symbol : check if it is already defined and patch |