change tcc_add/get_symbol to use void*

tcc-xref
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);
/* 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
}

View File

@ -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);

7
tcc.c
View File

@ -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;

View File

@ -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