2009-12-20 00:10:26 +03:00
|
|
|
/*
|
2009-12-20 03:53:49 +03:00
|
|
|
* TCC - Tiny C Compiler - Support for -run switch
|
2009-12-20 00:10:26 +03:00
|
|
|
*
|
|
|
|
* Copyright (c) 2001-2004 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
#include "tcc.h"
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-03-05 23:15:56 +04:00
|
|
|
/* only native compiler supports -run */
|
|
|
|
#ifdef TCC_IS_NATIVE
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2020-01-18 00:58:39 +03:00
|
|
|
typedef struct rt_context
|
2019-12-11 02:37:18 +03:00
|
|
|
{
|
2020-01-18 00:58:39 +03:00
|
|
|
/* --> tccelf.c:tcc_add_btstub wants those below in that order: */
|
|
|
|
Stab_Sym *stab_sym, *stab_sym_end;
|
|
|
|
char *stab_str;
|
|
|
|
ElfW(Sym) *esym_start, *esym_end;
|
|
|
|
char *elf_str;
|
|
|
|
addr_t prog_base;
|
|
|
|
void *bounds_start;
|
|
|
|
struct rt_context *next;
|
|
|
|
/* <-- */
|
|
|
|
int num_callers;
|
|
|
|
addr_t ip, fp, sp;
|
|
|
|
void *top_func;
|
|
|
|
jmp_buf jmp_buf;
|
|
|
|
char do_jmp;
|
|
|
|
} rt_context;
|
|
|
|
|
|
|
|
static rt_context g_rtctxt;
|
|
|
|
static void set_exception_handler(void);
|
|
|
|
static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);
|
|
|
|
static void rt_exit(int code);
|
|
|
|
#endif /* CONFIG_TCC_BACKTRACE */
|
|
|
|
|
2020-01-19 13:15:12 +03:00
|
|
|
/* defined when included from lib/bt-exe.c */
|
2020-01-18 00:58:39 +03:00
|
|
|
#ifndef CONFIG_TCC_BACKTRACE_ONLY
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
# include <sys/mman.h>
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2019-12-11 02:37:18 +03:00
|
|
|
static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length);
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff);
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-03-03 20:12:06 +04:00
|
|
|
#ifdef _WIN64
|
2016-10-19 20:21:27 +03:00
|
|
|
static void *win64_add_function_table(TCCState *s1);
|
|
|
|
static void win64_del_function_table(void *);
|
2011-07-14 21:09:49 +04:00
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
/* Do all relocations (needed before using tcc_get_symbol())
|
|
|
|
Returns -1 on error. */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-09-01 13:33:34 +04:00
|
|
|
LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2017-02-05 15:58:14 +03:00
|
|
|
int size;
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
addr_t ptr_diff = 0;
|
2012-09-01 13:33:34 +04:00
|
|
|
|
|
|
|
if (TCC_RELOCATE_AUTO != ptr)
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
return tcc_relocate_ex(s1, ptr, 0);
|
2012-09-01 13:33:34 +04:00
|
|
|
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
size = tcc_relocate_ex(s1, NULL, 0);
|
2016-10-19 20:21:27 +03:00
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
2013-02-12 22:13:28 +04:00
|
|
|
|
2010-04-24 14:28:54 +04:00
|
|
|
#ifdef HAVE_SELINUX
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
{
|
|
|
|
/* Using mmap instead of malloc */
|
|
|
|
void *prx;
|
|
|
|
char tmpfname[] = "/tmp/.tccrunXXXXXX";
|
|
|
|
int fd = mkstemp(tmpfname);
|
|
|
|
unlink(tmpfname);
|
|
|
|
ftruncate(fd, size);
|
|
|
|
|
|
|
|
ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
prx = mmap (NULL, size, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
|
|
|
|
if (ptr == MAP_FAILED || prx == MAP_FAILED)
|
|
|
|
tcc_error("tccrun: could not map memory");
|
2017-02-05 15:58:14 +03:00
|
|
|
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, prx);
|
|
|
|
ptr_diff = (char*)prx - (char*)ptr;
|
2020-01-15 10:53:19 +03:00
|
|
|
close(fd);
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
}
|
2010-04-24 14:28:54 +04:00
|
|
|
#else
|
2017-02-05 15:58:14 +03:00
|
|
|
ptr = tcc_malloc(size);
|
2010-04-24 14:28:54 +04:00
|
|
|
#endif
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
tcc_relocate_ex(s1, ptr, ptr_diff); /* no more errors expected */
|
2017-02-05 15:58:14 +03:00
|
|
|
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr);
|
2016-10-19 20:21:27 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ST_FUNC void tcc_run_free(TCCState *s1)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s1->nb_runtime_mem; ++i) {
|
|
|
|
#ifdef HAVE_SELINUX
|
2017-02-05 15:58:14 +03:00
|
|
|
unsigned size = (unsigned)(addr_t)s1->runtime_mem[i++];
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
munmap(s1->runtime_mem[i++], size);
|
2017-02-05 15:58:14 +03:00
|
|
|
munmap(s1->runtime_mem[i], size);
|
2016-10-19 20:21:27 +03:00
|
|
|
#else
|
2017-02-05 15:58:14 +03:00
|
|
|
#ifdef _WIN64
|
2016-10-19 20:21:27 +03:00
|
|
|
win64_del_function_table(*(void**)s1->runtime_mem[i]);
|
2017-02-05 15:58:14 +03:00
|
|
|
#endif
|
2016-10-19 20:21:27 +03:00
|
|
|
tcc_free(s1->runtime_mem[i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
tcc_free(s1->runtime_mem);
|
2020-01-18 00:58:39 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:55:27 +03:00
|
|
|
static void run_cdtors(TCCState *s1, const char *start, const char *end,
|
|
|
|
int argc, char **argv, char **envp)
|
2020-01-18 00:58:39 +03:00
|
|
|
{
|
2020-05-22 05:28:02 +03:00
|
|
|
void **a = (void **)get_sym_addr(s1, start, 0, 0);
|
|
|
|
void **b = (void **)get_sym_addr(s1, end, 0, 0);
|
2020-01-18 00:58:39 +03:00
|
|
|
while (a != b)
|
2020-06-22 15:55:27 +03:00
|
|
|
((void(*)(int, char **, char **))*a++)(argc, argv, envp);
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* launch the compiled program with the given arguments */
|
2011-08-11 18:55:30 +04:00
|
|
|
LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2020-06-22 15:55:27 +03:00
|
|
|
int (*prog_main)(int, char **, char **), ret;
|
2020-01-18 00:58:39 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
rt_context *rc = &g_rtctxt;
|
2020-01-15 10:53:19 +03:00
|
|
|
#endif
|
2020-06-22 19:10:30 +03:00
|
|
|
# if defined(__APPLE__)
|
|
|
|
char **envp = NULL;
|
|
|
|
#else
|
|
|
|
char **envp = environ;
|
|
|
|
#endif
|
2010-04-24 14:28:54 +04:00
|
|
|
|
2020-05-22 05:28:02 +03:00
|
|
|
s1->runtime_main = s1->nostdlib ? "_start" : "main";
|
2020-06-27 18:15:06 +03:00
|
|
|
if ((s1->dflag & 16) && (addr_t)-1 == get_sym_addr(s1, s1->runtime_main, 0, 1))
|
2017-07-20 23:21:27 +03:00
|
|
|
return 0;
|
2020-01-18 00:58:39 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
if (s1->do_debug)
|
2020-06-27 18:15:06 +03:00
|
|
|
tcc_add_symbol(s1, "exit", rt_exit);
|
2020-01-18 00:58:39 +03:00
|
|
|
#endif
|
2012-09-01 13:33:34 +04:00
|
|
|
if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
|
2009-12-20 00:22:43 +03:00
|
|
|
return -1;
|
2020-06-27 18:15:06 +03:00
|
|
|
prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1);
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2020-01-18 00:58:39 +03:00
|
|
|
memset(rc, 0, sizeof *rc);
|
2010-09-15 15:43:40 +04:00
|
|
|
if (s1->do_debug) {
|
2020-01-18 00:58:39 +03:00
|
|
|
void *p;
|
|
|
|
rc->stab_sym = (Stab_Sym *)stab_section->data;
|
|
|
|
rc->stab_sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
|
|
|
|
rc->stab_str = (char *)stab_section->link->data;
|
|
|
|
rc->esym_start = (ElfW(Sym) *)(symtab_section->data);
|
|
|
|
rc->esym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
|
|
|
|
rc->elf_str = (char *)symtab_section->link->data;
|
2020-01-18 03:22:28 +03:00
|
|
|
#if PTR_SIZE == 8
|
|
|
|
rc->prog_base = text_section->sh_addr & 0xffffffff00000000ULL;
|
|
|
|
#endif
|
2020-01-18 00:58:39 +03:00
|
|
|
rc->top_func = tcc_get_symbol(s1, "main");
|
|
|
|
rc->num_callers = s1->rt_num_callers;
|
|
|
|
rc->do_jmp = 1;
|
|
|
|
if ((p = tcc_get_symbol(s1, "__rt_error")))
|
|
|
|
*(void**)p = _rt_error;
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (s1->do_bounds_check) {
|
|
|
|
if ((p = tcc_get_symbol(s1, "__bound_init")))
|
2020-05-22 06:06:08 +03:00
|
|
|
((void(*)(void*, int))p)(bounds_section->data, 1);
|
2020-01-18 00:58:39 +03:00
|
|
|
}
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
set_exception_handler();
|
2010-09-15 15:43:40 +04:00
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
#endif
|
|
|
|
|
2016-10-01 21:47:36 +03:00
|
|
|
errno = 0; /* clean errno value */
|
2020-01-18 00:58:39 +03:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2020-05-22 05:28:02 +03:00
|
|
|
/* These aren't C symbols, so don't need leading underscore handling. */
|
2020-06-22 19:10:30 +03:00
|
|
|
run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);
|
2020-01-18 00:58:39 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
if (!rc->do_jmp || !(ret = setjmp(rc->jmp_buf)))
|
2020-01-15 10:53:19 +03:00
|
|
|
#endif
|
2020-01-18 00:58:39 +03:00
|
|
|
{
|
2020-06-22 19:10:30 +03:00
|
|
|
ret = prog_main(argc, argv, envp);
|
2015-03-25 13:26:11 +03:00
|
|
|
}
|
2020-06-22 15:55:27 +03:00
|
|
|
run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);
|
2020-01-18 00:58:39 +03:00
|
|
|
if ((s1->dflag & 16) && ret)
|
|
|
|
fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
|
2020-01-15 10:53:19 +03:00
|
|
|
return ret;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2017-02-05 16:00:42 +03:00
|
|
|
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
2018-06-01 00:51:51 +03:00
|
|
|
/* To avoid that x86 processors would reload cached instructions
|
|
|
|
each time when data is written in the near, we need to make
|
|
|
|
sure that code and data do not share the same 64 byte unit */
|
2017-01-08 15:27:24 +03:00
|
|
|
#define RUN_SECTION_ALIGNMENT 63
|
2017-01-07 00:56:19 +03:00
|
|
|
#else
|
2018-06-01 00:51:51 +03:00
|
|
|
#define RUN_SECTION_ALIGNMENT 0
|
2017-01-07 00:56:19 +03:00
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* relocate code. Return -1 on error, required size if ptr is NULL,
|
|
|
|
otherwise copy code into buffer passed by the caller */
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
Section *s;
|
2018-06-01 00:51:51 +03:00
|
|
|
unsigned offset, length, align, max_align, i, k, f;
|
|
|
|
addr_t mem, addr;
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (NULL == ptr) {
|
2009-12-20 00:22:43 +03:00
|
|
|
s1->nb_errors = 0;
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
pe_output_file(s1, NULL);
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
2009-12-20 00:22:43 +03:00
|
|
|
tcc_add_runtime(s1);
|
2017-12-12 19:33:37 +03:00
|
|
|
resolve_common_syms(s1);
|
2009-12-20 00:22:43 +03:00
|
|
|
build_got_entries(s1);
|
|
|
|
#endif
|
|
|
|
if (s1->nb_errors)
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2018-06-01 00:51:51 +03:00
|
|
|
offset = max_align = 0, mem = (addr_t)ptr;
|
2016-10-19 20:21:27 +03:00
|
|
|
#ifdef _WIN64
|
2018-06-01 00:51:51 +03:00
|
|
|
offset += sizeof (void*); /* space for function_table pointer */
|
2016-10-19 20:21:27 +03:00
|
|
|
#endif
|
2017-02-05 16:00:42 +03:00
|
|
|
for (k = 0; k < 2; ++k) {
|
2018-06-01 00:51:51 +03:00
|
|
|
f = 0, addr = k ? mem : mem + ptr_diff;
|
2017-02-05 16:00:42 +03:00
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (0 == (s->sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
|
|
|
if (k != !(s->sh_flags & SHF_EXECINSTR))
|
|
|
|
continue;
|
2018-06-01 00:51:51 +03:00
|
|
|
align = s->sh_addralign - 1;
|
|
|
|
if (++f == 1 && align < RUN_SECTION_ALIGNMENT)
|
|
|
|
align = RUN_SECTION_ALIGNMENT;
|
|
|
|
if (max_align < align)
|
|
|
|
max_align = align;
|
|
|
|
offset += -(addr + offset) & align;
|
|
|
|
s->sh_addr = mem ? addr + offset : 0;
|
|
|
|
offset += s->data_offset;
|
2017-02-05 16:00:42 +03:00
|
|
|
#if 0
|
|
|
|
if (mem)
|
2018-06-01 00:51:51 +03:00
|
|
|
printf("%-16s %p len %04x align %2d\n",
|
|
|
|
s->name, (void*)s->sh_addr, (unsigned)s->data_offset, align + 1);
|
2017-02-05 16:00:42 +03:00
|
|
|
#endif
|
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* relocate symbols */
|
2020-03-14 11:17:39 +03:00
|
|
|
relocate_syms(s1, s1->symtab, !(s1->nostdlib));
|
2009-12-20 00:22:43 +03:00
|
|
|
if (s1->nb_errors)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (0 == mem)
|
2018-06-01 00:51:51 +03:00
|
|
|
return offset + max_align;
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2017-12-12 19:57:20 +03:00
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
s1->pe_imagebase = mem;
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* relocate each section */
|
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (s->reloc)
|
|
|
|
relocate_section(s1, s);
|
|
|
|
}
|
2020-05-09 02:40:36 +03:00
|
|
|
#if !defined(TCC_TARGET_PE) || defined(TCC_TARGET_MACHO)
|
2014-04-06 02:30:22 +04:00
|
|
|
relocate_plt(s1);
|
2019-12-09 22:32:13 +03:00
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
|
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (0 == (s->sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
|
|
|
length = s->data_offset;
|
2013-02-04 19:08:06 +04:00
|
|
|
ptr = (void*)s->sh_addr;
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
if (s->sh_flags & SHF_EXECINSTR)
|
2019-12-23 22:23:18 +03:00
|
|
|
ptr = (char*)((addr_t)ptr - ptr_diff);
|
2009-12-20 00:22:43 +03:00
|
|
|
if (NULL == s->data || s->sh_type == SHT_NOBITS)
|
|
|
|
memset(ptr, 0, length);
|
|
|
|
else
|
|
|
|
memcpy(ptr, s->data, length);
|
|
|
|
/* mark executable sections as executable in memory */
|
|
|
|
if (s->sh_flags & SHF_EXECINSTR)
|
2019-12-23 22:23:18 +03:00
|
|
|
set_pages_executable(s1, (char*)((addr_t)ptr + ptr_diff), length);
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
2017-11-04 02:16:37 +03:00
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
*(void**)mem = win64_add_function_table(s1);
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
return 0;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
/* allow to run code in memory */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2019-12-11 02:37:18 +03:00
|
|
|
static void set_pages_executable(TCCState *s1, void *ptr, unsigned long length)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
unsigned long old_protect;
|
|
|
|
VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
|
|
|
|
#else
|
2017-02-05 15:58:14 +03:00
|
|
|
void __clear_cache(void *beginning, void *end);
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
# ifndef HAVE_SELINUX
|
2017-02-05 15:58:14 +03:00
|
|
|
addr_t start, end;
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
# ifndef PAGESIZE
|
|
|
|
# define PAGESIZE 4096
|
|
|
|
# endif
|
2013-02-04 19:08:06 +04:00
|
|
|
start = (addr_t)ptr & ~(PAGESIZE - 1);
|
|
|
|
end = (addr_t)ptr + length;
|
2009-12-20 00:22:43 +03:00
|
|
|
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
|
2016-10-19 20:15:04 +03:00
|
|
|
if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))
|
|
|
|
tcc_error("mprotect failed: did you mean to configure --with-selinux?");
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-11 19:13:43 +03:00
|
|
|
# endif
|
2017-02-05 15:58:14 +03:00
|
|
|
# if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
|
|
|
|
__clear_cache(ptr, (char *)ptr + length);
|
|
|
|
# endif
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2016-10-19 20:21:27 +03:00
|
|
|
#ifdef _WIN64
|
|
|
|
static void *win64_add_function_table(TCCState *s1)
|
|
|
|
{
|
|
|
|
void *p = NULL;
|
|
|
|
if (s1->uw_pdata) {
|
|
|
|
p = (void*)s1->uw_pdata->sh_addr;
|
2016-11-13 13:45:55 +03:00
|
|
|
RtlAddFunctionTable(
|
2016-10-19 20:21:27 +03:00
|
|
|
(RUNTIME_FUNCTION*)p,
|
|
|
|
s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
|
2017-12-12 19:57:20 +03:00
|
|
|
s1->pe_imagebase
|
2016-10-19 20:21:27 +03:00
|
|
|
);
|
|
|
|
s1->uw_pdata = NULL;
|
|
|
|
}
|
2017-12-12 19:57:20 +03:00
|
|
|
return p;
|
2016-10-19 20:21:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void win64_del_function_table(void *p)
|
|
|
|
{
|
|
|
|
if (p) {
|
|
|
|
RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-18 00:58:39 +03:00
|
|
|
#endif //ndef CONFIG_TCC_BACKTRACE_ONLY
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
static int rt_vprintf(const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int ret = vfprintf(stderr, fmt, ap);
|
|
|
|
fflush(stderr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rt_printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
r = rt_vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
#define INCLUDE_STACK_SIZE 32
|
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
/* print the position in the source file of PC value 'pc' by reading
|
|
|
|
the stabs debug information */
|
2020-01-18 00:58:39 +03:00
|
|
|
static addr_t rt_printline (rt_context *rc, addr_t wanted_pc,
|
|
|
|
const char *msg, const char *skip)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2019-12-14 14:36:12 +03:00
|
|
|
char func_name[128];
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t func_addr, last_pc, pc;
|
2009-12-20 00:10:26 +03:00
|
|
|
const char *incl_files[INCLUDE_STACK_SIZE];
|
2019-12-14 14:36:12 +03:00
|
|
|
int incl_index, last_incl_index, len, last_line_num, i;
|
2009-12-20 00:10:26 +03:00
|
|
|
const char *str, *p;
|
2020-01-18 00:58:39 +03:00
|
|
|
ElfW(Sym) *esym;
|
|
|
|
Stab_Sym *sym;
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
next:
|
2009-12-20 00:10:26 +03:00
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
incl_index = 0;
|
2013-02-04 19:08:06 +04:00
|
|
|
last_pc = (addr_t)-1;
|
2009-12-20 00:10:26 +03:00
|
|
|
last_line_num = 1;
|
2019-12-14 14:36:12 +03:00
|
|
|
last_incl_index = 0;
|
2010-09-15 15:43:09 +04:00
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
for (sym = rc->stab_sym + 1; sym < rc->stab_sym_end; ++sym) {
|
|
|
|
str = rc->stab_str + sym->n_strx;
|
2019-12-14 14:36:12 +03:00
|
|
|
pc = sym->n_value;
|
|
|
|
|
|
|
|
switch(sym->n_type) {
|
|
|
|
case N_SLINE:
|
|
|
|
if (func_addr)
|
|
|
|
goto rel_pc;
|
|
|
|
case N_SO:
|
|
|
|
case N_SOL:
|
|
|
|
goto abs_pc;
|
|
|
|
case N_FUN:
|
|
|
|
if (sym->n_strx == 0) /* end of function */
|
|
|
|
goto rel_pc;
|
|
|
|
abs_pc:
|
2020-01-18 03:22:28 +03:00
|
|
|
#if PTR_SIZE == 8
|
|
|
|
/* Stab_Sym.n_value is only 32bits */
|
|
|
|
pc += rc->prog_base;
|
|
|
|
#endif
|
2020-01-19 13:15:12 +03:00
|
|
|
goto check_pc;
|
2019-12-14 14:36:12 +03:00
|
|
|
rel_pc:
|
|
|
|
pc += func_addr;
|
2020-01-19 13:15:12 +03:00
|
|
|
check_pc:
|
|
|
|
if (pc >= wanted_pc && wanted_pc >= last_pc)
|
|
|
|
goto found;
|
2019-12-14 14:36:12 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
switch(sym->n_type) {
|
|
|
|
/* function start or end */
|
|
|
|
case N_FUN:
|
2019-12-14 14:36:12 +03:00
|
|
|
if (sym->n_strx == 0)
|
|
|
|
goto reset_func;
|
|
|
|
p = strchr(str, ':');
|
|
|
|
if (0 == p || (len = p - str + 1, len > sizeof func_name))
|
|
|
|
len = sizeof func_name;
|
|
|
|
pstrcpy(func_name, len, str);
|
|
|
|
func_addr = pc;
|
2009-12-20 00:10:26 +03:00
|
|
|
break;
|
|
|
|
/* line number info */
|
|
|
|
case N_SLINE:
|
|
|
|
last_pc = pc;
|
|
|
|
last_line_num = sym->n_desc;
|
2019-12-14 14:36:12 +03:00
|
|
|
last_incl_index = incl_index;
|
2009-12-20 00:10:26 +03:00
|
|
|
break;
|
|
|
|
/* include files */
|
|
|
|
case N_BINCL:
|
2019-12-14 14:36:12 +03:00
|
|
|
if (incl_index < INCLUDE_STACK_SIZE)
|
2009-12-20 00:10:26 +03:00
|
|
|
incl_files[incl_index++] = str;
|
|
|
|
break;
|
|
|
|
case N_EINCL:
|
|
|
|
if (incl_index > 1)
|
|
|
|
incl_index--;
|
|
|
|
break;
|
2019-12-14 14:36:12 +03:00
|
|
|
/* start/end of translation unit */
|
2009-12-20 00:10:26 +03:00
|
|
|
case N_SO:
|
2019-12-14 14:36:12 +03:00
|
|
|
incl_index = 0;
|
|
|
|
if (sym->n_strx) {
|
2009-12-20 00:10:26 +03:00
|
|
|
/* do not add path */
|
|
|
|
len = strlen(str);
|
|
|
|
if (len > 0 && str[len - 1] != '/')
|
2019-12-14 14:36:12 +03:00
|
|
|
incl_files[incl_index++] = str;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2019-12-14 14:36:12 +03:00
|
|
|
reset_func:
|
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
last_pc = (addr_t)-1;
|
|
|
|
break;
|
|
|
|
/* alternative file name (from #line or #include directives) */
|
|
|
|
case N_SOL:
|
|
|
|
if (incl_index)
|
|
|
|
incl_files[incl_index-1] = str;
|
2009-12-20 00:10:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
last_incl_index = 0;
|
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
/* we try symtab symbols (no line number info) */
|
2020-01-18 00:58:39 +03:00
|
|
|
for (esym = rc->esym_start + 1; esym < rc->esym_end; ++esym) {
|
2019-12-14 14:36:12 +03:00
|
|
|
int type = ELFW(ST_TYPE)(esym->st_info);
|
|
|
|
if (type == STT_FUNC || type == STT_GNU_IFUNC) {
|
|
|
|
if (wanted_pc >= esym->st_value &&
|
|
|
|
wanted_pc < esym->st_value + esym->st_size) {
|
|
|
|
pstrcpy(func_name, sizeof(func_name),
|
2020-01-18 00:58:39 +03:00
|
|
|
rc->elf_str + esym->st_name);
|
2019-12-14 14:36:12 +03:00
|
|
|
func_addr = esym->st_value;
|
|
|
|
goto found;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 14:36:12 +03:00
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
if ((rc = rc->next))
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
found:
|
2019-12-14 14:36:12 +03:00
|
|
|
i = last_incl_index;
|
2020-01-18 00:58:39 +03:00
|
|
|
if (i > 0) {
|
|
|
|
str = incl_files[--i];
|
|
|
|
if (skip[0] && strstr(str, skip))
|
|
|
|
return (addr_t)-1;
|
|
|
|
rt_printf("%s:%d: ", str, last_line_num);
|
|
|
|
} else
|
|
|
|
rt_printf("%08llx : ", (long long)wanted_pc);
|
|
|
|
rt_printf("%s %s", msg, func_name[0] ? func_name : "???");
|
|
|
|
#if 0
|
2010-09-15 15:42:52 +04:00
|
|
|
if (--i >= 0) {
|
2019-12-14 14:36:12 +03:00
|
|
|
rt_printf(" (included from ");
|
2010-09-15 15:42:52 +04:00
|
|
|
for (;;) {
|
2019-12-14 14:36:12 +03:00
|
|
|
rt_printf("%s", incl_files[i]);
|
2010-09-15 15:42:52 +04:00
|
|
|
if (--i < 0)
|
|
|
|
break;
|
2019-12-14 14:36:12 +03:00
|
|
|
rt_printf(", ");
|
2010-09-15 15:42:52 +04:00
|
|
|
}
|
2019-12-14 14:36:12 +03:00
|
|
|
rt_printf(")");
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2020-01-18 00:58:39 +03:00
|
|
|
#endif
|
2009-12-20 00:10:26 +03:00
|
|
|
return func_addr;
|
|
|
|
}
|
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level);
|
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_context *rc = &g_rtctxt;
|
|
|
|
addr_t pc = 0;
|
|
|
|
char skip[100];
|
|
|
|
int i, level, ret, n;
|
|
|
|
const char *a, *b, *msg;
|
|
|
|
|
|
|
|
if (fp) {
|
|
|
|
/* we're called from tcc_backtrace. */
|
|
|
|
rc->fp = (addr_t)fp;
|
|
|
|
rc->ip = (addr_t)ip;
|
|
|
|
msg = "";
|
|
|
|
} else {
|
|
|
|
/* we're called from signal/exception handler */
|
|
|
|
msg = "RUNTIME ERROR: ";
|
2019-12-14 14:36:12 +03:00
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
skip[0] = 0;
|
|
|
|
/* If fmt is like "^file.c^..." then skip calls from 'file.c' */
|
|
|
|
if (fmt[0] == '^' && (b = strchr(a = fmt + 1, fmt[0]))) {
|
|
|
|
memcpy(skip, a, b - a), skip[b - a] = 0;
|
|
|
|
fmt = b + 1;
|
|
|
|
}
|
2019-12-11 02:37:18 +03:00
|
|
|
|
2020-01-18 00:58:39 +03:00
|
|
|
n = rc->num_callers ? rc->num_callers : 6;
|
|
|
|
for (i = level = 0; level < n; i++) {
|
|
|
|
ret = rt_get_caller_pc(&pc, rc, i);
|
|
|
|
a = "%s";
|
|
|
|
if (ret != -1) {
|
|
|
|
pc = rt_printline(rc, pc, level ? "by" : "at", skip);
|
|
|
|
if (pc == (addr_t)-1)
|
|
|
|
continue;
|
|
|
|
a = ": %s";
|
|
|
|
}
|
|
|
|
if (level == 0) {
|
|
|
|
rt_printf(a, msg);
|
|
|
|
rt_vprintf(fmt, ap);
|
|
|
|
} else if (ret == -1)
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
2019-12-14 14:36:12 +03:00
|
|
|
rt_printf("\n");
|
2020-01-18 00:58:39 +03:00
|
|
|
if (ret == -1 || (pc == (addr_t)rc->top_func && pc))
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
2020-01-18 00:58:39 +03:00
|
|
|
++level;
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
2020-01-18 00:58:39 +03:00
|
|
|
|
|
|
|
rc->ip = rc->fp = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emit a run time error at position 'pc' */
|
|
|
|
static int rt_error(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = _rt_error(0, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rt_exit(int code)
|
|
|
|
{
|
|
|
|
rt_context *rc = &g_rtctxt;
|
|
|
|
if (rc->do_jmp)
|
|
|
|
longjmp(rc->jmp_buf, code ? code : 256);
|
|
|
|
exit(code);
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
2019-12-14 14:36:12 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifndef _WIN32
|
2019-12-14 14:36:12 +03:00
|
|
|
# include <signal.h>
|
|
|
|
# ifndef __OpenBSD__
|
|
|
|
# include <sys/ucontext.h>
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define ucontext_t CONTEXT
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
/* translate from ucontext_t* to internal rt_context * */
|
|
|
|
static void rt_getcontext(ucontext_t *uc, rt_context *rc)
|
|
|
|
{
|
|
|
|
#if defined _WIN64
|
|
|
|
rc->ip = uc->Rip;
|
|
|
|
rc->fp = uc->Rbp;
|
|
|
|
rc->sp = uc->Rsp;
|
|
|
|
#elif defined _WIN32
|
|
|
|
rc->ip = uc->Eip;
|
|
|
|
rc->fp = uc->Ebp;
|
|
|
|
rc->sp = uc->Esp;
|
|
|
|
#elif defined __i386__
|
|
|
|
# if defined(__APPLE__)
|
|
|
|
rc->ip = uc->uc_mcontext->__ss.__eip;
|
|
|
|
rc->fp = uc->uc_mcontext->__ss.__ebp;
|
|
|
|
# elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
|
|
|
rc->ip = uc->uc_mcontext.mc_eip;
|
|
|
|
rc->fp = uc->uc_mcontext.mc_ebp;
|
|
|
|
# elif defined(__dietlibc__)
|
|
|
|
rc->ip = uc->uc_mcontext.eip;
|
|
|
|
rc->fp = uc->uc_mcontext.ebp;
|
|
|
|
# elif defined(__NetBSD__)
|
|
|
|
rc->ip = uc->uc_mcontext.__gregs[_REG_EIP];
|
|
|
|
rc->fp = uc->uc_mcontext.__gregs[_REG_EBP];
|
|
|
|
# elif defined(__OpenBSD__)
|
|
|
|
rc->ip = uc->sc_eip;
|
|
|
|
rc->fp = uc->sc_ebp;
|
|
|
|
# elif !defined REG_EIP && defined EIP /* fix for glibc 2.1 */
|
|
|
|
rc->ip = uc->uc_mcontext.gregs[EIP];
|
|
|
|
rc->fp = uc->uc_mcontext.gregs[EBP];
|
|
|
|
# else
|
|
|
|
rc->ip = uc->uc_mcontext.gregs[REG_EIP];
|
|
|
|
rc->fp = uc->uc_mcontext.gregs[REG_EBP];
|
|
|
|
# endif
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
# if defined(__APPLE__)
|
|
|
|
rc->ip = uc->uc_mcontext->__ss.__rip;
|
|
|
|
rc->fp = uc->uc_mcontext->__ss.__rbp;
|
|
|
|
# elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
|
|
|
rc->ip = uc->uc_mcontext.mc_rip;
|
|
|
|
rc->fp = uc->uc_mcontext.mc_rbp;
|
|
|
|
# elif defined(__NetBSD__)
|
|
|
|
rc->ip = uc->uc_mcontext.__gregs[_REG_RIP];
|
|
|
|
rc->fp = uc->uc_mcontext.__gregs[_REG_RBP];
|
|
|
|
# else
|
|
|
|
rc->ip = uc->uc_mcontext.gregs[REG_RIP];
|
|
|
|
rc->fp = uc->uc_mcontext.gregs[REG_RBP];
|
|
|
|
# endif
|
|
|
|
#elif defined(__arm__)
|
|
|
|
rc->ip = uc->uc_mcontext.arm_pc;
|
|
|
|
rc->fp = uc->uc_mcontext.arm_fp;
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
rc->ip = uc->uc_mcontext.pc;
|
2019-12-15 02:05:30 +03:00
|
|
|
rc->fp = uc->uc_mcontext.regs[29];
|
2020-06-16 08:39:48 +03:00
|
|
|
#elif defined(__riscv)
|
|
|
|
rc->ip = uc->uc_mcontext.__gregs[REG_PC];
|
|
|
|
rc->fp = uc->uc_mcontext.__gregs[REG_S0];
|
2019-12-14 14:36:12 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#ifndef _WIN32
|
2009-12-20 00:22:43 +03:00
|
|
|
/* signal handler for fatal errors */
|
|
|
|
static void sig_error(int signum, siginfo_t *siginf, void *puc)
|
|
|
|
{
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_context *rc = &g_rtctxt;
|
|
|
|
rt_getcontext(puc, rc);
|
2009-12-20 00:22:43 +03:00
|
|
|
|
|
|
|
switch(signum) {
|
|
|
|
case SIGFPE:
|
|
|
|
switch(siginf->si_code) {
|
|
|
|
case FPE_INTDIV:
|
|
|
|
case FPE_FLTDIV:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("division by zero");
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
default:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("floating point exception");
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGBUS:
|
|
|
|
case SIGSEGV:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("invalid memory access");
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
case SIGILL:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("illegal instruction");
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
case SIGABRT:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("abort() called");
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
default:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("caught signal %d", signum);
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_exit(255);
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifndef SA_SIGINFO
|
|
|
|
# define SA_SIGINFO 0x00000004u
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* Generate a stack backtrace when a CPU exception occurs. */
|
|
|
|
static void set_exception_handler(void)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
/* install TCC signal handlers to print debug info on fatal
|
|
|
|
runtime errors */
|
|
|
|
sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
|
2020-01-18 00:58:39 +03:00
|
|
|
#if 0//def SIGSTKSZ // this causes signals not to work at all on some (older) linuxes
|
2020-01-15 10:53:19 +03:00
|
|
|
sigact.sa_flags |= SA_ONSTACK;
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
sigact.sa_sigaction = sig_error;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigaction(SIGFPE, &sigact, NULL);
|
|
|
|
sigaction(SIGILL, &sigact, NULL);
|
|
|
|
sigaction(SIGSEGV, &sigact, NULL);
|
|
|
|
sigaction(SIGBUS, &sigact, NULL);
|
|
|
|
sigaction(SIGABRT, &sigact, NULL);
|
2020-01-18 00:58:39 +03:00
|
|
|
#if 0//def SIGSTKSZ
|
2020-01-15 10:53:19 +03:00
|
|
|
/* This allows stack overflow to be reported instead of a SEGV */
|
|
|
|
{
|
|
|
|
stack_t ss;
|
|
|
|
static unsigned char stack[SIGSTKSZ] __attribute__((aligned(16)));
|
|
|
|
|
|
|
|
ss.ss_sp = stack;
|
|
|
|
ss.ss_size = SIGSTKSZ;
|
|
|
|
ss.ss_flags = 0;
|
|
|
|
sigaltstack(&ss, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
#else /* WIN32 */
|
2020-01-18 00:58:39 +03:00
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
/* signal handler for fatal errors */
|
|
|
|
static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
|
|
|
|
{
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_context *rc = &g_rtctxt;
|
2019-12-14 14:36:12 +03:00
|
|
|
unsigned code;
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_getcontext(ex_info->ContextRecord, rc);
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
switch (code = ex_info->ExceptionRecord->ExceptionCode) {
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("invalid memory access");
|
2019-12-14 14:36:12 +03:00
|
|
|
break;
|
|
|
|
case EXCEPTION_STACK_OVERFLOW:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("stack overflow");
|
2019-12-14 14:36:12 +03:00
|
|
|
break;
|
|
|
|
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("division by zero");
|
2019-12-14 14:36:12 +03:00
|
|
|
break;
|
|
|
|
case EXCEPTION_BREAKPOINT:
|
|
|
|
case EXCEPTION_SINGLE_STEP:
|
2020-01-18 00:58:39 +03:00
|
|
|
rc->ip = *(addr_t*)rc->sp;
|
|
|
|
rt_error("breakpoint/single-step exception:");
|
2019-12-14 14:36:12 +03:00
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
default:
|
2020-01-18 00:58:39 +03:00
|
|
|
rt_error("caught exception %08x", code);
|
2019-12-14 14:36:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-01-18 00:58:39 +03:00
|
|
|
if (rc->do_jmp)
|
|
|
|
rt_exit(255);
|
2019-12-14 14:36:12 +03:00
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2019-12-14 14:36:12 +03:00
|
|
|
/* Generate a stack backtrace when a CPU exception occurs. */
|
|
|
|
static void set_exception_handler(void)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2019-12-14 14:36:12 +03:00
|
|
|
SetUnhandledExceptionFilter(cpu_exception_handler);
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
2013-02-04 20:24:03 +04:00
|
|
|
/* return the PC at frame level 'level'. Return negative if not found */
|
2019-12-14 14:36:12 +03:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2019-12-14 14:36:12 +03:00
|
|
|
addr_t ip, fp;
|
2009-12-20 00:10:26 +03:00
|
|
|
if (level == 0) {
|
2019-12-14 14:36:12 +03:00
|
|
|
ip = rc->ip;
|
2009-12-20 00:10:26 +03:00
|
|
|
} else {
|
2019-12-14 14:36:12 +03:00
|
|
|
ip = 0;
|
|
|
|
fp = rc->fp;
|
|
|
|
while (--level) {
|
2009-12-20 00:10:26 +03:00
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000)
|
2019-12-14 14:36:12 +03:00
|
|
|
break;
|
2013-02-04 20:24:03 +04:00
|
|
|
fp = ((addr_t *)fp)[0];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2019-12-14 14:36:12 +03:00
|
|
|
if (fp > 0x1000)
|
|
|
|
ip = ((addr_t *)fp)[1];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2019-12-14 14:36:12 +03:00
|
|
|
if (ip <= 0x1000)
|
|
|
|
return -1;
|
|
|
|
*paddr = ip;
|
|
|
|
return 0;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2010-05-14 16:22:32 +04:00
|
|
|
#elif defined(__arm__)
|
2019-12-14 14:36:12 +03:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
|
2010-05-14 16:22:32 +04:00
|
|
|
{
|
2019-12-14 14:36:12 +03:00
|
|
|
/* XXX: only supports linux */
|
|
|
|
#if !defined(__linux__)
|
|
|
|
return -1;
|
2010-05-14 16:22:32 +04:00
|
|
|
#else
|
2019-12-14 14:36:12 +03:00
|
|
|
if (level == 0) {
|
|
|
|
*paddr = rc->ip;
|
2010-05-14 16:22:32 +04:00
|
|
|
} else {
|
2019-12-14 14:36:12 +03:00
|
|
|
addr_t fp = rc->fp;
|
2020-06-16 08:39:48 +03:00
|
|
|
while (--level)
|
|
|
|
fp = ((addr_t *)fp)[0];
|
|
|
|
*paddr = ((addr_t *)fp)[2];
|
2010-05-14 16:22:32 +04:00
|
|
|
}
|
2019-12-14 14:36:12 +03:00
|
|
|
return 0;
|
|
|
|
#endif
|
2010-05-14 16:22:32 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 21:58:31 +03:00
|
|
|
#elif defined(__aarch64__)
|
2019-12-14 14:36:12 +03:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
|
2015-02-13 21:58:31 +03:00
|
|
|
{
|
2020-06-16 08:39:48 +03:00
|
|
|
if (level == 0) {
|
2019-12-14 14:36:12 +03:00
|
|
|
*paddr = rc->ip;
|
|
|
|
} else {
|
2019-12-15 02:05:30 +03:00
|
|
|
addr_t *fp = (addr_t*)rc->fp;
|
2019-12-14 14:36:12 +03:00
|
|
|
while (--level)
|
2015-02-13 21:58:31 +03:00
|
|
|
fp = (addr_t *)fp[0];
|
|
|
|
*paddr = fp[1];
|
|
|
|
}
|
2019-12-14 14:36:12 +03:00
|
|
|
return 0;
|
2015-02-13 21:58:31 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 08:39:48 +03:00
|
|
|
#elif defined(__riscv)
|
|
|
|
static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
|
|
|
|
{
|
|
|
|
if (level == 0) {
|
|
|
|
*paddr = rc->ip;
|
|
|
|
} else {
|
|
|
|
addr_t *fp = (addr_t*)rc->fp;
|
2020-07-16 00:11:42 +03:00
|
|
|
while (--level && fp >= (addr_t*)0x1000)
|
2020-06-16 08:39:48 +03:00
|
|
|
fp = (addr_t *)fp[-2];
|
2020-07-16 00:11:42 +03:00
|
|
|
if (fp < (addr_t*)0x1000)
|
|
|
|
return -1;
|
2020-06-16 08:39:48 +03:00
|
|
|
*paddr = fp[-1];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
|
|
|
#warning add arch specific rt_get_caller_pc()
|
2019-12-14 14:36:12 +03:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-15 15:43:25 +04:00
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* CONFIG_TCC_BACKTRACE */
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#ifdef CONFIG_TCC_STATIC
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* dummy function for profiling */
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void *dlopen(const char *filename, int flag)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
return NULL;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void dlclose(void *p)
|
2009-12-20 00:11:12 +03:00
|
|
|
{
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
|
|
|
|
ST_FUNC const char *dlerror(void)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
return "error";
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
typedef struct TCCSyms {
|
|
|
|
char *str;
|
|
|
|
void *ptr;
|
|
|
|
} TCCSyms;
|
2009-12-20 00:10:26 +03:00
|
|
|
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* add the symbol you want here if no dynamic linking is done */
|
|
|
|
static TCCSyms tcc_syms[] = {
|
|
|
|
#if !defined(CONFIG_TCCBOOT)
|
2013-02-12 22:13:28 +04:00
|
|
|
#define TCCSYM(a) { #a, &a, },
|
2009-12-20 00:22:43 +03:00
|
|
|
TCCSYM(printf)
|
|
|
|
TCCSYM(fprintf)
|
|
|
|
TCCSYM(fopen)
|
|
|
|
TCCSYM(fclose)
|
2013-02-12 22:13:28 +04:00
|
|
|
#undef TCCSYM
|
2009-12-20 00:10:26 +03:00
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2016-11-13 13:45:55 +03:00
|
|
|
ST_FUNC void *dlsym(void *handle, const char *symbol)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
TCCSyms *p;
|
|
|
|
p = tcc_syms;
|
|
|
|
while (p->str != NULL) {
|
|
|
|
if (!strcmp(p->str, symbol))
|
|
|
|
return p->ptr;
|
|
|
|
p++;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* CONFIG_TCC_STATIC */
|
2013-02-04 19:08:06 +04:00
|
|
|
#endif /* TCC_IS_NATIVE */
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|