mirror of
https://github.com/mirror/tinycc.git
synced 2025-02-10 06:50:10 +08:00
tcc_relocate: revert to 0.9.24 behavior
This commit is contained in:
parent
3db219477a
commit
8bbde91f62
1
libtcc.c
1
libtcc.c
@ -1525,6 +1525,7 @@ void tcc_delete(TCCState *s1)
|
|||||||
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
|
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
|
||||||
|
|
||||||
tcc_free(s1->tcc_lib_path);
|
tcc_free(s1->tcc_lib_path);
|
||||||
|
tcc_free(s1->runtime_mem);
|
||||||
tcc_free(s1);
|
tcc_free(s1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
7
libtcc.h
7
libtcc.h
@ -90,10 +90,9 @@ LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename);
|
|||||||
tcc_relocate() before. */
|
tcc_relocate() before. */
|
||||||
LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv);
|
LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv);
|
||||||
|
|
||||||
/* copy code into memory passed in by the caller and do all relocations
|
/* Do all relocations (needed before using tcc_get_symbol())
|
||||||
(needed before using tcc_get_symbol()).
|
Returns -1 on error. */
|
||||||
returns -1 on error and required size if ptr is NULL */
|
LIBTCCAPI int tcc_relocate(TCCState *s1);
|
||||||
LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr);
|
|
||||||
|
|
||||||
/* return symbol value or NULL if not found */
|
/* return symbol value or NULL if not found */
|
||||||
LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name);
|
LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name);
|
||||||
|
1
tcc.h
1
tcc.h
@ -498,6 +498,7 @@ struct TCCState {
|
|||||||
|
|
||||||
/* for tcc_relocate */
|
/* for tcc_relocate */
|
||||||
int runtime_added;
|
int runtime_added;
|
||||||
|
void *runtime_mem;
|
||||||
|
|
||||||
struct InlineFunc **inline_fns;
|
struct InlineFunc **inline_fns;
|
||||||
int nb_inline_fns;
|
int nb_inline_fns;
|
||||||
|
35
tccrun.c
35
tccrun.c
@ -85,7 +85,7 @@ void *resolve_sym(TCCState *s1, const char *sym)
|
|||||||
return dlsym(RTLD_DEFAULT, 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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy code into memory passed in by the caller and do all relocations
|
/* relocate code. Return -1 on error, required size if ptr is NULL,
|
||||||
(needed before using tcc_get_symbol()).
|
otherwise copy code into buffer passed by the caller */
|
||||||
returns -1 on error and required size if ptr is NULL */
|
static int tcc_relocate_ex(TCCState *s1, void *ptr)
|
||||||
int tcc_relocate(TCCState *s1, void *ptr)
|
|
||||||
{
|
{
|
||||||
Section *s;
|
Section *s;
|
||||||
unsigned long offset, length;
|
unsigned long offset, length;
|
||||||
@ -412,6 +411,7 @@ int tcc_relocate(TCCState *s1, void *ptr)
|
|||||||
s->sh_addr = mem ? (mem + offset + 15) & ~15 : 0;
|
s->sh_addr = mem ? (mem + offset + 15) & ~15 : 0;
|
||||||
offset = (offset + length + 15) & ~15;
|
offset = (offset + length + 15) & ~15;
|
||||||
}
|
}
|
||||||
|
offset += 16;
|
||||||
|
|
||||||
/* relocate symbols */
|
/* relocate symbols */
|
||||||
relocate_syms(s1, 1);
|
relocate_syms(s1, 1);
|
||||||
@ -429,7 +429,7 @@ int tcc_relocate(TCCState *s1, void *ptr)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (0 == mem)
|
if (0 == mem)
|
||||||
return offset + 15;
|
return offset;
|
||||||
|
|
||||||
/* relocate each section */
|
/* relocate each section */
|
||||||
for(i = 1; i < s1->nb_sections; i++) {
|
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)
|
if (s->sh_flags & SHF_EXECINSTR)
|
||||||
set_pages_executable(ptr, length);
|
set_pages_executable(ptr, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef TCC_TARGET_PE
|
#ifndef TCC_TARGET_PE
|
||||||
#ifdef TCC_TARGET_X86_64
|
#ifdef TCC_TARGET_X86_64
|
||||||
set_pages_executable(s1->runtime_plt_and_got,
|
set_pages_executable(s1->runtime_plt_and_got,
|
||||||
@ -462,18 +463,24 @@ int tcc_relocate(TCCState *s1, void *ptr)
|
|||||||
return 0;
|
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 */
|
/* launch the compiled program with the given arguments */
|
||||||
int tcc_run(TCCState *s1, int argc, char **argv)
|
int tcc_run(TCCState *s1, int argc, char **argv)
|
||||||
{
|
{
|
||||||
int (*prog_main)(int, char **);
|
int (*prog_main)(int, char **);
|
||||||
void *ptr;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = tcc_relocate(s1, NULL);
|
if (tcc_relocate(s1) < 0)
|
||||||
if (ret < 0)
|
|
||||||
return -1;
|
return -1;
|
||||||
ptr = tcc_malloc(ret);
|
|
||||||
tcc_relocate(s1, ptr);
|
|
||||||
|
|
||||||
prog_main = tcc_get_symbol_err(s1, "main");
|
prog_main = tcc_get_symbol_err(s1, "main");
|
||||||
|
|
||||||
@ -510,9 +517,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
|
|||||||
bound_exit();
|
bound_exit();
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
ret = (*prog_main)(argc, argv);
|
return (*prog_main)(argc, argv);
|
||||||
tcc_free(ptr);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/********************************************************/
|
/********************************************************/
|
||||||
|
@ -36,8 +36,6 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
TCCState *s;
|
TCCState *s;
|
||||||
int (*func)(int);
|
int (*func)(int);
|
||||||
void *mem;
|
|
||||||
int size;
|
|
||||||
|
|
||||||
s = tcc_new();
|
s = tcc_new();
|
||||||
if (!s) {
|
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 */
|
You may also open a dll with tcc_add_dll() and use symbols from that */
|
||||||
tcc_add_symbol(s, "add", add);
|
tcc_add_symbol(s, "add", add);
|
||||||
|
|
||||||
/* get needed size of the code */
|
/* relocate the code */
|
||||||
size = tcc_relocate(s, NULL);
|
if (tcc_relocate(s) < 0)
|
||||||
if (size == -1)
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* allocate memory and copy the code into it */
|
|
||||||
mem = malloc(size);
|
|
||||||
tcc_relocate(s, mem);
|
|
||||||
|
|
||||||
/* get entry symbol */
|
/* get entry symbol */
|
||||||
func = tcc_get_symbol(s, "foo");
|
func = tcc_get_symbol(s, "foo");
|
||||||
if (!func)
|
if (!func)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* delete the state */
|
|
||||||
tcc_delete(s);
|
|
||||||
|
|
||||||
/* run the code */
|
/* run the code */
|
||||||
func(32);
|
func(32);
|
||||||
|
|
||||||
free(mem);
|
/* delete the state */
|
||||||
|
tcc_delete(s);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user