History

change tcc_add/get_symbol to use void*

grischka committed 17 years ago

Commit b1697be1 parent 795f6744 files · +19 −22Browse files

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t