From b1697be6917ecf16013d8467329bccff6a9e5b9f Mon Sep 17 00:00:00 2001 From: grischka Date: Thu, 16 Apr 2009 21:50:43 +0200 Subject: [PATCH] change tcc_add/get_symbol to use void* --- libtcc.h | 6 +++--- libtcc_test.c | 13 ++++++------- tcc.c | 7 +++---- tccelf.c | 15 +++++++-------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/libtcc.h b/libtcc.h index 48c14585..e7c7ae11 100644 --- a/libtcc.h +++ b/libtcc.h @@ -74,7 +74,7 @@ int tcc_add_library_path(TCCState *s, const char *pathname); int tcc_add_library(TCCState *s, const char *libraryname); /* add a symbol to the compiled program */ -int tcc_add_symbol(TCCState *s, const char *name, unsigned long val); +int tcc_add_symbol(TCCState *s, const char *name, void *val); /* output an executable, library or object file. DO NOT call tcc_relocate() before. */ @@ -89,8 +89,8 @@ int tcc_run(TCCState *s, int argc, char **argv); returns -1 on error and required size if ptr is NULL */ int tcc_relocate(TCCState *s1, void *ptr); -/* return symbol value. return 0 if OK, -1 if symbol not found */ -int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name); +/* return symbol value or NULL if not found */ +void *tcc_get_symbol(TCCState *s, const char *name); #ifdef __cplusplus } diff --git a/libtcc_test.c b/libtcc_test.c index 4c16902a..d257a725 100644 --- a/libtcc_test.c +++ b/libtcc_test.c @@ -35,7 +35,6 @@ int main(int argc, char **argv) { TCCState *s; int (*func)(int); - unsigned long val; void *mem; int size; @@ -50,10 +49,9 @@ int main(int argc, char **argv) tcc_compile_string(s, my_program); - /* as a test, we add a symbol that the compiled program can be - linked with. You can have a similar result by opening a dll - with tcc_add_dll(() and using its symbols directly. */ - tcc_add_symbol(s, "add", (unsigned long)&add); + /* as a test, we add a symbol that the compiled program can use. + You may also open a dll with tcc_add_dll() and use symbols from that */ + tcc_add_symbol(s, "add", add); /* get needed size of the code */ size = tcc_relocate(s, NULL); @@ -65,8 +63,9 @@ int main(int argc, char **argv) tcc_relocate(s, mem); /* get entry symbol */ - tcc_get_symbol(s, &val, "foo"); - func = (void *)val; + func = tcc_get_symbol(s, "foo"); + if (!func) + return 1; /* delete the state */ tcc_delete(s); diff --git a/tcc.c b/tcc.c index 56cc0a66..96c941c3 100644 --- a/tcc.c +++ b/tcc.c @@ -10398,8 +10398,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) void (*bound_init)(void); /* set error function */ - rt_bound_error_msg = (void *)tcc_get_symbol_err(s1, - "__bound_error_msg"); + rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); /* XXX: use .init section so that it also work in binary ? */ bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init"); @@ -10793,9 +10792,9 @@ int tcc_add_library(TCCState *s, const char *libraryname) return -1; } -int tcc_add_symbol(TCCState *s, const char *name, unsigned long val) +int tcc_add_symbol(TCCState *s, const char *name, void *val) { - add_elf_sym(symtab_section, val, 0, + add_elf_sym(symtab_section, (unsigned long)val, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, SHN_ABS, name); return 0; diff --git a/tccelf.c b/tccelf.c index 87c91c3d..6a26831b 100644 --- a/tccelf.c +++ b/tccelf.c @@ -169,25 +169,24 @@ static int find_elf_sym(Section *s, const char *name) } /* return elf symbol value or error */ -int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name) +void *tcc_get_symbol(TCCState *s, const char *name) { int sym_index; ElfW(Sym) *sym; - sym_index = find_elf_sym(symtab_section, name); if (!sym_index) - return -1; + return NULL; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; - *pval = sym->st_value; - return 0; + return (void*)sym->st_value; } void *tcc_get_symbol_err(TCCState *s, const char *name) { - unsigned long val; - if (tcc_get_symbol(s, &val, name) < 0) + void *sym; + sym = tcc_get_symbol(s, name); + if (!sym) error("%s not defined", name); - return (void *)val; + return sym; } /* add an elf symbol : check if it is already defined and patch -- 2.11.4.GIT