change tcc_add/get_symbol to use void*

This commit is contained in:
grischka 2009-04-16 21:50:43 +02:00
parent 795f67428e
commit b1697be691
4 changed files with 19 additions and 22 deletions

View File

@ -74,7 +74,7 @@ int tcc_add_library_path(TCCState *s, const char *pathname);
int tcc_add_library(TCCState *s, const char *libraryname); int tcc_add_library(TCCState *s, const char *libraryname);
/* add a symbol to the compiled program */ /* 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 /* output an executable, library or object file. DO NOT call
tcc_relocate() before. */ 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 */ returns -1 on error and required size if ptr is NULL */
int tcc_relocate(TCCState *s1, void *ptr); int tcc_relocate(TCCState *s1, void *ptr);
/* return symbol value. return 0 if OK, -1 if symbol not found */ /* return symbol value or NULL if not found */
int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name); void *tcc_get_symbol(TCCState *s, const char *name);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -35,7 +35,6 @@ int main(int argc, char **argv)
{ {
TCCState *s; TCCState *s;
int (*func)(int); int (*func)(int);
unsigned long val;
void *mem; void *mem;
int size; int size;
@ -50,10 +49,9 @@ int main(int argc, char **argv)
tcc_compile_string(s, my_program); tcc_compile_string(s, my_program);
/* as a test, we add a symbol that the compiled program can be /* as a test, we add a symbol that the compiled program can use.
linked with. You can have a similar result by opening a dll You may also open a dll with tcc_add_dll() and use symbols from that */
with tcc_add_dll(() and using its symbols directly. */ tcc_add_symbol(s, "add", add);
tcc_add_symbol(s, "add", (unsigned long)&add);
/* get needed size of the code */ /* get needed size of the code */
size = tcc_relocate(s, NULL); size = tcc_relocate(s, NULL);
@ -65,8 +63,9 @@ int main(int argc, char **argv)
tcc_relocate(s, mem); tcc_relocate(s, mem);
/* get entry symbol */ /* get entry symbol */
tcc_get_symbol(s, &val, "foo"); func = tcc_get_symbol(s, "foo");
func = (void *)val; if (!func)
return 1;
/* delete the state */ /* delete the state */
tcc_delete(s); tcc_delete(s);

7
tcc.c
View File

@ -10398,8 +10398,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
void (*bound_init)(void); void (*bound_init)(void);
/* set error function */ /* set error function */
rt_bound_error_msg = (void *)tcc_get_symbol_err(s1, rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
"__bound_error_msg");
/* XXX: use .init section so that it also work in binary ? */ /* XXX: use .init section so that it also work in binary ? */
bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init"); 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; 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, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
SHN_ABS, name); SHN_ABS, name);
return 0; return 0;

View File

@ -169,25 +169,24 @@ static int find_elf_sym(Section *s, const char *name)
} }
/* return elf symbol value or error */ /* 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; int sym_index;
ElfW(Sym) *sym; ElfW(Sym) *sym;
sym_index = find_elf_sym(symtab_section, name); sym_index = find_elf_sym(symtab_section, name);
if (!sym_index) if (!sym_index)
return -1; return NULL;
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
*pval = sym->st_value; return (void*)sym->st_value;
return 0;
} }
void *tcc_get_symbol_err(TCCState *s, const char *name) void *tcc_get_symbol_err(TCCState *s, const char *name)
{ {
unsigned long val; void *sym;
if (tcc_get_symbol(s, &val, name) < 0) sym = tcc_get_symbol(s, name);
if (!sym)
error("%s not defined", name); error("%s not defined", name);
return (void *)val; return sym;
} }
/* add an elf symbol : check if it is already defined and patch /* add an elf symbol : check if it is already defined and patch