Commit Graph

740 Commits

Author SHA1 Message Date
Guest0x0
b8b6a5fd7b fix UB in constant folding of double -> signed integer conversion 2024-09-14 06:17:23 +00:00
Jonathan M. Wilbur
1cee0908d2 fix: tests broken by use of assembly 2024-07-31 04:31:48 -04:00
Jonathan M. Wilbur
f15008da05 fix: previous two commits 2024-07-31 04:15:45 -04:00
Jonathan M. Wilbur
e4d874d88a fix: code in non-executable ELF sections 2024-07-30 10:33:44 -04:00
Jonathan M. Wilbur
c85eface68 feat: treat unknown macros with arguments as undefined 2024-07-30 08:54:00 -04:00
grischka
6b78e561c8 div fixes
- Makefile: don't produce unknown targets
- libtcc.c: tcc_set_linker(): improve parser
- tcc.h: tcc_internal_error(): don't record __FILE__ (for privacy reasons)
- tccgen.c:
  - reject pointer + float operation
  - use 'int level' for builtin_frame/return_address
  - save_regs(): remove VT_ARRAY (confuses riscv64-gen)
- tccpe.c: store just basename of loaded dlls (rather than full path)
- tccpp.c: remove unused TAL defines
- *-link.c: add missing ST_FUNC
- i386-gen.c: fix thiscall
- riscv64-asm.c/arm-asm.c: stay simple C89
  - avoid .designators, decl after statement
  - avoid multiple instances of same static const objects
  - use skip() instead of next() & expect()
  - use cstr_printf() instead of snprintf() & cstr_cat()
  - tcc_error(), expect(): never return
2024-06-11 14:26:34 +02:00
Gynt
3b943bec5d implemented thiscall by copying logic from fastcall
implemented improved thiscall by using mov ecx instead of pop ecx

include __thiscall and __thiscall__ as aliases

remove fake line in test
2024-06-03 13:56:32 +02:00
grischka
2b0a663df9 libtcc usability improvements
- tccgen.c: cleanup switch data etc. after errors (*)
- tccpe.c: faster get_dllexports (*)
- tccpe.c: support -Wl,-e[ntry]=... (*)
- libtcc.c: win32: use ANSI functions (GetModuleFileNameA etc.)
- tccrun.c: be nice to tcc-0.9.26 ("struct/enum already defined")
- tccpp.c: be nice to tcc-0.9.27's va_start/end macros

(*) suggested by Robert Schlicht
https://lists.gnu.org/archive/html/tinycc-devel/2024-03/msg00012.html
2024-03-13 21:06:01 +01:00
grischka
9d2068c630 tccrun: add option CONFIG_RUNMEM_RO=2
/* 0 = .text rwx  other rw (memory: min 2 pages) */
/* 1 = .text rx   other rw (memory: min 3 pages) */
/* 2 = .text rx  .rdata ro  .data/.bss rw (memory: min 4 pages) */

tcc -vv -run ... shows some info.
Also when compiled with -DMEM_DEBUG:
tcc -bench -run ... shows some memory usage
2024-03-03 21:39:53 +01:00
grischka
ca061f3a96 i386-asm: fix pc-relative label ariths
See test. We need to use 'ind' from later when the address
field of the instruction is put.

Also: fix crash when the substracted symbol is undefined
Also: assume asm-symbols to be lvalues (except func/array)
2024-03-03 19:56:18 +01:00
grischka
42395a1912 tccrun: PAGEALIGN'ed mprotect
the un-mprotect() after run was severly off the limits.

Also in tcc.c:main: do not confuse errors with non-zero
results from tcc_run()
2024-02-29 22:52:02 +01:00
grischka
d2f8ceac7a tccrun: review last changes
- LIBTCCAPI int tcc_set_backtrace_func(void *ud, ...)
  accept opaque user data pointer,
- tcc -vv -run... : show section info
- use memalign() to allocate runtime memory
- printline_/dwarf : pass output to parent function
- tccpe.c : fix -nostdlib -run
- --config-backtrace=no : make it work again
2024-02-19 17:45:44 +01:00
grischka
c88b19966c tccrun: exit() via rt_longjmp()
- new LIBTCC API tcc_setjmp() to allow longjmps & signals
  from compiled code back to libtcc per TCCState
- new LIBTCC API tcc_set_backtrace_func() to handle backtrace output
- move c/dtor/atexit stuff to runtime (lib/runmain.c)
- move bt-log.o into libtcc1.a
- add timeouts to github action (beware, it did happen to hang
  infinitely in the signal handler at some point)
2024-02-15 18:45:49 +01:00
herman ten brugge
8d8d75ca75 Allow tcc to run with bounds checking enabled
tcc failed to run with bounds checking enabled because the functions
rt_wait_sem, rt_post_sem and _rt_error where defined twice.
This is solved by making them weak in tccrun.c

Also a nested lock was present when setting TCC_BOUNDS_PRINT_CALLS=1
This is solved in lib/bt-exe.c by moving lock/unlock code.

Also added a testcase in tests/Makefile to test tcc with bounds
checking enabled.
2024-02-15 07:17:15 +01:00
grischka
a7cd016d71 tccrun: 'tcc_relocate()' twice no longer supported
- abort with notice when tcc_relocate() is called with the
  former two-step method
- support backtrace & bcheck not only with tcc_run() but also
  for directly called functions from tcc_get_symbol(); enable
  witn 'tcc_set_options("-bt/-b");'
- move struct rt_context and debug sections into compiled code
  for TCC_OUTPUT_MEMORY also
- protect access (g_rc) with semaphore
Also:
- add armv7/aarch4/riscv64 github tests (qemu emulated)
- win32/build-tcc.bat: build cross compiler only with -x
2024-02-14 00:56:36 +01:00
grischka
7b9f19eaab tccpp: macro cleanup
- remove TOK_NOSUBST, mark the token itself instead
- get_tok_str(); mask out SYM_FIELD & update uses
- next(): optimize (~5% faster with tcc -E)
- tok_flags: remove some redundancy
- parse_define(): do not remove spaces around '##' and after '#'
    and mark macros with '##' as MACRO_JOIN to avoid unnecessary
    call to macro_twosharps(mstr):
- next_nomacro(): removed, next_nomacro1(): renamed to next_nomacro()
- next_argstream(): cleanup & new function peek_file()
- macro_subst_tok(): handle special macros (__DATE__ etc.)
  like normal macros if they are #defined
- -DPP_DEBUG : more structured output
- pp_error(): better preprocessor expression error message
- tcctok.h: sort basic keywords (somehow)
- testspp/Makefile: generate .expect with 'make testspp.##+'
- tcc.c: tcc -E -o file : put unixy LFs also on windows
2024-02-09 18:38:14 +01:00
grischka
b671fc0594 LIBTCCAPI tcc_relocate(s) : REMOVED 2nd argument
removed second argument for tcc_relocate(s). previous
'TCC_RELOCATE_AUTO' is now default and only behavior.

Rationale:
  In the past, the option to compile into memory provided by the
  user was introduced because only one TCCState could exist at a time.

  This is no longer a limitation.  As such it is also possible now to
  keep any number of compiled code snippets around together with their
  state in order to run them as needed.

- Also
  - LIBTCCAPI tcc_get_error_func/opaque() removed
  - tccrun/SELINUX: switch rx/rw mappings such that rx comes first
    (risc64-link.c:relocate_plt() does not like got < plt)
  - tcc_relocate_ex(): free local symbols and obsolete sections
    to reduce memory after tcc_relocate()
2024-02-09 13:38:27 +01:00
herman ten brugge
a0ab99169e Pointer diff should use signed size 2024-02-09 08:35:32 +01:00
Eric Raible
be6584e2b9 Allow use of a custom allocator in libtcc
When using libtcc it's reasonable to be able to use the application's
memory allocator for all allocations, including tcc_new(), and including
#define MEM_DEBUG

Ideally the allocator would be stored in the TCCState, like TCCErrorFunc.
That would imply a new API tcc_new_with_allocator(), but more importantly
would require all uses of tcc_malloc(x) to be changed to s->tcc_malloc(x).
That's a non-starter in my book.

Instead I refactored the memory management code so that all allocations
flow through tcc_realloc().  Which simply calls a function pointer, the
default value of which is the previous tcc_realloc().

It then becomess trivial to install a new allocator with the new function:
LIBTCCAPI void tcc_set_realloc(TCCReallocFunc realloc);

The resulting code adds the trivial cost of an additional function call
per allocation/free.  It also doesn't distinguish between malloc failure
and realloc failure, but since both just fprintf then exit() that seems
unimportant to me.

On the plus side the preprocessor magic is much more clear.  The diffs
don't hightlight that, but take a look at the result to see if you agree.

All tests passed on my x86 linux box.
2024-02-06 20:01:18 -08:00
grischka
da0d43903b review recent commits
tccpp.c:
 - revert "Preprocessor fix + new testcase"
   Fix was not a fix and nobody could understand the test.
   This reverts 6379f2ee76
 - better fix and add new test (pp/18.c)

tccgen.c:
 - remove global variables 'in_sizeof', 'constant_p'
 - rework comma expression (gexpr())
 - merge func/data 'alias_target' codes
   (See 08c777053c)
 - move call to do_Static_assert()
 - better error: "expression expected before '%s'"
 - fix "statement after label"
    - remove unnecessary second parameter to block()
    - remove unnecessary call to decl()
    - revert changes to old C89 test file
    See 7f0a28f6ca

tccelf.c:
 - rework "...make undefined global symbol STT_NOTYPE"
   (See f44060f8fc)
 - move tccelf_add_crtbegin() from libtcc.c

tcctest:
 - unfix K&R fix (keep old look of K&R functions)

tccrun.c:
 - exit(0) returns 0

libtcc.c:
 - move #defines for -dumpmachine
 - more explicit error "file not found"
   (as opposed to error while loading file)

tccpe.c, x86_64-gen.c, i386-asm.c, tccasm.c:
 - use R_X86_64_PLT32 for functions on x86_64-win32

tccdefs.h
 - empty #defines for _Nonnull, __has_builtin(), etc.

configure:
 - Simpler "macOS .dylib ... VERSION letters."
   (See 6b967b1285)

Makefile:
 - macOS version also
 - add cross searchpaths for packages

build.yml:
 - disable codesign on macos-11 (doesn't seem to work)
2024-02-04 18:18:40 +01:00
herman ten brugge
7d1bbc80d4 Update for clang
Clang also removed K&R support so define IMPLICIT_INT.
Fix clang warning in lib/bt-log.c
2024-01-17 07:07:48 +01:00
herman ten brugge
0059d89c0f Prepare for gcc 14
Gcc 14 reports -Wimplicit-int errors because old K&R is not supported
any more.
2024-01-16 22:21:19 +01:00
herman ten brugge
bbe2e5a421 Allow declarations in case statement
This now works:

 case 1:
    int z = 123;
    break;
2024-01-16 07:51:56 +01:00
herman ten brugge
c13bbb5cb5 Add type promotion in comma expression and update testcase 94 2024-01-06 07:54:34 +01:00
herman ten brugge
6379f2ee76 Preprocessor fix + new testcase 2023-12-29 09:08:25 +01:00
Rob Pilling
fb164e0ab4 Error out for incomplete type initialisation 2023-11-26 09:29:06 +00:00
Reimar Döffinger
36f53cdc3b Revert "tests: Add support for codesigning command."
This reverts commit ece74ceaaf.

codesigning is already supported via --config-codesign.
This patch actually broke testing for tcc builds with
that set.
2023-10-30 19:08:57 +01:00
Reimar Döffinger
ece74ceaaf tests: Add support for codesigning command.
Allows running the tests out-of-the-box on macOS.
Also delete *.dylib in tests2 dir on "make clean".
One remaining issue on macOS 14.1 is that the
-undefined warning option no longer works,
thus breaking test3 and test1b.
2023-10-29 17:06:59 +01:00
herman ten brugge
31206a5bb8 Fix tests/tests2/Makefile for win32 2023-09-07 20:19:42 +02:00
herman ten brugge
9bb9a04b5f Small fixes
The testcases 22_floating_point and 24_math_library did not work with
make tcov-test
Add -lm for these in tests/tests2/Makefile

gcc -fanalyzer complains about tcc_malloc and tcc_realloc because
malloc(0) and realloc(ptr, 0) is not tested correctly.
2023-09-07 20:05:51 +02:00
grischka
086870addd configury update & bump VERSION to 0.9.28rc
configure:
- option --targetos=... for cross build
- cleanup

win32/build-tcc.bat:
- option -b <bindir>
- make 'libtcc1.a' and cross-prefix-libtcc1.a
  (same convention as with makefile)

Makefile:
- streamline tcov-tests, help, etc.

workflow/build.xml: simplify
- using "windows-2019" runner (instead of windows-latest)
  because its msys seems more complete and has no problems
  with the 96_nodata_wanted.test either.

Changelog,TODO,USES,tcc-doc.texi: update
2023-09-06 22:42:52 +02:00
grischka
cd75ca692a stuff & fixes
libtcc.c:
- revert "Small patch to allow..." (someone's personal easteregg)
  (see da3a763e97)
- check return value from macho_load_tbd/dylib

tcc.c:
- remove help for "not yet implemented" option

tccelf.c:
- check PIE's for "unresolved symbols"

tccgen.c:
- avoid int->double->int cast
  (see a46372e910)
- fix constant propagation with pseudo long doubles
  (must mask out VT_LONG from type)
- cleanup find_field() (again)

tccpp.c:
- disallow strings and double constants in #if expressions

win32/include/uchar.h:
- change file mode
2023-09-06 22:42:30 +02:00
herman ten brugge
ff2a372a9a Add tcov support in Makefile
The following targets are added:
testc / testc2.all / testc2.37 / testc2.37+ / testc2.37-
testcpp.all / make testcpp.17
This allows to check that the testcase(s) test the code modified.
See Makefile tests/pp/Makefile tests/tests2/Makefile

lib/tcov.c: Fix while loops with fgets.

tcc.h: Fix tcc_p compilation with latest gcc

tests/tests2/22_floating_point.*: Better test floating point
2023-08-31 11:09:49 +02:00
herman ten brugge
b50a0bdf31 Add bound checking for test 132 2023-08-30 16:44:44 +02:00
herman ten brugge
598134fff6 Undo part of incr_offset patch.
The incr_offset offset code was not working with bounds checking.
So I reverted part of tccgen.c.
See new test code 132.

Also added some debugging code that prints location of
bounds checking calls. Needed this to find the problem.
See lib/bcheck.c, lib/bt-dll.c, lib/bt-exe.c, lib/bt-log.c, tccrun.c
2023-08-30 16:10:39 +02:00
grischka
c29420ab0d tccgen: update "Fix invalid load generated by gfunc_return()"
tccgen.c:
- new function incr_offset(int) to increment a lvalue
- use it in gv/vstore to load/store from/to two-word types
- use it to advance the pointer to struct fields
- use it to load/store structs passed in registers
- structs: always assume that reg-classes of registers are 2^n
- adjust stack space when regsize > sizeof the_struct

x86_64-gen.c:
- return regsize=16 for VT_QLONG/QFLOAT

i386-gen.c:
- pass structs of size(8) as two VT_INT rather than one VT_LLONG
  (both should work now)

fixes a82aff3337
fixes fd6d2180c5 (slightly)
2023-07-31 12:22:15 +02:00
herman ten brugge
fd6d2180c5 Align stack when returning structs.
The stack was not aligned when a returned structure was stored on stack.
This resulted in destoying of previous values stored on stack.
See testcase 119 (tst_struct_return_align) where value d is overwritten.
2023-07-31 09:24:06 +02:00
herman ten brugge
5b28165fbf Fix test 131 for 32 bits targets 2023-07-05 19:28:58 +02:00
Yao Zi
a82aff3337
Fix invalid load generated by gfunc_return()
On backends that rely on gfunc_return() to handle structures
  returned in registers (like RISC-V), gfunc_return() may generate
  invalid loads for structures without VT_LOCAL and VT_LVAL. This
  commit fixes it and adds a regression test
  (131_return_struct_in_reg)
2023-07-02 04:02:36 +08:00
herman ten brugge
30fd24abd4 Allow different names for pragma once
pragma once can now be used with
test.h
./test.h

and other references to the same file just like gcc/clang.

On linux we can use stat and st_ino to check for the same file.
On windows the st_ino does not work so we calculate a file hash
if the size of the file differs and then compare the hash.
2023-06-25 20:23:58 +02:00
herman ten brugge
a46372e910 Add support for constant float compare 2023-05-22 15:09:59 +02:00
grischka
bb93bf8cd2 stuff & etc..
- tccpp.c: rename ... to __TCC_BCHECK__/__TCC_BACKTRACE__
- libtcc.c: correct total line count
- libtcc.c: support -run -- args... (instead of -run @ args ...)
- Makefile/build-tcc.bat: streamline GITHASH string somewhat
- bt-exe.c: avoid redef of pstrcpy() with 'tcc -bt tcc.c ...'
2023-04-25 15:22:31 +02:00
grischka
86f3d8e331 tccpp: remove unwanted space with __VA_ARGS__
reported by "vsfos"
https://lists.gnu.org/archive/html/tinycc-devel/2023-04/msg00007.html
2023-04-20 14:18:29 +02:00
Ziyao
b006b98334
Fix passing of large function arguments on riscv64
- Correct stack adjustment in gfunc_call()
- Add a regression test (130_large_argument)
2023-04-09 06:06:10 +08:00
herman ten brugge
95aab7d687 tests/tcctest.c: Add support for osx with clang 10 2023-03-18 14:02:52 +01:00
Michael Matz
4dc4e93f1d Fix 128_run_atexit.c on linux
if we include standard headers on glibc-based systems then
we can't use '__attribute__' (those are defined away for unknown
compilers) but must use '__attribute' (or #undef that token).
2023-03-13 17:01:35 +01:00
grischka
19e3e10e4b small scopes cleanup etc.
tccgen.c:
- just track local_stack for small scopes (can't declare variables)
- fix a vla problem with nested scopes
- move debug N_L/RBRAC into curly braced block

Also:
- tccpp.c: move 'label_...' functions to tccgen.c
- tccpp.c: let get_tok_str() say "<no name>" for anonymous symbols
- tcctest.c: let __pa_symbol() work for memory > 2GB
- 119_random_stuff.c: revert strtoll test (no reason to test libc)
- tccdefs.h/tcctest.c: enable bit fncs for _WIN32
- Makefile:
  - use i686-linux-gnu instead of i386-linux-gnu for cross-i386
  - update 'make help'
  - revert umplicit 'make all' with 'make install' (but print warning)
2023-03-12 20:40:50 +01:00
grischka
5f08561e10 Revert ""Fix" nocode_wanted in expr_landor"
instead in vcheck_cmp(), pass the CODE_OFF_BIT to generators
unless other nocode purposes are set.

This reverts commit cad8739594.

Also in 128_run_atexit.c:
avoid line output disorder which may happen on windows when
tcc itself and runned code are using two different printf
implementations and tcc would print the "[Returns 1]" above
any output from the runned test.
2023-03-12 13:39:07 +01:00
herman ten brugge
cad8739594 Fix nocode_wanted in expr_landor
The generated code by yarpgen failed. I traced the problem to
expr_landor. The problem was that nocode_wanted was removing
too much code.
See also testcase 33.
2023-03-11 07:51:00 +01:00
herman ten brugge
88d5c70bdb Update casted boolean expressions
The problem was that enums can also be present in vset_VT_JMP.
Also see testcode.
2023-03-11 07:35:42 +01:00