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
7474 int tcc_add_library(TCCState *s, const char *libraryname);
7575
7676 /* add a symbol to the compiled program */
77−int tcc_add_symbol(TCCState *s, const char *name, unsigned long val);
77+int tcc_add_symbol(TCCState *s, const char *name, void *val);
7878
7979 /* output an executable, library or object file. DO NOT call
8080 tcc_relocate() before. */
@@ -89,8 +89,8 @@ int tcc_run(TCCState *s, int argc, char **argv); · 8 unmodified lines
8989 returns -1 on error and required size if ptr is NULL */
9090 int tcc_relocate(TCCState *s1, void *ptr);
9191
92−/* return symbol value. return 0 if OK, -1 if symbol not found */
93−int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name);
92+/* return symbol value or NULL if not found */
93+void *tcc_get_symbol(TCCState *s, const char *name);
9494
9595 #ifdef __cplusplus
9696 }
libtcc_test.c+6−7
@@ -35,7 +35,6 @@ int main(int argc, char **argv) · 34 unmodified lines
3535 {
3636 TCCState *s;
3737 int (*func)(int);
38− unsigned long val;
3938 void *mem;
4039 int size;
4140
@@ -50,10 +49,9 @@ int main(int argc, char **argv) · 8 unmodified lines
5049
5150 tcc_compile_string(s, my_program);
5251
53− /* as a test, we add a symbol that the compiled program can be
54− linked with. You can have a similar result by opening a dll
55− with tcc_add_dll(() and using its symbols directly. */
56− tcc_add_symbol(s, "add", (unsigned long)&add);
52+ /* as a test, we add a symbol that the compiled program can use.
53+ You may also open a dll with tcc_add_dll() and use symbols from that */
54+ tcc_add_symbol(s, "add", add);
5755
5856 /* get needed size of the code */
5957 size = tcc_relocate(s, NULL);
@@ -65,8 +63,9 @@ int main(int argc, char **argv) · 5 unmodified lines
6563 tcc_relocate(s, mem);
6664
6765 /* get entry symbol */
68− tcc_get_symbol(s, &val, "foo");
69− func = (void *)val;
66+ func = tcc_get_symbol(s, "foo");
67+ if (!func)
68+ return 1;
7069
7170 /* delete the state */
7271 tcc_delete(s);
tcc.c+3−4
@@ -10398,8 +10398,7 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 10397 unmodified lines
1039810398 void (*bound_init)(void);
1039910399
1040010400 /* set error function */
10401− rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10402− "__bound_error_msg");
10401+ rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
1040310402
1040410403 /* XXX: use .init section so that it also work in binary ? */
1040510404 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
1079310792 return -1;
1079410793 }
1079510794
10796−int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10795+int tcc_add_symbol(TCCState *s, const char *name, void *val)
1079710796 {
10798− add_elf_sym(symtab_section, val, 0,
10797+ add_elf_sym(symtab_section, (unsigned long)val, 0,
1079910798 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1080010799 SHN_ABS, name);
1080110800 return 0;
tccelf.c+7−8
@@ -169,25 +169,24 @@ static int find_elf_sym(Section *s, const char *name) · 168 unmodified lines
169169 }
170170
171171 /* return elf symbol value or error */
172−int tcc_get_symbol(TCCState *s, unsigned long *pval, const char *name)
172+void *tcc_get_symbol(TCCState *s, const char *name)
173173 {
174174 int sym_index;
175175 ElfW(Sym) *sym;
176−
177176 sym_index = find_elf_sym(symtab_section, name);
178177 if (!sym_index)
179− return -1;
178+ return NULL;
180179 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
181− *pval = sym->st_value;
182− return 0;
180+ return (void*)sym->st_value;
183181 }
184182
185183 void *tcc_get_symbol_err(TCCState *s, const char *name)
186184 {
187− unsigned long val;
188− if (tcc_get_symbol(s, &val, name) < 0)
185+ void *sym;
186+ sym = tcc_get_symbol(s, name);
187+ if (!sym)
189188 error("%s not defined", name);
190− return (void *)val;
189+ return sym;
191190 }
192191
193192 /* 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