Commit Graph

3624 Commits

Author SHA1 Message Date
herman ten brugge
edcd228214 Init global_expr variable
This is needed when errors occur when compiling with libtcc.
A next compile with libtcc results in undefined symbol.
2025-10-15 10:02:09 +02:00
herman ten brugge
96229004c4 Fix debugging after commit tccgen: more of scope hacks
The commit added some extra defines that where not
handled in debugging code.
2025-10-15 09:01:54 +02:00
herman ten brugge
c96f0cad61 Free all preprocessor memmory in case of error. 2025-10-13 15:57:31 +02:00
grischka
34b45a69ff Reverts & cleanups
- include/stddef.h, tcctest.c
  Revert "tests/tcctest.c: include stdint.h"
  This reverts commit 8f23997ca7
  We don't want tcctest.c to rely on system include files

- libtcc.c:
  Revert "libtcc.c: Remove unused defines free and realloc"
  To be unused is the point why they do exist
  This reverts commit 2f88764100.

- tcc.c:
  fix formatting of commit e73529865d

- tccpp.c:
  parse_include(): print skipped include files too (with tcc -vv[v] file)
  next_nomacro(): faster L"str" parsing

- tccgen.c: fix c2y if declaration:
  * accept like GCC: if (int a = 0, b, c = x; c)
  * accept as "TCC extension": if (int a = 0, b, c = x)
  * "speak tcc" with symbol/function names

- README:
  cleanup
2025-10-09 13:08:33 +02:00
herman ten brugge
ce8b3432bf Allow gcc 16 for build 2025-10-07 14:32:13 +02:00
Stefan
bcfb872fd0 Makefile: Add space behind 'install -m'
There are install shell scripts around, often derived from X11 like
contained in glibc or Gash sources, which require to separate -m from
its value. Therefore add a space behind 'install -m' in the Makefile.
2025-10-03 17:58:45 +02:00
Stefan
ba0899d909 tccasm.c: Make .init and .fini sections executable
GAS selects default attributes for a section based on its name and makes
the sections .text, .init, .fini executable. The crti.s and crtn.s files
of musl rely on these defaults and don't set the "x" flag for the .init
and .fini sections. Unfortunately TCC does not care for .init and .fini
section defaults in assembler or C code. Fix this for assembler code.
2025-09-21 14:06:59 +02:00
Stefan
2f88764100 libtcc.c: Remove unused defines free and realloc
These defines are not used in the code below their definition. They are
defined in tcc.h as well.
2025-09-21 13:30:15 +02:00
Stefan
95d184cba1 i386-asm.c: Optional % for variables in specific registers
Handle 'register long r10 __asm__("%r10")' without %, too. Using GCC
the % is optional. The musl source code does not use the % prefix.
The GCC source code uses both variants.
2025-09-21 13:18:37 +02:00
Stefan
5aebf106e8 i386-asm.c: Use NB_ASM_REGS instead of 8
For x86_64 there are more than just 8 registers.
2025-09-21 12:21:27 +02:00
Stefan
9dc59f9c23 i386-asm.c: Do bit-check for OP_SEG in asm_opcode
Fix a bug with "call *%%gs:16", which sets the two flags OP_INDIR and
OP_SEG in pop->type.
2025-09-21 12:18:11 +02:00
grischka
de8c19e1da tccgen: globalize function return type too
Commit 38ab5f65b3 was missing
to move the function return type to the global stack too.

Also, check redefinition in sym_push(), as before.
2025-09-18 19:32:19 +02:00
noneofyourbusiness
47106ce103 tccgen.c: silence logical op warning
behavior is unchanged
2025-09-18 11:42:58 +02:00
noneofyourbusiness
8f23997ca7 tests/tcctest.c: include stdint.h
despite being defined in tcc's own stddef.h, intended to be included by
libtcc.h, libtcc.h actually included the libc's standard include

tested on musl
2025-09-18 11:17:55 +02:00
noneofyourbusiness
e73529865d tcc.c: add information about reporting bugs to the mailing list 2025-09-18 09:58:21 +02:00
grischka
38ab5f65b3 tccgen: more of scope hacks
* case 1: local scope of 'ff'
  int main() {
    int ff = 123;
    {
      int ff(int):
      ff(456)
    }}

* case 2: linkage of a static extern symbol
  (older gcc did allow that)
  static int ff(int);
  int main() {
    int ff(int):
    ff(456)
  }

Also:
- cleanup enum, let sym_push() handle redefinition
- just mark incomplete array base types, unshare only
  when needed (in decl_initializer_alloc())
- fix sizeof empty array (= 0) : int ii[] = {};
- rename 'Sym' members used by __attribute__((cleanup(f)))
2025-09-08 17:23:18 +02:00
grischka
ce4de961af tccpe: add COFF symbol table for GDB
wanted by gdb to have global variables info on windows

Also:
tcc.h:
- move dwarf inline fns
- use dwarf as default for 64-bit platforms. GDB can't handle
  locals info from stabs on 64-bit correctly (also not from gcc).
tccpe.c:
- use 's1' instead of 'pe->s1'
tccdbg.c:
- fix locals info problem on Windows where (unsigned long) s->value
  is uint32_t and would not cast to long long correctly.
- tweak dwarf to have line info at begin of functions, otherwise
  "gdb b main, r, n" would say "no line number info, single step..."
  (see also objdump --dwarf=decodedline ...)
2025-09-08 17:22:21 +02:00
herman ten brugge
03072a301c Use configure to set note release bsd
Using uname in tccelf.c was not working when cross compiling.
So move it to configure script.
2025-09-04 06:47:40 +02:00
herman ten brugge
8c3396a76f Fix cross compiling on windows 2025-09-03 07:43:01 +02:00
herman ten brugge
b45cea5f4b Update bsd notes
freebsd 13 must not have note section.
freebsd 14 must have note section.

also added version info.
2025-09-03 07:09:42 +02:00
herman ten brugge
b427545419 Update for freebsd versions >= 14.0 2025-08-31 12:24:03 +02:00
herman ten brugge
e6ea0d0424 tccgen: fix several problems
Fix 'void func2(char *(*md)(char *md))' declaration.
Fix global array and local extern array problems.
Fix scope problem with old function declaration.
Fix 'typedef int t[]' declaration. Empty size should remain.
2025-08-30 07:13:49 +02:00
herman ten brugge
8c59fd3cb6 Check enum range for typed enums 2025-08-24 11:22:38 +02:00
herman ten brugge
6ca6328e29 Fix long double to long long for x86_64 2025-08-23 14:48:34 +02:00
grischka
acb2a909dd tccgen.c: fix two scope problems
... see tests

Also:
- tccgen.c: cleanup _Generic a little
- libtcc.c: cleanup mem-debug a little
- arm-link.c: read/write only once
- tcc.h: another fix for clang offsetof
- tccdbg.c:
  * stabs: handle forward struct/enum decls
  * stabs: fix anonymous struct members (must not have a .L123 name)
  * avoid strncpy()
2025-08-22 19:14:44 +02:00
herman ten brugge
4e6c4db340 Small debug cleanup
A little bit better string hashing
Move free code to seperate function
2025-08-22 16:45:06 +02:00
herman ten brugge
e5e3dc687b Make debug info smaller
Currently debug size is big because some types are output multiple times.
Function scope was not treated correctly.
Rename anon(ymous) into forw(ard).
Compact string table by reusing names.
2025-08-22 10:27:59 +02:00
Matthias Gatto
f52b480f89 c2y if declaration (std n3356)
standart is here: https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3356.htm

This feature should not add bug to existing features,
as it is called only if there is a declaration in a switch or an if.
2025-08-22 00:01:58 +02:00
herman ten brugge
80e7040d0c tcc loops with some input arguments
This is one example:
tcc -c a b -o c

Detected with some configure scripts.
2025-08-18 22:28:05 +02:00
grischka
2662b7b43c tccgen: local scope for types of function parameters
int foo(struct xxx {int x[3];} *p) { ...

We want 'xxx' be visible only inside the function. To get that,
the patch removes the 'sym_push(param)' in xxx-gen.c, and instead
(in tccgen.c:gen_function()) pushes all symbols that were newly
defined during parsing of the parameter list in post_type().

Also,
- decl_initializer_alloc():
  patch existing globals earlier, which updates flex arrays too
- let patch_type() do the 'redefinition' check and FUNC_OLD update
2025-08-18 21:06:03 +02:00
grischka
deb7a3fc73 tcc.c:main() free all & etc...
tcc.c:
- be nice to leak checkers
tcctools.c:
- remove unused TCCState params
tccrun.c:
- call bound_exit() after signals to let it free mem
tccelf.c:
- use section_add() instead of section_ptr_add() when
  more appropriate
tccpp.c:
- use size_t to align tal_header naturally
- 'POINTER_SIZE' and 'PTR_SIZE' in the same source is confusing
- "char file_name[TAL_DEBUG_FILE_LEN + 1];" looks silly.
- next_nomacro(): skip UTF8 BOM at BOF
tccgen.c:
- get rid of STMT_EXPR clause on top of block
- warn with useless type like 'int;'
- move skip()'s in block() for better error line-info
- BIT_SIZE bits are meaningful only with VT_BITFIELD
  (not with enums for example)
workflow/test-win32:
- build with MSVC using build-tcc.bat also
alloca.S:
- fix 'off by one' problem on win32 (must touch current page
  too because the 'push %edx' at the end could touch the next page)
- must not align greater than 4 when used for struct args
  (i386-gen.c:gfunc_call())
libtcc.c:
- accept -g1dwarf (dwarf output, only line info)
2025-08-18 20:43:52 +02:00
herman ten brugge
8845b6cd45 Make signed/unsigned bitfields the same as gcc/clang 2025-08-18 16:22:00 +02:00
herman ten brugge
199e135142 arm double to (unsigned) long long conversion
Bug found with openssl test suite.
2025-08-17 08:49:29 +02:00
herman ten brugge
f8eb5f4ab8 Add -znodelete linker option
This option is needed for openssl to create a correct library.
2025-08-16 18:29:19 +02:00
herman ten brugge
19fdef46f9 Allow mixing old/new function prototypes
This fixes another external testsuite failure
2025-08-15 09:59:11 +02:00
Marek Knápek
571868c4f7 Fix for MSVC's usage of __declspec(align(#)). 2025-08-15 04:41:24 +02:00
herman ten brugge
03184fe33b riscv target did not handle FUNC_OLD correctly. 2025-08-11 06:49:22 +02:00
herman ten brugge
37e1c88d78 Convert float to double for old function code. 2025-08-10 22:02:44 +02:00
herman ten brugge
087cf2e579 x86_64 bound checking failure
The code:
void mul(double *p)
{
    *p *= 2.0;
}

failed on x86_64 because register was not loaded after
bound checking call.
Also printed size when pointer indir failes.
2025-08-10 21:55:48 +02:00
herman ten brugge
fa9795406d Do not read past array end in struct return 2025-08-07 11:32:09 +02:00
herman ten brugge
9dffcd29d3 Fix tests/boundtest.c
The test leaks memory on arm/arm64/riscv because alloca is
replaced by malloc and the memory is not freed for test16 and test17.
2025-08-05 22:14:13 +02:00
herman ten brugge
311218ee5f Allow testsuite to be build with CC -fsanitize
Makefile: Add sani-test* targets + help
tcc.h: redefine offsetof for clang -fsanitize
tccelf.c: section_ptr_add: allow clang -fsanitize
          tcc_add_btstub/tcc_load_object_file: revert previous change.
tccpp.c: Fix -fsanitize problem by casting to unsigned.
tests/Makefile: tcc -v leaks memory (a lot of returns in main() leak memory)
tests/tests2/Makefile: testcase 112 aborts and leaks memory. Disable leak test.
2025-08-05 16:47:11 +02:00
herman ten brugge
5364bc7c82 Fix i386 alloca code.
I commited the wrong version in previous commit.
2025-08-03 18:41:32 +02:00
herman ten brugge
6694391b74 Update gcctestsuite
I updated the tests/gcctestsuite.sh a bit.
before:
  3329 test(s) ok.
  210 test(s) skipped.
  168 test(s) failed.
  28 test(s) exe failed.
after:
  3331 test(s) ok.
  299 test(s) skipped.
  79 test(s) failed.
  26 test(s) exe failed.

I found some small problems:
include/tccdefs.h: Add alloca definition for i386 and x86_64
lib/alloca.S/lib/alloca-bt.S: align i386 alloca to 16 bytes.
			      i386_gen.c vla code and gcc do the same.
x86_64-gen.c: fix typo in comment
2025-08-03 10:35:58 +02:00
herman ten brugge
80bef6162a Unshare typedef definitions if it used more then once. 2025-08-03 10:29:16 +02:00
herman ten brugge
8a0ccbbd94 Another update for clang -fsanitize
arm-link.c:    remove some casts
tccelf.c:      code style and remove code
tccpp.c:       use correct pointer size so cross compiling works
riscv64-gen.c: make code more readable
2025-08-03 10:22:26 +02:00
herman ten brugge
666e88ee2a Update for clang -fsanitize
I tried to fix all remaining warnings/errors reported by -fsanitize.

The riscv64-gen.c is a bit large because it contained a lot of warnings.
I forgot some arm-link.c changes in last commit.
The other changes are all small fixes to avoid warnings/errors.
2025-07-29 08:26:57 +02:00
herman ten brugge
8025a829cc Check with clang -fsanitize
Tested code with:
clang -fsanitize=address,undefined,nullability -pie -fPIE -Iinclude -I. -g tcc.c -o tcc.tcc -lm -ldl -lpthread
./tcc.tcc -Iinclude -I. -b -g tcc.c -o tcc.tcc1 -lm -ldl -lpthread

Also checked on i386/x86_64 with -fsanitize=memory (others not supported).

arm-link.c: use read32le/write32le/add32le to avoid unaligned access
tcc.h i386-asm.c: fix signed left shift
lib/bcheck.c: Add _Atomic
libtcc.c: Correct MEM_DEBUG_CHECK3 to avoid unaligned access
riscv64-link.c: Fix R_RISCV_SET16
tccpp.c: Align tal_header_t to avoid unaligned access
tccgen.c x86_64-gen.c: avoid use of uninitialized value

There are still warnings reported:
tccgen.c:4031:13: runtime error: member access within null pointer of type 'TCCState' (aka 'struct TCCState')
tccelf.c:321:22: runtime error: applying zero offset to null pointer
tccelf.c:1132:23: runtime error: applying non-zero offset 169184 to null pointer

A lot of left shift of negative value warnings.

I ignored these for the moment.

Also the -run option does no work well with -fsanitize. It gets confused
because it does not detect that the generated code in memory is used
without -fsanitize option.

There are a lot more -fsanitize options. I did not find serious problems
with them.
2025-07-27 11:22:16 +02:00
DAVID
a2902d37a1 testing 2025-07-21 00:48:51 -03:00
Haelwenn (lanodan) Monnier
2ffb5869e0 lib/atomic.S: add GNU libatomic copyright notice 2025-07-19 00:30:12 +02:00