tcc_relocate: revert to 0.9.24 behavior

This commit is contained in:
grischka 2009-12-19 22:11:12 +01:00
parent 3db219477a
commit 8bbde91f62
5 changed files with 30 additions and 32 deletions

View File

@ -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);
}

View File

@ -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);

1
tcc.h
View File

@ -498,6 +498,7 @@ struct TCCState {
/* for tcc_relocate */
int runtime_added;
void *runtime_mem;
struct InlineFunc **inline_fns;
int nb_inline_fns;

View File

@ -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);
}
/********************************************************/

View File

@ -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;
}