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