Commit Graph

3770 Commits

Author SHA1 Message Date
grischka
64552b3faa tccelf: rework linker_sym (_end/end) logic
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-arm64-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
build and test / test-i386-linux (push) Has been cancelled
- tcc_add_linker_symbols():
    don't add for dlls
- bind_exe_dynsym():
    update _end symbol when done
    allow data definition in the .so override a bss symbol
    in the exe. For example
       exe: int x;
       .so: int x = 123;
- set_elf_sym():
    cannot compare section numbers with symbols from dlls
  tcc_load_obj():
    return -1 from symbol conflict errors

tests/tests2/148_linker_symbols:
- use only one file
- make it print useful output rather than just "2 errors."
  In general, the tests2 setup with .expect files is meant
  to compare text output.  For example:
      printf("val = %d\n", val);

  .expect file can be generated or refreshed using
       make tests2.nnn+
   or in order to force generation with tcc itself
       make tests2.nnn+ GEN-CC='$(GEN-TCC)'

tccgen.c:
- __attribute__((section(...))) : set section type/flags
    according to first usage
- fix bcheck bounds for -fcommon
2026-08-05 17:41:23 +02:00
grischka
128e78930e tccelf: unify SHT_NOTE sections
- put all note sections under PT_NOTE header automatically
- also move tls sections under relro group
- also sort standard sections to come last
2026-08-05 17:34:43 +02:00
Mounir IDRASSI
2be0218be4 arm64: fix symbol addends on LLP64 hosts
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-arm64-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
build and test / test-i386-linux (push) Has been cancelled
The host-sized symbol addend parameter was introduced by commit b14ef0e2 and truncates 64-bit addends on LLP64 hosts.

Use addr_t and cover the negative-addend case.
2026-07-30 22:53:29 +09:00
Mounir IDRASSI
da58264ab1 tests2: keep linker boundary fixtures common
Compile _etext and _edata with -fcommon so RISC-V GCC does not
place them in .sbss. This preserves the intended common/BSS replacement
coverage.

Fixes the reported test-riscv64-linux failure:
https://github.com/TinyCC/tinycc/actions/runs/30347315549/job/90236450313
2026-07-30 17:06:14 +09:00
grischka
380597704e tccpe: tls on windows (i386 / x86-64 / arm64)
Also:
- arm64-gen.c: cleanup load/store
  fixes a problem with bound-check test1b with
  ./configure --cc=tcc

  Also, do not use 'long' or 'ul' suffix in tcc because
  for example on win32-arm64
      (uint64_t) ~0xffful == 0x00000000fffff000
  but on linux-arm64
      (uint64_t) ~0xffful == 0xfffffffffffff000

Also:
- tccpe.c: cleanup "quoted dll nanes in def" parse
- tests2: enable some threaded tests on windows
2026-07-28 23:46:30 +02:00
Mounir IDRASSI
43158eaa71 tccelf: consider DSO symbols for PROVIDE
An input shared object can be the only place where a conventional boundary name is referenced or defined. In that case the regular symbol table has no entry, but native ELF linkers still synthesize the executable definition so that it participates in dynamic symbol resolution.

Consult the input dynamic symbol table before treating a plain boundary name as unreferenced. Exercise the case with a DSO-defined end symbol whose address must be interposed by the executable's final boundary.
2026-07-28 18:06:06 +09:00
Mounir IDRASSI
384614a945 tccelf: provide complete ELF boundary symbols
The traditional boundary symbols were recorded against the canonical .text, .data and .bss sections before dynamic binding and final layout. Separately named allocatable sections and copy-relocated data could therefore fall outside the advertised ranges.

Track definitions synthesized by the linker and finalize them from the ordered loadable sections. Refresh both regular and dynamic symbol tables, and give the plain etext, edata and end names PROVIDE semantics on every ELF target while preserving user definitions.

Cover archive references, overrides, copy relocations and independently named text/data/bss sections.

Reported-by: Richard Wheeler <18647491+PythonWillRule@users.noreply.github.com>
2026-07-28 18:06:04 +09:00
Mounir IDRASSI
31020bf963 win32: handle quoted LIBRARY names in def files
Parse quoted LIBRARY names as a single token and add the default .dll
extension when needed. Allocate the stored name dynamically.

Add a Windows regression test using a quoted name with spaces.
2026-07-28 11:01:33 +09:00
Sigrid Solveig Haflínudóttir
85ba3ae8f1 {arm,i386,riscv64}-asm.c: fix generic operand modifier 'n' not negating 2026-07-24 19:27:21 +02:00
Avi Halachmi (:avih)
d9d02c5640 win32: simplify tcc 0.9.27 self-host (part 2 of 2)
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-arm64-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
build and test / test-i386-linux (push) Has been cancelled
tcc 0.9.27 does have a usable ldexp - but not ldexpl. however,
on windows these functions are identical, so map ldexpl to ldexp
and remove the MUSL-implementation which was copied from tcc_libm.h.
2026-07-14 16:46:05 +03:00
Avi Halachmi (:avih)
add111e6fe win32: fix typo in comments: 0.97 -> 0.9.27 2026-07-14 09:58:21 +03:00
Avi Halachmi (:avih)
ff85981b6c win32: allow self-host with tcc 0.9.27 - part 2 of 2
Commit cb39bc4 ("Calculate hex floating point with 128 bits instead of
64 bits.", 2025-05-27) started using ldexpl in tccpp.c, however, older
tcc like 0.9.27 (2017) don't have it on windows - ldexpl was added to
win32/include/math.h (in tcc_libm.h) in commit b11144d (2020).

So copy the current ldexpl definition from current tcc_libm.h into
tccpp.c - but under a different name.
2026-07-14 09:37:17 +03:00
Avi Halachmi (:avih)
a667b53fcb win32: allow self-host with tcc 0.9.27 - part 1 of 2
Commit 03d58b0 ("arm64-win32 support : runtime", 2026-04-04) added
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE to winnt.h, and it was later
used in tccpe.c, but older tcc like 0.9.27 don't have it.

So define IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE is missing.
2026-07-14 09:26:47 +03:00
Avi Halachmi (:avih)
4ed845d0db Makefile: restore doc targets
Commit 44977b0 ("tcctest: restore & combine", 2026-05-06) effectively
changed the run-if macro to "true" - i.e. don't run the command and
don't report that it's skippped. As a result, all the doc targets
were skippped quietly and unconditionally.

Presumably it was a temporary in-development change - the commit
message doesn't mention docs changes, and the doc targets still exist
at Makefile, but this change was not reverted before the commit.

Revert this specific change, which restores the automatic doc targets
(build them only if the command to generate them exists).
2026-07-14 09:19:24 +03:00
Mounir IDRASSI
97a6d8c75f tls: fix local-exec addresses and external symbols
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-arm64-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
build and test / test-i386-linux (push) Has been cancelled
Preserve symbolic offsets when materializing i386 TLS addresses, generate ARM64 TLS addresses from the thread pointer, and carry RISC-V addends on both halves of the TPREL relocation pair.

Keep undefined ELF TLS symbols typed as STT_TLS so objects produced by TCC can be linked by the system linker. Extend the TLS regression coverage for narrow signed and unsigned accesses, address-taking, runtime indexing, thread isolation, large RISC-V offsets, unsupported runtimes, and external TLS definitions.
2026-07-10 23:58:23 +09:00
herman ten brugge
04206d2903 Some more TLS changes
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-arm64-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
build and test / test-i386-linux (push) Has been cancelled
tccgen.c:
  remove VT_TLS in gen_cast
libtcc.c:
  remove warning for gcc 16

i386-gen.c:
  fix gen_modrm for VT_LDOUBLE
riscv64-gen.c:
  add fc to TLS address
x86_64-gen.c:
  add TLS prefix in gen_opf

tests2/144_tls.c/tests2/144_tls.expect:
  updated test case
tests/tests2/Makefile
  skip TLS test for BSD
2026-07-09 07:54:16 +02:00
grischka
b895aa7e68 review platform tls support
Some checks are pending
build and test / test-x86_64-linux (push) Waiting to run
build and test / test-x86_64-osx (push) Waiting to run
build and test / test-aarch64-osx (push) Waiting to run
build and test / test-x86_64-win32 (push) Waiting to run
build and test / test-i386-win32 (push) Waiting to run
build and test / test-arm64-win32 (push) Waiting to run
build and test / test-armv7-linux (push) Waiting to run
build and test / test-aarch64-linux (push) Waiting to run
build and test / test-riscv64-linux (push) Waiting to run
build and test / test-i386-linux (push) Waiting to run
x86_64-gen.c:
- remove stupid copy&paste code
- integrate tls into gen_modrm()
- cleanup calls to gen_modrm[64/32]
i386-gen.c:
- remove stupid copy&paste code
arm64-gen.c:
- remove stupid copy&paste code
arm-gen.c:
- tls not working
2026-07-08 14:01:56 +02:00
grischka
5673055a9c review generic tls support
*-link.c: remove stupid loops
tccelf.c: ...also
tccgen.c: create .tdata/.tbss sections only when needed
2026-07-08 14:01:52 +02:00
grischka
7f7845cd7e review some recent changes
tccrun.c:
- simplify usage of VirtualAlloc on win32/64
  from 6728a64f1b
tccpe.c:
- revert changes to pe_create_pdb()
  (doesn't work and/or too much complication)
  from 37b7247796
- reduce #ifdef amount
tccgen.c:
- fix fix for cast to void
  from fad812360b
tccelf.c:
- put create_riscv_attribute_section() into right place
  from c77339ab41
tcc.h/libtcc.c:
- simplify link-option parser for pe dll_characteristics
  from 1a54e47dda
i386-gen.c:
- fix tls
  from 8502540b4a
workflow/build.yml:
- add i386-linux
- disable riscv64-linux-native (seems not supported currently)
2026-07-08 14:01:40 +02:00
Mounir IDRASSI
a338258d30 Fix qualifiers on const array struct members
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-arm64-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
build and test / test-riscv64-linux-native (push) Has been cancelled
Use the existing array-aware qualifier helper when propagating qualifiers from a const or volatile aggregate to a selected field. This keeps address-of array members from spuriously warning while preserving warnings for discarded qualifiers and read-only writes.

Add regression coverage for pointer-to-array returns, array decay, and writes through const array members.
2026-06-13 22:26:02 +09:00
Meng Zhuo
fb4077f2da CI: fix win32 MSVC setup and arm64 test
Replace hardcoded VsDevCmd.bat Enterprise path with vswhere.exe +
vcvarsall.bat for dynamic VS discovery across editions and runner
image versions.  Architecture args: amd64, x86, amd64_arm64.

Split x86_64/i386 win32 jobs into separate build and test steps.

For arm64: keep build+test in one step (needs vcvarsall for lib.exe).
Prepend standalone LLVM bin to PATH so clang targets ARM64 natively
instead of VS's clang-cl inheriting the x64-hosted cross-compile env.
2026-06-12 10:26:51 +08:00
Meng Zhuo
724b24eeb4 riscv64: implement float/double constant loading 2026-06-12 10:26:51 +08:00
Meng Zhuo
d8aee9b26e riscv64: consolidate riscv intrinsics into lib-riscv.c
Replace the single-purpose lib/riscvflush.c with lib/lib-riscv.c
following the lib-arm64.c naming convention.
2026-06-12 10:26:50 +08:00
Meng Zhuo
b8f680a3b4 CI: use RISE runner to test riscv64 2026-06-02 16:47:08 +08:00
Meng Zhuo
1444843fd1 riscv64-link: fix C90 mixed declaration warning
Fix -Wdeclaration-after-statement: move tp_offset declaration
before the for-loop in R_RISCV_TPREL_HI20/LO12_I relocation
handler.
2026-05-31 22:36:41 +08:00
Meng Zhuo
6b53465347 riscv64: enable gen_clear_cache with I-cache flushing
Wire gen_clear_cache to __riscv64_clear_cache builtin, emitting
fence.i for instruction cache synchronization.
Add lib/riscvflush.c to provide __clear_cache for JIT bootstrap.
Extend tccrun.c I-cache flush guard to include RISC-V targets.
Add gen_clear_cache declaration to tcc.h riscv64 section.
2026-05-31 22:36:41 +08:00
Mounir IDRASSI
3b1fe97a59 win32: define arm64 Interlocked helpers in winnt.h
Move the TinyCC/Windows ARM64 Interlocked workaround from the semlock call site into the bundled WinAPI header. This keeps tcc.h using the normal Interlocked API while avoiding unresolved Interlocked imports when tcc -run self-compiles on Windows ARM64.

Update lib/bt-exe.c as well so its ARM64 runtime object does not define a second InterlockedExchange fallback now provided by winnt.h.
2026-05-23 22:39:17 +09:00
Mounir IDRASSI
904e95cbdf win32: avoid Interlocked imports for tcc arm64
Windows/arm64 exposes Interlocked* operations as compiler intrinsics rather than kernel32 exports. When tcc -run self-compiles tcc.c, calls to InterlockedCompareExchange or InterlockedExchange can therefore remain as unresolved imports in the in-memory linker.

Route the TinyCC arm64 path through the existing __atomic helpers while keeping Interlocked* for other Windows compilers. The compile-lock initialization state machine remains unchanged.
2026-05-21 08:41:08 +09:00
Mounir IDRASSI
601a088214 win32: improve tccrun protection diagnostics
Report VirtualProtect failures with a Windows-specific -run diagnostic.
On Win64, also treat RtlAddFunctionTable failure as an error so unwind
registration problems are visible.
2026-05-15 21:41:46 +09:00
Mounir IDRASSI
1a54e47dda pe: add dll characteristic linker options
Add PE linker switches to set and clear DYNAMIC_BASE, NX_COMPAT,
HIGH_ENTROPY_VA and TERMINAL_SERVER_AWARE without changing x86/x64
defaults. HIGH_ENTROPY_VA is limited to 64-bit PE targets and implies
DYNAMIC_BASE; clearing DYNAMIC_BASE also clears HIGH_ENTROPY_VA.

When DYNAMIC_BASE is requested for an executable, emit base relocations
and clear RELOCS_STRIPPED so Windows ASLR can relocate the image. Keep
the existing ARM64 default hardening flags.
2026-05-15 21:40:44 +09:00
Mounir IDRASSI
37b7247796 win32: run cv2pdb without shell quoting issues
Invoke cv2pdb with _spawnvp on Windows so output paths are passed
as argv entries instead of through cmd.exe.

For non-Windows PE cross builds, keep the system() fallback but
shell-quote the executable name and size the PDB path dynamically.
2026-05-15 21:40:43 +09:00
Mounir IDRASSI
6a7c3df4d5 win32: make compile lock initialization thread-safe
Use interlocked state transitions around the lazily initialized Windows
CRITICAL_SECTION so concurrent first-time callers cannot race into a
double initialization.
2026-05-15 21:40:43 +09:00
Meng Zhuo
757507eb02 fix: skip TLS test on osx
tests/tests2/Makefile: skip 144_tls on OSX
2026-05-13 15:31:17 +08:00
Mounir IDRASSI
6728a64f1b win32: use VirtualAlloc for run memory 2026-05-13 14:25:22 +09:00
Meng Zhuo
256b4ef63c fix: TLS test failures on osx/win32/armv7
- tccmacho.c: add STT_TLS to convert_symbol() for Mach-O output
- tests/tests2/Makefile: skip 144_tls on Win32 (no pthread.h) and
  remove arm from TLS arch list (QEMU user-mode cannot emulate
  MRC p15 TLS register access)
2026-05-13 12:28:58 +08:00
Meng Zhuo
8502540b4a feat(i386): add TLS Local Exec code generation and linker relocations 2026-05-13 10:30:10 +08:00
Meng Zhuo
841ce0da03 feat(arm): add TLS Local Exec code generation and linker relocations 2026-05-13 10:29:44 +08:00
Meng Zhuo
8abaf10ab5 feat(arm64): add TLS Local Exec code generation and linker relocations 2026-05-13 09:37:59 +08:00
Meng Zhuo
11f5c6e1f9 feat(riscv64): add TLS Local Exec code generation and linker relocations 2026-05-13 09:37:42 +08:00
Meng Zhuo
ea26b85ac0 feat(x86_64): add TLS Local Exec support with tests 2026-05-13 09:07:31 +08:00
Meng Zhuo
8443e25bf5 feat: implement Thread Local Storage frontend support 2026-05-12 20:59:01 +08:00
inostensibl
fad812360b tccgen: fix void expression side effect regression
From 7e01b20362

reduced from an app that builds/runs with gcc or clang.
2026-05-09 22:11:58 -04:00
Meng Zhuo
2888e49f39 riscv64-gen: implement gen_clear_cache
Emit fence + fence.i (Zifencei extension) for I/D cache
synchronization, needed for tcc -run mode on RISC-V.
Zifencei is required by the RISC-V Linux ABI and present
on all Linux-capable cores, though technically not part
of the RV64G base ISA.
2026-05-07 16:07:53 +08:00
Meng Zhuo
c77339ab41 riscv64: emit .riscv.attributes section in ELF output 2026-05-07 11:44:46 +08:00
Meng Zhuo
7f764f340f riscv64-asm: complete AMO aq/rl suffixes for all ops
Add 48 tokens + handlers + dispatch cases for all AMO aq/rl
variants (amoswap, amoand, amoor, amoxor, amomax, amomaxu,
amomin, amominu; each x {.w,.d} x {.aq,.rl,.aqrl}).

GNU-compatible 2-dot naming via DEF_ASM_WITH_SUFFIXES.
Correct funct5 values: amoswap=0x01, amoand=0x0C, amoor=0x08,
amoxor=0x04, amomax=0x14, amomaxu=0x1C, amomin=0x10, amominu=0x18.

Extend 141_riscv_asm amo_main() with 10 representative aq/rl
variants covering all ops x ordering suffix combinations.

Verified against riscv64-linux-gnu-as 2.44.
2026-05-07 11:44:46 +08:00
Meng Zhuo
a672babc6f tests: fix 141_riscv_asm AMO crash, restore full test coverage
Replace raw-register AMO asm with proper inline-asm constraints
for all three operands (rd, rs2, rs1).  Use long long for .d-word
AMO variants with pre-set values (xd=0xCAFEBABECAFEBABE,
val=0xDEADBEEFDEADBEEF) to avoid uninitialized-data issues.

Restore csr_pseudo_main printf output and re-enable amo_main()
in the test (was commented out as 'crash on qemu').

Use GEN = $(GEN-TCC) since GCC and TCC produce different fcsr
initial values on riscv64.
2026-05-07 11:44:18 +08:00
grischka
44977b0de8 tcctest: restore & combine
Revert 199369bb17
- generating test.ref with tcc makes little sense.
- combine riscv_asm & conversion tests into only two files.
  (too many files are bad ...)
2026-05-06 18:52:01 +02:00
grischka
6daf1617ef configure & tcc.h: include & lib searchpath cleanup
configure:
- remove CONFIG_USE_LIBGCC, --with-libgcc
- remove CONFIG_LDDIR
- remove CONFIG_USR_INCLUDE
- add -q switch for quiet (=no) output

tcc.h:
- remove /usr/local/include&lib search paths

arm-gen.c:
- remove default_elfinterp() function

tccelf.c:
- setup the ELF-interp in tccelf_new()
2026-05-06 13:46:42 +02:00
grischka
923fba83f1 general: long double issues
tccgen.c:
- init_putv(): improve long double cross constants
  now in separate function including some basic conversions

arm-gen.c:
- switch to using TCC_USING_DOUBLE_FOR_LDOUBLE
  now it actually never will see VT_LDOUBLE

lib/lib-arm64.c:
- simplify by using unions

tccpp.c:
- reduce amounts of #ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
2026-05-06 13:46:38 +02:00
grischka
7e01b20362 tccgen: void values, etc...
originally, this was meant to avoid 'void' values leak
to the gen-xxx.c generators which would otherwise try
to load void from/to registers.  Also catch invalid types
such as 'unsigned bool' eariler.

tccgen.c:
- expr_cond()/gexpr(): ignore VT_VOID
- gen_cast(): set float reg with static initializers to make
  backends happier with invalid input
- init_putv():
  improve static compound literal initializers
Also:
- ignore "missing prototype" and "might return no value"
  for old style K&R functions

tcc.c, libtcc.c:
- tcc_set_output_type(): return errors from loading crt1.o

tcc.h/tccpp.c:
- fix horrible longstanding mistake with sizeof SValue.tab.
  Must have place for sizeof (long double) rather than the
  target's LDOUBLE_SIZE.

tccdefs.h:
- add a fake __[u]int128_t for all platforms

arm-gen.c:
- gen_cvt_ftof() must load to reg always

arm64-gen.c:
- simplify arm64_pcs() a bit

i386/x86_64-gen.c:
- do not assume char is signed (for cross compilation arm->x86)

tccelf/macho linker message:
- instead of
      "undefined symbol 'X'"
  now say
      "unresolved reference to 'X'"
  which makes it more clear that it is a _linker_error_.

tccasm.c:
- .align/p2align: use 'nop's in exec sections
2026-05-06 13:46:27 +02:00