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
22 # Tiny C Compiler Makefile
33 #
44 prefix=/usr/local
5+manpath=$(prefix)/man
56
67 CFLAGS=-O2 -g -Wall
78 LIBS=-ldl
@@ -117,7 +118,7 @@ bcheck.o: bcheck.c · 109 unmodified lines
117118
118119 install: tcc libtcc1.o bcheck.o
119120 $(INSTALL) -m755 tcc $(prefix)/bin
120− $(INSTALL) tcc.1 $(prefix)/man/man1
121+ $(INSTALL) tcc.1 $(manpath)/man1
121122 mkdir -p $(prefix)/lib/tcc
122123 mkdir -p $(prefix)/lib/tcc/include
123124 $(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
6363 /* add a symbol to the compiled program */
6464 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val);
6565
66−/* output an executable file */
66+/* output an executable, library or object file */
6767 int tcc_output_file(TCCState *s, const char *filename);
6868
6969 /* link and run main() function and return its value */
7070 int tcc_run(TCCState *s, int argc, char **argv);
7171
72+/* do all relocations (needed before using tcc_get_symbol()) */
73+void tcc_relocate(TCCState *s);
74+
75+/* return symbol value or error */
76+void *tcc_get_symbol(TCCState *s, const char *name);
77+
7278 #endif
libtcc_test.c+6−8
@@ -23,10 +23,8 @@ char my_program[] = · 22 unmodified lines
2323 " return fib(n-1) + fib(n-2);\n"
2424 "}\n"
2525 "\n"
26−"int main(int argc, char **argv)\n"
26+"int foo(int n)\n"
2727 "{\n"
28−" int n;\n"
29−" n = atoi(argv[1]);\n"
3028 " printf(\"Hello World!\\n\");\n"
3129 " printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
3230 " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
@@ -36,7 +34,7 @@ char my_program[] = · 3 unmodified lines
3634 int main(int argc, char **argv)
3735 {
3836 TCCState *s;
39− char *args[3];
37+ int (*func)(int);
4038
4139 s = tcc_new();
4240 if (!s) {
@@ -54,11 +52,11 @@ int main(int argc, char **argv) · 11 unmodified lines
5452 with tcc_add_dll(() and using its symbols directly. */
5553 tcc_add_symbol(s, "add", (unsigned long)&add);
5654
57− args[0] = "";
58− args[1] = "32";
59− args[2] = NULL;
55+ tcc_relocate(s);
6056
61− tcc_run(s, 2, args);
57+ func = tcc_get_symbol(s, "foo");
58+
59+ func(32);
6260
6361 tcc_delete(s);
6462 return 0;
tcc.c+13−6
@@ -7070,11 +7070,10 @@ static void sig_error(int signum, siginfo_t *siginf, void *puc) · 7069 unmodified lines
70707070 }
70717071 #endif
70727072
7073−/* launch the compiled program with the given arguments */
7074−int tcc_run(TCCState *s1, int argc, char **argv)
7073+/* do all relocations (needed before using tcc_get_symbol()) */
7074+void tcc_relocate(TCCState *s1)
70757075 {
70767076 Section *s;
7077− int (*prog_main)(int, char **);
70787077 int i;
70797078
70807079 tcc_add_runtime(s1);
@@ -7104,8 +7103,16 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 23 unmodified lines
71047103 if (s->reloc)
71057104 relocate_section(s1, s);
71067105 }
7106+}
7107+
7108+/* launch the compiled program with the given arguments */
7109+int tcc_run(TCCState *s1, int argc, char **argv)
7110+{
7111+ int (*prog_main)(int, char **);
7112+
7113+ tcc_relocate(s1);
71077114
7108− prog_main = (void *)get_elf_sym_val("main");
7115+ prog_main = tcc_get_symbol(s1, "main");
71097116
71107117 if (do_debug) {
71117118 #ifdef WIN32
@@ -7131,11 +7138,11 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 19 unmodified lines
71317138 void **bound_error_func;
71327139
71337140 /* 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");
71357142 *bound_error_func = rt_error;
71367143
71377144 /* 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");
71397146 bound_init();
71407147 }
71417148 #endif
tccelf.c+3−3
@@ -154,7 +154,7 @@ static int find_elf_sym(Section *s, const char *name) · 153 unmodified lines
154154 }
155155
156156 /* return elf symbol value or error */
157−static unsigned long get_elf_sym_val(const char *name)
157+void *tcc_get_symbol(TCCState *s, const char *name)
158158 {
159159 int sym_index;
160160 Elf32_Sym *sym;
@@ -163,7 +163,7 @@ static unsigned long get_elf_sym_val(const char *name) · 2 unmodified lines
163163 if (!sym_index)
164164 error("%s not defined", name);
165165 sym = &((Elf32_Sym *)symtab_section->data)[sym_index];
166− return sym->st_value;
166+ return (void *)sym->st_value;
167167 }
168168
169169 /* 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
12941294
12951295 /* get entry point address */
12961296 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");
12981298 else
12991299 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
13001300 }

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