History

added tcc_relocate() and tcc_get_symbol()

bellard committed 24 years ago

Commit d8ef07d1 parent 10f5d445 files · +31 −19Browse files

Unified · Split

5 files
  1. Makefile +2 −1
  2. libtcc.h +7 −1
  3. libtcc_test.c +6 −8
  4. tcc.c +13 −6
  5. tccelf.c +3 −3
Makefile+2−1
@@ -2,6 +2,7 @@ · 1 unmodified line
2# Tiny C Compiler Makefile2# Tiny C Compiler Makefile
3#3#
4prefix=/usr/local4prefix=/usr/local
5manpath=$(prefix)/man
5 6
6CFLAGS=-O2 -g -Wall7CFLAGS=-O2 -g -Wall
7LIBS=-ldl8LIBS=-ldl
@@ -117,7 +118,7 @@ bcheck.o: bcheck.c · 109 unmodified lines
117 118
118install: tcc libtcc1.o bcheck.o119install: tcc libtcc1.o bcheck.o
119 $(INSTALL) -m755 tcc $(prefix)/bin120 $(INSTALL) -m755 tcc $(prefix)/bin
120 $(INSTALL) tcc.1 $(prefix)/man/man1121 $(INSTALL) tcc.1 $(manpath)/man1
121 mkdir -p $(prefix)/lib/tcc122 mkdir -p $(prefix)/lib/tcc
122 mkdir -p $(prefix)/lib/tcc/include123 mkdir -p $(prefix)/lib/tcc/include
123 $(INSTALL) -m644 libtcc1.o bcheck.o $(prefix)/lib/tcc124 $(INSTALL) -m644 libtcc1.o bcheck.o $(prefix)/lib/tcc
libtcc.h+7−1
@@ -63,10 +63,16 @@ int tcc_add_library(TCCState *s, const char *libraryname); · 62 unmodified lines
63/* add a symbol to the compiled program */63/* add a symbol to the compiled program */
64int tcc_add_symbol(TCCState *s, const char *name, unsigned long val);64int tcc_add_symbol(TCCState *s, const char *name, unsigned long val);
65 65
66/* output an executable file */66/* output an executable, library or object file */
67int tcc_output_file(TCCState *s, const char *filename);67int tcc_output_file(TCCState *s, const char *filename);
68 68
69/* link and run main() function and return its value */69/* link and run main() function and return its value */
70int tcc_run(TCCState *s, int argc, char **argv);70int tcc_run(TCCState *s, int argc, char **argv);
71 71
72/* do all relocations (needed before using tcc_get_symbol()) */
73void tcc_relocate(TCCState *s);
74
75/* return symbol value or error */
76void *tcc_get_symbol(TCCState *s, const char *name);
77
72#endif78#endif
libtcc_test.c+6−8
@@ -23,10 +23,8 @@ char my_program[] = · 22 unmodified lines
23" return fib(n-1) + fib(n-2);\n"23" return fib(n-1) + fib(n-2);\n"
24"}\n"24"}\n"
25"\n"25"\n"
26"int main(int argc, char **argv)\n"26"int foo(int n)\n"
27"{\n"27"{\n"
28" int n;\n"
29" n = atoi(argv[1]);\n"
30" printf(\"Hello World!\\n\");\n"28" printf(\"Hello World!\\n\");\n"
31" printf(\"fib(%d) = %d\\n\", n, fib(n));\n"29" printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
32" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"30" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
@@ -36,7 +34,7 @@ char my_program[] = · 3 unmodified lines
36int main(int argc, char **argv)34int main(int argc, char **argv)
37{35{
38 TCCState *s;36 TCCState *s;
39 char *args[3];37 int (*func)(int);
40 38
41 s = tcc_new();39 s = tcc_new();
42 if (!s) {40 if (!s) {
@@ -54,11 +52,11 @@ int main(int argc, char **argv) · 11 unmodified lines
54 with tcc_add_dll(() and using its symbols directly. */52 with tcc_add_dll(() and using its symbols directly. */
55 tcc_add_symbol(s, "add", (unsigned long)&add);53 tcc_add_symbol(s, "add", (unsigned long)&add);
56 54
57 args[0] = "";55 tcc_relocate(s);
58 args[1] = "32";
59 args[2] = NULL;
60 56
61 tcc_run(s, 2, args);57 func = tcc_get_symbol(s, "foo");
58
59 func(32);
62 60
63 tcc_delete(s);61 tcc_delete(s);
64 return 0;62 return 0;
tcc.c+13−6
@@ -7070,11 +7070,10 @@ static void sig_error(int signum, siginfo_t *siginf, void *puc) · 7069 unmodified lines
7070}7070}
7071#endif7071#endif
7072 7072
7073/* launch the compiled program with the given arguments */7073/* do all relocations (needed before using tcc_get_symbol()) */
7074int tcc_run(TCCState *s1, int argc, char **argv)7074void tcc_relocate(TCCState *s1)
7075{7075{
7076 Section *s;7076 Section *s;
7077 int (*prog_main)(int, char **);
7078 int i;7077 int i;
7079 7078
7080 tcc_add_runtime(s1);7079 tcc_add_runtime(s1);
@@ -7104,8 +7103,16 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 23 unmodified lines
7104 if (s->reloc)7103 if (s->reloc)
7105 relocate_section(s1, s);7104 relocate_section(s1, s);
7106 }7105 }
7106}
7107
7108/* launch the compiled program with the given arguments */
7109int tcc_run(TCCState *s1, int argc, char **argv)
7110{
7111 int (*prog_main)(int, char **);
7112
7113 tcc_relocate(s1);
7107 7114
7108 prog_main = (void *)get_elf_sym_val("main");7115 prog_main = tcc_get_symbol(s1, "main");
7109 7116
7110 if (do_debug) {7117 if (do_debug) {
7111#ifdef WIN327118#ifdef WIN32
@@ -7131,11 +7138,11 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 19 unmodified lines
7131 void **bound_error_func;7138 void **bound_error_func;
7132 7139
7133 /* set error function */7140 /* set error function */
7134 bound_error_func = (void **)get_elf_sym_val("__bound_error_func");7141 bound_error_func = (void **)tcc_get_symbol(s1, "__bound_error_func");
7135 *bound_error_func = rt_error;7142 *bound_error_func = rt_error;
7136 7143
7137 /* XXX: use .init section so that it also work in binary ? */7144 /* XXX: use .init section so that it also work in binary ? */
7138 bound_init = (void *)get_elf_sym_val("__bound_init");7145 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
7139 bound_init();7146 bound_init();
7140 }7147 }
7141#endif7148#endif
tccelf.c+3−3
@@ -154,7 +154,7 @@ static int find_elf_sym(Section *s, const char *name) · 153 unmodified lines
154}154}
155 155
156/* return elf symbol value or error */156/* return elf symbol value or error */
157static unsigned long get_elf_sym_val(const char *name)157void *tcc_get_symbol(TCCState *s, const char *name)
158{158{
159 int sym_index;159 int sym_index;
160 Elf32_Sym *sym;160 Elf32_Sym *sym;
@@ -163,7 +163,7 @@ static unsigned long get_elf_sym_val(const char *name) · 2 unmodified lines
163 if (!sym_index)163 if (!sym_index)
164 error("%s not defined", name);164 error("%s not defined", name);
165 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];165 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
166 return sym->st_value;166 return (void *)sym->st_value;
167}167}
168 168
169/* add an elf symbol : check if it is already defined and patch169/* add an elf symbol : check if it is already defined and patch
@@ -1294,7 +1294,7 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1124 unmodified lines
1294 1294
1295 /* get entry point address */1295 /* get entry point address */
1296 if (file_type == TCC_OUTPUT_EXE)1296 if (file_type == TCC_OUTPUT_EXE)
1297 ehdr.e_entry = get_elf_sym_val("_start");1297 ehdr.e_entry = (unsigned long)tcc_get_symbol(s1, "_start");
1298 else1298 else
1299 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */1299 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
1300 }1300 }

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