mirror of
https://github.com/frida/tinycc
synced 2024-12-25 14:36:49 +03:00
change tcc_add/get_symbol to use void*
This commit is contained in:
parent
795f67428e
commit
b1697be691
6
libtcc.h
6
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);
|
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
|
||||||
}
|
}
|
||||||
|
@ -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
7
tcc.c
@ -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;
|
||||||
|
15
tccelf.c
15
tccelf.c
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user