Commit Graph

61 Commits

Author SHA1 Message Date
grischka
4a3741bf02 x86_64-asm: =m operand fixes
The problem was with tcctest.c:

    unsigned set;
    __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");

when with tcc compiled with the HAVE_SELINUX option, run with
tcc -run, it would use large addresses far beyond the 32bits
range when tcc did not use the pc-relative mode for accessing
'set' in global data memory.  In fact the assembler did not
know about %rip at all.

Changes:
- memory operands use (%rax) not (%eax)
- conversion from VT_LLOCAL: use type VT_PTR
- support 'k' modifier
- support %rip register
- support X(%rip) pc-relative addresses

The test in tcctest.c is from Michael Matz.
2016-11-20 14:50:56 +01:00
Thomas Stalder
4af25aed92 remove warnings 2016-11-13 11:45:55 +01:00
grischka
02642bc94c lib/libtcc1.c: cleanup
- remove #include dependencies from libtcc1.c
  for easier cross compilation
- clear_cache only on ARM
- error-message for mprotect failure
2016-10-19 19:21:36 +02:00
grischka
bfd1c08d6c tccrun/win64: cleanup runtime function table
- call RtlDeleteFunctionTable
  (important for multiple compilations)

- the RUNTIME_FUNCTION* is now at the beginning of the
  runtime memory.  Therefor when tcc_relocate is called
  with user memory, this should be done manually before
  it is free'd:
      RtlDeleteFunctionTable(*(void**)user_mem);
      [ free(user_mem); ]

- x86_64-gen.c: expand char/short return values to int
2016-10-19 19:21:27 +02:00
grischka
0be098929a tccpp_new/delete and other cleanups 2016-10-17 23:24:01 +02:00
Christian Jullien
36759ddacf Start support of OpenBSD, tcc compiler can be produced from source tree 2016-10-15 14:50:17 +02:00
grischka
b42cb16b65 Misc. fixes
Makefile :
- do not 'uninstall' peoples /usr/local/doc entirely
libtcc.c :
- MEM_DEBUG : IDE-friendly output "file:line: ..."
- always ELF for objects
tccgen.c :
- fix memory leak in new switch code
- move static 'in_sizeof' out of function
profiling :
- define 'static' to empty
resolve_sym() :
- replace by dlsym()

win32/64: fix R_XXX_RELATIVE fixme
- was fixed for i386 already in
  8e4d64be2f
- do not -Lsystemdir if compiling to .o
2016-10-05 18:34:17 +02:00
grischka
9c5bb16447 Revert part of "fix installation amd bcheck for Windows"
tccelf.c : force linking bcheck by adding elf symbol __bound_init
bcheck.c : use (size_t)1 for x86_64

Fixes 7e7e6148fd
2016-10-01 20:47:36 +02:00
seyko
5ee097fce9 allow to compile tcc by pcc
* pcc have only __linux__ macro (and no __linux)
    * pcc don't have __clear_cache proc
2016-04-15 17:41:49 +03:00
Edmund Grimley Evans
ba99a70cd8 Trivial changes to avoid some compiler warnings. 2015-11-19 18:26:47 +00:00
Edmund Grimley Evans
eafd7a7d3b Correct prototype: void __clear_cache(void *, void *). 2015-10-15 19:02:54 +01:00
gus knight
89ad24e7d6 Revert all of my changes to directories & codingstyle. 2015-07-29 16:57:12 -04:00
gus knight
47e06c6d4e Reorganize the source tree.
* Documentation is now in "docs".
 * Source code is now in "src".
 * Misc. fixes here and there so that everything still works.

I think I got everything in this commit, but I only tested this
on Linux (Make) and Windows (CMake), so I might've messed
something up on other platforms...
2015-07-27 16:03:25 -04:00
gus knight
d6b64e2574 Clean up lots of rogue tabs.
Still some more tabs to be taken care of. arm-gen.c and tcccoff.c
have so many style issues that I'm just going to throw clang-format
at them.
2015-07-27 14:14:41 -04:00
seyko
acef4ff244 make a bound checking more compatible with Windows 64
On Linux 32:   sizeof(long)=32 == sizeof(void *)=32
    on Linux 64:   sizeof(long)=64 == sizeof(void *)=64
    on Windows 64: sizeof(long)=32 != sizeof(void *)=64
2015-03-26 07:47:45 +03:00
seyko
cde79a805e fix a bug #43984: tcc -run reports errno=2
The following program (errno.c) reports errno=2 when run
    using "tcc -run errno.c"

    #include <errno.h>
    #include <stdio.h>
    int main(void) { printf("errno=%d\n", errno); return 0; }
2015-03-25 13:26:11 +03:00
Michael Matz
6d055312a2 aarch64: Fix -run.
This adds some more support for properly transfering some
offsets over the different stages of a relocations life.
Still not at all psABI compliant and DSOs can't yet be generated.
But it runs the testsuite in qemu-arm64.
2015-02-23 22:51:10 +00:00
Edmund Grimley Evans
b14ef0e24b Add arm64 (AArch64) as a target architecture. 2015-02-23 22:51:03 +00:00
minux
9714d2e75f build: add initial NetBSD support.
Not able to generate ELF files on NetBSD yet (lacks the note and crt1.o
is actually named crt0.o on NetBSD), but -run works with these extra
defines:
-D__lint__ -D"__symbolrename(x)=asm(#x)" -D__NetBSD__

The -D__lint__ is an ugly hack, TCC should be able to emulate GCC just
fine, but it seems TCC doesn't support __builtin_va_list yet?
	typedef __builtin_va_list __va_list;
/usr/include/sys/ansi.h:72: error: ';' expected (got "__va_list")
2014-04-12 01:42:46 -04:00
minux
8d3e0b3080 tccrun: fix build on DragonFly BSD. 2014-04-12 00:52:20 -04:00
Michael Matz
6a947d9d26 ELF: Remove traces of old RUNTIME_PLTGOT code
The last users of it went away, no use in keeping
this code.
2014-04-06 01:59:35 +02:00
Michael Matz
9750d0b725 x86_64: Create proper PLT and GOT also for -run
This makes us use the normal PLT/GOT codepaths also for -run,
which formerly used an on-the-side blob for the jump tables.
For x86_64 only for now, arm coming up.
2014-04-06 00:30:22 +02:00
Thomas Preud'homme
62d1da1b3e Fix warning of clang 2014-03-09 22:54:30 +08:00
Christian Jullien
b46f7461a3 Fix warning about undeclared __clear_cache function call. 2014-02-08 08:31:32 +01:00
Kirill Smelkov
75118780da tccrun: Mark argv area as valid for bcheck
On my x86_64 box in i386 mode with address space randomization turned off,
I've observed the following:

    tests$ ../tcc -B.. -b -run boundtest.c 1
    Runtime error: dereferencing invalid pointer
    boundtest.c:222: at 0x808da73 main()

With diagnostic patch (like in efd9d92b "lib/bcheck: Don't assume heap
goes right after bss") and bcheck traces for __bound_new_region,
__bound_ptr_indir, etc... here is how the program run looks like:

    >>> TCC

    etext:   0x8067ed8
    edata:   0x807321d
    end:     0x807d95c
    brk:     0x807e000
    stack:  0xffffd0b4
    &errno: 0xf7dbd688
    mark_invalid  0xfff80000 -      (nil)
    mark_invalid   0x80fa000 - 0x100fa000
    new  808fdb0  808ff40  101  101  fd0  ff0
    new  808ff44  808ff48  101  101  ff0  ff0
    new  808ff49  8090049  101  101  ff0 1000
    new  808fd20  808fd29  101  101  fd0  fd0
    new  808fd2c  808fd6c  101  101  fd0  fd0
    new  808fd6d  808fda0  101  101  fd0  fd0
    E: __bound_ptr_indir4(0xffffd184, 0x4)
    Runtime error: dereferencing invalid pointer
    boundtest.c:222: at 0x808ea83 main()

So we are accessing something on stack, above stack entry for compiled
main. Investigating with gdb shows that this is argv:

    tests$ gdb ../tcc
    Reading symbols from /home/kirr/src/tools/tinycc/tcc...done.
    (gdb) set args -B.. -b -run boundtest.c 1
    (gdb) r
    Starting program: /home/kirr/src/tools/tinycc/tests/../tcc -B.. -b -run boundtest.c 1
    warning: Could not load shared library symbols for linux-gate.so.1.
    Do you need "set solib-search-path" or "set sysroot"?

    >>> TCC

    etext:   0x8067ed8
    edata:   0x807321d
    end:     0x807d95c
    brk:     0x807e000
    stack:  0xffffd074
    &errno: 0xf7dbd688
    mark_invalid  0xfff80000 -      (nil)
    mark_invalid   0x80fa000 - 0x100fa000
    new  808fdb0  808ff40  101  101  fd0  ff0
    new  808ff44  808ff48  101  101  ff0  ff0
    new  808ff49  8090049  101  101  ff0 1000
    new  808fd20  808fd29  101  101  fd0  fd0
    new  808fd2c  808fd6c  101  101  fd0  fd0
    new  808fd6d  808fda0  101  101  fd0  fd0
    E: __bound_ptr_indir4(0xffffd144, 0x4)

    Program received signal SIGSEGV, Segmentation fault.
    0x0808ea83 in ?? ()
    (gdb) bt
    #0  0x0808ea83 in ?? ()
    #1  0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
    #2  0x080492b0 in main (argc=6, argv=0xffffd134) at tcc.c:346
    (gdb) f 1
    #1  0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
    132             ret = (*prog_main)(argc, argv);
    132             ret = (*prog_main)(argc, argv);
    (gdb) p argv
    $1 = (char **) 0xffffd144

So before running compiled program, mark argv as valid region and we are
done - now the test passes.

P.S. maybe it would be better to just mark the whole vector kernel passes to
program (argv, env, auxv, etc...) as valid all at once...
2014-01-19 16:47:51 +04:00
Iavael
8e724128e8 Revert "Use anonymous file instead of regular file to back mmap"
This reverts commit 935d8169b8,
because two anonymous mappings would have different content,
while they must have the same one.
2014-01-12 09:29:30 +04:00
keren
80b36ab628 Fix missing mem_size assignment when using mmap() 2014-01-10 10:23:11 -08:00
keren
935d8169b8 Use anonymous file instead of regular file to back mmap
Signed-off-by: Keren Tan <tankeren@gmail.com>
2014-01-09 14:00:19 -08:00
grischka
73faaea227 i386-gen: preserve fp control word in gen_cvt_ftoi
- Use runtime function for conversion
- Also initialize fp with tcc -run on windows

This fixes a bug where
  double x = 1.0;
  double y = 1.0000000000000001;
  double z = x < y ? 0 : sqrt (x*x - y*y);
caused a bad sqrt because rounding precision for the x < y comparison
was different to the one used within the sqrt function.

This also fixes a bug where
  printf("%d, %d", (int)pow(10, 2), (int)pow(10, 2));
would print
  100, 99

Unrelated:
  win32: document relative include & lib lookup
  win32: normalize_slashes: do not mirror silly gcc behavior
  This reverts part of commit 8a81f9e103
  winapi: add missing WINAPI decl. for some functions
2013-08-28 22:55:05 +02:00
Thomas Preud'homme
c68af2db9d Fix synchronization between data and instr caches 2013-03-19 14:03:15 +01:00
Thomas Preud'homme
6ed6a36a51 Flush caches before -running program
On some architectures, ARM for instance, the data and instruction caches
are not coherent with each other. This is a problem for the -run feature
since instructions are written in memory, and are thus written in the
data cache first and then later flushed to the main memory. If the
instructions are executed before they are pushed out of the cache, then
the processor will fetch the old content from the memory and not the
newly generated code. The solution is to flush from the data cache all
the data in the memory region containing the instructions and to
invalidate the same region in the instruction cache.
2013-03-18 10:08:39 +01:00
grischka
05108a3b0a libtcc: new LIBTCCAPI tcc_set_options(TCCState*, const char*str)
This replaces       -> use instead:
-----------------------------------
- tcc_set_linker    -> tcc_set_options(s, "-Wl,...");
- tcc_set_warning   -> tcc_set_options(s, "-W...");
- tcc_enable_debug  -> tcc_set_options(s, "-g");

parse_args is moved to libtcc.c (now tcc_parse_args).

Also some cleanups:
- reorder TCCState members
- add some comments here and there
- do not use argv's directly, make string copies
- use const char* in tcc_set_linker
- tccpe: use fd instead of fp

tested with -D MEM_DEBUG: 0 bytes left
2013-02-12 19:13:28 +01:00
grischka
d5f4df09ff tests: cleanup
tests:
- add "hello" to test first basic compilation to file/memory
- add "more" test (tests2 suite)
- remove some tests

tests2:
- move into tests dir
- Convert some files from DOS to unix LF
- remove 2>&1 redirection

win32:
- tccrun.c: modify exception filter to exit correctly (needed for btest)
- tcctest.c: exclude weak_test() (feature does not exist on win32)
2013-02-05 14:27:38 +01:00
grischka
4b539aa67d tccrun.c: unify rt_get_caller_pc prototype 2013-02-04 17:24:03 +01:00
grischka
82bcbd027f portability: fix void* <-> target address conversion confusion
- #define addr_t as ElfW(Addr)
- replace uplong by addr_t
- #define TCC_HAS_RUNTIME_PLTGOT and use it
2013-02-04 16:24:59 +01:00
grischka
ca38792df1 tccrun: another incompatible change to the tcc_relocate API
We are now compatible with the 0.9,25 version though.  A special
value for the second (ptr) argument is used to get the simple
behavior as with the 0.9.24 version.
2012-09-01 11:33:34 +02:00
grischka
a35b3059bb tcc.h: define TCC_IS_NATIVE
- disable tccrun feature for non-native (cross-) compilers
- define uplong for target adress size
- fix using -Wl,-Ttext=... for Win64 (tccpe: ADDR3264 imagebase)
2012-03-05 20:15:56 +01:00
Andrew Mulbrook
5775911dad Revert "Multiple fixes for 64 bit sections"
This reverts commit d7a7c3769d.
2012-03-03 10:12:06 -06:00
mob
d7a7c3769d Multiple fixes for 64 bit sections
This changeset attempts to fix a few problems when giving using
the high 32bits of a 64bit section offset. There are likely more
issues (or perhaps regressions) lurking in the muck here. In general,
this moves a few data type declarations to use uplong.  Also, add
support for 64bit mingw32 building under cygwin.  Because native
types are used for 64 bit offsets, this won't fix challenges with
cross compiling from 32bit -> 64bit.

Tested under cygwin, against binary compiled with
-Wl,-Ttext=0xffffff8000000000

Signed-off-by: Andrew Mulbrook <andrew262@gmail.com>
2012-02-26 19:02:51 -06:00
Milutin Jovanovic
8ca8b08890 Patch attempting to build OSX TinyCC.
Applied patch found on stackoverflow (link below). I also found some
related changes that looked like logically needed. The stackoverflow
changes addressed only two registers which were breaking a compile.
However reading the code in the same file shows two other register
accesses that, while not breaking the build, should have the same fix.

http://stackoverflow.com/questions/3712902/problems-compiling-tcc-on-os-x/3713144#3713144

The test driver was changed by changing 'cp -u' into 'cp' as '-u' is not
supported on mac osx.

I found that osx build required the WITHOUT_LIBTCC define. I suspect the
reason for this is tcc unability to handle mach-o files. In order to
properly address this I had to change 'configure' to propagate target os
name to Makefile.

Current state is that simple tests work, but not the whole 'make test'
suite runs.

To the best of my knowledge, these changes should not impact other
platforms.
2012-02-09 12:53:17 -05:00
grischka
bf374a5f23 rename error/warning -> tcc_(error/warning) 2011-08-11 17:07:56 +02:00
grischka
74a24d77fd libtcc: minor adjustments
- use {B} to substitute tcc_lih_path (instead of \b)

- expand CONFIG_TCC_CRTPREFIX in CONFIG_TCC_LIBPATHS
  which fixes duplicate CONFIG_SYSROOT.

- put default CONFIG_SYSROOT ("") into tcc.h

- remove hack from commit db6fcce78f
  because $(tccdir)/include is already in sysincludes

- configure: error out for unrecognized options.

- win32/build-tcc.bat: put libtcc into base dir where it will
  find lib/include automatically, and build libtcc_test example.
2011-08-11 16:55:30 +02:00
grischka
d483ab322f tccrun: win32: improve exception handler 2011-07-14 19:11:12 +02:00
grischka
08083ddb21 tccrun: win32: improve rt_get_caller_pc 2011-07-14 19:11:11 +02:00
grischka
d59bd8be8e tccrun: rt_printline: fix no-stabs case 2011-07-14 19:11:10 +02:00
grischka
af83993810 tccrun: improve rt_printline output format
Prefix line with "file:linenumber:" such that editors
can easily jump to the source location
2011-07-14 19:11:08 +02:00
grischka
df4c0892f3 tccrun: win64: add unwind function table for dynamic code
This works only when tcc.exe is compiled using MSC.  MinGW does
something in the startup code that defeats it.
2011-07-14 19:09:49 +02:00
Henry Kroll III
fb498eb925 selinux: correct ftruncate, fix bus error in tcc -run 2010-10-27 03:54:12 -07:00
Thomas Preud'homme
036d94112d Remove ifdef STT_GNU_IFUNC test in tccrun.c
STT_GNU_IFUNC is always defined (there is no conditional definition of
it) so the ifdef test for STT_GNU_IFUNC in tccrun.c has no reason to
be.
2010-10-10 16:25:41 +02:00
Thomas Preud'homme
776364f395 Add support for __FreeBSD_kernel__ kernel
Add support for kfreebsd-i386 and kfreebsd-amd64 Debian arch with
thanks to Pierre Chifflier <chifflier@cpe.fr>.
2010-09-10 21:09:07 +02:00