Commit Graph

3555 Commits

Author SHA1 Message Date
Detlef Riekenberg
6ca228339c arm-asm: Accept additional register names 2025-05-07 22:29:46 +02:00
Detlef Riekenberg
30c2373c8a Accept -MMD,depfile for the Linux Kernel 2025-05-07 22:25:29 +02:00
kbkpbot
36ff4f52b5 Add GCC-style atomic builtins: __atomic_*_n
This commit adds GCC-compatible atomic builtins to stdatomic.h for
better compatibility with code relying on GCC's atomic primitives.

1. ​**​New Macros​**​:
   - `__atomic_compare_exchange_n`.
   - `__atomic_load_n`.
   - `__atomic_store_n`.

2. ​**​Motivation​**​:
   - Improves compatibility with codebases using GCC's atomic builtins
     directly.
   - Simplifies atomic operations by allowing direct value passing
     (instead of pointers), enhancing code readability.
   - Aligns TCC's atomic support closer to GCC/Clang for cross-compiler
     projects.

3. ​**​Testing​**​:
   - Verified with basic test cases (integer CAS, load/store) on x86.
   - Confirmed no regressions in existing atomic operations.

- This change does not affect existing C11 `atomic_*` APIs.
- Struct types are intentionally unsupported in `_n` variants due to
  complexity.
2025-04-19 08:05:27 +08:00
grischka
f10ab130ec linker options & tcc_load_ldscript() reworked
cleanup libtcc.c:tcc_set_linker()
cleanup tccelf.c:tcc_load_ldscript()

Also
- tccrun.c, tccelf.c:relocate_syms():
  with tcc -run -nostdlib, do resolve but only from explicitly
  on the command-line given libraries.
- tccgen.c: optimize UMOD x % c -> x & (c-1) for c = 2^n
- tcc-doc.texi: cleanup
- tcc.h, tccpp.c, libtcc.c: add 'size' arg to pstrncpy()

Also reorder functions in libtcc.c a bit.
9 files changed, 556 insertions(+), 617 deletions(-)
2025-03-28 21:23:19 +01:00
grischka
b3381269d7 pp-expression numbers always long long
Aka 'intmax_t', as recommended by newer standards.
For example
    if (-0x80000000 < 0) is false,
but
    #if (-0x80000000 < 0) is true
because in C, 0x80000000 is an unsigned int, Whereas in
a preprocessor expression, it is a signed long long, even
without LL suffix.
2025-03-26 20:36:33 +01:00
grischka
f57cc34a0c Revert "Fix tcc -run on Windows" (almost)
Oviously what the patch wants is... on Windows, with
"tcc c:/dir/xyz.dll -run file"... pass the absolute
path of the dll to LoadLibrary() which can make sense
in situations.

Other changes in the patch to other platfurms seem to
have no effect. This reverts 52a9a541b0
except 2 lines in tccpe.c.

Also revert _Float16 patch a06c608625
As long as tcc does not really handle _Float16, we can just define
it in tccdefs.h.

Also move uint128_t to tccdefs.h for same reason.

Update github action (might fix random arm64 crashes)
2025-03-26 20:36:20 +01:00
Detlef Riekenberg
5527ca6dcb libtcc: Remove unrelated debug code 2025-03-20 00:34:39 +01:00
waterlens
52a9a541b0 Fix tcc -run on Windows 2025-03-19 12:03:52 +08:00
waterlens
a06c608625 Add fake _Float16 type to enable the latest math.h on macOS 2025-03-19 00:23:20 +08:00
grischka
8c4e67380e arm bits
arm-asm.c: add forward branches & some ops
lib/armeabi.c: faster i/udivmod (asm)
arm-gen.c: use movw/movt to load constants for CPUVER >= 70
configure: use dwarf-4 debug sections on android, detect idiv
lib/Makefile: always add -I$(TOP) for access to config.h
tcdbg.c: add a "code mapping symbol" to help out llvm-objdump
tccelf.c: don't load SHT_ARM_EXIDX, modify ARM.attributes
tccpp.c: accept '@' for arm asm comments
2025-03-11 22:56:22 +01:00
grischka
006174449e cleanups & stuff
libtcc.c:
- free 'elfint' string
- acceot  -O and -Os
- accept -gstabs (to override dwarf when default)
- better -Wp,...

tccpp.c:
- #line cleanup
  also warn with "extra tokens after directive"

tccgen.c & xxx_gen.c:
- force CPU flags to register earlier

tccelf.c:
- tcc_load_object: align size only for code sections
  data/bss objects are always put with their specfic type align
      (in decl_initializer_alloc())
  x86/64 doesn't need aligned code
  from c6afdff7ab

tccpe.c:
- enable dllimport for "_imp__<sym>" also from assembler

x86_64-gen.c & lib/libtcc1.c:
- simpler fneg without libtcc1 reference

tests2/134_double_to_signed.c:
- a tcc compiled by msvc won't pass this test
2025-03-11 22:56:01 +01:00
Reini Urban
f075851f9d tcc -hh: missing \n 2025-03-11 13:16:39 +01:00
remph
6ec4a10652 Process linker directives beginning with a colon
eg. -l :crti.o, with the same meaning as for other linkers (search library
paths for an exact match)

Also clean up some copy/pasting
2025-02-16 13:44:37 +00:00
remph
1747b45649 Fix -Wl,-nostdlib behaviour for compatibility with other compilers
TCC treats -nostdlib and -Wl,-nostdlib as equivalent, but on other compilers
which call a discrete linker, -nostdlib behaves as on tcc (not adding
startfiles/libc/endfiles) by modifying the ld command line, but -Wl,-nostdlib
adds -nostdlib to the ld cmdline, which stops the linker searching the default
directories for libraries.
2025-02-16 13:43:37 +00:00
remph
ec60d28e0f Add -Wl,-I and -Wl,-dynamic-linker options
Compatible with ld.bfd, gold, lld and mold. Also, document existing
behaviour of LD_SO environment variable
2025-02-16 13:35:20 +00:00
remph
f8bd136d19 Update tcc-doc.texi: bring in line with `tcc -hh' output 2025-02-13 02:50:05 +00:00
herman ten brugge
f6385c0530 Use TOK_PPNUM/TOK_PPSTR instead of TOK_CINT/TOK_STR
I noticed that '#line 0x20' worked.
So use the preprocessor macros.
2025-01-06 19:46:04 +01:00
herman ten brugge
999ec460a6 Allow macro in #line directive
Using:
  #define LINE1 10
  #line LINE1
  #define LINE2 20
  #define FILE "file"
  #line LINE2 FILE
Should now work.

Add new testcase tests/pp/23.S
2025-01-05 20:10:06 +01:00
herman ten brugge
68000c01ae Print correct values for testcase 134 on all targets
Testcase 134 only worked on x86_64 and i386 not on other targets.
2024-12-30 07:31:16 +01:00
kbkpbot
eef2db71a9 revert commit b8b6a5fd7b
As it will cause printf("%llu\n", (unsigned long long)1e19); output
9223372036854775808
instead of
10000000000000000000
2024-12-30 12:19:01 +08:00
herman ten brugge
c6afdff7ab Aligb section at end in tcc_load_object_file
Noticed this when the crt1.o file on an updated riscv had a
text section where the size is not multiple of align.
This file is loaded first and when the c code is then compiled
the code is not aligned any more.
This results in:
alignment of code section not multiple of 4
in riscv64-gen.c
2024-12-27 09:19:03 +01:00
kbkpbot
90ae383f62 x86_64/i386: Add missing fetch CPU flag
In x86_64-gen.c/i386-gen.c, gfunc_call will generate structure store
for bt == VT_STRUCT. Before generating any code, it is needed fetch
cpu flag.
As #ifdef TCC_TARGET_PE, gfunc_call() forgot to do this.
2024-12-26 19:17:49 +08:00
kbkpbot
af1cfd9e82 fix x86_64/i386 gfunc_call, when arg is VT_STRUCT, need fetch cpu flag before generating any code 2024-12-26 12:30:07 +08:00
Scott Graham
34b7b2cef5 Add lib/builtin.c to win32/build-tcc.bat
It looks like this is in the Makefile build, and it appears to be
necessary to make __builtin_ctzll available.
2024-12-15 12:49:16 -08:00
grischka
8620a312b2 tcc -run file.c : pass original exit code from file.c again
when there are no other errors then exit tcc with the exit cude
from tcc_run() (messed up in dd2e5f8b06)

Also in tccrun.c, use a more exotic random value to replace zero
with 'exit(0)' in user code (because lonhjmp(jb, c) needs c != 0)
2024-12-13 10:55:25 +01:00
Keith Thompson
b776bfaa53 Define LDBL_MAX_10_EXP for aarch64, riscv
There was a duplicate definition of LDBL_MAX_EXP, which this commit
removes.

There was a missing definition for LDBL_MAX_10_EXP, which this
commit adds.

I've confirmed the value of LDBL_MAX_10_EXP using gcc on an AARM64
Debian system (gcc 8.3.0).  I've only indirectly confirmed the value
on RISCV (using "#if ... #error" on godbolt.org).  I'm reasonably
sure this update is correct, but someone should confirm it before
merging to "mob".
2024-12-12 18:25:20 -08:00
herman ten brugge
ea75d5cf39 Add symver support for FreeBSD >= 14 2024-12-12 20:59:50 +01:00
Scott Graham
0ce0533854 add AddVectoredContinueHandler and AddVectoredExceptionHandler to kernel32.def 2024-12-12 11:56:19 -08:00
herman ten brugge
68c8c352fd Avoid Invalid relocation and section conflict on bsd.
With recent changes I got:
/usr/lib/crtbegin.o: error: Invalid relocation entry [ 2] '.rela.text' @ 0000007a
And:
libtcc.o: error: section type conflict: .eh_frame 01 <> 70000001
2024-12-12 20:46:00 +01:00
Avi Halachmi (:avih)
d440ed819c lib/Makefile: fix out-of-tree build of lib/bt-exe.c
Commit a522213 ("tccpe.c: never assume static...") removed the global
-I$(TOP) for cleaner lib build, and ensured that it is added for
bcheck.c as it now includes config.h, but forgot that bt-exe.c also
needs config.h, as it includes ../tccrun.c which includes it.

Also, the explicit tccrun.c prerequisite for bt-exe.c is not required,
as all *.o lib files depend on $(TCC), which depends on libtcc, which
depends on tccrun.c .

Also, at win32/built-tcc.bat, while bcheck.c needs -I.. for config.h,
bt-exe.c doesn't, as ../tccrun.c does find it where it expects it.
2024-12-07 00:53:16 +02:00
grischka
a522213cc8 tccpe.c: never assume static system libtaries
just clear s1->static_link before loading msvcrt etc.

Also:
- configure: get cc_version/name when making a cross compiler too
- configure: fix android triplets
- Makefile: remove CONFIG_TCC_CROSS, check TCC_TARGET_xxx instead
- libtcc.c: parse some linker option arguments more correctly
- tccelf.c: fix a versym problem with clang on android
- lib/Makefile, build-tcc.bat: bcheck.c now includes config.h
2024-12-06 16:21:30 +01:00
Avi Halachmi (:avih)
315828720d configure: --config-mingw32: clarify values, simplify code
Elsewhere at configure the value of $mingw32 is expected to be
exactly either yes or no (not empty, not caps, etc), and while it
complies internally, it also accepts arbitrary override value.

Clarify acceptable values, and simplify the parsing.
2024-12-04 20:04:08 +02:00
Avi Halachmi (:avih)
126b1ffd10 configure: win32: error only once about 'ln failed'
Don't try ln again if it failed, it won't have better luck next time.
2024-12-04 20:04:08 +02:00
Avi Halachmi (:avih)
c45559e124 configure: handle spaces correctly in $source_path
Add very few missing quotes where IFS/globs were applied to arbitrary
user input (commands, arguments, but not assignments or case $var in.
In fn_makelink $dn/$2/$f don't have IFS/globs).

While configure now handles it correctly, there are 2 issues:
- eval opt=\"$opt\"  coalesces IFS chars (--prefix="$HOME/x   y").
- Even with the eval removed, (gnu) make still fails to find it.

I made few quick experiments with quoting these values in config.mak,
but that had no impact, and make -d was not helpful. Was worth a try.

So no spaces in paths, but at least configure can handle the src path.
2024-12-04 20:02:06 +02:00
Avi Halachmi (:avih)
9289c6c5e0 configure: confvars: warn/error on bad key or value
$confvars is not supposed to hold the same key more than once, and
spaces in values won't be parsed correctly. Additionally, it's later
iterated using shell IFS-split - which will also apply globs.

We now abort if the value IFS-splits badly (spaces/glob/empty).

We now warn on duplicates, but still accept them like before.

Example duplicate: --enable-static --disable-static
Example spaces:    --config-foo="bar baz"
Example glob:      --config-foo=" * "

Note that globs currently already expand at  eval opt=\"$opt\"
before we get a chance to test it (but we still detect the spaces).
See commit message of 21272067 (boilerplate var=..) about removing it.

These tests are also performed on values which configure itself adds,
but currently there are no issues with those.

Also, default_conf() now uses confvars_has, which fixes the following:
- False-positive if the key is a substring of existing key or value.
- Incorrect test if the value contains '=' or sh pattern chars *?[] .
No-op, because current default_conf calls don't have such issues.
2024-12-04 18:53:58 +02:00
Avi Halachmi (:avih)
ab6e750bd5 configure: avoid boilerplate: confvars="$confvars ..."
Trivial, but less noisy when reading, and nicer for new code.

The code still adds the value[s] unconditionaly without checking for
duplicates or spaces in values - both are bad, but next commit will.
2024-12-04 18:53:40 +02:00
Avi Halachmi (:avih)
2127206790 configure: avoid boilerplate: var=echo $opt | ...
Add and use "assign_opt" instead of copy-pasting subshell assignment.

Slightly faster, and fixes option values with consecutive spaces,
for instance --libdir='/foo   bar' where previously `echo $opt | ...`
coalesced IFS chars because $opt was unquoted. (this is still very
likely to break, but at least now not at the options parsing).

Unrelated note:

The code does  eval opt=\"$opt\"  for every argument, to "reproduce
autotools behavior" (commit 2e7a1af, 2012-06-12, Thomas Preud'homme).

This is questionable, and not fun (try: --config-x='"; echo "PWNED').
I emailed the author for more info, but didn't get a reply aftre few
days, and without real-world use cases, I think it should be removed.
2024-12-04 18:53:40 +02:00
Avi Halachmi (:avih)
6f4b384e79 configure: avoid non-POSIX: local var=...
The vast majority of shells do support "local", but not all, notably
AT&T ksh (default sh in illumos-based distros and Solaris), but also
some other POSIX-compliant shells, so remove "local".

print_str(), print_num() are modified trivially.

default() now uses standard "Assign Default Value" - same semantics,
and works in all shells (and POSIX).

default_conf() is identical to before, but it had and still has few
minor issues, which will be addressed in a future commit.
2024-12-04 18:52:52 +02:00
Avi Halachmi (:avih)
05ebe494dd configure: avoid non-POSIX: test ... -a/-o ...
Shells do support those, and typically simple forms do work, but
these were removed in POSIX 2024 because they are notoriously hard
to parse correctly, and shells don't always agree on the result.

Instead, use standard forms which all shells support identically.

Extreme high level overview of quotes in POSIX shell:
- WORDs without $... or glob don't need quotes (echo, =, no, x86, ...).
- a=  b=foo  c=$...  and  case $d in ...  all don't need quotes.
- IFS/glob affect command and arguments (not assignments or case..in):
  - IFS only splits direct result of $..., quotes will prevent that.
  - Glob then splits/matches *?[]      and quotes will prevent that.

POSIX "test" (see Application Usage), and shell language (quotes):
  https://pubs.opengroup.org/onlinepubs/9799919799/utilities/test.html
  https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html

My own quick guide:
  https://gist.github.com/avih/6752ad1e20b334b56fef120cd09c766e
2024-12-04 18:52:35 +02:00
Avi Halachmi (:avih)
cea857bf73 configure: win32: don't fail when building using tcc
Commit 729918e ("make: make shorter command lines", 2024-11-21) added
"-static" when $cc_name is gcc to statically link with the mingw gcc
runtime, so that tcc.exe won't depend on additional non-system dlls.

However, $cc_name is still the default value at this time - gcc,
so "-static" was unconditional, and it failed if $cc is tcc (msvcrt).

This commit instead checks the already known $cc, and restores the
ability to build tcc using tcc (if "$cc" doesn't contain "gcc").
2024-12-04 18:21:46 +02:00
grischka
729918ef35 make: make shorter command lines
Put former NATIVE_DEFINES into config.h.  Such tcc can be run
and tested directly from source more easily, like for example:

    tcc -run tcc.c -B. -run test.c

Also:
- tccelf.c: cleanup
- tccpp.c: avoid stupid clang warning
- configure: reduce -Wno- switches
- tcc.h: inline wait/post_sem()
- tccpe.c: simplify import (assume STT_NOTYPE is function)
2024-11-30 20:05:02 +01:00
grischka
4b0402825e tccgen: C2x 'enum <tag> : <type> ...'
accept the new C2x typed enums (seems new android-NDK
headers are using that now)
2024-11-30 20:04:28 +01:00
Avi Halachmi (:avih)
249a0b6b60 win32: add minimal mingw header shellapi.h
This is a very stripped down version[1], with only CommandLineToArgvW
(typically used with GetCommandLineW() - which we already have),
ShellExecute and FindExecutable, which don't require any new structs,
and where used constants already exist at our winuser.h .

Pretty cheap, and hopefully covers some meaningful new use cases[2].

Requires linking with -lshell32 .

[1] original full version:
http://download.savannah.nongnu.org/releases/tinycc/winapi-full-for-0.9.27.zip

[2] Can now build "less" for windows - https://github.com/gwsw/less/ .
Add -luser32 to LIBS at Makefile.wng (mingw makefile), then (gnu make):
  make -f Makefile.wng CC=tcc less.exe
2024-11-30 16:29:15 +02:00
Avi Halachmi (:avih)
085e029f08 fix typos in comments 2024-11-22 15:33:12 +02:00
Avi Halachmi (:avih)
1cf33feb0f w32: improve quoting when spawning cross-compiler
The previous code had few issues:
- Didn't quote an empty string.
- Didn't quote if the string contains tabs.
- Didn't take into account existing backslashes at the string.

E.g. previously broken (in POSIX sh, like cygwin or busybox-w32):
- tcc -m 32 test.c -D 'CSTR="foo'$'\t''bar"' (wrongly rejected by tcc).
- tcc -m 32 test.c -D 'CSTR="foo\"bar"'  (incorrect CSTR at test.c).

Both issues are fixed with the new code, and presumably any others.

The empty/tabs issues could be fixed within the current code, but the
backslashes issue is not worth shoehorning it, so this is a rewrite.
2024-11-22 15:11:05 +02:00
Avi Halachmi (:avih)
a6ef31823b tcc -ar options: use strchr, strpbrk (trivial, no-op)
I happened to bump into thsese, but I didn't try to review the file,
and there may be other places which could be similarly improved.

This is originally my suboptimal code from commit 100f94be
(tiny_libmaker: more robust arguments interpretation), so, a bit late,
but let's improve it anyway.
2024-11-22 00:06:44 +02:00
checkroom
ef7e84454e Fixing preprocessor edge case preventing building with meson https://github.com/KaruroChori/tcc-vs 2024-11-20 01:39:04 +00:00
Avi Halachmi (:avih)
cff81434a5 win32: 32 bit: allow 64 bit time via __MINGW_USE_VC2005_COMPAT
Before VC2005, the time macros (time, time_t, localtime, etc) were
32 bit on 32 bit platforms, but they became 64 in VC2005.
This works even on XP 32 (_time64 etc do exist in XP32 - and in tcc).

However, tv_sec in struct timeval (which for msvc is in winsock2.h)
remains 32 bit to this day on 32 bit platforms, and dlls which were
not recompiled remain with time 32, possibly at the API boundary.

Due to these, and maybe more, mingw w64 decided to keep the time
macros 32 bit on 32 bit platforms, with __MINGW_USE_VC2005_COMPAT
override (which does nothing in mingw w64 except time -> time64).

It's not perfect, but it allows some existing code to easily switch
to 64 bit time without redefining time etc, e.g. used by libressl:
  https://github.com/libressl/portable/blob/master/README.mingw.md

Before, it was impossible to enable the 64 bit time macros in tcc 32.

This commit adds support for __MINGW_USE_VC2005_COMPAT in tcc as well,
which, like in mingw w64, affects only the 32->64 time macros, and is
a cheap way to get 64 bit time in existing code in some 32 bit apps.

The additional #ifndef _USE_32BIT_TIME_T is unrelated to the override,
and avoids a warning (and nothing else) when the code explicitly
defines it - which is allowed in MSVC, and also guarded in mingw w64.

Relevant current quote from the libressl link above:
------- >8 ---------

Why the -D__MINGW_USE_VC2005_COMPAT flag on 32-bit systems?

An ABI change introduced with Microsoft Visual C++ 2005 (also known as
Visual C++ 8.0) switched time_t from 32-bit to 64-bit. It is important
to build LibreSSL with 64-bit time_t whenever possible, because 32-bit
time_t is unable to represent times past 2038 (this is commonly known
as the Y2K38 problem).

If LibreSSL is built with 32-bit time_t, when verifying a certificate
whose expiry date is set past 19 January 2038, it will be unable to
tell if the certificate has expired or not, and thus take the safe
stance and reject it.

In order to avoid this, you need to build LibreSSL (and everything
that links with it) with the -D__MINGW_USE_VC2005_COMPAT flag. This
tells MinGW-w64 to use the new ABI.

64-bit systems always have a 64-bit time_t and are not affected by
this problem.
2024-11-18 15:45:30 +02:00
Avi Halachmi (:avih)
00b29f49a9 win32: mingw headers: mainly add LOAD_LIBRARY* values
The LOAD_LIBRARY_* constants allow safer and more controlled load.

The others in winnt.h and wincon.h are relatively minor.
2024-11-18 15:44:14 +02:00
grischka
dd2e5f8b06 tccelf: cleanup sort_sections() & etc. fixes
with -Wl,-oformat=binary, executable code should come first.
(for linux kernel image for example)

Also:
- simplify RELRO sections: create them as readonly, but add
  SHF_WRITE flag later when needed (i.e. relocations do exist)
- tcc.h etc: exclude eh_frames on non-elf platforms
- tccelf.c:tcc_load_object_file(): don't load debug sections when
  linking without -g (special dwarf case in relocate_section()
  wont work when dwlo/hi were not initialized).
- tcc.c: avoid loop if something fails (ret < 0) without message
  (while failing without message should not happen either)
- tccelf.c:tcc_load_alacarte: give message
- tccpp.c: treat '# 123xyz' in asm file as comment
- lib/Makefile: cleanup
- libtcc.c: tcc_add_library(): fallback to try filename as is
  (also remove tcc_add_library_err())
2024-11-17 21:39:38 +01:00