Commit graph

3550 commits

Author SHA1 Message Date
2596ba135d sync repo 2025-03-01 10:28:53 +01:00
5a54c79520 WIP replace ELF part with COFF 2025-02-21 15:10:26 +01:00
c5d1e40ebe Further strip down tcc 2025-02-14 17:47:45 +01:00
94b74d446f Remove _WIN32 bullshit (msys2 exist uU) 2025-02-11 20:40:33 +01:00
ef94f0cf60 Remove support for threaded use of libtcc.a, It's out of scope 2025-02-11 20:26:28 +01:00
9eb10536b0 Further split tcc into multiple file 2025-02-11 20:16:44 +01:00
ca1e24c395 Further refactor code 2025-02-11 13:07:04 +01:00
db4e043cd9 Move arguments parsing to option.c 2025-02-08 18:38:14 +01:00
6318a909ff Strip down further TCC (removing unused function, unused parameter...). Split source in multiple files (WIP) 2025-02-08 15:45:16 +01:00
f08ef63153 Refactor and remove code (tccdbg, tccmacho, ...) 2025-02-07 17:17:17 +01:00
05611e2658 Strip down TCC source code by removing useless targets 2025-02-06 11:42:10 +01: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
grischka
3eb6352c52 tcc -fgnu89-inline -fno-asynchronous-unwind-tables
-fgnu89-inline: 'extern inline' like 'static inline'
-fno-asynchronous-unwind-tables: don't emit eh_frames
2024-11-17 21:27:23 +01:00
grischka
f24727b6bb tcc -freverse-funcargs (reverse evaluation)
patch originally made to prove correctness (comparing stages)
with tinycc compiling gcc 2.95.3 which would assign registers
differently (but still correctly) when compiled with tcc without
this option).

Also: fixes get_temp_local_var() which on 32-bit systems
happened to return a temporary location that was still
in use because its offset was changed on the vstack
(incremented by four in gv() to load the second register
of a long long).

Also: optimize vrot-t/b (slightly) by using one memmove
instead of moving elements one by one in a loop.
2024-11-17 21:04:29 +01:00
grischka
c717bac6e3 tccgen: show useful line number with duplicate switch-case
- output correct line number with "error: duplicate case value"
- libtcc.c:error1(): support specific line numbers with "%i:"
       tcc_error("%i:message ...", line_num, ...);
Also:
- simplify signed/unsigned switch compare
- optimize implicit case ranges such as
      case 1: case 2: case 3: ...
- simplify llong constant propagation in gen_opic()
- rename Sym.ncl to Sym.cleanup_func
2024-11-17 20:56:12 +01:00
herman ten brugge
f0cd0fbe1b Print bound region when bound checking error occurs 2024-11-09 08:34:10 +01:00
herman ten brugge
233e22f23d Fix compile problem on windows 2024-11-09 08:24:58 +01:00