aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgrischka <grischka>2009-12-19 22:11:12 +0100
committergrischka <grischka>2009-12-19 22:16:23 +0100
commit8bbde91f62291cb0383c2406ae6123be903c944b (patch)
treeef27e46008262d2bf873c484266dba4da131f872
parent3db219477a6365d0f1d5214a345b3393a38e81d1 (diff)
downloadtinycc-8bbde91f62291cb0383c2406ae6123be903c944b.tar.gz
tinycc-8bbde91f62291cb0383c2406ae6123be903c944b.tar.bz2
tcc_relocate: revert to 0.9.24 behavior
-rw-r--r--libtcc.c1
-rw-r--r--libtcc.h7
-rw-r--r--tcc.h1
-rw-r--r--tccrun.c35
-rw-r--r--tests/libtcc_test.c18
5 files changed, 30 insertions, 32 deletions
diff --git a/libtcc.c b/libtcc.c
index 2afe432..9309b77 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -1525,6 +1525,7 @@ void tcc_delete(TCCState *s1)
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
tcc_free(s1->tcc_lib_path);
+ tcc_free(s1->runtime_mem);
tcc_free(s1);
}
diff --git a/libtcc.h b/libtcc.h
index 96070e2..ada20c2 100644
--- a/libtcc.h
+++ b/libtcc.h
@@ -90,10 +90,9 @@ LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename);
tcc_relocate() before. */
LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv);
-/* copy code into memory passed in by the caller and do all relocations
- (needed before using tcc_get_symbol()).
- returns -1 on error and required size if ptr is NULL */
-LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr);
+/* Do all relocations (needed before using tcc_get_symbol())
+ Returns -1 on error. */
+LIBTCCAPI int tcc_relocate(TCCState *s1);
/* return symbol value or NULL if not found */
LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name);
diff --git a/tcc.h b/tcc.h
index 59fe333..cc63e92 100644
--- a/tcc.h
+++ b/tcc.h
@@ -498,6 +498,7 @@ struct TCCState {
/* for tcc_relocate */
int runtime_added;
+ void *runtime_mem;
struct InlineFunc **inline_fns;
int nb_inline_fns;
diff --git a/tccrun.c b/tccrun.c
index c321c1c..d62e92a 100644
--- a/tccrun.c
+++ b/tccrun.c
@@ -85,7 +85,7 @@ void *resolve_sym(TCCState *s1, const char *sym)
return dlsym(RTLD_DEFAULT, sym);
}
-#endif /* defined CONFIG_TCC_STATIC
+#endif /* defined CONFIG_TCC_STATIC */
/********************************************************/
@@ -378,10 +378,9 @@ void set_pages_executable(void *ptr, unsigned long length)
#endif
}
-/* copy code into memory passed in by the caller and do all relocations
- (needed before using tcc_get_symbol()).
- returns -1 on error and required size if ptr is NULL */
-int tcc_relocate(TCCState *s1, void *ptr)
+/* relocate code. Return -1 on error, required size if ptr is NULL,
+ otherwise copy code into buffer passed by the caller */
+static int tcc_relocate_ex(TCCState *s1, void *ptr)
{
Section *s;
unsigned long offset, length;
@@ -412,6 +411,7 @@ int tcc_relocate(TCCState *s1, void *ptr)
s->sh_addr = mem ? (mem + offset + 15) & ~15 : 0;
offset = (offset + length + 15) & ~15;
}
+ offset += 16;
/* relocate symbols */
relocate_syms(s1, 1);
@@ -429,7 +429,7 @@ int tcc_relocate(TCCState *s1, void *ptr)
#endif
if (0 == mem)
- return offset + 15;
+ return offset;
/* relocate each section */
for(i = 1; i < s1->nb_sections; i++) {
@@ -453,6 +453,7 @@ int tcc_relocate(TCCState *s1, void *ptr)
if (s->sh_flags & SHF_EXECINSTR)
set_pages_executable(ptr, length);
}
+
#ifndef TCC_TARGET_PE
#ifdef TCC_TARGET_X86_64
set_pages_executable(s1->runtime_plt_and_got,
@@ -462,18 +463,24 @@ int tcc_relocate(TCCState *s1, void *ptr)
return 0;
}
+/* Do all relocations (needed before using tcc_get_symbol())
+ Returns -1 on error. */
+int tcc_relocate(TCCState *s1)
+{
+ int ret = tcc_relocate_ex(s1, NULL);
+ if (-1 == ret)
+ return ret;
+ s1->runtime_mem = tcc_malloc(ret);
+ return tcc_relocate_ex(s1, s1->runtime_mem);
+}
+
/* launch the compiled program with the given arguments */
int tcc_run(TCCState *s1, int argc, char **argv)
{
int (*prog_main)(int, char **);
- void *ptr;
- int ret;
- ret = tcc_relocate(s1, NULL);
- if (ret < 0)
+ if (tcc_relocate(s1) < 0)
return -1;
- ptr = tcc_malloc(ret);
- tcc_relocate(s1, ptr);
prog_main = tcc_get_symbol_err(s1, "main");
@@ -510,9 +517,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
bound_exit();
} else
#endif
- ret = (*prog_main)(argc, argv);
- tcc_free(ptr);
- return ret;
+ return (*prog_main)(argc, argv);
}
/********************************************************/
diff --git a/tests/libtcc_test.c b/tests/libtcc_test.c
index a602fb0..5a4c800 100644
--- a/tests/libtcc_test.c
+++ b/tests/libtcc_test.c
@@ -36,8 +36,6 @@ int main(int argc, char **argv)
{
TCCState *s;
int (*func)(int);
- void *mem;
- int size;
s = tcc_new();
if (!s) {
@@ -59,26 +57,20 @@ int main(int argc, char **argv)
You may also open a dll with tcc_add_dll() and use symbols from that */
tcc_add_symbol(s, "add", add);
- /* get needed size of the code */
- size = tcc_relocate(s, NULL);
- if (size == -1)
+ /* relocate the code */
+ if (tcc_relocate(s) < 0)
return 1;
- /* allocate memory and copy the code into it */
- mem = malloc(size);
- tcc_relocate(s, mem);
-
/* get entry symbol */
func = tcc_get_symbol(s, "foo");
if (!func)
return 1;
- /* delete the state */
- tcc_delete(s);
-
/* run the code */
func(32);
- free(mem);
+ /* delete the state */
+ tcc_delete(s);
+
return 0;
}