Compare commits

...

1420 Commits

Author SHA1 Message Date
Mounir IDRASSI
a338258d30 Fix qualifiers on const array struct members
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-riscv64-linux-native (push) Waiting to run
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
Meng Zhuo
016087c954 riscv64-asm: implement AMO, fcvt rounding, and fcvt encoding fixes
AMO (A-extension):
  18 base instructions: amoadd/swap/and/or/xor/max/maxu/min/minu .w/.d
  6 aq/rl suffixes: amoadd.w.aq/.rl/.aqrl, amoadd.d.aq/.rl/.aqrl
  Correct funct5 (GNU as verified):
    amoadd=0x00 amoswap=0x01 amoxor=0x04
    amoand=0x0C  amoor=0x08  amomax=0x14
    amomaxu=0x1C amomin=0x10 amominu=0x18
  R-type opcode 0x2F, aq/rl in bits [26:25]

FCVT rounding modes (GNU operand syntax):
  fcvt.w.s rd, rs1 [, rtz/rne/rup] -- optional 3rd operand
  asm_fcvt_opcode() handler with asm_fcvt_rm() helper
  Keywords rne=0, rtz=1, rdn=2, rup=3, rmm=4

FCVT encoding fixes:
  fcvt.s.d: funct7 0x40->0x20
  fcvt.d.s: funct7 0x42->0x21

Tests: 144 CSR, 145 F/D cmp+cvt, 146 AMO, 147 fcvt round.
All verified against riscv64-linux-gnu-as 2.44 on Spacemit X100.
2026-05-06 19:18:17 +08:00
Meng Zhuo
f8011ea9b7 riscv64-asm: implement CSR pseudo-instructions
Add handlers for csrr, csrw, csrwi, csrsi, csrci as pseudo-instructions:
  csrr rd, csr     -> csrrs rd, csr, x0
  csrw csr, rs     -> csrrw x0, csr, rs
  csrwi csr, uimm  -> csrrwi x0, csr, uimm
  csrsi csr, uimm  -> csrrsi x0, csr, uimm
  csrci csr, uimm  -> csrrci x0, csr, uimm

Tokens were already defined in riscv64-tok.h.  Tested on
Spacemit X100 using fcsr (0x003) which is accessible in user mode.
cycle/instret CSRs are privileged and not accessible from Linux
user mode on this hardware.
2026-05-06 18:33:15 +08:00
Meng Zhuo
199369bb17 tests: generate test.ref with TCC instead of host CC
The promote char/short funcret test in tcctest.c invokes
undefined behavior (narrow return without extension). GCC
produces one set of values, TCC with PROMOTE_RET produces
correct ABI-compliant values. Generate the reference with
TCC so self-tests are consistent.
2026-05-06 14:43:30 +08:00
Meng Zhuo
3935c3bb55 riscv64-asm: add basic F/D extension instructions (fadd/fsub/fmul/fdiv)
Adds the 8 most essential floating-point arithmetic instructions:
fadd.s/d, fsub.s/d, fmul.s/d, fdiv.s/d, for both single and double
precision.  These complement the existing fsgnj/fmax/fmin/fsqrt set.
Comparison (feq/flt/fle) and conversion (fcvt) instructions remain
as future work.
2026-05-06 12:30:34 +08:00
Meng Zhuo
273978b927 riscv64-asm: implement pseudo-instructions sext.w, fmv, fneg
sext.w: addiw rd, rs, 0 (sign-extend 32-bit word)
fmv.s/fmv.d: fsgnj.s/d rd, rs, rs (float register move)
fneg.s/fneg.d: fsgnjn.s/d rd, rs, rs (float register negate)

These were defined in riscv64-tok.h but had no handler code.
CSR pseudo-instructions (csrr, csrw, csrci, csrsi, csrwi) skipped
for now — they require CSR operand parsing in binary mode.
2026-05-06 12:30:34 +08:00
Meng Zhuo
2a33daedca tccasm: extend .reloc directive for RISCV64 relocation types
Previously, .reloc unconditionally errored on RISCV64 — only ARM64
had permission to use R_AARCH64_CALL26.  Now supports common RISCV64
relocations: R_RISCV_CALL, CALL_PLT, BRANCH, JAL, PCREL_HI20,
PCREL_LO12_I/S, 32_PCREL, 32, 64.

Restructured to use string-to-reloc-type mapping, shared across all
targets.
2026-05-06 12:30:34 +08:00
Meng Zhuo
3257afa160 riscv64-link: add R_RISCV_RELATIVE case in relocate()
arm64-link.c has R_AARCH64_RELATIVE handling.  Without this case,
incoming R_RISCV_RELATIVE relocations from object files fall through
to the 'FIXME: handle reloc type' default path.

P1.7 (NEED_BUILD_GOT guards) is not needed: NEED_RELOC_TYPE and
NEED_BUILD_GOT are always defined in the build context (tcc.h:1608).
2026-05-06 12:30:34 +08:00
Meng Zhuo
419b527657 riscv64-asm: implement far branches via expansion
For branch targets that are external/static symbols (where the
offset cannot be determined at assembly time), expand to:
  b<inverse> .+8       # skip if condition false
  auipc t0, %pcrel_hi  # load target upper bits
  jalr x0, %pcrel_lo(t0)  # jump

This handles the ±4 KiB B-type limitation without requiring
linker relaxation (R_RISCV_RELAX).  Local immediates within
range still use the compact B-type encoding.
2026-05-06 12:30:34 +08:00
Meng Zhuo
5c2240a896 riscv64-asm: remove long long 'not implemented' errors in inline asm
On RV64, long long (64-bit) fits in a single general-purpose register.
The existing load/store calls already handle 64-bit values correctly.
The 'not implemented' errors were vestigial from 32-bit architectures
where long long requires a register pair.

Removing these errors allows inline asm to accept 64-bit integer operands
on RV64, which is important since this is the native register width.
2026-05-06 12:30:34 +08:00
Meng Zhuo
366569eb7a riscv64-asm: fix neg/negw pseudo-instruction encoding
neg/negw were incorrectly using asm_emit_i (I-type/xori) with an
immediate of 1, producing 'xori rd, rs, 1' instead of the intended
'sub rd, x0, rs'.  Fixed to use asm_emit_r (R-type) with proper
SUB/SUBW opcode (0x33/0x3B, func3=0, func7=0x20).

The old code generated wrong machine code for these pseudo-instructions.
negw now also correctly uses OP-32 (0x3B) for 32-bit subtraction.
2026-05-06 12:30:34 +08:00
Meng Zhuo
ff917c09aa riscv64: implement gen_cvt_sxtw with addiw instruction
Replaces the empty stub that relied on 'RV64 registers are always
sign-extended' assumption.  Now emits addiw rd, rs, 0 for proper
32-to-64 bit sign extension, matching arm64's sxtw behavior.

Verified on Spacemit X100: tests2 pass (125_atomic_misc has a
pre-existing intermittent segfault, not caused by this change).
2026-05-06 12:30:34 +08:00
Meng Zhuo
9f0915a506 riscv64: implement gen_cvt_csti for narrow int promotion
Add sign/zero extension for char/short to int casts using
slli+srai (signed) or slli+srli/andi (unsigned) sequences.
Also adds RISCV64 to the gen_cvt_csti fast-path conditional
in tccgen.c:3466, matching arm64/i386/x86_64.

Verified on Spacemit X100: all tests2 pass, no regressions.
2026-05-06 12:30:34 +08:00
Meng Zhuo
69c8e92566 riscv64: add PROMOTE_RET for narrow return ABI compliance
Other mature backends (i386, x86_64, arm64) all define PROMOTE_RET,
which forces explicit sign/zero extension of narrow return types at the
caller side.  Without it, riscv64 relied on the assumption that RV64
registers are always sign-extended, which may not hold when interfacing
with non-TCC compilers.

Verified on Spacemit X100 (riscv64): self-compilation is self-consistent
across 3 layers.  test3 reference mismatch is expected — the test
intentionally invokes UB via type-punned function pointers (csf macro).
2026-05-06 12:30:34 +08:00
grischka
30afb50e64 arm64-win32 review: fix problems and pass tests
tccpe.c:
- fix arm64 unwind codes (to make native set/longjmp() work)
  sizeof(RUNTIME_FUNCTION) is 8 on arm64 in the first place
  no need to note stack slots if we don't save any registers anyway

arm64-gen.c:
- fix long double reg-move
- fix arm64_hfa() for structs with float arrays
- gfunc_prolog(): setup stackframe eariler (simplifies unwind codes)
- new function gv_addr(RC);

win32/include/setjmp.h:
- provide correct definition for setjmo() (frameoffset = 224)

tccasm.c:
- support ".quad" with symbol & relocation
- support ".size"
- fix ". - symbol" arithmetic

win32/lib/crt1.c and win32/include/stdlib.h:
- do not write to __argc/__argv which reside in msvcrt.dll
  (msvcrt.dll on arm64 does not like that, crashes on unload)

tcc.c,libtcc.c:
- new functions tcc_fopen/fclose to avoid different stdio unstances
  in tcc.exe & libtcc.dll

tests & github workflow:
- add test-win32.bat to run tests with a tcc compiled by build-tcc.bat
- add msvcrt_start.c for gcc/clang to use the same runtime as tcc

  the problem is that newer gcc as well as clang and cl are
  linking to newer runtimes (such as UCRT) that have partially
  different printf format behavior which makes tcctest fail.

  the solution here is to force these compilers to link with
  msvcrt.dll just like tcc.

  Also, there is no gcc on arm64-win32 currently at all.

  Anyway, this approach to running the github CI tests
  does not require msys2.  But It does rely on gnumake
  as well as on some 'sh' shell though which seems to be
  installed somewhere (maybe it is the one from git).
2026-05-04 12:51:10 +02:00
Benjamin Oldenburg
576cd2a923 arm64-win32 support: symbolic op-code constants
from: https://github.com/bold84/tinycc
Author: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Committer: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Branch: win_arm64_clean
Commit: 8b5ab1bb01

Also here: https://repo.or.cz/tinycc.git/shortlog/refs/mob/mob_bold84/win_arm64_clean

This and the previous commits on mob is selected parts
of that original branch. So it is not everything.

It is not, for example:
- unrelated whitespace changes in many files
- a "pin msvcrt.dll" feature in tccpe.c (why that)
- a native getenv() replacement in tcc.c (why that)
- larger changes to the win32/lib runtime and tccrun.c (not needed)
- a very gcc specific detail for struct alignent in tccgen.c
- a custom set/longjmp implementation/replacement (not needed)
- lots of rather basic test files in the win32 folder
- a 'tests/asm' folder with some files (one file renamed to 140_test...c)
- a .docs folder with one file
2026-05-04 12:50:42 +02:00
Benjamin Oldenburg
44e6853cb1 arm64-win32 support: target PE & varargs 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
5c728d6506 arm64-win32 support: improve HFA & struct 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
90d17c9748 arm64-win32 support: use arm64_sub_sp() 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
99713bcbfa arm64-win32 support: use arm64_sym() 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
303badef22 arm64-win32 support: fix scaled mask 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
41bcb4a78f arm64-win32 support : linker & -run 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
f459aff5f6 arm64-win32 support : arm64 assembler 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
ea823189d6 arm64-win32 support : tests 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
03d58b0746 arm64-win32 support : runtime 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
ff5d3b4874 arm64-win32 support : configure & Makefiles
from: https://github.com/bold84/tinycc
Author: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Committer: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Branch: win_arm64_clean
Commit: 8b5ab1bb01

Also here: https://repo.or.cz/tinycc.git/shortlog/refs/mob/mob_bold84/win_arm64_clean

This and the followup commits on mob is selected parts
of that original branch. So it is not everything.

It is not, for example:
- unrelated whitespace changes in many files
- a "pin msvcrt.dll" feature in tccpe.c (why that)
- a native getenv() replacement in tcc.c (why that)
- larger changes to the win32/lib runtime and tccrun.c (not needed)
- a very gcc specific detail for struct alignent in tccgen.c
- a custom set/longjmp implementation/replacement (not needed)
- lots of rather basic test files in the win32 folder
- a 'tests/asm' folder with some files (one file renamed to 140_test...c)
- a .docs folder with one file
2026-05-04 12:49:25 +02:00
grischka
d9a6d9aec0 reverts (11/2025 - 04/2026)
Revert "Add support to debug libtcc code"
- not fully developed experimental feature
This reverts commit 1fe3e3bff5.
This reverts commit 4768b11737.

Revert "tests: add test for x86_64 xor REX prefix bug in load()"
- AI generated nonsense test
This reverts commit d5ecb52a71.

Revert "tccpp.c: Improve integer constant overflow warning"
- Too long and confusing messages and comments for feature
  with questionable benefit.
This reverts commit 085bdf8997.

riscv64-link:
- cleanup "pair pcrel lo relocations by hi address"
  From fada98b1ce

tccgen.c:
- Simplify "Cast signed pointer offset to ptrdiff_t before performing arithmetic"
  From 5ad52cc1ed

libtcc.c:
- Revert "tcc options: document behavior and clashes (no-op)"
  a bit more information than one would like to have I think.
  (why try to understand that comments plus the extra
  script if one can as well just read the code itself ;)
  From 234e2dd2bf

tccdefs.h:
- Revert "Move lib/va_list.c into include/tccdefs.h"
  Lets not fill tccdefs.h with too much inline code
  Also, -nostdlib -run is no longer supported
  From fa6a6bfbbd

arm64-gen.c:
- cleanup "Implement TOK_NEG for floats natively"
  Also, make it "no lvalue" in tccgen.c/x86-64-gen.c
  From c39eaf10cf

lib/lib-arm64.c:
- cleanup "Remove libc dependency from lib-arm64"
  using unions is much faster than some made up memcpy()
  From 8c61b91de8
2026-05-03 11:09:45 +02:00
H-language
a66ac623b2 win32: add missing AttachConsole export to kernel32.def
AttachConsole has been exported from kernel32.dll since Windows XP
but was absent from TCC's import definitions, causing
"undefined symbol 'AttachConsole'" at link time for code that uses it.
2026-05-01 09:58:03 +12:00
Cyan Ogilvie
4768b11737 tccrun: fix OOB read in elf_output_obj after tcc_relocate with -g
When -g is set, cleanup_sections() preserves all sections after relocate,
including the SHT_RELX relocation sections. cleanup_symbols() drops all
STB_LOCAL symbols from the symbol table, which renumbers the remaining
globals. The kept relas still hold r_info indices from the pre-cleanup
numbering, but nothing rewrites them. A later elf_output_obj() call ends
up in sort_syms() / update_relocs(), which allocates old_to_new_syms[]
sized for the post-cleanup (smaller) symtab and then indexes it with the
stale (larger) sym_index values from those rela entries, reading past
the end of the array.

Fix: when do_debug is set, build an old->new index map while re-adding
globals, then walk every retained SHT_RELX section linked to the symtab
and rewrite its r_info entries. Locals (and the undef sym at 0) map to
0 by tcc_mallocz, so relocations against dropped locals now refer to
SHN_UNDEF - the best we can do without preserving the locals themselves.

Reproducible with libtcc_debug.c if the program contains enough static
helpers / string literals for the symtab to actually shrink; observed
under valgrind in jitc (https://github.com/cyanogilvie/jitc) when
compiling re2c-generated state machines with -g.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 15:37:49 -03:00
Petr Skocik
b39da9f6fa Fix false warnings with readonly atomics
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-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
Code like:

    #include <stdatomic.h>
    _Atomic const int ai; int aiGet(void){ return atomic_load(&ai); }

has been raising "warning: assignment of read-only location".

This is due to `__typeof__ (*ptr) tmp;` not rvalue-converting the target
type.
2026-04-28 08:40:53 +02:00
Petr Skocik
5675177b7f allow $ at nonstarting positions in asm symbols under -fdollars-in-identifiers
e.g., asm("foo$bar: ret;");
2026-04-28 08:36:09 +02:00
Reini Urban
543d84c668 .gitignore: Add tests/libtcc_test_xor_rex 2026-04-26 14:56:39 +02:00
Detlef Riekenberg
9b8765d8ba Revert commits from OpenCode-AI and a partial regression fix
This reverts the crap from an unknown committer "OpenCode[AI]", that added an extension to TinyCC,
which is incompatible to the C standard and not available in any other mainstream C compiler:

* Revert "No More ; needed"
This reverts commit 6547ea47d6

* Revert "Add AGENTS.md"
This reverts commit 898f496dc9.

This also reverts a regression fix by "Zuhaitz.dev", which fixed a small part of the OpenCode-AI chaos:
* Revert "tccgen: Fix optional semicolon regression"
This reverts commit 169628a6ab

--
Regards ... Detlef
2026-04-20 01:42:41 +02:00
Anon
7b92d2dcd2 Fix warning 2026-04-16 13:16:17 -07:00
Zuhaitz-dev
169628a6ab tccgen: Fix optional semicolon regression 2026-04-16 17:24:21 +01:00
Cyan Ogilvie
8ecfd0a722 Relicensing TinyCC 2026-04-16 10:45:24 -03:00
Cyan Ogilvie
690fb14015 x86_64-gen: fix missing REX prefix for xor zero into r8-r15
load() used a raw o() call to emit xor-zero which lost bit 3 of the
register number via REG_VALUE():

    o(0xc031 + REG_VALUE(r) * 0x900);

For r >= 8, this emitted the wrong instruction (e.g. xor %ebx,%ebx
for TREG_R11 instead of xor %r11d,%r11d), clobbering the wrong
register.

Use orex() to emit the REX prefix, consistent with all adjacent
branches in load().
2026-04-16 10:42:16 -03:00
Cyan Ogilvie
d5ecb52a71 tests: add test for x86_64 xor REX prefix bug in load()
When load() in x86_64-gen.c generates a zero constant for a 64-bit
register, it uses:

    o(0xc031 + REG_VALUE(r) * 0x900);

REG_VALUE(r) masks to (r & 7), losing bit 3, and no orex() call
emits the REX prefix needed for registers r8-r15.  For TREG_R11
(used by gcall_or_jmp for indirect calls), this emits
"xor %ebx,%ebx" (31 db) instead of "xor %r11d,%r11d" (45 31 db),
clobbering the wrong register.

The test compiles an indirect call through a null function pointer
((void(*)(void))0)() via libtcc, then inspects the generated machine
code for the incorrect encoding.
2026-04-16 10:42:16 -03:00
OpenCode[AI]
6547ea47d6 No more ; needed 2026-04-15 20:56:01 -07:00
OpenCode[AI]
898f496dc9 Add AGENTS.md 2026-04-15 20:28:42 -07:00
Stefan
98765e5ebc libtcc.c: Change parameter name of tcc_set_realloc
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-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
In libtcc.h there is void tcc_set_realloc(TCCReallocFunc *my_realloc).
Name the parameter in the function definition in libtcc.c accordingly.
2026-03-28 15:44:20 +01:00
Stefan
085bdf8997 tccpp.c: Improve integer constant overflow warning
Improve the warning about an integer constant overflow with the used
overflown value and the potentially aimed value.

This helps spotting the 0 to much in 0x80000000000000000.
2026-03-28 15:31:21 +01:00
diannaaa
fada98b1ce fix(riscv64-link): pair pcrel lo relocations by hi address
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-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
Track R_RISCV_PCREL_HI20/GOT_HI20 relocations by address and resolve LO12 relocations against the referenced HI, instead of only using the last seen HI.

Also reset/free the per-link relocation map in TCCState.
2026-02-23 22:26:06 +08:00
Aleksi Hannula
4597a9621e
arm64-gen: consider VT_LONG|VT_DOUBLE for TOK_NEG 2026-02-07 12:08:17 +02:00
Aleksi Hannula
5ad52cc1ed
Cast signed pointer offset to ptrdiff_t before performing arithmetic
Affects 32-bit platforms.
2026-02-05 11:49:59 +02:00
Aleksi Hannula
c937095552
Fix numbered register substitution on x86_64 2026-02-05 11:49:59 +02:00
Aleksi Hannula
8c61b91de8
Remove libc dependency from lib-arm64
Moving the u128_t struct may invoke calls to memcpy/memmove when
compiled with tinycc, which would depend on libc.

For example, some configure scripts get confused if they fail to build
test programs involving long doubles. I'm not sure if this is the root
cause, though.
2026-02-05 11:49:59 +02:00
Aleksi Hannula
aeae4b4cee
arm64: Fix long double comparison operators 2026-02-05 11:49:59 +02:00
Aleksi Hannula
c39eaf10cf
arm64: Implement TOK_NEG for floats natively 2026-02-05 11:49:59 +02:00
Aleksi Hannula
1cb0a3d52a
Allow building an empty libtcc1 on AArch64 2026-02-05 11:49:59 +02:00
herman ten brugge
abb2f394dd i386: bound checking needs pic in shared library 2026-02-05 09:31:59 +01:00
Avi Halachmi (:avih)
4fccaf6124 Revert "build c2str.exe with the host compiler; default it to gcc if cross prefix given"
This reverts commit 41fa74fc84.

Reasons for the revert:

1. The commit message is lacking and the use case is not obvious.
   At the very least a concrete example of use case should be given
   (likely cross-building tcc on platform X for running on platform Y),
   followed by an issue description in such use case, and fix descr.
   (e.g. "On platform X, when running ./configure ... && make ..., the
   build fails because whatever. this commit does Y and fixes it").

2. assign_opt is used incorrectly: 1st arg must be --OPTNAME=VALUE
   but it's not. The only thing it does is unconditional host_cc=gcc
   if --cross-prefix=... is used - no need for "assign_opt" at all.

3. cross-building tcc currently has several triggers: build/target
   cpu differ, or build/target OS differ, or --cross-prefix=... is
   used, but that commit only covers the --cross-prefix=... case.
   It's possible that it's intentional to only cover one case, but
   if so then it's not explained why.

4. The var name host_cc is likely incorrect and probably meant to be
   "native_cc", judging by the assigned value "gcc".

5. "gcc" is used unconditionally in such case, without any way to use
   a different native compiler (assuming that's indeed the goal).
   A better approach is likely supporting --native-cc=... option which
   defaults to $cc (which already defaults to "gcc") _before_
   $cross_prefix is added in-front.
2026-01-18 09:04:54 +02:00
Urja Rannikko
41fa74fc84 build c2str.exe with the host compiler; default it to gcc if cross prefix given 2026-01-17 20:47:00 +02:00
noneofyourbusiness
b39cbc70c4
riscv64-asm.c: parse_operand: document some ABI details 2026-01-17 11:35:01 +01:00
grischka
5ec0e6f84b some reverts & fixes
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-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
workflow:
- revert 'pinact for security' for readability
  from 831c3fa184
tccpp.c:
- remove code that allows tcc to parse numbers incorrectly (*)
  from 829c848520
tccgen.c:
- Revert "Relaxed the 'incompatible pointer type' warning a bit" (*)
  from d9ec17d334.
tccrun.c:
- remove support for -nostdlib -run
  for simplicity, we require "main" with tcc -run always
tccpp.c:
- Revert "Free all preprocessor memmory in case of error."
  from c96f0cad61
  Remove TinyAlloc->limit instead.  Thus it can do also bigger
  allocs.  Big TokenStrings (like 200kb+ when compiling tcc)
  may come from inline functions or from large initializers.
Makefile/configure:
- use --config-pie for configuring tcc output only
- use -fPIC with clang-x86_64 to avoid 32-bit relocs
libtcc.c:
- fix "tcc file.c -run" i.e. -run as last argument
i386-gen.c:
- PIC refactor

(*) sorry, but code in tcc should have a minimum of generic relevance
2026-01-10 13:46:23 +01:00
herman ten brugge
518279dc3e Another update for macos
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-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
Sorry about last commit.
I had no access to a macos machine for a for weeks.
2026-01-09 08:25:28 +01:00
herman ten brugge
1401967ce2 Fix macos
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-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
I fixed macos problem with test case 60.
Also tccrun mmap and debug code did not work.
2026-01-06 16:01:28 +01:00
herman ten brugge
1fe3e3bff5 Add support to debug libtcc code
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-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
I tried several gdb/lldb options to debug libtcc generated code.
But gdb/lldb complained that they could not load symbols or
that module does not exist. There was also another problem. The
code itself was in memory (string) and gdb/lldb do not have
functions to acces it.

So I came up with a new api for debugging libtcc code.
First you enable debugging with: tcc_set_options(s, "-g")
Then compile the code with: tcc_compile_string_file(s, program, "<file>.c")
Then call tcc_relocate().
And finaly write the object file to disk: elf_output_obj(s, "<file>.o")
Now you can start the debugger and put an breakpoint after the
elf_output_obj() code. Then use gdb command add-symbol-file <file>.o
and from there on you can set breakpoints in the libtcc generated code.
You can also step/print variables/...
I could not find a simular function in lldb yet.

When debugging is done you remove the tcc_set_options(s, "-g").
All other code can remain because tcc_compile_string_file and
elf_output_obj do not output any file any more is debug is not set.

See also tests/libtcc_debug.c
2026-01-06 12:11:51 +01:00
herman ten brugge
8a8388c6ff Solve some bug reports
The savannah web site had some new bug report last december.
A lot of them are assemmbly bugs.
See testcase 60 for an overview.
2026-01-06 07:49:02 +01:00
herman ten brugge
9a7edb20d3 Add pic/pie support to i386
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-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
Some targets (netbsd) require that code is
compiled with -fPIE.

i386-gen.c:
Lot of changes to load/store code

i386-link.c:
Set PCRELATIVE_DLLPLT depending on configuration

tccelf.c:
Change some relocations and add get_pc_thunk.o

Makefile lib/Makefile lib/get_pc_thunk.S:
Add new file get_pc_thunk.o
2025-12-30 20:15:35 +01:00
Dylan Fei
5ce2c4b454 arm64-gen: fix 16-byte alignment for variadic arguments on stack
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-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
According to AAPCS64, when an argument requires 16-byte alignment
and is passed on the stack, the stack address must be rounded up
to the next 16-byte boundary.

TCC previously failed to perform this alignment check in gen_va_arg,
leading to data corruption when a 16-byte aligned argument followed
an 8-byte argument on the stack.

Note: This issue is most prominent on Mach-O/Apple Silicon where
variadic arguments are passed entirely on the stack. Testing shows
this fix resolves the failure on macOS while remaining compatible
with Linux/ARM64 (e.g., Raspberry Pi).

Modified gen_va_arg to perform a round-up (addr + 15) & ~15 when
align == 16.
2025-12-29 00:57:35 +08:00
Dylan Fei
b8513fe895 arm64-gen: fix address calculation for large symbol offsets
When accessing a global symbol with an addend > 0xffffff, the AArch64
backend incorrectly encoded an 'add xr, xt, #0' (Add Immediate)
instead of 'add xr, xr, xt' (Add Register).

This resulted in the base address of the symbol being overwritten
by the offset value rather than being summed with it.

Fixes the issue where (sym + 0x1000000) would resolve to 0x1000000
instead of the correct memory address.
2025-12-28 21:32:58 +08:00
Dylan Fei
11118be717 Fix pointer difference issue in aarch64/riscv64
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-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
When calculating the difference between two pointers (TOK_PDIV), the
AArch64 and RISC-V64 backends used unsigned division
instructions (udiv/divu).

This patch moves TOK_PDIV to use signed division instructions (sdiv/div)
for both backends, ensuring correct behavior as per the C standard.
2025-12-23 16:01:10 +08:00
herman ten brugge
5a370d8a6b Relocation updates
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-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
x86_64-link.c:
Check relocations R_X86_64_32/R_X86_64_32S. These relocations
can become to large if not configured with '--disable-static'
or '--config-pie' and code is run with -run -ltcc.
This only happens on x86_64.

tccdbg.c:
1) Add some .debug (dummy) sections when dwarf is used. These
   sections are otherwise not relocated correctly for R_DATA_32DW.
2) Fix stab problem when reloc is applied twice.
2025-12-23 06:46:22 +01:00
herman ten brugge
64cf0b816b undo x86_64-link.c commmit
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-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
The relocation check does not work with stabs enabled on some targets.
It works fine on fedora with and without stabs.

So remove check.
2025-12-21 18:49:21 +01:00
herman ten brugge
96119149fe Problems solved on for libtcc, pie, riscv64, arm64
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-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
I found some problems with a testcase from mailing list.

On x86_64 an overflow on reloc R_X86_64_32 occurred that was
not reported when using -run -ltcc.
The problem could be solved by compiling tcc with -fPIE, -pie
or --disable-static.

Makefile, configure, libtcc.c, x86_64-link.c:
- add --config-pie to configure help. Ignore -pie in libtcc.c
  and check reloc overflow in x86_64-link.c

arm64-gen.c:
- Fix reading from constant like '*(int *)0x7fffb7f1280c'

elf.h, riscv64-link.c:
- fix for -run -ltcc. Ignore reloc R_RISCV_SET_ULEB128 and
  R_RISCV_SUB_ULEB128 that are used in .debug_loclists section.
2025-12-21 07:05:55 +01:00
herman ten brugge
34eed88a70 Small updates
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-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
Allow 'make speedtest' in tests directory
Fix compiler warning tccpp.c
2025-12-17 20:22:08 +01:00
herman ten brugge
3c18df610d Fix clang macos 15 bug on arm64 in tests/tcctest.c 2025-12-17 19:54:22 +01:00
herman ten brugge
f3de8b5307 Use macos 15 in build.yml 2025-12-17 18:21:01 +01:00
herman ten brugge
8569427459 Move -run -nostdlib code to lib directory
tcc.h, arm-gen.c, arm64-gen.c, i386-gen.c, riscv64-gen.c, x86_64-gen.c:
- remove old code

tccrun.c:
- update to use lib/run_nostdlib.c

Makefile, lib/Makefile, lib/run_nostdlib.c:
- new code

tests/nostdlib_test.c:
- testcode
2025-12-15 14:45:11 +01:00
Stefan
829c848520 tccpp.c: Configurable integer literal overflow handling
Add the define TCC_CUT_ON_INTEGER_LITERAL_OVERFLOW to use the most
significant digits of an integer literal before its parsing led to an
overflow.

When compiling tcc with another compiler, which is not able to handle
64 bit arithmetic, it is beneficial to use the last value before an
integer literal overflows. Parsing 0x1000000000000000 then results in
0x10000000. The mescc from GNU Mes is able to compile a first tcc on a
32 bit system without 64 bit arithmetic. This change allows this first
tcc to compile a second tcc with complete 64 bit arithmetic.
2025-12-13 18:53:15 +01:00
Aleksi Hannula
cb41cbfe71
x86-64: Fix cvts*2si
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-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
Co-authored-by: Herman ten Brugge <hermantenbrugge@home.nl>
2025-12-03 17:21:00 +02:00
Reini Urban
831c3fa184 github CI: pinact run -u
for security
2025-12-02 16:31:13 +01:00
Aleksi Hannula
0fcd46f364
rv64: Generate VT_CMP output from float comparison 2025-12-02 07:30:31 +02:00
Aleksi Hannula
b4569233cb
Set TYPE_PARAM parsing mode in old-style param lists 2025-12-02 07:30:27 +02:00
herman ten brugge
f5f544f436 allow cross compiling for alloca on arm64 2025-12-01 18:09:04 +01:00
Aleksi Hannula
7479a5c21a
elf: Set ph sizes only if ph was allocated 2025-11-29 22:28:36 +02:00
Aleksi Hannula
78e5e690a2
rv64: Implement some pseudo-ops from Zicsr 2025-11-29 21:11:46 +02:00
herman ten brugge
6da45946ae Add bound check support for alloca on all targets
stddef.h, tccdefs.h, tccgen.c, tests/boundtest.c, tests/tcctest.c:
- remove ifdef arround alloca

lib/Makefile:
- move alloca-bt.o to COMMON_O

lib/alloca-bt.S:
- add alloca bound check code for arm, arm64, riscv64

lib/alloca.S:
- check for leading underscore

tcc.h, libtcc.c:
- include arm64-asm.c instead of arm-asm.c for arm64

tcctok.h, tccasm.c:
- add simple reloc support (only for R_AARCH64_CALL26)
2025-11-29 07:54:28 +01:00
Aleksi Hannula
fc424c9f7b
Reorder "pure" attribute
Placing the definition of "pure" prior to TOK_DEFINE would cause it to
be treated as a keyword by tccgen. This breaks building gcc, in which
"pure" is used as a struct field name.
2025-11-28 16:07:24 +02:00
Aleksi Hannula
5b96aeb7fc
Implement some SSE/2 instructions
This patch implements some instructions required by musl 1.1.24.
2025-11-28 15:23:09 +02:00
Aleksi Hannula
06e24e7eed
Implement some RV64F/D instructions
This patch implements some instructions required by musl 1.1.24.
2025-11-28 15:23:07 +02:00
Aleksi Hannula
3c631fdb6d
Implement alloca for RV64 2025-11-28 15:17:06 +02:00
Aleksi Hannula
e7be7b192d
Simplify RV64 function epilogue
Previous sp can be computed based from only frame pointer and short
offset. ra and s0 can be restored from stack with constant fp offset.
2025-11-28 15:17:06 +02:00
H-language
3327327e5d Add WM_MOUSEHWHEEL to winuser.h 2025-11-26 11:10:10 +13:00
herman ten brugge
7e4fc3a0d0 Fix alloca for arm
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-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
2025-11-19 11:20:50 +01:00
kbkpbot
264229a0d4 Add arm64 alloca() support. 2025-11-19 17:17:05 +08:00
herman ten brugge
fa6a6bfbbd Move lib/va_list.c into include/tccdefs.h
This makes code with va_arg work with -nostdlib on x86_64.
2025-11-19 08:31:15 +01:00
herman ten brugge
3e8f1da9c5 Change asm into __asm__ in previous commit
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-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
2025-11-17 19:17:53 +01:00
herman ten brugge
c52b96cf85 Allow -nostdlib -run
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-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
The -run option did not support -nostdlib correctly.
The argc/argc/envp are now passed on stack according to sysv.

This implementation does not work for TCC_TARGET_PE.
Also arm64 does not work when running tcc compiled with itself.
This is because arm64 assembly is not implemented yet.

The following test code is used to check the code:

  #include <unistd.h>
  #include <sys/syscall.h>
  void _start() {
    syscall(SYS_write, 1, "hello world\n", 12);
    syscall(SYS_exit, 0);
  }

Run this with:
./tcc -nostdlib -lc -run a.c
make tcc_c
./tcc_c -nostdlib -lc -run a.c
2025-11-17 14:00:15 +01:00
DFP
d9ec17d334 Relaxed the 'incompatible pointer type' warning a bit
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-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
Do not emit that warning when the source struct (recursively) contains destination struct as the first member. For example, in this case the warning would have been emitted before, and is not anymore:

struct base {
	int a;
};

struct derived {
	struct base base;
	int b;
};

struct derived derived;
struct base *base = &derived;

I personally use that pattern in my code (in a bit more elaborate form) and so this change removes the warnings for those use cases, while still retaining it for everything else. Feel free to CC me on the mailing list if you have questions, suggestions or complaints.
2025-11-14 08:55:55 +01:00
herman ten brugge
ab2ce3b13a Ignore some more attributes
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-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
Detected when using -Wunsupported
2025-11-04 15:03:19 +01:00
Detlef Riekenberg
cdebce3079 tccpp: Add the define __STDC_HOSTED__ [C99]
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-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
Usually defined to 1, but when using "-nostdlib", the value is 0
(required for C99)

--
Regards ... Detlef
2025-11-01 18:11:49 +01:00
Avi Halachmi (:avih)
f4e01bfcab tcc -run: support custom stdin with -rstdin FILE
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-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
When tcc -run is used with input file from stdin, then run_main's
stdin (which is tcc's stdin) is already EOF after we've read it.
If the program operates exclusively on stdin, then it cannot be used.

This commit adds an option -rstdin FILE which sets run_main's stdin.

Can also be used in such case to set run_main's stdin to the tty,
using -rstdin /dev/tty (posix) or -rstdin con (windows).
2025-10-27 14:50:56 +02:00
Avi Halachmi (:avih)
234e2dd2bf tcc options: document behavior and clashes (no-op)
The tcc options behavior is non trivial, and it's also susceptible
to ambiguities - which do exist (but currently are not painful).

- Add a script 'optclash' which detects clashes/ambiguities.
- Document the parsing behaviour and current clashes/resolutions
  as comments in libtcc.c .
2025-10-27 14:50:54 +02:00
Aleksi Hannula
01d1b7bc76
Add support for f{l,s}d in riscv64-gen
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-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
2025-10-17 15:40:08 +03:00
herman ten brugge
19589288cb Some fixes and cleanups
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-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
tcc.h, tccgen.c:
  Add and use IS_BT_ARRAY
tccpp.c:
  free memory when size is 0 in realloc code
tccdbg.c:
  move common code to seperate function remove_type_info
2025-10-16 07:35:27 +02:00
herman ten brugge
edcd228214 Init global_expr variable
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-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
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.
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-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
2025-10-13 15:57:31 +02:00
grischka
34b45a69ff Reverts & cleanups
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-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
- 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
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-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
2025-10-07 14:32:13 +02:00
Stefan
bcfb872fd0 Makefile: Add space behind 'install -m'
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-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
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
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-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
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
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-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
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
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-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
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
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-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
* 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
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-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
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
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-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
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
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-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
2025-08-31 12:24:03 +02:00
herman ten brugge
e6ea0d0424 tccgen: fix several problems
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-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
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
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-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
2025-08-24 11:22:38 +02:00
herman ten brugge
6ca6328e29 Fix long double to long long for x86_64
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-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
2025-08-23 14:48:34 +02:00
grischka
acb2a909dd tccgen.c: fix two scope problems
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-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
... 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
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-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
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)
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-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
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
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-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
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
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-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
Bug found with openssl test suite.
2025-08-17 08:49:29 +02:00
herman ten brugge
f8eb5f4ab8 Add -znodelete linker option
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-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
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
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-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
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(#)).
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-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
2025-08-15 04:41:24 +02:00
herman ten brugge
03184fe33b riscv target did not handle FUNC_OLD correctly.
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-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
2025-08-11 06:49:22 +02:00
herman ten brugge
37e1c88d78 Convert float to double for old function code.
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-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
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
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-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
2025-08-07 11:32:09 +02:00
herman ten brugge
9dffcd29d3 Fix tests/boundtest.c
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-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
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
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-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
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.
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-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
I commited the wrong version in previous commit.
2025-08-03 18:41:32 +02:00
herman ten brugge
6694391b74 Update gcctestsuite
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-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
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
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-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
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
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-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
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
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-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
2025-07-21 00:48:51 -03:00
Haelwenn (lanodan) Monnier
2ffb5869e0 lib/atomic.S: add GNU libatomic copyright notice
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-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
2025-07-19 00:30:12 +02:00
herman ten brugge
2eca4df6e9 Correct riscv load_large_constant
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-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
2025-07-18 08:15:58 +02:00
herman ten brugge
7c23c48a93 Riscv struct saving
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-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
While adding some testcases for attribute cleanup I found a
bug in the riscv code. It modifies the sv->c.i in load_symofs.
If a structure contains more then one element only the
first is stored correctly. This only happens on a large
stack frame.
Fixed by saving/restoring sv->c.i.
2025-07-17 06:50:15 +02:00
grischka
9670d10294 fix github CI & stuff
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-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
workflows/build.yml:
- win32/64: install mingw32/64 gcc on msys (because the default
  gcc installed elsewhere seems to use ucrt, producing incompatible
  fp printf formats.)
tccgen.c:
- cleanup funcs: save any lvalues from return expressions.  Also use
  get_temp_local_var() which however was causing a problem on i386
  because its gfunc_call() removes the arguments from vstack and by
  that defeats the 'in-use'  tracking of get_temp_local_var().  Fixed by:
i386/arm/arm64/x86_64-gen.c:
- in gfunc_call(): save_regs before anything else, fixes
  problems seen in arm64/i386
tccpp.c:
- allow arm asm specific use of '#' in macros
libtcc.c:
- organize -M options, and:
tccpe.c:
- move the subsystem option parsing from libtcc.c
tccelf.c:
- improved error handling in tcc_load_ldscript()
lib/atomic.S:
- TCC_TARGET_... not defined when building the lib
- endbrNN security feature not supported by TCC
tests/tests2/136_atomic_gcc_style.c:
- never use standard assert() in tests
2025-07-16 21:32:21 +02:00
herman ten brugge
28d7fb85b2 Change windows_2019(deprecated) to windows_2022 in buid.xml
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-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
2025-07-14 16:39:33 +02:00
herman ten brugge
717a510a19 Enable atomic test for all targets 2025-07-14 15:37:13 +02:00
herman ten brugge
7657d871c1 Add endbr32 for i386
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-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
2025-07-14 08:27:14 +02:00
herman ten brugge
854d6c5ab4 Update arm and windows atomic functions
I edited the arm thumb code so it works on armv6k.
I used the minga64w libatomic.a for windows.
2025-07-14 08:17:42 +02:00
kbkpbot
0c12363fd3 arm64: Save func results before struct args
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-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
In gfunc_call(), structure members are loaded into registers during
argument handling.
This operation may overwrite previous function call results stored in
registers (e.g., s0). To prevent this, we must save function call
results to the stack before processing structure arguments.
2025-07-11 22:13:30 +08:00
herman ten brugge
32b597746c Update bcheck for recent atomic change
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-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
The functions fetch_and_add_arm, fetch_and_add_arm64 and
fetch_and_add_riscv64 where removed with atomic change.

Replace the code in bcheck.c with call to atomic_fetch_add.
2025-06-24 21:38:31 +02:00
kbkpbot
7f13f24e12 Use asm implements some atomic functions
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-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
Use asm implements for i386/x86_64/arm/aarch64/riscv:

 __atomic_load_[1,2,4,8]
 __atomic_store_[1,2,4,8]
 __atomic_compare_exchange_[1,2,4,8]
 __atomic_test_and_set_[1,2,4,8]
 atomic_thread_fence
 atomic_signal_fence
 atomic_flag_test_and_set
 atomic_flag_test_and_set_explicit
 atomic_flag_clear
 atomic_flag_clear_explicit

`atomic.S` is extracted from `gcc` v15.1.0 `libatomic.a`.
And generated by https://github.com/kbkpbot/gen_atomic.git
2025-06-24 22:40:07 +08:00
Urs Janssen
3f06cb0f2e always search library paths 2025-06-24 14:23:47 +02:00
Avi Halachmi (:avih)
2d4e4a69c4 win32: winbase.h: fix broken LOAD_LIBRARY_AS_IMAGE_RESOURCE
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-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
Typo fix: s/LINRARY/LIBRARY/
2025-06-23 10:30:24 +03:00
Detlef Riekenberg
5fad6dc4a3 libtcc: Fix a double free, when option -o is used twice
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-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
tcc_set_str() already calls tcc_free() to free the memory of the stringpointer in s->outfile.
With the additional tcc_free(), the memory would be freed twice.

--
Regards ... Detlef
2025-06-19 03:27:59 +02:00
Detlef Riekenberg
f2b9fcf85d libtcc: Avoid a regression for some usecases
Thanks tylo for hints on the mailing list

--
Regards ... Detlef
2025-06-19 03:12:41 +02:00
herman ten brugge
59aecdb539 Another hex float fix
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-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
Hex floating point number did fail when a lot of digits are present.
See testcase 70.
2025-05-28 21:33:30 +02:00
herman ten brugge
c6792dac03 Add LIBS when testing tccb on bsd
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-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
ldexpl is in -lm library on bsd
2025-05-27 20:50:33 +02:00
herman ten brugge
cb39bc4cac Calculate hex floating point with 128 bits instead of 64 bits.
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-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
2025-05-27 07:36:33 +02:00
grischka
83de532563 revert "Save registers around attribute cleanup" (almost)
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-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
In fact, we don't need to save registers.  We need to
save the symbol if it is a SValue on vstack (the return
value in this case)

Replaces b6a16e3be4
2025-05-25 00:20:23 +02:00
herman ten brugge
b6a16e3be4 Save registers around attribute cleanup
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-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
This makes attribute cleanup code work the same as gcc and also
makes bound checking a very little bit faster.

tcc.h:
  Add save_return_reg(CType *) and restore_return_reg(CType *)
  Change gfunc_epilog() to gfunc_epilog(Sym *)

arm-gen.c:
arm64-gen.c:
c67-gen.c:
i386-gen.c:
il-gen.c:
riscv64-gen.c:
x86_64-gen.c:
  Move save and restore register around bound_local_delete call
  to save_return_reg and restore_return_reg.
  Pass func_type from gfunc_epilog to gen_bounds_epilog.

tccgen.c:
  Call save_return_reg/restore_return_reg in try_call_scope_cleanup
  when RETURN is found.

tccrun.c:
  Fix warning when bound checking not used.

tests/tests2/101_cleanup.c
tests/tests2/101_cleanup.expect
  Extra checks attribute cleanup save/restore registers.

tests/tests2/Makefile:
  Fix when bound checking not used.
2025-05-22 16:58:12 +02:00
Detlef Riekenberg
6ca228339c arm-asm: Accept additional register names
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-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
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
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-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
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
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-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
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
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-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
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
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-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
2025-03-20 00:34:39 +01:00
waterlens
52a9a541b0 Fix tcc -run on Windows
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-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
2025-03-19 12:03:52 +08:00
waterlens
a06c608625 Add fake _Float16 type to enable the latest math.h on macOS
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-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
2025-03-19 00:23:20 +08:00
grischka
8c4e67380e arm bits
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-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
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
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-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
2025-03-11 13:16:39 +01:00
remph
6ec4a10652 Process linker directives beginning with a colon
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-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
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
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-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
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
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
herman ten brugge
322c4dc275 Add support for backtrace()
This requires adding .eh_frame and .eh_frame_hdr sections.

There are 3 new functions to setup the sections:
tcc_eh_frame_start: create cie in .eh_frame
tcc_debug_frame_end: add fde in .eh_frame for every function
tcc_eh_frame_hdr: create .eh_frame_hdr

The PT_GNU_EH_FRAME header is created.

The dwarf read functions are moved from tccrun.c to tcc.h

The backtrace() function is not supported on all targets.
windows, apple, bsd and arm are disabled.
arm uses its own sections .ARM.extab and .ARM.exidx.
2024-11-09 08:04:45 +01:00
John Nunley
a21b5f1fd7
x86_64-asm: support endbr64 instruction
endbr64 has no operand but comes with a ModR/M byte. Handle it in the
same way as *fence instructions.

Co-authored-by: Yao Zi <ziyao@disroot.org>
Signed-off-by: Yao Zi <ziyao@disroot.org>
Signed-off-by: John Nunley <dev@notgull.net>
2024-10-27 15:33:23 -07:00
John Nunley
6acf301e77
x86_64-asm.h: support callq for better compat
It has the same effect as call.

Co-authored-by: Yao Zi <ziyao@disroot.org>
Signed-off-by: Yao Zi <ziyao@disroot.org>
Signed-off-by: John Nunley <dev@notgull.net>
2024-10-27 15:27:00 -07:00
tuxcrafting
50cfe1141b fix -section-alignment smaller than ELF header size 2024-10-24 13:02:28 +03:00
Avi Halachmi (:avih)
d9f1836124 win32: tcc-win32.txt: fixup 2 mingw includes note
Note to self: review few times before pushing next time.
2024-10-23 16:32:49 +03:00
Avi Halachmi (:avih)
c7bf40b958 win32: tcc-win32.txt: fixup mingw includes note 2024-10-23 16:21:05 +03:00
Avi Halachmi (:avih)
4f7b8304df win32: tcc-win32.txt: add note about mingw includes 2024-10-23 16:14:40 +03:00
Avi Halachmi (:avih)
45788e91ca win32: make #include <unistd.h> work
We already have sys/unistd.h, but the standard place for it is at
the include root, so make that work too, but keep sys/unistd.h for
backward compatibility.
2024-10-23 14:58:41 +03:00
Avi Halachmi (:avih)
e1c8d3a1e6 win32: include/sys/types.h: add useconds_t
Required by unistd.h in the auxiliary package:
  winapi-full-for-0.9.27.zip
2024-10-23 14:48:50 +03:00
grischka
9fb89c23d0 tcc -ar: pad archive member position to even
Since commit 45cff8f0 tcc eventually generates object files
of non-even size.

tccelf.c:
- check ARFMAG for better invalid archive detection
- file_offset needs to be aligned, not the size (just a nitpick)
lib/Makefile:
- remake everything when tcc did change
2024-10-22 21:38:31 +02:00
herman ten brugge
d7f9166ab5 Fix last commit. 2024-10-20 11:05:10 +02:00
herman ten brugge
5a467ddc98 Move sort_syms to avoid linker error
compiling with tcc and linking with gcc gives error:

tcc -c a.c
gcc a.o
usr/bin/ld: a.o: .symtab local symbol at index 0 (>= sh_info of 0)
/usr/bin/ld: a.o: error adding symbols: bad value
collect2: error: ld returned 1 exit status

Solved by moving call to sort_syms.
2024-10-20 10:57:50 +02:00
Steffen Nurpmeso
7a6f3fded4 include/tccdefs.h: has_atttribute -> attribute (fixes 45cff8f03f) 2024-10-20 04:41:13 +02:00
grischka
45cff8f03f tccelf.c: write section headers before sections
also avoid zero lenght PT_LOAD segments (which some musl
loaders seem to dislike)

Also:
- tccasm.c: support .section with flags: .section .xyz,"wx"
  (fixes e4d874d88a)
- tccgen,c:  add __builtin_unreachable()
- tccdefs.h: #define __has_attribute(x) 0
- tcc.c: tidy help for -std
- tcctools.c/execvp_win32: quote strings more correctly
- x86_64-gen.c:win32: set unwind begin-address to function-start
- github action: add aarch64-osx (M1) & i386-win32
- configure: consider 32-bit build on MSYS64 native
2024-10-13 23:55:32 +02:00
Ben Noordhuis
c21576f8a3 Emit better x86_64 asm for constant loads
Instead of always emitting movabs, emit a regular mov or a xor.
Slims down sequences like:

    movabs $0,%rax
    mov %rsi,%rax

To:

    xor %eax,%eax  // also zeroes upper word
    mov %rsi,%rax

Future work is to just emit:

    xor %esi,%esi
2024-10-10 22:48:56 +02:00
Petr Skocik
b668b72b06 recognize -std=gnu11 and treat it the same as -std=c11 2024-10-09 08:22:53 +02:00
Meng Zhuo
3110f69e4e update Github action runner macOS to 12 2024-10-08 17:51:34 +08:00
Boian Berberov
3ead10dd94 fix: respect CFLAGS from environment, or set defaults 2024-09-27 12:26:21 -06:00
Guest0x0
b8b6a5fd7b fix UB in constant folding of double -> signed integer conversion 2024-09-14 06:17:23 +00:00
Maxim Logaev
12acbf3e92 tccdbg.c: DW_AT_language now matches the -std option
Signed-off-by: Maxim Logaev <maxlogaev@proton.me>
2024-08-20 17:58:50 +03:00
noneofyourbusiness
3d963aebcd
Relicensing TinyCC 2024-08-11 15:18:36 +02:00
noneofyourbusiness
3415dec979
riscv64-tok.h: don't export internal macros 2024-08-11 00:41:21 +02:00
noneofyourbusiness
3d65c596a2
tcc -dumpmachine: output -musl instead of -gnu when TCC_MUSL is defined 2024-08-11 00:18:21 +02: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
08a4c52de3 tccpp: tcc_warning("extra tokens after directive")
with stuff like
    #endif int x;
Also fix
    /* */ #else
Also:
- search_cached_include(): search for file->true_filename
- tccasm.c: avoid crash with .file
2024-06-11 14:42:56 +02: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
herman ten brugge
8cd21e91cc Address of solved for riscv64
A character size load was used instead of pointer size some times.
2024-06-01 07:29:28 +02:00
Avi Halachmi (:avih)
da5aa7d7a8 win32: wincon.h: support more console mode flags
Mainly VT modes (win 10+), quick-edit, insert.
2024-05-12 10:51:20 +03:00
Ekaitz Zarraga
0aca861194 fixup! riscv: Implement large addend for global address
Use `t1` instead of `t0` for the cases when `rr` is not set so `t0` is
used by default and this happens:

    lui t0, XXX
    add t0, t0, t0

Instead, now we do:

    lui t1, XXX
    add t0, t0, t1
2024-04-28 00:15:23 +02:00
Ekaitz Zarraga
8baadb3b55 riscv: asm: implement j offset 2024-04-25 15:13:21 +02:00
Ekaitz Zarraga
159776304f riscv: asm: Add branch to label 2024-04-24 00:54:51 +02:00
Ekaitz Zarraga
671d03f944 riscv: Add full fence instruction support
This commit adds support for `fence`'s predecessor and successor
arguments.
2024-04-23 15:10:08 +02:00
Ekaitz Zarraga
c994068175 riscv: asm: Add load-reserved and store-conditional
Add Atomic instructions `ld` and `sc` in their 32 bit and 64 bit
versions.
2024-04-23 12:05:05 +02:00
Ekaitz Zarraga
0703df1a6a Fix Extended Asm ignored constraints
This commit fixes the case where the register of for the Extended Asm
input or output is known. Before this commit, the following case:

  register long __a0 asm ("a0") = one;
  asm volatile (
       "ecall\n\t"
       : "+r" (__a0) // NOTE the +r here
  );

Didn't treat `a0` as an input+output register (+ contraint) as the code
skipped the constraint processing when the register was already chosen
(instead of allocated later).

This issue comes from f081acbfba, that was
taken as a reference in every other Extended Assembler implementation.
2024-04-16 02:47:56 +02:00
herman ten brugge
4944f509c3 riscv: Avoid some compiler warnings 2024-04-13 16:26:12 +02:00
Ekaitz Zarraga
6b3cfdd025 riscv: Add extended assembly support
NOTE: In order to be able to deal with general-purpose vs floating-point
registers, this commit adds a flag in the 6th bit of the register. If
set, it means the register is a floating-point one. This affects all the
assembler.
2024-04-09 00:19:41 +02:00
Ekaitz Zarraga
e02eec6bde riscv: fix jal: fix reloc and parsing 2024-03-27 11:50:02 +01:00
Ekaitz Zarraga
0239133488 fixup! riscv: Add .option assembly directive (unimp) 2024-03-23 12:32:32 +01:00
Ekaitz Zarraga
cbe70fa629 riscv: Add .option assembly directive (unimp) 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga
618c173421 riscv: libtcc1.c support some builtins for __riscv 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga
3782da8d0c riscv: Support $ in identifiers in extended asm.
Needed for using `__global_pointer$`.
$ don't have special meaning in RISC-V assembly.
2024-03-21 13:33:27 +01:00
Ekaitz Zarraga
e2d8eb3d1c riscv: jal: Add pseudo instruction support 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga
409007c9d5 riscv: jalr: implement pseudo and parse like GAS 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga
8bfef6ab18 riscv: Add pseudoinstructions
call, tail, jump, jr, mv, not, neg, negw, seqz, snez, sltz, sgtz, bnez,
beqz, blez, bgez, bltz, bgtz, li
2024-03-21 13:33:25 +01:00
Ekaitz Zarraga
8cbbd2b88a riscv: Use GAS syntax for loads/stores:
Before:
    ld rd, rs, imm
    sd rs1, rs2, imm

Now:
    ld rd, imm(rs)
    sd rs2, imm(rs1)

NOTES: Just as in GAS:
    - In stores the register order is swapped
    - imm is optional
    - when imm is not included parenthesis can be removed
2024-03-19 12:38:21 +01:00
Ekaitz Zarraga
019d10fc12 riscv: Move operand parsing to a separate function 2024-03-19 12:38:21 +01: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
herman ten brugge
9675c1e245 Use CONFIG_RUNMEM_RO=1 on apple 2024-03-03 06:41:27 +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
Ekaitz Zarraga
7bc0cb5ba0 riscv: Implement large addend for global address 2024-02-29 19:42:02 +01:00
herman ten brugge
2656f8edcc Fix win64 noreturn problem
On win64 the code would hang in longjump with previous change
2024-02-28 07:37:09 +01:00
herman ten brugge
f9cb198aa8 tccrun: update for apple and openbsd
Apple needs CONFIG_RUNMEM_RO=1
I now only set CONFIG_RUNMEM_RO=0 on _WIN32

Openbsd does not have malloc.h so remove some code

Also fix some warnings when compiling lib with gcc
2024-02-26 07:18:07 +01:00
grischka
4e91d38ddc tccrun: resign from "advanced" system calls (memaligh/gettid)
... let's stay compatible
2024-02-25 19:55:06 +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
Detlef Riekenberg
f2e8b2ad79 bcheck: fix argument order for memalign
Spotted by George Sedov on the mailing list.

The bug was in the tcc source since >20 years:
https://repo.or.cz/tinycc.git?a=commit;h=ad28d4c5b03da27dc0381afb58f255d49962cca0

manpage for memalign:
https://linux.die.net/man/3/memalign

--
Regards ... Detlef
2024-02-15 00:56:54 +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
kbkpbot
76d605192d add MemoryBarrier marco define; tested gcc msvc 2024-02-08 11:13:51 +08:00
herman ten brugge
cd6ad857cf Fix default_reallocator declaration 2024-02-07 07:01:36 +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
kbkpbot
105d70f7b4 atomic_load/atomic_store : to ensure return the latest value, should we add a memory barrier here?
If have no these memory barriers, sometime it will cause bug in multiple threads program.
2024-02-05 08:37:41 +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
Ekaitz Zarraga
6426cc3384 Revert "riscv64-gen: Fix load and store type_size usage"
It was already contemplated by c81116e29a.

This reverts commit c7263571d2.
2024-01-30 17:41:26 +01:00
Ekaitz Zarraga
c7263571d2 riscv64-gen: Fix load and store type_size usage
In `load` and `store` RISC-V gen used `type_size` to retrieve the size
of the types being moved around, the problem with that function is it
tries to obtain internal information of the type. When using `type_size`
with a `char *` it returns 1, and emits a `lb` instruction when using a
conditional expression like so (but probably also in other cases):

    char *a, *b;
    b = "hello";
    a = x ? b : "" ;                // this emits an `lb`

That `lb` clobbers the pointer, only loading the latest byte of it:

    // if `b` was, say: 0x1f822e
    a = b;
    // now `a` is 0x00002e

NOTE: We spotted this when building make-3.82, in `init_switches`
      function in the `main.c` file. This error made `make` unable to
      run long options like `make --help` (segfault when doing the
      `strlen` later)

This happens because a `char *` is internally stored as a `char[1]` or
something similar, it's result is the size of a `char` (1) times the
size of the array `1`. This is not what we want, we are copying the
pointer, not the array itself. Using `type_size` for this is not
appropriate even if it works for some cases.

If the conditional expression is rewritten to imperative style using an
`if` it works properly, so it might be related with the fact the pointer
is `load`ed to a register.

`load` and `store` should only work with integral types, so reading the
size of the array is not useful. This commit creates a simpler version
of this function that only reads the integral type of what's working
with: char * is considered just a pointer. So it makes an `ld` instead,
and the code above works.
2024-01-29 11:09:59 +01:00
herman ten brugge
7f0a28f6ca C23: Implement declaration after label
Only allow declaration after label if it is inside '{...}'
2024-01-28 07:18:12 +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
Andrius Štikonas
04365dd4c9 riscv64-asm.c: fix assembly instruction with negative immediate offsets.
This fixes expressions like ld a0, s0, -24 that regressed in
d87801bd50
2024-01-12 21:09:21 +00:00
herman ten brugge
6120656cbf Rewrite gexpr a bit 2024-01-08 11:34:47 +01:00
herman ten brugge
2701dcfb06 Add some relocations to riscv64-link.c
dlltest failed on a riscv machine.
2024-01-07 07:45:31 +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
herman ten brugge
48798969c5 Update riscv64-asm prototypes 2023-12-16 12:28:19 +01:00
noneofyourbusiness
ada17a08eb
riscv64-asm.c: add support for calculating addresses of symbols
add some pseudoinstructions
riscv64-tok.h: add pseudoinstructions from tables 25.{2,3}
2023-12-12 09:44:37 +01:00
noneofyourbusiness
b390feec6d
riscv64-asm.c: add Zicsr registers 2023-12-10 15:24:25 +01:00
noneofyourbusiness
5dc241fee1
riscv64-tok.h: add Zicsr pseudoinstructions, registers 2023-12-10 15:22:41 +01:00
noneofyourbusiness
3b3c9412ac
riscv64-asm.c: implement Zicsr extension 2023-12-09 14:32:14 +01:00
noneofyourbusiness
279dbb94e2
riscv64-asm.c: correct check for 12-bit immediate
asm_emit_cj: correct check for offset size
2023-12-09 01:00:16 +01:00
noneofyourbusiness
275dfbea20
riscv64-asm.c: implement M extension 2023-12-08 22:48:43 +01:00
noneofyourbusiness
c71415b543
riscv64 elf flags should be configurable 2023-12-08 17:08:03 +01:00
noneofyourbusiness
d87801bd50
riscv64-asm.c: implement C extension
add nop
fix asm_emit_i immediate check (negative offsets were missing)
fix check for IM12S
remove non-existent instructions (example: slli64 is just slli with imm=0)
2023-12-08 17:07:06 +01:00
noneofyourbusiness
81a32ec305
riscv64-asm.c: asm_emit_j: correct check of immediate 2023-12-02 17:08:05 +01:00
Reimar Döffinger
0655fd9637 configure: enable codesign by default on macOS.
It is useful for the default configuration to just
work out of the box and pass tests instead of crashing
in unexplained ways.
2023-11-27 20:17:52 +01:00
Reimar Döffinger
c45cb650fc tcctools.c: reduce duplicated code for -MP option.
Generate list of escaped dependencies as a separate
first step.
2023-11-27 20:17:21 +01:00
noneofyourbusiness
70328621f1
riscv64-asm.c: added asm_emit_j (J-type), changed jal to J-type
additionally added a comment about B-type instruction format
2023-11-27 09:15:32 +01:00
Rob Pilling
fb164e0ab4 Error out for incomplete type initialisation 2023-11-26 09:29:06 +00:00
herman ten brugge
be8f894710 Check for errors before codesign 2023-11-08 21:08:54 +01:00
herman ten brugge
fc8c01861b Fix STT_NOTYPE problem on win32
Since 'make undefined global symbol STT_NOTYPE' change win32 code
did not build any more.
2023-11-08 21:03:10 +01:00
herman ten brugge
ded713e90d Ignore as_needed in ld_add_file_list
After the change to DT_NEEDED I get warnings for some functions.
The reason is that libc.so on my machine contains:
GROUP ( /lib64/libc.so.6 /usr/lib64/libc_nonshared.a  AS_NEEDED ( /lib64/ld-linux-x86-64.so.2 ) )

Before the change to DT_NEEDED we solved the symbols because the
/lib64/libc.so.6 file has as DT_NEEDED set for ld-linux-x86-64.so.2
The above AS_NEEDED section was not followed so symbols in this
file gives a warning.
Currently fixed by including AS_NEEDED files.
2023-11-08 19:58:26 +01:00
herman ten brugge
0f29dbcfd5 Update do_debug handling
Make options '-g -b' and '-b -g' set -g2 in both cases.
2023-11-08 19:52:13 +01:00
herman ten brugge
bf928f3f4f Allow libtcc1-usegcc in lib/Makefile 2023-11-08 19:48:39 +01:00
herman ten brugge
be8cded098 Allow make tcov-test on bsd targets 2023-11-08 19:45:29 +01:00
Steffen Nurpmeso
2c1c20a48c Relicensing TinyCC 2023-10-31 22:59:44 +01: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
b7c732f01c tcctools: delete created ar file on error.
Leaving 0-sized files around might confuse the build system.
2023-10-29 18:13:19 +01:00
Reimar Döffinger
a473473fed stdatomic.h: Add ATOMIC_VAR_INIT macro. 2023-10-29 18:12:31 +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
Reimar Döffinger
32ac23656f Add support for -dumpmachine option.
Better compatibility with some build systems assuming gcc.
2023-10-29 15:55:16 +01:00
Reimar Döffinger
7a53029e4d Add -MP option. 2023-10-29 15:31:58 +01:00
Reimar Döffinger
1bfed06c2a Relicensing TinyCC 2023-10-29 14:53:06 +01:00
Reimar Döffinger
e9aa113240 tccdefs.h: Apple target requires __has_builtin definition.
Note: I have not been able to create working binaries on
macOS 14.1 so far, but at least tcc compiles and produces
binaries now...
2023-10-29 14:45:19 +01:00
Reimar Döffinger
fd775d941d arm64-gen.c: Add __AARCH64EL__ target_machine_defs.
The Python headers require __AARCH64EL__ to be defined.
Also simplify ifdef to avoid duplicating __aarch64__ entry.
2023-10-29 14:36:11 +01:00
Detlef Riekenberg
32c4df1497 tccelf: Do not load all referenced libraries when linking a library
Recursive loading of all references can break linking of libraries
(Example: building of netbsd-curses)

Thanks grischka, Michael and Herman for the comments.

--
Regards ... Detlef
2023-10-26 17:51:30 +02:00
Ben C
6b967b1285 Fix macOS .dylib build when VERSION contains letters.
macOS builds with --disable-static fail to link if the version number
contains letters because of the -current_version and
-compatibility_version arguments (current version is 0.9.28rc).

This commit adds a new MACOS_DYLIB_VERSION variable to config.mak for
builds that target macOS. It has no impact on other targets.
2023-10-25 22:38:07 -04:00
Sylvain BERTRAND
f44060f8fc ELF Relocatable: make undefined global symbol STT_NOTYPE
If undefined global symbols in ELF Relocatable files are not of
type STT_NOTYPE, it will confuse binutils bfd and corrupt product
files.
2023-10-24 18:13:39 +00:00
Petr Skocik
08c777053c Support aliasing static global nonfunction objects
Enables code such as:

    #undef NDEBUG
    #include <assert.h>
    #include <stdint.h>
    static int st_x = 42;
    static int st_x_ __attribute((alias("st_x")));
    int main(void){ assert((uintptr_t)&st_x == (uintptr_t)&st_x_); }

which would previously fail with no compiler warnings. The limitation of
this is that the alias must be done (or redone) after an actual definition.
An alias done right after a later overridden tentative declaration won't
work (sufficient for my use case).
2023-10-08 19:08:20 +02:00
Eric Raible
b214fb6ed3 Produce better error message on malformed while loop
echo "void bugged() { do {} }" | tcc -run -
now produces the sensible:  -:1: error: 'while' expected (got "}")

I believe (but am far from sure) that the additional use of &tokc is ok.
2023-10-03 22:13:56 -07:00
Detlef Riekenberg
3d128041c3 include/tccdefs: Add function renaming with __REDIRECT_NTHNL (0.9.28rc testing)
Fixes a compilation break in "glob.h"
Detected during tcc 0.9.28rc testing.
Affected project: https://github.com/craigbarnes/dte

--
Regards ... Detlef
2023-09-17 12:15:28 +02:00
Sergey Sushilin
7f39b4f573 Fix typo in atomic_compare_exchange_weak() 2023-09-09 23:28:45 +03: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
8c5fe87665 tcc -g1 : small debug info (lines/functions only) 2023-09-06 22:42:47 +02:00
grischka
452045422b win32: pe dwarf sections & option -g.pdb
On windows, create a .pdb file with option "-g.pdb"
Such executables created by tcc can be debugged with
"ollydbg" or "x64dbg"

This currently relies on the 3rd party tool cv2pdb from
    https://github.com/rainers/cv2pdb
which again relies on
    mspdbsrv.exe mspdbcore.dll msobj80.dll mspdb80.dll
from a MSVC installation.

cv2pdb.exe + the ms* files may be put in the path or in the
same directory as tcc.exe.
2023-09-06 22:42:44 +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
grischka
3f3cbb51ed bcheck: remove "tcc_location()" & x86_64 double fix
using (modified) tcc_backtrace() instead.
Also

Also fix the original bug with doubles on x86_64.
(which was not caused by incr_offset() actually).
See 598134fff6

Also cleanup on_exit() stuff
From fef701b57f
2023-09-06 22:42:21 +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
noneofyourbusiness
d1c107738b
riscv64-asm.c: add jal/jalr
this implements the base instructions, not the pseudoinstructions

examples
 jal ra, 0
 jalr x0, ra, 0
2023-08-11 10:08:41 +02:00
noneofyourbusiness
e70fec871b
riscv64-tok.h: update with more instructions from the spec
defined tokens for C, M, Ziscr extensions.

separate the base RV32 instructions from the RV64, for potential future
re-use in a RV32-only assembler, from which the RV64-tok can #include

scall, sbreak have been renamed (page 7 of spec),
necessitating some renaming in riscv64-asm.c

riscv-spec-20191213.pdf was used,
in which the "V" extension is not yet ratified.
available under https://riscv.org/technical/specifications/

Tables 16.5–16.7 do not list any "scall"
neither does the privileged spec

3 additional tokens not present in the tables were removed

note that this riscv64-asm.c still contains defects, which will
be addressed in another commit
2023-08-10 14:25:03 +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
grischka
022fb4293b tccpp: #pragma once normalized
In order to detect a "same file" faster, this makes one
restriction to the feature: the basenames are required to
match at least (as in "test.h" and "dir/../test.h")

Also remove obsolete 'pp_once' (cached includes do not persist
across compilations anymore anyway)

This rewrites commits
30fd24abd4
4e363a1728

 3 files changed, 38 insertions(+), 74 deletions(-)
2023-07-31 12:22:10 +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
c92f4f52d9 Update pragma once for file operations
Use open/read/close instead of fopen/fread/fclose for tcc4tcl and ziptcc
2023-06-26 11:19:41 +02:00
herman ten brugge
4e363a1728 Small update on pragma once
Update on calc_file_hash. Use xor instead of +.
Only calculate has if file name differs in search_cached_include.
2023-06-26 06:33:46 +02: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
OldWorldOrdr
5077d4c915 change which to command -v 2023-06-12 03:53:05 -04:00
waltje
583c3b4746 Update to previous patch (remove the strcpy.) 2023-05-29 20:46:44 -04:00
waltje
da3a763e97 Small patch to allow for the TCC.EXE (and TCCLIB.DLL) to reside in a (directory under) a "bin/" directory at the same level where the "include/" and "lib/" directories are. This allows one to set the PATH to the (proper) binaries directory and TCC will then "find" itself there. Tested under Windows 7, Windows 8.1 (ARM) and Windows 10+11, but should also be workable for Linux and macOS. 2023-05-26 20:19:21 -04:00
herman ten brugge
a46372e910 Add support for constant float compare 2023-05-22 15:09:59 +02:00
grischka
0ec3a40dfd about CONST_WANTED & VT_NONCONST
- integrate CONST_WANTED with nocode_wanted
- expr_const() checks VT_NONCONST
2023-04-25 15:43:38 +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
a045400501 CStrings leakless
avoid memory leaks with lost CStrings on stack after errors.

tccpp.c:
- use/abuse static Cstring tokcstr where possible
tccgen.c:
- use/abuse static Cstring initstr where possible
tcc.h/libtcc.a:
- add 'stk_data' array to track memory pointer on stack
- add macros stk_push/pop() and cstr_new/free_s()
tccasm.c:
- use that
- use char[16] instead of char* for op.constraint
2023-04-25 15:22:20 +02:00
grischka
40131b7e0f MEM_DEBUG thread-safe & tccdbg leakless
libtcc.c:
- guard tcc_realloc/free_debug() with a semaphore
tccdbg.c:
- allow tcc_debug_end() to be called twice safely
2023-04-25 15:22:15 +02:00
grischka
7916cf71cc tcc_error_noabort(): always use this unless compiling
This avoids 'exit(1)' with errors outside of compilation
(nasty in particular with libtcc usage)

As a sideeffect multiple errors can be seen for linker
errors (such as undefined symbols, relocation errors, ...)
2023-04-25 15:22:03 +02:00
grischka
19ef024aa9 lib/armeabi.c: fix zero from/to double conversion
tccgen.c:
- allow cross-compiling 0.0L (cross-compile tcc with tcc)
tccelf.c:
- use alignment of TLS section for PT_TLS
tccpp.c:
- support long double constants on systems that do not support
  long doubles (i.e. mark them (VT_DOUBLE | VT_LONG))
tccdefs.h:
  #define __has_feature() for android
  remove _unaligned (why define a msvc extension under !_WIN32)
2023-04-25 08:59:42 +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
herman ten brugge
6a24b762d3 Do not crach when compiling with libtcc. 2023-04-15 15:36:13 +02:00
herman ten brugge
24c930a9b8 Allow _unaligned and __unaligned on WIN32 2023-04-15 15:33:14 +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
Detlef Riekenberg
e7262accb6 Print correct values in tcc_print_stats (yarpgen v1)
Value 0 and values >2GB where printed wrong during tests with yarpgen (v1)
Starting the output with a hashtag make it compatible to TAP

--
Regards ... Detlef
2023-03-23 20:02:19 +01:00
Detlef Riekenberg
afa05caacf Ignore _unaligned and __unaligned. Required >15years ago, but still present in old code
These extensions where used by other compiler
to generate different code on old cpu's (arm and x86 before pentium/pentiumpro)
to prevent processor exceptions / very high delays when accessing an unaligned pointer.

--
Regards ... Detlef
2023-03-23 17:14:29 +01:00
herman ten brugge
95aab7d687 tests/tcctest.c: Add support for osx with clang 10 2023-03-18 14:02:52 +01:00
Avi Halachmi (:avih)
e21523529b configure: OSX/macos: autodetect new_macho value
Use old (=no) if autodetect succeeded and osx ver <= 10, else new.
Override is possible, and now also documented at ./configure --help.
2023-03-18 11:42:48 +02: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
Detlef Riekenberg
7abf2c03d6 fix: Do not define INCLUDE_STACK_SIZE twice
We have that define already in tcc.h
and changing the define in tcc.h breaks compilation of tcc
without this fix.

--
Regards ... Detlef
2023-03-11 14:13:23 +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
cb3589c56e Increase VSTACK_SIZE to 512.
The yarpgen test code uses very complex expressions.
2023-03-11 07:40:56 +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
Michael Matz
ef3eb02ccb Fix type of cond-op
the optimization of a ternary op with two boolean operands had
the same problem (as in the last commit) of loosing the type.
2023-03-10 17:23:55 +01:00
Michael Matz
96b31ba670 Fix casted boolean expressions
the casted type was lost when a delayed bool was finally converted
to a value.  See testcase, in the wrong case the '(unsigned int)' cast
was ignored, and hence the division was signed reulting in -1 instead of
the proper 0x7fffffff.
2023-03-10 16:49:27 +01:00
herman ten brugge
2cb8bddd82 Add attribute nodebug support and skip bitfield padding
The following code:
typedef struct {
  unsigned int a __attribute__((nodebug));
  unsigned int b;
  unsigned int : 32;
  unsigned int c;
} tst;

Supresses a and also suppresses bitfield padding.
So debugger shows only b and c in above example.
2023-03-10 12:41:43 +01:00
Michael Matz
2cf3a6eb4d Fix more jumpopts (in ternary op)
the code in expr_cond save nocode_wanted around some parts of
expression evaluation, but at the wrong spots.  If the evaluation
of the condition itself (e.g. in the testcase the first whole ternary
expression) resulted in CODE_OFF, then that was saved, and restored
before return, even if in-between codegen would have CODE_ON'ed already.
Thus the whole CODE_OFF state bled out to outside the expression
evaluation and also disabled the whole if-block.  Found by yarpgen v1
(seed 64).
2023-03-09 18:23:16 +01:00
herman ten brugge
ec81877fa7 Fix test90 for 32 bits targets
Should have used 'long long' instead of 'long' for 32 bits targets.
2023-03-09 16:11:39 +01:00
herman ten brugge
80909254c4 Fix Makefile install target
Update target all before installing
2023-03-09 16:00:46 +01:00
herman ten brugge
3f0a1719dc Fix init bitfield padding with size 32/64
The init code did not work for padding bitfields with size 32 and 64.
2023-03-09 15:53:48 +01:00
Michael Matz
c771cb52fa Fix code suppression during nocode_wanted
we activate code (CODE_ON) only when the target labels are used,
which doesn't happen during nocode_wanted regions.  So, we cannot
just switch off code either during nocode_wanted regions, nothing
would switch it on again (except in the happy coincidences when we
outright save/restore nocode_wanted).  See the testcase for one
example, reduced from code generated by yarpgen: in
  ext = (xxx || 1)           // #1
        || ((xxx && 1) || 1) // #2
code is first suppressed in #1 normally, then (before this commit)
was suppressed via CODE_OFF during #2 (via indirect gjmp),
then the suppression from #1 was undone, but nothing undoes the
suppression from #2 anymore as everything therein was generated
while nocode_wanted was active.

So, we would either need to save/restore nocode_wanted around
some more expressions, activate CODE_ON also with unused labels (breaks
some optimizations we want), or deactivate CODE_OFF only when not
already in nocode_wanted state.  This does the latter.
2023-03-07 18:10:02 +01:00
Petr Skocik
ccc1651075 Create scopes for switches/whiles/do-whiles
The C standard has required this since at least C99.
A new scope should also be created for ifs, but that's currently
breaking tests2/122_vla_reuse.
2023-03-05 22:31:40 +01:00
Brian Callahan
29ae3ed4d5 OpenBSD has removed the syscall() function from its libc, so we
need to use getthrid() directly in lib/bcheck.c
2023-02-23 17:19:37 -05:00
Michael Matz
605538f46d Fix sym_scope of typedefs
Sym.sym_scope and Sym.f (FuncAttr) share space, so blindly setting
one clobbers the other.  Right now this only leads to missing errors
on incompatible typedefs (see testcase), which this commit fixes.

But it points to a larger problem:
Generally we can only manipulate Sym.f for anonymous and field symbols,
not for anything that has a top-level name (basically any proper decl),
because the latter use sym_scope.  Luckily the functions type always
contains an anonymous symbol (in sym->type.ref), so we can use that.
But some of the functions attributes actually _do_ apply to the decl,
not the type (e.g. always_inline), so we still have a problem possibly,
when we update an pre-existing type that may already be shared with
another decl.

Would need untangling and perhaps using accessor functions that check
that Sym.f and Sym.sym_scope aren't used for the same symbol.
2023-02-22 17:59:31 +01:00
Detlef Riekenberg
bdec3c5345 Use unsigned int to count sizes for -bench (YARPgen) (part 2)
I have a testfile created by YARPGen (seed=9), which displayed
a negative bss size without this patch.

* tcc needs also a bigger VSTACK_SIZE (i used 512)

--
bye bye ... Detlef
2023-02-03 18:04:56 +01:00
Detlef Riekenberg
b2485d6cd5 Use unsigned int to count sizes for -bench (YARPgen)
I have a testfile created by YARPGen (seed=9), which displayed
a negative bss size without this patch.

* tcc needs also a bigger VSTACK_SIZE (i used 512)

--
bye bye ... Detlef
2023-02-03 01:04:38 +01:00
herman ten brugge
4dc7662a07 Fix preprocessor line directive update
The last commit was not a problem with macos.
The problem was that dwarf version < 5 uses different indexes for
files and directories.
They start at 1 for dwarf version < 5 and 0 for dwarf version >= 5.
2023-01-17 20:08:14 +01:00
Christian Jullien
7874397373 Don't forget to mention the amazing job made by Herman on native macOS. 2023-01-17 15:29:57 +01:00
noneofyourbusiness
4cccba74d2
update changelog 2023-01-17 13:38:04 +01:00
herman ten brugge
d0efbc5d3a Fix preprocessor line directive for macos
Macos has an offset of 1 for DW_LNS_set_file.

Also rewrite dwarf_file to not scan list twice and
move check for <command line> to this function.
2023-01-16 20:26:44 +01:00
herman ten brugge
ee3fef2ce2 Fix preprocessor line directive
The preprocessor line directive did not result in correct debug info
when -g was used.
Line directives like:
 #line 1 "filename"
should now work.
2023-01-16 08:39:34 +01:00
herman ten brugge
79d439ee4f Allow const expr in builtin_frame_address/builtin_return_address 2023-01-16 08:37:47 +01:00
noneofyourbusiness
560526a49d
Changelog: get started with the changelog since 0.9.27 2023-01-01 16:39:14 +01:00
noneofyourbusiness
fa0fa62b0c
Makefile: remove unportable -v flag from distclean target 2023-01-01 16:30:57 +01:00
herman ten brugge
20a22cf3d6 Fix tcc -ar c symbol header size
tcc -ar t failed sometimes because the header size was not correct.
2022-12-28 07:38:39 +01:00
noneofyourbusiness
d15dec711d
tcc -ar: change mode to 644, use ARMAG 2022-12-28 03:08:02 +01:00
Per Nordlöw
9957c686a4 Remove _Static_assert verifying the size of CString because it breaks C99 builds with gcc-5 2022-12-28 00:31:14 +01:00
Per Nordlöw
78c3ea30a6 Reduce CString memory usage from 3 to two 2 words 2022-12-27 21:50:39 +01:00
herman ten brugge
7ed9c95ae7 Allow parallel build testsuite 2022-12-27 14:11:18 +01:00
herman ten brugge
d1fa89a11f Fix problem with large section size.
In tcc_load_object_file use unsigned long for size/offset.
This avoids strange sizes of sections in case of overflow.
The sections that are now larger then 4G may not work.
This avoids a hang on macos in realloc.
2022-12-27 07:45:38 +01:00
noneofyourbusiness
676755f6ee
help: also update main help with tcc -ar options and sort them 2022-12-26 01:48:54 +01:00
Christian Jullien
95e39517ef Add -Wl,-install_name macOS clang compatible flag as clang refuses -Wl,-soname. This helps configure scripts using both clang and tcc. 2022-12-24 09:30:00 +01:00
herman ten brugge
3fd6a05fff Fix multiple rpath tccmacho
If multiple rpaths are specified then output multiple LC_RPATH load
commands instead of one with : seperator.
2022-12-23 19:48:39 +01:00
herman ten brugge
03841b6f16 Update bind problem macho
Changed -1 into -2 for plt_offset,
2022-12-23 12:17:55 +01:00
herman ten brugge
da0cc61794 Fix bind problem macho
The problem occurs when a shared library creates a pointer to
a function and then the application also uses this function.
2022-12-22 20:50:37 +01:00
grischka
62d857a6f9 tccgen: better find_field() not found error messages
Also: return all of 'cumofs' (no extra '+' required at caller)
2022-12-22 16:17:39 +01:00
herman ten brugge
909d58dd5e Add tcc -ar x and t options
On macos we cannot use the ar because is does not support elf files
and then does not generate a symbol table.
The solution is to use 'tcc -ar'. The problem now is that some
pacages first built an archive with 'tcc -ar c' and later extracts
the archive with 'tcc -ar x' and built with this a dylib file.
The 'tcc -ar x' failed. So I implemented it. I also added the t
option because it was simple to do,
2022-12-20 07:34:41 +01:00
herman ten brugge
d5f25d19b3 Add macos clang/ld compiler options
With these option I can now built several packages without problem.
There are a lot of options we can add so I tried to implement the
minumum.

Add the following options:
dynamiclib		same as -shared
flat_namespace		default/ignored
two_levelnamespace	ignored
undefined		default/ignored
install_name		name of dynmic lib
compatibility_version	version dynamic lib
current_version	version version dynamic lib

-Wl options:
all_load		same as whole-archive
force_load		same as whole-archive with lib
single_module		ignored
2022-12-20 07:24:48 +01:00
herman ten brugge
f6f3d646fb Fix dll support for macho
Export now all global symbols in export trie instead of only text
symbols.

I wrote too much data into dylib file because I did not check
for sk_unknown/sk_discard/sk_uw_info.

Simplified code in tccrun.c
2022-12-18 14:07:19 +01:00
herman ten brugge
04810db83a Allow building lib with gcc/clang
Needed when using ...libtcc1-usegcc=yes in lib/Makefile.

lib/bcheck.c: Add __clang__ for pragma diagnostic
lib/bt-log.c: Add pragma diagnostic as in lib/bcheck.c
lib/builtin.c: Add a lot of alias code to allow building with gcc/clang
lib/stdatomic.c: Same as lib/builtin.c
lib/tcov.c: Avoid clang compiler warning
2022-12-17 09:37:37 +01:00
herman ten brugge
d66a62fa6b Fix dll support macho for x86_64
Macho uses bind/rebase instead of dynsym so disable them for relocate_sections.
2022-12-17 07:43:01 +01:00
herman ten brugge
8d3930bdb6 Add dll support for macho
Update configure and remove dll=no for Darwin.
Also removed dwarf=$dwarf because this is not a config option.

In tccelf.c only add __PAGEZERO if stabs and executable.

In tccrun.c correct rt_printline_dwarf code for dll.

In tests/Makefile disable dlltest with PIC for Darwin because object
format of .o file is still in elf.

In tccmacho.c add dll support and and rpath support.
Corrected trie code. For some reason symbol 'xx' should be after 'xx1'.
Corrected weak symbol support for old macho code.
Used R_JMP_SLOT instead of R_DATA_PTR in check_relocs.
2022-12-16 20:57:45 +01:00
Mathias Krause
9cfc8f60ce x86asm: Add lzcnt/tzcnt support
We already support popcnt, add lzcnt and tzcnt as well.

Signed-off-by: Mathias Krause <minipli@grsecurity.net>
2022-12-13 11:34:54 +01:00
herman ten brugge
079692016d Fix stab debug problem with common section 2022-12-11 15:36:37 +01:00
herman ten brugge
dd69143ae6 Add builtins ffs, clz, ctz, clrsb, popcount, parity
Add new file lib/builtin.c
Modify include/tccdefs.h, lib/Makefile to compile it.
Update tests/tcctest.c to test it.
2022-12-11 08:23:22 +01:00
Petr Skocik
ea0c57e90a Allow _Static_assert declarations in structs
This usage has been part of the C grammar since C11
(http://port70.net/~nsz/c/c11/n1570.html#6.7.2.1) and is
also supported by gcc and clang.
2022-12-10 01:12:44 +01:00
herman ten brugge
ac9eeea1d5 Enable vla_test-run on all targets
Fix gen_vla_sp_save/gen_vla_sp_restore in riscv64-gen.c
2022-12-06 11:21:50 +01:00
herman ten brugge
657262c7e5 Enable testcases on apple and x86_64 2022-12-06 08:09:16 +01:00
Christian Jullien
d0c26ba4e5 OSX is used and can't be replaced by macOS yet, revert previous change 2022-12-05 16:28:54 +01:00
Christian Jullien
d1f5d132c6 cosmetic: OSX is a deprecated name, macOS is the official system name for Apple Operating System since 2016. 2022-12-05 15:42:38 +01:00
Christian Jullien
a0f88dca75 on macOS, configure displays the dwarf version used, currently dwarf=4 2022-12-05 15:22:46 +01:00
herman ten brugge
6b9c0024ac Default is now config-new_macho and dwarf=4 for apple
I tested on macos 10, 11, 12 and 13 that old and new macho code work.
I do not know when this new format is introduced but it is now
available on a lot of versions. So make it the default.

Also default to dwarf=4 because stabs and dwarf=5 is not supported.
2022-12-05 11:10:03 +01:00
herman ten brugge
85b27432bb Fix libtcc_test_mt
The test failed because atexit/on_exit code was not correct.
Make it look more like the g_exit_context.
Added a sleep to libtcc_test_mt to allow overlap of threads to
detect this problem.
2022-12-04 11:55:06 +01:00
herman ten brugge
e2db3a5e57 Update load segment code for macho
Add an array with segement to load instead of code.
Dynamically check if segment is used or not.
This fixes the strange __DWARF code segment handling.
2022-12-04 07:27:10 +01:00
herman ten brugge
7cc1cc3881 Fix for lldb usage
lldb does not like function end and next function start at same pc
2022-12-04 07:19:48 +01:00
noneofyourbusiness
ab39d34dde
produce a more informative error message when _Thread_local is used 2022-12-03 20:09:11 +01:00
herman ten brugge
894e166ecf Better fix for old macho format
The last commit only worked on arm64.

The problem was that the new __DWARF segment was empty.
I now remove the __DWARF segment if it is not needed.
This resolves the rebase problem on x86_64.
2022-12-02 22:57:49 +01:00
herman ten brugge
14e78d7cc2 Old macho format does not work with previous push 2022-12-02 20:25:27 +01:00
herman ten brugge
b7356bb638 Reduce memory size macho executables
Do not align vmsize/filesize to SEG_PAGE_SIZE every time.
Move rel_offset check down due to this change.
2022-12-02 13:53:37 +01:00
herman ten brugge
6b398686d6 Fix bounds checking without -run on macos
Update tccrun.c to detect rebase for dwarf debug info.
Enabled testcase 126 on macos.
Add prologue_end/epilogue_begin supoort in tcc.h, tccdbg.c, tccgen.c.
2022-12-02 13:09:47 +01:00
Christian Jullien
ffbf6c26b2 Test was inverted for locally modified version 2022-12-02 08:40:12 +01:00
Christian Jullien
bdaccc2898 Minor -v improvement: remove extra space 2022-12-02 08:28:14 +01:00
Christian Jullien
7015838f51 Use my latest proposed patch for improved -v message. It is simpler and gives a better message, especially when repository has been locally modified: 'tcc version 0.9.27 mob:fef701b 2022-12-02T07:11:55+01:00 locally modified (ARM eabihf Linux)'. Hope it's Ok for maintainers 2022-12-02 08:05:01 +01:00
herman ten brugge
fef701b57f Allow on_exit to be used with -run
Added on_exit support for tcc -run.
Also fixed the last atexit code.
Updated testcase 128
Fixed test/test2/Mafile for testcase 126
2022-12-02 07:11:55 +01:00
herman ten brugge
60d52f0d68 Fix add debug support to macos 2022-12-02 07:05:12 +01:00
noneofyourbusiness
702b802316
TODO: add releases - it's been a while since the last release 2022-12-01 10:03:56 +01:00
noneofyourbusiness
4cb99787a7
make tcc -v more informative (also add commit date/time)
patch from 'Christian Jullien' <eligis@orange.fr>
2022-12-01 06:33:51 +01:00
herman ten brugge
62096265ed Add debug support to macos
lldb now works with this push.

In tccmacho.c add S_ATTR_DEBUG to all debug sections.
Add __DWARF section and rearange struct skinfo.

In tccdbg.c The first filename in dwarf_line can not be used.
Also had to fix structure/union/lexical_block/subroutine to
allow empty childs.
dwarfdump --verify complained about this.
2022-11-30 21:49:07 +01:00
herman ten brugge
ac0604a4d4 Allow atexit to be used with -run
Add atexit code to tccrun.c and added testcase 128
2022-11-29 21:13:20 +01:00
herman ten brugge
065b401c6e Fix static assert with empty string 2022-11-29 19:45:05 +01:00
herman ten brugge
a722a124f3 Allow %n in *printf functions on macos
The problem is that %n in *printf functions require a format string
that is in a read only section. See man page.

So added __DATA_CONST segment and mapped rodata_section into this segment.
Also added __got to this segment.

Change maxprot of segments to the same values used by clang.
2022-11-29 11:15:21 -06:00
herman ten brugge
afcdaf121a Fix __builtin_constant_p with comma expression
See: https://savannah.nongnu.org/bugs/?58606
and: mpfr-4.1.1/tests/tcmp_ui.c

The code '__builtin_constant_p ((i++, 7))' was not working.
I Fixed it in tccgen.c

I added a testcase in tests/tcctest.c. I also wanted to add a test like
'__builtin_constant_p ((10, 7))' but gcc needs -O1 for that to work.
clang (and now tcc) work as expected.
2022-11-29 00:56:26 -06:00
herman ten brugge
747ad409ac New update tccmacho.c
Fix for external functions as pointers in data section.
See testcase 119 and tccmacho.c in check_relocs.

Make bind/rebase code faster in bind_rebase_import.
- Move reloc check out of loop
- Because relocs are sorted we can do bind/rebase by page.

Change name head into seq in trie code.

Remove unused code in bind_rebase.
2022-11-28 11:19:33 -06:00
herman ten brugge
6b9fb93cd1 Fixed trie code generation for apple
I removed the FIXME for generating trie code with working code.

I also fixed a problem where:
cc -g -c a.c; cc -o a a.o
created a bind/rebase error because the second cc did not use -g
and .debug sections where not checked correctly.

I also removed the elfinterp printout in tcc.c when
TCC_TARGET_MACHO is set.
2022-11-27 07:28:40 -06:00
Christian Jullien
d8329c2d19 __nan() is not available on Apple M1, replaced by (0.0F/0.0F) 2022-11-25 17:08:58 +01:00
grischka
312d28b0a8 tccelf: avoid find_section() for known section
- remove calls to "find_..." for stuff that we know to exist and where.
- rename find_section_create(s1,name,0) -> have_section(s1,name)

Also:
- call update_gnu_hash() from elf_output_file()

gnu_hasn() functions could be moved down into an already existing
!ELF_OBJ_ONLY clause, but in order to avoid too many diff lines
I didn't.

- avoid 'long' (elf_hash). sizeof (long) is host-dependent (4 or 8)
- remove unnecessary checks (for dynsym, versym).

Someone reading "if (dynsym == NULL) ..." must conclude that it
actually can happen under certain circumstances, or otherwise,
might conclude that the person who wrote that felt unsure what's
going on exactly.

arm64-gen.c:
 TCC_TARGET_MACHO instead of __APPLE__ (to support cross-compilers
 for the apple/M1 target)
2022-11-25 12:13:47 +01:00
herman ten brugge
28fa4d3db6 Add strncat and strrchr to bounds checking 2022-11-25 00:31:32 -06:00
herman ten brugge
f48efeef8c Update to run on apple 13.0 (Ventura)
Add --config-codesign option to run codesign on apple to sign executables.
See configure and Makefile

In tccmacho.c use codesign option to call codesign application.
Add build_version/source_version
Sort sections in __LINKEDIT the same way as llvm does.
Add simple support for trie code. Need some more attention.
Fix rebase/bind error.
2022-11-25 00:19:45 -06:00
herman ten brugge
b86d82c8b3 Add new macho object format to tccmacho
Apple has a new object format that uses chained fixups.
I have implemented this with a configure option '--config-new_macho'
See configure and Makefile

tccmacho.c contains the biggest change.
I split the lazy_bind_rebase structure into lazy_bind and rebase for the
old format.
Under the macro CONFIG_NEW_MACHO there is the new macho format code.

In arm64-gen.c we do not need to push all registers on stack for
variadic functions. variadic parameters are pushed on stack for apple.

There is still one open isue. The export trie is empty.
This only effects dlsym when a local symbol is used.

tested on apple x86_64(10.5) and arm64(12.3).
2022-11-22 23:58:40 -06:00
herman ten brugge
d605d75ba8 Fix stab support for apple 2022-11-18 01:56:46 -06:00
herman ten brugge
d578151f8c Fix weak support apple 2022-11-18 01:51:36 -06:00
herman ten brugge
e99cf72784 Add weak support apple
Update tccmacho.c for weak symbols.
Undo tests/tcctest.c disable weak symbols test.
2022-11-17 14:03:00 -06:00
Christian Jullien
233d5d5f6f Fix old_style_f call when compiled by clang. 2022-11-17 14:35:31 +01:00
herman ten brugge
f89a6f12a7 Fix atomic test_and_set and clear
The __atomic_test_and_set and __atomic_clear code was correct.
They needed locking.
Replaced to code with assembly code.
The changes are in include/stdatomic.h, lib/stdatomic.c, lib/atomic.S
Enabled tests/tests2/124_atomic_counter.c for apple again.

Also moved lib/fetch_and_add.S code to lib/atomic.S.
Removed lib/fetch_and_add.S
Adjusted lib/Makefile
2022-11-17 05:57:21 -06:00
herman ten brugge
c8ef84c854 Add support for apple m1
The apple m1 uses position independent executables (pie).
I have implemented this in tccmacho.c

Apple also uses the stack different for var_args.
Also characters are signed instead of unsigned.
This is implemented in arm64-gen.c/tccdefs.h

Add bounds checking lib to lib/Makefile.

Add underscore support in lib/atomic.S and lib/fetch_and_add.S

Disable __clear_cache in lib/lib-arm64.c (Use system version).
I will try to fix this in future push.

Disable test_asm_call in tests/tcctest.c. Clang does not support @plt.
Also disable weak symbols test.
I will try to fix weak support in future push.

Disable tests/tests2/124_atomic_counter.c for 64BITS.
This is a bug in the atomic code and will be fixed in future push.

You have to use --dwarf configure option. stabs only works with -run.

tested on apple x86_64(10.5) and arm64(12.3).
2022-11-16 12:52:51 -06:00
Vlad Vissoultchev
c6b722f3f8
github: use macOS 11.7 and silence 96_nodata_wanted on Windows 2022-11-12 15:21:17 +02:00
Christian Jullien
8eabf67e1e Fix typo in tst_strtoll_strtoull. The wrong function was called for unsigned long long type. 2022-11-12 06:55:39 +01:00
Christian Jullien
6219b2de00 Add tests on Windows for strtoll and strtoull. Tests can probably be used on all systems. 2022-11-11 07:12:49 +01:00
Christian Jullien
40f01478d8 Add support for missing strtoll and strtoull on Windows 32/64 2022-11-11 06:23:26 +01:00
noneofyourbusiness
aea2b53123
update TODO: static linking sort of works 2022-10-30 13:05:26 +01:00
Bernhard Reutner-Fischer
df6fd04aaf github: commentary typo fix
and remove a trailing space

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2022-10-23 08:52:06 +02:00
noneofyourbusiness
7d76420857
produce a more informative error message when parsing _Complex 2022-10-19 14:06:04 +02:00
herman ten brugge
878fcccdb2 Small preprocessor fix
Set BOL flag for code like:
  #define tcc_test()
  #if 1
  tcc_test
  #endif
2022-10-17 07:49:47 +02:00
herman ten brugge
79a8229fb5 Add atomic functions for arm/arm64/riscv
Make code more compatible with gcc. Change
__atomic_store
__atomic_load
__atomic_exchange
__atomic_compare_exchange

Also add (include/stdatomic.h, lib/stdatomic.c):
atomic_thread_fence
atomic_signal_fence
atomic_is_lock_free

And gcc extensions (tcctok.h, tccgen.c, lib/stdatomic.c):
__atomic_fetch_nand
__atomic_and_fetch
__atomic_sub_fetch
__atomic_or_fetch
__atomic_xor_fetch
__atomic_and_fetch
__atomic_nand_fetch

Add new file lib/atomic.S with assembly code for __atomic_compare_exchange_n
for arm/arm64/riscv. Also update lib/Makefile.

Update testcode in 124_atomic_counter and 125_atomic_misc to test new functions.
Also update tests/tests2/Makefile to run tests on arm/arm64/riscv.
2022-10-16 18:51:56 +02:00
grischka
bb80cbe0d9 tcctest.c: sizeof (long) != 8
Bad assumption make bad things happen:

    long d3;
    asm(..."1:\tdec %3\n\t" : ... "=&c" (d3)

Which wants 'dec RDI' but did 'dec EDI' on _WIN64.

Also:
- tcctest.c: enable more asm tests for win64
- configure: show errors if any with 'gcc conftest.c'
- tccgen.c: remove decl0(x, y, z)
2022-10-14 21:56:16 +02:00
mingodad
c60f1d953c Update tests on Ubuntu from 18.04 to 20.04 because github is deprecating 18.04 2022-10-14 15:01:54 +02:00
mingodad
d27ca0e0c0 Add a github action to build/test tinycc on github mirror 2022-10-14 12:31:26 +02:00
grischka
d76e03232b win64: hi-mem adjustments
- x86_64-link.c:
  ignore relocation overflow to undefined (weak) symbols
- 104_inline.test:
  test lower 32 bits only
- tccpe.c:
  support -Wl,--image-base=... above the 32bit range
2022-10-13 20:32:47 +02:00
herman ten brugge
c03d59eae0 Fix macos warning for create_gnu_hash
Also add new function modify_reloctions_old_to_new for common code in
sort_syms and update_gnu_hash.
2022-10-12 07:24:34 +02:00
Detlef Riekenberg
b9aeb21f3f gcctestsuite: Make the script usable on more systems, skip some tests
The script was expecting gcc-3.2 in a specific directory,
works only, when building tcc in the source directory
and creates >3200 files in /tmp (probably a SSD).

Now gcctestsuite.sh works much better.
* use TESTSUITE_PATH, to tell the script the location, where the actual
  gcc.c-torture directory is
* work also, when tcc was build in a separate build directory.
  use TCC_SOURCE_PATH, when guessing the tcc source directory does not work
* use RUNTIME_DIR to redirect compiler output
  (default: XDG_RUNTIME_DIR, then /tmp)
* skip tests for features not implemented in tcc ( *_builtin_*, _Complex )

--
Regards, Detlef
2022-10-12 00:59:02 +02:00
grischka
aef64762c1 tccpp: #define max(a,b) && max /= 8;
This fixes 20a1ebf854
where the '/' above was lost when max() was defined

Also: ignore binary files with -E (for example 'tcc -lxxx -E ...')
2022-10-09 20:30:15 +02:00
herman ten brugge
0fd7376000 Add .gnu_hash support
Almost all systems use .gnu_hash so implement it for tcc.
For most of them it is the default.

tccelf.c:
- Add functions elf_gnu_hash/create_gnu_hash/update_gnu_hash
- Update new_section/sort_sections/fill_dynamic/tcc_output_elf/elf_output_file

Tested with setting .hash section to SHF_PRIVATE in elf_output_file.
2022-10-06 10:07:02 +02:00
grischka
e5eedc0cda Revert "tcc.h: Extend search path for include, lib and crt"
This reverts commit 2507c71704.

Why crowd up search-paths for the normal native compiler with
all sort of invalid stuff.  Why -UCONFIG_TRIPLET if it isn't
set at all for cross-compilers.  Also, the patch still didn't
do anything useful "out of the box".

Instead I'd suggest to do something more simple and more effective.
See Makefile:
- set search paths for cross-compilers to /usr/<triplet> by default
- Install any built "xxx-tcc" and "xxx-libtcc1.a", even if not listed
  with PROGS_CROSS/LIBTCC1_CROSS
- Support ELF-<target> = ... to set the elf interpreter

Some common gnu-triplets are supported by default
  TRIPLET-i386 ?= i386-linux-gnu
  TRIPLET-x86_64 ?= x86_64-linux-gnu
  TRIPLET-arm ?= arm-linux-gnueabihf
  TRIPLET-arm64 ?= aarch64-linux-gnu
  TRIPLET-riscv64 ?= riscv64-linux-gnu

Other triplets can be given explicitly, for example like this:
  $ make cross-arm-eabi TRIPLET-arm-eabi=arm-linux-gnueabi
  $ sudo make install
2022-09-24 10:09:12 +02:00
grischka
e41730f11a - tcc -vv: show cross-libtcc1.a correctly (and more)
(As long as it is in the default install location and was not
moved elsewhere into the library search path manually)

Also:
- libtcc.c:
  - error1(): show correct line with "In file included from ..."
  - support "tcc -Bxxx -vv"
  - tcc_new()/tcc_compile(): Don't create elf sections for tcc -E
- tccdbg.c:
  - tcc -E -g : revert 1de025c13a
    Let's keep things simple, everybody understands 'do_debug'
    and dState is set by tcov too (but no debug sections).
- tccgen.c:
  - avoid the extra parameter for gind()
    (from c3e3a07ed4)
  - vla func params: use skip_or_save_block() and enable
    VT_LVAL (see 313855c232)
  - cleanup nocode_wanted a bit
- tccelf.c:
  - tccelf_end_file(): don't try to translate zero-sym relocs
    (seems to happen with asm "jmp 0x1000")
  - version_add(): do not make "ld-linux.so" DT_NEEDED
2022-09-24 09:46:17 +02:00
Detlef Riekenberg
414c22c67b configure: Fix Android detection 2022-09-24 02:33:46 +02:00
Detlef Riekenberg
2507c71704 tcc.h: Extend search path for include, lib and crt
This allow the tcc cross compilers to work as expected,
when tcc was build with '--enable-cross' and a simple config-extra.mak
(see the provided config-extra.mak.example).
Make sure, that cross development packages for libc are installed

Fixes also open bugs in various bug tracker. Example in debian: 940469
  tcc: error: library 'c' not found
  tcc: error: file 'crtn.o' not found
  tcc: error: undefined symbol 'printf'
2022-09-24 01:05:02 +02:00
Michael Matz
fa25630ce4 vla: Fix check for vla used by autoconf
1) recursive types have no storage anymore, so a
     static int (*p)[x];
   declaration is just fine
2) since somewhen VT_VLA doesn't imply VT_ARRAY anymore, so we
   now need to check VLA in at least one place before checking
   test_lvalue.

((2) should probably be cleaned up again so that VLA does again imply
ARRAY)
2022-09-19 15:32:07 +02:00
herman ten brugge
76b88e22a4 Fix last __has_include update
After this last change the radare2 project did not compile anymore.

Start with BOL flag after include ends in tccpp.c.
Update testcase 18.
2022-09-12 11:37:47 +02:00
Niklas Rosencrantz
4410dbdd7f Use uname -s since uname -o is not compatible with macOS/Darwin 2022-09-02 05:12:44 +02:00
Detlef Riekenberg
c51236eee7 libtcc: Activate DWARF debug infos with tcc -gdwarf ...
This fixes the bug that "tcc -gdwarf ..."
still creates the stab debug infos,
when tcc was build without using DWARF debug infos by default
(configure option: --dwarf=x)

--
Regards ... Detlef
2022-09-01 15:55:56 +02:00
Brian Callahan
2acf65f7e6 Fix OpenBSD PIE binary linking 2022-08-21 12:48:06 -04:00
grischka
20a1ebf854 tccpp : get rid of 'ch'
- also simplify parse(_line)_comment() and parse_pp_string()
- fixes a continuation problem in strings (see tcctest.c)
- no differences in performance could be observed

161 insertions(+), 246 deletions(-), less 85 lines
2022-08-18 11:34:36 +02:00
grischka
85c32ddd0b tccpp: integrate __has_include() more nicely
- make only one function from previously four pieces
- use parse_pp_string() for the normal cases "..." and <...>
- use the sanity check for computed include as end-condition too.
- use 's1' instead of 'tcc_state' when possible

117 insertions(+), 165 deletions(-), less 48 lines
2022-08-18 10:23:13 +02:00
herman ten brugge
0f72db09ab Add testcase __has_include and __has_include_next 2022-08-16 20:53:57 +02:00
herman ten brugge
90b68cbd58 Fix arm64 compilation
arm64-gen.c:
- Remove VT_NONCONST in load

tests/tests2/Makefile:
- Fix typo test 127
2022-08-16 17:54:42 +02:00
Michael Matz
aa9093a144 Fix null pointer constants
an expression like 'i*0', even though it's value is constant and
can be evaluated at compile time is not an integer constant expression,
and hence no null pointer constant, and therefore the conditional
operator doesn't select the other type.
2022-08-16 16:58:00 +02:00
Michael Matz
c3e3a07ed4 dce: Don't force unreachable case label to be live
if a switch is unreachable then so are the case labels.
Unlike normal labels they can't possibly be reached from not-yet
parsed code, so there's no reason to enable codegen again for those.
2022-08-16 15:59:41 +02:00
Michael Matz
0c36b3ff2d x86asm: Add movnti
in the unsuffixed form we also accept REG8 and REG16, for easier
parsing (we have no instruction type for only the LQ forms).
2022-08-16 15:59:41 +02:00
Michael Matz
64f4b00d34 tccpp: Fix #ifdef in macro args
see testcase.  We have to "empty" the macro-stack string on
end_macro, as it may be 'tokstr_buf' which is going to be used
twice in recursive invocations of macro_subst_tok.  The uses aren't
overlapping but the first one needs to be properly finalized.
2022-08-16 15:59:41 +02:00
Michael Matz
1da92cdd93 Accept attributes after labels
newer GCC can associate attributes with statements and labels,
this implements only the latter (and ignores the attributes).
2022-08-16 15:59:41 +02:00
Michael Matz
b077bc8ff7 x86asm: Add vm* and iret[wlq] insns
The no-operand vm* instructions can be interpreted as having a
funny modrm byte, but unlike no-operand OPC_MODRM it's also the
r/m field which selects the insn, not (only) the reg field (aka group),
so we need another insn type.
2022-08-16 15:59:41 +02:00
Michael Matz
2309517066 x86asm: Add popcnt
as this is the first opcode TCC supports that has a 0xf3 prefix
and uses integer registers (not SSE ones) this also needs some shuffling
of the prefix code to not generate invalid instructions (the REX prefix
_must_ come directly before the main opcode (including 0f prefix), and
hence needs to come after the 0xf3 prefix).  Also disable some mnemonics
in asmtest.S that new GAS doesn't support anymore.  The only difference
to GAS (in asmtest.S) is now the 'lock negl' instruction which TCC
emits as 'lock; negl'.  That's fine.
2022-08-16 15:59:41 +02:00
Michael Matz
98bab41cba Support asm goto
this adds generic support for asm goto, but I've tested only
on x86.
2022-08-16 15:59:41 +02:00
Michael Matz
154c3e7450 Fix unnamed bitfield struct members
they look similar to labels 'foobar : 32' (when foobar is a typedef),
but cannot be handled as such in parse_btype, the context is important
(labels can only happen when called from within 'block', aka decl0 in
VT_LOCAL context).  This got broken back in 2019 (1b57560).
2022-08-16 15:59:41 +02:00
herman ten brugge
47dc6e6d7b Add __has_include and __has_include_next support
tcctok.h:
- Add __has_include_next

tccpp.c:
- New functions parse_include and get_include_file
- Changed expr_preprocess and preprocess
2022-08-14 12:17:31 +02:00
herman ten brugge
1de025c13a Check dState instead of do_debug in tccdbg.c
This fixes:
  tcc -g -E

and programs like:
  #pragma comment (option,"-g")
  int main(void) {return 0;}
2022-08-11 09:03:13 +02:00
herman ten brugge
b7733aeef1 Fix wchar_t strings with 'x', 'u' and 'U' 2022-08-11 08:37:00 +02:00
Detlef Riekenberg
8b906427e3 tccgen: cleanup _Alignas implementation
Remove the unneeded compare, introduced in the recent _Alignas update.
sorry for that

--
bye bye Detlef
2022-08-04 12:02:52 +02:00
Vincent Lefevre
ef19711ba5 Use && rather than & for a Boolean operation. 2022-07-29 12:43:44 +02:00
Detlef Riekenberg
746560dc45 Do not fail with _Alignas(0) and _Alignas(1), used by autotools
Used by configure scripts, generated by newer GNU autotools
Autotools now checks for C11 and C99 compatibbility

--
bye bye Detlef
2022-07-28 18:30:54 +02:00
Detlef Riekenberg
bc12ab02c0 Accept -Wl,Map=filename.map
The parameter is stored in tccstate, but nothing more.

Used by multiple software packages.
Current tested example is OpenWatcom-v2

--
bye bye ... Detlef
2022-07-28 17:33:51 +02:00
herman ten brugge
2647dc063d Change plt_name size from 100 to 200.
I have jni code with very large symbol names (150 chars max).
This change avoids having names classes.
2022-07-25 13:00:15 +02:00
herman ten brugge
4589fdfad4 Fix netbsd elf problem
The dynamic linker on netbsd can only handle 2 PT_LOAD sections.
2022-07-25 12:55:26 +02:00
grischka
09808f327f tcc android-enabled (armeabi-v7a)
On an armeabi-v7a device (phone) in the termux app with
clang & make installed this passes all the tests.

Can be used as a cross compiler to create "native apps" as well.
Example 'config-extra.mak' for the cross arm-eabi-tcc:

  SYSROOT = <path_to_android_ndk...>/sysroot/usr
  TRIPLET = arm-linux-androideabi
  ANDRVER = 32

  ROOT-arm-eabi = $(SYSROOT)

  CRT-arm-eabi = {R}/lib/$(TRIPLET)/$(ANDRVER)
  LIB-arm-eabi = {B};{R}/lib/$(TRIPLET)/$(ANDRVER);{R}/lib/$(TRIPLET)
  INC-arm-eabi = {B}/lib/include;{R}/include/$(TRIPLET);{R}/include

  DEF-arm-eabi = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI
  DEF-arm-eabi += -DTARGETOS_ANDROID -DCONFIG_TCC_PIE -DCONFIG_NEW_DTAGS
  DEF-arm-eabi += -DCONFIG_TCC_ELFINTERP=\"/system/bin/linker\"

  # on unix replace ';' by ':'.
  $ ./configure && make cross-arm-eabi && make install
2022-07-24 09:51:32 +02:00
grischka
8e860702e4 tccelf: sort_sections()
Sort sections in separate function to allow variable
calculatiion of needed program headers.  Such execinstr
and relro cah have their own PT_LOAD header.

315 insertions(+), 370 deletions(-)
2022-07-24 09:45:48 +02:00
grischka
ebaa5c81f4 dynamic executables (PIE)
Allows to create dynamic executables, using the code path
for TCC_OUTPUT_DLL but add an .interp header (plus a FLAGS_1
entry in the dynamic section to make 'readelf' say "PIE")

Introduces TCC_OUTPUT_DYN as alias for TCC_OUTPUT_DLL.
There is no runtime option,  only one to configure:

    ./configure --config-pie

100 insertions(+), 76 deletions(-)
2022-07-24 09:44:50 +02:00
grischka
e460f7dbb2 tccelf: load libtcc1.a from library path
... rather than from tccdir.  Also allows to load cross
versions of bcheck.o etc. (if we would build them which
we currently don't).

18 insertions(+), 24 deletions(-)
2022-07-24 09:44:34 +02:00
grischka
d653cf01f3 tccelf: load objects first
It seems to make sense to load any helper objects (such as
bcheck.o) first, and then load standard libraries.

29 insertions(+), 29 deletions(-)
2022-07-24 09:42:28 +02:00
grischka
7d6979d452 tccelf: avoid lookup of _GLOBAL_OFFSET_TABLE_
Instead, return its symbol index from build_got_entries()
Also, copy dynamic symbols later when size of _GLOBAL_OFFSET_TABLE_
was already set and handle -rdynamic there too.
See commit 4c82b00342

5 insertions(+), 5 deletions(-)
2022-07-24 09:42:04 +02:00
grischka
c81519e1c4 tccgen: don't generate elf-symbols under nocode/NODATA
seems that symbols under nocode/NODATA should not produce
elf symbols at all, not just elf symbols with no size.
See commit 4c82b00342

5 insertions(+), 5 deletions(-)
2022-07-24 09:41:57 +02:00
grischka
e8adc64203 libtcc: extend tcc_add_dllref()
libtcc.c:
- integrate find_dllref loop into tcc_add_dllref()
tccelf.c:
- make sure a DT_NEEDED tag is put for versions even
  when the dll was not previously loaded.
Related commit 4c82b00342

48 insertions(+), 77 deletions(-)
2022-07-24 09:30:41 +02:00
grischka
7d31904247 tccelf: fix version patch for defined symbols
previous patch seems to work only for bcheck.o and only because
it's loaded after libraries (which is maybe wrong too).
See commit 4c82b00342

section_ptr_add() already clears area to zeros

4 insertions(+), 11 deletions(-)
2022-07-24 09:30:41 +02:00
Arthur Williams
8482682061 Define __TCC_BACKTRACE_ENABLED__ if backtrace is enabled
If the `-btN` was specified, then the __TCC_BACKTRACE_ENABLED__
macro will be defined. This provides a way for user code
to tell if tcc_backtrace is defined or not.
2022-07-23 15:12:15 -07:00
grischka
af1abf1f45 Revert "Fix wrong handling of strings..." (almost)
See commit e588b65390.
Was not "wrong" really, just different. But appears to be outdated.
Now disabled by default (top of tccpp.c: ACCEPT_LF_IN_STRINGS)
Also, in skipped code, just warn.

Also: cleanup "Optimize small structure copying on x86_64"
(commit 3715f1d7ee)
- remove some copy&paste coding (tccgen.c)
- RSI/RDI need to be preserved on windows
- simply don't use under bcheck (this is tinycc)
2022-07-16 00:41:37 +02:00
Ziyao
e588b65390
Fix wrong handling of strings which do not end properly at the end of line 2022-07-13 15:43:26 +08:00
herman ten brugge
a83b285685 Add extra locking in bound checking code 2022-07-09 12:10:08 +02:00
herman ten brugge
2c70652e04 Enabled test 112_backtrace
Changed script in Makefile for testcase 112
Used bound_struct_copy_count in lib/bcheck.c
2022-07-09 11:51:13 +02:00
Ziyao
2d210fef49
Skip test 112_backtrace on x86_64,because commit 3715f1d breaks
it.Commit 3715f1d makes the error message of bound check on x86_64
different from other platforms,which breaks the test process.But that
commit exactly works well.
2022-07-09 12:56:07 +08:00
Ziyao
3715f1d7ee
Optimize small structure copying on x86_64 2022-07-09 12:53:29 +08:00
Detlef Riekenberg
fe7ee1105c libtcc: Let "-Wl,*" also accept "--entry=xxx" as alias for "--e=xxx" 2022-07-08 00:18:14 +02:00
Michael Matz
73c22f831f Fix fallthrough of non-entered stmt expressions
commit ec5d94291 made is to that the nocode_wanted state from
inside a statement expression is retained after it.  That is wrong
if the statement expression can't be entered to start with.  In the
latter case the state from before the stmt-expr is the one we need.
2022-07-01 17:20:37 +02:00
Michael Matz
ebb4e71236 flexarray: Accept empty ones
See testcase, it's a GNU extension, but widely accepted (the
linux kernel uses it).  This got broken with 72b520e709.
2022-07-01 15:37:23 +02:00
Reinhard Urban
9712541156 Add missing ELFOSABI constants
from my github rurban/brandelf
2022-06-13 15:10:15 +02:00
herman ten brugge
cd627c6c40 Fix riscv assembler
riscv64-asm.c:
	asm_opcode: add return
	asm_parse_regvar: add abi register names

riscv64-tok.h:
	add abi register names
2022-06-07 20:53:14 +02:00
herman ten brugge
afc136262e Fix core dump in case -static is used 2022-05-26 15:41:52 +02:00
herman ten brugge
a4e0f7ef05 Fix bitfield long types for stabs/dwarf
Also made dwarf *leb128 fuctions a litlle bit faster.
2022-05-25 17:04:37 +02:00
herman ten brugge
e301a0ba8d Removed __bound_exit_dll from lib/bt-dll.c 2022-05-19 09:42:41 +02:00
herman ten brugge
aaec564a82 Fix bound checking dlcose problem
The main problem is that an application called dlclose and then
had a bound checking problem. The list of dll's in tccrun was
not updated an caused a crash.
Also fixed some minor other things.

tccdbg.c:
- Allow filenames like ../file.c
- Rewrite DWARF_ABBREV_MEMBER_BF/DWARF_ABBREV_MEMBER a bit

tccelf.c:
- Add call to __bt_exit. This solves problem when dlclose is called

tccrun.c:
- Rewrite rt_printline_dwarf a litlle to use opcode_length correctly
- Do not stop at DW_LNE_end_sequence
- Fix DW_LNE_set_address again. Works now in *bsd.

lib/bt-exe.c lib/bt-dll.c:
- Add __bt_exit/__bound_exit_dll

lib/bcheck.c:
- Add __bound_exit_dll
2022-05-19 07:40:14 +02:00
herman ten brugge
4c82b00342 Update for elflint problems
tccgen.c:
- When __FUNCTION__ is used and no code is generated use symbol len = 0.

tccelf.c:
- If library is present in verneed it should be in DT_NEEDED.
- @plt symbols should have size 0
- set _GLOBAL_OFFSET_TABLE_ st_size correct
- Remove version symbol if new value present

reported by elflint:

__FUNCTION__ problem:
  section [19] '.symtab': symbol 2134 (L.195) does not fit completely in referenced section [14] '.data.ro'
DT_NEEDED problem:
  section [26] '.gnu.version_r': entry 2 references unknown dependency
@plt symbols should have size 0:
  section [22] '.symtab': symbol 36557 (r_core_config_init@plt) does not fit completely in referenced section [14] '.plt'
_GLOBAL_OFFSET_TABLE_ size:
  section [44] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol size 4 does not match .got section size 736
Remove version symbol:
  section [25] '.gnu.version': symbol 86: version index 3 is for requested version
  This happened for example with bounds checking symbol malloc
2022-05-17 07:34:10 +02:00
Detlef Riekenberg
a4f9e3cf4c configure: Use the environment variable CC when compiling tcc (resend)
Using the environment CC is a common used feature to
select a compiler to build any software,
so it should be supported when building tcc.

The old way using the parameter --cc still works.

(resend)

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-05-15 23:18:41 +02:00
herman ten brugge
0e47167cbe New dwarf update
tccdbg.c
- Fix typo in dwarf_file. This resulted in not finding correct include
  files.

tccrun.c:
- Change FILE_TABLE_SIZE to 512. Some files use very large includes.
- When a dll containted more then 1 file the pc was not calculated
  correctly.
2022-05-14 15:58:40 +02:00
herman ten brugge
53dd065daf Fix unicode compiler warning again 2022-05-14 15:55:27 +02:00
herman ten brugge
19cc80788d Fix unicode compiler warning
The compiler warned about the unicode_to_utf8 code.
I fixed it by giving an error. This is the same as gcc does.
2022-05-12 11:18:37 +02:00
herman ten brugge
e7396c99b0 Dwarf update
Some small updates in tccdbg.c found by checking readelf output from
gcc/clang.

- Change encoding type bool type
- Fix enum signed/unsigned
- Do not hash anon symbol
- Use correct filename/line_number for global variables
- Fix compiler warning in tcc_get_dwarf_info
- Use unsigned long long for array indexes
- Display correct first line of function in gdb
2022-05-12 08:15:28 +02:00
herman ten brugge
748e38ad99 Fix stupid typo. 2022-05-10 07:21:49 +02:00
herman ten brugge
9a81e6366b Fix vla type bug for stabs/dwarf
The wrong vla stabs code was accepted by gdb.
The dwarf vla code was fatal.
2022-05-10 07:16:38 +02:00
grischka
2caaff20fb tccdbg.c: new file
Better avoid global variables, at least in new code.

tccdbg.c hopefully should be logically identical to the
former parts in tccgen/elf.c (s1 tccstate added in some
places)

tccelf.c: dwarf linkage seems special per dwarf rather
than special per target.
2022-05-09 22:37:25 +02:00
grischka
56481d554f bored...
/* Use "-g" as alias for "-g1". Use "-g0" to disable debug */

So not using -g is now the alias for -g0 ?!?

This reverts commit 8759b2581d.
This reverts commit 3ce7bc6efc.
This reverts commit 5fb582ab7f.
This reverts commit aea68dbb40.
This reverts commit fa9c31c3db.
This reverts commit b3bebdb20a.
This reverts commit ecf8e5a00e.
This reverts commit fe6b5c08dc.
This reverts commit e2e5377e7b.
This reverts commit 1cd7998905.
2022-05-09 22:19:15 +02:00
Detlef Riekenberg
1cd7998905 tccmacho: avoid warnings with strncpy and always terminate the target string 2022-05-09 12:49:48 +02:00
herman ten brugge
0241120b54 Dwarf update for readelf
readelf complained about DW_AT_stmt_list and DW_AT_location.
For dwarf >= 4 we should use DW_FORM_sec_offset instead of DW_FORM_data4
and DW_FORM_exprloc instead of DW_FORM_block1.
This is fixed in tccgen.c

I also updated tccrun.c to use dwarf_read_1 instead of DW_GETC.
2022-05-09 08:04:37 +02:00
herman ten brugge
499cf2305b Add dwarf clang support
This if for clang (pre)release 15.0.0.

tccrun.c:
- update directory/filename read. clang has extra md5 section
- start with filename 0 instead of 1. clang starts at 0
- change dwarf_read_32/dwarf_read_64 into macros

x86_64-link.c:
- Add support for R_X86_64_DTPOFF64/R_X86_64_TPOFF64. Needed by clang

tests/tcctest.c:
- add prototypes for puts/alloca. clang fails with error
- disable other_constraints_test for clang. clang prints 1 instead of 0
2022-05-07 18:10:59 +02:00
herman ten brugge
18808e325f Update dwarf2 support
tccgen.c:
- add anon support. So tcc_state in tcc works now.
- add function pointer support
- remove DW_FORM_implicit_const from DW_TAG_pointer_type

tccrun.c:
- set initial file name
- correctly use pc in DW_LNE_set_address (see lib/bt-exe.c)
- add DW_LNE_define_file support (even if it is deprecated)

tccelf.c
- do not include debug/test_coverage information for stub functions

lib/bt-exe.c
- use num_callers=-1 to mark dll
2022-05-07 06:54:13 +02:00
herman ten brugge
f0df48fcdd Fix dwarf on 32 bits targets
Never do last minute commits :-(
2022-05-05 09:23:29 +02:00
herman ten brugge
2f2708a769 Add dwarf support
The new gcc12 release does not support stabs any more.
This was a good reason to add support for dwarf.

The stabs code still works and is used if configure option --dwarf
is not used.

Tested on x86_64, i386, arm, arm64, riscv64 with dwarf-5.
Some debuggers may not support dwarf-5. Try using older dwarf versions
i that case.
The tccmacho.c code probably need some support for dwarf.

arm-gen.c, arm64-gen.c, i386-gen.c, riscv64-gen.c, x86_64-gen.
- fix get_sym_ref symbol size

arm-link.c, arm64-link.c, i386-link.c, riscv64-link.c, x86_64-link.c
- add R_DATA_32U

libtcc.c:
- parse -gdwarf option

tcc.c:
- add dwarf option

tcc.h:
- add dwarf option and sections

tccelf.c:
- init dwarf sections
- avoid adding sh_addr for dwarf sections
- remove dwarf relocs for output dll
- add dwarf sections for tccrun

tccgen.c:
- add dwarf defines + global data
- add dwarf_* functions
- mix dwarf code with stabs code
- a trick is used to emit function name in .debug_line section so
  only this section has to be parsed instead of .debug_info and
  .debug_abbrev.
- fix init debug_modes

tccrun.c:
- add dwarf sections in rt_context
- init them in tcc_run
- add new dwarf code rt_printline_dwarf to find file/function

dwarf.h:
- New file

tcc-doc.texi:
- document dwarf

configure:
- add dwarf option

lib/Makefile
- change -gstabs into -gdwarf

lib/bt-exe.c, tests/tests2/Makefile, tests/tests2/126_bound_global:
- Add __bound_init call
- Add new testcase to test it
2022-05-05 09:10:37 +02:00
illiliti
d3e940c71c include/float.h: Define DECIMAL_DIG
Values were shamelessly stolen from musl libc. Needed for hare compiler.
2022-04-28 21:44:01 +03:00
Detlef Riekenberg
fa9c31c3db tcc.h: Extend search path for include, lib and crt.
This allows more tcc cross compiler to work out of the box
without a buildsystem-specific config-extra.mak
2022-04-28 17:36:10 +02:00
Detlef Riekenberg
9ac128c0bd Makefile: Add a comment with an option to debug the Makefile 2022-04-27 20:25:23 +02:00
Christian Jullien
5a3d1024d9 Update global help to tell that obj files are now optional with -ar driver 2022-04-19 07:53:19 +02:00
Ziyao
39ea340a31
Add option -e for setting ELF file's entry 2022-04-18 14:26:15 +08:00
Ziyao
0366924047
Add support of ldmxcsr and stmxcsr instructions on x86_64 and i386 2022-04-18 14:25:11 +08:00
Ziyao
8777ae984c
Allowing 'tcc -ar' to create empty archives 2022-04-18 14:23:03 +08:00
herman ten brugge
d3e4664629 Optimize prolog code on arm64
Only store registers that are needed in arm64 gfunc_prolog code.
Fix code in gen_bounds_epilog.
2022-04-15 20:11:18 +02:00
herman ten brugge
e86aae4f6d Add default case to relocate function for x86_64 2022-04-12 07:11:52 +02:00
herman ten brugge
c1725a8f6e Only define alloca on i386/x86_64 2022-04-12 07:07:40 +02:00
herman ten brugge
a5588501ab Fix vla support for arrays with no size 2022-04-12 07:03:33 +02:00
Detlef Riekenberg
aea68dbb40 arm-asm, arm64-link: Silence warnings for unused functions
The code needs to be fixed: use the functions or remove them.

arm-asm.c:
asm_parse_vfp_regvar()

arm64-link:
gotplt_entry_type()
create_plt_entry()

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-12 02:11:06 +02:00
Detlef Riekenberg
ac42d6826b tccgen: Avoid warnings in callers of the type_size() function.
Warnings reported in in x86_64-gen.c and arm-gen.c:

warning: ‘align’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-12 01:30:44 +02:00
Detlef Riekenberg
ecf8e5a00e tccelf: Avoid a compiler warning with snprintf
Target buffer must be larger as the format string + the replacement for %s
to avoid a warning [-Wformat-truncation=]

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-12 01:06:10 +02:00
Detlef Riekenberg
fe6b5c08dc configure: Use the environment variable CC when compiling tcc
Using the environment CC is a common used feature to
select a compiler to build any software,
so it should be supported when building tcc.

The old way using the parameter --cc still works.

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-12 00:33:35 +02:00
herman ten brugge
6bb41a05d7 Update for gcc12 on x86_64
gcc12 uses the xmm registers a lot more.

- save xmm0/xmm1 in gen_bounds_epilog
- align stack for call to (__bound_)memmove call in gfunc_call
2022-04-11 08:21:34 +02:00
Detlef Riekenberg
5fb582ab7f libtcc: Accept "-g0" to disable debug in addition to "-g", "-g1", "-g2" and "-g3"
This does not change our generated code.

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-10 18:26:14 +02:00
Detlef Riekenberg
3ce7bc6efc Accept option "-Os" and define "__OPTIMIZE_SIZE__"
Some headers and source code change macros and
implementation, when __OPTIMIZE_SIZE__ is defined.

This does not change our generated code.

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-10 17:32:38 +02:00
Detlef Riekenberg
b3bebdb20a libtcc: Allow more values for the -std= option
With this code, we can compile more projects,
who expect gcc or clang as compiler. (-std=gnu11 used in Makefiles)

As a further extension, it would be easy to disable gcc extensions

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-10 00:55:11 +02:00
Detlef Riekenberg
8759b2581d Makefile: Use the .exe extension only on Windows for the c2str helper program
Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-07 02:18:19 +02:00
Detlef Riekenberg
e2e5377e7b Makefile: Add a doc target.
"make help" already documents a "doc" target,
but it was not present.

Signed-off-by: Detlef Riekenberg <wine.dev@web.de>
2022-04-01 21:26:55 +02:00
herman ten brugge
4a03f1fb20 Fix bounds checking struct return on arm/arm64 2022-03-28 09:07:09 +02:00
herman ten brugge
0244320b88 Add vla support for arrays with no size
Support vla arrays like arr[][s]

- tcc.h: add nesting parameter
- tccgen.c: use nesting to test for illegal vla's
- test/tcctest.c: add test case
2022-03-24 10:16:37 +01:00
Christian Jullien
750f0a3e3f As suggested by Herman, move comment to the next line otherwise it incorrectly handles --config-bcheck=no --config-backtrace=no 2022-03-23 09:03:54 +01:00
herman ten brugge
313855c232 Fix multi dimensional vla arrays on stack
There is no code generation in post_type for vla arrays. This code
generation has to be delayed until gen_function.

- post_type save code in type info.
- gen_function use this code to generate vla code.
- enable testcode for vla arrays.
2022-03-21 11:40:43 +01:00
herman ten brugge
8a3d0a0557 Fix multidimensional arrays on stack 2022-03-18 07:26:07 +01:00
herman ten brugge
4efcada291 Fix when bound checking and test coverage is used at the same time 2022-03-17 18:06:02 +01:00
herman ten brugge
184d845838 Fix riscv64 test 90 after struct init commit 2022-03-17 17:16:24 +01:00
herman ten brugge
291eccc154 Vla fix for 64 bits targets 2022-03-17 10:06:24 +01:00
grischka
719d96665e tccgen: Allow struct init from struct
Example:
    struct S {int x,y;}
        a = {1, 2},
        b = {3, 4},
        c[] = {a, b}, // new
        d[] = {b, (struct S){5,6}}; // new
2022-03-16 19:18:16 +01:00
grischka
0c6adcbe53 tccgen: multi-dimensional vla: bug fixes
fixes these cases:
   ptr - ptr             (-> ptrdiff_t)
   ptr +/- num           (-> ptr)
   sizeof (ptr+/-num)    (-> size_t)

Also some cleanups
2022-03-16 19:18:16 +01:00
grischka
ec5d94291c tccgen: accept array-size expressions in function paramters
Modify function parameter parser such that symbols are
put into token-table temporarily.  Benefits are:
- detects redefinitions, as with
    int foo(int a, int a);
- detects reserved symbols, as with
    int foo(int if);
- can parse expressions like
    int main(int argc, char *argv[argc + 1]);
- doesn't fix this one
    int main(int argc, char *argv[++argc]);

Also: fix unexpected "function might return no value"
with statement expression
    int f() { ({ return 0; }); }
2022-03-16 19:16:29 +01:00
Steffen Nurpmeso
917aad3bcf Add some missing fdopen(3) checks (Domingo Alvarez Duarte) 2022-02-28 21:19:41 +01:00
herman ten brugge
308d8d17dc Fix get thread id in lib/bcheck.c for freebsd
The api for getting the thread id is different for freebsd.
2022-02-21 09:15:43 +01:00
Christian Jullien
7225282ea5 Small patch from collective work to better support FreeBSD. 2022-02-20 15:45:24 +01:00
herman ten brugge
e9f59c804d Update for freebsd 13.0
Add __RUNETYPE_INTERNAL define in include/tccdefs.h
2022-02-08 18:53:16 +01:00
Christian Jullien
4e0e9b8f21 Add '#define __LITTLE_ENDIAN__ 1' which was missing for macOS port 2022-01-11 07:06:09 +01:00
herman ten brugge
d33b189427 Fix vla bug
This fixes a vla bug. Probably more fixes are needed.
Add testcode for bug.
2021-12-27 11:39:52 +01:00
herman ten brugge
1692060fb0 Fix cross compiling tcc on freebsd 2021-12-27 11:32:35 +01:00
herman ten brugge
6c0e0b998b Fix memcpy1/memcpy2 asm testcode on x86_64 2021-12-27 11:24:33 +01:00
Riccardo Schirone
027b8fb9b8 Add rpath to the library paths instead of state->rpath 2021-12-15 17:45:16 +01:00
Riccardo Schirone
d88857b210 If the DLL has a RPATH, use it when looking for NEEDED libraries 2021-12-15 17:30:25 +01:00
Alexander Sosedkin
da11cf6515 Don't skip weak symbols during ar creation
```
$ echo 'int __attribute__((__weak__)) f(void) { return 4; }' > w.c
$ tcc -c w.c -o w.o

$ tcc-old -ar rc w.a w.o; nm -s w.a  # previous behaviour, not indexed
w.o:
0000000000000000 W f

$ ar rc w.a w.o; nm -s w.a           # GNU binutils behaviour, indexed
Archive index:
f in w.o

0000000000000000 W f

$ tcc-new rc w.a w.o; nm -s w.a      # new behaviour, indexed
Archive index:
f in w.o

0000000000000000 W f
```
2021-10-30 16:07:00 +02:00
mingodad
1645616843 Revert "Move almost all global variables to TCCState, actually all tests pass on Ubuntu 18.04 x86_64"
This reverts commit af686a796b.
2021-10-22 07:39:54 +02:00
mingodad
2ce2dbcb09 Revert "Fix some errors on arm64-asm.c, rename some variables, fix several code style declarations"
This reverts commit 61537d899a.
2021-10-22 07:39:26 +02:00
mingodad
d33f582e25 Revert "Fix missing parameter"
This reverts commit 1a8fb94350.
2021-10-22 07:37:39 +02:00
mingodad
1a8fb94350 Fix missing parameter 2021-10-22 07:37:15 +02:00
mingodad
61537d899a Fix some errors on arm64-asm.c, rename some variables, fix several code style declarations 2021-10-22 07:20:00 +02:00
mingodad
af686a796b Move almost all global variables to TCCState, actually all tests pass on Ubuntu 18.04 x86_64 2021-10-21 20:09:42 +02:00
Ryan Burns
ca11849ebb Permit '=' character in configure options
By replacing the `-f 2` field selection with `-f 2-`, we select fields
2 and beyond, so that arguments containing the '=' character are not
truncated.

This option qualifier list format is POSIX standard and tested
to work with the `cut` program from:
* GNU coreutils
* macOS (FreeBSD)
* NetBSD
* toybox
* busybox
* uutils-coreutils

In my case, this is useful because I'm trying to use an installation
prefix which contains an equals sign.
2021-10-09 17:28:42 -07:00
coltrane
15e9b7384e Add few Windows API that can be used to detect the exact Windows version we are running on. 2021-09-16 08:52:35 +02:00
grischka
e97e108d0b tccpe: #pragma pack(push) - support this form 2021-09-01 19:36:24 +02:00
Tyge Løvset
c7a57bf1fa Prepended branch name to githash (tcc -v). WIN32: added githash support.
- E.g. "tcc version 0.9.27 mob:675046b (x86_64 Windows)"
- WIN32: Replaced compiler opt -O1 and -Os with -O2 (only 1% larger executable).
2021-08-21 09:10:56 +02:00
grischka
675046bd59 tcc/lib: reduce number of files
271 insertions(+), 292 deletions(-), -21 lines, -5 files
Also:
- tccdefs.h: WIN32: less (no) __builtins
- libtcc.c: simply ignore -arch
2021-08-03 22:49:18 +02:00
herman ten brugge
e538160a32 Fix netbsd build
Netbsd uses __UINTPTR_TYPE__ to detect gcc in machine/int_types.h
2021-08-03 11:32:54 +02:00
Christian Jullien
e449db5f1d macOS: if used, -arch option must match target 2021-08-02 14:21:28 +02:00
Christian Jullien
a951dc026f macOS: add -arch option to allow to bootstrap tcc with tcc on macOS. 2021-08-02 09:22:46 +02:00
grischka
dda95e9b0b WAIT/POST_SEM(): generalize interface (and more)
Currently used only with 'tcc_compile_sem' to protect
tcc_compile(),  but can be used with other semaphores

Also fix deadlock when tcc_enter_state() is called
recursively for the same state, for example with
tcc_warning() from #pragma comment(option,"...")

Also:
- libtcc.c: error1(): use cstr_[v]printf()
- tcc.h: set TCC_USING_DOUBLE_FOR_LDOUBLE for macho-arm64
  (rather than for macho-X86_64)
- tcc.h: define TCC_TARGET_MACHO on __APPLE__ by default
- tcc.h: cleanup TCCState, move DEFASM token stuff to tcctok.h
- tccgen.c: more static
- Makefile/tcc.c: review githash
- tccpe/tcctools: use read() instead of fgets() in pe_load_def()
  (all files opened by tcc for reading are now read via 'int fd')
- configure/win32: don't preset CONFIG_TCCDIR (to allow to override it)
- tcc.c -bench: do not include output/run-time
2021-08-01 20:33:31 +02:00
grischka
4b2c6cf3a4 -W[no-]error...: features by shrinkage
136 insertions(+), 226 deletions(-), less 90 lines (except tests)
only one set_flag() function required
* can now turn off specific errors: -Werror -Wno-error=option
* new interface: tcc_warning_c(warn_option)("format", args...);
* new warning: -Wdiscarded-qualifiers (on by default)
* new variable 'warn_all' for conditional warnings with -Wall
see also the tests
2021-07-31 21:45:33 +02:00
Sam Ellicott
931a146591 tccdef.h additons for riscv64 newlib stdint 2021-07-29 22:36:53 -04:00
Steffen Nurpmeso
2709b7ab03 -W[no-]error: fix previous (sigh) 2021-07-27 23:19:36 +02:00
Steffen Nurpmeso
49cd6f59b2 -W[no-]error: rid of TCCState.warn_none, too; and adjust -h/-hh output 2021-07-27 20:55:58 +02:00
Steffen Nurpmeso
0d59ac4817 -W[no-]error: fix previous 2021-07-27 20:00:20 +02:00
Steffen Nurpmeso
0c16762418 -W[no-]error=X: gcc compat: when disabling X again, do not unset the warning 2021-07-27 19:48:29 +02:00
Christian Jullien
b1d9de6794 [macOS] fix format warning 2021-07-27 07:54:46 +02:00
Steffen Nurpmeso
a7a13896b3 Add -W[no-]error=OPTION specific abortions 2021-07-27 00:37:22 +02:00
grischka
eadcee6501 macos: yet another tbd adjustment
configure:
- re-enable apple M1-arm64
- however, with --cpu=x86_64,  set -arch in CFLAGS/LDFLAGS too
  (assume rosetta)

Makefile:
- re-enable osx cross-test (Please do not disable tests)

tcc.h
- set TCC_IS_NATIVE for TCC_TARGET_MACHO on __APPLE__
- apply TCC_USING_DOUBLE_FOR_LDOUBLE for x86_64-osx

libtcc.c:
- cleanup tcc_add_file_internal()
- new function char *tcc_load_text(int fd); and use it for tbd files
2021-07-25 20:55:05 +02:00
Sushant Pandurangi
ecb384ac0c Fix 'unused function' warning on non-macOS targets 2021-07-17 13:42:54 +02:00
Brian Callahan
46bf3f940c Add register definitions for OpenBSD/riscv64 2021-07-09 12:16:18 -04:00
Christian Jullien
ab13f1a25a On Big Sur M1, force a x86_64 native build and count on Rosetta to make job as arm64 is not yet supported. 2021-07-08 09:37:37 +02:00
herman ten brugge
6e76d894fe Fix macOS memcheck problems 2021-07-07 10:05:23 +02:00
Christian Jullien
91a956823e [macOS] Add VT_LDOUBLE special case for Rosetta to handle --cpu=x86_64. 2021-07-07 09:50:20 +02:00
Christian Jullien
592cf7fb25 Temporary remove osx cross build which is broken, at least on Windows. 2021-07-07 09:09:12 +02:00
Christian Jullien
b964fc6922 dlopen does not exist on Windows, recent macOS adds a dlopen call which is skipped on Windows. 2021-07-07 09:08:15 +02:00
Sushant Pandurangi
588d64c538 (merge) macOS11+ dynamic linker support 2021-07-06 22:54:28 +02:00
Sushant Pandurangi
cca4ece0a8 macOS: get active SDK path from xcode-select 2021-07-06 22:48:38 +02:00
Sushant Pandurangi
d5e4b258e1 Minor fixes for gnu90 compatibility 2021-07-06 15:59:12 +02:00
Tyge Løvset
c4a2c52411 Added __faststorefence() MSVC intrinsic (required e.g. by sqlite3). 2021-07-04 13:32:55 +02:00
Sushant Pandurangi
33fa3a4d41 macOS 11: tcc -run using dyld shared cache 2021-07-04 11:07:48 +02:00
Sushant Pandurangi
f6fb4d0cf1 macOS 11: link using dyld shared cache
Supports linking using .tbd definitions found in the default installed
command-line tools SDK or Xcode.app SDK. Only for creating executables
(not yet for `tcc -run`).
2021-07-04 11:06:55 +02:00
Christian Jullien
e3a0eb5089 Minor change, help message starts with lowercase as other help strings. 2021-07-02 07:01:13 +02:00
Arthur Williams
ee75521dd5 Support -M, -MM, and -MMD
Add support for some more options to generate dependency fragments.
-M is likes -MD but doesn't compile anything so the command won't fail
when just supplied a file without all the relevant libs.
-MM and -MMD are like their counterparts but skip system libs.

Note the behavior of -MD has changed to include system libs. Use -MMD
for the old behavior. This matches gcc's corresponding flags
2021-07-01 01:44:34 -05:00
Arthur Williams
64d29c942a Have '-MF -' write to stdout
If a dash ("-") is specified to -MF, write to stdout instead of a file
called "-"
2021-07-01 01:44:23 -05:00
Arthur Williams
24d35faed2 Use grep -q instead of grep --quiet to be more portable 2021-07-01 01:44:23 -05:00
Christian Jullien
39d586d7fc gcc compiles .h files as .c source code, tcc now does the same and no longer complains with "unrecognized file type". It is however adviced to use "-x c" option which exists for this purpose in gcc and was supported by tcc. 2021-06-28 13:07:29 +02:00
herman ten brugge
b5d4b908c4 Fix function call on arm64 and riscv
arm64-gen.c/riscv64-gen.c
- Copy code from x86_64-gen.c (fetch cpu flag before generating any code)

tests/tcctest.c:
- Add test code
2021-06-22 07:38:39 +02:00
herman ten brugge
0378168c13 Fix macro processing
The code:

printf("%d\n", CALL(CONST));

did not work because we did not check for TOK_PLCHLDR.
2021-05-04 11:22:11 +02:00
Tyge Løvset
3564c47e52 Relicensing TinyCC 2021-04-27 11:56:43 +02:00
Danny Milosavljevic
431a74a446 Relicensing TinyCC 2021-04-26 19:34:27 +02:00
Danny Milosavljevic
9b76a64f96 riscv64-asm: Implement asm_clobber 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
468f338e23 riscv64-asm: Optimize gen_le32 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
99189ea707 riscv64-asm: Implement asm_parse_regvar 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
b28bf50d2b riscv64-asm: Add beq, bne, blt, bge, bltu, bgeu 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
9c0760a4d4 riscv64-asm: Add lb, lh, lw, lbu, lhu, ld, lwu, sb, sh, sw, sd 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
1e37ec4917 riscv64-asm: Add add, addi, sub, addw, addd, addiw, addid, subw, subd, xor, xori, or, ori, and, andi, slt, slti, sltu, sltiu 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
8b1a89fbdf riscv64-asm: Add sll, slli, srl, srli, sra, srai, sllw, slld, slliw, sllid, srlw, srld, srliw, srlid, sraw, srad, sraiw, sraid 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
0b1cc489e1 riscv64-asm: Add lui, auipc 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
e5a898e510 riscv64-asm: Add rdcycle, rdcycleh, rdtime, rdtimeh, rdinstret, rdinstreth 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
4891ab71a0 riscv64-asm: Add fence, fence.i, scall, sbreak, ecall, ebreak, wfi 2021-04-23 20:23:45 +02:00
Danny Milosavljevic
7f3114ebba riscv64-asm: Remove asm_error 2021-04-23 20:23:45 +02:00
herman ten brugge
1432574b2b Fix make lib/stdatomic.c gcc compatible 2021-04-14 12:22:18 +02:00
herman ten brugge
f8e50d23f5 Make lib/stdatomic.c gcc compatible 2021-04-14 11:59:57 +02:00
herman ten brugge
0f0f701212 Fix va_arg fox x86_64 2021-04-13 09:23:13 +02:00
herman ten brugge
d64923c7b4 Fix testcase 125 fox x86_64 2021-04-13 09:13:25 +02:00
Christian Jullien
035ae7d735 Fix Makefile as suggested by Urs Janßen 2021-04-09 14:21:36 +02:00
grischka
48df89e10e stdatomics: tidy & cleanup
- remove any error messages that were just for debugging the
  templates really
- don't use c99 in tcc (array designators etc.)
- remove memory model type (cannot be an own type really)
  and move memory model defines from built-in to stdatomics.h
- apply normal casts to non-pointer atomic_function arguments
- tidy the library support
- add some tests for errors/warnings

Also:
- Makefile: move GIT_HASH stuff from CFLAGS to DEFINES and into
  main section (away from what is included by tests for example)
- tccelf.c/tccgen.c: avoid some warnings with newer GCC
2021-04-09 10:47:35 +02:00
Dmitry Selyutin
4bb3b3cec7 stdatomic: simple counter test 2021-04-05 20:57:05 +03:00
herman ten brugge
aa7727964b plt-reloc fix for macos 2021-04-02 12:51:38 +02:00
Christian Jullien
affd736f19 Add 'modified' before hash when tcc is built from a patched mob version. 2021-04-02 08:00:31 +02:00
Christian Jullien
f68b39b922 Surround hash code with double quotes 2021-03-31 23:15:47 +02:00
Christian Jullien
65d00b13d5 tcc -v displays short hash to ease detection of source version used to compile tcc 2021-03-31 22:24:29 +02:00
Dmitry Selyutin
47da8e450e stdatomic: ld/st/xchg/cmpxchg on simple types
Some complex types can still be small and simple enough to fit into
register. Other compilers allow some operations on these types, and it
seems to be quite a reasonable choice. From now on, we should be able
to compile the following artificial example:

    struct combo {
        uint16_t lo;
        uint16_t hi;
    };

    struct combo load(const _Atomic(struct combo) *atom)
    {
        return atomic_load(atom);
    }

    void store(_Atomic(struct combo) *atom, struct combo value)
    {
        atomic_store(atom, value);
    }

    struct combo xchg(_Atomic(struct combo) *atom, struct combo value)
    {
        return atomic_exchange(atom, value);
    }

    bool cmpxchg(_Atomic(struct combo) *atom,
            struct combo *cmp, struct combo xchg)
    {
        return atomic_compare_exchange_strong(atom, cmp, xchg);
    }

This might be useful for some corner cases, though it is quite likely
that many programmers will prefer operating on a single 32-bit value
instead of using the structure consisting of 16-bit pair.

Things will work as long as the overall structure size happens to be
the same as for any integer type we support in atomics.
2021-03-25 22:45:58 +03:00
Michael Wilder
82b0af7450 Fix empty initializer compile error on win32 2021-03-18 18:45:28 -04:00
Dmitry Selyutin
9ed3de91a2 stdatomic: i386/x86_64 support 2021-03-17 00:33:59 +03:00
Dmitry Selyutin
806b3f987e stdatomic: fix warnings 2021-03-17 00:29:57 +03:00
Dmitry Selyutin
1ff8679e79 stdatomic: refactor parser and generator 2021-03-09 12:24:08 +03:00
Christian Jullien
d0d0c8b688 [macOS]: arm64, define both __aarch64__ and __arm64__ which are tested by standard include files. WIP 2021-02-21 11:56:16 +01:00
Christian Jullien
310d49668e [macOS]: arm64, add OBJ-arm64-osx lib object list. 2021-02-21 11:32:59 +01:00
Christian Jullien
ac8af47d7f [macOS]: arm64, correctly find clang which is not a symblink from cc 2021-02-21 10:54:49 +01:00
Christian Jullien
e8bff295f9 [macOS]: arm64, very early stage of Big Sur M1 native port. 2021-02-21 08:38:39 +01:00
grischka
97800177c9 tccgen: gen_cast(): detect 'cast from void' error 2021-02-19 09:55:40 +01:00
grischka
72f1dea537 tccelf: use rodata_section, use more rodata in tcc itself
libtcc.c: add -Wwrite-strings to -Wall
tccgen.c: ro float-consts, string-consts, ro arrays if base type is
tccpe.c: merge IAT with rodata
tccrun.c: mprotect rodata accordingly. free section data after copy
x86_64.c: do not use got for static data.
tcc -bench: show data.rw/ro

Probably STB_LOCAL should never get to put_got_entry(), and currently
it doesn't seem to happen (See "Hack Alarm" there)

Other files: use more ro-data in tinycc
2021-02-18 14:43:34 +01:00
grischka
02795106e1 tccelf: use plt-reloc instead of relocplt 2021-02-18 14:42:59 +01:00
grischka
e4f151c4cd tccgen: fix "Allow declared arrays to be initialized..."
See 355897a920
2021-02-18 14:04:53 +01:00
CodeHz
a1d10c8bde
Fix unnecessary symbol collision 2021-02-18 20:41:47 +08:00
Michael Matz
5201312cb2 Fix segfault with nested flex array structs
when used with string initialization the size of nested struct flex
array member was tested too late for < 0.

GCC accepts but discards such initializers (the flex array member will
become zero sized).  TCC supports flex arrays members only in
top-level structs and gives an error in this case.
2021-02-16 00:01:28 +01:00
herman ten brugge
3658c29b43 Add config pie option
Allow configure --config-pie for netbsd arm
2021-02-15 19:14:48 +01:00
herman ten brugge
87dcba16eb Fix tcov align and lock 2021-02-15 19:10:13 +01:00
herman ten brugge
f5b8444739 Fix declared arrays without size 2021-02-15 19:05:26 +01:00
Dmitry Selyutin
dd5b546bf7 stdatomic: atomic_init routine 2021-02-14 22:22:27 +03:00
Arthur Williams
355897a920 Allow declared arrays to be initialized without an explicit size
When defining an array with non-explicit size, one would get
"incompatible types for redefinition of 'array' if the array was already
declared with a different size.
For example:

    extern int array[2];
    int array[] = {1};

would fail to compile with tcc. Instead the above is now equivalent to:
    int array[] = {1, 0};
2021-02-13 19:18:08 -08:00
Michael Matz
d7f2775af8 Fix endless recursion due to type scoping
this change fixes building of invalid types.  The inner scope
struct P is return type of the forward decl foobar.  The outer scope
foobar() call implicitely declares that function again, with int
return type; overall this leads to access within the sym free list,
effectively building up a type directly referring to itself, leading
to endless recursion later.  The testcase is:

void n(void)
{
    {
      struct P {
          int __val;
      };
      struct P foobar(); // 1
    }
  foobar();  // 2
}

I've not included it in tests2 for now, because tcc accepts this.
Ideally we would like to reject it (as 'int foobar();' is incompatible
with the earlier decl).  clang also accepts it, but only because it's
not handling (1) above as an implicit decl of foobar (it warns, and
with -pedantic also warns about the type incompatiblity).  GCC rejects
this.

Implementing that in tcc requires some surgery, as we need to differ
between these cases:

  {  struct P foo(int); // 1
     foo();        // no implicit decl, call to foo from 1
  }

and

  { { struct P foo(int); // 2 }
    foo();         // implicit decl, _incompatible_ with 2
  }
2021-02-13 03:29:43 +01:00
Danny Milosavljevic
24c94fff09
arm-asm: Add vcvt 2021-02-13 01:03:41 +01:00
Danny Milosavljevic
f1fb23a661
arm-asm: Update comment 2021-02-13 01:03:39 +01:00
Michael Matz
30814dfacf Don't use stale section data pointers
put_elf_reloca might reallocate the section into which we point,
so don't remember the pointer just the offset.
2021-02-13 00:37:12 +01:00
Michael Matz
468e59206b Clear vtop.sym if saving on stack
normally the sym slot is meaningful only with VT_SYM.  But we also
use it when mentioning a decl for inline asms with register vars,
conditional on being a VT_LOCAL entry.  So when generating VT_LOCAL we
need to reset .sym as it might contain stale entries from the cmp_op
fields.
2021-02-13 00:24:29 +01:00
Michael Matz
ce8814cdd6 Avoid array overflow
with fuzzed source code we might run into this with idx out of bounds.
We're going to error out on this later, but let's not access
out-of-bounds elements.
2021-02-12 23:46:21 +01:00
Michael Matz
c4ae326a1d Revert "Long double Constant problem"
This reverts commit 405aef9155.
It doesn't compile (init_putv has no 'f1') and isn't necessary with a
compiler doing proper value initialization.
2021-02-12 22:52:05 +01:00
ayush-varshney
405aef9155 Long double Constant problem 2021-02-11 08:49:58 +01:00
Michael Matz
fbef90a703 Fix a VLA problem
see testcase, reduced example of a situation reported by
Kyryl Melekhin in https://github.com/kyx0r/neatvi/ .
Problem is that setting up the VLA sp-save in a scope that isn't
entered at runtime leaves traces of it in outer scopes that then
try to restore the stack pointer from uninitialized slots.
2021-02-03 04:30:11 +01:00
Danny Milosavljevic
d6f2d58158
Relicensing TinyCC 2021-02-02 14:07:43 +01:00
Riccardo Schirone
ad16628c9e Make sure to escape paths in generated make dependencies
If spaces are not escaped when generating the make dependencies file,
then if one of the dependencies has a space it would be interpreted as
two separate targets by Make, instead of just one.
2021-01-30 00:51:15 +01:00
Danny Milosavljevic
2ac8568503
arm-asm: Support immediate values without "#" (Unified Assembly Language) 2021-01-28 16:32:31 +01:00
Dmitry Selyutin
458457590d stdatomic: fix atomic_init parameters 2021-01-28 00:13:53 +03:00
Dmitry Selyutin
9d862be1e7 stdatomic: fix is_memory_model check 2021-01-28 00:13:07 +03:00
herman ten brugge
1c255baad5 test coverage update
Add myself to RELICENSING file
    Use locking when writing tcov file
    Fixed sometimes last line of function not shown
    Merge tcc_tcov_add_file and tcc_add_tcov
    Allow absolute file names
    Count case labels with no code better
2021-01-27 13:27:10 +01:00
Dmitry Selyutin
65773a5300 stdatomic: c11 prefix; migrate to models macros 2021-01-27 13:33:32 +03:00
Dmitry Selyutin
5053fd03a7 stdatomic: memory models as macros 2021-01-27 13:26:48 +03:00
Dmitry Selyutin
02ea864ad2 stdatomic: deplusification; LLVM notice 2021-01-27 13:25:12 +03:00
Dmitry Selyutin
f2e7742aea stdatomic: stdatomic.h header 2021-01-27 00:48:46 +03:00
Dmitry Selyutin
719a6b3a16 stdatomic: emit function calls 2021-01-26 22:37:44 +03:00
Dmitry Selyutin
a110287c31 stdatomic: atomic builtins parsing support 2021-01-26 22:37:44 +03:00
grischka
557b4a1f6d configure chmod 755 etc.
lib/tcov.c:
- can't be cross-compiled (needs stdio.h)
- can be included in libtcc1.a

Reason why bt-xxx.o/bcheck.o are linked separatly is because we
don't want then to linked into exe's and dlls at the same time.
2021-01-26 18:44:37 +01:00
Danny Milosavljevic
25628cffe5
arm-asm: Add vmsr, vmrs 2021-01-26 14:25:39 +01:00
Danny Milosavljevic
1c9d999114 arm-asm: Implement "vmov.f32 Sn, Rd", "vmov.f32 Rd, Sn", "vmov.f64 Dm, Rd, Rn", "vmov.f64 Rd, Rn, Dm" 2021-01-26 03:31:33 +01:00
Danny Milosavljevic
90343eba3a arm-asm: Mostly factor out VFP register reference parsing to parse_operand 2021-01-26 03:24:09 +01:00
Danny Milosavljevic
0416594071 arm-asm: Add vmov 2021-01-25 21:56:52 +01:00
Danny Milosavljevic
b82e52a497 arm-asm: Add vmla, vmls, vnmls, vnmla, vmul, vnmul, vadd, vsub, vdiv, vneg, vabs, vsqrt, vcmp, vcmpe 2021-01-25 21:56:52 +01:00
Danny Milosavljevic
104037a4c5
arm-asm: Raise error if user tries to use a shift instruction with an immediate source operand 2021-01-25 00:54:01 +01:00
grischka
1ed4b6ba1a debug_modes, re-unalign, cleanups
tccgen.c: debug_modes
- don't waste debug function calls during normal execution.
libtcc.c:
- mem_debug: no C99 features in tcc please, for example
  ({compound expressions}): do not use.
tccgen.c: struct_layout:
- unaligned access is completely ok for most targets.
- Moreover the patch was triggering single byte mode even
  for normal aligned access (as with tcc's SymAttr)

static Sym label: don't do this

arm-gen.c:
- use some #ifdefs to explain some code
tccpp.c:
- cleanup UCN chars
libtcc.c:
- replace openbsd library search
configure:
- cleanup strip fallouts
tccgen.c:
- expr_cond(): remove an exotic optimization that eventually
  got fixed to do the contrary by a gv(RC_InT)
- pop_local_syms(): remove some args
- init_putv() : use write##le functions to avoid cross-compiler
  unaligned access
- __bt_init(): remove unused param 'mode'
2021-01-24 18:00:33 +01:00
herman ten brugge
5043268cb1 Fix unaligned access arm (openbsd)
libtcc.c:
Fix unaligned load/store from magic3

tccgen.c:
For packed struct allways use single byte code
If field does not start at at align check it
Align stack correctly with vla
2021-01-24 11:28:26 +01:00
herman ten brugge
38eaf9b3a7 Fix compiler warnings utf8 code 2021-01-23 19:21:33 +01:00
herman ten brugge
b40a88ea46 Use arm assembler in lib dir 2021-01-23 19:08:59 +01:00
herman ten brugge
bc6c0c34c1 implement test coverage
I have implemented the -ftest-coverage option. It works a bit different
from the gcc version. It output .tcov text file which looks almost the
same as a gcov file after a executable/so file is run.

Add lib/tcov.c file
Modify Makefiles to compile/install it
Add -ftest-coverage option in tcc.c/tcc.h/tcc-doc.texi
Add code to tccelf.c/tccgen.c/tccpe.c
Add gen_increment_tcov to tcc.h/*gen.c

unrelated changes:
Add sigemptyset in tccrun.c
Fix riscv64-gen.c tok_alloc label size
2021-01-23 18:17:38 +01:00
Danny Milosavljevic
66de1550ab
arm-asm: Add vpush, vpop, vldm, vldmia, vldmdb, vstm, vstmia, vstmdb 2021-01-23 14:57:33 +01:00
Danny Milosavljevic
e350058532
arm-asm: Add svc 2021-01-23 14:20:06 +01:00
Danny Milosavljevic
2e87eb18ab
arm-asm: Improve build with MSVC 2021-01-21 21:56:10 +01:00
Danny Milosavljevic
3fc55e15e8
arm-asm: Enable VFP when invoking GNU as for testing VFP instructions 2021-01-21 20:33:04 +01:00
Danny Milosavljevic
cdbb55396c
arm-asm: Add vldr, vstr
Also add s0...s31, d0...d15
2021-01-21 18:15:19 +01:00
Danny Milosavljevic
31dde11ad5
arm-asm: Add ldc2, ldc2l, stc2, stc2l 2021-01-21 16:42:31 +01:00
Danny Milosavljevic
7900a6bb61
arm-asm: Add ldc, ldcl, stc, stcl 2021-01-21 16:42:28 +01:00
Danny Milosavljevic
d1a6c4aefa
arm-asm: Add mcr, mrc 2021-01-21 16:42:23 +01:00
Danny Milosavljevic
a1dad7a9f7
arm-asm: Add cdp2
Also allow instructions without condition code in the first place
2021-01-21 16:42:20 +01:00
Danny Milosavljevic
036a7fe7d4
arm-asm: Add cdp
Also add p0...p15 (coprocessors), c0...c15 (coprocessor register aliases)
2021-01-21 16:42:16 +01:00
Christian Jullien
593bed9b52 Fix arm NetBSD cross compilation 2021-01-19 08:58:24 +01:00
Danny Milosavljevic
d60d2bb60e
arm-asm: Make "!" optional in asm_block_data_transfer_opcode 2021-01-18 18:58:08 +01:00
Petr Skocik
704c8163fd re-add accidentally deleted printf("\n"); to tests2/97*.c 2021-01-18 08:32:50 +01:00
Petr Skocik
ffb95c2e0c Better handling of UCNs in strings
As the standard requires, take 4 hex digits after the \u opener of a
Universal Character Name, or take 8 hex digits after \U, but reject
smaller counts and don't consume more (https://port70.net/~nsz/c/c11/n1570.html#6.4.3,
https://port70.net/~nsz/c/c99/n1256.html#6.4.3).

The unicode codepoint used to get truncated to 1 byte. Now it gets expanded into UTF-8,
matching gcc & clang behavior on Linux.

TODO: Universal character names should also be supported in identifiers,
as in, e.g., char \u010dau_sv\u011bte[]="čau_světe";
2021-01-18 00:49:24 +01:00
herman ten brugge
6b614c4deb OpenBSD: arm fix
Disable warning softfloat. OpenBSD works fine.
save/restore s0-s15 during memcpy call for structs
update configure script. Works now on raspberry pi/All BSD
Add eabi_mem.. functions in armeabi.c for OpenBSD
Fix fp register in tccrun.c for OpenBSD
2021-01-17 20:43:15 +01:00
Christian Jullien
debe8d013d Use $targetos instead of a second call to uname 2021-01-17 11:45:23 +01:00
Christian Jullien
a2987fef19 configure automatically detects correct arm configuration on NetBSD 2021-01-17 09:02:00 +00:00
Christian Jullien
53f7d8baf4 conftest correctly detects NetBSD and OpenBSD for TRIPLET_OS 2021-01-17 08:49:37 +01:00
Christian Jullien
f5f8326531 FreeBSD: arm port is fully supported and added to cross list 2021-01-16 16:25:22 +01:00
herman ten brugge
5aba20f270 BSD: arm support
Support OpenBSD/FreeBSD/NetBSD on asm.

move PAGESIZE to tcc.h and use _SC_PAGESIZE (netbsd/arm has 8192 pagesize)

arm:
- fix cmp instruction for qemu (raspberry pi works without patch?)
- increase start address/size
- use large plt size
- add return R_ARM_PREL31
- add R_ARM_TARGET1 to prepare_dynamic_rel
- add gcc_s to FreeBSD (unwind code)
- do not use __clear_cache on bsd (sometimes bad system call)
- do stack unwinding on bsd
- test/tcctest.c: use %lld %llu on bsd
2021-01-16 07:01:59 +01:00
Danny Milosavljevic
757eccd1d2
arm-asm: Raise error if user tries to use PC for offset register of single data transfer 2021-01-14 23:16:38 +01:00
Danny Milosavljevic
007839597f
arm-asm: Implement ldr and str with shifted register offset
Factor out asm_parse_optional_shift
2021-01-14 23:16:31 +01:00
Christian Jullien
1d7b233562 Remove strip logic form configure script, keep a deprecated warning message when --strip-binaries is still used. 2021-01-14 07:10:41 +01:00
herman ten brugge
13f6e8a866 OpenBSD: arm support typo (WIP) 2021-01-13 19:58:19 +01:00
herman ten brugge
96e3923239 OpenBSD: arm support update (WIP) 2021-01-13 19:41:04 +01:00
Christian Jullien
20dbfe99b8 --strip-binaries is deprecates, build never strips, use 'make install-strip' to install stripped binaries 2021-01-13 18:51:23 +01:00
herman ten brugge
23989cbcf3 OpenBSD: arm support (WIP) 2021-01-13 13:20:36 +01:00
Christian Jullien
d5c78ce655 Add more supported systems for cross test 2021-01-13 09:34:37 +01:00
Christian Jullien
c35f61b958 Fix ./configure strip test 2021-01-13 07:56:05 +01:00
Christian Jullien
d7d470b357 ./configure always stripped binaries even if --strip-binaries was unset 2021-01-13 07:27:46 +01:00
herman ten brugge
56f74f2eeb Add shstrtab name in tccelf.c
Last commit removed shstrtab which make gdb/readelf unhappy.
2021-01-12 21:41:41 +01:00
grischka
62c0c4c77a tccelf.c: factor out elf_output_obj()
The small common parts within elf_output_file() aren't
worth the many #ifdefs.  Also, set section sizes and
allocate section names in 2 separate functions.
2021-01-12 18:39:35 +01:00
herman ten brugge
c74c6ed61a Serveral updates
arm-gen.c:
- remove fr parameter from load_value

tccelf.c:
- update comment and remove check for sh_size

tests/boundtest.c:
- fix testcase 16/17
2021-01-12 18:06:23 +01:00
herman ten brugge
28646b559d fix cross compiling on openbsd 2021-01-12 09:59:52 +01:00
herman ten brugge
ecdee4a697 arm64: Fix loading from constant address 2021-01-12 08:46:05 +01:00
Christian Jullien
00d467d44c OpenBSD: use portable strtoll instead of strtonum to allow cross-compilation test 2021-01-12 08:12:40 +01:00
herman ten brugge
4d254312be Remove gaps between RELX sections update
Update file_offset as well
2021-01-12 07:40:45 +01:00
Christian Jullien
64d5db7635 *BSD: fix broken Makefile on all BSD systems. 2021-01-12 07:08:39 +01:00
herman ten brugge
c9bdfcedb4 Remove gaps between RELX sections
These gaps are a result of final_sections_reloc. Here some relocs are removed.
The gaps are then filled with 0 in tcc_output_elf. The 0 is intepreted as
R_...NONE reloc. This does work on most targets but on OpenBSD/arm64 this
is illegal.
2021-01-12 06:59:51 +01:00
herman ten brugge
cd91ea658a OpenBSD: x86 update 2021-01-11 19:26:10 +01:00
grischka
bbc7070c82 make test: run cross-test always
To see inconsistencies when they happen.
2021-01-11 15:17:26 +01:00
Christian Jullien
42220676d7 OpenBSD: x86 use few clang compatible C types 2021-01-11 12:19:09 +01:00
Christian Jullien
db6dd66047 OpenBSD: -arm- Add more R_ARM* constants in elf.h. 2021-01-11 07:21:57 +01:00
Christian Jullien
6f27eca785 OpenBSD: Add 2 new R_ARM constants to be handled by arm-link.c 2021-01-10 19:02:55 +01:00
Christian Jullien
1018ee8749 OpenBSD: aarch64 99% working, armv7 WIP. 2021-01-10 11:56:14 +01:00
Christian Jullien
ef7c4388e7 OpenBSD: elfinterp location looks the same for all architectures. i386 WIP 2021-01-10 07:23:03 +01:00
Christian Jullien
0e74965d7f make install always strips, only install-strip target should do 2021-01-10 05:57:52 +01:00
herman ten brugge
68744e3260 OpenBSD: Add static link support 2021-01-09 20:35:54 +01:00
Christian Jullien
a3524bd780 OpenBSD: Fix warning and other *BSD builds. 2021-01-09 17:36:13 +01:00
Christian Jullien
e6d15a5df1 OpenBSD: add a workaround to find the versioned shared lib as no .so symlink exist. 2021-01-09 15:38:26 +01:00
Christian Jullien
5f9d4ad144 OpenBSD: crtbeginT.o does not exist on thsi system as with other *BSD systems. 2021-01-09 14:14:05 +01:00
Danny Milosavljevic
aed4941e6b
arm-asm: Add ldrh, ldrsh, ldrsb, strh 2021-01-08 13:52:25 +01:00
herman ten brugge
f44332c891 update arm text relocations
Avoid absolute text relocations and use relative/got addressing.
This makes code work on netbsd where this is required. (No DT_TEXTREL)
2021-01-06 09:48:15 +01:00
Christian Jullien
cb788d72c7 NetBSD: Minor edit, keep tab style of previous line 2021-01-06 07:10:56 +01:00
Christian Jullien
760024bde9 NetBSD: Trying to fix reloc error 38 on arm - WIP 2021-01-06 07:02:19 +01:00
Danny Milosavljevic
f9c3b61884
arm-asm-testsuite.sh: Prefer current tcc as specified in $(TCC) 2021-01-06 02:18:14 +01:00
Danny Milosavljevic
f3912fafdd
arm-asm-testsuite.sh: Prefer cross-binutils' objdump 2021-01-06 02:14:49 +01:00
grischka
aeb8f427e2 tccgen: introduce TOK_NEG for unary minus
for floats (currently only).  On x86_64 uses built-in fp
constants (in libtcc1.c) to avoid multiple anonymous
instances.

Also: win32/i386: use __alloca for big struct stack store
- use new function int tok_alloc_const(const char*);
- change alloca86.S to preserve EDX

tccelf.c: fix a warning with 'roinf_use'
2021-01-06 01:44:22 +01:00
herman ten brugge
a5865fab22 text relocation for arm 2021-01-05 19:46:43 +01:00
herman ten brugge
81fec84012 disable nan test for clang 2021-01-05 19:44:06 +01:00
Danny Milosavljevic
77a6a9166d tests: Make arm-asm-testsuite.sh print the failed test cases. 2021-01-05 17:46:08 +00:00
Danny Milosavljevic
0c399306b8 tests: Call arm-asm-testsuite.sh on "asmtest" target. 2021-01-05 17:46:08 +00:00
CodeHz
425bd2a3db fix "invalid token at start of a preprocessor expression" 2021-01-05 17:46:08 +00:00
Christian Jullien
3873f95197 NetBSD: start arm support. WIP 2021-01-05 17:46:08 +00:00
Danny Milosavljevic
0754912ebd arm-asm: Add testsuite 2021-01-05 17:46:08 +00:00
Danny Milosavljevic
29f36a8953 arm-asm: Allow implicit offset 0 in input of asm_single_data_transfer_opcode 2021-01-05 17:46:08 +00:00
Danny Milosavljevic
483625065f arm-asm: Add ldrex, ldrexb, strex, strexb 2021-01-05 17:46:08 +00:00
Danny Milosavljevic
54ef167111 arm-asm: Implement branch to label 2021-01-05 17:46:08 +00:00
Michael Matz
29d8871d61 Implement proper floating point negation
Using "-0.0 - x" still isn't enough if x is a NaN, so bite the bullet
and implement sign-bit flipping via memory.  (It's usually not too bad,
the -0.0-x method also uses memory for the floating point constant).

This way is at least shorter than implementing a new top-level operation
for all backends (a negate) that would be unary.
2021-01-04 04:13:42 +01:00
herman ten brugge
4c9516941c Fix wine
After removing uint64_t from stddef.h the tcc_libm.h now needs to
include stdint.h
2021-01-03 20:12:34 +01:00
herman ten brugge
33d5a9fadb add arm64 relocs 2021-01-03 19:14:53 +01:00
Danny Milosavljevic
78d0f07e32
arm-asm: Improve immediate error message in asm_data_processing_opcode. 2021-01-03 15:54:19 +01:00
Danny Milosavljevic
a7205f738b
arm-asm: Support shift of 0 in asm_shift_opcode 2021-01-03 15:54:19 +01:00
Danny Milosavljevic
9539f51369
Makefile: Split arm64-asm from arm-asm 2021-01-03 15:49:17 +01:00
herman ten brugge
8712b4c9c1 Fix arm64 asm 2021-01-03 11:24:37 +01:00
Danny Milosavljevic
7f98aefddf
arm-asm: Implement subst_asm_operand 2021-01-03 02:34:12 +01:00
Danny Milosavljevic
9a460d3234
arm-asm: Implement asm_compute_constraints 2021-01-03 02:34:12 +01:00
Danny Milosavljevic
795d7d5ce6
arm-asm: Implement asm_gen_code 2021-01-03 02:34:12 +01:00
Danny Milosavljevic
d66c155239
arm-asm: Support bigger immediates for data processing instructions 2021-01-03 02:34:12 +01:00
Danny Milosavljevic
14b7973ab5
arm-asm: Add movw 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
c882d03673
arm-asm: Add movt 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
86cc9c587b
arm-asm: Raise error if asm_data_processing_opcode and asm_shift_opcode try to use PC for register-controlled shifts 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
79567004b4
arm-asm: Raise error if more than two operands are specified on mov, mvn, cmp, cmn, tst, teq 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
612d9d7ae6
arm-asm: Print a warning if asm_binary_opcode is used with SP as operand 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
67b402fda4
arm-asm: Raise an error if asm_binary_opcode is used with PC as operand 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
3a6f3e5f30
arm-asm: Add error case in asm_multiplication_opcode 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
e0cb5184f5
arm-asm: Warn if regset registers are not specified in ascending order 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
49365d563e
arm-asm: Support rotation for sxtb, sxth, uxtb, uxth 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
daaa88ce68
arm-asm: For data processing instructions, support shifts and rotations. 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
abef8f6ca7
arm-asm: Add lsl, lsr, asr, ror, rrx 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
b940d8edb2
arm-asm: Optimize gen_le32 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
b85c3e1595
arm-asm: Add b, bl, bx, blx 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
3b1f06c3b2
arm-asm: Add and, eor, sub, rsb, add, adc, sbc, rsc, tst, teq, cmp, cmn, orr, mov, bic, mvn 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
c7682dd9aa
arm-asm: Add ldr, ldrb, str, strb 2021-01-03 02:34:11 +01:00
Danny Milosavljevic
632b213756
arm-asm: Add stmda, ldmda, stm, ldm, stmia, ldmia, stmdb, ldmdb, stmib, ldmib 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
82663d33bb
arm-asm: Add mul, mla, smull, umull, smlal, umlal 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
c4e13c1ef9
arm-asm: Add clz, sxtb, sxth, uxtb, uxth 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
c9e0c2a543
arm-asm: Add swi 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
b495959e4b
arm-asm: Add push, pop
Also edited tcctok.h to not redefine push, pop
2021-01-03 02:34:10 +01:00
Danny Milosavljevic
c5428cd19c
arm-asm: Add parse_operand, Operand
tccpp: Allow '#' token to reach the assembler.
2021-01-03 02:34:10 +01:00
Danny Milosavljevic
a16678e9f3
arm-asm: Add wfe, wfi 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
aaf052391d
arm-asm: Add nop 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
9d302620c2
arm-asm: Remove asm_error 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
b10824dcdc
arm-asm: Update copyright header 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
f88ded6c2d
arm-asm: Implement asm_parse_regvar and asm_clobber 2021-01-03 02:34:10 +01:00
Danny Milosavljevic
4a6fb47b8d
arm-asm: Publish g, gen_le16, gen_le32 in tcc.h 2021-01-03 02:34:10 +01:00
herman ten brugge
3221cc4a5a Align next section after gnu_ro to pagesize
To allow remapping the ro section the next section must start
on a new page.
2021-01-02 15:17:25 +01:00
Christian Jullien
b147a37c23 Remove useless [u]intN_t definitions from stddef.h 2021-01-02 11:23:26 +01:00
Christian Jullien
c9fe8fe470 Yet another int64_t/uint64_t definition fix. For wine this time. 2021-01-02 05:58:46 +01:00
herman ten brugge
2633c30fb4 riscv64 update
implement load/store to constant address
use VT_LLONG instead of VT_PTR in register passing
fix bound checking problem with small structs
enable riscv tests in tests/tcctest.c and tests/tests2/119_random_stuff.c
2021-01-01 20:36:57 +01:00
Christian Jullien
9a2a05ba1e *BSD: fix extra #endif 2021-01-01 16:01:03 +01:00
Christian Jullien
33257c6439 *BSD: protect against incompatible redefinitions of int64_t/uint64_t. 2021-01-01 08:50:13 +01:00
herman ten brugge
8db839cc85 FreeBSD32 struct return 2020-12-31 06:48:14 +01:00
herman ten brugge
9d49883895 update clang warning 2020-12-31 06:39:56 +01:00
grischka
ea82d0826d tccpp: cleanup target-os defines
moved target_machine defines to the <target>-gen.c files.

Also:
- c2str.c moved into conftest.c
- tccdefs.h ; defined(__TINYC__) && !defined(_LOCORE) removed
  (in tinycc __TINYC__ is always defined and _LO... is never.)
- stddef.h : too many #ifdefs, removed
- tccgen.c:stabs: support win32 long doubles aka doubles.
- win32: math.h/tcc_libm.h: fix pointer mismatch in modfl
- tccpp.c: increment include_stack_ptr after the file was
  actually found otherwise it would print
  "in file included from <itself>: file not found..."
2020-12-31 02:03:31 +01:00
Michael Matz
d784b28877 No need for sm_table.size entry
the sh_size input member is reliable, we don't need to duplicate
it in the section mapping info.
2020-12-31 01:21:52 +01:00
Michael Matz
b117088a91 Explain some reloc hackery
just changes a comment; it's not specific to __dso_handle, but general
to all hidden defined symbols for shared libraries.
2020-12-31 00:54:00 +01:00
Michael Matz
60eac659d4 fix cross compiler compilation
{l,}bounds_section is only conditionally defined.
2020-12-31 00:16:23 +01:00
herman ten brugge
cf10c1db66 Fix clang warnings 2020-12-30 19:12:05 +01:00
herman ten brugge
85b6efbf0c Real fix include SHT_NOTE sections everywhere
Thinko in last commit
2020-12-30 18:23:43 +01:00
herman ten brugge
766bd82032 Fix include SHT_NOTE sections everywhere 2020-12-30 16:24:15 +01:00
herman ten brugge
0821940e26 text relocation for netbsd
netbsd does not allow text relocations in text segment.

tcc.h:
- Add data_ro_section
- Fix typo rela.plt

tccelf.c:
- Add data_ro_section
- Make bounds_section/lbounds_section rw
- Add GNU_RELRO section for data_ro_section/bounds_section/lbounds_section
- Fix relocation for __dso_handle in atexit()

tccgen.c:
- Use data_ro_section

x86_64-gen.c:
- Use R_X86_64_PC32 instead of R_X86_64_64 for bounds checking

tests/Makefile, tests/tests2/Makefile
- Enable dll tests for netbsd
2020-12-30 14:08:06 +01:00
Christian Jullien
c13c434383 Fix warnings detected by clang when compiling c2str. 2020-12-30 08:57:30 +01:00
Michael Matz
cc40305a12 Fix errors with parallel make
seen when doing 'make -j i386-tcc tcc', as explained in the comment.
2020-12-25 02:10:43 +01:00
Christian Jullien
16ed67537c NetBSD: longjmp(jmp_buf, 0) is not supported. 2020-12-24 09:29:25 +01:00
Christian Jullien
b9db7c90ee NetBSD: arm64 prevent use of __asm. 2020-12-24 07:31:52 +01:00
Michael Matz
1032e7175a ELF: include SHT_NOTE sections everywhere
... not just on the BSDs.  Sometimes e.g. .note.ABI-tag is allocated
and other sections might contain relocations referring to symbols in
them.  TCC doesn't do any special processing to them, like merging
or somesuch, it just pastes them all together in normal link-editing
behaviour.

(Seen on a recent openSUSE with glibc 2.32, when the crt1.o file
contains debug information)
2020-12-24 07:01:04 +01:00
Christian Jullien
b56099aeb4 NetBSD: arm64 fix int64_t/uint64_t definitions. 2020-12-24 06:50:48 +01:00
Christian Jullien
63e29113ab NetBSD: arm64 requires at least gcc 4.1 to work 2020-12-23 20:52:25 +01:00
Christian Jullien
ba61c7bb37 NetBSD: arm64 is curiously detected as evbarm. Struct uc_mcontext is once again different. 2020-12-23 20:08:26 +01:00
herman ten brugge
f7d2d04d96 Fix include/tccdefs.h
Remove __amd64__ for FreeBD (32 bits version did not work any more)
Add __APPLE__ at HAVE MALLOC_REDIR so bound checking works on apple
2020-12-22 18:53:29 +01:00
herman ten brugge
0352c41a07 add R_ARM_GOT_PREL for clang 2020-12-22 14:31:09 +01:00
grischka
9f6b65230a include/tccdefs.h: moved and use it
tcc_define_symbol(): now only for -D on command line
include/tccdefs.h: converted to strings and compiled into
the tcc executable.  Can be disabled with
    ./configure --config-predefs=no
In this case include/tccdefs.h is loaded at runtime.  This is
default for other build-methods (build-tcc.bat) also (to avoid
some complexity).

Also:
- lib/Makefile: fix typo
- tcc.h : avoid _strto(u)i64 (for TCC on WIN98/2K)
- tccpp.h:cstr_printf() : workaround incompatible vsnprintf's
  (generally faster too)
2020-12-22 11:06:19 +01:00
herman ten brugge
8f8abcc756 more bsd updates
This implements support for FreeBSD on aarch64
This partial implements support on FreeBSD(32). This still needs fixing
i386_gen.c because small structures on this target are passed in registers.

Add aligned 16 to __int128_t for FreeBSD
Support __i386__ on FreeBSD/NetBSD
Fix testcase 115 on FreeBSD/NetBSD
Disable testcase 116 on *BSD* because TLS_FUNC/TLS_VAR not set in bcheck.c
Remove FreeBSD/FreeBSD_kernel code from tccelf.c
2020-12-22 07:02:47 +01:00
Christian Jullien
888f4fd982 FreeBSD (aarch64): fix relocation error. 2020-12-22 05:52:45 +01:00
Christian Jullien
6cc87629b4 NetBSD: define __amd64__ which is sometimes used in headers. 2020-12-20 14:19:55 +01:00
Christian Jullien
a493fbb1fa *BSD: mention all BSD ports in README. 2020-12-20 10:50:07 +01:00
Christian Jullien
d4a4ee798b FreeBSD: Fix multiple ctype inline definitions 2020-12-20 09:45:16 +01:00
Christian Jullien
ac470a49d5 *BSD: Add few __builtin_ math missing definitions. 2020-12-20 07:26:04 +01:00
herman ten brugge
7eea5306e9 rename testcase 106 2020-12-19 21:02:44 +01:00
herman ten brugge
7f898abb82 bsd update
Fix crtbegin/crtend
Use dlsym on all bsd targets
Check .eh_frame on all bsd targets
Disable test3 on FreeBSD and NetBSD and use test1 instead because dlsym not working (WIP)
Disable dlltest and 113_btdll on NetBSD because text relocations are not allowed
Disable 115_bound_setjmp on NetBSD because longjmp is renamed into __longjmp14
2020-12-19 20:55:52 +01:00
herman ten brugge
a87dee588b dlltest_arm
This fixes the dlltest_arm on arm(32)
2020-12-19 20:33:36 +01:00
herman ten brugge
50b4f320dc lazy binding
Currently tcc does not use lazy binding. It puts all relocations in the RELX
section and solve them all at startup.
This was not working on bsd.

tcc.h:
- New RELPLT_SECTION_FMT for plt relocations
- New entry relocplt in struct Section

tccelf.c:
- put_elf_reloca: put R_JMP_SLOT in relocplt section
- build_got_entries*: Use two passes because R_JMP_SLOT and R_GLOB_DAT
                      can not be intermixed on some targets (arm, arm64)
- layout_sections: Calculate correct size relocplt section for DT_ values.
                   Make sure relocplt is last
- fill_dynamic: Add DT_ values when got is filled
                move DT_VERSYM because dynamic linker cannot handle it standone
- Add note section for NetBSD

arm-link.c/arm64-link.c/i386-link.c/riscv64-link.c/x86_64-link.c:
- fill got table with pointer to plt section or symbol value in case
  of TCC_OUTPUT_MEMORY

arm-link.c/arm64-link.c:
- fix offset first plt entry

i386-link.c/x86_64-link.c:
- use correct reloc entry
- use relofs - sizeof (ElfW_Rel) because the reloc is already done

lib/bcheck.c:
- no __libc_freeres on FreeBSD and NetBSD

tests/Makefile:
- Add -fno-stack-protector for OpenBSD

tests/tests2/Makefile:
- disable 106_pthread/114_bound_signal
2020-12-18 15:24:32 +01:00
grischka
e2e62fcb8b replace native platform macros in the compiler
- The compiler should not use these
- However tccrun.c & libtcc1.a files should use these
Also:
- use s1->loaded_dlls for loaded dlls instead of dlopens
- alpine musl: fully supported now and tested
- ./configure ...
   --config-backtrace=no : disable backtraces
   --config-bcheck=no : disable bcheck
- tests:dlltest: enable by default
- tccrun.c : simplify mmaps
- __builtin_alloca : always use asm-alias (instead of #define)
- tccpe.c : use write32le
2020-12-17 12:39:16 +01:00
Christian Jullien
02f61d5b49 riscv64: mention this supported cpu in README. 2020-12-17 09:10:00 +01:00
Christian Jullien
d5519c929c OpenBSD: mention this port on README 2020-12-17 06:22:10 +01:00
herman ten brugge
6eef0e35b4 OpenBSD: testcases
Fix all testcases for openbsd except dlltest.

Remember dlopen calls and use them to fix openbsd dlsym problem
Use crtbeginS.o/crtendS.o for DLL for bsd
Do not include -ldl for __NetBSD__
Redirect malloc, realloc, ..., free for bsd
Align stack in tests/asm-c-connect-*.c for x86_64
Remove -B in tests/pp/Makefile (not supported on bsd)
2020-12-16 17:52:59 +01:00
Christian Jullien
f48a9ed001 FreeBSD (on arm64): partial support, only -run works - WIP 2020-12-13 11:08:44 +01:00
Christian Jullien
eab324a7cf NetBSD (on arm64): reorder cpu detection to detect aarch64 target when cpu is arm64 2020-12-13 10:22:15 +01:00
Christian Jullien
c32cfc1113 NetBSD: predefine __ELF__ macro which is tested by some standard headers 2020-12-13 07:24:01 +01:00
Christian Jullien
f233cb182c NetBSD: reintroduce NetBSD support. Currently, -run works but executable has an incorrect format. 2020-12-11 14:26:26 +01:00
grischka
8ff705554d tcc_enter/exit_state(): do not use!
tcc_enter/exit_state() are meant exclusively to protect
the tcc_compile() and its sub-functions in tccpp.c,
tccgen.c, tccasm.c and xxx-gen.c.

Other files that are part of libtcc simply must not use global
variables.

- riscv64/last_hi: move to TCCState
  from 72250bece2

- tccrun.c: Using a fixed address would not work anyway
  ("tcc -run tcc.c -run ..." for example)
  from baacb0f52a

- tests/Makefile: support for a platform doesn't make sense if
  it doesn't pass our basic tests.
  from 591feda103
Also:
- tccgen: cleanup "duplicate member" (only 2 passes,
  avoids additional TokenSym field)
  from 170be79a42
2020-12-08 19:57:57 +01:00
Christian Jullien
a06fef3b11 FreeBSD: little better support that targets FreeBSD 12.2 on amd64 but still not working - WIP 2020-12-08 09:13:29 +01:00
Christian Jullien
f9c580b8a0 FreeBSD: start to reintroduce support - WIP 2020-12-08 06:55:50 +01:00
herman ten brugge
591feda103 OpenBSD: disable some tests 2020-12-07 12:06:17 +01:00
herman ten brugge
72250bece2 riscv64 relocation fix 2020-12-07 11:36:54 +01:00
herman ten brugge
baacb0f52a OpenBSD: runtime fixes
After this commit we can compile and run code with some limitations.
- The dlsym function is broken so this makes -run and bound checking
  not work all the time. Make -k test does work for most code.
- You have to do:
    ln -s /usr/lib/libN.so.x.y /usr/lib/libN.so
  for all .so files in /usr/lib.
  OpenBSD uses opendir/readdir to find the correct so file. This is
  not the way other platforms do this.
  Also the .a versions do not have all symbols that are present in the .so
  files.

tcc.h:
- Use different dynamic loader

elf.h:
- Add SHT_X86_64_UNWIND

tccelf.c:
- Do not use -dl
- Add required NOTE section
- Add extra dynamic tags
- Allow SHT_X86_64_UNWIND/SHT_NOTE in tcc_load_object_file

tccrun.c:
- Uses MAP_FIXED because without the offset between exec and data section
  becomes too big for x86_64

lib/bcheck.c:
- Do not use __libc_freeres

tests/tcctest.c:
- aligned_function also disabled for __GNUC__ == 4
2020-12-07 08:27:10 +01:00
Christian Jullien
19d287aae3 OpenBSD: fix typo in _ANSI_LIBRARY defined symbol 2020-12-07 07:57:01 +01:00
Christian Jullien
a3d5e4aa43 OpenBSD: define more internal symbols to support tcc on OpenBSD. 2020-12-07 07:15:56 +01:00
herman ten brugge
170be79a42 duplicate member
Check duplicate struct/union member names

tcc.h: Add cnt field in TokenSym

tccgen.c: New function check_fields to find duplicate member names.

This avoids quadratic behavior and can be used for large structs.
2020-12-03 07:53:44 +01:00
Christian Jullien
6f2659c230 OpenBSD: SYS_gettid syscall is named SYS_getthrid - WIP 2020-12-02 08:05:34 +01:00
Christian Jullien
38dea90b2f OpenBSD: workaround missing SYS_gettid - WIP 2020-11-29 08:54:55 +01:00
Christian Jullien
dfe031caa6 OpenBSD: Fix crt startup names - WIP 2020-11-29 08:47:27 +01:00
Christian Jullien
4e12c2a6dc OpenBSD: improve partial support for x86_64 - WIP 2020-11-27 11:24:19 +01:00
grischka
cf8d9cf072 win64: fix pe_isafunc()
- tccpe.c: commit "tidy support for helper function" created
  STT_NOTYPE symbols and hence relied on ad-hoc detection which
  didn't work for x86_64 (as reported by Christian Jullien)

- tccgen.c: However to be more safe the helper symbols are
  now made STT_FUNC anyway (via new VT_ASM_FUNC).

Also:
- tcc.h: minor reorder
- riscv64-*, arm64-*, tccmacho.c: avoid some gcc format-warnings
  (mingw-gcc complains about "%llx" being "unknown conversion",
  although it does work since Vista or so)
2020-11-26 16:11:17 +01:00
Tyge Løvset
13a18906ef win32 tcc_libm.h: Implemented overrides for msvcrt.dll's frexp(), ldexp(), and fabs() with speedups 4.5X, 7.3X and 6.3X correspondingly.
This is the last commit on this for now.
2020-11-25 14:29:42 +01:00
grischka
4a42b0d95e tidy support for helper function such as memmove
tcc.h, tccgen.c: Introduce Sym *external_helper_sym(int v);
  to create an external reference with no specific type. This
  avoids type conflicts if the symbol is used from C too.
the other files: use it.
2020-11-24 11:47:02 +01:00
Tyge Løvset
2f78f54924 win32 libm: Final optimizations and added nan*() function. 2020-11-23 20:42:36 +01:00
Arthur Williams
3709f8de14 Treat func pointers with different return types as not compatible
Tcc considered function ptrs with different return types to be
compatible which disallowed some otherwise valid operations like:
`_Generic(foo, int(*)():0, void(*)(void):1)`
which would fail to compile with a error message of "type match twice"

This changed also required longjump's return type to be void and
munmap's to be int to be compatible with standard headers.
2020-11-22 16:30:34 -06:00
Tyge Løvset
3b1a42e734 Cleanup and minor fixes in win32/tcc/tcc_libm.h: truncf(), cbrtf(), exp2f(), log2f(), scalbln*() 2020-11-21 14:45:35 +01:00
Tyge Løvset
b11144d69c Overhauled WIN32 math and added missing functions:
1) Cleanup: moved function implementations from win32/include/math.h to win32/include/tcc/tcc_libm.h
2) Added missing math functions: MUSL: asinh(), acosh(), atanh(), scalbn(). My impl: log1p(), expm1(), ilogb(), scalbln(), nexttoward()
 - now only a few are missing: remquo(), remainder(), fma(), nan(), erf(), erfc(), nearbyint().
3) Added/defined all missing *f() and *l() math functions.
4) Added functions have short but accurate/fast implementations.
5) Added <tgmath.h> for all platforms. (not too useful, IMO, but is C99 standard).
2020-11-20 23:34:35 +01:00
Wirtos_new
82611f5e6d Fix char limits
By the standard CHAR_MIN and CHAR_MAX should be 0 and UCHAR_MAX respectively when char implementation is unsigned ( -fno-signed-char)
2020-11-17 09:06:48 +02:00
Tyge Løvset
1073b379c8 WIN32/64 only: Added C99 math functions: tgamma(), tgammaf(), lgamma(), lgammaf(), cbrt(), cbrtf(), log2(), log2f(), exp2(), exp2f().
The gamma() functions are accurate to 13 digits over the full range. Returns INFINITY / NAN identical to other compiler's math libs.

#include <stdio.h>
#include <math.h>

int main() {
  double x;
  for (x = -7.0; x <= 520.0; x += 1.0) {
    printf("tgamma/lgamma %6.3f: %20.13g  %20.13g\n", x/3, tgamma(x/3.0), lgamma(x/3.0));
  }
  printf("tgamma/lgamma %g: %20.13g  %20.13g\n", 0.00000234, tgamma(0.00000234), lgamma(0.00000234));
  printf("log2, exp2, cbrt, cbrt: %.15g %.15g %.15g %.15g\n", log2(1024), exp2(10), cbrt(-10), cbrt(10));
}
2020-11-14 00:02:58 +01:00
herman ten brugge
08d8015750 Fix fork problem 114_bound_signal
There is a race condition in old libc in fork().
The result was that 'end' was sometimes printed twice.

This did not happen with glibc-2.32.
2020-11-10 10:08:30 +01:00
herman ten brugge
54b4aa3cd6 Disable signals in bcheck.c pthread_create
glibc-2.32 contains new code to prevent a race conditions with signals
in pthread_create.
2020-11-09 10:57:14 +01:00
grischka
8b69059f66 three small fixes & three reverts
- tcc.h: msvc doesn't grok __func__ (reverts previous commit)

- tccgen.c: fortify tcc against bogus code:
  - n[sizeof({3;})]; // statement expression outside of function
  - f(){"123"4}; // tokens with values following each other
  (also, add "type defaults to int" warning for variables)

- tccpe.c: removed a check that caused BSS symbols not to be
  exported.  Whatever that check was meant to prevent.

- win32/build-tcc.bat: cmd.exe sometimes doesn't grok '-' in labels

- Revert "libtcc: no need to undef"
  This reverts commit 2b7aa2a1e1.
- Revert "tcc.h libtcc.c: remove unused defines"
  This reverts commit 985d963745.

The point of these "unused defines" is to be unused,  that is
to remind people not to use malloc but please to "use_tcc_malloc",
instead.
2020-11-02 18:08:56 +01:00
Kyryl Melekhin
2b7aa2a1e1 libtcc: no need to undef 2020-11-01 15:51:50 +00:00
Kyryl Melekhin
985d963745 tcc.h libtcc.c: remove unused defines
pretty sure that functions with use_* do not exist
and attempts to use the macros will fail to link
I could rename them to be more up to date like tcc_malloc,
but to me overloading the std calls with macros is
probably taking away control from the programmer,
so for the best is to just get rid of them.
2020-11-01 15:24:45 +00:00
John Scott
53587ee415
tcc_internal_error: print function with __func__.
Though __func__ was introduced in C99, this is
preferable to __FUNCTION__, an obsolete GCC extension.
Found with gcc -Wpedantic.
2020-10-18 17:54:01 -04:00
John Scott
558c6f56e2
tcc_mallocz: clear with memset only if nonzero size
Even if non-NULL, dereferencing the pointer from malloc(0)
or passing it to memset() may invoke undefined behavior.
2020-10-16 21:20:33 -04:00
grischka
72b520e709 tccgen: flex arrays etc.
Fixes potential writes past the allocated space with mostly
illegal flex array initializers. (60_errors_and_warnings.c
:test_var_array)

In exchange suspicious precautions such as section_reserve
or checks with sec->data_allocated were removed.  (There is
an hard check 'init_assert()' for now but it's meant to be
just temporary)

Also, instead of filling holes, always memset(0) structures
& arrays on stack.  Sometimes more efficient, sometimes isn't.
At least we can omit putting null initializers.

About array range inititializers:  Reparsing tokens has a
small problem with sideeffects, for example

   int c = 0, dd[] = { [0 ... 1] = ++c, [2 ... 3] = ++c };

Also, instead of 'squeeze_multi_relocs()', delete pre-existing
relocations in advance. This works even if secondary initializers
don't even have relocations, as with
    [0 ... 7] = &stuff,
    [4] = NULL

Also, in tcc.h: new macro "tcc_internal_error()"
2020-10-03 18:12:46 +02:00
grischka
40395511d7 Revert "function pointer compare"
Always fine to try out things but not everything must be shown
to the public. ;)

Also, AFAIK pointers must compare equal only if derived directly
from each other (for example by cast to void* and back).

This reverts commit 8f9bf3f223.
2020-10-03 17:40:22 +02:00
grischka
cdc3df949b tcc -bench: show text/data/bss binary output sizes 2020-10-02 19:50:45 +02:00
herman ten brugge
ae1796fc34 Fix testcase 114 for arm and apple
Arm has a problem with tls after a fork. The pthread_key_create seems to
be forgotten?

Apple has a problem with the exit(0) code in do_fork(). An IO mutex
is still held after a fork().
2020-10-02 17:50:31 +02:00
Michael Matz
78da4586a0 Fix tests2/120_alias.c on macos
While MacOS doesn't natively support the alias attribute, let's support
it with TCC anyway.  This means we need to make a decision if the
string in the alias attribute is decorated or not due to the implicit
underscore on MacOS.  To make life easier we decide that it's the C name,
i.e. without underscore, and so TCC needs to emit alias names with
underscore handling.

Irrespective of that the test case needs to deal with the underscore
itself for __asm__ renaming which is always requiring the assembler name.
2020-10-01 18:03:56 +02:00
herman ten brugge
0da93838c1 Init range wth symbols
The init range with symbols did only init the first value.
The relocation for all other symbols was missing.
Also see testcase.

tccgen.c:
- New function get_init_string
- Use macro processing in decl_designator for each init string
- Use get_init_string in decl_initializer_alloc

tccelf.c:
- Fix insertion sort in squeeze_multi_relocs

tests/tests2/90_struct-init.c:
- Add test case test_init_ranges
2020-10-01 17:50:20 +02:00
herman ten brugge
afc0917f88 Bound checking fixes
tccgen.c:
- Fix 'tcc -b conftest.s'
- Add offset during bound checking for struct return

lib/bcheck.c:
- Check overlap when reusing vla/alloca

arm-gen.c:
arm64-gen.c:
riscv64-gen.c:
lib/alloca86-bt.S:
- add space for vla/alloca during bound checking

tests/tests2/Makefile:
tests/tests2/121_struct_return:
tests/tests2/122_vla_reuse:
- New test cases with bound checking enabled to test vla and struct return
2020-10-01 17:09:09 +02:00
Michael Matz
352e1d0fc4 Reinstate attribute alias handling
commit 2a0167a merged alias and asm symbol renaming, but broke
semantics of aliases, see testcase.  Basically the difference between
the two is that an asm rename doesn't generate a new symbol, i.e. with

  int foo __asm__("bar");

all source reference to 'foo' will be to 'bar', nothing of the name
'foo' will remain in the object file, and for instance reference to
'foo' from other compilation units won't be resolved to this one.

Aliases OTOH create an additional symbol.  With:

  void target (void) { return; }
  void afunc (void) __attribute__((alias("target")));

reference to 'afunc' will remain 'afunc' in the object file.  It will
generate two symbols, 'afunc' and 'target' referring to the same entity.
This difference matters if other compilation units make references to
'afunc'.

A side requirement of this is that for alias to work that the target
symbol needs to be defined in the same unit.  For TCC we even require a
stricter variant: it must be defined before the alias is created.

Now, with this I merely re-instated the old flow of events before above
commit.  It didn't seem useful anymore to place both names in the
asm_label member of attributes, and the asm_label member of Sym now
again only needs the hold the __asm__ rename.

It also follows that tcc_predefs.h can't make use of attribute alias to
e.g. map __builtin_memcpy to __bound_memcpy (simply because the latter
isn't defined in all units), but rather must use __asm__ renaming, which
in turn means that the underscore handling needs to be done by hand.
2020-09-30 17:46:01 +02:00
herman ten brugge
727e24cb0a Add typedef debug info
tccgen.c:
- In tcc_get_debug_info mask VT_STORAGE instead of VT_EXTERN | VT_STATIC
- New function tcc_debug_typedef
- Call tcc_debug_typedef in decl0
2020-09-27 11:13:37 +02:00
herman ten brugge
8fd7a384e2 Fix bitfields init : tiny solution
tccgen.c: Check struct/union size for bitfield.
tests/tcctest.c: Add test code.
2020-09-25 12:23:48 +02:00
herman ten brugge
89ea62481d clang7 does not support zero sized structs
This happens with aarch64 on raspberry pi.
2020-09-21 15:55:58 +02:00
herman ten brugge
8f9bf3f223 function pointer compare
tccelf.c:
- Check if symbol is in data section and UNDEF. Then generate new
  relocation and let dynamic linker solve it.

tests/tests2/42_function_pointer.c:
- Add new test code
2020-09-21 09:18:48 +02:00
Christian Jullien
ffac4e7688 Herman proposed patch seems to fix clang issue on x64 Debian 2020-09-19 08:04:20 +02:00
grischka
8cb3e5368f bitfields init : tiny solution
This replaces commit 5c6356ff8e,
except the tests.
2020-09-18 23:31:34 +02:00
herman ten brugge
5c6356ff8e default-initialization of bitfields
The code:

struct bf_SS {unsigned int bit:1,bits31:31; };
void func(void) {
  struct bf_SS bf_finit = { .bit = 1 };
}

will not init bits31 to 0.

tccgen.c:
- check_bf: New function to check if bitfield is present in struct/union
- decl_initializer: Call check_bf and set value to 0 is bitfield found

tests/tcctest.c:
- Add struct bitfield test code
2020-09-18 19:20:57 +02:00
Christian Jullien
6d819d7267 Indentation was missing after empty string check was added by wanjochan. 2020-09-18 06:39:30 +02:00
wanjochan
bea7dcde86 check strlen(TCC_LIBTCC1) before tcc_add_support(s1, TCC_LIBTCC1) 2020-09-18 06:57:56 +08:00
grischka
aed5cd0ce9 revert 'no-libtcc1' feature
from 43ae350390
and a3578379fb

Sorry, but this feature appears to be rather a personal
experiment than generically interesting for other people.
Also those "other people" always have some interest in
TCC staying simple.

One can still avoid the startup code from libtcc1.a simply
by providing __start explicitly.

While at it: Tidy tccpe.c:pe_add_runtime(), somewhat.
2020-09-17 20:57:21 +02:00
grischka
4cd6298f9d libtcc1.c: cleanup fp2ull
- replace '>>' by '<<' in __fixunsxfdi (typo)
- replace 'long' by 'long long' (long may be 32 bits)
- return an overflow result more similar to GCC
2020-09-17 20:25:31 +02:00
Kyryl Melekhin
55b4754e84 Revert "add tests for float conversions to u64"
Because test's are not applicable in this case.

This reverts commit a5e714abec.
2020-09-17 07:12:49 +00:00
herman ten brugge
c9bbd4e707 Allow strings in __builtin_constant_p
tccgen.c:
- Fix handling __builtin_constant_p

tests/bug.c:
- Remove tst3

tests/tcctest.c:
- Add new tests for __builtin_constant_p
2020-09-17 09:11:10 +02:00
herman ten brugge
4a16bebfab Struct va_arg fix
lib/va_list.c:
- Handle struct {double, double} correctly

arm64-gen.c:
riscv64-gen.c:
x86_64-gen.c:
- Allow zero sized structs to work with va_arg

tcctest.c:
- Add new va_arg test code

test/bug.c:
- Remove tst2 va_arg test
2020-09-17 08:42:28 +02:00
herman ten brugge
757a97466f Fix testcase 114 for macos
lib/bt-exe.c:
- call __bound_init before sigset_exception_handler because sigaction
  is redirected.

tests/tests2/Makefile:
- run testcase 114 on macos again
2020-09-17 08:22:53 +02:00
Christian Jullien
13c66526ed Fix wanjochan commit 'when tcc1 omit: use main()' when tcc is bootstrapped by tcc on Windows. Variable must be NULL initialized 2020-09-17 06:52:39 +02:00
Christian Jullien
15182d7fdd Don't mix code and declaration 2020-09-17 06:51:59 +02:00
wanjochan
a3578379fb when tcc1 omit: use main() directly 2020-09-17 10:26:16 +08:00
wanjochan
7eebf614dc tcc c-tyle-compliance: space between if and ( 2020-09-17 06:54:13 +08:00
wanjochan
89935229a7 tcc_add_support(): void return for win 2020-09-17 00:53:50 +08:00
wanjochan
43ae350390 tcc_add_support(): skip when filename is empty 2020-09-17 00:47:32 +08:00
wanjochan
55eafa66b7 test 114: skip bcheck for osx (tcc -run not support fork with -b) 2020-09-17 00:43:19 +08:00
herman ten brugge
d55e586bc6 Fix boundschecking fork for macos/SELINUX
lib/bcheck.c:
- Use INIT_SEM for child process fork on macos

tests/tests2/Makefile:
- tcc -run does not work for fork due to SHARED mmap
2020-09-14 19:31:56 +02:00
herman ten brugge
61c0c852b5 Update boundschecking for fork
bcheck.c:
- Fix fork function.
- Move use_sem
- Fix bound_alloc_error text

tests/tests2/114_bound_signal.c:
- Add test for fork
2020-09-14 08:24:01 +02:00
Pursuer2
8878c29c5d misplaced parenthese around definition of CONFIG_TCC_BCHECK 2020-09-12 07:09:18 +02:00
Kyryl Melekhin
a5e714abec add tests for float conversions to u64
Note:
I removed the test that used sin()
function because it makes no sense
to use that there and besides I could
not get the test to work because
sin requires -lm linked but for some reason
make does not compile with -lm and
I get errors like undefined symbol sin.
Coerce function should do the same thing
for the purposes of that test.
2020-09-11 09:18:58 +00:00
Kyryl Melekhin
618ba55a81 fix float to u64 intrinsics
reverts commit
310e3b428c
(more info there)

now functions check for
sign bit in float.

now hopefully this patch will
cover entirety of areas it might affect
2020-09-10 17:35:36 +00:00
Christian Jullien
60c1f70bb9 Revert commit 55f8963dfa from wanjochan until better tested on all platforms 2020-09-10 05:49:15 +02:00
Kyryl Melekhin
310e3b428c (bug caused by tcc intristics)
reproduce bug:
$ ./configure --cc=gcc
$ make
$ make install
(OK)
run a test:

extern int printf(const char *str, ...);
int main()
{
        int t2 = (int)(-1.847759065f * 4096);
        printf("%d\n", t2);
}

$ tcc test.c
$ ./a.out
$ -7568
(OK)

(self compiled now)
$ ./configure --cc=tcc
$ make
$ make install
(OK)

$ tcc test.c
$ ./a.out
$ 7568
(WRONG!!!)

why:
gcc does not have intristics for
uint to long double conversion
therefore it does cast implicitly, so
the sign bit is preserved, but this does
not happen when __fixunsxfdi is called
because tcc was bootstrapped.

solution:
force cast to int64 and preserve the
sign bit.

side effects:
not found.
2020-09-08 22:12:01 +00:00
wanjochan
55f8963dfa ignore symbol main for .dylib; skip libtcc1 for tccrun mode; 2020-09-08 22:05:00 +08:00
herman ten brugge
853a498f2c Fix boundschecking for signal/sigaction/fork
The BOUNDS_CHECKING_ON/BOUNDS_CHECKING_OFF is not working for
signal/sigaction/fork. The reason is that the code stops bound checking
for the whole application. This result in wrong handling of
__bound_local_new/__bound_local_delete and malloc/calloc/realloc/free.
Consider the following code:

void tst(int n) {
  int i, arr[n];
  for (i = 0; i < n; i++) arr[i] = 0;
}

void *some_thread(void *dummy) {
  while (running) { tst(10); tst(20); }
}

void signal_handler(int sig) { ... }

When the signal handler is called the some_thread code can be interrupted when
is just registered the arr[10] data. When the signal handler is leaved the
arr[10] is still registered and did not see the call to deregister arr[10] and
then register arr[20]. The code resumes when tst(20) is running. This results
in a bound checking error when i >= 10.

To solve the above problem I changed the bound checking code to use
tls (thread local storage) for the no_checking variable.
This also makes it now possible to redirect signal/sigaction/fork code
through the bound checking library and disable checking when a signal is
running and to correct the bounds_sem for the fork child process.
The BOUNDS_CHECKING_ON/BOUNDS_CHECKING_OFF is not needed any more for
signal/sigaction/fork. In fact I could remove them from all my applications.

The use of the tls function code slows down the code by about 10%.
So if the slowdown due to bound checking was 5. It is now 5.5 times slower.

For x86_64/i386 I also allowed to use __thread variable in bcheck.c when
compiled with gcc with:
make x86_64-libtcc1-usegcc=yes
make i386-libtcc1-usegcc=yes
This makes code run faster due to use of gcc and __thread variable.
With the __thread variable there is no 10% slowdown.
For other targets this does not work because stabs is not supported.

Changes:

lib/bcheck.c:
- Add TRY_SEM
- Add HAVE_SIGNAL/HAVE_SIGACTION/HAVE_FORK/HAVE_TLS_FUNC/HAVE_TLS_VAR
  - HAVE_SIGNAL: redirect signal() call if set.
  - HAVE_SIGACTION: redirect sigaction() call if set.
  - HAVE_FORK: redirect fork() call if set.
  - HAVE_TLS_FUNC: If target has tls function calls.
  - HAVE_TLS_VAR: If target has __thread tls support.
- Replace all no_checking refecrences to NO_CHECKING_SET/NO_CHECKING_GET macros

tcc-doc.texi:
- Remove examples for signal/sigaction/fork code.
- Add some explanation for signal/sigaction/fork code.
- Add documentaion for __bounds_checking().

tccelf.c:
- Add support for SHF_TLS

tests/tests2/114_bound_signal.c:
- Remove BOUNDS_CHECKING_ON/BOUNDS_CHECKING_OFF
- Add code to trigger failure when tls is not working.

x86_64-link.c:
- Add support for R_X86_64_TLSGD/R_X86_64_TLSLD/R_X86_64_DTPOFF32/R_X86_64_TPOFF32

i386-link.c:
- Add support for R_386_TLS_GD/R_386_TLS_LDM/R_386_TLS_LDO_32/R_386_TLS_LE
2020-09-08 14:31:58 +02:00
grischka
53d815b8a0 win32/tccpe: use full dll-path with -run
This allows for example this scenario:

- A dll to be linked with is specified in file.c, where file.c
  and the dll exist in the same directory:
    #pragma comment(lib, "txml")
    #pragma comment(option, "-L{f}")

- tcc is called to run file.c from other, varying directories:
    $ tcc -run some/dir/file.c <args...>

Note that tcc replaces {f} by the currently compiled file's
directory ('some/dir' in this example).

Also:
- tccgen.c: fix last commit for gen_cast.
2020-08-21 22:47:56 +02:00
grischka
d746e32349 tests2: rework 117..119 to follow our conventions
Please respect some conventions:

- tests2 filenames don't end with '..._test'

- tests2 tests are meant to produce some output

- the output should be somehow informative, not just
  "error" or "dummy". Because other people would want to
  know where it fails if it does.

- tests2 tests should work with both GCC and TCC, except
  if there are specifc reasons (like testing tcc-only
  feature such as bounds checking)

- tests2 tests should never crash or abort.  Because that
  would cause gui dialogs to pop up on windows, and because
  other people would not know where it fails if it does.

- tests2 tests should be somehow specific, in general.
  (rather than just collections of random stuff)

- in general, do not use 'long' if you mean 'larger than int'
  Because it isn't on many platforms.

- use four (4) spaces for block indention.  Do not insert
  tab characters in files if possible.

Also:
- tccgen.c:gen_cast() simplify last fix.
2020-08-21 21:44:11 +02:00
grischka
f9870f7860 bcheck: remove static (compile-time) control
Providing both run-time and compile-time control for bounds
checking as an user interface appears unnecessary and confusing.

Also:
- replace 'bound_...' by 'bounds_...' for consistency
- tcc-doc: put related info into one place and cleanup

The __bounds_checking(x) function is still missing explanation.
(I.e. what happens if the accumulated value drops below zero.)
2020-08-21 20:26:36 +02:00
herman ten brugge
a34a9775ba Fix char to ushort cast
tccgen.c:
- gen_cast: add check for char to ushort cast

tests/bug.c:
- remove tst1

tests/tests2/117_gcc_test.c:
- add tst_cast
2020-08-21 19:35:30 +02:00
herman ten brugge
696b765437 Fix switch/case
Fix switch for signed/unsigned switch
Also add new testcase 118
2020-08-18 20:05:53 +02:00
Willy Tarreau
b107f7bdd9 Fix switch/case on uint64_t
The switch/case operation was entirely performed on int64_t, resulting
in a warning and bad code to be emitted on 64 bit machines when used on
an unsigned long with a case range whose signed representation starts
positive and ends negative like in the example below:

  #include <limits.h>
  #include <stdio.h>
  #include <stdlib.h>

  int nbdg(unsigned long n)
  {
  	switch (n) {
  	case                    1UL ...                   9UL: return 1;
  	case                   10UL ...                  99UL: return 2;
  	case                  100UL ...                 999UL: return 3;
  	case                 1000UL ...                9999UL: return 4;
  	case                10000UL ...               99999UL: return 5;
  	case               100000UL ...              999999UL: return 6;
  	case              1000000UL ...             9999999UL: return 7;
  	case             10000000UL ...            99999999UL: return 8;
  	case            100000000UL ...           999999999UL: return 9;
  	case           1000000000UL ...          9999999999UL: return 10;
  	case          10000000000UL ...         99999999999UL: return 11;
  	case         100000000000UL ...        999999999999UL: return 12;
  	case        1000000000000UL ...       9999999999999UL: return 13;
  	case       10000000000000UL ...      99999999999999UL: return 14;
  	case      100000000000000UL ...     999999999999999UL: return 15;
  	case     1000000000000000UL ...    9999999999999999UL: return 16;
  	case    10000000000000000UL ...   99999999999999999UL: return 17;
  	case   100000000000000000UL ...  999999999999999999UL: return 18;
  	case  1000000000000000000UL ... 9999999999999999999UL: return 19; // this one
  	case 10000000000000000000UL ...             ULONG_MAX: return 20;
  	}
  	return 0;
  }

  int main(int argc, char **argv)
  {
  	unsigned long v = strtoul(argc > 1 ? argv[1] : "1111", NULL, 0);
  	printf("%lu : %d\n", v, nbdg(v));
  	return 0;
  }

  $ tcc dg.c
  dg.c:26: warning: empty case range
  $ x="";for i in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0; do x=$x$i; ./a.out $x;done
  1 : 1
  12 : 2
  123 : 3
  1234 : 4
  12345 : 5
  123456 : 6
  1234567 : 7
  12345678 : 8
  123456789 : 9
  1234567890 : 10
  12345678901 : 11
  123456789012 : 12
  1234567890123 : 13
  12345678901234 : 14
  123456789012345 : 15
  1234567890123456 : 16
  12345678901234567 : 17
  123456789012345678 : 18
  1234567890123456789 : 0
  12345678901234567890 : 20

What this patch does is to use a separate set of signed and unsigned
case_cmp functions depending on whether the expression is signed or
unsigned, and also does this to decide when to emit the warning.

The bad code on output was caused by the removal of the unsigned bit
resulting from the signed sort, which causes only signed comparisons
to be emitted in the asm code. As such some sets could not match.

Note that there is no way to rely on the values only to sort properly
nor to emit the warning because we're effectively dealing with 65-bit
arithmetic here and any two values will have a different behavior
depending on the signed or unsigned expectation.

For unsigned expressions now the warning only happens when bounds are
switched, For signed expressions (e.g. if the input is signed long
above), the warning remains and the abnormal output as well. In both
cases this remains consistent with what gcc produces.
2020-08-18 11:27:27 +02:00
wanjochan
3613a11454 rm helper scripts newly added by me 2020-08-16 10:46:19 +08:00
wanjochan
777c017034 cleanup win32/tccwin_build.sh 2020-08-15 11:13:10 +08:00
Thomas Preud'homme
62c30a4a13 Fix typo in tcc-doc 2020-08-14 22:54:51 +01:00
herman ten brugge
4c9e3a5988 Update attribute bound_no_checking
tcctok.h:
- Add CONFIG_TCC_BCHECK  arround TOK_NO_BOUND_CHECK1/TOK_NO_BOUND_CHECK2

tccgen.c:
- Add CONFIG_TCC_BCHECK  arround TOK_NO_BOUND_CHECK1/TOK_NO_BOUND_CHECK2
- Undo alias definition in tccpp.c when function bound checking if off

tests/tests2/114_bound_signal.c:
- Test alias undo
- fix sleep problem
2020-08-14 06:35:47 +02:00
herman ten brugge
c740fa2795 Fix attribute patch for windows 2020-08-13 11:26:59 +02:00
herman ten brugge
50fe33f880 Add attribute bound_no_checking
tcc-doc.texi:
- Document attribute bound_no_checking

tcctok.h:
- Add bound_no_checking attribute

tcc.h:
- Add no_bcheck function attribute

tccgen.c:
- Use function attribute no_bcheck in merge_funcattr/parse_attribute/gen_function

bcheck.c:
- Fix no_checking in __bound_new_region/__bound_free/__bound_check

tests/tests2/114_bound_signal.c:
- Fix code with new attribute bound_no_checking

tests/tests2/103_implicit_memmove.c:
- Fix memmove prototype
2020-08-13 11:19:11 +02:00
wanjochan
5aaed43efd tune win32/tccwin_build.sh for libtccXX.dll 2020-08-13 12:34:03 +08:00
wanjochan
a06e8350aa helper script: build tcc32.exe tcc64.exe libtcc32.dll libtcc64.dll 2020-08-12 18:30:14 +08:00
wanjochan
cdf001c296 helper scripts for windows 2020-08-12 10:21:19 +08:00
herman ten brugge
70b16cb7f8 Fix argv/environ bound checking 2020-08-11 08:39:12 +02:00
herman ten brugge
8b8e714517 Fix bound checking for packed struct 2020-08-11 07:33:11 +02:00
herman ten brugge
09ed7e9557 Add bool debug type 2020-08-11 07:23:01 +02:00
herman ten brugge
e5da657c85 Fix testcase 95 for windows 2020-08-09 08:28:45 +02:00
herman ten brugge
dcb87d36fe Fix long bitfield
The fix is avoiding a core dump for radare2 project.
2020-08-09 07:50:34 +02:00
wanjochan
9085b38a71 add "libtcc.osx" entry to Makefile, for programs who need more pure libtcc.dylib without "@rpath/" config 2020-08-07 21:10:39 +08:00
herman ten brugge
3cfaaaf1eb Fix dll on arm.
Do not output the arm stack unwinding section (SHT_ARM_EXIDX).
2020-08-05 13:55:11 +02:00
herman ten brugge
a0a0f4d029 Add riscv dll support
Most support was already present.

riscv64-link.c:
- create_plt_entry:
 - remove DLLs unimplemented!
- relocate:
 - Add TCC_OUTPUT_DLL for R_RISCV_32, R_RISCV_64

tccelf.c:
- prepare_dynamic_rel:
 - Add R_RISCV_32, R_RISCV_64
2020-08-04 10:36:47 +02:00
herman ten brugge
b8fb2b02d9 Add arm dll support
Most support was already present.

arm-link.c:
- set RELOCATE_DLLPLT to 1
- create_plt_entry:
 - remove DLLs unimplemented!
 - leave code gen to relocate_plt. only set got_offset
- relocate_plt:
 - create code for got entry
- relocate:
 - Add TCC_OUTPUT_DLL for R_ARM_ABS32

tccelf.c:
- prepare_dynamic_rel:
 - Add R_ARM_ABS32
- alloc_sec_names:
 - Always add SHT_ARM_ATTRIBUTES section
- New function create_arm_attribute_section
- elf_output_file:
 - call create_arm_attribute_section
2020-08-04 09:15:42 +02:00
herman ten brugge
1da7159689 Add arm64 dll support
Most support was already present.

arm64-link.c:
- create_plt_entry:
 - remove DLLs unimplemented!
- relocate:
 - Add TCC_OUTPUT_DLL for R_AARCH64_ABS64/R_AARCH64_ABS32/R_AARCH64_PREL32

tccelf.c:
- prepare_dynamic_rel:
 - Add R_AARCH64_ABS64/R_AARCH64_ABS32/R_AARCH64_PREL32
- fill_got_entry:
 - Change 'TCC_TARGET_X86_64' into 'PTR_SIZE == 8'
2020-08-01 19:44:49 +02:00
herman ten brugge
d55a3f3362 Fix riscv64 compare problem.
Fix 64->32 bits sign/zero extention for riscv64.
2020-07-30 09:40:35 +02:00
herman ten brugge
d1ce34448f Faster load/store arm64
The load/store code is optimized to make better use of the offsets
present in the load/store instructions.
Also use GOT reloc's instead of ABS64 relocs.

arm64-gen.c/arm64_check_offset:
- New function to split offset used by load/store and by arm64_sym.

arm64-gen.c/arm64_sym:
- Use GOT reloc's instead of ABS64 relocs.

arm64-gen.c/load arm64-gen.c/store:
- Use new arm64_check_offset function.

arm64-gen.c/gen_bounds_prolog arm64-gen.c/gen_bounds_epilog:
- Use GOT reloc's instaed of ABS64 relocs.
2020-07-30 09:26:20 +02:00
Christian Jullien
58be11f701 Add L suffix to LDBL values. 2020-07-16 07:59:34 +02:00
Michael Matz
9b2329f66c riscv64: Work around qemu bug
old qemu (before april 2020) have a bug in the layout of
struct ucontext, so we get invalid values under qemu-userspace emulation
when inspecting the signal context.  Try to recognize this and
graciously error out instead of segfaulting in the backtracer routines.
2020-07-15 23:11:58 +02:00
Michael Matz
2e798523e4 Fix conversions of subnormals to long double
those need to be normalized when extending from float/double to
binary128.
2020-07-15 22:02:02 +02:00
Michael Matz
a614269794 riscv64: Fix a corner case
found in mpfr.  Expressions like "(longlong)i <= MAX_ULONGLONG" are
always true (not yet short-circuited in tcc), but still need to be
handled correctly in the backends.
2020-07-15 21:47:39 +02:00
Christian Jullien
92769362c7 Fix LDBL_xx values for aarch64 and riscv 2020-07-15 15:55:49 +02:00
herman ten brugge
c1e1c17c0a Move bound functions to tccgen.c
Move gen_bounded_ptr_add() and gen_bounded_ptr_deref() code to tccgen.c
No functional changes.
2020-07-12 10:55:40 +02:00
Christian Jullien
8314119662 USES: add mpc library 2020-07-11 20:00:37 +02:00
Christian Jullien
497678eee3 USES: just tested with gmp and mpfr on RPi 2020-07-11 07:42:18 +02:00
Christian Jullien
b5af2d3428 Add -C gcc compatible option which does currently nothing. This allows, among others, to compile gnumake from git ROOTB. 2020-07-11 06:41:44 +02:00
Christian Jullien
28e9fc1913 macos: 'make uninstall' also removes *.dylib 2020-07-10 10:38:36 +02:00
Christian Jullien
e19665c0d8 USES: add zlib 2020-07-10 06:35:02 +02:00
Steffen Nurpmeso
513a8b074d USES: s-nail 2020-07-09 23:52:43 +02:00
Avi Halachmi (:avih)
c69290fb0c macos: support arbitrary configure --libdir
Previously, after installation, only ../lib relative to the binary was
supported for finding libtcc.dylib, now any custom libdir.
2020-07-10 00:03:48 +03:00
Christian Jullien
6c94df6e2d macos: add path relative ../lib 2020-07-09 19:37:37 +02:00
Christian Jullien
6bd0ced239 Add USES file which list some software known to support tcc builds. 2020-07-09 17:21:23 +02:00
Christian Jullien
af0370a75d macos: add tcc version to libtcc.dylib. It can be shown with 'otool -L'. 2020-07-09 14:25:19 +02:00
Christian Jullien
b5a89c8c93 macos: tcc searches for libtcc.dyln in the same directory as its executable 2020-07-09 12:04:57 +02:00
Christian Jullien
1ea425811a macos: ldd does not exit, use otool instead 2020-07-09 08:03:31 +02:00
herman ten brugge
9d75f14107 Fix structure passing i386 PE
The orignal code does:
    push eax/edx/size
    call alloca
    pop  eax/edx/size

The pop does not work because the stack pointer has changed.
To make this also work with bound checking the code is now
using the stack probing from alloca.
2020-07-08 15:48:15 +02:00
herman ten brugge
20fa63488a Fix bounds checking
i386-gen.c:
- Fix large stack size alloca code.
  The returned value of alloca was not used corectly.

libtcc.c:
- Use __SIZE_TYPE__ for __builtin_offsetof

tccpp.c:
- Fix __MAYBE_REDIR and abort builtins.

tests/tests2/Makefile
- Run 117_gcc_test also with bound checking enabled
  This found the above problems.
2020-07-07 21:10:51 +02:00
Christian Jullien
49e2d06921 macos: initialize macho struct with memset which removes a clang warning on macOS. 2020-07-07 07:47:48 +02:00
herman ten brugge
fc05da3c0b Fix alloca and arm problems
alloca is only defined for i386 and x86_64
arm has __aeabi_ prefixes for mem... calls
2020-07-06 20:10:56 +02:00
Michael Matz
40671f76e4 Fix parsing of .s files
those aren't preprocessed, but our use of a fake file in
preprocess_start requires inline stack processing (which isn't done
without preprocessing).  Just don't try to setup anything requiring
preprocessing at all in this case.
2020-07-06 18:12:35 +02:00
Michael Matz
de06193d88 tccpp: Add -bench -E mode
for benchmarking the preprocessor, without having to use the PP_BENCH
macro.  No output will be produced for '-E -bench', so it's the raw
tokenization/preprocess speed.
2020-07-06 17:41:05 +02:00
grischka
2a0167adfe __builtin_/__bound_: A 'stay tiny' approach to the matter
tccgen.c:
- cleanup __builtin_... stuff
- merge __attribute((alias("sym"))) with __asm__("sym")
    Now one cannot have both, however for alias underscores are
    added if enabled.  For __asm__ they aren't.
tccpp.c:
- extend tcc_predefs accordingly.  Was generated with
  'cd tests/misc && tcc -run c2str.c tcc_predef.h tcc_predefs'
xxx-gen.c:
- move bcheck setjmp test to tccgen.c:gbound_args()
i386-gen.c:
- create win32 compatible stack space for big structures
tcctest.c:
- some cleanup + nicer output
2020-07-06 13:42:02 +02:00
grischka
6a4f3cf127 rework leading underscores
tested on win32/64 to pass the tests when enabled

- libtcc.c :
  let tcc define __leading_underscore if enabled
  tcc_add_symbol() : add _ automatically
- tccelf.c : remove tcc_get_symbol_err(), find_c_sym()
  currently symbol length is limited to 256 in several
  places, so we can use a fixed local buffer for now as well.
- win32/lib/crtinit.c : new file for init/fini
- lib/*.S, tests7* : use __leading_underscore
- bt-log.c: this file wont work relibaly if compiled with gcc
2020-07-06 13:00:47 +02:00
grischka
72277967ff some cleanups related to recent commits
- configure/Makefile : cleanup, really use CC_NAME
- tccasm.c : remove C99 construct that MSVC doesn't compile
- arm-gen.c, x86_64-gen.c, riscv64-gen.c, tccmacho.c : ditto
- arm64-gen.c: commit 383acf8eff wrote:
  "Instead of a cast, it would be better to pass the exact type."
  It is true that there are better solutions but it is not
  passing the exact type (I think).
- tcctest.c: revert "fix cast test for clang" 03646ad46f
  this obviously wants to test non-portable conversions
- 114_bound_signal.test: clock_nanosleep is too new for older
  linuxes, just use sleep() instead
2020-07-06 13:00:47 +02:00
grischka
0ee4989ed3 libtcc: accept tcc_define_symbol(s1, "sym=value", NULL)
Thus it can parse command-line -Dsym=value directly, for the
convenience of libtcc users or tcc itself

Also used in libtcc_test_mt.c to avoid strdup().
2020-07-06 13:00:47 +02:00
coltrane
90e9e34bec Add missing ws2tcpip.h which lets Microsoft Winsock Server sample compile ROOTB on Windows platform. 2020-07-06 09:47:09 +02:00
coltrane
e5d7e37157 Add missing ws2tcpip.h which lets tcc compile Microsoft Winsock Server sample on Windows platform. 2020-07-06 09:45:41 +02:00
herman ten brugge
9712aff9c0 Fix gcc testsuite problems
arm-gen.c:
- is_hgen_float_aggr/gfunc_sret: Fix for zero sized structs

arm64-gen.c:
- arm64_ldrs: Fix for zero sized structs
- arm64_sym: Use R_AARCH64_ABS64 instead of R_AARCH64_MOVW_UABS_G*
             This has same speed. See 117_gcc_test.c(tst_adr)
- load: Fix for zero sized structs and add VT_CONST | VT_LVAL support
- store: add VT_CONST | VT_LVAL support
- arm64_gen_bl_or_b: Allow branch. See 117_gcc_test.c(tst_branch)
- gen_bounds_prolog: Use R_AARCH64_ABS64 for bound checking
- gen_bounds_epilog: Use R_AARCH64_ABS64 for bound checking
- gfunc_call: Allow large stack
- arm64_gen_opic: Do not crash on large shift

riscv64-gen.c:
- load: Move type_size call. (move_reg set sv.type.ref NULL for VT_STRUCT)
- gfunc_call: Allow large stack
- gen_opil: Fix word mode calls

x86_64-gen.c:
- load: Fix for zero sized structs

libtcc.c:
- Add some defines for gcc testsuite (only most common)

tccgen.c:
- parse_builtin_params: Add types for builtins
- unary: Add builtins: __builtin_abort __builtin_memcpy __builtin_memcmp
                       __builtin_memmove __builtin_memset __builtin_strlen
                       __builtin_strcpy __builtin_strncpy __builtin_strcmp
                       __builtin_strncmp __builtin_strcat __builtin_strchr
                       __builtin_strdup __builtin_malloc __builtin_realloc
                       __builtin_calloc __builtin_free __builtin_alloca
- decl_initializer: Fix crash. See 60_errors_and_warnings(test_var_array)

tccmacho.c:
- Remove 'ret = 0'

tcctok.h:
- Add builtin/bound checking tokens

tests/gcctestsuite.sh:
- Add more counters and run execute tests

tests/bug.c
- Some remaining bugs in tcc (not complete)

tests/tests2/60_errors_and_warnings:
- Add test_var_array test

tests/tests2/117_gcc_test:
- New test

Results of gcctestsuite.sh for all targets:

linux:
x86_64: 3036 test(s) ok.  328 test(s) failed.  24 test(s) exe failed.
i386:   3037 test(s) ok.  327 test(s) failed.  24 test(s) exe failed.
arm:    2986 test(s) ok.  362 test(s) failed.  40 test(s) exe failed.
arm64:  2996 test(s) ok.  367 test(s) failed.  25 test(s) exe failed.
macos:  3031 test(s) ok.  332 test(s) failed.  25 test(s) exe failed.
riscv:  2948 test(s) ok.  401 test(s) failed.  39 test(s) exe failed.

windows:
x86_64: 3027 test(s) ok.  333 test(s) failed.  28 test(s) exe failed.
i386:   3029 test(s) ok.  331 test(s) failed.  28 test(s) exe failed.

linux with bounds checking:
x86_64: 3030 test(s) ok.  328 test(s) failed.  30 test(s) exe failed.
i386:   3028 test(s) ok.  327 test(s) failed.  33 test(s) exe failed.
arm:    2997 test(s) ok.  362 test(s) failed.  29 test(s) exe failed.
arm64:  2986 test(s) ok.  367 test(s) failed.  35 test(s) exe failed.
macos:  3026 test(s) ok.  332 test(s) failed.  30 test(s) exe failed.
riscv:  2936 test(s) ok.  409 test(s) failed.  43 test(s) exe failed.

windows with bounds checking:
x86_64: 3029 test(s) ok.  332 test(s) failed.  27 test(s) exe failed.
i386:   3027 test(s) ok.  331 test(s) failed.  30 test(s) exe failed.

all: fail for complex and vector types, nested functions
     and a lot of gcc defines/buitins.
arm/arm64/riscv: fail for asm and alloca.
riscv: fail with 'error: unimp: store(struct)'
2020-07-05 14:01:50 +02:00
Christian Jullien
6a15f15093 Fix coredump on linux when boostrapping tcc with -std=c11 2020-06-26 11:36:41 +02:00
herman ten brugge
03646ad46f Fix cast_test for clang.
The code was not portable and gcc could produce the same problem.
2020-06-25 09:29:45 +02:00
herman ten brugge
86992622dc do not disable all asm_test for macos 2020-06-25 09:10:11 +02:00
Christian Jullien
383acf8eff Fix warning detected by clang when passing unsigned int pointer when signed is expected. Instead of a cast, it would be better to pass the exact type. 2020-06-25 07:15:34 +02:00
herman ten brugge
50abaaeb0a Support clang on all targets
tccelf.c/tccpp.c/tccrun.c
Change: "..."+int into &"..."[int] to avoid clang warning.

tests/tcctest.c:
- Change __APPLE__ into __clang__
- Add undefined_function for clang
- disable most asm code for clang
- Fix res6/res8 for __builtin_constant_p
2020-06-24 20:51:18 +02:00
herman ten brugge
d5822b725e Fix ctype.h warning in lib/bcheck.c 2020-06-24 15:12:58 +02:00
Christian Jullien
f6e392a4af macos: add __builtin_flt_rounds. Forced to 1 which means 'to nearest' 2020-06-24 10:37:28 +02:00
Christian Jullien
f635de709e macos: clang -s option is obsolete - at least on macOS. 2020-06-24 07:06:48 +02:00
Christian Jullien
8bad5bfae5 macos: internally define _DONT_USE_CTYPE_INLINE_ otherwise, ctype.h macros may be defined multiple times in different translation units. 2020-06-23 15:53:01 +02:00
Christian Jullien
6b2bed447f macos: add some macros to replace few __builtin_xxx functions used in headers. 2020-06-23 12:27:03 +02:00
Christian Jullien
15e4f8b3b0 macos: Add DYLD_LIBRARY_PATH to tcc when configured with --disable-static - i.e. with .dylib support 2020-06-23 09:14:02 +02:00
Christian Jullien
9617ea6a3a macos: workaround to build tcc using tcc until .o object files generated by tcc are fully compatible with ar. Instead of ar, use 'tcc -ar' 2020-06-23 06:51:51 +02:00
herman ten brugge
3c53d54a43 Fix environ variable for macos. 2020-06-22 18:10:30 +02:00
herman ten brugge
039e4ec2a4 Call __bound_main_arg at startup
This uses a glibc feature present since constructor/destructor support was added.

Modify tccrun.c to call constructor with argc, argcv, envp.
In lib/bcheck.c use these values to register them in the splay tree.
Remove HAS_ENVIRON is lib/bcheck.c as it is not needed any more.
Modify win32/lib/crt1.c/win32/lib/dllcrt1.c/win32/lib/wincrt1.c to also
call constructor with argc, argcv, envp.
While implementing I saw that tccrun did nog call main with envp. Fixed it.
Also fix fetch_and_add_arm.S to make it work on armv6 (raspberry pi default).
2020-06-22 14:55:27 +02:00
Christian Jullien
0262f0eb1e macos: fix typo in conftest 2020-06-22 10:19:09 +02:00
Christian Jullien
f84e96fe81 macos: add 'c' option to conftest to get real compiler name and not an alias as with gcc on macOS 2020-06-22 10:12:18 +02:00
Christian Jullien
28d5c5e54f macos: apply readlink to get exact compiler name 2020-06-22 10:11:08 +02:00
Christian Jullien
9ad25d7257 Add clock_nanosleep emulation for macOS
modified:   tests/tests2/114_bound_signal.c
2020-06-22 07:37:54 +02:00
Christian Jullien
925f6704ce Add clock_nanosleep emulation for macOS 2020-06-22 07:36:15 +02:00
Christian Jullien
4d28120e9b macos: test 114 is not supported on macOS and this is not related to old gcc version 2020-06-21 11:49:29 +02:00
Christian Jullien
c69356635c macos: add darwin OS detection. 2020-06-21 10:58:36 +02:00
Christian Jullien
a3d1e9cd3d macos: Reorder C compiler detection so that conftest allows to boostrap tcc with tcc on macos. 2020-06-21 10:52:04 +02:00
Christian Jullien
8d9b83bd11 macOS configure correctly reports clang and version 2020-06-21 09:53:52 +02:00
Michael Matz
ba8980f492 limit gnu_inline hack to macos
some linux programs (kernel) really want to have 'extern inline'
functions be visible from other units, i.e. not be static, but also
mark them as always_inline.  That's -fgnu89-inline semantics, so it's
fine, but we don't (yet) implement this, so we can't make them static
just so.  But we do need this hack on MacOS due to some uses in
system headers (see commit f18f8651).

So, for now conditionalize the hack on Mach-O.
2020-06-21 02:03:46 +02:00
Michael Matz
c883d43ae5 fixes for deadlocks and dollars in asm
* tccelf code doesn't use USING_GLOBALS, hence must be called from
  outside the semaphore
* dollars aren't part of identifiers in asm mode
2020-06-21 01:45:35 +02:00
Michael Matz
18db8da7df Workaround old GCC viz indexed string literals
GCCs before gcc8 didn't deal with these where constants are
required.
2020-06-20 23:17:02 +02:00
Michael Matz
1711eb2ef3 macos: add overview comment 2020-06-20 22:25:08 +02:00
Michael Matz
d174af08f9 macos: Fix memtest
* free allocated memory
* fix weak symbols related to -run
* remove memtest from ignored tests for OSX
2020-06-20 22:17:08 +02:00
Michael Matz
ac86fb50cd macos: Tidy code 2020-06-20 22:17:08 +02:00
Michael Matz
c86760c1e9 macos: Read exported symbols from dylibs
up to now we simply assumed that any undefined symbols will be
provided by some shared libs on the cmdline.  Now we check this.
2020-06-20 22:17:08 +02:00
Michael Matz
fbfe6209be macos: Fix asm-c-connect-test
via some heavy-handed hackery in the ASM symbol handling in case
C symbols get a leading underscore (but ASM symbols do not).
But this is now like clang and GCC on Darwin work: asm symbols are
undecorated, C symbols get a _ prepended, so to connect both some
trickery is involved for the ASM symbols that don't have a _ prepended.
They must be included in the C symbol table (because that's what we use
to lookup also ASM labels), but they also must not disturb the normal
C symbol (which don't have the _ prepended), so they need some mangling.

A bit unsatisfying, but well.  So, add asm-c-connect-test to the working
ones for Darwin as well.
2020-06-20 22:17:08 +02:00
Michael Matz
3cf7bec12f macos: Enable all working tests
all except the below work now on MacOS, also as executable test,
not just with -run:
* dlltest - we don't support dylib generation (yet)
* memtest - tccmacho.c contains some leaks
* asm-c-connect-test - some confusion with underscores still
2020-06-20 22:17:08 +02:00
Michael Matz
f486b19f56 macos: bound_alloca symbol adjust
so that tests/boundtest.c links as well.
2020-06-20 22:17:08 +02:00
Michael Matz
f2e154e1e5 macos: Disable verbose linking output 2020-06-20 22:17:08 +02:00
Michael Matz
9ce5b4a691 macos: Adjust tcctest.c for clang
we need to disable or adjust some tests where clang behaves
slightly different from GCC:
* slight difference in __FILE__ behaviour
* difference (to less useful vs GCC) in computed #include
* difference in __builtin_constant_p
* attribute(weak, alias) isn't supported by clang on MacOS (though
  it could be, as Mach-O has the capabilities for this)
* the built-in assembler of clang is mediocre

this was all checked with
  Apple LLVM version 10.0.1 (clang-1001.0.46.4)
2020-06-20 22:17:08 +02:00
Michael Matz
4eff2b5f6a macos: more adjustments for OSX systems
* instead of /usr/include use the current SDK path as system
  include directory (/usr/include is empty with current tools)
  (this also removes the need to add these paths in individual
  Makefiles)
* define _DARWIN_C_SOURCE in tcc.h to get the full set of decls
  from system headers (e.g. vsnprintf), similar to _GNU_SOURCE
  (and don't define _ANSI_SOURCE in the main Makefile anymore)
* tests/tests2/Makefile: remove the -w flag, it's added when necessary
  in the rules generating the .expect files
2020-06-20 22:17:06 +02:00
Michael Matz
f18f865159 Handle always_inline as GNU inline
this is needed for multi-file testcases using stdio.h, as
the __sputc function is implemented as a extern inline
function (with gnu_inline attribute, but we don't support that for now).

Without this change that leads to multiply defined symbols when using
multiple units including stdio.h.

It also has an always_inline attribute, which we can use to guide our
behaviour, as in ISO-C an always_inline can't be defined with ISO
'extern inline' semantics.  This is the minimal change and not a full
implementation of GNU inline semantics, which would require thorough
testcases.

If __clang__ would be defined the header would make use of C99 semantics,
which would work for us.  It would also do that if _GNUC_ wouldn't be
defined.  But we can't do the latter (as the whole MacOSX SDK refuses
to be compiled with anything not defining that).  I haven't tested
defining __clang__, but suspect that's going to be problematic.
2020-06-20 22:14:56 +02:00
Michael Matz
5d2f4cb403 macos: various fixes
* alloca needs to be _alloca
* int64_t and uint64_t are also defined in <sys/_types/_int64_t.h>
  but as long long, not as long, so adjust out <stddef.h> to agree
  on Darwin
* define __APPLE_CC__ so that <TargetConditionals.h> correctly defines
  various macros like TARGET_OS_MAC and TARGET_CPU_X86_64
2020-06-20 22:14:56 +02:00
Michael Matz
b0b0e48409 macos: Use <dispatch.h> for locking
the global compiler lock can't be a POSIX non-shared semaphore on
Darwin (because not implemented there).  Use the dispatch framework.
2020-06-20 22:14:56 +02:00
Michael Matz
57ba50e611 macos: support bounds checking
* non-process-shared POSIX semaphores aren't supported on
  Darwin, we use the dispatch framework
* dlsym segfaults with RTLD_NEXT from JIT code, so we must not
  even try this for -run.  So we need to know in __bound_init
  if called from -run code, or from normal code, which means passing
  this down also from __bt_init and hence from the stub added in
  tcc_add_btstub
* Darwin uses different structures for <ctype.h> facilities, this
  merely adds a warning about this
* __libc_freeres doesn't exist
* for non -run modus the context (.prog_base member) is constructed
  incorrectly (uses symbol zero for trying to get at the load bias,
  which doesn't really work that way), on Mach-O this errors out
  (and could also error out on ELF).  For now deactivate this, which
  makes backtraces not be symbolic on MacOS for not -run.
2020-06-20 22:14:56 +02:00
Michael Matz
0b3c8360a0 macos: section syms, strtab, runtime
uncovered by the backtrace/boundcheck tests:

* handle STT_SECTION symbols
* call tcc_add_runtime (to get the bcheck.o/bt-exe.o files added)
* add .stab strtab into segments (we should probably add all stab
  syms to the output LC_SYMTAB eventually, but right now TCC uses
  32 bit stabs, while mach-o uses 32/64bit stabs
2020-06-20 22:14:56 +02:00
Michael Matz
91cb41330d macos: Adjust tests2.112 and tests2 Makefile
* <malloc.h> isn't as portable as <stdlib.h>
* skip 113_btdll.c on Darwin
* replace [...]\+ with [...]\{1,\} in the sed regex (basic REs
  have no + even some sed(1) accept it as \+, but bounds _are_ part
  of POSIX BREs)
2020-06-20 22:14:38 +02:00
Michael Matz
71b0634168 Add find_c_sym and friends
for handling leading underscores when looking up symbols.
Necessary on MacOS, as there C symbols have a '_' prepended.
get_sym_addr (replacing get_elf_sym_addr) gets an argument to
specify if bare/raw/ELF symbols should be looked up or if decorated
C symbols should be looked up.  That reflects into tcc_get_symbol.
tcc_add_symbol is _not_ yet changed, but probably should be.
2020-06-20 22:12:02 +02:00
Michael Matz
cc11750bda macos: Support .init_array and .fini_array
called different on Mach-O, but same functionality (list of pointers
to init and fini functions).
2020-06-20 22:12:02 +02:00
Michael Matz
ef730c4d2f macos: don't like in crt startup code
it's not needed with LC_MAIN executables.
2020-06-20 22:12:02 +02:00
Michael Matz
51f15d9971 macos: Deal with leading underscore on Mach-O
all C/C++/ObjC symbols in symbols tables have a leading underscore
in Mach-O.  Within TCC there's some confusion with tcc_add_symbol
(not adding it) and tcc_get_elf_symbol (not expecting it), and
resolve_syms (using dlsym, which doesn't expect it) and -run support.
But this sort of works.
2020-06-20 22:12:02 +02:00
Michael Matz
1ca209dad0 macos: set LC_MAIN entrypoint correctly
to the file offset of 'main' not simply to the start of TEXT.
2020-06-20 22:12:02 +02:00
Michael Matz
84c3fecf5e macos: Support external functions
these are resolved non-lazy for now.  We only need to generate
the jump stub (using the GOT slot that will be initialized due
to the non-lazy pointer marking, like with data symbols).  On
x86-64 we don't even need special marking of these stubs (with
S_SYMBOL_STUBS and associated additional indirect symbol entries),
as that's only used on i386 (where the stubs are self-modifying).

So, this now works:

extern int _printf(const char*, ...);
int _start(void)
{
  _printf("hello\n");
  return 0;
}
2020-06-20 22:12:02 +02:00
Michael Matz
d82e64c163 macos: handle undefined symbols a bit
at least data symbols coming from dylibs can be used now, as in the
below.  Note in the example that optind is defined in libc (really in
libsystem_c.dylib, reexported from libSystem.B.dylib):

static int loc;
extern int _optind;
int _start(void)
{
  _optind = 0;
  loc = 42 + _optind;
  return loc - 42;
}
2020-06-20 22:12:02 +02:00
Michael Matz
fab8787b23 macos: Fix GOT access to local defined symbols
if a GOT slot is required (due to codegen, indicated by
presence of some relocation types), then it needs to contain
the address of the wanted symbol, also when it's local and defined,
i.e. not overridable.  For simplicity we use a GOT slot for that as
well (other storage would require rewriting relocs and symbols,
as resolving of GOT relocs is hardwired to be based on s1->got).
But that means we need to fill in its indirect symbol mapping slot as
well, for which Mach-O provides a mean to say "not symbol based,
resolved locally".  So this fixes this testcase:

static int loc;
int _start(void)
{
  loc = 42;
  return loc - 42;
}

(where our codegen currently uses a GOT-based access for the write
by accident)
2020-06-20 22:12:02 +02:00
Michael Matz
6ebd463021 macos: dynamic symbol table and __got
this now sorts the symbols properly (local, global defined, undefined;
the latter two by name), marks the three ranges within LC_DYSYMTAB,
generates a __got section (non-lazy pointers) and slots for
relocations which need them, and the indirect symbol mapping for
them.

This doesn't yet deal with undefined symbols.  But it means compared to
last example now this also works, i.e. read access to _global_ data:

% cat simple3.c
int loc = 42;
int _start(void)
{
  return loc - 42;
}
2020-06-20 22:12:02 +02:00
Michael Matz
bbccb13566 for_each_element for all; add tcc_exit_state
this moves the for_each_element macro to tcc.h and adds a
tcc_exit_state (and uses it) pairing with tcc_enter_state.
2020-06-20 22:11:57 +02:00
Michael Matz
1320d85742 macos: Create symtab
this creates a proper LC_SYMTAB, with reasonable entries.  It's
not sorted, so not usable for LC_DYSYMTAB.  But 'nm -x -no-sort'
allows to see us some useful info.

This also relocates sections and symbols, so now this example
works as well (i.e. read access to static local data):

% cat simple2.c
static int loc = 42;
int main(void)
{
  return loc - 42;
}
2020-06-20 22:09:21 +02:00
Michael Matz
94066765ed macos: First cut at generating Mach-O executables
this does generate a working executable for a very simple
example input, e.g. this:

% cat simple.c
int main(void)
{
  return 0;
}
% ./tcc -B. -c simple.c
% ./tcc -nostdlib -B. simple.o -lc
% ./a.out && echo okay
okay

(the -lc is actually not necessary right now, see below).  This
has many limitations:

* no symbol table, hence no calls to external functions from
  e.g. libc, aka libSystemB
* no proper entry point (should be main, but is hardcoded to first
  real .text address)
* libSystemB is hardcoded, no other libs are supported (but again
  no external calls anyway)
* generated Mach-O executable is in old format: neither LC_DYLD_INFO
  no export tries for symbols are created (no symbol table at all!)
* the __LINKEDIT segment is faked and empty, as dyld doesn't like
  it empty even if no symbols point into it
* same with __DATA, dyld wants a non-empty writable segment which
  we enforce with useless data
* no relocations, hence no function call stubs (lazy or not) are
  generated
* hardcodes some other constants as well
2020-06-20 22:09:21 +02:00
Michael Matz
b882cad67e macos: Don't try to load dylibs or linker scripts
we ignore dylibs for now (can't inspect them yet for meta-info).
Also don't try to load GNU linker scripts, it's simply an unknown file
type (e.g. when mentioning Mach-O object files).
2020-06-20 22:09:21 +02:00
Michael Matz
9d2ce50d2f macos: Use SDK path for headers
until we have it properly encoded within TCC itself.
2020-06-20 22:09:21 +02:00
Michael Matz
6178e47345 macos: detect clang also when called as gcc 2020-06-20 22:09:21 +02:00
Michael Matz
032664bf7f macos: make Mach-O somewhat ELF_OBJ_ONLY
we only emit .o files in ELF format, but do use the .got building
from generic ELF code also on MacOS for -run.
2020-06-20 22:09:21 +02:00
Michael Matz
c16f5d2fe6 Fake __has_include handling
cctools for MacOS 10.14 (at least) unconditionally uses the
__has_include preprocessor directive (i.e. without checking
  if defined __has_include
as normally suggested for portable code).  So we need to handle
it a little bit.  For now we simply say "nope" aka evaluate to 0.
2020-06-20 22:09:21 +02:00
Michael Matz
34a5658564 macos: Add system include dir for building libs
we use xcrun to determine the correct path.  In the future those
should be determined at configure time or even runtime of tcc itself.
2020-06-20 22:09:17 +02:00
Michael Matz
4cc27b816f macos: Use newer MACOSX_DEPLOYMENT_TARGET
10.6 seems to still be supported on Mojave, but not 10.4
anymore.
2020-06-20 22:07:24 +02:00
Michael Matz
7b6931ed1f Fix some string literal expressions in initializers
Things like 'char x[] = { "foo"[1], 0 }' (which should initialize
a two-character array 'x' with "o\0").  See added testcase.
2020-06-20 22:00:18 +02:00
herman ten brugge
b2d351e0ec Fix fetch_and_add code
Change type from signed char to int.
Make assembly code work with tcc and gcc.
2020-06-18 07:21:48 +02:00
grischka
b5faa45d90 tccpp: faster next()
- call TOK_GET() as a function only for tokens with values
- get rid of 'next_nomacro_spc()'
- be sligtly more efficient in next()

This made about 4-5%+ speed in my tests.

Also: tcc.h: reorder tokens
2020-06-17 18:44:11 +02:00
grischka
781872517d tccpp.c: compile 'tcc_predefs' from string
It might have advantages in cases if tcc/libtcc does not
depend on extern files.

Also:
- apply "stray \\ ..." check to macros only. For files it
  was already checked. Add positive test.
2020-06-17 18:01:45 +02:00
grischka
cbef54653a tcc -MD: drop system includes and duplicates
Also:
- print filenames in errors for binary input files
  (was lost with the thread support recently)
2020-06-17 18:01:40 +02:00
grischka
e7a4140d28 tcc --help -v: cleanup
from e640ed1aeb

Also:
- cleanup -std, -O, -pthread
- tcc.h:win32: use win32-type include paths even for cross
  compilers (needed for loading tcc_predefs.h in cases)
- Makefile: simplify OSX .dylib clause
2020-06-17 17:41:46 +02:00
herman ten brugge
8fb8d88ea6 Add vla bound support for arm,arm64 and riscv64
tcctok.h:
- Add __bound_new_region

arm-gen.c arm64-gen.c riscv64-gen.c:
- Add bound checking support to gen_vla_alloc

tests/Makefile boundtest.c
- Add test18 vla bound checking test
2020-06-17 11:24:17 +02:00
herman ten brugge
44019e874f Fix debug info for functions
Add code for VT_FUNC.
Use octal number for unsigned int/unsigned long for 32 bits targets.
Add VT_BYTE | VT_UNSIGNED for targets with default unsigned signed char.
Remove extra ',' in default_debug struct.
2020-06-17 07:58:18 +02:00
herman ten brugge
0b8ee7364a Add bound checking to arm, arm64 and riscv64
Checked on:
- i386/x86_64 (linux/windows)
- arm/arm64 (rapberry pi)
- riscv64 (simulator)
Not tested for arm softfloat because raspberry pi does not support it.

Modifications:

Makefile:
  add arm-asm.c to arm64_FILES
  add riscv64-asm.c (new file) to riscv64_FILES

lib/Makefile:
  add fetch_and_add_arm.o(new file) to ARM_O
  add fetch_and_add_arm64.o(new file) to ARM64_O
  add fetch_and_add_riscv64.o(new file) to RISCV64_O
  add $(BCHECK_O) to OBJ-arm/OBJ-arm64/OBJ-riscv64

tcc.h:
  Enable CONFIG_TCC_BCHECK for arm32/arm64/riscv64
  Add arm-asm.c, riscv64-asm.c

tcctok.h:
  for arm use memmove4 instead of memcpy4
  for arm use memmove8 instead of memcpy8

tccgen.c:
  put_extern_sym2: for arm check memcpy/memmove/memset/memmove4/memmove8
                   only use alloca for i386/x86_64
  for arm use memmove4 instead of memcpy4
  for arm use memmove8 instead of memcpy8
  fix builtin_frame_address/builtin_return_address for arm/riscv64

tccrun.c:
  Add riscv64 support
  fix rt_getcontext/rt_get_caller_pc for arm

tccelf.c:
  tcc_load_dll: Print filename for bad architecture

libtcc.c:
  add arm-asm.c/riscv64-asm.c

tcc-doc.texi:
  Add arm, arm64, riscv64 support for bound checking

lib/bcheck.c:
  add __bound___aeabi_memcpy/__bound___aeabi_memmove
      __bound___aeabi_memmove4/__bound___aeabi_memmove8
      __bound___aeabi_memset for arm
  call fetch_and_add_arm/fetch_and_add_arm64/fetch_and_add_riscv64
  __bound_init: Fix type for start/end/ad
  __bound_malloc/__bound_memalign/__bound_realloc/__bound_calloc: Use size + 1

arm-gen.c:
  add bound checking code like i386/x86_64
  assign_regs: only malloc if nb_args != 0
  gen_opi/gen_opf: Fix reload problems

arm-link.c:
  relocate_plt: Fix address calculating

arm64-gen.c:
  add bound checking code like i386/x86_64
  load/store: remove VT_BOUNDED from sv->r
  arm64_hfa_aux/arm64_hfa_aux: Fix array code
  gfunc_prolog: only malloc if n != 0

arm64-link.c:
  code_reloc/gotplt_entry_type/relocate: add R_AARCH64_LDST64_ABS_LO12_NC
  relocate: Use addXXle instead of writeXXle

riscv64-gen.c:
  add bound checking code like i386/x86_64
  add NB_ASM_REGS/CONFIG_TCC_ASM

riscv64-link.c:
  relocate: Use addXXle instead of writeXXle

i386-gen.c/x86_64-gen.c
  gen_bounds_epilog: Fix code (unrelated)

tests/Makefile:
  add $(BTESTS) for arm/arm64/riscv64

tests/tests2/Makefile:
  Use 85 only on i386/x86_64 because of asm code
  Use 113 only on i386/x86_64 because of DLL code
  Add 112/114/115/116 for arm/arm64/riscv64
  Fix FILTER (failed on riscv64)

tests/boundtest.c:
  Only use alloca for i386/x86_64
2020-06-16 07:39:48 +02:00
Michael Matz
9eef33993a Fix type compatiblity of enums and ints
an enum must be compatible with one or more integer type,
so adjust the test accordingly.  That means the following
redeclarations should work:

  enum e6 { E1 = -1, E0 };
  void f3(enum e6);
  void f3(int);        // should work as int and e6 are compatible

while the following should not:

  void f4(enum e6 e);
  void f4(unsigned e); // should error as unsigned and e6 are incompatible
2020-06-05 16:02:08 +02:00
Michael Matz
068d5b3d20 pp: Move errors about stray backslashes
they were emitted too early, in particular also in macro
substitution which might turn out to not be stray in case it's
further stringified.  Check in next() instead.  Add two testcases
that an error is still emitted for obvious top-level baskslashes,
and that stringifying such a thing works.
2020-06-03 18:38:11 +02:00
Christian Jullien
e640ed1aeb Also support --help -v, i.e. arguments in different order. 2020-06-01 18:01:12 +02:00
Christian Jullien
bbb3f79bb8 Missied a commit in order to support -v --help the same way as gcc does 2020-06-01 17:23:20 +02:00
Christian Jullien
47363cbb97 Support -v --help the same way as gcc does 2020-06-01 17:11:11 +02:00
Michael Matz
ae14ef5426 riscv64: Fix printf warning
now that tcc_error also checks arguments.
2020-05-26 20:17:39 +02:00
herman ten brugge
3b617fdc53 Add sigsetjmp/siglongjmp bound checking support
tcctok.h:
- Add sigsetjmp/__sigsetjmp/siglongjmp

tccgen.c:
- redirect sigsetjmp/siglongjmp to bcheck.c code

i386-gen.c/x86_64-gen.c
- gcall_or_jmp: Set func_bound_add_epilog also when sigsetjmp is called
- gen_bounds_epilog: Only call __bound_local_new when needed (unrelated)

bcheck.c:
- Add __bound_siglongjmp
- __bound_setjmp/__bound_long_jump: Check no_checking
- Optimize __bound_local_delete (unrelated)

Modify testcase:
- 114_bound_signal
2020-05-25 12:26:55 +02:00
herman ten brugge
045632defb Fix gcc10 warnings
i386-gen.c:
- load/gen_opf: set v1.sym to NULL

lib/Makefile:
- Add -gstabs -fno-omit-frame-pointer -Wno-unused-function -Wno-unused-variable

lib/bt-log.c:
- tcc_backtrace: Prevent __builtin_frame_address warning

tccgen.c:
- struct_layout: Set t.t to VT_BYTE
- default_debug: Use octal instead of -1 to make size_t work

tccpp.c:
- tal_realloc_impl: Only memcpy when p set

x86_64-gen.c:
- gen_bounds_epilog: Do not save/restore rcx (not caller/callee saved)
                     This also made stack not aligned to 16 bytes.
2020-05-23 20:27:43 +02:00
herman ten brugge
b5b92c7d6d Add setjmp/longjmp bound checking support
tcctok.h:
- Add __bound_setjmp/setjmp/_setjmp/longjmp

tccgen.c:
- redirect setjmp/longjmp to bcheck.c code

i386-gen.c/x86_64-gen.c
- Change func_bound_alloca_used into func_bound_add_epilog
- Set func_bound_add_epilog also when setjmp is called

bcheck.c:
- Add __bound_setjmp/__bound_longjmp
- __bound_local_delete: remove setjmp if used in function
- __bound_exit: clear setjmp list and print statistic
- make malloc_redir more readable (unrelated)

New testcases:
- 115_bound_setjmp
- 116_bound_setjmp2
2020-05-23 20:02:41 +02:00
grischka
4429cef9f6 tccgen.c: merge more function attributes
Merge function attributes with those given given for the
prototype, also handle post-decl appearance such as

    void func() __attribute__((noreturn))
    {
    }

Also, some test fixes (unrelated).
2020-05-13 11:39:39 +02:00
grischka
7bb5454ef3 make SILENT=yes
This allows to run make more silently.

Also, turns off debug info for binaries by default,
but adds a switch to get it back easily

    ./configure --debug
2020-05-12 20:35:43 +02:00
Christian Jullien
92236bfe1d Rework help messages: split --version -v and -vv, display -m32/64 only when available on current plateform. 2020-05-11 16:23:48 +02:00
grischka
9c28349757 tccgen.c: cleanup debug support
from 3e731e3a78

tccgen.c:
- make 'struct default_debug' const
- pass TCCState* as parameter to tcc_debug_xxx functions
- always check tcc_state->do_debug before calling functions
- factor out tcc_debug_extern_sym()
- remove formats "%lld"/"%llu" (not reliable on windows)

xxx-gen files:
- set func_vt/var from caller
2020-05-11 11:41:56 +02:00
grischka
5bc1720776 tccgen.c: move 'alloca_used' complication to *-gen files
related to commit 8370bc03a1
2020-05-11 11:41:56 +02:00
Michael Matz
8de7c092f0 Support --version cmdline arg
for simplicity handles like -v (verbose), but enables the usual idiom
of configure scripts to use '$CC --version' to find out the compiler
variant and version.
2020-05-10 22:47:53 +02:00
herman ten brugge
29ba50da29 Fix some printf like functions 2020-05-05 09:00:24 +02:00
herman ten brugge
8370bc03a1 Allow signal handlers when bound checking
Disable generating bound_local_new/bound_local_delete when not needed.
Add new testcase 114_bound_signal.
2020-05-05 08:31:57 +02:00
herman ten brugge
973a14bb2f Fix symbolic debugging for -g -dt -run 2020-05-04 08:13:41 +02:00
herman ten brugge
3e731e3a78 Add symbolic debug support
This allows debugging with variables/structs/unions/enums/bitfields.

Add new functions:
- tcc_debug_stabs: store stabs debug info
- tcc_debug_stabn: store stabn debug info
- tcc_get_debug_info: generate stabs debug info
- tcc_debug_finish: store debug info in debug section
- tcc_add_debug_info: Add function debug info

Update functions:
- tcc_debug_end: free debug hash
- tcc_debug_funcstart: Generate correct function debug info
- tcc_debug_funcend: Finish debug info
- put_extern_sym2: Generate debug info for symbols
- pop_local_syms: Add debug info
- prev_scope: Add local symbols
2020-05-03 11:59:57 +02:00
Michael Matz
fb1fb8219c riscv64: fcvt.d.s doesn't need a rounding mode
it doesn't round so the RM field can be zero.  According to some
sourcs it should be set to zero by software in these cases, and
the binutils disassembler doesn't like us setting it to 7.

This shouldn't matter in practice, but who knows.
2020-04-16 00:02:32 +02:00
Michael Matz
2f94390223 musl: disable boundcheck tests
for now that doesn't work with musl libc.
2020-04-15 22:08:18 +02:00
Michael Matz
245f6a0d13 stdarg: always have the __builtin_va_* available
This makes available the __builtin_va_list type and __builtin variants
of va_start, va_arg, va_copy and va_end.  We do this via a header file
that's prepended to all compilations always (except if merely
preprocessing): tcc_predefs.h.  That header could also be used
for predefining other builtins in the future.

We don't need the define hacks for musl anymore with this.

Also fix x86_64 gfunc_prologue to reserve enoug space for the
full va_list structure, not just 16 bytes.
2020-04-15 22:06:52 +02:00
Michael Matz
8c6143d86f Fix stdarg on x86-64
this partly reverts 1803762e3 to fix stdarg on x86-64 again.  I've tried
to retain the apple specific changes from that commit.
Also include stdarg.h first in tcc.h, maybe that helps as well.
2020-04-15 04:49:34 +02:00
Robert Hoelzl
1803762e3f Make tcclib1.a compile on macOS again
When compiling on macOS (at least in version 10.12) the TCC compiler failed
to compile libtcc1.a. Three problems were solved:
 - The predefined macro "__APPLE__" is now available, as it is tested in the
   libc darwin header files
 - the libtcc1 Makefile defined _ANSI_SOURCE, although it used signals
 - stdargs.h defined va_list differently from the darwin libc.
   If the darwin standard library was included BEFORE stdargs this caused
   problems.
 - the darwin libc generated a warning if GCC < 4 was used
 - additional defines are predefined now to make darwin libc headers compile.
2020-04-15 02:56:24 +02:00
Michael Matz
38ab621b55 Factor out common type combination
as there's overlap between handling types for binary and ternay
operations.  Factor this into a single routine (combine_types).
This uses the structure that gen_op was following, and expr_cond
was using as well in the past, which I find easier to reconvene
with the standard language.  But it reuses the new functions for
diagnostics to improve (a little) on what GCC or clang produce :)
2020-04-15 02:44:12 +02:00
Michael Matz
c085645f98 Expect c99 support in tcctest.c
the support for the macro GCC_MAJOR is gone since 2017, and it's
fairly doubtful that anyone serious is using gcc 2.95.

Also adds a test for the ternary ops typing rules: 'x?bool:bool' has
to promote to int.
2020-04-15 02:28:23 +02:00
Michael Matz
00fbf65524 Move type_incompatibility_error earlier
also move type_incompatibility_warning and type_to_str.
2020-04-15 02:11:05 +02:00
Michael Matz
096c93c0c6 Fix interaction of (local) labels and stmt exprs
as per testcase.  We must not reset token.sym_label twice with
kept symbols.  This is no problem for non-label symbols because those
aren't generated on demand when mentioning them.
2020-04-14 22:43:13 +02:00
grischka
6696da2f61 win32: long double as distinct C-type
On windows. there is no long double really IOW it is the
same as double.  However setting the VT_LONG flag in
combination with VT_DOUBLE allows to keep track of the
original type for the purpose of '_Generic() or more
accurate type warnings.
2020-04-11 22:03:09 +02:00
grischka
d019586378 win32/include/math.h: rint/trunc: pop fp stack
... in order to avoid fp stack overflow (see test below).

#include <math.h>
#include <stdio.h>
int main()
{
   printf("%f %f %f %f\n", trunc(1.2), rint(1.2), trunc(1.2), rint(1.2));
   printf("%f %f %f %f\n", trunc(1.2), rint(1.2), trunc(1.2), rint(1.2));
   printf("%f %f %f %f\n", trunc(1.2), rint(1.2), trunc(1.2), rintl(1.2));
}

Also in rintl:
- 'long double' is not a ten-byte float on windows.
2020-04-11 21:45:57 +02:00
Tyge
024214af2d Finalizing math.h fixes:
- Updated msvcrt.def with symbols from 64bit version of dll - it contains the float math functions missing in the 32bit dll.
- Made sure this patch only apply to to WIN32 and WIN64. For WIN32 float functions calls the double variants, on 64bit they are called natively.
2020-04-11 02:29:51 +02:00
Tyge
df67d8617b Minimum changes to make all float variants of math.h functions work,
by calling the double functions and removing unsupported __asm implementations using "=t".
This patch also enables the double version of logb(double).

Note: None of the 'long double' variants works though - could easily be fixed
by aliasing the double equvalents for completeness/compability.
2020-04-07 20:18:51 +02:00
wanjochan
c386ca91c6 OSX: libtcc.dylib Makefile fix 2020-03-22 20:36:14 +08:00
Christian Jullien
5ade19c421 Add a test function which verifies we can pass a struct ptr to va_arg for all supported architectures. The bug has been identify by trying to compile FFTW-3.3.8. 2020-03-22 08:40:35 +01:00
Christian Jullien
ec0e93616f Fix arm64-gen.c when passing a struct ptr to va_arg. 2020-03-22 08:26:03 +01:00
wanjochan
6fa78a3635 OSX: make libtcc.dylib 2020-03-21 23:57:00 +08:00
wanjochan
4caa9a4cc7 relocate_syms() should using !(nostdlib) for parameter "int do_resolve" 2020-03-14 16:17:39 +08:00
matthias
704b602184 instruduce C2x _Static_assert syntaxe 2020-03-04 11:35:34 +01:00
matthias
cb041f11f6 improve _Static_assert
Fix static assert to support literal string instead of just printing
the sring of the current token as it use to be

so we can now use _Static_assert(0, "0" "1") which will print
__FILE__ __LINE__ error: 01
2020-03-04 11:35:34 +01:00
Sizhe Zhao
e050ae845e win32/build-tcc.bat: clean .o in win32/lib
commit ef42295fe8 intrduced 4 .o files to
win32/lib. But they (bcheck.o, bt-dll.o, bt-exe.o and bt-log.o) are not
removed by "win32/build-tcc.bat -clean". This commit should fix it.
2020-02-19 00:43:41 +01:00
Udo
923100c498 Better follow spacing style. (only changed formatting) 2020-02-18 21:11:49 +01:00
Udo
9272fac7c4 rework type coercion in ternary expr (a bit) and uncomment previously failing test. Be more explicit in diagnostic messages. 2020-02-17 18:25:43 +01:00
Udo
a9e7fe19c7 extend test 03_struct for attr. __cleanup__ 2020-02-09 18:31:19 +01:00
Udo
89b3cf0b87 warn if attr. __cleanup__ is given in type decl. (Allow this as an extension?) 2020-02-09 18:21:59 +01:00
Udo
c092f2ed61 Check if symbol given for attr. cleanup is actually a function 2020-02-07 23:23:31 +01:00
grischka
7e901299bf Rework expr_infix
- revert const-folding in gvtst() and put it back into
  expr_landor().  Although it did make sense, one reason
  not to do it is __builtin_constant_p() which may return
  true when it shouldn't because of nocode_wanted, see test.

- tccgen_init() can do init_prec(), also for tcc -E.

- for nostalgic reasons, keep the original expression parser
  functions in the source.

- Makefile: remove stale stuff
2020-01-22 21:57:19 +01:00
Michael Matz
aeac24de98 Rework expr_landor
so that it also is called from the precedence parser.  This
is complicated by the fact that something needs to be done before
the second operand is parsed in a single pass compiler, so it
doesn't quite fit into expr_infix itself.  It turns out the smallest
code changes result when expr_landor remains separate.  But it can
be tidied a bit.
2020-01-20 05:48:48 +01:00
Michael Matz
23a8bac7b5 Use precedence parser for expressions
This is smaller and uses less stack depth per expression (eight function
calls from expr_or to get down to a unary).  It's a tiny bit faster
depending on how good the branch predictor is, on my machine a wash.
2020-01-20 05:48:48 +01:00
Michael Matz
fdeeb62e28 Fix symbol versions with shared libs
ELF files that refer to shared libs containing sym-versions, but
don't refer to any dynamic symbols with symbol versions (should happen
only with very simple shared libs) would generate an empty .gnu.version_r
section.  Some dynamic linker contain bugs in that they don't check
the section size or DT_VERNEEDNUM (which are both zero for such files
we generate) before accessing the first entry, and then bail out with
a message like

./a.exe: error while loading shared libraries: ./a1.so: unsupported
 version 25960 of Verneed record

(where the "version number" actually comes from neighboring bytes
from different sections).

So, there's not much choice, we simply must not generate such section.
2020-01-20 05:31:09 +01:00
grischka
d79e1dee8c backtrace: test with DLLs
- tests2/113_btdll.c: test handling multiple stabs infos
Also:
- libtcc.c: remove _ISOC99_SOURCE pre-defines.  It is causing
  strange warnings such as 'strdup not declared'

- i386/x86_64-gen.c cleanup bounds_pro/epilog.  This discards
  the extra code for main's argv.  If needed, __argv might be
  processed instead.

- tccgen.c:block(): reduce stackspace usage.  For example with
  code like "if (..) ... else if (..) ... else if (..)... "
  considerable numbers of nested block() calls may occur.

  Before that most stack space used when compiling itself was
  for libtcc.c:tcc_set_linker().

  Now it's rather this construct at tccpp.c:2765: in next_nomacro1():

  if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
        || c == '.'
        || ((c == '+' || c == '-')
        ...
2020-01-19 11:46:07 +01:00
Michael Matz
a5f6e6189e Make 112_backtrace/bcheck_123 more robust
the strcat checker first checks dest for overlap, then src.
If the padding byte between b[] and the pad[] arrays happens to be zero
the dest check would have succeeded and the src test failed.  If that
padding byte would be zero the dest check would trigger first.
As we can't influence the padding byte (only the b[] and pad[] arrays)
it was random if the dest or src checks triggered.

This makes it reliably trigger the dest check first.
2020-01-18 03:06:44 +01:00
grischka
2b7cffac74 fixes for previous commit 2020-01-18 01:22:28 +01:00
gr
ef42295fe8 tccrun.c: standalone backtraces with -bt[N] or -b
This makes it possible to get backtraces with executables
(including DLLs/SOs) like we had it already with -g -run.

Option -b includes -bt, and -bt includes -g.

- new file lib/bt-exe.c: used to link rt_printline and the
  exception handler from tccrun.c into executables/DLLs.

- new file lib/bt-log.c: provides a function that may be
  called from user code to print out a backtrace with a
  message (currently for i386/x86_64 only):

     int (*tcc_backtrace)(const char *fmt, ...);

  As an extra hack, if 'fmt' is prefixed like "^file.c^..."
  then the backtrace will skip calls from within 'file.c'.

- new file lib/bt-dll.c:  used on win32 to link the backtrace
  and bcheck functions with the main module at runtime

- bcheck.c: now uses the tcc_backtrace function from above

- tccgen.c: minor cleanups

- tccelf.c: stab sections get SHF_ALLOC for easy access.
  Also in relocate_section(): 64bit relocations for stabs
  in DLLs cannot work.  To find DLL addresses, the DLL base
  is added manually in tccrun.c via rc.prog_base instead.

- tccpe.c: there are some changes to allow merging sections,
  used to merge .finit_array into .data in the first place.

- tccpp.c: tcc -run now #defines __TCC_RUN__
  also: refactor a line in tal_realloc that was incompatible
  with bcheck

- tcctest.c: fixed a problem with r12 which tcc cannot preserve
  as well as gcc does.

- tests2/112_backtrace.c: test the feature and the bcheck test18
  that previously was in boundtest.c
2020-01-17 22:58:39 +01:00
herman ten brugge
4092b05068 Exclude ellipsis functions from bounds checking.
This fails on i386.
2020-01-16 09:40:33 +01:00
Christian Jullien
c2976962da Merge branch 'mob' of git://repo.or.cz/tinycc into mypatch 2020-01-16 09:00:26 +01:00
Christian Jullien
c7e3d5d7a3 Add _ISOCxx_SOURCE glibc compatible macros. 2020-01-16 08:58:43 +01:00
herman ten brugge
0d7c40b948 Call pop_local_syms before gfunc_epilog 2020-01-16 08:24:17 +01:00
Michael Matz
65f2fe390c Cleanup new bound checking code
remove quadratic loops by not using side tables; address-taken
can simply be a flag per local sym, and the lbounds section can
be filled after symbols go out of scope at which point we know
if the address was taken, so that there's no need to compress it
again after the funcion is done.
2020-01-16 01:19:59 +01:00
Michael Matz
4a70b2bc2d Fix handling of unevaluated subexpression of const
we were emitting error messages for something like
'static int i = 2 || 1/0', even though the exception would be in
the unevaluated part.  This doesn't destroy const-ness, so we must
accept it.  This requires splitting the nocode_wanted values a bit more,
so that nocode_wanted due to const_wanted can be differentiated from
nocode_wanted due to non-evaluation.
2020-01-15 23:32:40 +01:00
herman ten brugge
3877618785 Update bound checking code.
Add __attribute__((constructor)) to __bounds_init.
- remove tcc_add_bcheck from i386-link.c and x86_64-link.c
- add simplified tcc_add_bcheck to tccelf.c
- Update tccrun.c to call constructor/destructor.
Set dynsym sh_info to number of local symbols in tccelf.c
Reduce stack size when bounds checking is enabled.
Added variable TCC_LIBBCHECK for windows support.
Add signal stack to detect stack overflow.
Add all & parameters in lbound_section and remove them if not used.
Close fd in tcc_relocate in tccrun.c
Fix section type constructor/destructor in tccelf.c
Add check code in tests/boundtest.c for mem/str functions.
Remove -ba from documentation.
Add bounds check signal info in documentation.

bcheck.c:
- Fix initial_pool alignment.
. Fix printf statements.
. Add prototypes for all external interface functions.
- Add TCC_BOUNDS_WARN_POINTER_ADD environment variable.
. Add ctype and errno data.
- Fix alloca when multithreading is used.
- Add lock for __bound_checking and __bound_never_fatal.
- Catch pthread_create and use locks when called.
- Detect in loaded in shared lib and use locks when found
- Use spin locks instead of semaphore locks.
- Make spin locked code as small as possible.
- Fix mem/str functions checking.
- Fix overlap checking mem/str functions.
2020-01-15 08:53:19 +01:00
Christian Jullien
0d6801b130 C11, section 7.2: The macro static_assert expands to _Static_assert. This macro was missing on Windows 2020-01-14 07:41:58 +01:00
herman ten brugge
269042503e Fix sellinux pointer code in tccrun.
Free all memory on exit in __bound_exit.
2019-12-23 20:23:18 +01:00
Michael Matz
f150f93854 Fix the get_asm_string fail on i386 in another way
the problem is that new debian GCC enabled -fPIC or -fPIE by
default, causing the mentioned compile error.
2019-12-17 17:54:54 +01:00
Giovanni Mascellani
43fb5a72e7
tcctest: explain why i386 is not tested 2019-12-17 12:36:02 +01:00
Michael Matz
1d4d74d6d0 Use explicit signed char in char_short_test
as 'char' is unsigned on some architectures, so the tests weren't
testing what they were supposed to.
2019-12-17 04:56:54 +01:00
Michael Matz
c8ca64d28b Adjust return value promotion for some archs
this is a bit complicated: for i386 and x86-64 we really need to
extend return values ourself, as the common code now does.  For arm64
this at least preserves old behaviour.  For riscv64 we don't have to
extend ourself but can expect things to be extended up to int (this
matters for var-args tests, when the sign-extension to int64 needs to
happen explicitely).  As the extensions are useless, don't do them.

And for arm32 we actually can't express GCC behaviour: the callee side
expects the return value to be correctly extended to int32, but
remembers the original type.  In case the ultimate target type for the
call result is only int, no further extension is done.  But in case
the target type is e.g. int64 an extension happens, but not from int32
but from the original type.  We don't know the ultimate target type,
so we have to choose a type to put into vtop:
* original type (plus VT_MUSTCAST) - this looses when the ultimate
  target is int (GCC: no cast, TCC: a cast)
* int (without MUSTCAST) - this looses when the ultimate target is
  int64 (GCC: cast from original type, TCC: cast from int)
This difference can only be seen with undefined sources, like the
testcases, so it doesn't seem worthwhile to try an make it work, just
disable the test on arm and choose the second variant as that generates
less code.
2019-12-17 01:46:06 +01:00
Michael Matz
2d17e5a6c4 riscv64: adjust for cast changes
gfunc_call plays with types and needs to retain the unsignedness
now (this was a latent problem before commit 35475b5).
2019-12-17 00:48:57 +01:00
grischka
d30d68d738 #pragma comment(option, "file.c")
This allows adding files or libraries from
   #pragma comment(option, ...)

Also, {f}/file.c will be expanded with the directory of
the current source, that is the file that has the #pragma
2019-12-16 22:48:31 +01:00
grischka
a64353ce71 tccgen.c: generic char/short promotion for function return values 2019-12-16 21:58:32 +01:00
grischka
89372dc482 update gen_cast 2019-12-16 21:37:44 +01:00
grischka
35475b5423 remove VT_LVAL_BYTE etc.
For some reason there was no point for that anymore.
2019-12-16 20:59:23 +01:00
grischka
5914f4d57d tccgen.c: cleanup reg classes
wrap some copy&paste code into functions
2019-12-16 20:44:48 +01:00
grischka
ff3b5ee91c -bench fix
Put total_lines etc. into TCCState.  Also, initialize
the predefined compiler types for the preprocessor too.
tccpe.c: fix BaseOfCode if .init section present (with tcc -b)
2019-12-16 20:17:10 +01:00
Michael Matz
b476a5f478 Readd lost error on local static function decls
see testcase: block scope decls for functions can't use static
(allowed is only extern or none).  This got lost in commit 85690480.
2019-12-16 07:00:26 +01:00
Michael Matz
6cb68c3a17 arm64: fix some casts
in the new common backtrace/context code.
2019-12-15 00:05:30 +01:00
Michael Matz
2fb79a6326 Fix some races
protect some more accesses to global data with the semaphore.
(And for the testcase: don't just write into global data, use local
copies; it's not important for speed here).
2019-12-14 23:58:45 +01:00
grischka
65f74a4df0 tccrun.c: better stab debug support
* a major revision of the rt_printline() feature in
  tccrun.c to report file:linenumber more correctly.

* minor changes to the stab info produced by the
  compiler in tccgen.c

However stab addresses are limited to 32 bits.  I added
a work around:

    if (sizeof pc == 8)
        pc |= wanted_pc & 0xffffffff00000000ULL;

However GDB has problems with that too.
2019-12-14 17:48:50 +01:00
grischka
56db092ab7 bcheck cleanup
- revert Makefiles to state before last bcheck additions
  Instead, just load bcheck.o explicitly if that is
  what is wanted.

- move tcc_add_bcheck() to the <target>-link.c files and
  remove revently added arguments.  This function is to
  support tccelf.c with linking, not for tccgen.c to
  support compilation.

- remove -ba option:  It said:
  "-ba  Enable better address checking with bounds checker"
  Okay, if it is better then to have it is not an option.

- remove va_copy. It is C99 and we try to stay C89 in tinycc
  when possible.  For example, MS compilers do not have va_copy.

- win64: revert any 'fixes' to alloca
  It was correct as it was before, except for bound_checking
  where it was not implemented.  This should now work too.

- remove parasitic filename:linenum features
  Such feature is already present with rt_printline in
  tccrun.c.  If it doesn't work it can be fixed.

- revert changes to gen_bounded_ptr_add()
  gen_bounded_ptr_add() was working as it should before
  (mostly).  For the sake of simplicity I switched it to
  CDECL.  Anyway, FASTCALL means SLOWCALL with tinycc.

In exchange you get one addition which is required for
bounds_cnecking function arguments.  The important thing
is to check them *BEFORE* they are loaded into registers.
New function gbound_args() does that.

In any case, code instrumentation with the bounds-check
functions as such now seems to work flawlessly again,
which means when they are inserted as NOPs, any code that
tcc can compile, seems to behave just the same as without
them.

What these functions then do when fully enabled, is a
differnt story.  I did not touch this.
2019-12-14 13:26:18 +01:00
herman ten brugge
a86f47889c Fix debug info 2019-12-13 15:07:02 +01:00
herman ten brugge
87639aae7c Add linenumber filename support for bounds checking. 2019-12-13 13:45:09 +01:00
herman ten brugge
56e70bfa31 Fix bounds checking new functions.
There was a problem with strncpy and strncmp.
Made bound_ptr_add and bound_ptr_indir a little bit faster.
Fix statistic counter types. Change long into long long.
2019-12-13 10:02:20 +01:00
herman ten brugge
39c0ff311d Add new bounds checking functions.
The following functions are now also bounds checked:
memcmp, strncpy, strcmp, strncmp, strcat, strchr, strdup.

Add statistics code for bounds checking functions.
The statistics can be printed by settings environment variable
"TCC_BOUNDS_PRINT_STATISTIC".

Enabled more tests in test/Makefile.
2019-12-12 20:49:35 +01:00
herman ten brugge
35512be1ee Fix typo with -ba option 2019-12-12 14:21:07 +01:00
herman ten brugge
75145ddc1a Add -ba option for bounds_checking 2019-12-12 13:29:45 +01:00
herman ten brugge
4a2e33d160 Update bounds checking.
The bounds checking code has now enabled gen_bounded_ptr_add tests.
This makes the code slower but finds more errors.
I had to correct some things in tcc to make it work.
- Fixed off by one in lib/bcheck.c
- Corrected tccelf.c sym_versions.
- Disabled USE_TAL when using bounds checking.
- Fixed cstr_printf va_start.
- Fixed tests/tests2/46_grep.c off by one error.
- Updated gen_bounded_ptr_add in x86_64-gen.c
- Fixed x86_64-link.c pointer diff.
For gen_vla_alloc now always use alloca call when bounds checking.
Added line/filename in %rax before bound calls to find location of error.
2019-12-12 12:56:06 +01:00
Michael Matz
ce4eafb34f Include some headers in new multi-thread test
for usleep and gettimeofday.
2019-12-12 03:31:21 +01:00
Michael Matz
11fc58fa13 Fix parallel make targets
my last patch here fixed a missing dependency, but in the wrong
way.  It would build some object files twice, which could race.
The lib/all target already builds both libtcc{,b}1.a libs, so
a simple dependency is enough.
2019-12-12 02:00:13 +01:00
Michael Matz
90e09ed75f Disable thumb code generation on tcctest.gcc
this breaks getting the alignment of functions via bit masking
which we assume to work in one test.
2019-12-12 01:52:50 +01:00
Michael Matz
83f822d0cd Also parse -Dfoo in libtcc_test_mt
otherwise the -DTCC_ARM_VFP doesn't get through.
2019-12-12 01:39:58 +01:00
Michael Matz
8b23662d53 Fix libtcc_test_mt with lib64
we need to pass NATIVE_DEFINES, and we need to actually parse
and pass them into the state.
2019-12-11 17:57:43 +01:00
herman ten brugge
b3893baa45 Fix3 alloca on x86_64 windows 2019-12-11 14:26:27 +01:00
herman ten brugge
b45a8d456d Fix2 alloca on x86_64 windows 2019-12-11 14:22:01 +01:00
herman ten brugge
426d32af23 Fix alloca on x86_64 windows 2019-12-11 14:06:15 +01:00
herman ten brugge
a0bc149b0c Fix bounds checking after concurrently commit 2019-12-11 12:07:48 +01:00
grischka
72729d8e36 allow libtcc states to be used concurrently
This allows creation of TCCStates and operation with API
calls independently from each other, even from threads.

Frontend (option parsing/libtcc.c) and backend (linker/tccelf.c)
now depend only on the TCCState (s1) argument.

Compilation per se (tccpp.c, tccgen.c) is still using
globals for convenience.  There is only one entry point
to this section which is tcc_compile() which is protected
by a semaphore.

There are some hacks involved to avoid too many changes,
as well as some changes in order to avoid too many hacks ;)

The test libtcc_test_mt.c shows the feature.  Except this
new file the patch adds 87 lines overall.
2019-12-11 02:36:19 +01:00
grischka
6082dd62bb tcc -vv ... : show objects loaded from archives 2019-12-10 20:55:57 +01:00
grischka
df349ddc43 versym cleanup
get rid of some globals, in the first place.  Also, for the
PE target, ifdef out ELF executable creation.
2019-12-10 20:41:35 +01:00
herman ten brugge
4d297d354c Add bound checking print heap 2019-12-10 19:47:33 +01:00
Michael Matz
fb22e0c12d Fix type/r/r2 confusion differently
on i386 111_conversion.c breaks when save_reg_upstack isn't careful
about r2 and type mismatches.  The bcheck patches fixed this by
enlarging the stack slot beyond the natural type, this variant simply
avoids saving the second register is the type indicates that it isn't
needed.

Adds also a comment how this should ideally work, namely that type
and r/r2 entries in the vstack are consistent.  In the 111_conversion
testcase it's specifically gen_cast via gen_cvt_ftoi that breaks
this, but there more general code broken as well, so that would deserve
a careful fixup based on some additional asserts.
2019-12-10 17:49:04 +01:00
Michael Matz
ac2a61d1b3 Add dependency on libtccb1.a
otherwise parallel makes might throw errors.
2019-12-10 17:48:23 +01:00
Michael Matz
353db307ed Fix compile warning with cross compilers
'make i386-tcc' on a x86-64 host gave a warning about the
unconditionally declared static tcc_add_bcheck function.
2019-12-10 16:56:10 +01:00
herman ten brugge
88dd577302 Disable bounds_check1_test on arm 2019-12-10 09:39:45 +01:00
herman ten brugge
17850a51f5 Fix bounds checking on targets with no bcheck.c 2019-12-10 08:44:44 +01:00
herman ten brugge
4461f38a9e Fix bounds checking for linux/windows 2019-12-10 08:07:25 +01:00
Michael Matz
474f95dda8 Fix setting of options
now that the type of boolean options is uchar set_flag() needs
an accompanying change.
2019-12-05 17:04:04 +01:00
Michael Matz
1dc92f8ff8 Fix indentation and run_test type
tcc_state.run_test is used as counter, not flag, so int is appropriate.
Also fix indentation garbled by last patch.
2019-12-02 19:20:56 +01:00
Sergey Sushilin
4873f6fada use uchar instead of int to save memory 2019-12-01 23:22:57 +03:00
Sergey Sushilin
14fc6b6dcb use size_t instead of int when work with strings 2019-12-01 22:45:42 +03:00
herman ten brugge
df72b8d487 Add windows constructor/destructor support 2019-11-27 18:29:12 +01:00
Michael Matz
4e9ce59fe8 Workaround false positive warning 2019-11-27 17:35:24 +01:00
Michael Matz
8a93ce106a elf: rewrite version support
so it isn't quadratic in number of symbols.  It's still quadratic
in number of lib/version combinations (at library load time the
sym versions are internalized), but that's much less a problem as
in practice only glibc uses sym versioning.
2019-11-25 21:06:07 +01:00
Christian Jullien
25781e4552 Fix float conversion in arm64 backend 2019-11-08 07:55:05 +01:00
Jookia
f420259ee5 arm: Don't override syscall implementation 2019-11-06 01:01:56 +11:00
herman ten brugge
96f1fb1da0 rename testcase 2019-10-29 12:02:58 +01:00
herman ten brugge
fadfc118e5 Fix sse calling bug 2019-10-29 07:19:42 +01:00
herman ten brugge
800c3a5e0b Add constructor/destructor support 2019-10-29 07:02:58 +01:00
herman ten brugge
c4c5ea4381 remove wine 2019-10-25 07:40:37 +02:00
herman ten brugge
b84ab23011 Add wine support 2019-10-24 22:10:04 +02:00
herman ten brugge
588dd6827a Fix elf version info 2019-10-24 20:47:02 +02:00
Pursuer2
a7b37f9c63 Fix bug in gen_cvt_ftoi1. Add test 107_stack_safe for this fix.
(Thanks to the support of herman ten brugge)
2019-10-24 00:57:59 +08:00
herman ten brugge
749dd15ee8 Fix testcase for elf version 2019-10-23 13:14:41 +02:00
herman ten brugge
66a14a1e33 Add testcase for elf_version 2019-10-22 20:45:03 +02:00
herman ten brugge
b073a9c103 Add testcase for elf_version 2019-10-22 20:43:30 +02:00
herman ten brugge
7268fe7239 Add elf version support 2019-10-22 16:55:20 +02:00
Luc Everse
491773ac58
Add error_func and error_opaque getters to libtcc 2019-10-14 09:36:14 +02:00
Jan Boon
944c4003bd Add function to list all symbols, for purpose of linking separate in-memory compilations 2019-09-16 15:24:16 +08:00
Øyvind A. Holm
c6635504fe README: Fix typo and remove trailing whitespace 2019-09-14 15:37:20 +02:00
Sergey Sushilin
53a1521c2e fix _Noreturn keyword 2019-09-08 18:35:15 +03:00
grischka
7b8799e5ff tccgen.c: local extern decls: copy s->ref for VT_PTR too
This fixes the issue

    int main() { extern char *x; }
    void main1() { extern char *x; }
    t2.c:5: error: incompatible types for redefinition of 'x'

(reported by Giovanni Mascellani 2019/07/16)
2019-09-08 16:59:17 +02:00
Sergey Sushilin
9298555eb6 add C11 stdalign.h header 2019-09-08 15:04:06 +03:00
Siddhesh Poyarekar
7d4a71fc8b arm64: Increase page size
Fedora, RHEL and Centos have a default page size of 64K for arm64, so
bump up the page size so that ELF sections of the generated inary are
correctly aligned for 64K pages too.
2019-09-03 12:39:05 +05:30
Michael Matz
9264f06efe Improve ?: expansion a little
there's no need to prematurely convert the condition into registers
before emitting the test.
2019-09-01 23:13:21 +02:00
Michael Matz
91e297acd3 riscv: Optimize compares
don't convert to 1/0 prematurely, we can save enough info
in the VT_CMP metadata to be usable for the gtst branch expansion.
2019-09-01 23:13:21 +02:00
Michael Matz
48ba22c744 riscv: Replace RR insn emitter with specific one
instead of using o() directly.  Makes the code a little easier
to read.
2019-09-01 23:13:21 +02:00
Michael Matz
d5bb407cc4 riscv: Add special cases for const operands
RISC-V supports small immediates for some operations, let's
use them.
2019-09-01 23:13:21 +02:00
Michael Matz
f8d80464a9 riscv: factor load/store code
split out common code and tidy result.
2019-09-01 23:13:21 +02:00
Michael Matz
83cf5bf17f riscv: TLC of param passing
factor and cleanup the param passing code somewhat.
2019-09-01 23:13:21 +02:00
Michael Matz
c505074a9f riscv: rewrite parameter passing
this fixes the ret_mixed_test of abitest.c, now everything of the
testsuite works.

The generic code for returns is good enough for our use, except in
the specific case of a mixed int/float structures returned in registers,
so instead of duplicating the whole generic gfunc_return function, add
another modus for gfunc_sret: returning -1 makes the actual register
transfer by a new backend function.
2019-09-01 23:13:21 +02:00
Michael Matz
9b0efa9346 riscv: Make PLT reloc be AUTO_GOT
relocs against defined symbols are replaced by relative
relocs, when a GOT slot is created.  But code relocs (usually
calls via PLT) use the plt_offset member of attr, not the got_offset
member, so the "huh" warning was triggered in the case of calls to
static functions (the code still worked).

So, for now just use the AUTO_GOT_PLT mechanism.  We could also
emit a non-PLT reloc in the backend for calls to VT_STATIC functions
(like the x86-64 backend does) and do the same as for x86-64 in
build_got_entries (which transforms PLT32 into PC32 relocs, riscv
would transform CALL_PLT into CALL relocs).  Maybe later.
2019-09-01 23:13:21 +02:00
Michael Matz
0cb6e3fff8 riscv: Fix mixed2 and mixed3 abi tests
this was caused by a simple bug, not by the current inability
to pass a {float,int} struct per psABI.  So now only ret_mixed_test
is still broken.
2019-09-01 23:13:21 +02:00
Michael Matz
98f1b83ffe riscv: Start fixing float struct passing/returnig
this fixes ret_2float_test, ret_2double_test and
ret_8plus2double_test of abitest.c.  The common gfunc_return
actually works for these cases, so let's use that for now.

The ret_mixed_test (as well as mixed2 and mixed3) are left
broken, and tccgen.c:gfunc_return can't be used for that as is,
so I'll leave the gfunc_return implementation in riscv64-gen.c for
now, I'll have to think about this some more.
2019-09-01 23:13:20 +02:00
Michael Matz
98dc4c123d riscv: Fix stdarg_many_test
if named params are passed on stack, the va_arg area begins after
that, not at sp+0.  Fixes abitest:stdarg_many_test.
2019-09-01 23:13:20 +02:00
Michael Matz
e9c2a1996a riscv: Fix tcctest.c
this is it!  With this tcctest.c works, as well as compiling
TCC with itself.  abitest.c doesn't yet work due to known
problems, all the rest does.  There are still warnings during
linking.
2019-09-01 23:13:20 +02:00
Michael Matz
509f561823 riscv: fix more sign/zero-extension problems
see the testcase.  For the signed case this problably does
the wrong thing, and it should break other archs.  Rework once
there are testcases for this.
2019-09-01 23:13:20 +02:00
Michael Matz
1ada32900b riscv: Fix ftoi and ftof(long double->float)
float to int must be truncations, not normal rounding.
And ftof was typoed to select the wrong conversion function.

This fixes tcctest.c completely.  (make -C tests test1)
2019-09-01 23:13:20 +02:00
Michael Matz
06184aec53 riscv: fix relocs for global syms
loads and stores to global symbols need to go via the
GOT (at least for weaks), otherwise -run doesn't work.
Ideally we'd generate GOT relocs (and loads) always and replace
them with PCREL relocs and adds during linking.
2019-09-01 23:13:20 +02:00
Michael Matz
69c77d1597 riscv: Fix unsigned 32bit loads
the invariant for the risc-v codegen is, that 64bit regs
generally contain the sign-extension of a 32bit value.
This wasn't heeded by loads of 32bit values from memory, which
used lwu and ultimately caused a miscompilation in string_test
of tcctest.c.

Now most of tcctest.c works (not with -run, but with linking
a real executable), except some ftoi/ftof conversions.
2019-09-01 23:13:20 +02:00
Michael Matz
2668eda595 riscv: Implement long double support
for the implementation of operations we can reuse the ones
from lib/lib-arm64.c, risc-v long double is also float128.
Also implement ggoto, and PDIV, and use t0 in load/store as
temporary register if necessary, not one given by get_reg
(the latter can destroy assignments of long double parameters
in function calls that are already set up).

This let's us compile tcc.c and tcctest.c, though both
don't yet work.
2019-09-01 23:13:20 +02:00
Michael Matz
2616c6b230 riscv: Fix 73_arm.c
some constants were loaded wrong (e.g. 0xffffabcdU), and
risc-v needs to do explicit zero-extensions for widening from
32bit (not sign-extensions like the other 64bit targets).

This makes the whole tests2.all testsuite work.

Parameter passing is still not psABI-compliant, but internally
consistent.  (e.g. structs of two floats/doubles are passed
in integer registers, but should sit in float regs).
2019-09-01 23:13:20 +02:00
Michael Matz
982de78e8d riscv: implement stdarg functions
this also fixes passing of params > 16 bytes.  In riscv
they aren't passed by value on stack, but via reference (and
because callees are allowed to modify by-ref params the caller must
allocate an own copy per call).

This fixes the stdarg parts of 73_arm.c.
2019-09-01 23:13:20 +02:00
Michael Matz
f44df9d85b riscv: some work on large function arguments
like long double (16 bytes) and structs.  Not completely
correct, but 73_arm64 somewhat works now (when the stdarg part
is disabled), though with some errors.  What's definitely incorrect
is arguments of a mixed int/float struct.  I'm using VT_LDOUBLE
(which conveniently has to be placed in a int-reg-pair) to load/store
structure arguments of size > 8 and <= 16, and that can lead to
overreads.
2019-09-01 23:13:20 +02:00
Michael Matz
ddb0c2de92 riscv: support large stack and far jumps
for 101_cleanup we need 256kb stack (with the associated problem
of needing a larger prologue/epilogue) as well as jumps out of
range for the 21bit offsets (exactly for second part of the prologue).
2019-09-01 23:13:19 +02:00
Michael Matz
b69c2ea2cf riscv: fix 90_struct-init
indirect calls were broken.
2019-09-01 23:13:19 +02:00
Michael Matz
31ecaa7c28 riscv: GOT loads, signed remainder, ELF flags
* support loading sym addresses from GOT: important for weak syms,
  fixes 104_inline.  This is still incomplete, it only works
  for taking the sym address, not for directly loading/storing into
  such symbols (i.e. not for VT_LVAL)
* another op: '%'
* ELF flags: add EF_RISCV_FLOAT_ABI_DOUBLE, which is our ABI.
2019-09-01 23:13:19 +02:00
Michael Matz
b0329ac081 riscv: predefine more macros
RISC-V uses some more control macros for features (e.g. used
int <setjmp.h> to declare the jumpbuf contents).
2019-09-01 23:13:19 +02:00
Michael Matz
5390a729d9 riscv: Implement VLA stuff
makes 78_vla_label and 79_vla_continue work.
2019-09-01 23:13:19 +02:00
Michael Matz
9164594d1f riscv: load 64bit constants, and 32bit shifts
fixes 95_bitfields.  loading 64bit constants is suboptimal
right now.  int32_t shifts really need to use the W form,
otherwise 'x << 24 >> 24' doesn't extract the low 8 bits.
2019-09-01 23:13:19 +02:00
Michael Matz
215bc1aab4 riscv: Add sar, shr insn
fixes 92_enum_bitfield.
2019-09-01 23:13:19 +02:00
Michael Matz
f64d460d29 riscv: fix 72_long_long_constant
32bit signed constants larger than 0x7ffff800 were handled wrongly.
2019-09-01 23:13:19 +02:00
Michael Matz
5fcb87138d riscv: Handle more relocs
those happen on SUSE systems.
2019-09-01 23:13:19 +02:00
Michael Matz
9309585dbe riscv: some long double support
long double on risc-v is 128bit, but there are no registers
for that type (without the Q ISA extension).  They are passed
like two 64bit integers values (with an exception for varargs,
where it's an aligned register pair).  This all requires some
hacks in generic code as otherwise RC_FLOAT regs are tried for
holding values of long double type, but we need a RC_INT register
pair.  This really could all use some cleanup for all archs.

This doesn't implement any conversions of operations for long
double, but it's enough to get 70_floating_point_literals working.
2019-09-01 23:13:19 +02:00
Michael Matz
9c1b17407f riscv: Make 32_leds work
* more ops: umod and udiv
* large immediates: suboptimal code, e.g. when loading
  0xffffffffU (which is what a cast from long to int does).

tests2 work up to 67_macro_concat.
2019-09-01 23:13:19 +02:00
Michael Matz
02c8e69a07 riscv: fp parameters
makes 23_type_coercion work.  (clean up to 31_args)
2019-09-01 23:13:19 +02:00
Michael Matz
0d3db83f16 riscv: float ops
* compares
* add/sub/mul/div
* float-float converts

makes 22_floating_point work.
2019-09-01 23:13:19 +02:00
Michael Matz
9214087259 riscv: More insns, operands and arg slots
* load/store of FP ops
* load/store from symbols, VT_LLOCAL, some with large addend
* load of VT_JMP result
* calls with many args (stack slots)
* calls with FP args
* more operations: and/or/xor/div
2019-09-01 23:13:19 +02:00
Michael Matz
b1c7520886 riscv: more insns
* register indirect loads and stores
* load of local addresses
* indirect calls (uses ra as temporary reg if necessary)
* operations *, -, <<
* gen_cvt_sxtw: is not needed in most cases, let's see

tests2 runs until (incl) 09_do_while.
2019-09-01 23:13:18 +02:00
Michael Matz
16edda58b7 riscv: Add more ops and fixes
* implement compares, gtst and gsym/gjmp and add
* implement stores (simple cases)
* fix arg passing with more than one register arg, fix
  loads to not always use 8byte loads
* add some predefined macros: __riscv, __riscv_xlen,
  __SIZEOF_POINTER__ (needed by glibc header)

The first 5 tests of tests2 run now.
2019-09-01 23:13:08 +02:00
Michael Matz
55040845f3 riscv: Handle JUMP_SLOT reloc
so that -run works.
2019-08-14 18:22:21 +02:00
Michael Matz
ef7c1a4e96 riscv: Implement some loads and calls
so that a hello world works.
2019-08-14 18:22:18 +02:00
Michael Matz
35d7b5934e riscv: hacky prolog, epilog and return
this now allows to compile a simple working example:

  int main(void) { return 0; }
2019-08-14 18:22:14 +02:00
Michael Matz
1353ccd9e2 riscv: Handle some usual relocs
this is enough to let me link a tcctest.c compiled by GCC
using some current debian sid riscv64 system.  It needs
linking against libgcc.a for various floating point TFmode
routines.  The result runs.
2019-08-14 18:19:00 +02:00
Michael Matz
0676d5bc23 riscv64: Add skeleton target 2019-08-14 18:18:46 +02:00
Michael Matz
9e429dbef0 Fix invalid size with GNU designated initializers
the uninitialized cumofs was leading to random sizes for
the memset when initializing local structures, potentially
leading to segfaults from it.  Only a problem with GNU
designated initializers, which we didn't test very well.
See testcase.
2019-07-21 21:14:58 +02:00
grischka
ce1ef5b8fc some smaller fixes
- libtcc.c/tccpp.c: fix -U option for multiple input files
- libtcc: remove decl of tcc_add_crt() for PE
- tcc.h: define __i386__ and __x86_64__ for msvc
- tcc.h: undef __attribute__ for __TINYC__ on gnu/linux platforms
- tccelf.c: disable prepare_dynamic_rel unless x86/x64
- tccpe.c: construct rather than predefine PE section flags
- tccpp.c: (alt.) fix access of dead stack variable after error/longjmp
- x86_64-gen.c: fix func_alloca chain for nocode_wanted
- tccpp.c/tccgen.c: improve file:line info for inline functions
- winapi/winnt.h: correct position for DECLSPEC_ALIGN attribute
- win32/lib/crt: simplify top exception handler (needed for signal)
- arm64-gen.c: remove dprintf left from VT_CMP commit
- tccgen.c: limit binary scan with gcase to > 8 (= smaller code)
- tccgen.c: call save_regs(4) in gen_opl for cmp-ops (see test in tcctest.c)
2019-07-14 22:46:19 +02:00
Christian Jullien
4bb5bc4401 Add Windows WSAPool declaration, associated struct and constants. It allows 100% poll compatible POSIX API on Windows. 2019-07-09 18:02:59 +02:00
YX Hao
756e766295 win: ignore dllimport/dllexport for typedef with warning instead of error
Keep the same as gcc and clang.
The usage exists in MinGW-w64 headers.
2019-07-02 18:19:24 +08:00
Christian Jullien
a4997bf3d9 Inverse last intr test so that function always returns. This prevents warning as found with gcc 8.3 and it matches other function styles doing the same in ARM generator. 2019-06-26 05:43:47 +02:00
Christian Jullien
a9340dd325 Applying grischka proposed patch fixes error reported on 93_integer_promotion test as found on Debian buster / gcc 8.3 for ARM plateform 2019-06-26 05:26:20 +02:00
Avi Halachmi (:avih)
a7eef33859 configure: conftest win32: don't translate LF to CRLF
This removes a spurious \r at $gcc_{major,minor} in configure on cygwin.

Details:

The EOL output of conftest.exe for windows was \r\n . Now it's only \n .
The output of conftest is used with sh command substitutions which trim
trailing newlines, but not all windows shells consider \r\n as newline.

E.g. msys2 and busybox (for windows) were designed for tight integration
with windows apps and their shells do trim \r\n, but cygwin sh is closer
to POSIX and trims only \n - leaving \r at the string.
2019-06-25 16:44:43 +03:00
Avi Halachmi (:avih)
84779b2b84 configure: allow cc tests even if cross compiling, test i386/x86-64
Adds a tool `ppif` at configure which can test preprocessor conditions
even when $cc is a cross compiler to any foreign platform.

Currently used only to identify i386 or x86_64 (including when cross
compiling) as a mini-demonstration.

Hopefully will be used in the future to test more compiler features
and/or replace uname-related tests with more accurate results.
2019-06-25 16:44:43 +03:00
grischka
3d78918e63 introduce scopes for VLA & attribute cleanup 2019-06-24 11:40:01 +02:00
grischka
8227db3a23 jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.

Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).

example:
    if (a && b || c && d)
        e = 0;

before this patch:
   a:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
   d:	83 f8 00             	cmp    $0x0,%eax
  10:	0f 84 11 00 00 00    	je     27 <main+0x27>
  16:	8b 45 f8             	mov    0xfffffff8(%ebp),%eax
  19:	83 f8 00             	cmp    $0x0,%eax
  1c:	0f 84 05 00 00 00    	je     27 <main+0x27>
  22:	e9 22 00 00 00       	jmp    49 <main+0x49>
  27:	8b 45 f4             	mov    0xfffffff4(%ebp),%eax
  2a:	83 f8 00             	cmp    $0x0,%eax
  2d:	0f 84 11 00 00 00    	je     44 <main+0x44>
  33:	8b 45 f0             	mov    0xfffffff0(%ebp),%eax
  36:	83 f8 00             	cmp    $0x0,%eax
  39:	0f 84 05 00 00 00    	je     44 <main+0x44>
  3f:	e9 05 00 00 00       	jmp    49 <main+0x49>
  44:	e9 08 00 00 00       	jmp    51 <main+0x51>
  49:	b8 00 00 00 00       	mov    $0x0,%eax
  4e:	89 45 ec             	mov    %eax,0xffffffec(%ebp)
  51:   ...

with this patch:
   a:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
   d:	83 f8 00             	cmp    $0x0,%eax
  10:	0f 84 0c 00 00 00    	je     22 <main+0x22>
  16:	8b 45 f8             	mov    0xfffffff8(%ebp),%eax
  19:	83 f8 00             	cmp    $0x0,%eax
  1c:	0f 85 18 00 00 00    	jne    3a <main+0x3a>
  22:	8b 45 f4             	mov    0xfffffff4(%ebp),%eax
  25:	83 f8 00             	cmp    $0x0,%eax
  28:	0f 84 14 00 00 00    	je     42 <main+0x42>
  2e:	8b 45 f0             	mov    0xfffffff0(%ebp),%eax
  31:	83 f8 00             	cmp    $0x0,%eax
  34:	0f 84 08 00 00 00    	je     42 <main+0x42>
  3a:	b8 00 00 00 00       	mov    $0x0,%eax
  3f:	89 45 ec             	mov    %eax,0xffffffec(%ebp)
  42:   ...
2019-06-24 11:40:01 +02:00
grischka
1b57560502 nocode, noreturn
A more automatic approach to code suppression (aka. nocode_wanted)

The simple rules are:
- Clear 'nocode_wanted' at (im/explicit) label IF it was used
- Set 'nocode_wanted' after unconditional jumps

Also in order to test this then I did add the "function might
return no value" warning, and then to make that work again I
did add the __attribute__((noreturn)).

Also moved the look ahead label check into the type parser
to gain a little speed.
2019-06-24 11:40:01 +02:00
grischka
8569048031 work on local extern declarations
Example:
    int a = 1;
    void f(void)
    {
        int a = 2;
        {
             extern int a; // = 1 !!
             ....

To get this (more) correctly there is a new function to copy
syms between local to global stacks.

Also, this patch changes the meaning of VT_EXTERN back
to the simpler and IMO more useful notion of
    DECLARED but not (yet) DEFINED.
and that for both variables and functions.  That is, VT_EXTERN
in tcc doesn't have to do with the keyword 'extern' necessarily.

Also this patch does allow
    int x[];
as alias for
    extern int x[];
(as do gcc and msvc)
2019-06-24 11:38:32 +02:00
Pascal Cuoq
cbbba01b46 reject invalid arrays in post_type() 2019-06-24 10:28:44 +02:00
Michael Matz
942b73bbbb Try fixing asm_dot_test on Windows
its GAS lacks .pushsection/.popsection and .previous, so
we must manually switch back to .text in the snippets on
that platform.
2019-06-24 05:18:08 +02:00
Avi Halachmi (:avih)
6599483304 test 104: fix out-of-tree build+test
This was broken, and now works again:
mkdir build && cd build && ../configure && make && make test
2019-06-22 09:47:57 +03:00
Michael Matz
dd60b20c6e Define __dso_handle in libtcc1.a
new glibc really can't avoid it anymore, so let's provide it.
I've tried doing it only on systems that possibly are glibc
based.  (For others it would be harmless as it simply wouldn't
be picked up from libtcc1.a)
2019-06-22 01:38:43 +02:00
Michael Matz
7894f39e65 Make VT_STRUCT_MASK unsigned
avoids a (overly anal, but correct) undefined behaviour warning
about shifting 4095 as int by 20.
2019-06-22 00:42:24 +02:00
grischka
e6d8f9a8bb test 104: adjust to (unwritten) tests2 suite conventions
... which IMO are:

1) files don't need a _test suffix because all files in
   the directory are tests ;)

2) we test the BEHAVIOR of the program, rather than its
   binary bit contents.

Ok, but nobody said a test can't use two files ;)

(where the 104+_ construct is meant to prevent the file
from being picked up by the makefile as a test on its own).
2019-06-22 00:26:31 +02:00
Avi Halachmi (:avih)
5dfcc7506c test 104: simplify, ensure sort locale, parametric tcc
Previously test 104 used a combination of *nix tools and system() calls
to emulate a `sh` script, which required split code paths for windows
due to different shell and different absolute path representation.

Also, it used a hardcoded tcc binary path, didn't set locale for sort.

Now the tools are used from a `sh` script which the program generates
and invokes, tmp files are at CWD and no conversion is required, tcc
path is taken from Makefile (exported), and `sort` uses LC_ALL=C.
2019-06-21 22:36:09 +03:00
Pascal Cuoq
944fe7036c x86-64 codegen: avoid allocating VLA of size 0 2019-06-19 20:43:10 +02:00
Christian Jullien
d39c49db2d Remove empty conditional _WIN32 code 2019-06-18 15:05:46 +02:00
Christian Jullien
d052f609fe Remove \r in win32/include/uchar.h 2019-06-18 14:39:54 +02:00
Christian Jullien
21841cb622 Add win32/include/uchar.h which is a C11 standard header. It makes Windows standalone port more C11 compliant 2019-06-18 14:38:16 +02:00
Christian Jullien
78ee3eb7c7 Add Windows support for tests of inline functions - test 104 2019-06-18 14:29:58 +02:00
Michael Matz
cb73be5346 Fix last commit
it wasn't complete.
2019-06-17 20:52:09 +02:00
Michael Matz
c3f0937012 Don't emit unreferenced static inlines
there's no need to emit unreferenced static function, even
if they are forced.
2019-06-17 19:36:59 +02:00
Michael Matz
3980e4a5b9 Add testcase for 69a46b0
that fixed mingw but also fixed the problem for which the testcase
is now added.
2019-06-17 19:08:08 +02:00
Michael Matz
fe23a14ebb Deal with more tentative definitions
see testcase.
2019-06-17 18:52:49 +02:00
Michael Matz
69a46b0c53 Make mingw work again
my last inline changes caused parameter names to be overwritten
always (as VT_EXTERN now doesn't mark the current def anymore),
leading to a compile error when including windows.h.  Rework this.

Also silence a warning that currently happens for mingw, which is
written with gnu-inline behaviour in mind.  Our work-arounds
of using "static inline" actually create invalid C (which we warn
about).  Until we implement this properly, just silence the warning.
2019-06-17 18:28:56 +02:00
Michael Matz
cb8bbf1ab9 TLC for C99 inline implementation
there's no need for two new flags in type.t .  We just can't use
VT_EXTERN as marker if functions are defined or not (like we can
for objects), and then can simply implement the rules of C99/C11
by not overwriting VT_STATIC/VT_EXTERN at all but rather only
look at them.  A function already on the inline list can be
forced by removing the VT_INLINE flag, and then linkage
follows from some combination of VT_STATIC, VT_EXTERN and VT_INLINE.
2019-06-17 03:34:03 +02:00
Michael Matz
4dfc27b101 Stricter C99 inline tests
these three functions are external definitions, and so need
to be emitted even without any references.
2019-06-17 03:33:52 +02:00
Michael Matz
7ed2d15345 Revert "Fix relocs of unwanted sections"
This reverts commit fef838db2d,
should not be necessary anymore.
2019-06-16 22:50:06 +02:00
Michael Matz
eaf5f3fa52 Ignore relocs to ignored sections as well
relocs to sections which we ignore should be ignored as well,
they have no meaning and currently would segfault.
2019-06-16 22:48:15 +02:00
Michael Matz
4c2b55f962 Fix use-after-free in tccelf.c
build_got might realloc the symbol table (for the _GLOBAL_OFFSET_TABLE_
symbol), so we can't reuse sym (a pointer into it) after build_got.
Using it isn't necessary, as we pass the sym_index to put_got_entry,
and that recomputes sym.
2019-06-16 22:06:07 +02:00
Petr Skocik
47722a8c2e fix windows errors uncovered by the inline patch
By always instantiating extern inlines, the patch has discovered
2 assembly errors, which were fixed in the original mingw64 in 2009.

This fixes those errors.

Additionally it changes __CRT_INLINE in win32/include/_mingw.h
from `extern __inline__` to `static __inline__`.

__CRT_INLINE is used exclusively in header files and as such
it should not create externally visible instantiations like a `extern
inline` would (as per the C standard).
2019-06-12 15:37:59 +02:00
Petr Skocik
587e1f5598 standard conformant inline functions
- add tests for standard conformant inline functions
- implement it

The  old tinycc failed to provide a conforming implementation
of non-static inlines.  It would expose external symbols where it
shouldn't and hide them where it should expose them.

This commit provides a hopefully comprehensive test suite
for how things should be done. The .expect file can be obtained
by compiling the example c file (embedded in the test)
with a conforming compiler such as gcc, clang or icc and then
printing the exported symbols (e.g., with nm+awk+sort).

(The implementation currently reserves two new VT_ flags.
If anyone can provide an implementation without reserving
two extra flags, please replace mine.)
2019-06-11 16:29:24 +02:00
Petr Skocik
f2fd56a27d turn -fdollars-in-identifiers on by default
gcc and clang do it too
2019-06-11 13:45:22 +02:00
Petr Skocik
60ceab1453 in c11 mode, skip __STDC_ISO_10646__
The macro conflicts (=> redef warnings in a simple hello world)
with a definition introduced by glibc headers and
it's probably not "necessary" anyway since clang doesn't use it.
2019-06-11 13:43:24 +02:00
Petr Skocik
18a2ed2936 make -h|-hh succeed if the output is successfully written 2019-06-11 12:42:26 +02:00
Michael Matz
d04ce7772c Don't allow section switches in local asm instructions
GCC wouldn't be able to implement this either (due to the separate
phases of compilation and assembly).  We could allow it but it
makes not much sense and actively can confuse broken code into
segfaulting TCC.  At least we can warn.

Warning exposes a problem in tcctest, and fixing that gives us
an opportunity to also test .pushsection/.popsection and .previous
directive support.
2019-05-30 21:31:35 +02:00
Vlad Vissoultchev
1dd6842654
Don't drop asm_label hack on external symbols for win32 DLL exports 2019-05-14 22:37:13 +03:00
matthias
cf3d23741f add tests for cleanup loop
- Also fix some gcc warning
2019-05-03 12:32:55 +02:00
matthias
14be3a1dc1 fix cleanup with for loop initialisation
Signed-off-by: matthias <uso.cosmo.ray@gmail.com>
2019-05-03 12:32:55 +02:00
matthias
0d54946dec fix cleanup with break and continue
fix cleanup
2019-05-03 12:32:55 +02:00
Michael Matz
d30bc6d00a _Static_assert must be followed by semicolon
as per the C11 grammar.
2019-05-03 00:22:35 +02:00
matthias gatto
e371642e6b _Static_assert test 2019-04-28 08:27:33 +02:00
matthias
5a0101856b add C11 _Static_assert support 2019-04-28 01:07:21 +02:00
Michael Matz
85483f321d Add 103_implicit_memmove testcase
which checks that a declaration after TCC implicitely declares
an internally used function (memmove here) doesn't create any problem.
2019-04-18 03:42:23 +02:00
Michael Matz
c07e81b087 Tidy some code
the real difference is in decl0 where we can use external_sym
just fine also for function definitions, we don't have to use
external_global_sym.  Setting VT_EXTERN in external_sym isn't
necessary either (the type will have it set if necessary).
The rest is tidying: removing unused arguments and moving
some code around.
2019-04-18 03:42:23 +02:00
Christian Jullien
f2461096b1 Add minimal includes and .def files to support, by default, BSD socket programming on Windows. 2019-04-16 07:26:04 +02:00
Michael Matz
c16dadbb97 win64: make signal handlers work somewhat
we can register a top-level exception filter which does
nothing else than call into _XcptFilter (provided by
the default C runtime environment) and signal handlers
for the few POSIX signals that Windows can handle start
working (that includes e.g. SEGV).
2019-04-15 20:02:45 +02:00
Michael Matz
105107124d Make 102_alignas independend of architecture
alignment of double is of course depending on the target,
so the .expect file was arch dependend.  Fix this by simply doing
some comparison tests.
2019-04-11 00:37:07 +02:00
Michael Matz
0344c0b6a0 Fix more struct inits
anonymous struct members were somewhat broken as the testcase
demonstrates.  The reason is the jumping through hoops to fiddle
with the offsets I once introduced to avoid having to track
a cumulative offset.  That's now not necessary anymore and actively
harmful, doing the obvious thing is now better.
2019-04-11 00:30:41 +02:00
Michael Matz
94846d3eef Some types testcases
during some rework I tripped over some more obscure but valid declarators.
Let's not loose them.
2019-04-09 04:39:38 +02:00
Michael Matz
ce4aea2478 Fix last commit's expected output
I forgot to regenerate the expected output of 102_alignas.c
just before committing and pushing :-/  This rectifies that.
2019-04-09 03:58:17 +02:00
Michael Matz
38a6aba468 Fix _Alignas
* don't accept _Alignas as type qualifier (after pointer '*').
* accept type-names within _Alignas
* add testcases
2019-04-08 22:06:51 +02:00
Ziga Lenarcic
fa0ef91a24 With -run -nostdlib use "_start" as the entry symbol. 2019-04-07 13:44:07 +02:00
Michael Matz
2a417b50ee Detect invalid VLA decls
see testcase, when the inner array dimension of multi-dimensional
VLAs isn't given TCC was generating invalid vstack accesses.
Those are actually invalid, so just diagnose them.
2019-04-07 04:09:25 +02:00
Michael Matz
5ac2a26666 Don't endlessly recurse on invalid nested typedefs
see testcase.
2019-04-07 03:15:05 +02:00
Michael Matz
d00f98a7a5 Fix 98_al_ax_extend.c compile error
this test is run only on i386 so its failing went unnoticed for
a while, since 1fd3709379.  IS_ASM_SYMs should not be tested
for conflicting types, the C typing overrides.
2019-04-07 02:44:32 +02:00
Devin Hussey
9382a3ad58 C11 conformance: Add _Noreturn, stdnoreturn.h, and partial _Alignas support
_Noreturn, just like __attribute__((noreturn)), is ignored.
I also added stdnoreturn.h, in all its glorious uselessness.

_Alignas only works for integer expressions right now. In order
to comply, we need:
 - _Alignas(type) -> _Alignas(_Alignof(type)).
 - stdalign.h as soon as it is done.

Note: DR 444 is supported; it works on struct members.

Signed-off-by: Devin Hussey <husseydevin@gmail.com>
2019-03-20 15:03:27 -04:00
Michael Matz
1fd3709379 Fix local extern vardecl
see testcases.  A local 'extern int i' declaration needs to
refer to the global declaration, not to a local one it might
be shadowing.  Doesn't seem to happen in the wild very often as
this was broken forever.
2019-03-18 05:53:03 +01:00
Michael Matz
e6980f6cc7 Detect more invalid initializers
in presence of invalid source code we can't rely on the
next token to determine if we have or haven't already parsed
an initializer element, we really have to track it in some separate
state; it's a flag, so merge it with the other two we have (size_only
and first).  Also add some syntax checks for situations which
formerly lead to vstack leaks, see the added testcases.
2019-03-18 03:31:11 +01:00
Michael Matz
d72b877e45 Fix invalid memory access in preprocess_end
when an error is thrown macro_stack might point to
unwound local variables, we can't access them.
2019-03-15 13:17:58 +01:00
Vincent Lefevre
be7c870718 Fix va_end() definition: must be an expression of type void
Also added a test yielding a failure with the previous definition,
i.e. when using: (va_end(ap));

The test also checks potentially incorrect va_start() definition.
2019-03-14 15:39:52 +01:00
Vincent Lefevre
920f773a81 Fix "make -j test"
The clean-s rule must be run before the tests, not at the same time!
2019-03-14 14:59:27 +01:00
Michael Matz
4b46e0ec63 Handle corner case for abstract decls
sometimes abstract decls in parameter lists left the returned name
uninitialized potentially leading to segfaults, like in

  int f(int ()) {
    return 0;
  }

Deal with this.
2019-03-12 17:27:15 +01:00
Michael Matz
ef0397cf3d Fix crash on invalid code
like on 'enum myenum { L = -1 } L;'.  It's a bit tedious as
there are two paths (for global vs local symbols), and because
the scope and enum_val share same storage.
2019-03-08 17:58:25 +01:00
matthias
b082659f19 fix segv in "{,}" combound literal 2019-03-06 18:51:13 +01:00
Michael Matz
fef838db2d Fix relocs of unwanted sections
relocation sections to sections we don't want to handle lead
to segfaults, handle this situation (by ignoring the relocs
as well).
2019-03-04 16:53:24 +01:00
Michael Matz
749d19d70b Fix sub-int returns on x86-64 and i386
the ABIs (and other compilers) extend sub-int return values in the
caller.  TCC extends them in the callee.  For compatibility with
those other compilers we have extend them in the caller as well.
That introduces a useless double extension in pure TCC-compiled code,
but fixing that generally requires that the code generator of TCC would
understand sub-int types.  For the time being bite the bullet.
2019-02-10 18:27:41 +01:00
Michael Matz
5f737fb4d3 tidy code 2019-01-31 01:04:55 +01:00
Michael Matz
5c862a08b4 suppress code after continue as well
fix an oversight in code suppression
2019-01-31 00:37:49 +01:00
matthias
d27ea5155f remove incr/decr_local_scope functions 2019-01-29 21:32:38 +01:00
Michael Matz
08e22d8ad2 More cleanup tests
test3 wasn't working originally.
2019-01-28 05:54:19 +01:00
Michael Matz
4cc802a88e Tidy new support for cleanups
encode most things in Syms, do only as much work as necessary
(e.g. pending cleanups), don't track scopes in a large
structure (instead encode the scopes with cleanups directly
in the cleanups tree).  Removes ca. 120 lines of code.
2019-01-28 05:54:19 +01:00
Detlef Riekenberg
5d805a90e3 tcc.c: Fix outdated help info for the std option
the std option is no longer ignored

--
bye bye ... Detlef

Signed-off-by: Detlef Riekenberg <tcc.dev@web.de>
2019-01-27 17:32:11 +01:00
matthias
46145af4a1 remove C99 'for' loop initial declarations 2019-01-23 19:42:04 +01:00
matthias
5010023428 remove outdated comment 2019-01-23 17:21:14 +01:00
matthias
de92fbee7e add a cleanup test that use a lot of scope 2019-01-23 17:21:14 +01:00
matthias
26f0cf0708 Fix scope limit for cleanup attribute
old implementation use only a global static array for storing
ScopeTracker which have the advantage to be fast, but you can't
use cleanup in a function that have move than SCOPE_TCK_STORE_SIZE
scopes.

I don't want to use only dynarray_* as it would slow down tcc for
every functions, so I keep both stores.
2019-01-23 17:21:14 +01:00
matthias
ff38d90d5d Add attribute cleanup test 2019-01-23 17:21:14 +01:00
matthias
0d91ba749c Add gcc attribute cleanup support
The major difficulty was to handle cleanup when a goto happen
to do so, I've had a "ScopeTracker" struct.
I can't use local_scope because that would not work with code like below
as local_scope would be at the same level:

{
    char * __attribute__ ((cleanup(clean_function))) str = "hej";
    goto next;
}
{
    next:
}
2019-01-23 17:21:14 +01:00
Giovanni Mascellani
f6be0d483b
Fix read() usage in tccelf.c.
read() is allowed to short-read, and return less bytes then requested.
The caller must restart read() when this happens (and they want more
bytes).

This patch is still buggy, because errors are not always checked.
Still, less buggy than before.
2019-01-13 15:20:39 +01:00
Christian Jullien
acac38afb2 Add C99 compiliant iso646.h header 2019-01-13 10:24:50 +01:00
Michael Matz
45b8b0e57d Revert 337dc84b (other -static fix)
this should work now with 05f6aa1a.
2019-01-13 06:01:59 +01:00
Michael Matz
05f6aa1ade Fix -static linking with uClibc
symbols are local when defined and referred to from the executable.
Also, we need to relocate the .got section when this is a static link
(our static linking effectively generates code as if this were a dynamic
link with PLT and GOT, and then emulates the runtime loader).
2019-01-13 06:00:51 +01:00
Michael Matz
adbe794a46 Properly access sym_attrs
in corner cases the direct access to the sym_attrs[] array in the
backends is out of bounds and replacec garbage symindices into
the relocs.
2019-01-13 02:55:44 +01:00
Christian Jullien
d44d8cdf60 Add more STDC_ C11 compatible constants 2019-01-12 10:41:56 +01:00
Pursuer
ecb90de4cc FIX:Revert commit 3f05d88d5b
The function should to be saved to stack in some cases (fastcall on i386, struct argument on arm etc.). But I neglected. So I revert this commit.
2019-01-12 01:54:24 +08:00
Christian Jullien
8482f9e54b Add __STDC_xxx test features related to ISO/IEC C11 when -std=c11 is passed. 2019-01-11 14:26:17 +01:00
Petr Skocik
065a3b35fc Make stddef.h expose max_align_t with -std= >= c11 2019-01-11 11:17:49 +01:00
Matthias Gatto
2b94c0c3b1 Revert "allow c11 feature only when -std=c11 is use"
This reverts commit 756988e8f9.
2019-01-11 10:35:44 +01:00
matthias gatto
756988e8f9 allow c11 feature only when -std=c11 is use 2019-01-10 23:42:45 +01:00
Christian Jullien
a3a291784a Add -std=c11 option which sets __STDC_VERSION__ to 201112L and allows to implement c11 features conditionally 2019-01-11 02:32:48 +08:00
Kurt Nalty
337dc84b57 Fixed -static linking on x86_64 Linux 2019-01-11 02:32:48 +08:00
Petr Skocik
51f6e52dd3 Support multiple __label__ declarations
Support multiple __label__ declarations at the beginning of a block
as long as they're contiguous.

gcc and clang accept:
    { __label__ a,b; __label__ c;  /*...*/ }
.
Tcc would fail it. This patch makes it accept it.

The patch:
-        if (tok == TOK_LABEL) {
+        while (tok == TOK_LABEL) {
2019-01-11 02:32:47 +08:00
Pursuer
3f05d88d5b optimize the generated code when save_reg is required (2)
In gfunc_call, regisger will be saved before gcall_or_jmp. The register
stored the function will be saved too, though in some generator the SValue
of this function will be immediately poped after gcall_or_jmp, and no need to be saved. So I modify some generator to avoid save redundant SValue before gcall_or_jmp.
2019-01-11 02:32:12 +08:00
Pursuer
b3b685d92a optimize the generated code when save_reg is required
Before this patch, save_reg can't reuse the temporary local variable
created before to save register. It may consume a lot of stack memory. this patch make save_reg reuse the temporary local variable.
2019-01-10 13:17:14 +08:00
Pursuer
0c313f491b Insert arm-xxx_FILES into Makefile to suport CROSS_TARGET and Remove duplicate function lexpand_nr 2019-01-09 02:06:26 +08:00
Petr Skocik
7369cfc490 Revert "Add max_align_t to stddef.h"
max_align_t is C11 not C99 and the name is not reserved by C99.

This reverts commit 7eceba178d.
2019-01-08 12:32:18 +01:00
Petr Skocik
7eceba178d Add max_align_t to stddef.h 2019-01-04 14:56:37 +01:00
Christian Jullien
f21b53b5ed Add winnls.h to allow SQLite compile ROOTB on Windows 2019-01-01 08:58:09 +01:00
Michael Matz
3e007193a2 Fix more attribute placements
when parsing the type in this cast:
  (int (__attribute__(X) *)(int))foo
we ignored the attribute, which matters if it's e.g. a 'stdcall'
attribute on the function pointer.  Only this particular placement
was misparsed.  Putting the attribute after the '*' or outside the inner
parens worked.  This idiom seems to be used on SQLite, perhaps this
fixes a compilation problem on win32 with that.
2018-12-31 22:00:31 +01:00
Christian Jullien
325241c0de zfunc works again on ARM after Pursuer fix 2018-12-22 19:14:22 +01:00
general
bf09349f8e fix arm-gen.c -> gcall_or_jmp to VT_CONST 2018-12-22 19:48:56 +08:00
Theodore Dubois
82abfd75f3 Fix ELF interpreter of i386 musl target 2018-12-22 04:29:06 +00:00
Michael Matz
63f69c2bdd Disable -Wformat-truncation
this new gcc 8 warning triggers here, but is useless for our
purposes.
2018-12-21 01:03:38 +01:00
Michael Matz
f68a1f50fd Don't use C99 decls
we're consciously trying to write C90 compatible code, hence
no mixed declaration and code.
2018-12-21 01:03:05 +01:00
Christian Jullien
adfcf3b1dd Temporary remove zfunc test on ARM which generates a relocation error. Waiting for a fix 2018-12-20 08:44:47 +01:00
Christian Jullien
8494f2c318 Fix Thomas commit 776aa0c093 which failedto build on Windows. 2018-12-17 17:05:05 +01:00
Petr Skocik
070646b790 Recognize C11' _Alignof 2018-12-12 20:12:03 +01:00
Michael Matz
c4787e3626 Fix type completion for array types as well
like qualifier merging the array sizes are merged as well
for the operands of ?:, and they must not statically influence
the types of decls.

Also fix some style problems, spaces aren't stinky :)
2018-11-30 23:43:30 +01:00
Petr Skocik
f7779efe58 Fix incorrect one-sided void handling in ?:
Also make the case when one side in ?: is a ptr
and the other is neither a ptr nor a null-ptr constant
fail cleanly instead of segfaulting.
2018-11-29 13:40:48 +01:00
Petr Skocik
49dfb5755a Make fn designators in ?: decay to ptrs
This
    _Generic((__typeof(1?f:f)*){0}, void (**)(void): (void)0);
should compile like it does in gcc and clang.
2018-11-29 12:43:01 +01:00
Petr Skocik
3058d4116e Fix (Cexpr?struct1:struct2).member
Tcc used to fail this with `lvalue expected`
if the expression was a compile-time constant.
2018-11-29 10:26:56 +01:00
Petr Skocik
9d44b02a49 Fix the fix on type combining (e0012c2)
char **argv;
	    _Generic(argv, char**: (void)0);
	    _Generic(0?(char const*)0:argv[0], char const*: (void)0);
	    _Generic(argv, char**: (void)0);

    would fail because the type of argv would get modified by the
    ternary. Now allocate a separate type on the value stack to
    prevent this error.
2018-11-29 10:26:35 +01:00
Petr Skocik
e0012c2767 Fix ptr type combining inside the ternary operator
Make it match http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6
(or http://port70.net/~nsz/c/c11/n1570.html#6.5.15p6).
2018-11-28 10:36:58 +01:00
Petr Skocik
c81116e29a Make casts lose top-level qualifiers
TODO: also make them lose lvalue status
2018-11-20 19:24:24 +01:00
Petr Skocik
f85b1e393f Always allow ({ }) in the ctrl-expr of _Generic
tcc would reject e.g.,
    void f(){ struct {_Bool x:_Generic(({0;}),default:1);} my_x; }
with `expected constant`. This patch makes it accept it.

(The patch also makes tcc's _Generic a little more "generic" than that
 of gcc and clang in that that tcc now also accepts
`struct {_Bool x:_Generic(({0;}),default:1);} my_x;` in file scope
while gcc and clang don't, but I think there's no harm in that
and gcc and clang might as well accept it in filescope too, given
that they have no problem with
e.g., `/*filescope:*/int x=1, y=2, z=_Generic(x+y, int:3);`)
2018-11-13 12:51:16 +01:00
Petr Skocik
1e2e5671f7 Make tcc accept -l lib as well as -llib.
The POSIX spec for `c99` specifically favors the space-containing version
(http://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html)
and the `-l lib` syntax is also supported by gcc and clang.
2018-11-12 20:52:53 +01:00
Petr Skocik
314843ffc3 Fix how _Generic treats pointers to arrays.
_Generic should distinguish pointers to differently sized
arrays such as `int(*)[2]` and `int(*)[4]`.
2018-11-12 20:52:14 +01:00
Petr Skocik
73ca09ff32 Fix array pointer stringification.
`int (*)[4];` should not be stringified as `int *[4];`
Add stringification tests.
2018-11-12 20:50:51 +01:00
Petr Skocik
93a4ddfa63 Treat the - output as stdout. 2018-11-12 20:50:33 +01:00
Michael Matz
3b9c3fd186 Fix noreturn in main()
ISO C requires 'main' falling through the end without explicit
returns to implicitely return 0 (if declared as returning int).
2018-11-03 22:17:20 +01:00
Michael Matz
61ba9f2299 Check for void type in top-level decls
give an error message from the parser on things like "void x;" instead
of relying on codegen erroring out.
2018-08-03 22:51:35 +02:00
Michael Matz
22420ee1ee Fix misparsed type in presence of attributes
The type within the cast (int (__attribute__((foo)) *)(void))
was misparsed because of the presence of the attribute (parse_btype
prematurely concluded that (__attribute__() *) is a type.

Also see testcase.  This construct is used in sqlite it seems.
2018-07-28 18:55:54 +02:00
Jonathan Newman
0edbed1d52 Implement __attribute__((nodecorate))
Prevent any decoration that would otherwise affect an exported
PE function. For example, given the following:

__declspec(dllexport) __stdcall
int decorated(int arg) {
    return 0;
}

__declspec(dllexport) __stdcall __attribute__((nodecorate))
int undecorated(int arg) {
    return 0;
}

The following exported functions can now be seen in the DLL:
_decorated@4
undecorated

The attribute is recognised for all targets but only
affects PE codegen. I'm not sure whether this would be
useful for other targets; its intended use was to allow
the creation of a DLL matching an existing set of signatures.
2018-07-22 00:54:01 +01:00
Michael Matz
d79caa9ff6 x86-64: Fix calls via absolute function pointers
linkers don't treat relocations using symindex 0 (undefined)
very well, it can't be misused as indicator for an absolute number.
Just don't bother with special casing this, rather emit an indirect
call/jump right away. ARM64 needs the same (and didn't handle
calls via constant absolute func pointers before).

The testcase as is doesn't fail without the patch, it actually
needs separate compilation (to -fPIC .o file, then to shared lib)
to fail.
2018-07-02 01:57:29 +02:00
Michael Matz
65c7f19deb Fix stored type of arguments on x86-64
the lvalue Syms for arguments didn't correctly reflect
their own types in all cases (a side-effect of the type being
stored in type->t and the ->r members (as VT_LVAL_xxx), so the below
used an int load (not a byte load) in the conditional.

extern void bar (void);
void foo (signed char c)
{
  signed char x = c;
  if (c)
    bar();
}
2018-06-24 20:12:51 +02:00
Andrey Gursky
91bdb5a4a3 Add linker's --export-dynamic flag alias
Since 9336fa7ae5 --export-dynamic is
supported. Add this conventional flag as alias.
2018-06-11 18:26:04 +02:00
grischka
8f6fcb709a misc fixes
misc fixes including:
- tcc.c: fix "tcc -vv" for libtcc1.a on win32/PE
- tccelf.c: fix a crash when GOT has no relocs (witn -nostdlib)
- tccelf.c: fix stab linkage for zero n_strx
- tccgen.c: fix stdcall decoration for array parameters
    int __stdcall func(char buf[10]) is _func@4 (was _func@12)
- tccgen.c: fix static variables with nocode/nodata_wanted
    see tests2/96_nodata_wanted.c
- tccrun.c: align sections using sh_addralign (for reliable function_alignment)
- tests2/Makefile sort 100 after 99
- win32/include/sys/stat.h fix _stat and _wstat
- x86_64-gen.c: win64/gfunc_call: fix a bug with xmmN register args
    previously overwrote valid other xmmN registers eventually
2018-06-01 12:52:01 +02:00
grischka
2b155a8c16 tccgen.c: fix warning for incompatible struct- and function pointers
see tests2/60_errors_and_warnings.c
2018-06-01 12:41:21 +02:00
grischka
ace1225492 tcc_add_file(): preserve s->filetype
This is supposed to fix a bug where libtcc eventually was trying to
compile libtcc1.a as C source code.

Anyway, there is now only two functions that refer to s->filetype,
tcc_add_file() and tcc_add_library().
2018-06-01 12:41:17 +02:00
Michael Matz
671dcace82 Implement function alignment via attributes
which requires being able to emit an arbitrary number of NOP
instructions, which is also implemented here.  For x86 we
could emit other sequences but these are the easiest.
2018-04-06 23:02:42 +02:00
Petr Skocik
ef668aae1e Don't fail on const/restrict/static/* inside []
This patch makes tcc ignore them.

Normally (as per the C standard), They should
be only applicable inside parameter arrays
and affect (const/restrict) the pointer the
array gets converted to.

[matz: fix formatting, add volatile handling, add testcase,
add comment about above deficiency]
2018-04-01 00:48:09 +02:00
Petr Skocik
d6d3cf00ec patch type_to_str to handle complex function-ptr decls better
Code like:

    #include <signal.h>
    int main() { _Generic(signal, int: 0); }

should fail with
    error: type 'extern void (*(int, void (*)(int)))(int)' does not match any association
not
    error: type 'extern void *(int)(int, void *(int))' does not match any association

[matz: fix formatting, fix function-to-pointer decay for operands of
_Generic, add testcase for this]
2018-04-01 00:38:11 +02:00
Michael Matz
f0a25ca263 Fix shortening casts of long long
see added testcase.
2018-03-31 21:52:20 +02:00
Thomas Preud'homme
c41caac02d Select VFP if triplet is arm-linux-gnueabihf
A target triplet of arm-linux-gnueabihf indicates that the compiler
should use the VFP variant of the calling convention which as its name
implies requires VFP. This commit enables VFP when triplet is
arm-linux-gnueabihf.
2018-03-15 23:05:25 +00:00
Thomas Preud'homme
e76058c478 Remove asm-c-connect-sep in tests clean target 2018-03-09 20:10:36 +00:00
Thomas Preud'homme
776aa0c093 Prevent dead code on !x86 in prepare_dynamic_rel
In prepare_dynamic_rel() on non x86 targets the count++ statements
appear before any case label and are therefore dead code. This triggers
build failure when building with -Werror. This patch adds an extra guard
around all the x86 case labels and their associated action, leaving just
the default case label for non x86 targets which builds fine.

Origin: vendor
Forwarded: no
Last-Updated: 2018-02-24
2018-02-24 19:35:15 +00:00
Michael Matz
3e6515b64f Add make testspp.all/testspp.20
like we have already make tests2.XX.
2018-01-05 02:19:26 +01:00
Michael Matz
7ad2cf8d68 Code suppression fixes
See adjusted testcase, a lone break; in a do while loop was
incorrectly disabling the exit test.
2018-01-05 02:19:26 +01:00
Michael Matz
8294285d8f Don't emit applied .rel sections
for a final link we shouldn't emit relocation sections that are applied
already.  For now we need to emit ALLOCed .rel sections as they contain
dynamic relocs, they should be put into their own (new) section instead.
2018-01-01 05:29:46 +01:00
foobar
988e2ff7fe fix debug info with musl on x86_64
http://lists.nongnu.org/archive/html/tinycc-devel/2017-12/msg00031.html
2017-12-28 15:05:27 +00:00
Michael Matz
414b224efa Accept more floating point constant expressions
the rules for constant expressions in static initializers are more
relaxed than for integer constant expressions.  We need to accept
0.0/0.0 in static initializers (in non-static initializers the potential
exceptions need to be raised though, so no translation-time calculation
then).
2017-12-25 12:44:29 +01:00
Michael Matz
9e47b18229 Make type of __nan__ __inf__ and __snan__ be float
so that those builtins can be used directly for the C99 NAN and
INFINITY math.h macros.
2017-12-24 13:16:09 +01:00
Michael Matz
3b27b3b1d1 Fix -pthread in a different way 2017-12-23 14:49:07 +01:00
Michael Matz
1b1b270f1e Revert "Fix -pthread option handling"
This reverts commit 3f8225509b.
It fixes the linking case but introduces an ugly error with -c
about adding a library.  To fix both uses the old code structure is
better.
2017-12-23 14:46:27 +01:00
Michael Matz
3f8225509b Fix -pthread option handling
adding -pthread confused option parsing as the number of file counting
came out wrong.  Simplify and fit it, can be handled purely within
option parsing, no need for a state flag.
2017-12-23 14:14:57 +01:00
294 changed files with 57157 additions and 12801 deletions

143
.github/workflows/build.yml vendored Normal file
View File

@ -0,0 +1,143 @@
name: build and test
on:
push:
branches: [ mob ]
jobs:
test-x86_64-linux:
runs-on: ubuntu-22.04
timeout-minutes: 2
steps:
- uses: actions/checkout@v4
- name: make & test tcc (x86_64-linux)
run: ./configure && make && make test -k
test-x86_64-osx:
runs-on: macos-15-intel
timeout-minutes: 2
steps:
- uses: actions/checkout@v4
- name: make & test tcc (x86_64-osx)
run: ./configure && make && make test -k
test-aarch64-osx:
runs-on: macos-15
timeout-minutes: 2
steps:
- uses: actions/checkout@v4
- name: make & test tcc (aarch64-osx)
run: ./configure && make && make test -k
test-x86_64-win32:
runs-on: windows-2025
timeout-minutes: 6
steps:
- uses: actions/checkout@v4
- name: build tcc (x86_64-win32)
shell: cmd
run: |
cd win32
for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" amd64
call build-tcc.bat -c cl -t x86_64
- name: test (x86_64-win32)
shell: cmd
run: |
cd tests
call test-win32.bat all -k
test-i386-win32:
runs-on: windows-2025
timeout-minutes: 6
steps:
- uses: actions/checkout@v4
- name: build tcc (i386-win32)
shell: cmd
run: |
cd win32
for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" x86
call build-tcc.bat -c cl -t i386
- name: test (i386-win32)
shell: cmd
run: |
cd tests
call test-win32.bat -p c:\mingw32\bin all -k
test-arm64-win32:
runs-on: windows-11-arm
timeout-minutes: 6
steps:
- uses: actions/checkout@v4
- name: build and test (arm64-win32)
shell: cmd
run: |
cd win32
for /f "delims=" %%i in ('vswhere.exe -latest -property installationPath') do call "%%i\VC\Auxiliary\Build\vcvarsall.bat" amd64_arm64
call build-tcc.bat -c cl -t arm64
set "PATH=C:\Program Files\LLVM\bin;%CD%;%PATH%"
cd ..\tests
call test-win32.bat -c clang all -k
test-armv7-linux:
runs-on: ubuntu-22.04
timeout-minutes: 8
steps:
- uses: actions/checkout@v4
- uses: uraimo/run-on-arch-action@v3
name: make & test tcc (armv7-linux)
with:
arch: armv7
distro: ubuntu22.04
githubToken: ${{ github.token }}
install: |
apt-get update -q -y
apt-get install -q -y gcc make
run: |
echo "::endgroup::" # flatten 'run container'
./configure && make && make test -k
test-aarch64-linux:
runs-on: ubuntu-22.04
timeout-minutes: 8
steps:
- uses: actions/checkout@v4
- uses: uraimo/run-on-arch-action@v3
name: make & test tcc (aarch64-linux)
with:
arch: aarch64
distro: ubuntu24.04
githubToken: ${{ github.token }}
install: |
apt-get update -q -y
apt-get install -q -y gcc make
run: |
echo "::endgroup::" # flatten 'run container'
./configure && make && make test -k
test-riscv64-linux:
runs-on: ubuntu-22.04
timeout-minutes: 8
steps:
- uses: actions/checkout@v4
- uses: uraimo/run-on-arch-action@v3
name: make & test tcc (riscv64-linux)
with:
arch: riscv64
distro: ubuntu22.04
githubToken: ${{ github.token }}
install: |
apt-get update -q -y
apt-get install -q -y gcc make
run: |
echo "::endgroup::" # flatten 'run container'
./configure && make && make test -k
test-riscv64-linux-native:
runs-on: ubuntu-24.04-riscv
timeout-minutes: 8
steps:
- uses: actions/checkout@v4
- name: make & test tcc (riscv64-linux native)
run: |
uname -m # riscv64
./configure && make && make test -k

15
.gitignore vendored
View File

@ -16,30 +16,41 @@
a.out
tcc_g
tcc
tcc_c
tcc_p
*-tcc
libtcc*.def
config*.h
*_.h
config*.mak
config.texi
conftest*
c2str
tags
TAGS
tcc.1
tcc.pod
*.pod
*.tcov
tcc-doc.html
tcc-doc.info
win32/doc
win32/examples/libtcc_test.c
win32/libtcc
win32/lib/32
win32/lib/64
win32/include/float.h
win32/include/stdalign.h
win32/include/stdarg.h
win32/include/stdbool.h
win32/include/stddef.h
win32/include/stdnoreturn.h
win32/include/varargs.h
win32/include/tcclib.h
win32/include/tccdefs.h
win32/include/stdatomic.h
win32/include/tgmath.h
tests/tcctest[1234]
tests/tcctest.gcc
@ -50,8 +61,10 @@ tests/*.gcc
tests/*-cc*
tests/*-tcc*
tests/libtcc_test
tests/libtcc_test_mt
tests/asm-c-connect
tests/asm-c-connect-sep
tests/vla_test
tests/hello
tests/tests2/fred.txt
libtcc.dylib

View File

@ -1,3 +1,27 @@
version 0.9.28:
User interface:
- -b : bounds checker much improved (herman ten brugge)
- -bt : support for standalone backtraces also (grischka)
- -gdwarf : debug format (herman ten brugge)
- -M, -MM, and -MMD (Arthur Williams)
- -W[no-]error=<option> (Steffen Nurpmeso)
Platforms:
- new RISC-V (riscv64) target (Michael Matz)
- native macOS support for x86_64 (Michael Matz, Herman ten Brugge)
- arm and riscv64 assemblers (Danny Milosavljevic)
- Android support with position independent executables (grischka)
Features:
- _Static_assert() (matthias)
- __attribute__ ((cleanup(func))) (matthias)
- stdatomic (Dmitry Selyutin)
- asm goto ("jmp %l[label]" : : : : label) (Michael Matz)
Fixes:
- ... many, see git shortlog release_0_9_27...release_0_9_27
Version 0.9.27:
User interface:

428
Makefile
View File

@ -8,9 +8,11 @@ ifndef TOP
INCLUDED = no
endif
ifeq ($(findstring $(MAKECMDGOALS),clean distclean),)
include $(TOP)/config.mak
endif
ifeq (-$(CC)-$(GCC_MAJOR)-$(findstring $(GCC_MINOR),56789)-,-gcc-4--)
ifeq (-$(GCC_MAJOR)-$(findstring $(GCC_MINOR),56789)-,-4--)
CFLAGS += -D_FORTIFY_SOURCE=0
endif
@ -18,84 +20,125 @@ LIBTCC = libtcc.a
LIBTCC1 = libtcc1.a
LINK_LIBTCC =
LIBS =
CFLAGS += -I$(TOP)
CFLAGS += $(CPPFLAGS)
VPATH = $(TOPSRC)
-LTCC = $(TOP)/$(LIBTCC)
ifdef CONFIG_WIN32
CFG = -win
ifneq ($(CONFIG_static),yes)
LIBTCC = libtcc$(DLLSUF)
LIBTCCDEF = libtcc.def
endif
CFGWIN = -win
NATIVE_TARGET = $(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32)
ifneq ($(CONFIG_debug),yes)
ifneq ($(CC_NAME),clang)
LDFLAGS += -s
endif
endif
NATIVE_TARGET = $(if $(findstring arm64,$(ARCH)),arm64-win32,$(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32))
else
LIBS=-lm
CFG = -unx
LIBS+=-lm
ifneq ($(CONFIG_ldl),no)
LIBS+=-ldl
endif
ifneq ($(CONFIG_pthread),no)
LIBS+=-lpthread
endif
# make libtcc as static or dynamic library?
ifeq ($(CONFIG_static),no)
LIBTCC=libtcc$(DLLSUF)
export LD_LIBRARY_PATH := $(CURDIR)/$(TOP)
ifneq ($(CONFIG_rpath),no)
ifndef CONFIG_OSX
LINK_LIBTCC += -Wl,-rpath,"$(libdir)"
else
# macOS doesn't support env-vars libdir out of the box - which we need for
# `make test' when libtcc.dylib is used (configure --disable-static), so
# we bake a relative path into the binary. $libdir is used after install.
LINK_LIBTCC += -Wl,-rpath,"@executable_path/$(TOP)" -Wl,-rpath,"$(libdir)"
# -current/compatibility_version must not contain letters.
MACOS_DYLIB_VERSION := $(firstword $(subst rc, ,$(VERSION)))
DYLIBVER += -current_version $(MACOS_DYLIB_VERSION)
DYLIBVER += -compatibility_version $(MACOS_DYLIB_VERSION)
endif
endif
endif
CFGWIN =-unx
NATIVE_TARGET = $(ARCH)
ifdef CONFIG_OSX
NATIVE_TARGET = $(ARCH)-osx
LDFLAGS += -flat_namespace -undefined warning
export MACOSX_DEPLOYMENT_TARGET := 10.2
ifneq ($(CC_NAME),tcc)
LDFLAGS += -flat_namespace
ifneq (1,$(shell expr $(GCC_MAJOR) ">=" 15))
LDFLAGS += -undefined warning # depreciated in clang >= 15.0
endif
endif
export MACOSX_DEPLOYMENT_TARGET := 10.6
endif
endif
# run local version of tcc with local libraries and includes
TCCFLAGS-unx = -B$(TOP) -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP)
TCCFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP) -L$(TOP)
TCCFLAGS = $(TCCFLAGS$(CFGWIN))
TCC = $(TOP)/tcc$(EXESUF) $(TCCFLAGS)
ifdef CONFIG_OSX
TCCFLAGS += -D_ANSI_SOURCE
TCCFLAGS = $(TCCFLAGS$(CFG))
TCC_LOCAL = $(TOP)/tcc$(EXESUF)
TCC = $(TCC_LOCAL) $(TCCFLAGS)
# run tests with the installed tcc instead
ifdef TESTINSTALL
TCC_LOCAL = $(bindir)/tcc
TCCFLAGS-unx = -I$(TOP)
TCCFLAGS-win = -B$(bindir) -I$(TOP)
-LTCC = $(libdir)/$(LIBTCC) $(LINK_LIBTCC)
endif
CFLAGS_P = $(CFLAGS) -pg -static -DCONFIG_TCC_STATIC -DTCC_PROFILE
LIBS_P = $(LIBS)
LDFLAGS_P = $(LDFLAGS)
CONFIG_$(ARCH) = yes
NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386
NATIVE_DEFINES_$(CONFIG_x86_64) += -DTCC_TARGET_X86_64
NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE
NATIVE_DEFINES_$(CONFIG_OSX) += -DTCC_TARGET_MACHO
NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC
NATIVE_DEFINES_$(CONFIG_musl) += -DTCC_MUSL
NATIVE_DEFINES_$(CONFIG_libgcc) += -DCONFIG_USE_LIBGCC
NATIVE_DEFINES_$(CONFIG_selinux) += -DHAVE_SELINUX
NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM
NATIVE_DEFINES_$(CONFIG_arm_eabihf) += -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT
NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI
NATIVE_DEFINES_$(CONFIG_arm_vfp) += -DTCC_ARM_VFP
NATIVE_DEFINES_$(CONFIG_arm64) += -DTCC_TARGET_ARM64
NATIVE_DEFINES += $(NATIVE_DEFINES_yes)
DEF-i386 = -DTCC_TARGET_I386
DEF-i386-win32 = -DTCC_TARGET_I386 -DTCC_TARGET_PE
DEF-i386-OpenBSD = $(DEF-i386) -DTARGETOS_OpenBSD
DEF-x86_64 = -DTCC_TARGET_X86_64
DEF-x86_64-win32 = -DTCC_TARGET_X86_64 -DTCC_TARGET_PE
DEF-x86_64-osx = -DTCC_TARGET_X86_64 -DTCC_TARGET_MACHO
DEF-arm-fpa = -DTCC_TARGET_ARM
DEF-arm-fpa-ld = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12
DEF-arm-vfp = -DTCC_TARGET_ARM -DTCC_ARM_VFP
DEF-arm-eabi = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI
DEF-arm-eabihf = $(DEF-arm-eabi) -DTCC_ARM_HARDFLOAT
DEF-arm = $(DEF-arm-eabihf)
DEF-arm-NetBSD = $(DEF-arm-eabihf) -DTARGETOS_NetBSD
DEF-arm-wince = $(DEF-arm-eabihf) -DTCC_TARGET_PE
DEF-arm64 = -DTCC_TARGET_ARM64
DEF-arm64-osx = $(DEF-arm64) -DTCC_TARGET_MACHO
DEF-arm64-FreeBSD = $(DEF-arm64) -DTARGETOS_FreeBSD
DEF-arm64-NetBSD = $(DEF-arm64) -DTARGETOS_NetBSD
DEF-arm64-OpenBSD = $(DEF-arm64) -DTARGETOS_OpenBSD
DEF-arm64-win32 = $(DEF-arm64) -DTCC_TARGET_PE
DEF-riscv64 = -DTCC_TARGET_RISCV64
DEF-c67 = -DTCC_TARGET_C67 -w # disable warnigs
DEF-x86_64-FreeBSD = $(DEF-x86_64) -DTARGETOS_FreeBSD
DEF-x86_64-NetBSD = $(DEF-x86_64) -DTARGETOS_NetBSD
DEF-x86_64-OpenBSD = $(DEF-x86_64) -DTARGETOS_OpenBSD
ifeq ($(INCLUDED),no)
# --------------------------------------------------------------------------
# running top Makefile
PROGS = tcc$(EXESUF)
TCCLIBS = $(LIBTCC1) $(LIBTCC) $(LIBTCCDEF)
TCCLIBS = $(LIBTCCDEF) $(LIBTCC) $(LIBTCC1)
TCCDOCS = tcc.1 tcc-doc.html tcc-doc.info
all: $(PROGS) $(TCCLIBS) $(TCCDOCS)
# cross compiler targets to build
TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67
TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm64-win32 arm-wince c67
TCC_X += riscv64 arm64-osx
# TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi
# cross libtcc1.a targets to build
LIBTCC1_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince
LIBTCC1_X = $(filter-out c67,$(TCC_X))
PROGS_CROSS = $(foreach X,$(TCC_X),$X-tcc$(EXESUF))
LIBTCC1_CROSS = $(foreach X,$(LIBTCC1_X),$X-libtcc1.a)
@ -106,66 +149,76 @@ cross: $(LIBTCC1_CROSS) $(PROGS_CROSS)
# build specific cross compiler & lib
cross-%: %-tcc$(EXESUF) %-libtcc1.a ;
install: ; @$(MAKE) --no-print-directory install$(CFGWIN)
install-strip: ; @$(MAKE) --no-print-directory install$(CFGWIN) CONFIG_strip=yes
uninstall: ; @$(MAKE) --no-print-directory uninstall$(CFGWIN)
install: ; @$(MAKE) --no-print-directory install$(CFG)
install-strip: ; @$(MAKE) --no-print-directory install$(CFG) CONFIG_strip=yes
uninstall: ; @$(MAKE) --no-print-directory uninstall$(CFG)
ifdef CONFIG_cross
all : cross
endif
# --------------------------------------------
T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
T = $(or $(CROSS_TARGET),$(NATIVE_TARGET))
X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
DEF-i386 = -DTCC_TARGET_I386
DEF-x86_64 = -DTCC_TARGET_X86_64
DEF-i386-win32 = -DTCC_TARGET_PE -DTCC_TARGET_I386
DEF-x86_64-win32= -DTCC_TARGET_PE -DTCC_TARGET_X86_64
DEF-x86_64-osx = -DTCC_TARGET_MACHO -DTCC_TARGET_X86_64
DEF-arm-wince = -DTCC_TARGET_PE -DTCC_TARGET_ARM -DTCC_ARM_EABI -DTCC_ARM_VFP -DTCC_ARM_HARDFLOAT
DEF-arm64 = -DTCC_TARGET_ARM64
DEF-c67 = -DTCC_TARGET_C67 -w # disable warnigs
DEF-arm-fpa = -DTCC_TARGET_ARM
DEF-arm-fpa-ld = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12
DEF-arm-vfp = -DTCC_TARGET_ARM -DTCC_ARM_VFP
DEF-arm-eabi = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI
DEF-arm-eabihf = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT
DEF-arm = $(DEF-arm-eabihf)
DEF-$(NATIVE_TARGET) = $(NATIVE_DEFINES)
DEFINES += $(DEF-$T) $(DEF-all)
ifneq ($(T),$(NATIVE_TARGET))
$(if $(DEF-$T),,$(error error: unknown target: '$T'))
ifneq ($(CONFIG_WIN32),yes)
DEF-win = -DCONFIG_TCCDIR="\"$(tccdir)/win32\""
endif
# some default config for cross compilers
TRIPLET-i386 = i686-linux-gnu
TRIPLET-x86_64 = x86_64-linux-gnu
TRIPLET-arm = arm-linux-gnueabihf
TRIPLET-arm64 = aarch64-linux-gnu
TRIPLET-riscv64 = riscv64-linux-gnu
ifneq ($(TRIPLET-$T),)
# assume support files in "/usr/<triplet>"
ROOT-$T = /usr/$(TRIPLET-$T)
INC-$T = {B}/include:{R}/include
LIB-$T = {R}/lib:{B}
CRT-$T = {R}/lib
endif
DEFINES += $(DEF-$T)
DEFINES += $(if $(ROOT-$T),-DCONFIG_SYSROOT="\"$(ROOT-$T)\"")
DEFINES += $(if $(CRT-$T),-DCONFIG_TCC_CRTPREFIX="\"$(CRT-$T)\"")
DEFINES += $(if $(LIB-$T),-DCONFIG_TCC_LIBPATHS="\"$(LIB-$T)\"")
DEFINES += $(if $(INC-$T),-DCONFIG_TCC_SYSINCLUDEPATHS="\"$(INC-$T)\"")
DEFINES += $(if $(ELF-$T),-DCONFIG_TCC_ELFINTERP="\"$(ELF-$T)\"")
DEFINES += $(DEF-$(or $(findstring win,$T),unx))
ifneq ($(X),)
ifeq ($(CONFIG_WIN32),yes)
DEF-win += -DTCC_LIBTCC1="\"$(X)libtcc1.a\""
DEF-unx += -DTCC_LIBTCC1="\"lib/$(X)libtcc1.a\""
else
DEF-all += -DTCC_LIBTCC1="\"$(X)libtcc1.a\""
DEF-win += -DCONFIG_TCCDIR="\"$(tccdir)/win32\""
endif
DEFINES += -DCONFIG_TCC_CROSSPREFIX="\"$X\""
endif
# include custom configuration (see make help)
-include config-extra.mak
CORE_FILES = tcc.c tcctools.c libtcc.c tccpp.c tccgen.c tccelf.c tccasm.c tccrun.c
# so one can use: make EXTRA-DEFS=...
DEFINES += $(EXTRA-DEFS)
# find config.h with 'out of tree' builds
DEFINES += -I$(TOP)
CORE_FILES = tcc.c tcctools.c libtcc.c tccpp.c tccgen.c tccdbg.c tccelf.c tccasm.c tccrun.c
CORE_FILES += tcc.h config.h libtcc.h tcctok.h
i386_FILES = $(CORE_FILES) i386-gen.c i386-link.c i386-asm.c i386-asm.h i386-tok.h
i386-win32_FILES = $(i386_FILES) tccpe.c
x86_64_FILES = $(CORE_FILES) x86_64-gen.c x86_64-link.c i386-asm.c x86_64-asm.h
x86_64-win32_FILES = $(x86_64_FILES) tccpe.c
x86_64-osx_FILES = $(x86_64_FILES)
arm_FILES = $(CORE_FILES) arm-gen.c arm-link.c arm-asm.c
x86_64-osx_FILES = $(x86_64_FILES) tccmacho.c
arm_FILES = $(CORE_FILES) arm-gen.c arm-link.c arm-asm.c arm-tok.h
arm-wince_FILES = $(arm_FILES) tccpe.c
arm64_FILES = $(CORE_FILES) arm64-gen.c arm64-link.c
arm-fpa_FILES = $(arm_FILES)
arm-fpa-ld_FILES = $(arm_FILES)
arm-vfp_FILES = $(arm_FILES)
arm-eabi_FILES = $(arm_FILES)
arm-eabihf_FILES = $(arm_FILES)
arm64_FILES = $(CORE_FILES) arm64-gen.c arm64-link.c arm64-asm.c arm64-tok.h
arm64-osx_FILES = $(arm64_FILES) tccmacho.c
arm64-win32_FILES = $(arm64_FILES) tccpe.c
c67_FILES = $(CORE_FILES) c67-gen.c c67-link.c tcccoff.c
riscv64_FILES = $(CORE_FILES) riscv64-gen.c riscv64-link.c riscv64-asm.c
TCCDEFS_H$(subst yes,,$(CONFIG_predefs)) = tccdefs_.h
# libtcc sources
LIBTCC_SRC = $(filter-out tcc.c tcctools.c,$(filter %.c,$($T_FILES)))
@ -174,80 +227,133 @@ ifeq ($(ONE_SOURCE),yes)
LIBTCC_OBJ = $(X)libtcc.o
LIBTCC_INC = $($T_FILES)
TCC_FILES = $(X)tcc.o
tcc.o : DEFINES += -DONE_SOURCE=0
$(X)tcc.o $(X)libtcc.o : $(TCCDEFS_H)
else
LIBTCC_OBJ = $(patsubst %.c,$(X)%.o,$(LIBTCC_SRC))
LIBTCC_INC = $(filter %.h %-gen.c %-link.c,$($T_FILES))
TCC_FILES = $(X)tcc.o $(LIBTCC_OBJ)
$(TCC_FILES) : DEFINES += -DONE_SOURCE=0
$(X)tccpp.o : $(TCCDEFS_H)
$(X)libtcc.o : DEFINES += -DONE_SOURCE=0
$(CROSS_TARGET)-tcc.o : DEFINES += -DONE_SOURCE=0
endif
# native tcc always made from tcc.o and libtcc.[so|a]
tcc.o : DEFINES += -DONE_SOURCE=0
GITHASH:=$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo no)
ifneq ($(GITHASH),no)
GITHASH:=$(shell git log -1 --date=short --pretty='format:%cd $(GITHASH)@%h')
GITMODF:=$(shell git diff --quiet || echo '*')
DEF_GITHASH:= -DTCC_GITHASH="\"$(GITHASH)$(GITMODF)\""
endif
ifeq ($(CONFIG_debug),yes)
CFLAGS += -g
LDFLAGS += -g
endif
# convert "include/tccdefs.h" to "tccdefs_.h"
%_.h : include/%.h c2str.exe
$S./c2str.exe $< $@
c2str.exe : conftest.c
$S$(CC) -DC2STR $< -o $@
# target specific object rule
$(X)%.o : %.c $(LIBTCC_INC)
$(CC) -o $@ -c $< $(DEFINES) $(CFLAGS)
$S$(CC) -o $@ -c $< $(addsuffix ,$(DEFINES) $(CFLAGS))
# additional dependencies
$(X)tcc.o : tcctools.c
$(X)tcc.o : DEFINES += $(DEF_GITHASH)
# Host Tiny C Compiler
tcc$(EXESUF): tcc.o $(LIBTCC)
$(CC) -o $@ $^ $(LIBS) $(LDFLAGS) $(LINK_LIBTCC)
$S$(CC) -o $@ $^ $(addsuffix ,$(LIBS) $(LDFLAGS) $(LINK_LIBTCC))
# Cross Tiny C Compilers
%-tcc$(EXESUF): FORCE
# (the TCCDEFS_H dependency is only necessary for parallel makes,
# ala 'make -j x86_64-tcc i386-tcc tcc', which would create multiple
# c2str.exe and tccdefs_.h files in parallel, leading to access errors.
# This forces it to be made only once. Make normally tracks multiple paths
# to the same goals and only remakes it once, but that doesn't work over
# sub-makes like in this target)
%-tcc$(EXESUF): $(TCCDEFS_H) FORCE
@$(MAKE) --no-print-directory $@ CROSS_TARGET=$* ONE_SOURCE=$(or $(ONE_SOURCE),yes)
$(CROSS_TARGET)-tcc$(EXESUF): $(TCC_FILES)
$(CC) -o $@ $^ $(LIBS) $(LDFLAGS)
$S$(CC) -o $@ $^ $(LIBS) $(LDFLAGS)
# profiling version
tcc_p$(EXESUF): $($T_FILES)
$(CC) -o $@ $< $(DEFINES) $(CFLAGS_P) $(LIBS_P) $(LDFLAGS_P)
$S$(CC) -o $@ $< $(DEFINES) $(CFLAGS_P) $(LIBS_P) $(LDFLAGS_P)
# static libtcc library
libtcc.a: $(LIBTCC_OBJ)
$(AR) rcs $@ $^
$S$(AR) rcs $@ $^
ifeq ($(CC_NAME)-$(ARCH),clang-x86_64)
# avoid 32-bit relocations in libtcc.a for its usage with tcc -run
libtcc.a: override CFLAGS += -fPIC
endif
# dynamic libtcc library
libtcc.so: $(LIBTCC_OBJ)
$(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDFLAGS)
$S$(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LIBS) $(LDFLAGS)
libtcc.so: CFLAGS+=-fPIC
libtcc.so: LDFLAGS+=-fPIC
libtcc.so: override CFLAGS += -fPIC
libtcc.so: override LDFLAGS += -fPIC
# OSX dynamic libtcc library
libtcc.dylib: $(LIBTCC_OBJ)
$S$(CC) -dynamiclib $(DYLIBVER) -install_name @rpath/$@ -o $@ $^ $(LDFLAGS)
# OSX libtcc.dylib (without rpath/ prefix)
libtcc.osx: $(LIBTCC_OBJ)
$S$(CC) -shared -install_name libtcc.dylib -o libtcc.dylib $^ $(LDFLAGS)
# windows dynamic libtcc library
libtcc.dll : $(LIBTCC_OBJ)
$(CC) -shared -o $@ $^ $(LDFLAGS)
$S$(CC) -shared -o $@ $^ $(LDFLAGS)
libtcc.dll : DEFINES += -DLIBTCC_AS_DLL
# import file for windows libtcc.dll
libtcc.def : libtcc.dll tcc$(EXESUF)
$(XTCC) -impdef $< -o $@
$S$(XTCC) -impdef $< -o $@
XTCC ?= ./tcc$(EXESUF)
# TinyCC runtime libraries
libtcc1.a : tcc$(EXESUF) FORCE
@$(MAKE) -C lib DEFINES='$(DEF-$T)'
@$(MAKE) -C lib
# Cross libtcc1.a
%-libtcc1.a : %-tcc$(EXESUF) FORCE
@$(MAKE) -C lib DEFINES='$(DEF-$*)' CROSS_TARGET=$*
@$(MAKE) -C lib CROSS_TARGET=$*
.PRECIOUS: %-libtcc1.a
FORCE:
# WHICH = which $1 2>/dev/null
# some versions of gnu-make do not recognize 'command' as a shell builtin
WHICH = sh -c 'command -v $1'
run-if = $(if $(shell $(call WHICH,$1x)),$S $1 $2,@true||echo "(skipping $@ - no $1)")
S = $(if $(findstring yes,$(SILENT)),@$(info * $@))
# --------------------------------------------------------------------------
# documentation and man page
tcc-doc.html: tcc-doc.texi
makeinfo --no-split --html --number-sections -o $@ $< || true
tcc.1: tcc-doc.texi
$(TOPSRC)/texi2pod.pl $< tcc.pod \
&& pod2man --section=1 --center="Tiny C Compiler" --release="$(VERSION)" tcc.pod >tmp.1 \
&& mv tmp.1 $@ || rm -f tmp.1
$(call run-if,makeinfo,--no-split --html --number-sections -o $@ $<)
tcc-doc.info: tcc-doc.texi
makeinfo $< || true
$(call run-if,makeinfo,$< || true)
tcc.1 : tcc-doc.pod
$(call run-if,pod2man,--section=1 --center="Tiny C Compiler" \
--release="$(VERSION)" $< >$@)
%.pod : %.texi
$(call run-if,perl,$(TOPSRC)/texi2pod.pl $< $@)
doc : $(TCCDOCS)
# --------------------------------------------------------------------------
# install
@ -257,17 +363,22 @@ INSTALLBIN = install -m755 $(STRIP_$(CONFIG_strip))
STRIP_yes = -s
LIBTCC1_W = $(filter %-win32-libtcc1.a %-wince-libtcc1.a,$(LIBTCC1_CROSS))
LIBTCC1_U = $(filter-out $(LIBTCC1_W),$(LIBTCC1_CROSS))
IB = $(if $1,mkdir -p $2 && $(INSTALLBIN) $1 $2)
LIBTCC1_U = $(filter-out $(LIBTCC1_W),$(wildcard *-libtcc1.a))
IB = $(if $1,$(IM) mkdir -p $2 && $(INSTALLBIN) $1 $2)
IBw = $(call IB,$(wildcard $1),$2)
IF = $(if $1,mkdir -p $2 && $(INSTALL) $1 $2)
IF = $(if $1,$(IM) mkdir -p $2 && $(INSTALL) $1 $2)
IFw = $(call IF,$(wildcard $1),$2)
IR = mkdir -p $2 && cp -r $1/. $2
IR = $(IM) mkdir -p $2 && cp -r $1/. $2
IM = @echo "-> $2 : $1" ;
BINCHECK = $(if $(wildcard $(PROGS) *-tcc$(EXESUF)),,@echo "Makefile: nothing found to install" && exit 1)
EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o
# install progs & libs
install-unx:
$(call IBw,$(PROGS) $(PROGS_CROSS),"$(bindir)")
$(call IFw,$(LIBTCC1) $(LIBTCC1_U),"$(tccdir)")
$(call BINCHECK)
$(call IBw,$(PROGS) *-tcc,"$(bindir)")
$(call IFw,$(LIBTCC1) $(EXTRA_O) $(LIBTCC1_U),"$(tccdir)")
$(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include")
$(call $(if $(findstring .so,$(LIBTCC)),IBw,IFw),$(LIBTCC),"$(libdir)")
$(call IF,$(TOPSRC)/libtcc.h,"$(includedir)")
@ -282,40 +393,43 @@ endif
# uninstall
uninstall-unx:
@rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS),"$(bindir)/$P")
@rm -fv "$(libdir)/libtcc.a" "$(libdir)/libtcc.so" "$(includedir)/libtcc.h"
@rm -fv $(addprefix "$(bindir)/",$(PROGS) $(PROGS_CROSS))
@rm -fv $(addprefix "$(libdir)/", libtcc*.a libtcc*.so libtcc.dylib)
@rm -fv $(addprefix "$(includedir)/", libtcc.h)
@rm -fv "$(mandir)/man1/tcc.1" "$(infodir)/tcc-doc.info"
@rm -fv "$(docdir)/tcc-doc.html"
rm -r "$(tccdir)"
@rm -frv "$(tccdir)"
# install progs & libs on windows
install-win:
$(call IBw,$(PROGS) $(PROGS_CROSS) $(subst libtcc.a,,$(LIBTCC)),"$(bindir)")
$(call BINCHECK)
$(call IBw,$(PROGS) *-tcc.exe libtcc.dll,"$(bindir)")
$(call IF,$(TOPSRC)/win32/lib/*.def,"$(tccdir)/lib")
$(call IFw,libtcc1.a $(LIBTCC1_W),"$(tccdir)/lib")
$(call IFw,libtcc1.a $(EXTRA_O) $(LIBTCC1_W),"$(tccdir)/lib")
$(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include")
$(call IR,$(TOPSRC)/win32/include,"$(tccdir)/include")
$(call IR,$(TOPSRC)/win32/examples,"$(tccdir)/examples")
$(call IF,$(TOPSRC)/tests/libtcc_test.c,"$(tccdir)/examples")
$(call IFw,$(TOPSRC)/libtcc.h $(subst .dll,.def,$(LIBTCC)),"$(libdir)")
$(call IFw,$(TOPSRC)/libtcc.h libtcc.def libtcc.a,"$(libdir)")
$(call IFw,$(TOPSRC)/win32/tcc-win32.txt tcc-doc.html,"$(docdir)")
ifneq "$(wildcard $(LIBTCC1_U))" ""
$(call IFw,$(LIBTCC1_U),"$(tccdir)/lib")
$(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/lib/include")
endif
# the msys-git shell works to configure && make except it does not have install
ifeq "$(and $(CONFIG_WIN32),$(shell which install >/dev/null 2>&1 || echo no))" "no"
install-win : INSTALL = cp
install-win : INSTALLBIN = cp
endif
# uninstall on windows
uninstall-win:
@rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS) libtcc.dll,"$(bindir)/$P")
@rm -fv $(foreach F,tcc-doc.html tcc-win32.txt,"$(docdir)/$F")
@rm -fv $(foreach F,libtcc.h libtcc.def libtcc.a,"$(libdir)/$F")
rm -r "$(tccdir)"
@rm -fv $(foreach P,libtcc*.dll $(PROGS) *-tcc.exe,"$(bindir)"/$P)
@rm -fr $(foreach P,doc examples include lib libtcc,"$(tccdir)"/$P/*)
@rm -frv $(foreach P,doc examples include lib libtcc,"$(tccdir)"/$P)
# the msys-git shell works to configure && make except it does not have install
ifeq ($(OS),Windows_NT)
ifeq ($(shell $(call WHICH,install) || echo no),no)
INSTALL = cp
INSTALLBIN = cp
endif
endif
# --------------------------------------------------------------------------
# other stuff
@ -328,8 +442,9 @@ ETAGS : ; etags $(TAGFILES)
# create release tarball from *current* git branch (including tcc-doc.html
# and converting two files to CRLF)
TCC-VERSION = tcc-$(VERSION)
TCC-VERSION = tinycc-mob-$(shell git rev-parse --short=7 HEAD)
tar: tcc-doc.html
mkdir $(TCC-VERSION)
mkdir -p $(TCC-VERSION)
( cd $(TCC-VERSION) && git --git-dir ../.git checkout -f )
cp tcc-doc.html $(TCC-VERSION)
for f in tcc-win32.txt build-tcc.bat ; do \
@ -344,60 +459,79 @@ config.mak:
# run all tests
test:
$(MAKE) -C tests
@$(MAKE) -C tests
# run test(s) from tests2 subdir (see make help)
tests2.%:
$(MAKE) -C tests/tests2 $@
@$(MAKE) -C tests/tests2 $@
# run test(s) from testspp subdir (see make help)
testspp.%:
@$(MAKE) -C tests/pp $@
# run tests with code coverage
tcov-tes% : tcc_c$(EXESUF)
@rm -f $<.tcov
@$(MAKE) --no-print-directory TCC_LOCAL=$(CURDIR)/$< tes$*
tcc_c$(EXESUF): $($T_FILES)
$S$(TCC) tcc.c -o $@ -ftest-coverage $(DEFINES) $(LIBS)
# run tests with sanitize option
sani-tes% : tcc_s$(EXESUF)
@$(MAKE) --no-print-directory TCC_LOCAL=$(CURDIR)/$< tes$*
tcc_s$(EXESUF): $($T_FILES)
$S$(CC) tcc.c -o $@ -fsanitize=address,undefined $(DEFINES) $(CFLAGS) $(LDFLAGS) $(LIBS)
# test the installed tcc instead
test-install: $(TCCDEFS_H)
@$(MAKE) -C tests TESTINSTALL=yes #_all
clean:
rm -f tcc$(EXESUF) tcc_p$(EXESUF) *-tcc$(EXESUF) tcc.pod
rm -f *~ *.o *.a *.so* *.out *.log lib*.def *.exe *.dll a.out tags TAGS
@$(MAKE) -C lib $@
@$(MAKE) -C tests $@
@rm -f tcc *-tcc tcc_p tcc_c tcc_s
@rm -f tags ETAGS *.o *.a *.so* *.out *.log lib*.def *.exe *.dll
@rm -f a.out *.dylib *_.h *.pod *.tcov
@$(MAKE) -s -C lib $@
@$(MAKE) -s -C tests $@
distclean: clean
rm -f config.h config.mak config.texi tcc.1 tcc-doc.info tcc-doc.html
@rm -vf config.h config.mak config.texi
@rm -vf $(TCCDOCS)
.PHONY: all clean test tar tags ETAGS distclean install uninstall FORCE
.PHONY: all clean test tar tags ETAGS doc distclean install uninstall FORCE
help:
@echo "make"
@echo " build native compiler (from separate objects)"
@echo ""
@echo "make cross"
@echo " build cross compilers (from one source)"
@echo ""
@echo "make ONE_SOURCE=yes / no"
@echo " force building from one source / separate objects"
@echo ""
@echo "make ONE_SOURCE=no/yes SILENT=no/yes"
@echo " force building from separate/one object(s), less/more silently"
@echo "make cross-TARGET"
@echo " build one specific cross compiler for 'TARGET', as in"
@echo " $(TCC_X)"
@echo ""
@echo "Custom configuration:"
@echo " The makefile includes a file 'config-extra.mak' if it is present."
@echo " This file may contain some custom configuration. For example:"
@echo ""
@echo " NATIVE_DEFINES += -D..."
@echo ""
@echo " Or for example to configure the search paths for a cross-compiler"
@echo " that expects the linux files in <tccdir>/i386-linux:"
@echo ""
@echo " ROOT-i386 = {B}/i386-linux"
@echo " CRT-i386 = {B}/i386-linux/usr/lib"
@echo " LIB-i386 = {B}/i386-linux/lib:{B}/i386-linux/usr/lib"
@echo " INC-i386 = {B}/lib/include:{B}/i386-linux/usr/include"
@echo " DEF-i386 += -D__linux__"
@echo ""
@echo " build one specific cross compiler for 'TARGET'. Currently supported:"
@echo " $(wordlist 1,8,$(TCC_X))"
@echo " $(wordlist 9,99,$(TCC_X))"
@echo "make cross"
@echo " build all cross compilers"
@echo "make test"
@echo " run all tests"
@echo ""
@echo "make tests2.all / make tests2.37 / make tests2.37+"
@echo " run all/single test(s) from tests2, optionally update .expect"
@echo ""
@echo "make testspp.all / make testspp.17"
@echo " run all/single test(s) from tests/pp"
@echo "make tcov-test / tcov-tests2.37 / tcov-testspp.17"
@echo " run tests as above with code coverage. After test(s) see tcc_c$(EXESUF).tcov"
@echo "make sani-test / sani-tests2.37 / sani-testspp.17"
@echo " run tests as above with sanitize option."
@echo "make test-install"
@echo " run tests with the installed tcc"
@echo "Other supported make targets:"
@echo " install install-strip tags ETAGS tar clean distclean help"
@echo ""
@echo " install install-strip uninstall doc [dist]clean tags ETAGS tar help"
@echo "Custom configuration:"
@echo " The makefile includes a file 'config-extra.mak' if it is present."
@echo " This file may contain some custom configuration. For example to"
@echo " configure the search paths for a cross-compiler, assuming the"
@echo " support files in /usr/i686-linux-gnu:"
@echo " ROOT-i386 = /usr/i686-linux-gnu"
@echo " CRT-i386 = {R}/lib"
@echo " LIB-i386 = {B}:{R}/lib"
@echo " INC-i386 = {B}/include:{R}/include (*)"
@echo " DEF-i386 += -D__linux__"
@echo " Or also, for the cross platform files in /usr/<triplet>"
@echo " TRIPLET-i386 = i686-linux-gnu"
@echo " (*) tcc replaces {B} by 'tccdir' and {R} by 'CONFIG_SYSROOT'"
# --------------------------------------------------------------------------
endif # ($(INCLUDED),no)

16
README
View File

@ -7,12 +7,11 @@ Features:
- SMALL! You can compile and execute C code everywhere, for example on
rescue disks.
- FAST! tcc generates optimized x86 code. No byte code
overhead. Compile, assemble and link about 7 times faster than 'gcc
-O0'.
- FAST! tcc generates machine code for i386, x86_64, arm, aarch64 or
riscv64. Compiles and links about 10 times faster than 'gcc -O0'.
- UNLIMITED! Any C dynamic library can be used directly. TCC is
heading torward full ISOC99 compliance. TCC can of course compile
heading toward full ISOC99 compliance. TCC can of course compile
itself.
- SAFE! tcc includes an optional memory and bound checker. Bound
@ -28,14 +27,14 @@ Features:
Documentation:
-------------
1) Installation on a i386/x86_64/arm Linux/OSX/FreeBSD host
1) Installation on Linux, BSD variants or macOS hosts:
./configure
make
make test
make install
Notes: For OSX and FreeBSD, gmake should be used instead of make.
Notes: On BSD hosts, gmake should be used instead of make.
For Windows read tcc-win32.txt.
makeinfo must be installed to compile the doc. By default, tcc is
@ -82,9 +81,8 @@ when doing 'make test'.
4) Full Documentation
Please read tcc-doc.html to have all the features of TCC.
Additional information is available for the Windows port in tcc-win32.txt.
Please read tcc-doc.html to have all the features of TCC. Additional
information for the Windows port is in tcc-win32.txt.
License:
-------

View File

@ -30,23 +30,30 @@
Adam Sampson YES makefiles
Daniel Glöckner NO arm-gen.c
Daniel Glöckner YES not arm-gen.c
Danny Milosavljevic YES arm-asm.c riscv64-asm.c
Edmund Grimley Evans YES arm64
Fabrice Bellard YES original author
Frédéric Féret YES x86 64/16 bit asm
grischka YES tccpe.c
Henry Kroll YES
Herman ten Brugge YES
Joe Soroka YES
Kirill Smelkov YES
mingodad YES
Pip Cet YES
Shinichiro Hamaji YES x86_64-gen.c
Steffen Nurpmeso YES
Vincent Lefèvre YES
Thomas Preud'homme YES arm-gen.c
Timo VJ Lähde (Timppa) ? tiny_libmaker.c
TK ? tcccoff.c c67-gen.c
Tyge Løvset YES tgmath.h, Windows tcc_libm.h math.h
Urs Janssen YES
waddlesplash YES
Christian Jullien YES Windows Cygwin build and tests
Reimar Döffinger YES
noneofyourbusiness YES
Cyan Ogilvie YES
------------------------------------------------------------------------------

10
TODO
View File

@ -1,5 +1,10 @@
TODO list:
Releases:
- release tcc on a regular basis
- testing repo.or.cz
Bugs:
- i386 fastcall is mostly wrong
@ -33,7 +38,10 @@ Portability:
Linking:
- static linking (-static) does not work
- static linking (-static) does sort of work
works with musl libc
glibc requires libc.so even when statically linked (very bad, but not
up to tcc)
Bound checking:

68
USES Normal file
View File

@ -0,0 +1,68 @@
The following software are known to use or support tcc builds.
Feel free to complete this list (*).
Name Short Description
---- -----------------
bigz An infinite precision Z & Q library.
gawk GNU awk.
gmp Library for arbitrary precision arithmetic.
gnumake GNU makefile.
gnu mes using tinycc to bootstrap a system
mpfr Multiple-precision floating-point library.
mpc Complex floating-point library with exact rounding.
mpv A free, open source, and cross-platform media player.
openlisp ISLISP ISO/IEC 13816 Lisp interpreter and compiler.
s-nail BSD Mail/POSIX mailx: send and receive Internet mail.
sqlite Embbedable SQL engine.
st Simple Terminal.
tcc Tiny CC which compiles itself.
zlib Lossless data-compression library.
(*) This list is ordered by name.
Forks & Experiments
-------------------
arm-thumb target
by Erlend Sveen <erlend.sveen@hotmail.com>
https://git.erlendjs.no/erlendjs/tinycc.git
riscv32 target
by Sam Ellicott <sellicott@cedarville.edu>
https://github.com/sellicott/tcc-riscv32.git
Transputer target
by David Smith <agentdavo@mac.com>
https://github.com/agentdavo/tinycc-transputer
tcc-65816 - Tiny C Compiler for 65816 CPU (based on V0.9.23) from SNES-SDK
https://github.com/nArnoSNES/tcc-65816
PE-UEFI arm64
by Andrei Warkentin <andrey.warkentin@gmail.com>
https://github.com/andreiw/tinycc/
TCCLS - global register allocator (proof of concept)
by Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
https://bitbucket.org/theStack/tccls_poc.git
softfloat
by Giovanni Mascellani <gio@debian.org>
https://gitlab.com/giomasce/tinycc.git
optimize 386
by Jason Hood <jadoxa@yahoo.com.au>
tcctcl : tcl binding
https://code.google.com/archive/p/tcltcc/
tcc4tcl : tcl binding
https://chiselapp.com/user/rkeene/repository/tcc4tcl/index
lua-tcc : allows a Lua script to compile C code
https://github.com/javierguerragiraldez/lua-tcc
tcclua : semi-high-level bindings for `libtcc`
https://github.com/nucular/tcclua/blob/master/tcc.lua

View File

@ -1 +1 @@
0.9.27
0.9.28rc

3020
arm-asm.c

File diff suppressed because it is too large Load Diff

616
arm-gen.c

File diff suppressed because it is too large Load Diff

View File

@ -12,24 +12,20 @@
#define R_NUM R_ARM_NUM
#define ELF_START_ADDR 0x00008000
#define ELF_PAGE_SIZE 0x1000
#define ELF_START_ADDR 0x00010000
#define ELF_PAGE_SIZE 0x10000
#define PCRELATIVE_DLLPLT 1
#define RELOCATE_DLLPLT 0
enum float_abi {
ARM_SOFTFP_FLOAT,
ARM_HARD_FLOAT,
};
#define RELOCATE_DLLPLT 1
#else /* !TARGET_DEFS_ONLY */
#include "tcc.h"
#ifdef NEED_RELOC_TYPE
/* Returns 1 for a code relocation, 0 for a data relocation. For unknown
relocations, returns -1. */
int code_reloc (int reloc_type)
ST_FUNC int code_reloc (int reloc_type)
{
switch (reloc_type) {
case R_ARM_MOVT_ABS:
@ -41,9 +37,14 @@ int code_reloc (int reloc_type)
case R_ARM_GOTPC:
case R_ARM_GOTOFF:
case R_ARM_GOT32:
case R_ARM_GOT_PREL:
case R_ARM_COPY:
case R_ARM_GLOB_DAT:
case R_ARM_NONE:
case R_ARM_TARGET1:
case R_ARM_MOVT_PREL:
case R_ARM_MOVW_PREL_NC:
case R_ARM_TLS_LE32:
return 0;
case R_ARM_PC24:
@ -57,21 +58,20 @@ int code_reloc (int reloc_type)
case R_ARM_JUMP_SLOT:
return 1;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
/* Returns an enumerator to describe whether and when the relocation needs a
GOT and/or PLT entry to be created. See tcc.h for a description of the
different values. */
int gotplt_entry_type (int reloc_type)
ST_FUNC int gotplt_entry_type (int reloc_type)
{
switch (reloc_type) {
case R_ARM_NONE:
case R_ARM_COPY:
case R_ARM_GLOB_DAT:
case R_ARM_JUMP_SLOT:
case R_ARM_TLS_LE32:
return NO_GOTPLT_ENTRY;
case R_ARM_PC24:
@ -88,6 +88,9 @@ int gotplt_entry_type (int reloc_type)
case R_ARM_ABS32:
case R_ARM_REL32:
case R_ARM_V4BX:
case R_ARM_TARGET1:
case R_ARM_MOVT_PREL:
case R_ARM_MOVW_PREL_NC:
return AUTO_GOTPLT_ENTRY;
case R_ARM_GOTPC:
@ -95,13 +98,13 @@ int gotplt_entry_type (int reloc_type)
return BUILD_GOT_ONLY;
case R_ARM_GOT32:
case R_ARM_GOT_PREL:
return ALWAYS_GOTPLT_ENTRY;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
#ifdef NEED_BUILD_GOT
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
{
Section *plt = s1->plt;
@ -110,8 +113,6 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
/* when building a DLL, GOT entry accesses must be done relative to
start of GOT (see x86_64 example above) */
if (s1->output_type == TCC_OUTPUT_DLL)
tcc_error("DLLs unimplemented!");
/* empty PLT: create PLT0 entry that push address of call site and
jump to ld.so resolution routine (GOT + 8) */
@ -131,12 +132,8 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
write32le(p+2, 0x46c0); /* nop */
}
p = section_ptr_add(plt, 16);
/* Jump to GOT entry where ld.so initially put address of PLT0 */
write32le(p, 0xe59fc004); /* ldr ip, [pc, #4] */
write32le(p+4, 0xe08fc00c); /* add ip, pc, ip */
write32le(p+8, 0xe59cf000); /* ldr pc, [ip] */
/* p + 12 contains offset to GOT entry once patched by relocate_plt */
write32le(p+12, got_offset);
/* save GOT offset for relocate_plt */
write32le(p + 4, got_offset);
return plt_offset;
}
@ -154,23 +151,35 @@ ST_FUNC void relocate_plt(TCCState *s1)
if (p < p_end) {
int x = s1->got->sh_addr - s1->plt->sh_addr - 12;
write32le(s1->plt->data + 16, x - 16);
write32le(s1->plt->data + 16, x - 4);
p += 20;
while (p < p_end) {
unsigned off = x + read32le(p + 4) + (s1->plt->data - p) + 4;
if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */
p += 4;
add32le(p + 12, x + s1->plt->data - p);
write32le(p, 0xe28fc200 | ((off >> 28) & 0xf)); // add ip, pc, #0xN0000000
write32le(p + 4, 0xe28cc600 | ((off >> 20) & 0xff)); // add ip, pc, #0xNN00000
write32le(p + 8, 0xe28cca00 | ((off >> 12) & 0xff)); // add ip, ip, #0xNN000
write32le(p + 12, 0xe5bcf000 | (off & 0xfff)); // ldr pc, [ip, #0xNNN]!
p += 16;
}
}
if (s1->plt->reloc) {
ElfW_Rel *rel;
p = s1->got->data;
for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
write32le(p + rel->r_offset, s1->plt->sh_addr);
}
}
}
#endif
#endif
void relocate_init(Section *sr) {}
void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
{
ElfW(Sym) *sym;
int sym_index;
int sym_index, esym_index;
sym_index = ELFW(R_SYM)(rel->r_info);
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
@ -182,17 +191,18 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
case R_ARM_PLT32:
{
int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko;
x = (*(int *) ptr) & 0xffffff;
unsigned code = read32le(ptr);
x = code & 0x00ffffff;
#ifdef DEBUG_RELOC
printf ("reloc %d: x=0x%x val=0x%x ", type, x, val);
#endif
(*(int *)ptr) &= 0xff000000;
if (x & 0x800000)
x -= 0x1000000;
code &= 0xff000000;
x <<= 2;
blx_avail = (TCC_CPU_VERSION >= 5);
if (x & 0x2000000)
x -= 0x4000000;
blx_avail = (CONFIG_TCC_CPUVER >= 5);
is_thumb = val & 1;
is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
is_bl = code == 0xeb000000;
is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));
x += val - addr;
#ifdef DEBUG_RELOC
@ -202,15 +212,15 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
h = x & 2;
th_ko = (x & 3) && (!blx_avail || !is_call);
if (th_ko || x >= 0x2000000 || x < -0x2000000)
tcc_error("can't relocate value at %x,%d",addr, type);
tcc_error_noabort("can't relocate value at %x,%d",addr, type);
x >>= 2;
x &= 0xffffff;
/* Only reached if blx is avail and it is a call */
if (is_thumb) {
x |= h << 24;
(*(int *)ptr) = 0xfa << 24; /* bl -> blx */
code = 0xfa000000; /* bl -> blx */
}
(*(int *) ptr) |= x;
write32le(ptr, code | x);
}
return;
/* Since these relocations only concern Thumb-2 and blx instruction was
@ -229,8 +239,8 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
return;
/* Get initial offset */
hi = (*(uint16_t *)ptr);
lo = (*(uint16_t *)(ptr+2));
hi = read16le(ptr);
lo = read16le(ptr+2);
s = (hi >> 10) & 1;
j1 = (lo >> 13) & 1;
j2 = (lo >> 11) & 1;
@ -254,24 +264,24 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
int index;
uint8_t *p;
char *name, buf[1024];
Section *text_section;
Section *text;
name = (char *) symtab_section->link->data + sym->st_name;
text_section = s1->sections[sym->st_shndx];
text = s1->sections[sym->st_shndx];
/* Modify reloc to target a thumb stub to switch to ARM */
snprintf(buf, sizeof(buf), "%s_from_thumb", name);
index = put_elf_sym(symtab_section,
text_section->data_offset + 1,
text->data_offset + 1,
sym->st_size, sym->st_info, 0,
sym->st_shndx, buf);
to_thumb = 1;
val = text_section->data_offset + 1;
val = text->data_offset + 1;
rel->r_info = ELFW(R_INFO)(index, type);
/* Create a thumb stub function to switch to ARM mode */
put_elf_reloc(symtab_section, text_section,
text_section->data_offset + 4, R_ARM_JUMP24,
put_elf_reloc(symtab_section, text,
text->data_offset + 4, R_ARM_JUMP24,
sym_index);
p = section_ptr_add(text_section, 8);
p = section_ptr_add(text, 8);
write32le(p, 0x4778); /* bx pc */
write32le(p+2, 0x46c0); /* nop */
write32le(p+4, 0xeafffffe); /* b $sym */
@ -291,7 +301,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
- instruction must be a call (bl) or a jump to PLT */
if (!to_thumb || x >= 0x1000000 || x < -0x1000000)
if (to_thumb || (val & 2) || (!is_call && !to_plt))
tcc_error("can't relocate value at %x,%d",addr, type);
tcc_error_noabort("can't relocate value at %x,%d",addr, type);
/* Compute and store final offset */
s = (x >> 24) & 1;
@ -301,9 +311,9 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
j2 = s ^ (i2 ^ 1);
imm10 = (x >> 12) & 0x3ff;
imm11 = (x >> 1) & 0x7ff;
(*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) |
write16le(ptr, (hi & 0xf800) |
(s << 10) | imm10);
(*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) |
write16le(ptr+2, (lo & 0xc000) |
(j1 << 13) | blx_bit | (j2 << 11) |
imm11);
}
@ -318,9 +328,23 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
imm4 = (val >> 12) & 0xf;
x = (imm4 << 16) | imm12;
if (type == R_ARM_THM_MOVT_ABS)
*(int *)ptr |= x;
write32le(ptr, read32le(ptr) | x);
else
*(int *)ptr += x;
add32le(ptr, x);
}
return;
case R_ARM_MOVT_PREL:
case R_ARM_MOVW_PREL_NC:
{
int insn = read32le(ptr);
int addend = ((insn >> 4) & 0xf000) | (insn & 0xfff);
addend = (addend ^ 0x8000) - 0x8000;
val += addend - addr;
if (type == R_ARM_MOVT_PREL)
val >>= 16;
write32le(ptr, (insn & 0xfff0f000) |
((val & 0xf000) << 4) | (val & 0xfff));
}
return;
case R_ARM_THM_MOVT_ABS:
@ -335,48 +359,68 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
imm4 = (val >> 12) & 0xf;
x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4;
if (type == R_ARM_THM_MOVT_ABS)
*(int *)ptr |= x;
write32le(ptr, read32le(ptr) | x);
else
*(int *)ptr += x;
add32le(ptr, x);
}
return;
case R_ARM_PREL31:
{
int x;
x = (*(int *)ptr) & 0x7fffffff;
(*(int *)ptr) &= 0x80000000;
x = read32le(ptr) & 0x7fffffff;
write32le(ptr, read32le(ptr) & 0x80000000);
x = (x * 2) / 2;
x += val - addr;
if((x^(x>>1))&0x40000000)
tcc_error("can't relocate value at %x,%d",addr, type);
(*(int *)ptr) |= x & 0x7fffffff;
tcc_error_noabort("can't relocate value at %x,%d",addr, type);
write32le(ptr, read32le(ptr) | (x & 0x7fffffff));
}
return;
case R_ARM_ABS32:
*(int *)ptr += val;
case R_ARM_TARGET1:
if (s1->output_type & TCC_OUTPUT_DYN) {
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
qrel->r_offset = rel->r_offset;
if (esym_index) {
qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
qrel++;
return;
} else {
qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
qrel++;
}
}
add32le(ptr, val);
return;
case R_ARM_REL32:
*(int *)ptr += val - addr;
add32le(ptr, val - addr);
return;
case R_ARM_GOTPC:
*(int *)ptr += s1->got->sh_addr - addr;
add32le(ptr, s1->got->sh_addr - addr);
return;
case R_ARM_GOTOFF:
*(int *)ptr += val - s1->got->sh_addr;
add32le(ptr, val - s1->got->sh_addr);
return;
case R_ARM_GOT32:
/* we load the got offset */
*(int *)ptr += s1->sym_attrs[sym_index].got_offset;
add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
return;
case R_ARM_GOT_PREL:
/* we load the pc relative got offset */
add32le(ptr, s1->got->sh_addr +
get_sym_attr(s1, sym_index, 0)->got_offset -
addr);
return;
case R_ARM_COPY:
return;
case R_ARM_V4BX:
/* trade Thumb support for ARMv4 support */
if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10)
*(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */
if ((0x0ffffff0 & read32le(ptr)) == 0x012FFF10)
write32le(ptr, read32le(ptr) ^ 0xE12FFF10 ^ 0xE1A0F000); /* BX Rm -> MOV PC, Rm */
return;
case R_ARM_GLOB_DAT:
case R_ARM_JUMP_SLOT:
*(addr_t *)ptr = val;
write32le(ptr, val);
return;
case R_ARM_NONE:
/* Nothing to do. Normally used to indicate a dependency
@ -388,8 +432,38 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
#endif
/* do nothing */
return;
case R_ARM_TLS_LE32:
{
ElfW(Sym) *sym;
Section *sec;
int32_t x;
addr_t tls_start = 0, tls_end = 0, tls_align = 1;
int i;
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
sec = s1->sections[sym->st_shndx];
for (i = 1; i < s1->nb_sections; i++) {
Section *s = s1->sections[i];
if (s->sh_flags & SHF_TLS && s->sh_size) {
if (!tls_start || s->sh_addr < tls_start)
tls_start = s->sh_addr;
if (s->sh_addr + s->sh_size > tls_end)
tls_end = s->sh_addr + s->sh_size;
if (s->sh_addralign > tls_align)
tls_align = s->sh_addralign;
}
}
if (tls_end > tls_start) {
x = val - tls_start + 8;
} else {
x = val - sec->sh_addr - sec->data_offset + 8;
}
add32le(ptr, x);
}
return;
default:
fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n",
fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
type, (unsigned)addr, ptr, (unsigned)val);
return;
}

406
arm-tok.h Normal file
View File

@ -0,0 +1,406 @@
/* ------------------------------------------------------------------ */
/* WARNING: relative order of tokens is important. */
/* register */
DEF_ASM(r0)
DEF_ASM(r1)
DEF_ASM(r2)
DEF_ASM(r3)
DEF_ASM(r4)
DEF_ASM(r5)
DEF_ASM(r6)
DEF_ASM(r7)
DEF_ASM(r8)
DEF_ASM(r9)
DEF_ASM(r10)
DEF_ASM(r11) /* fp */
DEF_ASM(r12) /* ip[c] */
DEF_ASM(r13) /* sp */
DEF_ASM(r14) /* lr */
DEF_ASM(r15) /* pc */
/* synonym register names */
DEF_ASM(a1) /* argument/result/scratch register 1: alias for r0 */
DEF_ASM(a2) /* argument/result/scratch register 2: alias for r1 */
DEF_ASM(a3) /* argument/result/scratch register 3: alias for r2 */
DEF_ASM(a4) /* argument/result/scratch register 4: alias for r3 */
DEF_ASM(v1) /* variable register 1: alias for r4 */
DEF_ASM(v2) /* variable register 2: alias for r5 */
DEF_ASM(v3) /* variable register 3: alias for r6 */
DEF_ASM(v4) /* variable register 4: alias for r7 */
DEF_ASM(v5) /* ARM state variable register 5: alias for r8 */
DEF_ASM(v6) /* ARM state variable register 6: alias for r9 */
DEF_ASM(v7) /* ARM state variable register 7: alias for r10 */
DEF_ASM(v8) /* ARM state variable register 8: alias for r11 */
/* special register names */
DEF_ASM(sb) /* alias for r9 */
DEF_ASM(sl) /* alias for r10 */
DEF_ASM(fp) /* alias for r11 */
DEF_ASM(ip) /* alias for r12 */
DEF_ASM(sp) /* alias for r13 */
DEF_ASM(lr) /* alias for r14 */
DEF_ASM(pc) /* alias for r15 */
/* coprocessors */
DEF_ASM(p0)
DEF_ASM(p1)
DEF_ASM(p2)
DEF_ASM(p3)
DEF_ASM(p4)
DEF_ASM(p5)
DEF_ASM(p6)
DEF_ASM(p7)
DEF_ASM(p8)
DEF_ASM(p9)
DEF_ASM(p10)
DEF_ASM(p11)
DEF_ASM(p12)
DEF_ASM(p13)
DEF_ASM(p14)
DEF_ASM(p15)
/* coprocessor registers */
DEF_ASM(c0)
DEF_ASM(c1)
DEF_ASM(c2)
DEF_ASM(c3)
DEF_ASM(c4)
DEF_ASM(c5)
DEF_ASM(c6)
DEF_ASM(c7)
DEF_ASM(c8)
DEF_ASM(c9)
DEF_ASM(c10)
DEF_ASM(c11)
DEF_ASM(c12)
DEF_ASM(c13)
DEF_ASM(c14)
DEF_ASM(c15)
/* single-precision VFP registers */
DEF_ASM(s0)
DEF_ASM(s1)
DEF_ASM(s2)
DEF_ASM(s3)
DEF_ASM(s4)
DEF_ASM(s5)
DEF_ASM(s6)
DEF_ASM(s7)
DEF_ASM(s8)
DEF_ASM(s9)
DEF_ASM(s10)
DEF_ASM(s11)
DEF_ASM(s12)
DEF_ASM(s13)
DEF_ASM(s14)
DEF_ASM(s15)
DEF_ASM(s16)
DEF_ASM(s17)
DEF_ASM(s18)
DEF_ASM(s19)
DEF_ASM(s20)
DEF_ASM(s21)
DEF_ASM(s22)
DEF_ASM(s23)
DEF_ASM(s24)
DEF_ASM(s25)
DEF_ASM(s26)
DEF_ASM(s27)
DEF_ASM(s28)
DEF_ASM(s29)
DEF_ASM(s30)
DEF_ASM(s31)
/* double-precision VFP registers */
DEF_ASM(d0)
DEF_ASM(d1)
DEF_ASM(d2)
DEF_ASM(d3)
DEF_ASM(d4)
DEF_ASM(d5)
DEF_ASM(d6)
DEF_ASM(d7)
DEF_ASM(d8)
DEF_ASM(d9)
DEF_ASM(d10)
DEF_ASM(d11)
DEF_ASM(d12)
DEF_ASM(d13)
DEF_ASM(d14)
DEF_ASM(d15)
/* VFP status registers */
DEF_ASM(fpsid)
DEF_ASM(fpscr)
DEF_ASM(fpexc)
/* VFP magical ARM register */
DEF_ASM(apsr_nzcv)
/* data processing directives */
DEF_ASM(asl)
/* instructions that have no condition code */
DEF_ASM(cdp2)
DEF_ASM(ldc2)
DEF_ASM(ldc2l)
DEF_ASM(stc2)
DEF_ASM(stc2l)
#define ARM_INSTRUCTION_GROUP(tok) ((((tok) - TOK_ASM_nopeq) & 0xFFFFFFF0) + TOK_ASM_nopeq)
/* Note: condition code is 4 bits */
#define DEF_ASM_CONDED(x) \
DEF(TOK_ASM_ ## x ## eq, #x "eq") \
DEF(TOK_ASM_ ## x ## ne, #x "ne") \
DEF(TOK_ASM_ ## x ## cs, #x "cs") \
DEF(TOK_ASM_ ## x ## cc, #x "cc") \
DEF(TOK_ASM_ ## x ## mi, #x "mi") \
DEF(TOK_ASM_ ## x ## pl, #x "pl") \
DEF(TOK_ASM_ ## x ## vs, #x "vs") \
DEF(TOK_ASM_ ## x ## vc, #x "vc") \
DEF(TOK_ASM_ ## x ## hi, #x "hi") \
DEF(TOK_ASM_ ## x ## ls, #x "ls") \
DEF(TOK_ASM_ ## x ## ge, #x "ge") \
DEF(TOK_ASM_ ## x ## lt, #x "lt") \
DEF(TOK_ASM_ ## x ## gt, #x "gt") \
DEF(TOK_ASM_ ## x ## le, #x "le") \
DEF(TOK_ASM_ ## x, #x) \
DEF(TOK_ASM_ ## x ## rsvd, #x "rsvd")
/* Note: condition code is 4 bits */
#define DEF_ASM_CONDED_WITH_SUFFIX(x, y) \
DEF(TOK_ASM_ ## x ## eq ## _ ## y, #x "eq." #y) \
DEF(TOK_ASM_ ## x ## ne ## _ ## y, #x "ne." #y) \
DEF(TOK_ASM_ ## x ## cs ## _ ## y, #x "cs." #y) \
DEF(TOK_ASM_ ## x ## cc ## _ ## y, #x "cc." #y) \
DEF(TOK_ASM_ ## x ## mi ## _ ## y, #x "mi." #y) \
DEF(TOK_ASM_ ## x ## pl ## _ ## y, #x "pl." #y) \
DEF(TOK_ASM_ ## x ## vs ## _ ## y, #x "vs." #y) \
DEF(TOK_ASM_ ## x ## vc ## _ ## y, #x "vc." #y) \
DEF(TOK_ASM_ ## x ## hi ## _ ## y, #x "hi." #y) \
DEF(TOK_ASM_ ## x ## ls ## _ ## y, #x "ls." #y) \
DEF(TOK_ASM_ ## x ## ge ## _ ## y, #x "ge." #y) \
DEF(TOK_ASM_ ## x ## lt ## _ ## y, #x "lt." #y) \
DEF(TOK_ASM_ ## x ## gt ## _ ## y, #x "gt." #y) \
DEF(TOK_ASM_ ## x ## le ## _ ## y, #x "le." #y) \
DEF(TOK_ASM_ ## x ## _ ## y, #x "." #y) \
DEF(TOK_ASM_ ## x ## rsvd ## _ ## y, #x "rsvd." #y)
#define DEF_ASM_CONDED_VFP_F32_F64(x) \
DEF_ASM_CONDED_WITH_SUFFIX(x, f32) \
DEF_ASM_CONDED_WITH_SUFFIX(x, f64)
#define DEF_ASM_CONDED_WITH_TWO_SUFFIXES(x, y, z) \
DEF(TOK_ASM_ ## x ## eq ## _ ## y ## _ ## z, #x "eq." #y "." #z) \
DEF(TOK_ASM_ ## x ## ne ## _ ## y ## _ ## z, #x "ne." #y "." #z) \
DEF(TOK_ASM_ ## x ## cs ## _ ## y ## _ ## z, #x "cs." #y "." #z) \
DEF(TOK_ASM_ ## x ## cc ## _ ## y ## _ ## z, #x "cc." #y "." #z) \
DEF(TOK_ASM_ ## x ## mi ## _ ## y ## _ ## z, #x "mi." #y "." #z) \
DEF(TOK_ASM_ ## x ## pl ## _ ## y ## _ ## z, #x "pl." #y "." #z) \
DEF(TOK_ASM_ ## x ## vs ## _ ## y ## _ ## z, #x "vs." #y "." #z) \
DEF(TOK_ASM_ ## x ## vc ## _ ## y ## _ ## z, #x "vc." #y "." #z) \
DEF(TOK_ASM_ ## x ## hi ## _ ## y ## _ ## z, #x "hi." #y "." #z) \
DEF(TOK_ASM_ ## x ## ls ## _ ## y ## _ ## z, #x "ls." #y "." #z) \
DEF(TOK_ASM_ ## x ## ge ## _ ## y ## _ ## z, #x "ge." #y "." #z) \
DEF(TOK_ASM_ ## x ## lt ## _ ## y ## _ ## z, #x "lt." #y "." #z) \
DEF(TOK_ASM_ ## x ## gt ## _ ## y ## _ ## z, #x "gt." #y "." #z) \
DEF(TOK_ASM_ ## x ## le ## _ ## y ## _ ## z, #x "le." #y "." #z) \
DEF(TOK_ASM_ ## x ## _ ## y ## _ ## z, #x "." #y "." #z) \
DEF(TOK_ASM_ ## x ## rsvd ## _ ## y ## _ ## z, #x "rsvd." #y "." #z)
/* Note: add new tokens after nop (MUST always use DEF_ASM_CONDED) */
DEF_ASM_CONDED(nop)
DEF_ASM_CONDED(wfe)
DEF_ASM_CONDED(wfi)
DEF_ASM_CONDED(swi)
DEF_ASM_CONDED(svc)
/* misc */
DEF_ASM_CONDED(clz)
/* size conversion */
DEF_ASM_CONDED(sxtb)
DEF_ASM_CONDED(sxth)
DEF_ASM_CONDED(uxtb)
DEF_ASM_CONDED(uxth)
DEF_ASM_CONDED(movt)
DEF_ASM_CONDED(movw)
/* multiplication */
DEF_ASM_CONDED(mul)
DEF_ASM_CONDED(muls)
DEF_ASM_CONDED(mla)
DEF_ASM_CONDED(mlas)
DEF_ASM_CONDED(smull)
DEF_ASM_CONDED(smulls)
DEF_ASM_CONDED(umull)
DEF_ASM_CONDED(umulls)
DEF_ASM_CONDED(smlal)
DEF_ASM_CONDED(smlals)
DEF_ASM_CONDED(umlal)
DEF_ASM_CONDED(umlals)
DEF_ASM_CONDED(mls)
DEF_ASM_CONDED(udiv)
DEF_ASM_CONDED(sdiv)
/* load/store */
DEF_ASM_CONDED(ldr)
DEF_ASM_CONDED(ldrb)
DEF_ASM_CONDED(str)
DEF_ASM_CONDED(strb)
DEF_ASM_CONDED(ldrex)
DEF_ASM_CONDED(ldrexb)
DEF_ASM_CONDED(ldrexh)
DEF_ASM_CONDED(strex)
DEF_ASM_CONDED(strexb)
DEF_ASM_CONDED(strexh)
DEF_ASM_CONDED(ldrh)
DEF_ASM_CONDED(ldrsh)
DEF_ASM_CONDED(ldrsb)
DEF_ASM_CONDED(strh)
DEF_ASM_CONDED(stmda)
DEF_ASM_CONDED(ldmda)
DEF_ASM_CONDED(stm)
DEF_ASM_CONDED(ldm)
DEF_ASM_CONDED(stmia)
DEF_ASM_CONDED(ldmia)
DEF_ASM_CONDED(stmdb)
DEF_ASM_CONDED(ldmdb)
DEF_ASM_CONDED(stmib)
DEF_ASM_CONDED(ldmib)
DEF_ASM_CONDED(ldc)
DEF_ASM_CONDED(ldcl)
DEF_ASM_CONDED(stc)
DEF_ASM_CONDED(stcl)
/* instruction macros */
DEF_ASM_CONDED(push)
DEF_ASM_CONDED(pop)
/* branches */
DEF_ASM_CONDED(b)
DEF_ASM_CONDED(bl)
DEF_ASM_CONDED(bx)
DEF_ASM_CONDED(blx)
/* data processing instructions; order is important */
DEF_ASM_CONDED(and)
DEF_ASM_CONDED(ands)
DEF_ASM_CONDED(eor)
DEF_ASM_CONDED(eors)
DEF_ASM_CONDED(sub)
DEF_ASM_CONDED(subs)
DEF_ASM_CONDED(rsb)
DEF_ASM_CONDED(rsbs)
DEF_ASM_CONDED(add)
DEF_ASM_CONDED(adds)
DEF_ASM_CONDED(adc)
DEF_ASM_CONDED(adcs)
DEF_ASM_CONDED(sbc)
DEF_ASM_CONDED(sbcs)
DEF_ASM_CONDED(rsc)
DEF_ASM_CONDED(rscs)
DEF_ASM_CONDED(tst)
DEF_ASM_CONDED(tsts) // necessary here--but not useful to the user
DEF_ASM_CONDED(teq)
DEF_ASM_CONDED(teqs) // necessary here--but not useful to the user
DEF_ASM_CONDED(cmp)
DEF_ASM_CONDED(cmps) // necessary here--but not useful to the user
DEF_ASM_CONDED(cmn)
DEF_ASM_CONDED(cmns) // necessary here--but not useful to the user
DEF_ASM_CONDED(orr)
DEF_ASM_CONDED(orrs)
DEF_ASM_CONDED(mov)
DEF_ASM_CONDED(movs)
DEF_ASM_CONDED(bic)
DEF_ASM_CONDED(bics)
DEF_ASM_CONDED(mvn)
DEF_ASM_CONDED(mvns)
DEF_ASM_CONDED(lsl)
DEF_ASM_CONDED(lsls)
DEF_ASM_CONDED(lsr)
DEF_ASM_CONDED(lsrs)
DEF_ASM_CONDED(asr)
DEF_ASM_CONDED(asrs)
DEF_ASM_CONDED(ror)
DEF_ASM_CONDED(rors)
DEF_ASM_CONDED(rrx)
DEF_ASM_CONDED(rrxs)
DEF_ASM_CONDED(cdp)
DEF_ASM_CONDED(mcr)
DEF_ASM_CONDED(mrc)
// Floating point high-level instructions
DEF_ASM_CONDED(vldr)
DEF_ASM_CONDED(vstr)
DEF_ASM_CONDED_VFP_F32_F64(vmla)
DEF_ASM_CONDED_VFP_F32_F64(vmls)
DEF_ASM_CONDED_VFP_F32_F64(vnmls)
DEF_ASM_CONDED_VFP_F32_F64(vnmla)
DEF_ASM_CONDED_VFP_F32_F64(vmul)
DEF_ASM_CONDED_VFP_F32_F64(vnmul)
DEF_ASM_CONDED_VFP_F32_F64(vadd)
DEF_ASM_CONDED_VFP_F32_F64(vsub)
DEF_ASM_CONDED_VFP_F32_F64(vdiv)
DEF_ASM_CONDED_VFP_F32_F64(vneg)
DEF_ASM_CONDED_VFP_F32_F64(vabs)
DEF_ASM_CONDED_VFP_F32_F64(vsqrt)
DEF_ASM_CONDED_VFP_F32_F64(vcmp)
DEF_ASM_CONDED_VFP_F32_F64(vcmpe)
DEF_ASM_CONDED_VFP_F32_F64(vmov)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, s32, f64)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, s32, f32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, u32, f64)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, u32, f32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, s32, f64)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, s32, f32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, u32, f64)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, u32, f32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f64, s32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f32, s32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f64, u32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f32, u32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f64, f32)
DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f32, f64)
DEF_ASM_CONDED(vpush)
DEF_ASM_CONDED(vpop)
DEF_ASM_CONDED(vldm)
DEF_ASM_CONDED(vldmia)
DEF_ASM_CONDED(vldmdb)
DEF_ASM_CONDED(vstm)
DEF_ASM_CONDED(vstmia)
DEF_ASM_CONDED(vstmdb)
DEF_ASM_CONDED(vmsr)
DEF_ASM_CONDED(vmrs)

2276
arm64-asm.c Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,7 @@
#define R_NUM R_AARCH64_NUM
#define ELF_START_ADDR 0x00400000
#define ELF_PAGE_SIZE 0x1000
#define ELF_PAGE_SIZE 0x10000
#define PCRELATIVE_DLLPLT 1
#define RELOCATE_DLLPLT 1
@ -21,9 +21,10 @@
#include "tcc.h"
#ifdef NEED_RELOC_TYPE
/* Returns 1 for a code relocation, 0 for a data relocation. For unknown
relocations, returns -1. */
int code_reloc (int reloc_type)
ST_FUNC int code_reloc (int reloc_type)
{
switch (reloc_type) {
case R_AARCH64_ABS32:
@ -37,6 +38,13 @@ int code_reloc (int reloc_type)
case R_AARCH64_ADD_ABS_LO12_NC:
case R_AARCH64_ADR_GOT_PAGE:
case R_AARCH64_LD64_GOT_LO12_NC:
case R_AARCH64_LDST128_ABS_LO12_NC:
case R_AARCH64_LDST64_ABS_LO12_NC:
case R_AARCH64_LDST32_ABS_LO12_NC:
case R_AARCH64_LDST16_ABS_LO12_NC:
case R_AARCH64_LDST8_ABS_LO12_NC:
case R_AARCH64_TLSLE_ADD_TPREL_HI12:
case R_AARCH64_TLSLE_ADD_TPREL_LO12:
case R_AARCH64_GLOB_DAT:
case R_AARCH64_COPY:
return 0;
@ -44,17 +52,17 @@ int code_reloc (int reloc_type)
case R_AARCH64_JUMP26:
case R_AARCH64_CALL26:
case R_AARCH64_JUMP_SLOT:
case R_AARCH64_CONDBR19:
case R_AARCH64_TSTBR14:
return 1;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
/* Returns an enumerator to describe whether and when the relocation needs a
GOT and/or PLT entry to be created. See tcc.h for a description of the
different values. */
int gotplt_entry_type (int reloc_type)
ST_FUNC int gotplt_entry_type (int reloc_type)
{
switch (reloc_type) {
case R_AARCH64_PREL32:
@ -64,9 +72,18 @@ int gotplt_entry_type (int reloc_type)
case R_AARCH64_MOVW_UABS_G3:
case R_AARCH64_ADR_PREL_PG_HI21:
case R_AARCH64_ADD_ABS_LO12_NC:
case R_AARCH64_LDST128_ABS_LO12_NC:
case R_AARCH64_LDST64_ABS_LO12_NC:
case R_AARCH64_LDST32_ABS_LO12_NC:
case R_AARCH64_LDST16_ABS_LO12_NC:
case R_AARCH64_LDST8_ABS_LO12_NC:
case R_AARCH64_GLOB_DAT:
case R_AARCH64_JUMP_SLOT:
case R_AARCH64_COPY:
case R_AARCH64_CONDBR19:
case R_AARCH64_TSTBR14:
case R_AARCH64_TLSLE_ADD_TPREL_HI12:
case R_AARCH64_TLSLE_ADD_TPREL_LO12:
return NO_GOTPLT_ENTRY;
case R_AARCH64_ABS32:
@ -79,20 +96,16 @@ int gotplt_entry_type (int reloc_type)
case R_AARCH64_LD64_GOT_LO12_NC:
return ALWAYS_GOTPLT_ENTRY;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
#ifdef NEED_BUILD_GOT
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
{
Section *plt = s1->plt;
uint8_t *p;
unsigned plt_offset;
if (s1->output_type == TCC_OUTPUT_DLL)
tcc_error("DLLs unimplemented!");
if (plt->data_offset == 0) {
section_ptr_add(plt, 32);
}
@ -118,58 +131,103 @@ ST_FUNC void relocate_plt(TCCState *s1)
if (p < p_end) {
uint64_t plt = s1->plt->sh_addr;
uint64_t got = s1->got->sh_addr;
uint64_t got = s1->got->sh_addr + 16;
uint64_t off = (got >> 12) - (plt >> 12);
if ((off + ((uint32_t)1 << 20)) >> 21)
tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off, got, plt);
write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]!
write32le(p + 4, (0x90000010 | // adrp x16,...
tcc_error_noabort("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off, (long)got, (long)plt);
write32le(p, ARM64_STP_X_PRE | ARM64_RT(16) | ARM64_RT2(30) |
ARM64_RN(31) | ARM64_IMM7(-2)); // stp x16,x30,[sp,#-16]!
write32le(p + 4, (ARM64_ADRP | ARM64_RD(16) | // adrp x16,...
(off & 0x1ffffc) << 3 | (off & 3) << 29));
write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...]
write32le(p + 8, (ARM64_LDR_X | ARM64_RT(17) | ARM64_RN(16) | // ldr x17,[x16,#...]
(got & 0xff8) << 7));
write32le(p + 12, (0x91000210 | // add x16,x16,#...
write32le(p + 12, (ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RD(16) | ARM64_RN(16) | // add x16,x16,#...
(got & 0xfff) << 10));
write32le(p + 16, 0xd61f0220); // br x17
write32le(p + 20, 0xd503201f); // nop
write32le(p + 24, 0xd503201f); // nop
write32le(p + 28, 0xd503201f); // nop
write32le(p + 16, ARM64_BR | ARM64_RN(17)); // br x17
write32le(p + 20, ARM64_NOP); // nop
write32le(p + 24, ARM64_NOP); // nop
write32le(p + 28, ARM64_NOP); // nop
p += 32;
got = s1->got->sh_addr;
while (p < p_end) {
uint64_t pc = plt + (p - s1->plt->data);
uint64_t addr = got + read64le(p);
uint64_t off = (addr >> 12) - (pc >> 12);
if ((off + ((uint32_t)1 << 20)) >> 21)
tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, pc);
write32le(p, (0x90000010 | // adrp x16,...
tcc_error_noabort("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", (long)off, (long)addr, (long)pc);
write32le(p, (ARM64_ADRP | ARM64_RD(16) | // adrp x16,...
(off & 0x1ffffc) << 3 | (off & 3) << 29));
write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...]
write32le(p + 4, (ARM64_LDR_X | ARM64_RT(17) | ARM64_RN(16) | // ldr x17,[x16,#...]
(addr & 0xff8) << 7));
write32le(p + 8, (0x91000210 | // add x16,x16,#...
write32le(p + 8, (ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RD(16) | ARM64_RN(16) | // add x16,x16,#...
(addr & 0xfff) << 10));
write32le(p + 12, 0xd61f0220); // br x17
write32le(p + 12, ARM64_BR | ARM64_RN(17)); // br x17
p += 16;
}
}
if (s1->plt->reloc) {
ElfW_Rel *rel;
p = s1->got->data;
for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
write64le(p + rel->r_offset, s1->plt->sh_addr);
}
}
}
#endif
#endif
void relocate_init(Section *sr) {}
void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
{
int sym_index = ELFW(R_SYM)(rel->r_info);
int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
#ifdef DEBUG_RELOC
ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
#endif
switch(type) {
case R_AARCH64_ABS64:
write64le(ptr, val);
if ((s1->output_type & TCC_OUTPUT_DYN)) {
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
qrel->r_offset = rel->r_offset;
if (esym_index) {
qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_ABS64);
qrel->r_addend = rel->r_addend;
qrel++;
break;
} else {
qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
qrel->r_addend = read64le(ptr) + val;
qrel++;
}
}
add64le(ptr, val);
return;
case R_AARCH64_ABS32:
write32le(ptr, val);
if (s1->output_type & TCC_OUTPUT_DYN) {
/* XXX: this logic may depend on TCC's codegen
now TCC uses R_AARCH64_RELATIVE even for a 64bit pointer */
qrel->r_offset = rel->r_offset;
qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
/* Use sign extension! */
qrel->r_addend = (int)read32le(ptr) + val;
qrel++;
}
add32le(ptr, val);
return;
case R_AARCH64_PREL32:
write32le(ptr, val - addr);
if (s1->output_type == TCC_OUTPUT_DLL) {
/* DLL relocation */
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
if (esym_index) {
qrel->r_offset = rel->r_offset;
qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_PREL32);
/* Use sign extension! */
qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
qrel++;
break;
}
}
add32le(ptr, val - addr);
return;
case R_AARCH64_MOVW_UABS_G0_NC:
write32le(ptr, ((read32le(ptr) & 0xffe0001f) |
@ -189,35 +247,106 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
return;
case R_AARCH64_ADR_PREL_PG_HI21: {
uint64_t off = (val >> 12) - (addr >> 12);
#ifdef TCC_TARGET_PE
/* Weak undefined symbols resolve to address 0 on PE. ADRP cannot
encode that from the default 64-bit image base, so materialize
zero directly and let the paired ADD handle any low addend. */
if ((off + ((uint64_t)1 << 20)) >> 21) {
ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
if (sym->st_shndx == SHN_UNDEF
&& ELFW(ST_BIND)(sym->st_info) == STB_WEAK) {
write32le(ptr, 0xd2800000 | (read32le(ptr) & 0x1f));
return;
}
tcc_error_noabort("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
}
#else
if ((off + ((uint64_t)1 << 20)) >> 21)
tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
tcc_error_noabort("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
#endif
write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
(off & 0x1ffffc) << 3 | (off & 3) << 29));
return;
}
case R_AARCH64_ADD_ABS_LO12_NC:
case R_AARCH64_LDST8_ABS_LO12_NC:
write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
(val & 0xfff) << 10));
return;
case R_AARCH64_JUMP26:
case R_AARCH64_CALL26:
case R_AARCH64_LDST16_ABS_LO12_NC:
write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
(val & 0xffe) << 9));
return;
case R_AARCH64_LDST32_ABS_LO12_NC:
write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
(val & 0xffc) << 8));
return;
case R_AARCH64_LDST64_ABS_LO12_NC:
write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
(val & 0xff8) << 7));
return;
case R_AARCH64_LDST128_ABS_LO12_NC:
write32le(ptr, ((read32le(ptr) & 0xffc003ff) |
(val & 0xff0) << 6));
return;
case R_AARCH64_CONDBR19:
/* Conditional branch: 19-bit signed offset, bits 23:5 */
#ifdef DEBUG_RELOC
printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
(char *) symtab_section->link->data + sym->st_name);
#endif
if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc)
tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed"
" (val=%lx, addr=%lx)", val, addr);
if (((val - addr) + ((uint64_t)1 << 20)) & ~(uint64_t)0x1ffffc)
tcc_error_noabort("R_AARCH64_CONDBR19 relocation failed"
" (val=%lx, addr=%lx)", (long)val, (long)addr);
write32le(ptr, ((read32le(ptr) & 0xff00001f) |
(((val - addr) >> 2 & 0x7ffff) << 5)));
return;
case R_AARCH64_TSTBR14:
/* Test and branch: 14-bit signed offset, bits 20:5 */
#ifdef DEBUG_RELOC
printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
(char *) symtab_section->link->data + sym->st_name);
#endif
if (((val - addr) + ((uint64_t)1 << 15)) & ~(uint64_t)0xfffc)
tcc_error_noabort("R_AARCH64_TSTBR14 relocation failed"
" (val=%lx, addr=%lx)", (long)val, (long)addr);
write32le(ptr, ((read32le(ptr) & 0xfff8001f) |
(((val - addr) >> 2 & 0x3fff) << 5)));
return;
case R_AARCH64_JUMP26:
case R_AARCH64_CALL26:
{
const char *name;
#ifdef DEBUG_RELOC
printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val,
(char *) symtab_section->link->data + sym->st_name);
#endif
if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc) {
#ifdef TCC_TARGET_PE
ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
if (sym->st_shndx == SHN_UNDEF
&& ELFW(ST_BIND)(sym->st_info) == STB_WEAK) {
write32le(ptr, ARM64_NOP); /* nop */
return;
}
#endif
name = (char *)symtab_section->link->data +
((ElfW(Sym) *)symtab_section->data)[sym_index].st_name;
tcc_error_noabort("R_AARCH64_(JUMP|CALL)26 relocation failed"
" for '%s' (val=%lx, addr=%lx)",
name, (long)val, (long)addr);
}
write32le(ptr, (0x14000000 |
(uint32_t)(type == R_AARCH64_CALL26) << 31 |
((val - addr) >> 2 & 0x3ffffff)));
return;
}
case R_AARCH64_ADR_GOT_PAGE: {
uint64_t off =
(((s1->got->sh_addr +
s1->sym_attrs[sym_index].got_offset) >> 12) - (addr >> 12));
get_sym_attr(s1, sym_index, 0)->got_offset) >> 12) - (addr >> 12));
if ((off + ((uint64_t)1 << 20)) >> 21)
tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed");
tcc_error_noabort("R_AARCH64_ADR_GOT_PAGE relocation failed");
write32le(ptr, ((read32le(ptr) & 0x9f00001f) |
(off & 0x1ffffc) << 3 | (off & 3) << 29));
return;
@ -226,7 +355,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
write32le(ptr,
((read32le(ptr) & 0xfff803ff) |
((s1->got->sh_addr +
s1->sym_attrs[sym_index].got_offset) & 0xff8) << 7));
get_sym_attr(s1, sym_index, 0)->got_offset) & 0xff8) << 7));
return;
case R_AARCH64_COPY:
return;
@ -240,6 +369,27 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
#endif
write64le(ptr, val - rel->r_addend);
return;
case R_AARCH64_TLSLE_ADD_TPREL_HI12:
case R_AARCH64_TLSLE_ADD_TPREL_LO12: {
addr_t tls_start = 0;
int i;
for (i = 1; i < s1->nb_sections; i++) {
Section *s = s1->sections[i];
if (s->sh_flags & SHF_TLS && s->sh_size) {
if (!tls_start || s->sh_addr < tls_start)
tls_start = s->sh_addr;
}
}
/* glibc arm64: tp points to tcbhead_t (DTV), TLS data starts after it */
int64_t tp_offset = val - tls_start + 16;
int64_t imm;
if (type == R_AARCH64_TLSLE_ADD_TPREL_HI12)
imm = (tp_offset >> 12) & 0xfff;
else
imm = tp_offset & 0xfff;
write32le(ptr, ((read32le(ptr) & 0xffc003ff) | (imm << 10)));
return;
}
case R_AARCH64_RELATIVE:
#ifdef TCC_TARGET_PE
add32le(ptr, val - s1->pe_imagebase);

840
arm64-tok.h Normal file
View File

@ -0,0 +1,840 @@
/* ------------------------------------------------------------------ */
/* ARM64 (AArch64) assembler token definitions for TCC */
/* General purpose registers - 64-bit */
DEF_ASM(x0)
DEF_ASM(x1)
DEF_ASM(x2)
DEF_ASM(x3)
DEF_ASM(x4)
DEF_ASM(x5)
DEF_ASM(x6)
DEF_ASM(x7)
DEF_ASM(x8)
DEF_ASM(x9)
DEF_ASM(x10)
DEF_ASM(x11)
DEF_ASM(x12)
DEF_ASM(x13)
DEF_ASM(x14)
DEF_ASM(x15)
DEF_ASM(x16)
DEF_ASM(x17)
DEF_ASM(x18)
DEF_ASM(x19)
DEF_ASM(x20)
DEF_ASM(x21)
DEF_ASM(x22)
DEF_ASM(x23)
DEF_ASM(x24)
DEF_ASM(x25)
DEF_ASM(x26)
DEF_ASM(x27)
DEF_ASM(x28)
DEF_ASM(x29)
DEF_ASM(x30)
/* General purpose registers - 32-bit */
DEF_ASM(w0)
DEF_ASM(w1)
DEF_ASM(w2)
DEF_ASM(w3)
DEF_ASM(w4)
DEF_ASM(w5)
DEF_ASM(w6)
DEF_ASM(w7)
DEF_ASM(w8)
DEF_ASM(w9)
DEF_ASM(w10)
DEF_ASM(w11)
DEF_ASM(w12)
DEF_ASM(w13)
DEF_ASM(w14)
DEF_ASM(w15)
DEF_ASM(w16)
DEF_ASM(w17)
DEF_ASM(w18)
DEF_ASM(w19)
DEF_ASM(w20)
DEF_ASM(w21)
DEF_ASM(w22)
DEF_ASM(w23)
DEF_ASM(w24)
DEF_ASM(w25)
DEF_ASM(w26)
DEF_ASM(w27)
DEF_ASM(w28)
DEF_ASM(w29)
DEF_ASM(w30)
/* Special registers */
DEF_ASM(sp)
DEF_ASM(xzr)
DEF_ASM(wzr)
/* SIMD/FP registers - 128-bit views */
DEF_ASM(v0)
DEF_ASM(v1)
DEF_ASM(v2)
DEF_ASM(v3)
DEF_ASM(v4)
DEF_ASM(v5)
DEF_ASM(v6)
DEF_ASM(v7)
DEF_ASM(v8)
DEF_ASM(v9)
DEF_ASM(v10)
DEF_ASM(v11)
DEF_ASM(v12)
DEF_ASM(v13)
DEF_ASM(v14)
DEF_ASM(v15)
DEF_ASM(v16)
DEF_ASM(v17)
DEF_ASM(v18)
DEF_ASM(v19)
DEF_ASM(v20)
DEF_ASM(v21)
DEF_ASM(v22)
DEF_ASM(v23)
DEF_ASM(v24)
DEF_ASM(v25)
DEF_ASM(v26)
DEF_ASM(v27)
DEF_ASM(v28)
DEF_ASM(v29)
DEF_ASM(v30)
DEF_ASM(v31)
/* SIMD/FP registers - 64-bit views (double) */
DEF_ASM(d0)
DEF_ASM(d1)
DEF_ASM(d2)
DEF_ASM(d3)
DEF_ASM(d4)
DEF_ASM(d5)
DEF_ASM(d6)
DEF_ASM(d7)
DEF_ASM(d8)
DEF_ASM(d9)
DEF_ASM(d10)
DEF_ASM(d11)
DEF_ASM(d12)
DEF_ASM(d13)
DEF_ASM(d14)
DEF_ASM(d15)
DEF_ASM(d16)
DEF_ASM(d17)
DEF_ASM(d18)
DEF_ASM(d19)
DEF_ASM(d20)
DEF_ASM(d21)
DEF_ASM(d22)
DEF_ASM(d23)
DEF_ASM(d24)
DEF_ASM(d25)
DEF_ASM(d26)
DEF_ASM(d27)
DEF_ASM(d28)
DEF_ASM(d29)
DEF_ASM(d30)
DEF_ASM(d31)
/* SIMD/FP registers - 32-bit views (single) */
DEF_ASM(s0)
DEF_ASM(s1)
DEF_ASM(s2)
DEF_ASM(s3)
DEF_ASM(s4)
DEF_ASM(s5)
DEF_ASM(s6)
DEF_ASM(s7)
DEF_ASM(s8)
DEF_ASM(s9)
DEF_ASM(s10)
DEF_ASM(s11)
DEF_ASM(s12)
DEF_ASM(s13)
DEF_ASM(s14)
DEF_ASM(s15)
DEF_ASM(s16)
DEF_ASM(s17)
DEF_ASM(s18)
DEF_ASM(s19)
DEF_ASM(s20)
DEF_ASM(s21)
DEF_ASM(s22)
DEF_ASM(s23)
DEF_ASM(s24)
DEF_ASM(s25)
DEF_ASM(s26)
DEF_ASM(s27)
DEF_ASM(s28)
DEF_ASM(s29)
DEF_ASM(s30)
DEF_ASM(s31)
/* SIMD/FP registers - 16-bit views (half) */
DEF_ASM(h0)
DEF_ASM(h1)
DEF_ASM(h2)
DEF_ASM(h3)
DEF_ASM(h4)
DEF_ASM(h5)
DEF_ASM(h6)
DEF_ASM(h7)
DEF_ASM(h8)
DEF_ASM(h9)
DEF_ASM(h10)
DEF_ASM(h11)
DEF_ASM(h12)
DEF_ASM(h13)
DEF_ASM(h14)
DEF_ASM(h15)
DEF_ASM(h16)
DEF_ASM(h17)
DEF_ASM(h18)
DEF_ASM(h19)
DEF_ASM(h20)
DEF_ASM(h21)
DEF_ASM(h22)
DEF_ASM(h23)
DEF_ASM(h24)
DEF_ASM(h25)
DEF_ASM(h26)
DEF_ASM(h27)
DEF_ASM(h28)
DEF_ASM(h29)
DEF_ASM(h30)
DEF_ASM(h31)
/* SIMD/FP registers - 8-bit views (byte) */
DEF_ASM(b0)
DEF_ASM(b1)
DEF_ASM(b2)
DEF_ASM(b3)
DEF_ASM(b4)
DEF_ASM(b5)
DEF_ASM(b6)
DEF_ASM(b7)
DEF_ASM(b8)
DEF_ASM(b9)
DEF_ASM(b10)
DEF_ASM(b11)
DEF_ASM(b12)
DEF_ASM(b13)
DEF_ASM(b14)
DEF_ASM(b15)
DEF_ASM(b16)
DEF_ASM(b17)
DEF_ASM(b18)
DEF_ASM(b19)
DEF_ASM(b20)
DEF_ASM(b21)
DEF_ASM(b22)
DEF_ASM(b23)
DEF_ASM(b24)
DEF_ASM(b25)
DEF_ASM(b26)
DEF_ASM(b27)
DEF_ASM(b28)
DEF_ASM(b29)
DEF_ASM(b30)
DEF_ASM(b31)
/* Condition codes */
DEF_ASM(eq)
DEF_ASM(ne)
DEF_ASM(cs)
DEF_ASM(hs)
DEF_ASM(cc)
DEF_ASM(lo)
DEF_ASM(mi)
DEF_ASM(pl)
DEF_ASM(vs)
DEF_ASM(vc)
DEF_ASM(hi)
DEF_ASM(ls)
DEF_ASM(ge)
DEF_ASM(lt)
DEF_ASM(gt)
DEF_ASM(le)
DEF_ASM(al)
/* Data processing - arithmetic (no condition suffixes for ARM64) */
DEF_ASM(add)
DEF_ASM(adds)
DEF_ASM(sub)
DEF_ASM(subs)
DEF_ASM(cmn)
DEF_ASM(cmp)
DEF_ASM(neg)
DEF_ASM(negs)
DEF_ASM(adc)
DEF_ASM(adcs)
DEF_ASM(sbc)
DEF_ASM(sbcs)
DEF_ASM(ngc)
DEF_ASM(ngcs)
/* Data processing - bitwise */
DEF_ASM(and)
DEF_ASM(ands)
DEF_ASM(bic)
DEF_ASM(bics)
DEF_ASM(orr)
DEF_ASM(orn)
DEF_ASM(eor)
DEF_ASM(eon)
DEF_ASM(mvn)
DEF_ASM(mov)
/* Shifts */
DEF_ASM(lsl)
DEF_ASM(lsr)
DEF_ASM(asr)
DEF_ASM(ror)
/* Multiply/divide */
DEF_ASM(mul)
DEF_ASM(madd)
DEF_ASM(msub)
DEF_ASM(smaddl)
DEF_ASM(smsubl)
DEF_ASM(umaddl)
DEF_ASM(umsubl)
DEF_ASM(smulh)
DEF_ASM(umulh)
DEF_ASM(udiv)
DEF_ASM(sdiv)
/* Moves */
DEF_ASM(movz)
DEF_ASM(movn)
DEF_ASM(movk)
/* Compare/test */
DEF_ASM(tst)
DEF_ASM(teq)
/* Branch instructions */
DEF_ASM(b)
DEF_ASM(bl)
DEF_ASM(br)
DEF_ASM(blr)
DEF_ASM(ret)
DEF_ASM(cbz)
DEF_ASM(cbnz)
DEF_ASM(tbz)
DEF_ASM(tbnz)
/* Conditional branches */
DEF_ASM(beq)
DEF_ASM(bne)
DEF_ASM(bcs)
DEF_ASM(bhs)
DEF_ASM(bcc)
DEF_ASM(blo)
DEF_ASM(bmi)
DEF_ASM(bpl)
DEF_ASM(bvs)
DEF_ASM(bvc)
DEF_ASM(bhi)
DEF_ASM(bls)
DEF_ASM(bge)
DEF_ASM(blt)
DEF_ASM(bgt)
DEF_ASM(ble)
/* Conditional select */
DEF_ASM(csel)
DEF_ASM(csinc)
DEF_ASM(csinv)
DEF_ASM(csneg)
/* Load/Store */
DEF_ASM(ldr)
DEF_ASM(ldrb)
DEF_ASM(ldrh)
DEF_ASM(ldrsb)
DEF_ASM(ldrsh)
DEF_ASM(ldrsw)
DEF_ASM(str)
DEF_ASM(strb)
DEF_ASM(strh)
/* Load/Store - pair */
DEF_ASM(ldp)
DEF_ASM(stp)
DEF_ASM(ldpsw)
/* Address generation */
DEF_ASM(adr)
DEF_ASM(adrp)
/* System instructions */
DEF_ASM(mrs)
DEF_ASM(msr)
DEF_ASM(nop)
DEF_ASM(wfi)
DEF_ASM(wfe)
DEF_ASM(sev)
DEF_ASM(sevl)
DEF_ASM(isb)
DEF_ASM(dsb)
DEF_ASM(dmb)
/* Hints */
DEF_ASM(yield)
DEF_ASM(clrex)
/* Push/pop */
DEF_ASM(push)
DEF_ASM(pop)
/* Floating point */
DEF_ASM(fmov)
DEF_ASM(fadd)
DEF_ASM(fsub)
DEF_ASM(fmul)
DEF_ASM(fnmul)
DEF_ASM(fdiv)
DEF_ASM(fmax)
DEF_ASM(fmin)
DEF_ASM(fmaxnm)
DEF_ASM(fminnm)
DEF_ASM(fsqrt)
DEF_ASM(fabs)
DEF_ASM(fneg)
DEF_ASM(frintn)
DEF_ASM(frintp)
DEF_ASM(frintm)
DEF_ASM(frintz)
DEF_ASM(frinta)
DEF_ASM(frintx)
DEF_ASM(frinti)
DEF_ASM(fcmp)
DEF_ASM(fcmpe)
DEF_ASM(fccmp)
DEF_ASM(fccmpe)
DEF_ASM(fcvts)
DEF_ASM(fcvtd)
DEF_ASM(fcvth)
DEF_ASM(fcvtx)
DEF_ASM(scvtf)
DEF_ASM(ucvtf)
DEF_ASM(fcvtns)
DEF_ASM(fcvtnu)
DEF_ASM(fcvtps)
DEF_ASM(fcvtpu)
/* SIMD instructions */
DEF_ASM(addv)
DEF_ASM(faddp)
DEF_ASM(fmaxp)
DEF_ASM(fminp)
DEF_ASM(fmaxnmp)
DEF_ASM(fminnmp)
DEF_ASM(addp)
DEF_ASM(bif)
DEF_ASM(bit)
DEF_ASM(bsl)
DEF_ASM(dup)
DEF_ASM(ext)
DEF_ASM(ins)
DEF_ASM(movi)
DEF_ASM(mvni)
DEF_ASM(not)
DEF_ASM(shl)
DEF_ASM(shll)
DEF_ASM(shll2)
DEF_ASM(sli)
DEF_ASM(sri)
DEF_ASM(sqshl)
DEF_ASM(sqshlu)
DEF_ASM(srshl)
DEF_ASM(sshll)
DEF_ASM(sshll2)
DEF_ASM(sshr)
DEF_ASM(ushll)
DEF_ASM(ushll2)
DEF_ASM(ushr)
/* Misc */
DEF_ASM(bfm)
DEF_ASM(sbfm)
DEF_ASM(ubfm)
DEF_ASM(extr)
DEF_ASM(crc32b)
DEF_ASM(crc32h)
DEF_ASM(crc32w)
DEF_ASM(crc32x)
DEF_ASM(crc32cb)
DEF_ASM(crc32ch)
DEF_ASM(crc32cw)
DEF_ASM(crc32cx)
DEF_ASM(rev)
DEF_ASM(rev16)
DEF_ASM(rev32)
DEF_ASM(rev64)
DEF_ASM(clz)
DEF_ASM(cls)
DEF_ASM(rbit)
/* Exception generating */
DEF_ASM(svc)
DEF_ASM(hvc)
DEF_ASM(smc)
DEF_ASM(brk)
DEF_ASM(hlt)
DEF_ASM(dcps1)
DEF_ASM(dcps2)
DEF_ASM(dcps3)
/* Conditional branches */
DEF_ASM(b_eq)
DEF_ASM(b_ne)
DEF_ASM(b_cs)
DEF_ASM(b_cc)
DEF_ASM(b_mi)
DEF_ASM(b_pl)
DEF_ASM(b_vs)
DEF_ASM(b_vc)
DEF_ASM(b_hi)
DEF_ASM(b_ls)
DEF_ASM(b_ge)
DEF_ASM(b_lt)
DEF_ASM(b_gt)
DEF_ASM(b_le)
/* LD/ST exclusive */
DEF_ASM(ldxr)
DEF_ASM(ldxrb)
DEF_ASM(ldxrh)
DEF_ASM(stxr)
DEF_ASM(stxrb)
DEF_ASM(stxrh)
DEF_ASM(ldaxr)
DEF_ASM(ldaxrb)
DEF_ASM(ldaxrh)
DEF_ASM(stlxr)
DEF_ASM(stlxrb)
DEF_ASM(stlxrh)
/* LD/ST acquire-release */
DEF_ASM(ldar)
DEF_ASM(ldarb)
DEF_ASM(ldarh)
DEF_ASM(stlr)
DEF_ASM(stlrb)
DEF_ASM(stlrh)
DEF_ASM(ldalr)
DEF_ASM(ldalrb)
DEF_ASM(ldalrh)
DEF_ASM(stllr)
DEF_ASM(stllrb)
DEF_ASM(stllrh)
/* LD/ST unscaled immediate */
DEF_ASM(ldur)
DEF_ASM(ldurb)
DEF_ASM(ldurh)
DEF_ASM(ldursb)
DEF_ASM(ldursh)
DEF_ASM(ldursw)
DEF_ASM(stur)
DEF_ASM(sturb)
DEF_ASM(sturh)
/* Vector load/store */
DEF_ASM(ld1)
DEF_ASM(st1)
DEF_ASM(ld2)
DEF_ASM(st2)
DEF_ASM(ld3)
DEF_ASM(st3)
DEF_ASM(ld4)
DEF_ASM(st4)
/* ------------------------------------------------------------------ */
/* ARM64 instruction opcode constants and encoding helpers */
/* ------------------------------------------------------------------ */
/* Data processing - immediate */
#define ARM64_ADD_IMM 0x11000000U
#define ARM64_ADDS_IMM 0x2B000000U
#define ARM64_SUB_IMM 0x51000000U
#define ARM64_SUBS_IMM 0x6B000000U
/* Data processing - register */
#define ARM64_ADD_REG 0x0B000000U
#define ARM64_ADDS_REG 0x2B000000U
#define ARM64_SUB_REG 0x4B000000U
#define ARM64_SUBS_REG 0x6B000000U
#define ARM64_AND_REG 0x0A000000U
#define ARM64_ANDS_REG 0x6A000000U
#define ARM64_ORR_REG 0x2A000000U
#define ARM64_EOR_REG 0x4A000000U
#define ARM64_MUL_REG 0x1B000000U /* Base opcode, Rm/Rn/Rd must be filled in */
/* Move wide immediate */
#define ARM64_MOVZ 0x52800000U
#define ARM64_MOVN 0x12800000U
#define ARM64_MOVK 0xF2800000U
/* ARM64_MOVI_W/X removed: MOVI is a SIMD&FP instruction, not general-purpose */
/* Use MOVZ/MOVN/MOVK for general-purpose, or SIMD MOVI variants (0x0F000400, etc.) */
/* MOVZ/MOVN 64-bit base opcodes */
#define ARM64_MOVZ64 0xD2800000U /* MOVZ (64-bit), use with ARM64_HW() */
#define ARM64_MOVN64 0x92800000U /* MOVN (64-bit), use with ARM64_HW() */
/* Move wide immediate shift field (LSL #0/16/32/48 encoded as hw*16) */
#define ARM64_HW(v) (((uint32_t)(v) & 3) << 21)
/* Load/store register (unsigned immediate) */
#define ARM64_LDR_X 0xF9400000U
#define ARM64_LDR_W 0xB9400000U
#define ARM64_LDR_B 0x39400000U
#define ARM64_LDR_H 0x79400000U
#define ARM64_LDR_D 0xFD400000U
#define ARM64_LDR_S 0xBD400000U
#define ARM64_STR_X 0xF9000000U
#define ARM64_STR_W 0xB9000000U
#define ARM64_STR_B 0x39000000U
#define ARM64_STR_H 0x79000000U
#define ARM64_STR_D 0xFD000000U
#define ARM64_STR_S 0xBD000000U
/* Load/store register (unscaled immediate) */
#define ARM64_LDUR_X 0xF8400000U
#define ARM64_LDUR_W 0xB8400000U
#define ARM64_LDUR_B 0x38400000U
#define ARM64_LDUR_H 0x78400000U
#define ARM64_STUR_X 0xF8000000U
#define ARM64_STUR_W 0xB8000000U
#define ARM64_STUR_B 0x38000000U
#define ARM64_STUR_H 0x78000000U
/* Load/store register (register offset) */
#define ARM64_LDR_X_REG 0xF8606800U
#define ARM64_LDR_W_REG 0xB8606800U
#define ARM64_LDR_B_REG 0x38606800U
#define ARM64_LDR_H_REG 0x78606800U
#define ARM64_STR_X_REG 0xF8206800U
#define ARM64_STR_W_REG 0xB8206800U
#define ARM64_STR_B_REG 0x38206800U
#define ARM64_STR_H_REG 0x78206800U
/* Load/store (pre/post-indexed) */
#define ARM64_STR_X_PRE 0xF8000000U /* STR X pre-indexed base */
#define ARM64_LDR_X_POST 0xF8400000U /* LDR X post-indexed base */
/* SIMD load/store (unsigned immediate) */
#define ARM64_LDR_SCALAR 0x3D400000U /* Base for scalar load (size built dynamically) */
#define ARM64_LDR_S_VEC 0xBD400000U
#define ARM64_LDR_D_VEC 0xFD400000U
#define ARM64_LDR_Q_VEC 0x3DC00000U
#define ARM64_STR_SCALAR 0x3D000000U /* Base for scalar store (size built dynamically) */
#define ARM64_STR_S_VEC 0xBD000000U
#define ARM64_STR_D_VEC 0xFD000000U
#define ARM64_STR_Q_VEC 0x3D800000U
/* SIMD load/store (unscaled immediate) */
#define ARM64_LDUR_S_SIMD 0xBC400000U
#define ARM64_LDUR_D_SIMD 0xFC400000U
#define ARM64_LDUR_Q_SIMD 0x3C400000U
#define ARM64_STUR_S_SIMD 0xBC000000U
#define ARM64_STUR_D_SIMD 0xFC000000U
#define ARM64_STUR_Q_SIMD 0x3C000000U
/* SIMD load/store (register offset) */
#define ARM64_LDR_S_REG 0xBC606800U
#define ARM64_LDR_D_REG 0xFC606800U
#define ARM64_LDR_Q_REG 0x3C606800U
#define ARM64_STR_S_REG 0xBC206800U
#define ARM64_STR_D_REG 0xFC206800U
#define ARM64_STR_Q_REG 0x3C206800U
/* Load/store pair */
#define ARM64_LDP_X 0xA9400000U
#define ARM64_LDP_X_PRE 0xA9C00000U
#define ARM64_LDP_X_POST 0xA8C00000U
#define ARM64_STP_X 0xA9000000U
#define ARM64_STP_X_PRE 0xA9800000U
#define ARM64_STP_X_POST 0xA8800000U
#define ARM64_LDP_D 0x6D400000U
#define ARM64_LDP_D_PRE 0x6DC00000U
#define ARM64_LDP_D_POST 0x6CC00000U
#define ARM64_STP_D 0x6D000000U
#define ARM64_STP_D_PRE 0x6D800000U
#define ARM64_STP_D_POST 0x6C800000U
/* Branch instructions */
#define ARM64_B 0x14000000U
#define ARM64_BL 0x94000000U
#define ARM64_BR 0xD61F0000U
#define ARM64_BLR 0xD63F0000U
#define ARM64_RET 0xD65F0000U
/* Conditional branch */
#define ARM64_B_COND 0x54000000U
/* Compare and branch */
#define ARM64_CBZ 0x34000000U
#define ARM64_CBNZ 0x35000000U
/* System instructions */
#define ARM64_NOP 0xD503201FU
#define ARM64_ISB 0xD50330DFU
#define ARM64_DSB 0xD503309FU
#define ARM64_DMB 0xD50330BFU
#define ARM64_MRS 0xD5380000U
#define ARM64_MSR 0xD5180000U
#define ARM64_MRS_FPCR 0xD53B4400U
#define ARM64_MRS_FPSR 0xD53B4420U
#define ARM64_MSR_FPCR 0xD51B4400U
#define ARM64_MSR_FPSR 0xD51B4420U
/* Shifts (register) */
#define ARM64_LSL_REG 0x1AC02000U
#define ARM64_LSR_REG 0x1AC02400U
#define ARM64_ASR_REG 0x1AC02800U
#define ARM64_ROR_REG 0x1AC02C00U
/* Shifts (immediate - UBFM/SBFM) */
#define ARM64_LSL_IMM 0xD3400000U
#define ARM64_LSR_IMM 0xD3400000U
#define ARM64_LSR_IMM_32 0x53000000U /* 32-bit LSR base */
#define ARM64_ASR_IMM 0x93400000U
/* Shifted register encoding for ORR/AND/EOR */
#define ARM64_SHIFT_LSL(imm) (((uint32_t)(imm) & 63) << 10)
#define ARM64_SHIFT_LSR(imm) (0x00200000U | (((uint32_t)(imm) & 63) << 10))
#define ARM64_SHIFT_ASR(imm) (0x00400000U | (((uint32_t)(imm) & 63) << 10))
#define ARM64_SHIFT_ROR(imm) (0x00600000U | (((uint32_t)(imm) & 63) << 10))
/* UBFM/SBFM immediate fields (for LSL/LSR/ASR immediate aliases) */
#define ARM64_IMM_R(r) (((uint32_t)(r) & 0x3F) << 16)
#define ARM64_IMM_S(s) (((uint32_t)(s) & 0x3F) << 10)
/* Extended register encoding */
#define ARM64_EXTEND_LSL(lsl) (((uint32_t)(lsl) & 7) << 10)
/* MOV (register) - ORR with zero register */
#define ARM64_MOV_REG 0x2A0003E0U
/* Address generation */
#define ARM64_ADRP 0x90000000U
#define ARM64_ADR 0x10000000U
/* Logical immediate */
#define ARM64_AND_IMM 0x12000000U
#define ARM64_ORR_IMM_BASE 0x32000000U
#define ARM64_EOR_IMM 0x52000000U
#define ARM64_ANDS_IMM 0x72000000U
#define ARM64_ORR_IMM 0x320003E0U /* ORR immediate alias with Rn = XZR/WZR */
/* ------------------------------------------------------------------ */
/* ARM64 instruction encoding helper macros */
/* ------------------------------------------------------------------ */
/* Register field encodings */
#define ARM64_RD(r) ((uint32_t)(r) & 0x1FU)
#define ARM64_RN(r) (((uint32_t)(r) & 0x1FU) << 5)
#define ARM64_RM(r) (((uint32_t)(r) & 0x1FU) << 16)
#define ARM64_RT(r) ((uint32_t)(r) & 0x1FU)
#define ARM64_RT2(r) (((uint32_t)(r) & 0x1FU) << 10)
/* Immediate field encodings */
#define ARM64_IMM12(v) (((uint32_t)(v) & 0xFFFU) << 10)
#define ARM64_IMM7(v) (((uint32_t)(v) & 0x7FU) << 15)
#define ARM64_IMM14(v) (((uint32_t)(v) & 0x3FFFU) << 5)
#define ARM64_IMM16(v) (((uint32_t)(v) & 0xFFFFU) << 5)
#define ARM64_IMM_HW(v, hw) (((uint32_t)(v) & 0xFFFFU) << 5 | (((hw) & 3) << 21))
/* Shift and size encodings */
#define ARM64_SIZE(s) (((uint32_t)(s) & 3) << 30)
#define ARM64_SF(s) (((uint32_t)(s) & 1) << 31)
#define ARM64_S(v) (((uint32_t)(v) & 1) << 29)
#define ARM64_N(v) (((uint32_t)(v) & 1) << 22)
#define ARM64_SH(v) (((uint32_t)(v) & 1) << 22)
/* Condition code encoding */
#define ARM64_COND(c) ((uint32_t)(c) & 0xFU)
/* Branch offset encoding */
#define ARM64_OFFSET26(v) (((uint32_t)(v) >> 2) & 0x3FFFFFFU)
#define ARM64_OFFSET19(v) (((uint32_t)(v) >> 2) & 0x7FFFFU)
#define ARM64_OFFSET14(v) (((uint32_t)(v) >> 2) & 0x3FFFU)
/* Special register field (for MRS/MSR) */
#undef ARM64_SYSREG
#define ARM64_SYSREG(op0, op1, crn, crm, op2) \
((((op0) & 3) << 19) | (((op1) & 7) << 16) | \
(((crn) & 15) << 12) | (((crm) & 15) << 8) | (((op2) & 7) << 5))
/* Barrier option encoding */
#define ARM64_ISB_OPTION(opt) (((uint32_t)(opt) & 0xFU) << 8)
#define ARM64_DSB_OPTION(opt) (((uint32_t)(opt) & 0xFU) << 8)
#define ARM64_DMB_OPTION(opt) (((uint32_t)(opt) & 0xFU) << 8)
/* Additional opcodes for code generator - VERIFIED */
/* Note: Many of these are specific instances, not general templates */
/* Floating-point move - VERIFIED */
#define ARM64_FMOV_D_S 0x1E604000U /* FMOV Dd,Dn (scalar) */
#define ARM64_FMOV_X_D 0x9E660000U /* FMOV Xd,Dn (general to FP) */
#define ARM64_FMOV_W_S 0x1E260000U /* FMOV Wd,Sn (general to FP) */
/* ARM64_FMOV_S_D removed: 0x4EA01C00 is SIMD vector, not scalar FMOV */
/* Use 0x1E204000 for FMOV Sd,Sn or 0x1E604000 variant for cross-size */
/* FMOV variants for code generator */
#define ARM64_FMOV_SCALAR 0x1E604000U /* FMOV Dd, Dn (scalar FP) */
#define ARM64_FMOV_XD 0x9E660000U /* FMOV Xd, Dn (FP to GP 64-bit) */
#define ARM64_FMOV_WS 0x1E260000U /* FMOV Wd, Sn (FP to GP 32-bit) */
/* MOV vector (ORR vector register alias) */
#define ARM64_MOV_V16B 0x4EA01C00U /* MOV Vd.16B, Vn.16B (ORR vector, Rm=Rn alias) */
/* Load/Store SIMD&FP - Base opcodes (register fields must be filled in) */
#define ARM64_STR_Q_PRE 0x3C800000U /* STR Q pre-index base */
#define ARM64_LDR_Q_POST 0x3CC00000U /* LDR Q post-index base */
/* LDPSW - Base opcode (register fields must be filled in) */
/* Use gen_ldst_pair() with appropriate mode for LDPSW */
/* Base encodings: 0x68C00000 (post), 0x69400000 (offset), 0x69C00000 (pre) */
/* ARM64_LDR_S_SIMD removed: 0x0D00801C is not a standard encoding */
/* Use ARM64_LDR_S (0xBD400000) for scalar S or ARM64_LDR_S_VEC for SIMD */
/* MOV between SIMD and general - Use UMOV/SMOV instead */
/* ARM64_MOV_V_D removed: 0x4E083C00 is UMOV/SMOV encoding */
/* Use appropriate UMOV/SMOV base: 0x0E002C00/0x0E003C00 (32-bit) */
/* or 0x4E002C00/0x4E003C00 (64-bit) */
/* Verified from previous section */
#define ARM64_FCMP 0x1E202008U /* FCMP with zero */
#define ARM64_SDIV 0x1AC00C00U /* SDIV (32-bit) */
/* EXTR (Extract) */
#define ARM64_EXTR 0x13800000U /* EXTR Wd, Wn, Wm, #imm (32-bit) */
#define ARM64_EXTR64 0x93C00000U /* EXTR Xd, Xn, Xm, #imm (64-bit) */
/* ARM64_MUL removed - use ARM64_MUL_REG with gen_dp_reg() */
/* ORR shifted - Base opcodes (register fields must be filled in) */
#define ARM64_ORR_REG_LSL 0x2A000000U /* ORR (shifted register) base */
/* ARM64_ORR_REG_LSL32 removed: use ARM64_ORR_REG_LSL with SF=1 */
/* ARM64_ORR_REG_MOV is duplicate of ARM64_MOV_REG */
/* LSR immediate - These are UBFM encodings, use gen_shift() instead */
/* Base UBFM encodings: 0x53000000 (W), 0xD3400000 (X) */
/* gen_shift() handles immr/imms encoding for LSR/LSL/ASR */
/* ARM64_LSR_W_8, ARM64_LSR_X_8, ARM64_LSR_X_16, ARM64_LSR_X_24 removed */
/* They are specific instances, not templates */
/* SUB shifted - Base opcode (use gen_sub_reg or asm handler) */
#define ARM64_SUB_REG_LSL 0xCB000000U /* SUB (shifted register) base */
/* Duplicates removed: ARM64_LDP_X, ARM64_B, ARM64_BL, ARM64_BR, ARM64_NOP */
/* These are already defined in their respective sections above */

View File

@ -56,7 +56,7 @@
#define RC_C67_B12 0x04000000
#define RC_C67_B13 0x08000000
#define RC_IRET RC_C67_A4 /* function return: integer register */
#define RC_LRET RC_C67_A5 /* function return: second integer register */
#define RC_IRE2 RC_C67_A5 /* function return: second integer register */
#define RC_FRET RC_C67_A4 /* function return: float register */
/* pretty names for the registers */
@ -89,7 +89,7 @@ enum {
/* return registers for function */
#define REG_IRET TREG_C67_A4 /* single word int return register */
#define REG_LRET TREG_C67_A5 /* second word return register (for long long) */
#define REG_IRE2 TREG_C67_A5 /* second word return register (for long long) */
#define REG_FRET TREG_C67_A4 /* float return register */
/* defined if function parameters must be evaluated in reverse order */
@ -108,11 +108,18 @@ enum {
/* maximum alignment (for aligned attribute support) */
#define MAX_ALIGN 8
#undef CONFIG_TCC_BCHECK
/******************************************************/
#else /* ! TARGET_DEFS_ONLY */
/******************************************************/
#define USING_GLOBALS
#include "tcc.h"
ST_DATA const char * const target_machine_defs =
"__C67__\0"
;
ST_DATA const int reg_classes[NB_REGS] = {
/* eax */ RC_INT | RC_FLOAT | RC_EAX,
// only allow even regs for floats (allow for doubles)
@ -226,11 +233,6 @@ void gsym_addr(int t, int a)
}
}
void gsym(int t)
{
gsym_addr(t, ind);
}
// these are regs that tcc doesn't really know about,
// but assign them unique values so the mapping routines
// can distinguish them
@ -369,7 +371,7 @@ int C67_map_D12(char *s)
void C67_asm(char *s, int a, int b, int c)
void C67_asm(const char *s, int a, int b, int c)
{
BOOL xpath;
@ -1944,8 +1946,9 @@ void gfunc_call(int nb_args)
// parameters are loaded and restored upon return (or if/when needed).
/* generate function prolog of type 't' */
void gfunc_prolog(CType * func_type)
void gfunc_prolog(Sym *func_sym)
{
CType *func_type = &func_sym->type;
int addr, align, size, func_call, i;
Sym *sym;
CType *type;
@ -1955,8 +1958,6 @@ void gfunc_prolog(CType * func_type)
addr = 8;
/* if the function returns a structure, then add an
implicit pointer parameter */
func_vt = sym->type;
func_var = (sym->f.func_type == FUNC_ELLIPSIS);
if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
func_vc = addr;
addr += 4;
@ -1967,7 +1968,7 @@ void gfunc_prolog(CType * func_type)
/* define parameters */
while ((sym = sym->next) != NULL) {
type = &sym->type;
sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | lvalue_type(type->t), addr);
gfunc_set_param(sym, addr, 0);
size = type_size(type, &align);
size = (size + 3) & ~3;
@ -2035,6 +2036,16 @@ void gfunc_epilog(void)
}
}
ST_FUNC void gen_fill_nops(int bytes)
{
if ((bytes & 3))
tcc_error("alignment of code section not multiple of 4");
while (bytes > 0) {
C67_NOP(4);
bytes -= 4;
}
}
/* generate a jump to a label */
int gjmp(int t)
{
@ -2067,15 +2078,13 @@ void gjmp_addr(int a)
}
/* generate a test. set 'inv' to invert test. Stack entry is popped */
int gtst(int inv, int t)
ST_FUNC int gjmp_cond(int op, int t)
{
int ind1, n;
int v, *p;
int ind1;
int inv = op & 1;
if (nocode_wanted)
return t;
v = vtop->r & VT_VALMASK;
if (nocode_wanted) {
;
} else if (v == VT_CMP) {
/* fast case : can jump directly since flags are set */
// C67 uses B2 sort of as flags register
ind1 = ind;
@ -2093,16 +2102,18 @@ int gtst(int inv, int t)
C67_NOP(5);
t = ind1; //return where we need to patch
} else if (v == VT_JMP || v == VT_JMPI) {
/* && or || optimization */
if ((v & 1) == inv) {
return t;
}
ST_FUNC int gjmp_append(int n0, int t)
{
if (n0) {
int n = n0, *p;
/* insert vtop->c jump list in t */
// I guess the idea is to traverse to the
// null at the end of the list and store t
// there
n = vtop->c.i;
while (n != 0) {
p = (int *) (cur_text_section->data + n);
@ -2112,14 +2123,8 @@ int gtst(int inv, int t)
}
*p |= (t & 0xffff) << 7;
*(p + 1) |= ((t >> 16) & 0xffff) << 7;
t = vtop->c.i;
} else {
t = gjmp(t);
gsym(vtop->c.i);
t = n0;
}
}
vtop--;
return t;
}
@ -2195,10 +2200,8 @@ void gen_opi(int op)
ALWAYS_ASSERT(FALSE);
vtop--;
if (op >= TOK_ULT && op <= TOK_GT) {
vtop->r = VT_CMP;
vtop->c.i = op;
}
if (op >= TOK_ULT && op <= TOK_GT)
vset_VT_CMP(0x80);
break;
case '-':
case TOK_SUBC1: /* sub with carry generation */
@ -2257,7 +2260,7 @@ void gen_opi(int op)
call_func:
vswap();
/* call generic idiv function */
vpush_global_sym(&func_old_type, t);
vpush_helper_func(t);
vrott(3);
gfunc_call(2);
vpushi(0);
@ -2354,7 +2357,7 @@ void gen_opf(int op)
} else {
ALWAYS_ASSERT(FALSE);
}
vtop->r = VT_CMP; // tell TCC that result is in "flags" actually B2
vset_VT_CMP(0x80);
} else {
if (op == '+') {
if ((ft & VT_BTYPE) == VT_DOUBLE) {
@ -2388,18 +2391,18 @@ void gen_opf(int op)
// must call intrinsic DP floating point divide
vswap();
/* call generic idiv function */
vpush_global_sym(&func_old_type, TOK__divd);
vpush_helper_func(TOK__divd);
vrott(3);
gfunc_call(2);
vpushi(0);
vtop->r = REG_FRET;
vtop->r2 = REG_LRET;
vtop->r2 = REG_IRE2;
} else {
// must call intrinsic SP floating point divide
vswap();
/* call generic idiv function */
vpush_global_sym(&func_old_type, TOK__divf);
vpush_helper_func(TOK__divf);
vrott(3);
gfunc_call(2);
vpushi(0);

View File

@ -24,7 +24,7 @@
/* Returns 1 for a code relocation, 0 for a data relocation. For unknown
relocations, returns -1. */
int code_reloc (int reloc_type)
ST_FUNC int code_reloc (int reloc_type)
{
switch (reloc_type) {
case R_C60_32:
@ -39,15 +39,13 @@ int code_reloc (int reloc_type)
case R_C60_PLT32:
return 1;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
/* Returns an enumerator to describe whether and when the relocation needs a
GOT and/or PLT entry to be created. See tcc.h for a description of the
different values. */
int gotplt_entry_type (int reloc_type)
ST_FUNC int gotplt_entry_type (int reloc_type)
{
switch (reloc_type) {
case R_C60_32:
@ -64,14 +62,12 @@ int gotplt_entry_type (int reloc_type)
case R_C60_GOT32:
return ALWAYS_GOTPLT_ENTRY;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
{
tcc_error("C67 got not implemented");
tcc_error_noabort("C67 got not implemented");
return 0;
}
@ -95,9 +91,7 @@ ST_FUNC void relocate_plt(TCCState *s1)
}
}
void relocate_init(Section *sr) {}
void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
{
switch(type) {
case R_C60_32:

779
configure vendored
View File

@ -27,9 +27,9 @@ mandir=""
infodir=""
sysroot=""
cross_prefix=""
os_release=""
cc="gcc"
ar="ar"
strip="strip"
bigendian="no"
mingw32="no"
LIBSUF=".a"
@ -40,224 +40,145 @@ tcc_libpaths=""
tcc_crtprefix=""
tcc_elfinterp=""
triplet=
tcc_lddir=
confvars=
suggest="yes"
cpu=
cpuver=
gcc_major=0
gcc_minor=0
cc_name="gcc"
ar_set=
cpu=
cpuver=
dwarf=
targetos=
build_cross=
quiet=
# OS specific
targetos=`uname`
case $targetos in
Darwin)
confvars="$confvars OSX"
DLLSUF=".dylib"
;;
MINGW*|MSYS*|CYGWIN*)
mingw32=yes
;;
DragonFly|OpenBSD|FreeBSD|NetBSD)
confvars="$confvars ldl=no"
;;
*)
;;
esac
# use CC/AR from environment when set
test -n "$CC" && cc="$CC"
test -n "$AR" && ar="$AR"
# set default CFLAGS if unset in environment
test -z "$CFLAGS" && CFLAGS="-Wall -O2"
# find source path
source_path=${0%configure}
source_path=${source_path%/}
source_path_used="yes"
if test -z "$source_path" -o "$source_path" = "." ; then
source_path=`pwd`
source_path_used="no"
fi
# $1: --OPTNAME=VALUE [, $2: NAME to assign-to instead of OPTNAME]
assign_opt() {
set -- "${2:-${1%%=*}}" "${1#*=}" # [--OPT]NAME VALUE
eval ${1#--}=\$2 # no risk of IFS/glob in [OPT]NAME
}
# succeed if $1 doesn't IFS-split funny (globs, spaces, ...)
good_split() {
set -- "$1" $1
test $# = 2 && test "$1" = "$2"
}
# $1: NAME[=VALUE] succeed if confvars has NAME or NAME=* element
confvars_has() {
! case " $confvars " in *" ${1%%=*} "* | *" ${1%%=*}="*)
false
esac
}
# [multiple] NAME or NAME=VAL
confvars_set() {
for cv; do
good_split "$cv" || { echo "configure: ERROR: bad config '$cv'"; exit 1; }
confvars_has "$cv" && echo "configure: WARNING: duplicate config '$cv'"
confvars="$confvars $cv"
done
}
for opt do
eval opt=\"$opt\"
case "$opt" in
--prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
--prefix=*) assign_opt "$opt"
;;
--exec-prefix=*) execprefix=`echo $opt | cut -d '=' -f 2`
--exec-prefix=*) assign_opt "$opt" execprefix
;;
--tccdir=*) tccdir=`echo $opt | cut -d '=' -f 2`
--tccdir=*) assign_opt "$opt"
;;
--bindir=*) bindir=`echo $opt | cut -d '=' -f 2`
--bindir=*) assign_opt "$opt"
;;
--libdir=*) libdir=`echo $opt | cut -d '=' -f 2`
--libdir=*) assign_opt "$opt"
;;
--includedir=*) includedir=`echo $opt | cut -d '=' -f 2`
--includedir=*) assign_opt "$opt"
;;
--sharedir=*) sharedir=`echo $opt | cut -d '=' -f 2`
--sharedir=*) assign_opt "$opt"
;;
--mandir=*) mandir=`echo $opt | cut -d '=' -f 2`
--mandir=*) assign_opt "$opt"
;;
--infodir=*) infodir=`echo $opt | cut -d '=' -f 2`
--infodir=*) assign_opt "$opt"
;;
--docdir=*) docdir=`echo $opt | cut -d '=' -f 2`
--docdir=*) assign_opt "$opt"
;;
--sysroot=*) sysroot=`echo $opt | cut -d '=' -f 2`
--sysroot=*) assign_opt "$opt"
;;
--source-path=*) source_path=`echo $opt | cut -d '=' -f 2`
--targetos=*) assign_opt "$opt"
;;
--cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2`
--source-path=*) assign_opt "$opt" source_path
;;
--cc=*) cc=`echo $opt | cut -d '=' -f 2`
--cross-prefix=*) assign_opt "$opt" cross_prefix
;;
--ar=*) ar=`echo $opt | cut -d '=' -f 2`
--os-release=*) assign_opt "$opt" os_release
;;
--extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
--cc=*) assign_opt "$opt"
;;
--extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
--ar=*) assign_opt "$opt" ; ar_set="yes"
;;
--extra-libs=*) extralibs="${opt#--extra-libs=}"
--extra-cflags=*) assign_opt "$opt" CFLAGS
;;
--sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2`
--extra-ldflags=*) assign_opt "$opt" LDFLAGS
;;
--libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2`
--extra-libs=*) assign_opt "$opt" extralibs
;;
--crtprefix=*) tcc_crtprefix=`echo $opt | cut -d '=' -f 2`
--sysincludepaths=*) assign_opt "$opt" tcc_sysincludepaths
;;
--elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2`
--libpaths=*) assign_opt "$opt" tcc_libpaths
;;
--triplet=*) triplet=`echo $opt | cut -d '=' -f 2`
--crtprefix=*) assign_opt "$opt" tcc_crtprefix
;;
--cpu=*) cpu=`echo $opt | cut -d '=' -f 2`
--elfinterp=*) assign_opt "$opt" tcc_elfinterp
;;
--enable-cross) confvars="$confvars cross"
--triplet=*) assign_opt "$opt"
;;
--disable-static) confvars="$confvars static=no"
--cpu=*) assign_opt "$opt"
;;
--enable-static) confvars="$confvars static"
--dwarf=*) confvars_set "dwarf=${opt#*=}"
;;
--disable-rpath) confvars="$confvars rpath=no"
--enable-cross) confvars_set cross
;;
--strip-binaries) confvars="$confvars strip"
--disable-static) confvars_set static=no
;;
--with-libgcc) confvars="$confvars libgcc"
--enable-static) confvars_set static
;;
--with-selinux) confvars="$confvars selinux"
--disable-rpath) confvars_set rpath=no
;;
--config-mingw32*) mingw32=$(echo "$opt=yes" | cut -d '=' -f 2)
--debug) confvars_set debug
;;
--config-*) confvars="$confvars ${opt#--config-}"; suggest="no"
--with-selinux) confvars_set selinux
;;
--tcc-switches=*) assign_opt "$opt" tcc_switches
;;
--config-mingw32) mingw32=yes
;;
--config-mingw32=*) assign_opt "$opt" mingw32
;;
--config-*) confvars_set "${opt#--config-}"; suggest="no"
;;
--help|-h) show_help="yes"
;;
-q) quiet=yes
;;
*) echo "configure: WARNING: unrecognized option $opt"
;;
esac
done
if test -z "$cpu" ; then
if test -n "$ARCH" ; then
cpu="$ARCH"
else
cpu=`uname -m`
fi
fi
case "$cpu" in
x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386)
cpu="i386"
;;
x86_64|amd64|x86-64)
cpu="x86_64"
;;
arm*)
case "$cpu" in
arm|armv4l)
cpuver=4
;;
armv5tel|armv5tejl)
cpuver=5
;;
armv6j|armv6l)
cpuver=6
;;
armv7a|armv7l)
cpuver=7
;;
esac
cpu="arm"
;;
aarch64)
cpu="aarch64"
;;
alpha)
cpu="alpha"
;;
"Power Macintosh"|ppc|ppc64)
cpu="ppc"
;;
mips)
cpu="mips"
;;
s390)
cpu="s390"
;;
*)
echo "Unsupported CPU"
exit 1
;;
esac
# Checking for CFLAGS
if test -z "$CFLAGS"; then
CFLAGS="-Wall -g -O2"
fi
if test "$mingw32" = "yes" ; then
if test "$source_path_used" = "no"; then
source_path="."
fi
if test "$cc" = gcc; then
test -z "$LDFLAGS" && LDFLAGS="-static"
fi
test -z "$prefix" && prefix="C:/Program Files/tcc"
test -z "$tccdir" && tccdir="${prefix}"
test -z "$bindir" && bindir="${tccdir}"
test -z "$docdir" && docdir="${tccdir}/doc"
test -z "$libdir" && libdir="${tccdir}/libtcc"
confvars="$confvars WIN32"
LIBSUF=".lib"
EXESUF=".exe"
DLLSUF=".dll"
else
if test -z "$prefix" ; then
prefix="/usr/local"
fi
if test -z "$sharedir" ; then
sharedir="${prefix}/share"
fi
if test x"$execprefix" = x""; then
execprefix="${prefix}"
fi
if test x"$libdir" = x""; then
libdir="${execprefix}/lib"
fi
if test x"$bindir" = x""; then
bindir="${execprefix}/bin"
fi
if test x"$docdir" = x""; then
docdir="${sharedir}/doc"
fi
if test x"$mandir" = x""; then
mandir="${sharedir}/man"
fi
if test x"$infodir" = x""; then
infodir="${sharedir}/info"
fi
if test x"$tccdir" = x""; then
tccdir="${libdir}/tcc"
fi
if test x"$includedir" = x""; then
includedir="${prefix}/include"
fi
fi # mingw32
if test x"$show_help" = "xyes" ; then
show_help() {
cat << EOF
Usage: configure [options]
Options: [defaults in brackets after descriptions]
@ -275,103 +196,346 @@ Standard options:
--docdir=DIR documentation in DIR [SHAREDIR/doc/tcc]
--mandir=DIR man documentation in DIR [SHAREDIR/man]
--infodir=DIR info documentation in DIR [SHAREDIR/info]
-q be quiet
Advanced options (experts only):
--source-path=PATH path of source code [$source_path]
--cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix]
--sysroot=PREFIX prepend PREFIX to library/include paths []
--sysroot=PREFIX prepend PREFIX to library/include paths [$sysroot]
--cc=CC use C compiler CC [$cc]
--ar=AR create archives using AR [$ar]
--extra-cflags= specify compiler flags [$CFLAGS]
--extra-ldflags= specify linker options []
--cpu=CPU CPU [$cpu]
--strip-binaries strip symbol tables from resulting binaries
--extra-ldflags= specify linker options [$LDFLAGS]
--debug include debug info with resulting binaries
--disable-static make libtcc.so instead of libtcc.a
--enable-static make libtcc.a instead of libtcc.dll (win32)
--disable-rpath disable use of -rpath with the above
--with-libgcc use libgcc_s.so.1 instead of libtcc1.a
--enable-cross build cross compilers
--with-selinux use mmap for executable memory (with tcc -run)
--disable-rpath disable use of -rpath with libtcc.so
--with-selinux use mmap for executable memory (tcc -run)
--enable-cross build all cross compilers (see also 'make help')
--sysincludepaths=... specify system include paths, colon separated
--libpaths=... specify system library paths, colon separated
--crtprefix=... specify locations of crt?.o, colon separated
--elfinterp=... specify elf interpreter
--triplet=... specify system library/include directory triplet
--config-uClibc,-musl,-mingw32... enable system specific configurations
--tcc-switches=... specify implicit switches passed to tcc
--config-uClibc,-musl enable system specific configurations
--config-mingw32[=yes|no] build on windows using msys, busybox, etc.
--config-backtrace=no disable stack backtraces (with -run or -bt)
--config-bcheck=no disable bounds checker (-b)
--config-predefs=no do not compile tccdefs.h, instead just include
--config-new_macho=no|yes force apple object format (autodetect osx <= 10)
--config-new_dtags=yes use new ELF DTAGs (DT_RUNPATH instead of DT_RPATH)
--config-codesign=no do not use codesign on apple to sign executables
--config-dwarf=x use dwarf debug info instead of stabs (x=2..5)
--config-pie let tcc generate position independent executables
Cross build options (experimental):
--cpu=CPU target CPU [$cpu]
--targetos=... target OS (Darwin,WIN32,Android/Termux) [$targetos]
--os-release=x.y.z target os release x.y.z [$os_release] (with BSD systems)
--cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix]
EOF
#echo "NOTE: The object files are build at the place where configure is launched"
exit 1
}
default() # set variable unless already set and not empty
{
test -n "$2" && eval : \${$1:=\$2} # ': ${foo:=$2}'
}
default_conf() # add one config to confvars unless already present
{
confvars_has "$1" || confvars_set "$1"
}
if test -z "${source_path#.}" ; then
source_path=$(pwd)
source_path_used="no"
else
source_path_used="yes"
fi
# OS specific
buildos=$(uname)
cpu_sys=$(uname -m)
default os_release "$(uname -r)"
case $buildos in
Windows_NT|MINGW*|MSYS*|CYGWIN*)
buildos="WIN32"
case "$MSYSTEM" in
MINGW32) cpu_sys=i386 ;;
MINGW64) cpu_sys=x86_64 ;;
CLANGARM64|MINGW_ARM64) cpu_sys=arm64 ;;
esac
;;
Linux)
if test "$(uname -o)" = "Android"; then
buildos=Android
if test -n "$TERMUX_VERSION"; then
buildos=Termux
fi
fi
;;
esac
if test "$mingw32" = "yes"; then
default targetos WIN32
else
default targetos "$buildos"
fi
default cpu "$cpu_sys"
# check for crpss build
if test "$cpu" != "$cpu_sys" ||
test "$targetos" != "$buildos" ||
test -n "$cross_prefix"
then
build_cross="yes"
cc="${cross_prefix}${cc}"
ar="${cross_prefix}${ar}"
strip="${cross_prefix}${strip}"
fi
if test -z "$cross_prefix" ; then
CONFTEST=./conftest$EXESUF
if ! $cc -o $CONFTEST $source_path/conftest.c 2>/dev/null ; then
echo "configure: error: '$cc' failed to compile conftest.c."
case "$cpu" in
x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386)
cpu="i386"
;;
x86_64|amd64|x86-64)
cpu="x86_64"
;;
evbarm)
case "`uname -p`" in
aarch64|arm64)
cpu="arm64"
;;
earmv*)
cpu="arm"
;;
esac
;;
aarch64|arm64|evbarm)
cpu="arm64"
;;
arm*)
case "$cpu" in
arm|armv4l)
cpuver=4
;;
armv5tel|armv5tejl)
cpuver=5
;;
armv6j|armv6l)
cpuver=6
;;
armv7|armv7a|armv7l)
cpuver=7
;;
esac
cpu="arm"
;;
alpha)
cpu="alpha"
;;
"Power Macintosh"|ppc|ppc64)
cpu="ppc"
;;
mips)
cpu="mips"
;;
s390)
cpu="s390"
;;
riscv64)
cpu="riscv64"
;;
*)
echo "Unsupported CPU"
exit 1
;;
esac
case $targetos in
Darwin)
confvars_set OSX dwarf=4
default_conf "codesign"
DLLSUF=".dylib"
if test -z "$build_cross"; then
cc=`command -v cc`
cc=`readlink $cc || echo clang`
tcc_usrinclude="`xcrun --show-sdk-path`/usr/include"
default tcc_sysincludepaths "{B}/include:$tcc_usrinclude"
if test "${confvars%new_macho*}" = "${confvars}"; then
# if new_macho was not specified and (known) ver <= 10, use old (=no)
osxver=$(sw_vers -productVersion 2>/dev/null) # X.Y.Z
osxver=${osxver%%.*} # major version (or empty on sw_vers error)
[ "${osxver:-11}" -ge 11 ] || confvars_set new_macho=no
fi
fi
# on OSX M1 with --cpu=x86_64, build a tcc to run under rosetta entirely
if test "$cpu" = x86_64 && test "$cpu_sys" = arm64; then
CFLAGS="$CFLAGS -arch $cpu"
LDFLAGS="$LDFLAGS -arch $cpu"
fi
;;
DragonFly|OpenBSD|FreeBSD|NetBSD)
confvars_set BSD ldl=no
;;
Android|Termux)
if test "$targetos" = "Termux"; then
targetos=Android
default sysroot "/data/data/com.termux/files/usr"
else
default sysroot "/usr"
fi
default prefix "${sysroot}"
confvars_set Android new_dtags rpath=no dwarf=4
test "${cpu}" != "i386" && confvars_set pie
default_conf "static=no"
case "$cpu" in
arm) default triplet "arm-linux-androideabi"; cpuver=7 ;;
arm64) default triplet "aarch64-linux-android" ;;
x86_64) default triplet "x86_64-linux-android" ;;
i386) default triplet "i686-linux-android" ;;
esac
test "${cpu%64}" != "${cpu}" && S="64" || S=""
default tcc_sysincludepaths "{B}/include:{R}/include:{R}/include/${triplet}"
default tcc_libpaths "{B}:{R}/lib:/system/lib${S}"
default tcc_crtprefix "{R}/lib"
default tcc_elfinterp "/system/bin/linker${S}"
default tcc_switches "-Wl,-rpath=$sysroot/lib"
;;
WIN32)
mingw32="yes"
confvars="WIN32 $confvars" # WIN32 intentionally first (commit 729918ef)
default prefix "C:/Program Files/tcc"
default tccdir "${prefix}"
default bindir "${tccdir}"
default docdir "${tccdir}/doc"
default libdir "${tccdir}/libtcc"
# set tccdir at runtime from executable path
test "$tccdir" = "$bindir" && tccdir_auto="yes"
# chech $cc to avoid mingw gcc dependencies such as 'libgcc_s_dw2-1.dll'
# (no confirmed $cc_name yet, and also will never have if cross compiling)
test "${cc%gcc*}" = "$cc" || default LDFLAGS "-static"
LIBSUF=".lib"
EXESUF=".exe"
DLLSUF=".dll"
if test "$source_path_used" = "no"; then
source_path="."
fi
;;
*)
;;
esac
if test "$mingw32" = "no"; then
default prefix "/usr/local"
default execprefix "${prefix}"
default libdir "${execprefix}/lib"
default bindir "${execprefix}/bin"
default tccdir "${libdir}/tcc"
default includedir "${prefix}/include"
default sharedir "${prefix}/share"
default docdir "${sharedir}/doc"
default mandir "${sharedir}/man"
default infodir "${sharedir}/info"
fi
if test x"$show_help" = "xyes" ; then
show_help
fi
CONFTEST=./conftest$EXESUF
if test -z "$cross_prefix" \
&& $cc -o $CONFTEST "$source_path/conftest.c" \
&& $CONFTEST 2>/dev/null; then
cc_name="$($CONFTEST compiler)"
gcc_major="$($CONFTEST version)"
gcc_minor="$($CONFTEST minor)"
else
if test -z "$build_cross"; then
echo "configure: error: '$cc' failed to compile conftest.c."
fi
if test "${cc%tcc*}" != "$cc"; then
cc_name="tcc"
elif test "${cc%clang*}" != "$cc"; then
cc_name="clang"
fi
fi
if test -z "$build_cross"; then
bigendian="$($CONFTEST bigendian)"
_triplet="$($CONFTEST triplet)"
if test "$mingw32" = "no" ; then
if test -z "$triplet"; then
tt="$($CONFTEST triplet)"
if test -n "$tt" -a -f "/usr/lib/$tt/crti.o" ; then
triplet="$tt"
if test -z "$triplet" && test -n "$_triplet"; then
if test -f "/usr/lib/$_triplet/crti.o"; then
triplet="$_triplet"
fi
fi
if test -z "$triplet"; then
if test $cpu = "x86_64" -o $cpu = "aarch64" ; then
case $cpu in x86_64|arm64|riscv64)
if test -f "/usr/lib64/crti.o" ; then
tcc_lddir="lib64"
default tcc_libpaths "{B}:/usr/lib64"
default tcc_crtprefix "/usr/lib64"
fi
esac
fi
fi
if test "$cpu" = "arm" ; then
if test "${triplet%eabihf}" != "$triplet" ; then
confvars="$confvars arm_eabihf"
elif test "${triplet%eabi}" != "$triplet" ; then
confvars="$confvars arm_eabi"
fi
if grep -s -q "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo ; then
confvars="$confvars arm_vfp"
fi
fi
if test "$suggest" = "yes"; then
if test -f "/lib/ld-uClibc.so.0" ; then
echo "Perhaps you want ./configure --config-uClibc"
fi
if test -f "/lib/ld-musl-$cpu.so.1"; then
if test -f "/lib/ld-musl-${cpu}.so.1"; then
echo "Perhaps you want ./configure --config-musl"
fi
fi
fi
else
# if cross compiling, cannot launch a program, so make a static guess
# can only make guesses about compiler and target
case $cpu in
ppc|mips|s390) bigendian=yes;;
esac
case $targetos in
Linux)
default triplet "${cpu}-linux-gnu"
esac
fi
if test "$bigendian" = "yes" ; then
confvars="$confvars BIGENDIAN"
confvars_set BIGENDIAN
fi
if test "$cpu" = "arm"; then
if test "${triplet%eabihf}" != "$triplet" ; then
confvars_set arm_eabihf arm_vfp
elif test "${triplet%eabi}" != "$triplet" ; then
confvars_set arm_eabi arm_vfp
elif test -z "$build_cross"; then
if test "${_triplet%eabihf}" != "$_triplet" ; then
confvars_set arm_eabihf arm_vfp
elif test "${_triplet%eabi}" != "$_triplet" ; then
confvars_set arm_eabi arm_vfp
elif grep -s -q "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo ; then
confvars_set arm_vfp
fi
fi
if test -z "$build_cross"; then
if grep -s -q "^Features.* \(idiv\)" /proc/cpuinfo ; then
confvars_set arm_idiv
fi
fi
fi
# a final configuration tuning
if ! echo "$cc" | grep -q "tcc"; then
OPT1="-Wdeclaration-after-statement -fno-strict-aliasing"
if test "$cc_name" != "tcc"; then
OPT1="-Wdeclaration-after-statement" #-fno-strict-aliasing
# we want -Wno- but gcc does not always reject unknown -Wno- options
OPT2="-Wpointer-sign -Wsign-compare -Wunused-result"
if echo "$cc" | grep -q "clang"; then
OPT1="$OPT1 -fheinous-gnu-extensions"
OPT2="$OPT2 -Wstring-plus-int"
if test "$cc_name" = "clang"; then
OPT2= #"-Wstring-plus-int"
else
OPT2="-Wunused-result"
fi
$cc $OPT1 $OPT2 -o a.out -c -xc - < /dev/null > cc_msg.txt 2>&1
for o in $OPT1; do # enable these options
@ -383,10 +547,13 @@ if ! echo "$cc" | grep -q "tcc"; then
# cat cc_msg.txt
# echo $CFLAGS
rm -f cc_msg.txt a.out
else # cc is tcc
test "$ar_set" || ar="$cc -ar"
fi
fcho() { if test -n "$2"; then echo "$1$2"; fi }
if test -z "$quiet"; then
fcho "Binary directory " "$bindir"
fcho "TinyCC directory " "$tccdir"
fcho "Library directory " "$libdir"
@ -396,12 +563,21 @@ fcho "Info directory " "$infodir"
fcho "Doc directory " "$docdir"
fcho "Target root prefix " "$sysroot"
echo "Source path $source_path"
echo "Build OS $(uname -m -s)"
echo "C compiler $cc ($gcc_major.$gcc_minor)"
echo "Target OS $targetos"
echo "CPU $cpu"
fcho "Triplet " "$triplet"
fcho "Libs " "$tcc_libpaths"
fcho "Sysinclude " "$tcc_sysincludepaths"
fcho "Crt " "$tcc_crtprefix"
fcho "Elfinterp " "$tcc_elfinterp"
fcho "Switches " "$tcc_switches"
fcho "Config " "${confvars# }"
echo "Creating config.mak and config.h"
fi
version=$(head "$source_path/VERSION")
cat >config.mak <<EOF
# Automatically generated by configure - do not modify
@ -414,69 +590,21 @@ mandir=\$(DESTDIR)$mandir
infodir=\$(DESTDIR)$infodir
docdir=\$(DESTDIR)$docdir
CC=$cc
CC_NAME=$cc_name
GCC_MAJOR=$gcc_major
GCC_MINOR=$gcc_minor
AR=$ar
STRIP=$strip -s -R .comment -R .note
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
LIBSUF=$LIBSUF
EXESUF=$EXESUF
DLLSUF=$DLLSUF
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
ARCH=$cpu
TARGETOS=$targetos
BUILDOS=$buildos
VERSION=$version
EOF
print_inc() {
if test -n "$2"; then
echo "#ifndef $1" >> $TMPH
echo "# define $1 \"$2\"" >> $TMPH
echo "#endif" >> $TMPH
fi
}
print_mak() {
if test -n "$2"; then
echo "NATIVE_DEFINES+=-D$1=\"\\\"$2\\\"\"" >> config.mak
fi
}
print_mak_int() {
if test -n "$2"; then
echo "NATIVE_DEFINES+=-D$1=$2" >> config.mak
fi
}
echo "/* Automatically generated by configure - do not modify */" > $TMPH
print_inc CONFIG_SYSROOT "$sysroot"
print_inc CONFIG_TCCDIR "$tccdir"
print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"
print_mak CONFIG_TCC_CRTPREFIX "$tcc_crtprefix"
print_mak CONFIG_TCC_ELFINTERP "$tcc_elfinterp"
print_mak CONFIG_LDDIR "$tcc_lddir"
print_mak CONFIG_TRIPLET "$triplet"
print_mak_int TCC_CPU_VERSION "$cpuver"
if test "$cpu" = "aarch64" ; then
echo "ARCH=arm64" >> config.mak
else
echo "ARCH=$cpu" >> config.mak
fi
echo "TARGETOS=$targetos" >> config.mak
for v in $confvars ; do
if test "${v%=*}" = "$v"; then
echo "CONFIG_$v=yes" >> config.mak
else
echo "CONFIG_$v" >> config.mak
fi
done
version=`head $source_path/VERSION`
echo "VERSION = $version" >> config.mak
echo "#define TCC_VERSION \"$version\"" >> $TMPH
echo "@set VERSION $version" > config.texi
if test "$source_path_used" = "yes" ; then
case $source_path in
/*) echo "TOPSRC=$source_path";;
@ -486,18 +614,127 @@ else
echo 'TOPSRC=$(TOP)' >>config.mak
fi
# $1: macro name, $2: val to set - quoted [, $3: non-empty for #ifndef]
print_str() {
if test -n "$2"; then
test -n "$3" && echo "#ifndef $1" >> $TMPH
echo "#define $1 \"$2\"" >> $TMPH
test -n "$3" && echo "#endif" >> $TMPH
fi
}
# $1: macro name, $2: val to set [, $3: non-empty for #ifndef]
print_num() {
if test -n "$2"; then
test -n "$3" && echo "#ifndef $1" >> $TMPH
echo "#define $1 $2" >> $TMPH
test -n "$3" && echo "#endif" >> $TMPH
fi
}
cat >$TMPH <<EOF
/* Automatically generated by configure - do not modify */
#define TCC_VERSION "$version"
#define CC_NAME CC_$cc_name
#define GCC_MAJOR $gcc_major
#define GCC_MINOR $gcc_minor
#if !(TCC_TARGET_I386 || TCC_TARGET_X86_64 || TCC_TARGET_ARM\
|| TCC_TARGET_ARM64 || TCC_TARGET_RISCV64 || TCC_TARGET_C67)
EOF
predefs=1
# options that are applied only to the native tcc
for v in $cpu $confvars ; do
if test "${v%=*}" = "$v"; then
v="$v=yes"
fi
R="CONFIG_$v"
echo "$R" >> config.mak
case "$R" in
# CPU
CONFIG_i386=yes) print_num TCC_TARGET_I386 1 ;;
CONFIG_x86_64=yes) print_num TCC_TARGET_X86_64 1 ;;
CONFIG_arm64=yes) print_num TCC_TARGET_ARM64 1 ;;
CONFIG_riscv64=yes) print_num TCC_TARGET_RISCV64 1 ;;
CONFIG_arm=yes) print_num TCC_TARGET_ARM 1
print_num CONFIG_TCC_CPUVER "$cpuver" ;;
CONFIG_arm_eabihf=yes) print_num TCC_ARM_EABI 1
print_num TCC_ARM_HARDFLOAT 1 ;;
CONFIG_arm_eabi=yes) print_num TCC_ARM_EABI 1 ;;
CONFIG_arm_vfp=yes) print_num TCC_ARM_VFP 1 ;;
CONFIG_arm_idiv=yes) print_num __ARM_FEATURE_IDIV 1 ;;
# OS
CONFIG_WIN32=yes) print_num TCC_TARGET_PE 1 ;;
CONFIG_OSX=yes) print_num TCC_TARGET_MACHO 1 ;;
CONFIG_Android=yes) print_num TARGETOS_ANDROID 1 ;;
CONFIG_BSD=yes) print_num TARGETOS_$targetos 1
case "$targetos" in
FreeBSD) default tcc_elfinterp "/libexec/ld-elf.so.1";;
FreeBSD_kernel)
case "$cpu" in
x86_64) default tcc_elfinterp "/lib/ld-kfreebsd-x86-64.so.1";;
*) default tcc_elfinterp "/lib/ld.so.1";;
esac ;;
DragonFly) default tcc_elfinterp "/usr/libexec/ld-elf.so.2";;
NetBSD) default tcc_elfinterp "/usr/libexec/ld.elf_so";;
OpenBSD) default tcc_elfinterp "/usr/libexec/ld.so";;
esac
;;
CONFIG_uClibc=yes) print_num CONFIG_TCC_UCLIBC 1
default tcc_elfinterp "/lib/ld-uClibc.so.0"
;;
CONFIG_musl=yes) print_num CONFIG_TCC_MUSL 1
case "$cpu" in
arm64) default tcc_elfinterp "/lib/ld-musl-aarch64.so.1";;
*) default tcc_elfinterp "/lib/ld-musl-${cpu}.so.1";;
esac
;;
# other
CONFIG_selinux=yes) print_num CONFIG_SELINUX 1 ;;
CONFIG_pie=yes) print_num CONFIG_TCC_PIE 1 ;;
CONFIG_pic=yes) print_num CONFIG_TCC_PIC 1 ;;
CONFIG_new_dtags=yes) print_num CONFIG_NEW_DTAGS 1 ;;
CONFIG_codesign=yes) print_num CONFIG_CODESIGN 1 ;;
CONFIG_new_macho=no) print_num CONFIG_NEW_MACHO 0 ;;
CONFIG_bcheck=no) print_num CONFIG_TCC_BCHECK 0 ;;
CONFIG_backtrace=no) print_num CONFIG_TCC_BACKTRACE 0 ;;
CONFIG_dwarf=*) print_num CONFIG_DWARF_VERSION ${R#*=} ;;
CONFIG_semlock=*) print_num CONFIG_TCC_SEMLOCK ${R#*=} ;;
CONFIG_predefs=no) predefs=0 ;;
esac
done
print_str CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
print_str CONFIG_TCC_LIBPATHS "$tcc_libpaths"
print_str CONFIG_TCC_CRTPREFIX "$tcc_crtprefix"
print_str CONFIG_TCC_ELFINTERP "$tcc_elfinterp"
print_str CONFIG_TCC_SWITCHES "$tcc_switches"
print_str CONFIG_TRIPLET "$triplet"
print_str CONFIG_OS_RELEASE "$os_release"
echo "#endif" >> $TMPH && echo >> $TMPH
print_str CONFIG_SYSROOT "$sysroot" x
test "$tccdir_auto" = "yes" || print_str CONFIG_TCCDIR "$tccdir" x
print_num CONFIG_TCC_PREDEFS "$predefs"
diff $TMPH config.h >/dev/null 2>&1
if test $? -ne 0 ; then
mv -f $TMPH config.h
else
elif test -z "$quiet"; then
echo "config.h is unchanged"
fi
echo "@set VERSION $version" > config.texi
rm -f $TMPN* $CONFTEST
# ---------------------------------------------------------------------------
# build tree in object directory if source path is different from current one
use_cp=
fn_makelink()
{
tgt=$1/$2
@ -514,13 +751,15 @@ fn_makelink()
;;
esac
ln -sfn $tgt $2 || ( echo "ln failed. Using cp instead."; cp -f $1/$2 $2 )
test -n "$use_cp" || ln -sfn "$tgt" $2 ||
{ use_cp=yes; echo "ln failed. Using cp instead."; }
test -z "$use_cp" || cp -f "$1/$2" $2
}
if test "$source_path_used" = "yes" ; then
FILES="Makefile lib/Makefile tests/Makefile tests/tests2/Makefile tests/pp/Makefile"
for f in $FILES ; do
fn_makelink $source_path $f
fn_makelink "$source_path" $f
done
fi

View File

@ -1,4 +1,182 @@
/* ----------------------------------------------------------------------- */
/* with -D C2STR: convert tccdefs.h to C-strings */
#if C2STR
#include <stdio.h>
#include <string.h>
/* replace native host macros by compile-time versions */
const char *platform_macros[] = {
"__i386__", "TCC_TARGET_I386",
"__x86_64__", "TCC_TARGET_X86_64",
"_WIN32", "TCC_TARGET_PE",
"__arm__", "TCC_TARGET_ARM",
"__ARM_EABI__", "TCC_ARM_EABI",
"__aarch64__", "TCC_TARGET_ARM64",
"__riscv", "TCC_TARGET_RISCV64",
"__APPLE__", "TCC_TARGET_MACHO",
"__FreeBSD__", "TARGETOS_FreeBSD",
"__FreeBSD_kernel__", "TARGETOS_FreeBSD_kernel",
"__OpenBSD__", "TARGETOS_OpenBSD",
"__NetBSD__", "TARGETOS_NetBSD",
"__linux__", "TARGETOS_Linux",
"__ANDROID__", "TARGETOS_ANDROID",
"__SIZEOF_POINTER__", "PTR_SIZE",
"__SIZEOF_LONG__", "LONG_SIZE",
0
};
int isid(int c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9') || c == '_';
}
int isspc(int c)
{
return (unsigned char)c <= ' ' && c != 0;
}
int main(int argc, char **argv)
{
char l[1000], l2[1000], *p, *q, *p0;
FILE *fp, *op;
int c, e, f, s, cmt, cmt_n;
const char *r;
if (argc < 3)
return 1;
fp = fopen(argv[1], "rb");
op = fopen(argv[2], "wb");
if (!fp || !op) {
fprintf(stderr, "c2str: file error\n");
return 1;
}
cmt = cmt_n = 0;
for (;;) {
p = l;
append:
if (fgets(p, sizeof l - (p - l), fp)) {
p = strchr(p, 0);
while (p > l && isspc(p[-1]))
--p;
*p = 0;
} else if (p == l)
break;
/* check for continuation */
if (p > l && p[-1] == '\\') {
p[-1] = ' ';
goto append;
}
/* count & skip leading spaces */
p = l, q = l2, f = 0;
while (*p && isspc(*p))
++p, ++f;
/* handle comments */
if (p[0] == '/' && cmt == 0) {
if (p[1] == '*')
cmt = 2;
if (p[1] == '/')
cmt = 1;
}
if (cmt) {
fprintf(op, "%s", l);
if (++cmt_n == 1)
fprintf(op, " (converted, do not edit this file)");
fprintf(op, "\n");
if (cmt == 1)
cmt = 0;
if (cmt == 2) {
p = strchr(l, 0);
if (p >= l + 2 && p[-1] == '/' && p[-2] == '*')
cmt = 0;
}
continue;
}
if (f < 4) {
do {
/* replace machine/os macros by compile-time counterparts */
for (e = f = 0; (r = platform_macros[f]); f += 2) {
c = strlen(r);
/* remove 'defined' */
//e = memcmp(p, "defined ", 8) ? 0 : 8;
if (0 == memcmp(p + e, r, c)) {
p += e + c;
q = strchr(strcpy(q, platform_macros[f + 1]), 0);
break;
}
}
if (r)
continue;
} while (!!(*q++ = *p++));
/* output as is */
fprintf(op, "%s\n", l2);
continue;
} else {
s = e = f = 0, p0 = p;
for (;;) {
c = *p++;
if (isspc(c)) {
s = 1;
continue;
}
if (c == '/' && (p[0] == '/' || p[0] == '*'))
c = 0; /* trailing comment detected */
else if (s && q > l2
&& ((isid(q[-1]) && isid(c))
// keep space after macro name
|| (q >= l2 + 2
&& l2[0] == '#'
&& l2[1] == 'd'
&& f < 2 && !e
)))
*q++ = ' ', ++f;
s = 0;
if (c == '(')
++e;
if (c == ')')
--e;
if (c == '\\' || c == '\"')
*q++ = '\\';
*q++ = c;
if (c == 0)
break;
p0 = p;
}
/* output with quotes */
fprintf(op, " \"%s\\n\"%s\n", l2, p0);
}
}
fclose(fp);
fclose(op);
return 0;
}
/* ----------------------------------------------------------------------- */
/* get some information from the host compiler for configure */
#elif 1
#include <stdio.h>
#if defined(_WIN32)
#include <fcntl.h>
#include <io.h>
int _CRT_glob = 0;
#endif
/* Define architecture */
#if defined(__i386__) || defined _M_IX86
@ -9,6 +187,8 @@
# define TRIPLET_ARCH "arm"
#elif defined(__aarch64__)
# define TRIPLET_ARCH "aarch64"
#elif defined(__riscv) && defined(__LP64__)
# define TRIPLET_ARCH "riscv64"
#else
# define TRIPLET_ARCH "unknown"
#endif
@ -18,21 +198,33 @@
# define TRIPLET_OS "linux"
#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
# define TRIPLET_OS "kfreebsd"
#elif defined _WIN32
#elif defined(__NetBSD__)
# define TRIPLET_OS "netbsd"
#elif defined(__OpenBSD__)
# define TRIPLET_OS "openbsd"
#elif defined(_WIN32)
# define TRIPLET_OS "win32"
#elif defined(__APPLE__)
# define TRIPLET_OS "darwin"
#elif !defined (__GNU__)
# define TRIPLET_OS "unknown"
#endif
#if defined __ANDROID__
# define ABI_PREFIX "android"
#else
# define ABI_PREFIX "gnu"
#endif
/* Define calling convention and ABI */
#if defined (__ARM_EABI__)
# if defined (__ARM_PCS_VFP)
# define TRIPLET_ABI "gnueabihf"
# define TRIPLET_ABI ABI_PREFIX"eabihf"
# else
# define TRIPLET_ABI "gnueabi"
# define TRIPLET_ABI ABI_PREFIX"eabi"
# endif
#else
# define TRIPLET_ABI "gnu"
# define TRIPLET_ABI ABI_PREFIX
#endif
#if defined _WIN32
@ -43,45 +235,74 @@
# define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS "-" TRIPLET_ABI
#endif
#if defined(_WIN32)
int _CRT_glob = 0;
#endif
int main(int argc, char *argv[])
{
#if defined(_WIN32)
_setmode(_fileno(stdout), _O_BINARY); /* don't translate \n to \r\n */
#endif
switch(argc == 2 ? argv[1][0] : 0) {
case 'b':
case 'b'://igendian
{
volatile unsigned foo = 0x01234567;
puts(*(unsigned char*)&foo == 0x67 ? "no" : "yes");
break;
}
#ifdef __GNUC__
case 'm':
printf("%d\n", __GNUC_MINOR__);
#if defined(__clang__)
case 'm'://inor
printf("%d\n", __clang_minor__);
break;
case 'v':
printf("%d\n", __GNUC__);
case 'v'://ersion
printf("%d\n", __clang_major__);
break;
#elif defined __TINYC__
case 'v':
#elif defined(__TINYC__)
case 'v'://ersion
puts("0");
break;
case 'm':
case 'm'://inor
printf("%d\n", __TINYC__);
break;
#elif defined(_MSC_VER)
case 'v'://ersion
puts("0");
break;
case 'm'://inor
printf("%d\n", _MSC_VER);
break;
#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
/* GNU comes last as other compilers may add 'GNU' compatibility */
case 'm'://inor
printf("%d\n", __GNUC_MINOR__);
break;
case 'v'://ersion
printf("%d\n", __GNUC__);
break;
#else
case 'm':
case 'v':
case 'm'://inor
case 'v'://ersion
puts("0");
break;
#endif
case 't':
case 't'://riplet
puts(TRIPLET);
break;
case 'c'://ompiler
#if defined(__clang__)
puts("clang");
#elif defined(__TINYC__)
puts("tcc");
#elif defined(_MSC_VER)
puts("msvc");
#elif defined(__GNUC__)
puts("gcc");
#else
puts("unknown");
#endif
break;
default:
break;
}
return 0;
}
/* ----------------------------------------------------------------------- */
#endif

1046
dwarf.h Normal file

File diff suppressed because it is too large Load Diff

94
elf.h
View File

@ -159,7 +159,12 @@ typedef struct
#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */
#define ELFOSABI_MODESTO 11 /* Novell Modesto. */
#define ELFOSABI_OPENBSD 12 /* OpenBSD. */
#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */
#define ELFOSABI_OPENVMS 13
#define ELFOSABI_NSK 14 /* Hewlett-Packard Non-Stop Kernel. */
#define ELFOSABI_AROS 15 /* Amiga Research OS. */
#define ELFOSABI_FENIXOS 16 /* FenixOS. */
#define ELFOSABI_ARM_AEABI 64 /* ARM EABI. */
#define ELFOSABI_C6000_LINUX 65 /* Linux TMS320C6000. */
#define ELFOSABI_ARM 97 /* ARM */
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
@ -262,7 +267,8 @@ typedef struct
#define EM_AARCH64 183 /* ARM AARCH64 */
#define EM_TILEPRO 188 /* Tilera TILEPro */
#define EM_TILEGX 191 /* Tilera TILE-Gx */
#define EM_NUM 192
#define EM_RISCV 243 /* RISC-V */
#define EM_NUM 253
/* If it is necessary to assign new unofficial EM_* values, please
pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
@ -381,7 +387,7 @@ typedef struct
#define SHF_MASKPROC 0xf0000000 /* Processor-specific */
#define SHF_ORDERED (1 << 30) /* Special ordering requirement
(Solaris). */
#define SHF_EXCLUDE (1 << 31) /* Section is excluded unless
#define SHF_EXCLUDE (1U << 31) /* Section is excluded unless
referenced or allocated (Solaris).*/
/* Section group handling. */
@ -819,6 +825,7 @@ typedef struct
#define DF_1_SYMINTPOSE 0x00800000 /* Object has individual interposers. */
#define DF_1_GLOBAUDIT 0x01000000 /* Global auditing required. */
#define DF_1_SINGLETON 0x02000000 /* Singleton symbols are used. */
#define DF_1_PIE 0x08000000
/* Flags for the feature selection in DT_FEATURE_1. */
#define DTF_1_PARINIT 0x00000001
@ -2330,6 +2337,7 @@ typedef Elf32_Addr Elf32_Conflict;
#define SHT_ARM_EXIDX (SHT_LOPROC + 1) /* ARM unwind section. */
#define SHT_ARM_PREEMPTMAP (SHT_LOPROC + 2) /* Preemption details. */
#define SHT_ARM_ATTRIBUTES (SHT_LOPROC + 3) /* ARM attributes section. */
#define SHT_RISCV_ATTRIBUTES 0x70000003
/* AArch64 relocs. */
@ -2494,22 +2502,30 @@ typedef Elf32_Addr Elf32_Conflict;
#define R_ARM_CALL 28
#define R_ARM_JUMP24 29
#define R_ARM_THM_JUMP24 30
#define R_ARM_BASE_ABS 31 /* Adjust by program base. */
#define R_ARM_ALU_PCREL_7_0 32
#define R_ARM_ALU_PCREL_15_8 33
#define R_ARM_ALU_PCREL_23_15 34
#define R_ARM_LDR_SBREL_11_0 35
#define R_ARM_ALU_SBREL_19_12 36
#define R_ARM_ALU_SBREL_27_20 37
#define R_ARM_TARGET1 38
#define R_ARM_SBREL31 39 /* Program base relative. */
#define R_ARM_V4BX 40
#define R_ARM_TARGET2 41
#define R_ARM_PREL31 42
#define R_ARM_MOVW_ABS_NC 43
#define R_ARM_MOVT_ABS 44
#define R_ARM_MOVW_PREL_NC 45 /* PC relative 16-bit (MOVW). */
#define R_ARM_MOVT_PREL 46 /* PC relative (MOVT). */
#define R_ARM_THM_MOVW_ABS_NC 47
#define R_ARM_THM_MOVT_ABS 48
/* Values from 49 to 89 are not yet used/handled by tcc. */
#define R_ARM_TLS_GOTDESC 90
#define R_ARM_TLS_CALL 91
#define R_ARM_TLS_DESCSEQ 92
#define R_ARM_THM_TLS_CALL 93
#define R_ARM_GOT_PREL 96
#define R_ARM_GNU_VTENTRY 100
#define R_ARM_GNU_VTINHERIT 101
#define R_ARM_THM_PC11 102 /* thumb unconditional branch */
@ -2907,6 +2923,8 @@ typedef Elf32_Addr Elf32_Conflict;
#define R_X86_64_NUM 43
/* x86-64 sh_type values. */
#define SHT_X86_64_UNWIND 0x70000001 /* Unwind information. */
/* AM33 relocations. */
#define R_MN10300_NONE 0 /* No reloc. */
@ -3233,5 +3251,75 @@ typedef Elf32_Addr Elf32_Conflict;
#define R_TILEGX_NUM 130
/* RISC-V ELF Flags */
#define EF_RISCV_RVC 0x0001
#define EF_RISCV_FLOAT_ABI 0x0006
#define EF_RISCV_FLOAT_ABI_SOFT 0x0000
#define EF_RISCV_FLOAT_ABI_SINGLE 0x0002
#define EF_RISCV_FLOAT_ABI_DOUBLE 0x0004
#define EF_RISCV_FLOAT_ABI_QUAD 0x0006
/* RISC-V relocations. */
#define R_RISCV_NONE 0
#define R_RISCV_32 1
#define R_RISCV_64 2
#define R_RISCV_RELATIVE 3
#define R_RISCV_COPY 4
#define R_RISCV_JUMP_SLOT 5
#define R_RISCV_TLS_DTPMOD32 6
#define R_RISCV_TLS_DTPMOD64 7
#define R_RISCV_TLS_DTPREL32 8
#define R_RISCV_TLS_DTPREL64 9
#define R_RISCV_TLS_TPREL32 10
#define R_RISCV_TLS_TPREL64 11
#define R_RISCV_BRANCH 16
#define R_RISCV_JAL 17
#define R_RISCV_CALL 18
#define R_RISCV_CALL_PLT 19
#define R_RISCV_GOT_HI20 20
#define R_RISCV_TLS_GOT_HI20 21
#define R_RISCV_TLS_GD_HI20 22
#define R_RISCV_PCREL_HI20 23
#define R_RISCV_PCREL_LO12_I 24
#define R_RISCV_PCREL_LO12_S 25
#define R_RISCV_HI20 26
#define R_RISCV_LO12_I 27
#define R_RISCV_LO12_S 28
#define R_RISCV_TPREL_HI20 29
#define R_RISCV_TPREL_LO12_I 30
#define R_RISCV_TPREL_LO12_S 31
#define R_RISCV_TPREL_ADD 32
#define R_RISCV_ADD8 33
#define R_RISCV_ADD16 34
#define R_RISCV_ADD32 35
#define R_RISCV_ADD64 36
#define R_RISCV_SUB8 37
#define R_RISCV_SUB16 38
#define R_RISCV_SUB32 39
#define R_RISCV_SUB64 40
#define R_RISCV_GNU_VTINHERIT 41
#define R_RISCV_GNU_VTENTRY 42
#define R_RISCV_ALIGN 43
#define R_RISCV_RVC_BRANCH 44
#define R_RISCV_RVC_JUMP 45
#define R_RISCV_RVC_LUI 46
#define R_RISCV_GPREL_I 47
#define R_RISCV_GPREL_S 48
#define R_RISCV_TPREL_I 49
#define R_RISCV_TPREL_S 50
#define R_RISCV_RELAX 51
#define R_RISCV_SUB6 52
#define R_RISCV_SET6 53
#define R_RISCV_SET8 54
#define R_RISCV_SET16 55
#define R_RISCV_SET32 56
#define R_RISCV_32_PCREL 57
#define R_RISCV_IRELATIVE 58
#define R_RISCV_PLT32 59
#define R_RISCV_SET_ULEB128 60
#define R_RISCV_SUB_ULEB128 61
#define R_RISCV_NUM 62
#endif /* elf.h */

View File

@ -1,6 +1,6 @@
#include <tcclib.h>
int fib(n)
int fib(int n)
{
if (n <= 2)
return 1;
@ -18,6 +18,6 @@ int main(int argc, char **argv)
}
n = atoi(argv[1]);
printf("fib(%d) = %d\n", n, fib(n, 2));
printf("fib(%d) = %d\n", n, fib(n));
return 0;
}

View File

@ -19,6 +19,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define USING_GLOBALS
#include "tcc.h"
#define MAX_OPERANDS 3
@ -39,6 +40,8 @@
#define OPC_ARITH 0x30 /* arithmetic opcodes */
#define OPC_FARITH 0x40 /* FPU arithmetic opcodes */
#define OPC_TEST 0x50 /* test opcodes */
#define OPC_0F01 0x60 /* 0x0f01XX (group 7, XX is 2nd opcode,
no operands and unstructured mod/rm) */
#define OPCT_IS(v,i) (((v) & OPCT_MASK) == (i))
#define OPC_0F 0x100 /* Is secondary map (0x0f prefix) */
@ -133,17 +136,19 @@ enum {
# define OP_EA32 0
#endif
#define OP_EA 0x40000000
#define OP_EA 0x40000000u
#define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64)
#ifdef TCC_TARGET_X86_64
# define TREG_XAX TREG_RAX
# define TREG_XCX TREG_RCX
# define TREG_XDX TREG_RDX
# define TOK_ASM_xax TOK_ASM_rax
#else
# define TREG_XAX TREG_EAX
# define TREG_XCX TREG_ECX
# define TREG_XDX TREG_EDX
# define TOK_ASM_xax TOK_ASM_eax
#endif
typedef struct ASMInstr {
@ -484,18 +489,11 @@ ST_FUNC void gen_expr32(ExprValue *pe)
if (pe->pcrel)
/* If PC-relative, always set VT_SYM, even without symbol,
so as to force a relocation to be emitted. */
gen_addrpc32(VT_SYM, pe->sym, pe->v);
gen_addrpc32(VT_SYM, pe->sym, pe->v + (ind + 4));
else
gen_addr32(pe->sym ? VT_SYM : 0, pe->sym, pe->v);
}
#ifdef TCC_TARGET_X86_64
ST_FUNC void gen_expr64(ExprValue *pe)
{
gen_addr64(pe->sym ? VT_SYM : 0, pe->sym, pe->v);
}
#endif
/* XXX: unify with C code output ? */
static void gen_disp32(ExprValue *pe)
{
@ -512,7 +510,13 @@ static void gen_disp32(ExprValue *pe)
sym->type.t = VT_FUNC;
sym->type.ref = NULL;
}
#ifdef TCC_TARGET_X86_64
greloca(cur_text_section, sym, ind, R_X86_64_PLT32, pe->v - 4);
gen_le32(0);
#else
gen_addrpc32(VT_SYM, sym, pe->v);
#endif
}
}
@ -637,10 +641,12 @@ static void asm_rex(int width64, Operand *ops, int nb_ops, int *op_type,
}
#endif
static void maybe_print_stats (void)
{
static int already = 1;
if (!already)
static int already;
if (0 && !already)
/* print stats about opcodes */
{
const struct ASMInstr *pa;
@ -679,7 +685,7 @@ static void maybe_print_stats (void)
ST_FUNC void asm_opcode(TCCState *s1, int opcode)
{
const ASMInstr *pa;
int i, modrm_index, modreg_index, reg, v, op1, seg_prefix, pc;
int i, modrm_index, modreg_index, reg, v, op1, seg_prefix, pc, p;
int nb_ops, s;
Operand ops[MAX_OPERANDS], *pop;
int op_type[3]; /* decoded op type */
@ -709,7 +715,7 @@ ST_FUNC void asm_opcode(TCCState *s1, int opcode)
}
parse_operand(s1, pop);
if (tok == ':') {
if (pop->type != OP_SEG || seg_prefix)
if (!(pop->type & OP_SEG) || seg_prefix)
tcc_error("incorrect prefix");
seg_prefix = segment_prefixes[pop->reg];
next();
@ -830,6 +836,7 @@ again:
goto next;
alltypes |= ops[i].type;
}
(void)alltypes; /* maybe unused */
/* all is matching ! */
break;
next: ;
@ -887,30 +894,6 @@ again:
}
}
#ifdef TCC_TARGET_X86_64
/* Generate addr32 prefix if needed */
for(i = 0; i < nb_ops; i++) {
if (ops[i].type & OP_EA32) {
g(0x67);
break;
}
}
#endif
/* generate data16 prefix if needed */
p66 = 0;
if (s == 1)
p66 = 1;
else {
/* accepting mmx+sse in all operands --> needs 0x66 to
switch to sse mode. Accepting only sse in an operand --> is
already SSE insn and needs 0x66/f2/f3 handling. */
for (i = 0; i < nb_ops; i++)
if ((op_type[i] & (OP_MMX | OP_SSE)) == (OP_MMX | OP_SSE)
&& ops[i].type & OP_SSE)
p66 = 1;
}
if (p66)
g(0x66);
#ifdef TCC_TARGET_X86_64
rex64 = 0;
if (pa->instr_type & OPC_48)
@ -943,8 +926,45 @@ again:
g(0x9b);
if (seg_prefix)
g(seg_prefix);
#ifdef TCC_TARGET_X86_64
/* Generate addr32 prefix if needed */
for(i = 0; i < nb_ops; i++) {
if (ops[i].type & OP_EA32) {
g(0x67);
break;
}
}
#endif
/* generate data16 prefix if needed */
p66 = 0;
if (s == 1)
p66 = 1;
else {
/* accepting mmx+sse in all operands --> needs 0x66 to
switch to sse mode. Accepting only sse in an operand --> is
already SSE insn and needs 0x66/f2/f3 handling. */
for (i = 0; i < nb_ops; i++)
if ((op_type[i] & (OP_MMX | OP_SSE)) == (OP_MMX | OP_SSE)
&& ops[i].type & OP_SSE)
p66 = 1;
}
if (p66)
g(0x66);
v = pa->opcode;
p = v >> 8; /* possibly prefix byte(s) */
switch (p) {
case 0: break; /* no prefix */
case 0x48: break; /* REX, handled elsewhere */
case 0x66:
case 0x67:
case 0xf2:
case 0xf3: v = v & 0xff; g(p); break;
case 0xd4: case 0xd5: break; /* aam and aad, not prefix, but hardcoded immediate argument "10" */
case 0xd8: case 0xd9: case 0xda: case 0xdb: /* x87, no normal prefix */
case 0xdc: case 0xdd: case 0xde: case 0xdf: break;
default: tcc_error("bad prefix 0x%2x in opcode table", p); break;
}
if (pa->instr_type & OPC_0F)
v = ((v & ~0xff) << 8) | 0x0f00 | (v & 0xff);
if ((v == 0x69 || v == 0x6b) && nb_ops == 2) {
@ -977,11 +997,20 @@ again:
if (pa->instr_type & OPC_MODRM) {
if (!nb_ops) {
/* A modrm opcode without operands is a special case (e.g. mfence).
It has a group and acts as if there's an register operand 0
(ax). */
It has a group and acts as if there's an register operand 0 */
i = 0;
ops[i].type = OP_REG;
ops[i].reg = 0;
#ifdef TCC_TARGET_X86_64
if (pa->sym == TOK_ASM_endbr64)
ops[i].reg = 2; // dx
else if (pa->sym >= TOK_ASM_lfence && pa->sym <= TOK_ASM_sfence)
ops[i].reg = 0; // ax
#else
if (pa->sym == TOK_ASM_endbr32)
ops[i].reg = 3; // bx
#endif
else
tcc_error("bad MODR/M opcode without operands");
goto modrm_found;
}
/* first look for an ea operand */
@ -1055,6 +1084,8 @@ again:
}
if (OPCT_IS(pa->instr_type, OPC_TEST))
v += test_bits[opcode - pa->sym];
else if (OPCT_IS(pa->instr_type, OPC_0F01))
v |= 0x0f0100;
op1 = v >> 16;
if (op1)
g(op1);
@ -1195,19 +1226,20 @@ static const char *skip_constraint_modifiers(const char *p)
return p;
}
/* If T (a token) is of the form "%reg" returns the register
number and type, otherwise return -1. */
/* If t (a token) is of the form "%reg" or "reg" return the register number and
type, otherwise return -1. With GCC the % is optional, too. */
ST_FUNC int asm_parse_regvar (int t)
{
const char *s;
Operand op;
if (t < TOK_IDENT)
if (t < TOK_IDENT || (t & SYM_FIELD))
return -1;
s = table_ident[t - TOK_IDENT]->str;
if (s[0] != '%')
return -1;
t = tok_alloc(s+1, strlen(s)-1)->tok;
if (s[0] == '%')
++s;
t = tok_alloc_const(s);
unget_tok(t);
/* Internally the % prefix is required. */
unget_tok('%');
parse_operand(tcc_state, &op);
/* Accept only integer regs for now. */
@ -1315,7 +1347,6 @@ ST_FUNC void asm_compute_constraints(ASMOperand *operands,
if (is_reg_allocated(op->reg))
tcc_error("asm regvar requests register that's taken already");
reg = op->reg;
goto reg_found;
}
try_next:
c = *str++;
@ -1358,12 +1389,17 @@ ST_FUNC void asm_compute_constraints(ASMOperand *operands,
case 'D':
reg = 7;
alloc_reg:
if (op->reg >= 0 && reg != op->reg)
goto try_next;
if (is_reg_allocated(reg))
goto try_next;
goto reg_found;
case 'q':
/* eax, ebx, ecx or edx */
for(reg = 0; reg < 4; reg++) {
if (op->reg >= 0) {
if ((reg = op->reg) < 4)
goto reg_found;
} else for(reg = 0; reg < 4; reg++) {
if (!is_reg_allocated(reg))
goto reg_found;
}
@ -1372,7 +1408,9 @@ ST_FUNC void asm_compute_constraints(ASMOperand *operands,
case 'R':
case 'p': /* A general address, for x86(64) any register is acceptable*/
/* any general register */
for(reg = 0; reg < 8; reg++) {
if ((reg = op->reg) >= 0)
goto reg_found;
else for(reg = 0; reg < NB_ASM_REGS; reg++) {
if (!is_reg_allocated(reg))
goto reg_found;
}
@ -1407,7 +1445,7 @@ ST_FUNC void asm_compute_constraints(ASMOperand *operands,
if (j < nb_outputs || c == 'm') {
if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) {
/* any general register */
for(reg = 0; reg < 8; reg++) {
for(reg = 0; reg < NB_ASM_REGS; reg++) {
if (!(regs_allocated[reg] & REG_IN_MASK))
goto reg_found1;
}
@ -1440,7 +1478,7 @@ ST_FUNC void asm_compute_constraints(ASMOperand *operands,
if (op->reg >= 0 &&
(op->vt->r & VT_VALMASK) == VT_LLOCAL &&
!op->is_memory) {
for(reg = 0; reg < 8; reg++) {
for(reg = 0; reg < NB_ASM_REGS; reg++) {
if (!(regs_allocated[reg] & REG_OUT_MASK))
goto reg_found2;
}
@ -1472,7 +1510,6 @@ ST_FUNC void subst_asm_operand(CString *add_str,
SValue *sv, int modifier)
{
int r, reg, size, val;
char buf[64];
r = sv->r;
if ((r & VT_VALMASK) == VT_CONST) {
@ -1487,8 +1524,10 @@ ST_FUNC void subst_asm_operand(CString *add_str,
in the C symbol table when later looking up
this name. So enter them now into the asm label
list when we still know the symbol. */
get_asm_sym(tok_alloc(name, strlen(name))->tok, sv->sym);
get_asm_sym(tok_alloc_const(name), sv->sym);
}
if (tcc_state->leading_underscore)
cstr_ccat(add_str, '_');
cstr_cat(add_str, name, -1);
if ((uint32_t)sv->c.i == 0)
goto no_offset;
@ -1497,37 +1536,24 @@ ST_FUNC void subst_asm_operand(CString *add_str,
val = sv->c.i;
if (modifier == 'n')
val = -val;
snprintf(buf, sizeof(buf), "%d", (int)sv->c.i);
cstr_cat(add_str, buf, -1);
cstr_printf(add_str, "%d", (int)sv->c.i);
no_offset:;
#ifdef TCC_TARGET_X86_64
if (r & VT_LVAL)
cstr_cat(add_str, "(%rip)", -1);
#endif
} else if ((r & VT_VALMASK) == VT_LOCAL) {
#ifdef TCC_TARGET_X86_64
snprintf(buf, sizeof(buf), "%d(%%rbp)", (int)sv->c.i);
#else
snprintf(buf, sizeof(buf), "%d(%%ebp)", (int)sv->c.i);
#endif
cstr_cat(add_str, buf, -1);
cstr_printf(add_str, "%d(%%%s)", (int)sv->c.i, get_tok_str(TOK_ASM_xax + 5, NULL));
} else if (r & VT_LVAL) {
reg = r & VT_VALMASK;
if (reg >= VT_CONST)
tcc_error("internal compiler error");
snprintf(buf, sizeof(buf), "(%%%s)",
#ifdef TCC_TARGET_X86_64
get_tok_str(TOK_ASM_rax + reg, NULL)
#else
get_tok_str(TOK_ASM_eax + reg, NULL)
#endif
);
cstr_cat(add_str, buf, -1);
tcc_internal_error("");
cstr_printf(add_str, "(%%%s)", get_tok_str(TOK_ASM_xax + reg, NULL));
} else {
/* register case */
reg = r & VT_VALMASK;
if (reg >= VT_CONST)
tcc_error("internal compiler error");
tcc_internal_error("");
/* choose register operand size */
if ((sv->type.t & VT_BTYPE) == VT_BYTE ||
@ -1563,6 +1589,10 @@ ST_FUNC void subst_asm_operand(CString *add_str,
#endif
}
if (reg >= 8) {
cstr_printf(add_str, "%%r%d%c", reg, (size == 1) ? 'b' : ((size == 2) ? 'w' : ((size == 4) ? 'd' : ' ')));
return;
}
switch(size) {
case -1:
reg = TOK_ASM_ah + reg;
@ -1582,8 +1612,7 @@ ST_FUNC void subst_asm_operand(CString *add_str,
break;
#endif
}
snprintf(buf, sizeof(buf), "%%%s", get_tok_str(reg, NULL));
cstr_cat(add_str, buf, -1);
cstr_printf(add_str, "%%%s", get_tok_str(reg, NULL));
}
}
@ -1601,12 +1630,12 @@ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands,
call-preserved registers, but currently it doesn't matter. */
#ifdef TCC_TARGET_X86_64
#ifdef TCC_TARGET_PE
static uint8_t reg_saved[] = { 3, 6, 7, 12, 13, 14, 15 };
static const uint8_t reg_saved[] = { 3, 6, 7, 12, 13, 14, 15 };
#else
static uint8_t reg_saved[] = { 3, 12, 13, 14, 15 };
static const uint8_t reg_saved[] = { 3, 12, 13, 14, 15 };
#endif
#else
static uint8_t reg_saved[] = { 3, 6, 7 };
static const uint8_t reg_saved[] = { 3, 6, 7 };
#endif
/* mark all used registers */
@ -1695,7 +1724,6 @@ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands,
ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str)
{
int reg;
TokenSym *ts;
#ifdef TCC_TARGET_X86_64
unsigned int type;
#endif
@ -1704,8 +1732,7 @@ ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str)
!strcmp(str, "cc") ||
!strcmp(str, "flags"))
return;
ts = tok_alloc(str, strlen(str));
reg = ts->tok;
reg = tok_alloc_const(str);
if (reg >= TOK_ASM_eax && reg <= TOK_ASM_edi) {
reg -= TOK_ASM_eax;
} else if (reg >= TOK_ASM_ax && reg <= TOK_ASM_di) {

View File

@ -37,6 +37,9 @@
DEF_ASM_OP0(pause, 0xf390)
DEF_ASM_OP0(xlat, 0xd7)
/* Control-Flow Enforcement */
DEF_ASM_OP0L(endbr32, 0xf30f1e, 7, OPC_MODRM)
/* strings */
ALT(DEF_ASM_OP0L(cmpsb, 0xa6, 0, OPC_BWLX))
ALT(DEF_ASM_OP0L(scmpb, 0xa6, 0, OPC_BWLX))
@ -73,6 +76,11 @@ ALT(DEF_ASM_OP2(btrw, 0x0fba, 6, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA
ALT(DEF_ASM_OP2(btcw, 0x0fbb, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA))
ALT(DEF_ASM_OP2(btcw, 0x0fba, 7, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA))
ALT(DEF_ASM_OP2(popcntw, 0xf30fb8, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW))
ALT(DEF_ASM_OP2(tzcntw, 0xf30fbc, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW))
ALT(DEF_ASM_OP2(lzcntw, 0xf30fbd, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW))
/* prefixes */
DEF_ASM_OP0(wait, 0x9b)
DEF_ASM_OP0(fwait, 0x9b)
@ -447,6 +455,8 @@ ALT(DEF_ASM_OP2(psrlq, 0x0f73, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE ))
DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE )
/* sse */
DEF_ASM_OP1(ldmxcsr, 0x0fae, 2, OPC_MODRM, OPT_EA)
DEF_ASM_OP1(stmxcsr, 0x0fae, 3, OPC_MODRM, OPT_EA)
DEF_ASM_OP2(movups, 0x0f10, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE )
ALT(DEF_ASM_OP2(movups, 0x0f11, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 ))
DEF_ASM_OP2(movaps, 0x0f28, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE )

File diff suppressed because it is too large Load Diff

View File

@ -15,16 +15,21 @@
#define ELF_START_ADDR 0x08048000
#define ELF_PAGE_SIZE 0x1000
#if defined CONFIG_TCC_PIC
#define PCRELATIVE_DLLPLT 1
#else
#define PCRELATIVE_DLLPLT 0
#define RELOCATE_DLLPLT 0
#endif
#define RELOCATE_DLLPLT 1
#else /* !TARGET_DEFS_ONLY */
#include "tcc.h"
#ifdef NEED_RELOC_TYPE
/* Returns 1 for a code relocation, 0 for a data relocation. For unknown
relocations, returns -1. */
int code_reloc (int reloc_type)
ST_FUNC int code_reloc (int reloc_type)
{
switch (reloc_type) {
case R_386_RELATIVE:
@ -36,6 +41,10 @@ int code_reloc (int reloc_type)
case R_386_GOT32X:
case R_386_GLOB_DAT:
case R_386_COPY:
case R_386_TLS_GD:
case R_386_TLS_LDM:
case R_386_TLS_LDO_32:
case R_386_TLS_LE:
return 0;
case R_386_PC16:
@ -44,15 +53,13 @@ int code_reloc (int reloc_type)
case R_386_JMP_SLOT:
return 1;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
/* Returns an enumerator to describe whether and when the relocation needs a
GOT and/or PLT entry to be created. See tcc.h for a description of the
different values. */
int gotplt_entry_type (int reloc_type)
ST_FUNC int gotplt_entry_type (int reloc_type)
{
switch (reloc_type) {
case R_386_RELATIVE:
@ -79,13 +86,16 @@ int gotplt_entry_type (int reloc_type)
case R_386_GOT32:
case R_386_GOT32X:
case R_386_PLT32:
case R_386_TLS_GD:
case R_386_TLS_LDM:
case R_386_TLS_LDO_32:
case R_386_TLS_LE:
return ALWAYS_GOTPLT_ENTRY;
}
tcc_error ("Unknown relocation type: %d", reloc_type);
return -1;
}
#ifdef NEED_BUILD_GOT
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
{
Section *plt = s1->plt;
@ -94,7 +104,7 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
unsigned plt_offset, relofs;
/* on i386 if we build a DLL, we add a %ebx offset */
if (s1->output_type == TCC_OUTPUT_DLL)
if (s1->output_type & TCC_OUTPUT_DYN)
modrm = 0xa3;
else
modrm = 0x25;
@ -116,7 +126,7 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
/* The PLT slot refers to the relocation entry it needs via offset.
The reloc entry is created below, so its offset is the current
data_offset */
relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
relofs = s1->plt->reloc ? s1->plt->reloc->data_offset : 0;
/* Jump to GOT entry where ld.so initially put the address of ip + 4 */
p = section_ptr_add(plt, 16);
@ -124,7 +134,7 @@ ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_
p[1] = modrm;
write32le(p + 2, got_offset);
p[6] = 0x68; /* push $xxx */
write32le(p + 7, relofs);
write32le(p + 7, relofs - sizeof (ElfW_Rel));
p[11] = 0xe9; /* jmp plt_start */
write32le(p + 12, -(plt->data_offset));
return plt_offset;
@ -142,7 +152,7 @@ ST_FUNC void relocate_plt(TCCState *s1)
p = s1->plt->data;
p_end = p + s1->plt->data_offset;
if (p < p_end) {
if (!(s1->output_type & TCC_OUTPUT_DYN) && p < p_end) {
add32le(p + 2, s1->got->sh_addr);
add32le(p + 8, s1->got->sh_addr);
p += 16;
@ -151,16 +161,21 @@ ST_FUNC void relocate_plt(TCCState *s1)
p += 16;
}
}
if (s1->plt->reloc) {
ElfW_Rel *rel;
int x = s1->plt->sh_addr + 16 + 6;
p = s1->got->data;
for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
write32le(p + rel->r_offset, x);
x += 16;
}
static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
void relocate_init(Section *sr)
{
qrel = (ElfW_Rel *) sr->data;
}
}
#endif
#endif
void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
{
int sym_index, esym_index;
@ -168,8 +183,8 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
switch (type) {
case R_386_32:
if (s1->output_type == TCC_OUTPUT_DLL) {
esym_index = s1->sym_attrs[sym_index].dyn_index;
if (s1->output_type & TCC_OUTPUT_DYN) {
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
qrel->r_offset = rel->r_offset;
if (esym_index) {
qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
@ -185,7 +200,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
case R_386_PC32:
if (s1->output_type == TCC_OUTPUT_DLL) {
/* DLL relocation */
esym_index = s1->sym_attrs[sym_index].dyn_index;
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
if (esym_index) {
qrel->r_offset = rel->r_offset;
qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
@ -211,12 +226,12 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
case R_386_GOT32:
case R_386_GOT32X:
/* we load the got offset */
add32le(ptr, s1->sym_attrs[sym_index].got_offset);
add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
return;
case R_386_16:
if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) {
output_file:
tcc_error("can only produce 16-bit binary files");
tcc_error_noabort("can only produce 16-bit binary files");
}
write16le(ptr, read16le(ptr) + val);
return;
@ -237,6 +252,104 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t
(to remove noise of default case). Is this true?
*/
return;
case R_386_TLS_GD:
{
static const unsigned char expect[] = {
/* lea 0(,%ebx,1),%eax */
0x8d, 0x04, 0x1d, 0x00, 0x00, 0x00, 0x00,
/* call __tls_get_addr@PLT */
0xe8, 0xfc, 0xff, 0xff, 0xff };
static const unsigned char replace[] = {
/* mov %gs:0,%eax */
0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
/* sub 0,%eax */
0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 };
if (memcmp (ptr-3, expect, sizeof(expect)) == 0) {
ElfW(Sym) *sym;
Section *sec;
int32_t x;
memcpy(ptr-3, replace, sizeof(replace));
rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
sec = s1->sections[sym->st_shndx];
x = sym->st_value - sec->sh_addr - sec->data_offset;
add32le(ptr + 5, -x);
}
else
tcc_error_noabort("unexpected R_386_TLS_GD pattern");
}
return;
case R_386_TLS_LDM:
{
static const unsigned char expect[] = {
/* lea 0(%ebx),%eax */
0x8d, 0x83, 0x00, 0x00, 0x00, 0x00,
/* call __tls_get_addr@PLT */
0xe8, 0xfc, 0xff, 0xff, 0xff };
static const unsigned char replace[] = {
/* mov %gs:0,%eax */
0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
/* nop */
0x90,
/* lea 0(%esi,%eiz,1),%esi */
0x8d, 0x74, 0x26, 0x00 };
if (memcmp (ptr-2, expect, sizeof(expect)) == 0) {
memcpy(ptr-2, replace, sizeof(replace));
rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
}
else
tcc_error_noabort("unexpected R_386_TLS_LDM pattern");
}
return;
case R_386_TLS_LDO_32:
{
ElfW(Sym) *sym;
Section *sec;
int32_t x;
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
sec = s1->sections[sym->st_shndx];
x = val - sec->sh_addr - sec->data_offset;
add32le(ptr, x);
}
return;
case R_386_TLS_LE:
{
ElfW(Sym) *sym;
Section *sec;
int32_t x;
addr_t tls_start = 0, tls_end = 0, tls_align = 1;
int i;
sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
sec = s1->sections[sym->st_shndx];
for (i = 1; i < s1->nb_sections; i++) {
Section *s = s1->sections[i];
if (s->sh_flags & SHF_TLS && s->sh_size) {
if (!tls_start || s->sh_addr < tls_start)
tls_start = s->sh_addr;
if (s->sh_addr + s->sh_size > tls_end)
tls_end = s->sh_addr + s->sh_size;
if (s->sh_addralign > tls_align)
tls_align = s->sh_addralign;
}
}
if (tls_end > tls_start) {
addr_t tls_size = tls_end - tls_start;
addr_t aligned_size = (tls_size + tls_align - 1) & ~(tls_align - 1);
x = val - (tls_start + aligned_size);
} else {
x = val - sec->sh_addr - sec->data_offset;
}
add32le(ptr, x);
}
return;
case R_386_NONE:
return;
default:
fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
type, (unsigned)addr, ptr, (unsigned)val);

View File

@ -1,6 +1,82 @@
/* ------------------------------------------------------------------ */
/* WARNING: relative order of tokens is important. */
#define DEF_BWL(x) \
DEF(TOK_ASM_ ## x ## b, #x "b") \
DEF(TOK_ASM_ ## x ## w, #x "w") \
DEF(TOK_ASM_ ## x ## l, #x "l") \
DEF(TOK_ASM_ ## x, #x)
#define DEF_WL(x) \
DEF(TOK_ASM_ ## x ## w, #x "w") \
DEF(TOK_ASM_ ## x ## l, #x "l") \
DEF(TOK_ASM_ ## x, #x)
#ifdef TCC_TARGET_X86_64
# define DEF_BWLQ(x) \
DEF(TOK_ASM_ ## x ## b, #x "b") \
DEF(TOK_ASM_ ## x ## w, #x "w") \
DEF(TOK_ASM_ ## x ## l, #x "l") \
DEF(TOK_ASM_ ## x ## q, #x "q") \
DEF(TOK_ASM_ ## x, #x)
# define DEF_WLQ(x) \
DEF(TOK_ASM_ ## x ## w, #x "w") \
DEF(TOK_ASM_ ## x ## l, #x "l") \
DEF(TOK_ASM_ ## x ## q, #x "q") \
DEF(TOK_ASM_ ## x, #x)
# define DEF_BWLX DEF_BWLQ
# define DEF_WLX DEF_WLQ
/* number of sizes + 1 */
# define NBWLX 5
#else
# define DEF_BWLX DEF_BWL
# define DEF_WLX DEF_WL
/* number of sizes + 1 */
# define NBWLX 4
#endif
#define DEF_FP1(x) \
DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
#define DEF_FP(x) \
DEF(TOK_ASM_ ## f ## x, "f" #x ) \
DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
DEF_FP1(x)
#define DEF_ASMTEST(x,suffix) \
DEF_ASM(x ## o ## suffix) \
DEF_ASM(x ## no ## suffix) \
DEF_ASM(x ## b ## suffix) \
DEF_ASM(x ## c ## suffix) \
DEF_ASM(x ## nae ## suffix) \
DEF_ASM(x ## nb ## suffix) \
DEF_ASM(x ## nc ## suffix) \
DEF_ASM(x ## ae ## suffix) \
DEF_ASM(x ## e ## suffix) \
DEF_ASM(x ## z ## suffix) \
DEF_ASM(x ## ne ## suffix) \
DEF_ASM(x ## nz ## suffix) \
DEF_ASM(x ## be ## suffix) \
DEF_ASM(x ## na ## suffix) \
DEF_ASM(x ## nbe ## suffix) \
DEF_ASM(x ## a ## suffix) \
DEF_ASM(x ## s ## suffix) \
DEF_ASM(x ## ns ## suffix) \
DEF_ASM(x ## p ## suffix) \
DEF_ASM(x ## pe ## suffix) \
DEF_ASM(x ## np ## suffix) \
DEF_ASM(x ## po ## suffix) \
DEF_ASM(x ## l ## suffix) \
DEF_ASM(x ## nge ## suffix) \
DEF_ASM(x ## nl ## suffix) \
DEF_ASM(x ## ge ## suffix) \
DEF_ASM(x ## le ## suffix) \
DEF_ASM(x ## ng ## suffix) \
DEF_ASM(x ## nle ## suffix) \
DEF_ASM(x ## g ## suffix)
/* ------------------------------------------------------------------ */
/* register */
DEF_ASM(al)
DEF_ASM(cl)
@ -192,6 +268,9 @@
DEF_WLX(bts)
DEF_WLX(btr)
DEF_WLX(btc)
DEF_WLX(popcnt)
DEF_WLX(tzcnt)
DEF_WLX(lzcnt)
DEF_WLX(lar)
DEF_WLX(lsl)

View File

@ -38,19 +38,37 @@
#define LDBL_MAX_EXP 16384
#define LDBL_MAX 1.18973149535723176502e+4932L
#define LDBL_MAX_10_EXP 4932
#define DECIMAL_DIG 21
#elif defined __aarch64__ || defined __riscv
/*
* Use values from:
* gcc -dM -E -xc /dev/null | grep LDBL | sed -e "s/__//g"
*/
#define LDBL_MANT_DIG 113
#define LDBL_DIG 33
#define LDBL_EPSILON 1.92592994438723585305597794258492732e-34L
#define LDBL_MIN_EXP (-16381)
#define LDBL_MIN 3.36210314311209350626267781732175260e-4932L
#define LDBL_MIN_10_EXP (-4931)
#define LDBL_MAX_EXP 16384
#define LDBL_MAX 1.18973149535723176508575932662800702e+4932L
#define LDBL_MAX_10_EXP 4932
#define DECIMAL_DIG 36
#else
/* same as IEEE double */
#define LDBL_MANT_DIG 53
#define LDBL_DIG 15
#define LDBL_EPSILON 2.2204460492503131e-16
#define LDBL_EPSILON 2.2204460492503131e-16L
#define LDBL_MIN_EXP (-1021)
#define LDBL_MIN 2.2250738585072014e-308
#define LDBL_MIN 2.2250738585072014e-308L
#define LDBL_MIN_10_EXP (-307)
#define LDBL_MAX_EXP 1024
#define LDBL_MAX 1.7976931348623157e+308
#define LDBL_MAX 1.7976931348623157e+308L
#define LDBL_MAX_10_EXP 308
#define DECIMAL_DIG 17
#endif

16
include/stdalign.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef _STDALIGN_H
#define _STDALIGN_H
#if __STDC_VERSION__ < 201112L && (defined(__GNUC__) || defined(__TINYC__))
# define _Alignas(t) __attribute__((__aligned__(t)))
# define _Alignof(t) __alignof__(t)
#endif
#define alignas _Alignas
#define alignof _Alignof
#define __alignas_is_defined 1
#define __alignof_is_defined 1
#endif /* _STDALIGN_H */

View File

@ -1,76 +1,11 @@
#ifndef _STDARG_H
#define _STDARG_H
#ifdef __x86_64__
#ifndef _WIN64
//This should be in sync with the declaration on our lib/libtcc1.c
/* GCC compatible definition of va_list. */
typedef struct {
unsigned int gp_offset;
unsigned int fp_offset;
union {
unsigned int overflow_offset;
char *overflow_arg_area;
};
char *reg_save_area;
} __va_list_struct;
typedef __va_list_struct va_list[1];
void __va_start(__va_list_struct *ap, void *fp);
void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align);
#define va_start(ap, last) __va_start(ap, __builtin_frame_address(0))
#define va_arg(ap, type) \
(*(type *)(__va_arg(ap, __builtin_va_arg_types(type), sizeof(type), __alignof__(type))))
#define va_copy(dest, src) (*(dest) = *(src))
#define va_end(ap)
/* avoid conflicting definition for va_list on Macs. */
#define _VA_LIST_T
#else /* _WIN64 */
typedef char *va_list;
#define va_start(ap,last) __builtin_va_start(ap,last)
#define va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \
? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8))
#define va_copy(dest, src) ((dest) = (src))
#define va_end(ap)
#endif
#elif __arm__
typedef char *va_list;
#define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x)
#define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \
& ~(_tcc_alignof(type) - 1))
#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)
#define va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \
&~3), *(type *)(ap - ((sizeof(type)+3)&~3)))
#define va_copy(dest, src) (dest) = (src)
#define va_end(ap)
#elif defined(__aarch64__)
typedef struct {
void *__stack;
void *__gr_top;
void *__vr_top;
int __gr_offs;
int __vr_offs;
} va_list;
#define va_start(ap, last) __va_start(ap, last)
#define va_arg(ap, type) __va_arg(ap, type)
#define va_end(ap)
#define va_copy(dest, src) ((dest) = (src))
#else /* __i386__ */
typedef char *va_list;
/* only correct for i386 */
#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)
#define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3)))
#define va_copy(dest, src) (dest) = (src)
#define va_end(ap)
#endif
typedef __builtin_va_list va_list;
#define va_start __builtin_va_start
#define va_arg __builtin_va_arg
#define va_copy __builtin_va_copy
#define va_end __builtin_va_end
/* fix a buggy dependency on GCC in libio.h */
typedef va_list __gnuc_va_list;

171
include/stdatomic.h Normal file
View File

@ -0,0 +1,171 @@
/* This file is derived from clang's stdatomic.h */
/*===---- stdatomic.h - Standard header for atomic types and operations -----===
*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*===-----------------------------------------------------------------------===
*/
#ifndef _STDATOMIC_H
#define _STDATOMIC_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#define __ATOMIC_RELAXED 0
#define __ATOMIC_CONSUME 1
#define __ATOMIC_ACQUIRE 2
#define __ATOMIC_RELEASE 3
#define __ATOMIC_ACQ_REL 4
#define __ATOMIC_SEQ_CST 5
/* Memory ordering */
typedef enum {
memory_order_relaxed = __ATOMIC_RELAXED,
memory_order_consume = __ATOMIC_CONSUME,
memory_order_acquire = __ATOMIC_ACQUIRE,
memory_order_release = __ATOMIC_RELEASE,
memory_order_acq_rel = __ATOMIC_ACQ_REL,
memory_order_seq_cst = __ATOMIC_SEQ_CST,
} memory_order;
/* Atomic typedefs */
typedef _Atomic(_Bool) atomic_bool;
typedef _Atomic(char) atomic_char;
typedef _Atomic(signed char) atomic_schar;
typedef _Atomic(unsigned char) atomic_uchar;
typedef _Atomic(short) atomic_short;
typedef _Atomic(unsigned short) atomic_ushort;
typedef _Atomic(int) atomic_int;
typedef _Atomic(unsigned int) atomic_uint;
typedef _Atomic(long) atomic_long;
typedef _Atomic(unsigned long) atomic_ulong;
typedef _Atomic(long long) atomic_llong;
typedef _Atomic(unsigned long long) atomic_ullong;
typedef _Atomic(uint_least16_t) atomic_char16_t;
typedef _Atomic(uint_least32_t) atomic_char32_t;
typedef _Atomic(wchar_t) atomic_wchar_t;
typedef _Atomic(int_least8_t) atomic_int_least8_t;
typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
typedef _Atomic(int_least16_t) atomic_int_least16_t;
typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
typedef _Atomic(int_least32_t) atomic_int_least32_t;
typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
typedef _Atomic(int_least64_t) atomic_int_least64_t;
typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
typedef _Atomic(intptr_t) atomic_intptr_t;
typedef _Atomic(uintptr_t) atomic_uintptr_t;
typedef _Atomic(size_t) atomic_size_t;
typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
typedef _Atomic(intmax_t) atomic_intmax_t;
typedef _Atomic(uintmax_t) atomic_uintmax_t;
/* Atomic flag */
typedef struct {
atomic_bool value;
} atomic_flag;
#define ATOMIC_FLAG_INIT {0}
#define ATOMIC_VAR_INIT(value) (value)
/* Generic routines */
#define atomic_init(object, desired) \
atomic_store_explicit(object, desired, __ATOMIC_RELAXED)
#define __atomic_store_n(ptr, val, order) \
(*(ptr) = (val), __atomic_store((ptr), &(typeof(*(ptr))){val}, (order)))
#define atomic_store_explicit(object, desired, order) \
({ __typeof__ (object) ptr = (object); \
__typeof__ (*ptr) tmp = (desired); \
__atomic_store (ptr, &tmp, (order)); \
})
#define atomic_store(object, desired) \
atomic_store_explicit (object, desired, __ATOMIC_SEQ_CST)
#define __atomic_load_n(ptr, order) \
({ typeof(*(ptr)) __val; \
__atomic_load((ptr), &__val, (order)); \
__val; })
#define atomic_load_explicit(object, order) \
({ __typeof__ (object) ptr = (object); \
__typeof__ ((void)0,*ptr) tmp; \
__atomic_load (ptr, &tmp, (order)); \
tmp; \
})
#define atomic_load(object) atomic_load_explicit (object, __ATOMIC_SEQ_CST)
#define atomic_exchange_explicit(object, desired, order) \
({ __typeof__ (object) ptr = (object); \
__typeof__ (*ptr) val = (desired); \
__typeof__ (*ptr) tmp; \
__atomic_exchange (ptr, &val, &tmp, (order)); \
tmp; \
})
#define atomic_exchange(object, desired) \
atomic_exchange_explicit (object, desired, __ATOMIC_SEQ_CST)
#define __atomic_compare_exchange_n(ptr, expected, desired, weak, success, failure) \
({ typeof(*(ptr)) __desired = (desired); \
__atomic_compare_exchange((ptr), (expected), &__desired, \
(weak), (success), (failure)); })
#define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \
({ __typeof__ (object) ptr = (object); \
__typeof__ (*ptr) tmp = desired; \
__atomic_compare_exchange(ptr, expected, &tmp, 0, success, failure); \
})
#define atomic_compare_exchange_strong(object, expected, desired) \
atomic_compare_exchange_strong_explicit (object, expected, desired, \
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
#define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \
({ __typeof__ (object) ptr = (object); \
__typeof__ (*ptr) tmp = desired; \
__atomic_compare_exchange(ptr, expected, &tmp, 1, success, failure); \
})
#define atomic_compare_exchange_weak(object, expected, desired) \
atomic_compare_exchange_weak_explicit (object, expected, desired, \
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
#define atomic_fetch_add(object, operand) \
__atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST)
#define atomic_fetch_add_explicit __atomic_fetch_add
#define atomic_fetch_sub(object, operand) \
__atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST)
#define atomic_fetch_sub_explicit __atomic_fetch_sub
#define atomic_fetch_or(object, operand) \
__atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST)
#define atomic_fetch_or_explicit __atomic_fetch_or
#define atomic_fetch_xor(object, operand) \
__atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST)
#define atomic_fetch_xor_explicit __atomic_fetch_xor
#define atomic_fetch_and(object, operand) \
__atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST)
#define atomic_fetch_and_explicit __atomic_fetch_and
extern void atomic_thread_fence (memory_order);
#define __atomic_thread_fence(order) atomic_thread_fence (order)
extern void atomic_signal_fence (memory_order);
#define __atomic_signal_fence(order) atomic_signal_fence(order)
#define atomic_signal_fence(order) __atomic_signal_fence (order)
extern bool __atomic_is_lock_free(size_t size, void *ptr);
#define atomic_is_lock_free(OBJ) __atomic_is_lock_free (sizeof (*(OBJ)), (OBJ))
extern bool atomic_flag_test_and_set(void *object);
extern bool atomic_flag_test_and_set_explicit(void *object, memory_order order);
extern void atomic_flag_clear(void *object);
extern void atomic_flag_clear_explicit(void *object, memory_order order);
#endif /* _STDATOMIC_H */

View File

@ -8,31 +8,16 @@ typedef __PTRDIFF_TYPE__ ptrdiff_t;
typedef __PTRDIFF_TYPE__ intptr_t;
typedef __SIZE_TYPE__ uintptr_t;
#ifndef __int8_t_defined
#define __int8_t_defined
typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
#ifdef __LP64__
typedef signed long int int64_t;
#else
typedef signed long long int int64_t;
#endif
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
#ifdef __LP64__
typedef unsigned long int uint64_t;
#else
typedef unsigned long long int uint64_t;
#endif
#if __STDC_VERSION__ >= 201112L
typedef union { long long __ll; long double __ld; } max_align_t;
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
#define offsetof(type, field) ((size_t)&((type *)0)->field)
#undef offsetof
#define offsetof(type, field) __builtin_offsetof(type, field)
void *alloca(size_t size);

7
include/stdnoreturn.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _STDNORETURN_H
#define _STDNORETURN_H
/* ISOC11 noreturn */
#define noreturn _Noreturn
#endif /* _STDNORETURN_H */

338
include/tccdefs.h Normal file
View File

@ -0,0 +1,338 @@
/* tccdefs.h
Nothing is defined before this file except target machine, target os
and the few things related to option settings in tccpp.c:tcc_predefs().
This file is either included at runtime as is, or converted and
included as C-strings at compile-time (depending on CONFIG_TCC_PREDEFS).
Note that line indent matters:
- in lines starting at column 1, platform macros are replaced by
corresponding TCC target compile-time macros. See conftest.c for
the list of platform macros supported in lines starting at column 1.
- only lines indented >= 4 are actually included into the executable,
check tccdefs_.h.
*/
#if __SIZEOF_POINTER__ == 4
/* 32bit systems. */
#if defined __OpenBSD__
#define __SIZE_TYPE__ unsigned long
#define __PTRDIFF_TYPE__ long
#else
#define __SIZE_TYPE__ unsigned int
#define __PTRDIFF_TYPE__ int
#endif
#define __ILP32__ 1
#define __INT64_TYPE__ long long
#elif __SIZEOF_LONG__ == 4
/* 64bit Windows. */
#define __SIZE_TYPE__ unsigned long long
#define __PTRDIFF_TYPE__ long long
#define __LLP64__ 1
#define __INT64_TYPE__ long long
#else
/* Other 64bit systems. */
#define __SIZE_TYPE__ unsigned long
#define __PTRDIFF_TYPE__ long
#define __LP64__ 1
# if defined __linux__
#define __INT64_TYPE__ long
# else /* APPLE, BSD */
#define __INT64_TYPE__ long long
# endif
#endif
#define __SIZEOF_INT__ 4
#define __INT_MAX__ 0x7fffffff
#if __SIZEOF_LONG__ == 4
#define __LONG_MAX__ 0x7fffffffL
#else
#define __LONG_MAX__ 0x7fffffffffffffffL
#endif
#define __SIZEOF_LONG_LONG__ 8
#define __LONG_LONG_MAX__ 0x7fffffffffffffffLL
#define __CHAR_BIT__ 8
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __ORDER_BIG_ENDIAN__ 4321
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#if defined _WIN32
#define __WCHAR_TYPE__ unsigned short
#define __WINT_TYPE__ unsigned short
#elif defined __linux__
#define __WCHAR_TYPE__ int
#define __WINT_TYPE__ unsigned int
#else
#define __WCHAR_TYPE__ int
#define __WINT_TYPE__ int
#endif
#if __STDC_VERSION__ >= 201112L
# define __STDC_NO_ATOMICS__ 1
# define __STDC_NO_COMPLEX__ 1
# define __STDC_NO_THREADS__ 1
#if !defined _WIN32
# define __STDC_UTF_16__ 1
# define __STDC_UTF_32__ 1
#endif
#endif
#if defined _WIN32
#define __declspec(x) __attribute__((x))
#define __cdecl
#elif defined __FreeBSD__
#define __GNUC__ 9
#define __GNUC_MINOR__ 3
#define __GNUC_PATCHLEVEL__ 0
#define __GNUC_STDC_INLINE__ 1
#define __NO_TLS 1
#define __RUNETYPE_INTERNAL 1
# if __SIZEOF_POINTER__ == 8
#define __SIZEOF_SIZE_T__ 8
#define __SIZEOF_PTRDIFF_T__ 8
#else
#define __SIZEOF_SIZE_T__ 4
#define __SIZEOF_PTRDIFF_T__ 4
# endif
#elif defined __FreeBSD_kernel__
#elif defined __NetBSD__
#define __GNUC__ 4
#define __GNUC_MINOR__ 1
#define __GNUC_PATCHLEVEL__ 0
#define _Pragma(x)
#define __ELF__ 1
#if defined __aarch64__
#define _LOCORE /* avoids usage of __asm */
#endif
#elif defined __OpenBSD__
#define __GNUC__ 4
#define _ANSI_LIBRARY 1
#elif defined __APPLE__
/* emulate APPLE-GCC to make libc's headerfiles compile: */
#define __GNUC__ 4 /* darwin emits warning on GCC<4 */
#define __APPLE_CC__ 1 /* for <TargetConditionals.h> */
#define __LITTLE_ENDIAN__ 1
#define _DONT_USE_CTYPE_INLINE_ 1
/* avoids usage of GCC/clang specific builtins in libc-headerfiles: */
#define __FINITE_MATH_ONLY__ 1
#define _FORTIFY_SOURCE 0
//#define __has_builtin(x) 0
#define _Float16 short unsigned int /* fake type just for size & alignment (macOS Sequoia) */
#elif defined __ANDROID__
#define BIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD
#else
/* Linux */
#endif
/* Some derived integer types needed to get stdint.h to compile correctly on some platforms */
#ifndef __NetBSD__
#define __UINTPTR_TYPE__ unsigned __PTRDIFF_TYPE__
#define __INTPTR_TYPE__ __PTRDIFF_TYPE__
#endif
#define __INT32_TYPE__ int
#if !defined _WIN32
/* glibc defines. We do not support __USER_NAME_PREFIX__ */
#define __REDIRECT(name, proto, alias) name proto __asm__ (#alias)
#define __REDIRECT_NTH(name, proto, alias) name proto __asm__ (#alias) __THROW
#define __REDIRECT_NTHNL(name, proto, alias) name proto __asm__ (#alias) __THROWNL
#endif
/* not implemented */
#define __PRETTY_FUNCTION__ __FUNCTION__
#define __has_builtin(x) 0
#define __has_feature(x) 0
#define __has_attribute(x) 0
/* C23 Keywords */
#define _Nonnull
#define _Nullable
#define _Nullable_result
#define _Null_unspecified
/* skip __builtin... with -E */
#ifndef __TCC_PP__
#define __builtin_offsetof(type, field) ((__SIZE_TYPE__)&((type*)0)->field)
#define __builtin_extract_return_addr(x) x
#if !defined __linux__ && !defined _WIN32
/* used by math.h */
#define __builtin_huge_val() 1e500
#define __builtin_huge_valf() 1e50f
#define __builtin_huge_vall() 1e5000L
# if defined __APPLE__
#define __builtin_nanf(ignored_string) (0.0F/0.0F)
/* used by floats.h to implement FLT_ROUNDS C99 macro. 1 == to nearest */
#define __builtin_flt_rounds() 1
/* used by _fd_def.h */
#define __builtin_bzero(p, ignored_size) bzero(p, sizeof(*(p)))
# else
#define __builtin_nanf(ignored_string) (0.0F/0.0F)
# endif
#endif
/* GCC's __uint128_t appears in some Linux/OSX header files.
Just make it some type with same size and alignment. */
struct __uint128__ { char x[16]; } __attribute((__aligned__(16)));
#define __int128_t struct __uint128__
#define __uint128_t struct __uint128__
/* __builtin_va_list */
#if defined __x86_64__
#if !defined _WIN32
/* GCC compatible definition of va_list. */
/* This should be in sync with the declaration in our lib/va_list.c */
typedef struct {
unsigned gp_offset, fp_offset;
union {
unsigned overflow_offset;
char *overflow_arg_area;
};
char *reg_save_area;
} __builtin_va_list[1];
void *__va_arg(__builtin_va_list ap, int arg_type, int size, int align);
#define __builtin_va_start(ap, last) \
(*(ap) = *(__builtin_va_list)((char*)__builtin_frame_address(0) - 24))
#define __builtin_va_arg(ap, t) \
(*(t *)(__va_arg(ap, __builtin_va_arg_types(t), sizeof(t), __alignof__(t))))
#define __builtin_va_copy(dest, src) (*(dest) = *(src))
#else /* _WIN64 */
typedef char *__builtin_va_list;
#define __builtin_va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \
? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8))
#endif
#elif defined __arm__
typedef char *__builtin_va_list;
#define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x)
#define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \
& ~(_tcc_alignof(type) - 1))
#define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
#define __builtin_va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \
&~3), *(type *)(ap - ((sizeof(type)+3)&~3)))
#elif defined __aarch64__
#if defined _WIN32
typedef char *__builtin_va_list;
#elif defined __APPLE__
typedef struct {
void *__stack;
} __builtin_va_list;
#else
typedef struct {
void *__stack, *__gr_top, *__vr_top;
int __gr_offs, __vr_offs;
} __builtin_va_list;
#endif
#elif defined __riscv
typedef char *__builtin_va_list;
#define __va_reg_size (__riscv_xlen >> 3)
#define _tcc_align(addr,type) (((unsigned long)addr + __alignof__(type) - 1) \
& -(__alignof__(type)))
#define __builtin_va_arg(ap,type) (*(sizeof(type) > (2*__va_reg_size) ? *(type **)((ap += __va_reg_size) - __va_reg_size) : (ap = (va_list)(_tcc_align(ap,type) + (sizeof(type)+__va_reg_size - 1)& -__va_reg_size), (type *)(ap - ((sizeof(type)+ __va_reg_size - 1)& -__va_reg_size)))))
#else /* __i386__ */
typedef char *__builtin_va_list;
#define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
#define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3)))
#endif
#define __builtin_va_end(ap) (void)(ap)
#ifndef __builtin_va_copy
# define __builtin_va_copy(dest, src) (dest) = (src)
#endif
/* TCC BBUILTIN AND BOUNDS ALIASES */
#ifdef __leading_underscore
# define __RENAME(X) __asm__("_"X)
#else
# define __RENAME(X) __asm__(X)
#endif
#ifdef __TCC_BCHECK__
# define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME("__bound_"#name);
# define __BOUND(ret,name,params) ret name params __RENAME("__bound_"#name);
#else
# define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME(#name);
# define __BOUND(ret,name,params)
#endif
#ifdef _WIN32
#define __BOTH __BOUND
#define __BUILTIN(ret,name,params)
#else
#define __BOTH(ret,name,params) __BUILTINBC(ret,name,params)__BOUND(ret,name,params)
#define __BUILTIN(ret,name,params) ret __builtin_##name params __RENAME(#name);
#endif
__BOTH(void*, memcpy, (void *, const void*, __SIZE_TYPE__))
__BOTH(void*, memmove, (void *, const void*, __SIZE_TYPE__))
__BOTH(void*, memset, (void *, int, __SIZE_TYPE__))
__BOTH(int, memcmp, (const void *, const void*, __SIZE_TYPE__))
__BOTH(__SIZE_TYPE__, strlen, (const char *))
__BOTH(char*, strcpy, (char *, const char *))
__BOTH(char*, strncpy, (char *, const char*, __SIZE_TYPE__))
__BOTH(int, strcmp, (const char*, const char*))
__BOTH(int, strncmp, (const char*, const char*, __SIZE_TYPE__))
__BOTH(char*, strcat, (char*, const char*))
__BOTH(char*, strncat, (char*, const char*, __SIZE_TYPE__))
__BOTH(char*, strchr, (const char*, int))
__BOTH(char*, strrchr, (const char*, int))
__BOTH(char*, strdup, (const char*))
#if defined __ARM_EABI__
__BOUND(void*,__aeabi_memcpy,(void*,const void*,__SIZE_TYPE__))
__BOUND(void*,__aeabi_memmove,(void*,const void*,__SIZE_TYPE__))
__BOUND(void*,__aeabi_memmove4,(void*,const void*,__SIZE_TYPE__))
__BOUND(void*,__aeabi_memmove8,(void*,const void*,__SIZE_TYPE__))
__BOUND(void*,__aeabi_memset,(void*,int,__SIZE_TYPE__))
#endif
#if defined __linux__ || defined __APPLE__ // HAVE MALLOC_REDIR
#define __MAYBE_REDIR __BUILTIN
#else
#define __MAYBE_REDIR __BOTH
#endif
__MAYBE_REDIR(void*, malloc, (__SIZE_TYPE__))
__MAYBE_REDIR(void*, realloc, (void *, __SIZE_TYPE__))
__MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__))
__MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__))
__MAYBE_REDIR(void, free, (void*))
__BOTH(void*, alloca, (__SIZE_TYPE__))
void *alloca(__SIZE_TYPE__);
__BUILTIN(void, abort, (void))
__BOUND(void, longjmp, ())
#if !defined _WIN32
__BOUND(void*, mmap, ())
__BOUND(int, munmap, ())
#endif
#undef __BUILTINBC
#undef __BUILTIN
#undef __BOUND
#undef __BOTH
#undef __MAYBE_REDIR
#undef __RENAME
#define __BUILTIN_EXTERN(name,u) \
int __builtin_##name(u int); \
int __builtin_##name##l(u long); \
int __builtin_##name##ll(u long long);
__BUILTIN_EXTERN(ffs,)
__BUILTIN_EXTERN(clz, unsigned)
__BUILTIN_EXTERN(ctz, unsigned)
__BUILTIN_EXTERN(clrsb,)
__BUILTIN_EXTERN(popcount, unsigned)
__BUILTIN_EXTERN(parity, unsigned)
#undef __BUILTIN_EXTERN
#endif /* ndef __TCC_PP__ */

89
include/tgmath.h Normal file
View File

@ -0,0 +1,89 @@
/*
* ISO C Standard: 7.22 Type-generic math <tgmath.h>
*/
#ifndef _TGMATH_H
#define _TGMATH_H
#include <math.h>
#ifndef __cplusplus
#define __tgmath_real(x, F) \
_Generic ((x), float: F##f, long double: F##l, default: F)(x)
#define __tgmath_real_2_1(x, y, F) \
_Generic ((x), float: F##f, long double: F##l, default: F)(x, y)
#define __tgmath_real_2(x, y, F) \
_Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y)
#define __tgmath_real_3_2(x, y, z, F) \
_Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y, z)
#define __tgmath_real_3(x, y, z, F) \
_Generic ((x)+(y)+(z), float: F##f, long double: F##l, default: F)(x, y, z)
/* Functions defined in both <math.h> and <complex.h> (7.22p4) */
#define acos(z) __tgmath_real(z, acos)
#define asin(z) __tgmath_real(z, asin)
#define atan(z) __tgmath_real(z, atan)
#define acosh(z) __tgmath_real(z, acosh)
#define asinh(z) __tgmath_real(z, asinh)
#define atanh(z) __tgmath_real(z, atanh)
#define cos(z) __tgmath_real(z, cos)
#define sin(z) __tgmath_real(z, sin)
#define tan(z) __tgmath_real(z, tan)
#define cosh(z) __tgmath_real(z, cosh)
#define sinh(z) __tgmath_real(z, sinh)
#define tanh(z) __tgmath_real(z, tanh)
#define exp(z) __tgmath_real(z, exp)
#define log(z) __tgmath_real(z, log)
#define pow(z1,z2) __tgmath_real_2(z1, z2, pow)
#define sqrt(z) __tgmath_real(z, sqrt)
#define fabs(z) __tgmath_real(z, fabs)
/* Functions defined in <math.h> only (7.22p5) */
#define atan2(x,y) __tgmath_real_2(x, y, atan2)
#define cbrt(x) __tgmath_real(x, cbrt)
#define ceil(x) __tgmath_real(x, ceil)
#define copysign(x,y) __tgmath_real_2(x, y, copysign)
#define erf(x) __tgmath_real(x, erf)
#define erfc(x) __tgmath_real(x, erfc)
#define exp2(x) __tgmath_real(x, exp2)
#define expm1(x) __tgmath_real(x, expm1)
#define fdim(x,y) __tgmath_real_2(x, y, fdim)
#define floor(x) __tgmath_real(x, floor)
#define fma(x,y,z) __tgmath_real_3(x, y, z, fma)
#define fmax(x,y) __tgmath_real_2(x, y, fmax)
#define fmin(x,y) __tgmath_real_2(x, y, fmin)
#define fmod(x,y) __tgmath_real_2(x, y, fmod)
#define frexp(x,y) __tgmath_real_2_1(x, y, frexp)
#define hypot(x,y) __tgmath_real_2(x, y, hypot)
#define ilogb(x) __tgmath_real(x, ilogb)
#define ldexp(x,y) __tgmath_real_2_1(x, y, ldexp)
#define lgamma(x) __tgmath_real(x, lgamma)
#define llrint(x) __tgmath_real(x, llrint)
#define llround(x) __tgmath_real(x, llround)
#define log10(x) __tgmath_real(x, log10)
#define log1p(x) __tgmath_real(x, log1p)
#define log2(x) __tgmath_real(x, log2)
#define logb(x) __tgmath_real(x, logb)
#define lrint(x) __tgmath_real(x, lrint)
#define lround(x) __tgmath_real(x, lround)
#define nearbyint(x) __tgmath_real(x, nearbyint)
#define nextafter(x,y) __tgmath_real_2(x, y, nextafter)
#define nexttoward(x,y) __tgmath_real_2(x, y, nexttoward)
#define remainder(x,y) __tgmath_real_2(x, y, remainder)
#define remquo(x,y,z) __tgmath_real_3_2(x, y, z, remquo)
#define rint(x) __tgmath_real(x, rint)
#define round(x) __tgmath_real(x, round)
#define scalbln(x,y) __tgmath_real_2_1(x, y, scalbln)
#define scalbn(x,y) __tgmath_real_2_1(x, y, scalbn)
#define tgamma(x) __tgmath_real(x, tgamma)
#define trunc(x) __tgmath_real(x, trunc)
/* Functions defined in <complex.h> only (7.22p6)
#define carg(z) __tgmath_cplx_only(z, carg)
#define cimag(z) __tgmath_cplx_only(z, cimag)
#define conj(z) __tgmath_cplx_only(z, conj)
#define cproj(z) __tgmath_cplx_only(z, cproj)
#define creal(z) __tgmath_cplx_only(z, creal)
*/
#endif /* __cplusplus */
#endif /* _TGMATH_H */

View File

@ -7,67 +7,100 @@ include $(TOP)/Makefile
VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib
T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
BIN = $(TOP)/$(X)libtcc1.a
XCFG = $(or $(findstring -win,$T),-unx)
S = $(if $(findstring yes,$(SILENT)),@$(info * $@))
TCC = $(TOP)/$(X)tcc$(EXESUF)
XTCC ?= $(TOP)/$(X)tcc$(EXESUF)
XCC = $(XTCC)
XAR = $(XTCC) -ar
XFLAGS-unx = -B$(TOPSRC)
XFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include
XFLAGS = $(XFLAGS$(XCFG))
XCFG = $(or $(findstring -win,$T),-unx)
BFLAGS = -bt
# in order to use gcc, tyoe: make <target>-libtcc1-usegcc=yes
# in order to use gcc, type: make <target>-libtcc1-usegcc=yes
arm-libtcc1-usegcc ?= no
# This makes bounds checking 40%..60% faster.
#x86_64-libtcc1-usegcc=yes
#i386-libtcc1-usegcc=yes
ifeq "$($(T)-libtcc1-usegcc)" "yes"
XCC = $(CC)
XAR = $(AR)
XFLAGS = $(CFLAGS) -fPIC
XFLAGS = $(CFLAGS) -fPIC -fno-omit-frame-pointer -Wno-unused-function -Wno-unused-variable
BFLAGS = $(if $(CONFIG_dwarf),-gdwarf,-gstabs)
endif
# only for native compiler
$(X)BCHECK_O = bcheck.o
XFLAGS += -I$(TOP)
ifeq ($(CONFIG_musl)$(CONFIG_uClibc),yes)
BCHECK_O =
endif
I386_O = libtcc1.o $(COMMON_O)
X86_64_O = libtcc1.o $(COMMON_O)
ARM_O = libtcc1.o armeabi.o armflush.o $(COMMON_O)
ARM64_O = lib-arm64.o $(COMMON_O)
RISCV64_O = lib-arm64.o $(COMMON_O)
COMMON_O = stdatomic.o atomic.o builtin.o alloca.o alloca-bt.o
WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o winex.o
LIN_O = dsohandle.o
OSX_O =
ifdef CONFIG_OSX
XFLAGS += -D_ANSI_SOURCE
endif
# backtrace/bcheck/run only for native compiler
Nat = $(if $X,no,)
Cbt = $(Nat)$(subst yes,,$(CONFIG_backtrace))
Cbc = $(Cbt)$(subst yes,,$(CONFIG_bcheck))
I386_O = libtcc1.o alloca86.o alloca86-bt.o
X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o
ARM_O = libtcc1.o armeabi.o alloca-arm.o armflush.o
ARM64_O = lib-arm64.o
WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o
$(Nat)COMMON_O += runmain.o tcov.o
$(Cbt)COMMON_O += bt-exe.o bt-log.o
$(Cbt)WIN_O += bt-dll.o
$(Cbc)COMMON_O += bcheck.o
OBJ-i386 = $(I386_O) $(BCHECK_O)
OBJ-x86_64 = $(X86_64_O) va_list.o $(BCHECK_O)
OBJ-x86_64-osx = $(X86_64_O) va_list.o
OBJ-i386-win32 = $(I386_O) chkstk.o bcheck.o $(WIN_O)
OBJ-x86_64-win32 = $(X86_64_O) chkstk.o bcheck.o $(WIN_O)
OBJ-arm64 = $(ARM64_O)
OBJ-arm = $(ARM_O)
OBJ-arm-fpa = $(ARM_O)
OBJ-arm-fpa-ld = $(ARM_O)
OBJ-arm-vfp = $(ARM_O)
OBJ-arm-eabi = $(ARM_O)
OBJ-arm-eabihf = $(ARM_O)
# not in libtcc1.a
EXTRA_O = runmain.o bt-exe.o bt-dll.o bt-log.o bcheck.o
OBJ-i386 = $(I386_O) pic86.o $(LIN_O)
OBJ-x86_64 = $(X86_64_O) va_list.o $(LIN_O)
OBJ-x86_64-osx = $(X86_64_O) va_list.o $(OSX_O)
OBJ-i386-win32 = $(I386_O) chkstk.o $(WIN_O)
OBJ-x86_64-win32 = $(X86_64_O) chkstk.o $(WIN_O)
OBJ-arm64 = $(ARM64_O) armflush.o $(LIN_O)
OBJ-arm64-osx = $(ARM64_O) $(OSX_O)
OBJ-arm64-win32 = $(ARM64_O) chkstk.o $(WIN_O)
OBJ-arm = $(ARM_O) $(LIN_O)
OBJ-arm-fpa = $(OBJ-arm)
OBJ-arm-fpa-ld = $(OBJ-arm)
OBJ-arm-vfp = $(OBJ-arm)
OBJ-arm-eabi = $(OBJ-arm)
OBJ-arm-eabihf = $(OBJ-arm)
OBJ-arm-wince = $(ARM_O) $(WIN_O)
OBJ-riscv64 = $(RISCV64_O) lib-riscv.o $(LIN_O)
$(BIN) : $(patsubst %.o,$(X)%.o,$(OBJ-$T))
$(XAR) rcs $@ $^
OBJ-extra = $(filter $(EXTRA_O),$(OBJ-$T))
OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T)))
$(X)%.o : %.c
$(XCC) -c $< -o $@ $(XFLAGS)
ALL = $(addprefix $(TOP)/,$(X)libtcc1.a $(OBJ-extra))
$(X)%.o : %.S
$(XCC) -c $< -o $@ $(XFLAGS)
all: $(ALL)
$(TOP)/$(X)libtcc1.a : $(OBJ-libtcc1) $(TCC)
$S$(XAR) rcs $@ $(OBJ-libtcc1)
$(X)%.o : %.c $(TCC)
$S$(XCC) -c $< -o $@ $(XFLAGS)
$(X)%.o : %.S $(TCC)
$S$(XCC) -c $< -o $@ $(XFLAGS)
$(TOP)/%.o : %.c $(TCC)
$S$(XCC) -c $< -o $@ $(XFLAGS)
$(TOP)/bcheck.o : XFLAGS += $(BFLAGS)
$(X)crt1w.o : crt1.c
$(X)wincrt1w.o : wincrt1.c
# don't try to make it
$(TCC) : ;
clean :
rm -f *.a *.o $(BIN)
rm -f *.o $(addprefix $(TOP)/,*libtcc1.a $(EXTRA_O))

View File

@ -1,17 +0,0 @@
.text
.align 2
.global alloca
.type alloca, %function
alloca:
#ifdef __TINYC__
.int 0xe060d00d
.int 0xe3cdd007
.int 0xe1a0000d
.int 0xe1a0f00e
#else
rsb sp, r0, sp
bic sp, sp, #7
mov r0, sp
mov pc, lr
#endif
.size alloca, .-alloca

200
lib/alloca-bt.S Normal file
View File

@ -0,0 +1,200 @@
/* ---------------------------------------------- */
/* alloca-bt.S */
#ifdef __leading_underscore
# define _(s) _##s
#else
# define _(s) s
#endif
/* ---------------------------------------------- */
#if defined __i386__
.globl _(__bound_alloca)
_(__bound_alloca):
pop %edx
pop %eax
mov %eax, %ecx
test %eax,%eax
jz p6
add $3 + 1,%eax
and $-4,%eax
#ifdef _WIN32
p4:
cmp $4096,%eax
jb p5
test %eax,-4096(%esp)
sub $4096,%esp
sub $4096,%eax
jmp p4
p5:
#endif
sub %eax,%esp
mov %esp,%eax
push %edx
push %eax
push %ecx
push %eax
call _(__bound_new_region)
add $8, %esp
pop %eax
pop %edx
p6:
push %edx
push %edx
ret
/* ---------------------------------------------- */
#elif defined __x86_64__
.globl _(__bound_alloca)
_(__bound_alloca):
#ifdef _WIN32
inc %rcx # add one extra to separate regions
jmp _(alloca)
.globl _(__bound_alloca_nr)
_(__bound_alloca_nr):
dec %rcx
push %rax
mov %rcx,%rdx
mov %rax,%rcx
sub $32,%rsp
call _(__bound_new_region)
add $32,%rsp
pop %rax
ret
#else
pop %rdx
mov %rdi,%rax
and %eax,%eax
jz p3
mov %rax,%rsi # size, a second parm to the __bound_new_region
add $15 + 1,%rax # add one extra to separate regions
and $-16,%rax
sub %rax,%rsp
mov %rsp,%rdi # pointer, a first parm to the __bound_new_region
mov %rsp,%rax
push %rdx
push %rax
call _(__bound_new_region)
pop %rax
pop %rdx
p3:
push %rdx
ret
#endif
/* ---------------------------------------------- */
#elif defined __arm__
.globl _(__bound_alloca)
_(__bound_alloca):
mov r1, r0
add r0, r0, #1
rsb sp, r0, sp
bic sp, sp, #7
mov r0, sp
push { lr }
bl _(__bound_new_region)
pop { lr }
mov r0, sp
mov pc, lr
/* ---------------------------------------------- */
#elif defined __aarch64__ || defined __arm64__
.globl _(__bound_alloca)
_(__bound_alloca):
#ifdef __TINYC__
.int 0xaa0003e1
.int 0x91004000
.int 0x927cec00
#ifdef _WIN32
.int 0xb4000160
.int 0xd2820002
.int 0xeb02001f
.int 0x540000c3
.int 0xcb2263e3
.int 0xf940007f
.int 0xcb2263ff
.int 0xcb020000
.int 0x17fffffa
.int 0xb4000040
#endif
.int 0xcb2063ff
.int 0x910003e0
.int 0xa9bf7bfd
.reloc ., R_AARCH64_CALL26, _(__bound_new_region)
.int 0x94000000
.int 0xa8c17bfd
.int 0x910003e0
.int 0xd65f03c0
#else
mov x1, x0
add x0, x0, #16 // Round up to 16-byte boundary
and x0, x0, #-16 // Ensure 16-byte alignment
#ifdef _WIN32
cbz x0, p100 // If size is 0, skip to return
// Windows requires page-wise allocation with stack probing
mov x2, #4096 // Page size = 4096 bytes
p101:
cmp x0, x2 // Compare remaining size with page size
b.lo p102 // If less than page, jump to remainder
// Probe first, then allocate
sub x3, sp, x2 // Calculate guard page address (sp - 4096)
ldr xzr, [x3] // Touch guard page FIRST
sub sp, sp, x2 // THEN allocate the page
sub x0, x0, x2 // Decrement remaining size
b p101 // Continue loop
p102:
// Allocate remaining bytes (less than one page)
cbz x0, p100 // If no remaining bytes, skip
sub sp, sp, x0 // Allocate remaining space
#else
// Non-Windows: simple one-time allocation
sub sp, sp, x0 // Allocate space on stack
#endif
p100:
mov x0, sp // Return allocated address
stp x29, x30, [sp, #-16]!
bl _(__bound_new_region)
ldp x29, x30, [sp], #16
mov x0, sp // Return allocated address
ret // Return to caller
#endif
/* ---------------------------------------------- */
#elif defined __riscv
.globl _(__bound_alloca)
_(__bound_alloca):
mv a1, a0
sub sp, sp, a0
addi sp, sp, -16
andi sp, sp, -16
add a0, sp, zero
addi sp,sp,-16
sd s0,0(sp)
sd ra,8(sp)
jal _(__bound_new_region)
ld s0,0(sp)
ld ra,8(sp)
addi sp,sp,16
add a0, sp, zero
ret
/* ---------------------------------------------- */
#endif

148
lib/alloca.S Normal file
View File

@ -0,0 +1,148 @@
/* ---------------------------------------------- */
/* alloca.S */
#ifdef __leading_underscore
# define _(s) _##s
#else
# define _(s) s
#endif
/* ---------------------------------------------- */
#if defined __i386__
.globl _(alloca), _(__alloca)
_(alloca):
_(__alloca):
pop %edx
pop %eax
add $3,%eax
and $-4,%eax
jz p3
#ifdef _WIN32
p1:
cmp $4096,%eax
jb p2
test %eax,-4096(%esp)
sub $4096,%esp
sub $4096,%eax
jmp p1
p2:
#endif
sub %eax,%esp
mov %esp,%eax
p3:
push %edx
push %edx
ret
/* ---------------------------------------------- */
#elif defined __x86_64__
.globl _(alloca)
_(alloca):
pop %rdx
#ifdef _WIN32
mov %rcx,%rax
#else
mov %rdi,%rax
#endif
add $15,%rax
and $-16,%rax
jz p3
#ifdef _WIN32
p1:
cmp $4096,%rax
jb p2
test %rax,-4096(%rsp)
sub $4096,%rsp
sub $4096,%rax
jmp p1
p2:
#endif
sub %rax,%rsp
mov %rsp,%rax
p3:
push %rdx
ret
/* ---------------------------------------------- */
#elif defined __arm__
.globl _(alloca)
_(alloca):
rsb sp, r0, sp
bic sp, sp, #7
mov r0, sp
mov pc, lr
/* ---------------------------------------------- */
#elif defined __aarch64__ || defined __arm64__
.globl _(alloca)
_(alloca):
#ifdef __TINYC__
.int 0x91003c00
.int 0x927cec00
#ifdef _WIN32
.int 0xb4000160
.int 0xd2820001
.int 0xeb01001f
.int 0x540000c3
.int 0xcb2163e2
.int 0xf940005f
.int 0xcb2163ff
.int 0xcb010000
.int 0x17fffffa
.int 0xb4000040
#endif
.int 0xcb2063ff
.int 0x910003e0
.int 0xd65f03c0
#else
add x0, x0, #15 // Round up to 16-byte boundary
and x0, x0, #-16 // Ensure 16-byte alignment
#ifdef _WIN32
cbz x0, p100 // If size is 0, skip to return
// Windows requires page-wise allocation with stack probing
mov x1, #4096 // Page size = 4096 bytes
p101:
cmp x0, x1 // Compare remaining size with page size
b.lo p102 // If less than page, jump to remainder
// Probe first, then allocate
sub x2, sp, x1 // Calculate guard page address (sp - 4096)
ldr xzr, [x2] // Touch guard page FIRST
sub sp, sp, x1 // THEN allocate the page
sub x0, x0, x1 // Decrement remaining size
b p101 // Continue loop
p102:
// Allocate remaining bytes (less than one page)
cbz x0, p100 // If no remaining bytes, skip
sub sp, sp, x0 // Allocate remaining space
#else
// Non-Windows: simple one-time allocation
sub sp, sp, x0 // Allocate space on stack
#endif
p100:
mov x0, sp // Return allocated address
ret // Return to caller
#endif
/* ---------------------------------------------- */
#elif defined __riscv
.globl _(alloca)
_(alloca):
sub sp, sp, a0
addi sp, sp, -15
andi sp, sp, -16
add a0, sp, zero
ret
#endif

View File

@ -1,47 +0,0 @@
/* ---------------------------------------------- */
/* alloca86-bt.S */
.globl __bound_alloca
__bound_alloca:
pop %edx
pop %eax
mov %eax, %ecx
add $3,%eax
and $-4,%eax
jz p6
#ifdef _WIN32
p4:
cmp $4096,%eax
jbe p5
test %eax,-4096(%esp)
sub $4096,%esp
sub $4096,%eax
jmp p4
p5:
#endif
sub %eax,%esp
mov %esp,%eax
push %edx
push %eax
push %ecx
push %eax
call __bound_new_region
add $8, %esp
pop %eax
pop %edx
p6:
push %edx
push %edx
ret
/* mark stack as nonexecutable */
#if defined __ELF__ && defined __linux__
.section .note.GNU-stack,"",@progbits
#endif
/* ---------------------------------------------- */

View File

@ -1,31 +0,0 @@
/* ---------------------------------------------- */
/* alloca86.S */
.globl alloca
alloca:
pop %edx
pop %eax
add $3,%eax
and $-4,%eax
jz p3
#ifdef _WIN32
p1:
cmp $4096,%eax
jbe p2
test %eax,-4096(%esp)
sub $4096,%esp
sub $4096,%eax
jmp p1
p2:
#endif
sub %eax,%esp
mov %esp,%eax
p3:
push %edx
push %edx
ret
/* ---------------------------------------------- */

View File

@ -1,56 +0,0 @@
/* ---------------------------------------------- */
/* alloca86_64.S */
.globl __bound_alloca
__bound_alloca:
#ifdef _WIN32
# bound checking is not implemented
pop %rdx
mov %rcx,%rax
add $15,%rax
and $-16,%rax
jz p3
p1:
cmp $4096,%rax
jbe p2
test %rax,-4096(%rsp)
sub $4096,%rsp
sub $4096,%rax
jmp p1
p2:
sub %rax,%rsp
mov %rsp,%rax
add $32,%rax
p3:
push %rdx
ret
#else
pop %rdx
mov %rdi,%rax
mov %rax,%rsi # size, a second parm to the __bound_new_region
add $15,%rax
and $-16,%rax
jz p3
sub %rax,%rsp
mov %rsp,%rdi # pointer, a first parm to the __bound_new_region
mov %rsp,%rax
push %rdx
push %rax
call __bound_new_region
pop %rax
pop %rdx
p3:
push %rdx
ret
#endif
/* ---------------------------------------------- */

View File

@ -1,34 +0,0 @@
/* ---------------------------------------------- */
/* alloca86_64.S */
.globl alloca
alloca:
pop %rdx
#ifdef _WIN32
mov %rcx,%rax
#else
mov %rdi,%rax
#endif
add $15,%rax
and $-16,%rax
jz p3
#ifdef _WIN32
p1:
cmp $4096,%rax
jbe p2
test %rax,-4096(%rsp)
sub $4096,%rsp
sub $4096,%rax
jmp p1
p2:
#endif
sub %rax,%rsp
mov %rsp,%rax
p3:
push %rdx
ret
/* ---------------------------------------------- */

View File

@ -47,7 +47,7 @@ typedef struct unsigned_int_struct {
} unsigned_int_struct;
#define REGS_RETURN(name, type) \
void name ## _return(type ret) {}
static void name ## _return(type ret) {}
/* Float helper functions */
@ -137,6 +137,11 @@ void __aeabi_ ## name(double_unsigned_struct val) \
int exp, high_shift, sign; \
double_unsigned_struct ret; \
\
if ((val.high & ~0x80000000) == 0 && val.low == 0) { \
ret.low = ret.high = 0; \
goto _ret_; \
} \
\
/* compute sign */ \
sign = val.high >> 31; \
\
@ -172,7 +177,7 @@ void __aeabi_ ## name(double_unsigned_struct val) \
ret.high = 0; \
ret.low = 1 << exp; \
if (exp > DOUBLE_FRAC_BITS - 32) { \
high_shift = exp - DOUBLE_FRAC_BITS - 32; \
high_shift = exp - (DOUBLE_FRAC_BITS - 32); \
ret.low |= val.high << high_shift; \
ret.low |= val.low >> (32 - high_shift); \
} else \
@ -190,6 +195,7 @@ void __aeabi_ ## name(double_unsigned_struct val) \
ret.low++; \
} \
\
_ret_: \
double_unsigned_struct_return(ret); \
}
@ -313,7 +319,7 @@ void __aeabi_ ## name(unsigned long long v) \
} \
} else { \
ret.high = ret.low = 0; \
double_unsigned_struct_return(ret); \
goto _ret_; \
} \
} \
\
@ -323,6 +329,7 @@ void __aeabi_ ## name(unsigned long long v) \
/* fill sign bit */ \
ret.high |= sign << 31; \
\
_ret_: \
double_unsigned_struct_return(ret); \
}
@ -332,7 +339,7 @@ __AEABI_XL2D(ul2d, 0)
/* long long to double conversion */
__AEABI_XL2D(l2d, 1)
#if 1
/* Long long helper functions */
/* TODO: add error in case of den == 0 (see §4.3.1 and §4.3.2) */
@ -467,6 +474,7 @@ void __aeabi_lasr(unsigned_int_struct val, int shift)
/* Integer division functions */
#if 0 /* very slow */
AEABI_UXDIVMOD(uidivmod, unsigned, uidiv_t, UINT)
int __aeabi_idiv(int numerator, int denominator)
@ -499,3 +507,136 @@ void __aeabi_uidivmod(unsigned num, unsigned den)
{
uidiv_t_return(aeabi_uidivmod(num, den));
}
#else
# define UIDIVMOD_ASM 1
#endif
/* Some targets do not have all eabi calls (OpenBSD) */
typedef __SIZE_TYPE__ size_t;
extern void *memcpy(void *dest, const void *src, size_t n);
extern void *memmove(void *dest, const void *src, size_t n);
extern void *memset(void *s, int c, size_t n);
void *
__aeabi_memcpy (void *dest, const void *src, size_t n)
{
return memcpy (dest, src, n);
}
void *
__aeabi_memmove (void *dest, const void *src, size_t n)
{
return memmove (dest, src, n);
}
void *
__aeabi_memmove4 (void *dest, const void *src, size_t n)
{
return memmove (dest, src, n);
}
void *
__aeabi_memmove8 (void *dest, const void *src, size_t n)
{
return memmove (dest, src, n);
}
void *
__aeabi_memset (void *s, size_t n, int c)
{
return memset (s, c, n);
}
/* ***************************************************************** */
#if UIDIVMOD_ASM
#include <config.h>
__asm__(
"\n .global __aeabi_idiv, __aeabi_idivmod"
"\n .global __aeabi_uidiv, __aeabi_uidivmod"
#if __ARM_FEATURE_IDIV
"\n__aeabi_idiv:"
"\n__aeabi_idivmod:"
"\n mov r2, r0"
"\n sdiv r0, r0, r1"
"\n mls r1, r1, r0, r2"
"\n bx lr"
"\n__aeabi_uidiv:"
"\n__aeabi_uidivmod:"
"\n mov r2, r0"
"\n udiv r0, r0, r1"
"\n mls r1, r1, r0, r2"
"\n bx lr"
#else
/* Runtime ABI for the ARM Cortex-M0
* idivmod.S: signed 32 bit division (quotient and remainder)
*
* Copyright (c) 2012 Jörg Mische <bobbl@gmx.de>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
"\n__aeabi_idiv:"
"\n__aeabi_idivmod:"
"\n cmp r0, #0"
"\n bge .Lnumerator_pos"
"\n rsb r0, r0, #0" // num = -num
"\n cmp r1, #0"
"\n bge .Lboth_neg"
"\n rsb r1, r1, #0" // den = -den
"\n push {lr}"
"\n bl __aeabi_uidivmod"
"\n rsb r1, r1, #0" // rem = -rem
"\n pop {pc}"
"\n.Lboth_neg:"
"\n push {lr}"
"\n bl __aeabi_uidivmod"
"\n rsb r0, r0, #0" // quot = -quot
"\n rsb r1, r1, #0" // rem = -rem
"\n pop {pc}"
"\n.Ldenom_neg:"
"\n rsb r1, r1, #0" // den = -den
"\n push {lr}"
"\n bl __aeabi_uidivmod"
"\n rsb r0, r0, #0" // quot = -quot
"\n pop {pc}"
"\n.Lnumerator_pos:"
"\n cmp r1, #0"
"\n blt .Ldenom_neg"
// Divide r0 by r1 and return the quotient in r0 and the remainder in r1
"\n__aeabi_uidiv:"
"\n__aeabi_uidivmod:"
// Shift left the denominator until it is greater than the numerator
"\n mov r2, #1" // counter
"\n mov r3, #0" // result
"\n cmp r0, r1"
"\n bls .Lsub_loop"
"\n adds r1, #0" // dont shift if denominator would overflow
"\n bmi .Lsub_loop"
"\n beq .Luidiv0"
"\n.Ldenom_shift_loop:"
"\n lsl r2, #1"
"\n lsls r1, #1"
"\n bmi .Lsub_loop"
"\n cmp r0, r1"
"\n bhi .Ldenom_shift_loop"
"\n.Lsub_loop:"
"\n cmp r0, r1" // if (num >= den)...
"\n subcs r0, r1" // numerator -= denom
"\n orrcs r3, r2" // result(r3) |= bitmask(r2)
"\n lsr r1, #1" // denom(r1) >>= 1
"\n lsrs r2, #1" // bitmask(r2) >>= 1
"\n bne .Lsub_loop"
"\n mov r1, r0" // remainder(r1) = numerator(r0)
"\n mov r0, r3" // quotient(r0) = result(r3)
"\n bx lr"
"\n.Luidiv0:" // XXX: division by zero
"\n mov r0, #0"
"\n bx lr"
#endif
);
#endif /* UIDIVMOD_ASM */
/* ***************************************************************** */
#endif

View File

@ -4,22 +4,25 @@
intrinsic with gcc. However tcc in order to compile
itself needs this function */
/* ------------------------------------------------------------- */
#if defined __arm__
#ifdef __TINYC__
/* syscall wrapper */
unsigned syscall(unsigned syscall_nr, ...);
unsigned _tccsyscall(unsigned syscall_nr, ...);
/* arm-tcc supports only fake asm currently */
__asm__(
".global syscall\n"
"syscall:\n"
".int 0xe92d4080\n" // push {r7, lr}
".int 0xe1a07000\n" // mov r7, r0
".int 0xe1a00001\n" // mov r0, r1
".int 0xe1a01002\n" // mov r1, r2
".int 0xe1a02003\n" // mov r2, r3
".int 0xef000000\n" // svc 0x00000000
".int 0xe8bd8080\n" // pop {r7, pc}
".global _tccsyscall\n"
"_tccsyscall:\n"
"push {r7, lr}\n\t"
"mov r7, r0\n\t"
"mov r0, r1\n\t"
"mov r1, r2\n\t"
"mov r2, r3\n\t"
"svc #0\n\t"
"pop {r7, pc}"
);
/* from unistd.h: */
@ -31,6 +34,8 @@ __asm__(
#define __ARM_NR_BASE (__NR_SYSCALL_BASE+0x0f0000)
#define __ARM_NR_cacheflush (__ARM_NR_BASE+2)
#define syscall _tccsyscall
#else
#define _GNU_SOURCE
@ -45,14 +50,15 @@ void __clear_cache(void *beginning, void *end)
{
/* __ARM_NR_cacheflush is kernel private and should not be used in user space.
* However, there is no ARM asm parser in tcc so we use it for now */
#if 1
syscall(__ARM_NR_cacheflush, beginning, end, 0);
#else
__asm__ ("push {r7}\n\t"
"mov r7, #0xf0002\n\t"
"mov r2, #0\n\t"
"swi 0\n\t"
"pop {r7}\n\t"
"ret");
#endif
}
/* ------------------------------------------------------------- */
#elif defined __aarch64__
void __clear_cache(void *beg, void *end)
{
__arm64_clear_cache(beg, end);
}
/* ------------------------------------------------------------- */
#endif

2669
lib/atomic.S Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

95
lib/bt-dll.c Normal file
View File

@ -0,0 +1,95 @@
/* ------------------------------------------------------------- */
/* stubs for calling bcheck functions from a dll. */
#include <windows.h>
#include <stdio.h>
#define REDIR_ALL \
REDIR(__bt_init) \
REDIR(__bt_exit) \
REDIR(tcc_backtrace) \
\
REDIR(__bound_ptr_add) \
REDIR(__bound_ptr_indir1) \
REDIR(__bound_ptr_indir2) \
REDIR(__bound_ptr_indir4) \
REDIR(__bound_ptr_indir8) \
REDIR(__bound_ptr_indir12) \
REDIR(__bound_ptr_indir16) \
REDIR(__bound_local_new) \
REDIR(__bound_local_delete) \
REDIR(__bound_new_region) \
\
REDIR(__bound_free) \
REDIR(__bound_malloc) \
REDIR(__bound_realloc) \
REDIR(__bound_memcpy) \
REDIR(__bound_memcmp) \
REDIR(__bound_memmove) \
REDIR(__bound_memset) \
REDIR(__bound_strlen) \
REDIR(__bound_strcpy) \
REDIR(__bound_strncpy) \
REDIR(__bound_strcmp) \
REDIR(__bound_strncmp) \
REDIR(__bound_strcat) \
REDIR(__bound_strchr) \
REDIR(__bound_strdup) \
REDIR(__bound_strncat) \
REDIR(__bound_strrchr) \
REDIR(__bound_setjmp) \
REDIR(__bound_longjmp)
#ifdef __leading_underscore
#define _(s) "_"#s
#else
#define _(s) #s
#endif
#define REDIR(s) void *s;
static struct { REDIR_ALL } all_ptrs;
#undef REDIR
#define REDIR(s) #s"\0"
static const char all_names[] = REDIR_ALL;
#undef REDIR
#if __aarch64__
# define REDIR(s) \
__asm__(".global "_(s)";"_(s)":"); \
__asm__(".int 0x58000090"); /* ldr x16, [pc, #16] */ \
__asm__(".int 0xf9400210"); /* ldr x16, [x16] */ \
__asm__(".int 0xd61f0200"); /* br x16 */ \
__asm__(".int 0xd503201f"); /* nop for alignment */ \
__asm__(".quad all_ptrs + (. - all_jmps - 16) / 24 * 8"); \
__asm__(".type "_(s)",function\n.size "_(s)",.-"_(s));
__asm__(".text\n.align 8\nall_jmps:");
REDIR_ALL
#else
# define REDIR(s) \
__asm__(".global "_(s)";"_(s)":"); goto *all_ptrs.s;
static void all_jmps() { REDIR_ALL }
#endif
#undef REDIR
void __bt_init_dll(int bcheck)
{
const char *s = all_names;
void **p = (void**)&all_ptrs;
do {
*p = (void*)GetProcAddress(GetModuleHandle(NULL), (char*)s);
if (NULL == *p) {
char buf[100];
sprintf(buf,
"Error: function '%s()' not found in executable. "
"(Need -bt or -b for linking the exe.)", s);
if (GetStdHandle(STD_ERROR_HANDLE))
fprintf(stderr, "TCC/BCHECK: %s\n", buf), fflush(stderr);
else
MessageBox(NULL, buf, "TCC/BCHECK", MB_ICONERROR);
ExitProcess(1);
}
s = strchr(s,'\0') + 1, ++p;
} while (*s && (bcheck || p < &all_ptrs.__bound_ptr_add));
}

87
lib/bt-exe.c Normal file
View File

@ -0,0 +1,87 @@
/* ------------------------------------------------------------- */
/* for linking rt_printline and the signal/exception handler
from tccrun.c into executables. */
#define CONFIG_TCC_BACKTRACE_ONLY
#define ONE_SOURCE 1
#define pstrcpy tcc_pstrcpy
#include "../tccrun.c"
#ifndef _WIN32
# define __declspec(n)
#endif
#ifdef _WIN64
static void bt_init_pe_prog_base(rt_context *p)
{
MEMORY_BASIC_INFORMATION mbi;
addr_t imagebase;
if (!p->prog_base)
return;
if (!VirtualQuery(p, &mbi, sizeof(mbi)) || !mbi.AllocationBase)
return;
imagebase = (addr_t)mbi.AllocationBase - p->prog_base;
p->prog_base = (addr_t)mbi.AllocationBase - (imagebase & 0xffffffffu);
}
#endif
__declspec(dllexport)
void __bt_init(rt_context *p, int is_exe)
{
__attribute__((weak)) int main();
__attribute__((weak)) void __bound_init(void*, int);
//fprintf(stderr, "__bt_init %d %p %p %p\n", is_exe, p, p->stab_sym, p->bounds_start), fflush(stderr);
/* call __bound_init here due to redirection of sigaction */
/* needed to add global symbols */
if (p->bounds_start)
__bound_init(p->bounds_start, -1);
#ifdef _WIN64
bt_init_pe_prog_base(p);
#endif
/* add to chain */
rt_wait_sem();
p->next = g_rc, g_rc = p;
rt_post_sem();
if (is_exe) {
/* we are the executable (not a dll) */
p->top_func = main;
set_exception_handler();
}
}
__declspec(dllexport)
void __bt_exit(rt_context *p)
{
struct rt_context *rc, **pp;
__attribute__((weak)) void __bound_exit_dll(void*);
//fprintf(stderr, "__bt_exit %d %p\n", !!p->top_func, p);
if (p->bounds_start)
__bound_exit_dll(p->bounds_start);
/* remove from chain */
rt_wait_sem();
for (pp = &g_rc; rc = *pp, rc; pp = &rc->next)
if (rc == p) {
*pp = rc->next;
break;
}
rt_post_sem();
}
/* copy a string and truncate it. */
ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
{
int l = strlen(s);
if (l >= buf_size)
l = buf_size - 1;
memcpy(buf, s, l);
buf[l] = 0;
return buf;
}

56
lib/bt-log.c Normal file
View File

@ -0,0 +1,56 @@
/* ------------------------------------------------------------- */
/* function to get a stack backtrace on demand with a message */
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#undef __attribute__
#ifdef _WIN32
# define DLL_EXPORT __declspec(dllexport)
#else
# define DLL_EXPORT
#endif
/* Needed when using ...libtcc1-usegcc=yes in lib/Makefile */
#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wframe-address"
#endif
typedef struct rt_frame {
void *ip, *fp, *sp;
} rt_frame;
__attribute__((weak))
int _tcc_backtrace(rt_frame *f, const char *fmt, va_list ap);
DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
{
va_list ap;
int ret;
if (_tcc_backtrace) {
rt_frame f;
f.fp = __builtin_frame_address(1);
f.ip = __builtin_return_address(0);
va_start(ap, fmt);
ret = _tcc_backtrace(&f, fmt, ap);
va_end(ap);
} else {
const char *p, *nl = "\n";
if (fmt[0] == '^' && (p = strchr(fmt + 1, fmt[0])))
fmt = p + 1;
if (fmt[0] == '\001')
++fmt, nl = "";
va_start(ap, fmt);
ret = vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "%s", nl), fflush(stderr);
}
return ret;
}
#if (defined(__GNUC__) && (__GNUC__ >= 6)) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

164
lib/builtin.c Normal file
View File

@ -0,0 +1,164 @@
/* uses alias to allow building with gcc/clang */
#ifdef __TINYC__
#define BUILTIN(x) __builtin_##x
#define BUILTINN(x) "__builtin_" # x
#else
#define BUILTIN(x) __tcc_builtin_##x
#define BUILTINN(x) "__tcc_builtin_" # x
#endif
/* ---------------------------------------------- */
/* This file implements:
* __builtin_ffs
* __builtin_clz
* __builtin_ctz
* __builtin_clrsb
* __builtin_popcount
* __builtin_parity
* for int, long and long long
*/
static const unsigned char table_1_32[] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
static const unsigned char table_2_32[32] = {
31, 22, 30, 21, 18, 10, 29, 2, 20, 17, 15, 13, 9, 6, 28, 1,
23, 19, 11, 3, 16, 14, 7, 24, 12, 4, 8, 25, 5, 26, 27, 0
};
static const unsigned char table_1_64[] = {
0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
};
static const unsigned char table_2_64[] = {
63, 16, 62, 7, 15, 36, 61, 3, 6, 14, 22, 26, 35, 47, 60, 2,
9, 5, 28, 11, 13, 21, 42, 19, 25, 31, 34, 40, 46, 52, 59, 1,
17, 8, 37, 4, 23, 27, 48, 10, 29, 12, 43, 20, 32, 41, 53, 18,
38, 24, 49, 30, 44, 33, 54, 39, 50, 45, 55, 51, 56, 57, 58, 0
};
#define FFSI(x) \
return table_1_32[((x & -x) * 0x077cb531u) >> 27] + (x != 0);
#define FFSL(x) \
return table_1_64[((x & -x) * 0x022fdd63cc95386dull) >> 58] + (x != 0);
#define CTZI(x) \
return table_1_32[((x & -x) * 0x077cb531u) >> 27];
#define CTZL(x) \
return table_1_64[((x & -x) * 0x022fdd63cc95386dull) >> 58];
#define CLZI(x) \
x |= x >> 1; \
x |= x >> 2; \
x |= x >> 4; \
x |= x >> 8; \
x |= x >> 16; \
return table_2_32[(x * 0x07c4acddu) >> 27];
#define CLZL(x) \
x |= x >> 1; \
x |= x >> 2; \
x |= x >> 4; \
x |= x >> 8; \
x |= x >> 16; \
x |= x >> 32; \
return table_2_64[x * 0x03f79d71b4cb0a89ull >> 58];
#define POPCOUNTI(x, m) \
x = x - ((x >> 1) & 0x55555555); \
x = (x & 0x33333333) + ((x >> 2) & 0x33333333); \
x = (x + (x >> 4)) & 0xf0f0f0f; \
return ((x * 0x01010101) >> 24) & m;
#define POPCOUNTL(x, m) \
x = x - ((x >> 1) & 0x5555555555555555ull); \
x = (x & 0x3333333333333333ull) + ((x >> 2) & 0x3333333333333333ull); \
x = (x + (x >> 4)) & 0xf0f0f0f0f0f0f0full; \
return ((x * 0x0101010101010101ull) >> 56) & m;
/* Returns one plus the index of the least significant 1-bit of x,
or if x is zero, returns zero. */
int BUILTIN(ffs) (int x) { FFSI(x) }
int BUILTIN(ffsll) (long long x) { FFSL(x) }
#if __SIZEOF_LONG__ == 4
int BUILTIN(ffsl) (long x) __attribute__((alias(BUILTINN(ffs))));
#else
int BUILTIN(ffsl) (long x) __attribute__((alias(BUILTINN(ffsll))));
#endif
/* Returns the number of leading 0-bits in x, starting at the most significant
bit position. If x is 0, the result is undefined. */
int BUILTIN(clz) (unsigned int x) { CLZI(x) }
int BUILTIN(clzll) (unsigned long long x) { CLZL(x) }
#if __SIZEOF_LONG__ == 4
int BUILTIN(clzl) (unsigned long x) __attribute__((alias(BUILTINN(clz))));
#else
int BUILTIN(clzl) (unsigned long x) __attribute__((alias(BUILTINN(clzll))));
#endif
/* Returns the number of trailing 0-bits in x, starting at the least
significant bit position. If x is 0, the result is undefined. */
int BUILTIN(ctz) (unsigned int x) { CTZI(x) }
int BUILTIN(ctzll) (unsigned long long x) { CTZL(x) }
#if __SIZEOF_LONG__ == 4
int BUILTIN(ctzl) (unsigned long x) __attribute__((alias(BUILTINN(ctz))));
#else
int BUILTIN(ctzl) (unsigned long x) __attribute__((alias(BUILTINN(ctzll))));
#endif
/* Returns the number of leading redundant sign bits in x, i.e. the number
of bits following the most significant bit that are identical to it.
There are no special cases for 0 or other values. */
int BUILTIN(clrsb) (int x) { if (x < 0) x = ~x; x <<= 1; CLZI(x) }
int BUILTIN(clrsbll) (long long x) { if (x < 0) x = ~x; x <<= 1; CLZL(x) }
#if __SIZEOF_LONG__ == 4
int BUILTIN(clrsbl) (long x) __attribute__((alias(BUILTINN(clrsb))));
#else
int BUILTIN(clrsbl) (long x) __attribute__((alias(BUILTINN(clrsbll))));
#endif
/* Returns the number of 1-bits in x.*/
int BUILTIN(popcount) (unsigned int x) { POPCOUNTI(x, 0x3f) }
int BUILTIN(popcountll) (unsigned long long x) { POPCOUNTL(x, 0x7f) }
#if __SIZEOF_LONG__ == 4
int BUILTIN(popcountl) (unsigned long x) __attribute__((alias(BUILTINN(popcount))));
#else
int BUILTIN(popcountl ) (unsigned long x) __attribute__((alias(BUILTINN(popcountll))));
#endif
/* Returns the parity of x, i.e. the number of 1-bits in x modulo 2. */
int BUILTIN(parity) (unsigned int x) { POPCOUNTI(x, 0x01) }
int BUILTIN(parityll) (unsigned long long x) { POPCOUNTL(x, 0x01) }
#if __SIZEOF_LONG__ == 4
int BUILTIN(parityl) (unsigned long x) __attribute__((alias(BUILTINN(parity))));
#else
int BUILTIN(parityl) (unsigned long x) __attribute__((alias(BUILTINN(parityll))));
#endif
#ifndef __TINYC__
#if defined(__GNUC__) && (__GNUC__ >= 6)
/* gcc overrides alias from __builtin_ffs... to ffs.. so use assembly code */
__asm__(".globl __builtin_ffs");
__asm__(".set __builtin_ffs,__tcc_builtin_ffs");
__asm__(".globl __builtin_ffsl");
__asm__(".set __builtin_ffsl,__tcc_builtin_ffsl");
__asm__(".globl __builtin_ffsll");
__asm__(".set __builtin_ffsll,__tcc_builtin_ffsll");
#else
int __builtin_ffs(int x) __attribute__((alias("__tcc_builtin_ffs")));
int __builtin_ffsl(long x) __attribute__((alias("__tcc_builtin_ffsl")));
int __builtin_ffsll(long long x) __attribute__((alias("__tcc_builtin_ffsll")));
#endif
int __builtin_clz(unsigned int x) __attribute__((alias("__tcc_builtin_clz")));
int __builtin_clzl(unsigned long x) __attribute__((alias("__tcc_builtin_clzl")));
int __builtin_clzll(unsigned long long x) __attribute__((alias("__tcc_builtin_clzll")));
int __builtin_ctz(unsigned int x) __attribute__((alias("__tcc_builtin_ctz")));
int __builtin_ctzl(unsigned long x) __attribute__((alias("__tcc_builtin_ctzl")));
int __builtin_ctzll(unsigned long long x) __attribute__((alias("__tcc_builtin_ctzll")));
int __builtin_clrsb(int x) __attribute__((alias("__tcc_builtin_clrsb")));
int __builtin_clrsbl(long x) __attribute__((alias("__tcc_builtin_clrsbl")));
int __builtin_clrsbll(long long x) __attribute__((alias("__tcc_builtin_clrsbll")));
int __builtin_popcount(unsigned int x) __attribute__((alias("__tcc_builtin_popcount")));
int __builtin_popcountl(unsigned long x) __attribute__((alias("__tcc_builtin_popcountl")));
int __builtin_popcountll(unsigned long long x) __attribute__((alias("__tcc_builtin_popcountll")));
int __builtin_parity(unsigned int x) __attribute__((alias("__tcc_builtin_parity")));
int __builtin_parityl(unsigned long x) __attribute__((alias("__tcc_builtin_parityl")));
int __builtin_parityll(unsigned long long x) __attribute__((alias("__tcc_builtin_parityll")));
#endif

1
lib/dsohandle.c Normal file
View File

@ -0,0 +1 @@
void * __dso_handle __attribute((visibility("hidden"))) = &__dso_handle;

View File

@ -18,40 +18,40 @@ typedef int int32_t;
typedef unsigned uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;
void *memcpy(void*,void*,__SIZE_TYPE__);
#else
#include <stdint.h>
#include <string.h>
#endif
void __clear_cache(void *beg, void *end)
{
__arm64_clear_cache(beg, end);
}
typedef struct {
uint64_t x0, x1;
typedef union {
struct { uint64_t x0, x1; };
long double f;
} u128_t;
typedef union {
uint64_t x;
double f;
} u64_t;
typedef union {
uint32_t x;
float f;
} u32_t;
static long double f3_zero(int sgn)
{
long double f;
u128_t x = { 0, (uint64_t)sgn << 63 };
memcpy(&f, &x, 16);
return f;
return x.f;
}
static long double f3_infinity(int sgn)
{
long double f;
u128_t x = { 0, (uint64_t)sgn << 63 | 0x7fff000000000000 };
memcpy(&f, &x, 16);
return f;
return x.f;
}
static long double f3_NaN(void)
{
long double f;
#if 0
// ARM's default NaN usually has just the top fraction bit set:
u128_t x = { 0, 0x7fff800000000000 };
@ -59,28 +59,31 @@ static long double f3_NaN(void)
// GCC's library sets all fraction bits:
u128_t x = { -1, 0x7fffffffffffffff };
#endif
memcpy(&f, &x, 16);
return f;
return x.f;
}
static int fp3_convert_NaN(long double *f, int sgn, u128_t mnt)
static int fp3_convert_NaN(long double *f, int sgn, u128_t *mnt)
{
u128_t x = { mnt.x0,
mnt.x1 | 0x7fff800000000000 | (uint64_t)sgn << 63 };
memcpy(f, &x, 16);
u128_t x = { mnt->x0,
mnt->x1 | 0x7fff800000000000 | (uint64_t)sgn << 63 };
*f = x.f;
return 1;
#define fp3_convert_NaN(a,b,c) fp3_convert_NaN(a,b,&c)
}
static int fp3_detect_NaNs(long double *f,
int a_sgn, int a_exp, u128_t a,
int b_sgn, int b_exp, u128_t b)
int a_sgn, int a_exp, u128_t *a,
int b_sgn, int b_exp, u128_t *b)
#define a (*a)
#define b (*b)
{
#if 0
// Detect signalling NaNs:
if (a_exp == 32767 && (a.x0 | a.x1 << 16) && !(a.x1 >> 47 & 1))
return fp3_convert_NaN(f, a_sgn, a);
if (b_exp == 32767 && (b.x0 | b.x1 << 16) && !(b.x1 >> 47 & 1))
return fp3_convert_NaN(f, b_sgn, b);
#endif
// Detect quiet NaNs:
if (a_exp == 32767 && (a.x0 | a.x1 << 16))
return fp3_convert_NaN(f, a_sgn, a);
@ -88,12 +91,16 @@ static int fp3_detect_NaNs(long double *f,
return fp3_convert_NaN(f, b_sgn, b);
return 0;
#undef a
#undef b
#define fp3_detect_NaNs(a,b,c,d,e,f,g) fp3_detect_NaNs(a,b,c,&d,e,f,&g)
}
static void f3_unpack(int *sgn, int32_t *exp, u128_t *mnt, long double f)
{
u128_t x;
memcpy(&x, &f, 16);
x.f = f;
*sgn = x.x1 >> 63;
*exp = x.x1 >> 48 & 32767;
x.x1 = x.x1 << 16 >> 16;
@ -101,74 +108,72 @@ static void f3_unpack(int *sgn, int32_t *exp, u128_t *mnt, long double f)
x.x1 |= (uint64_t)1 << 48;
else
*exp = 1;
*mnt = x;
mnt->f = x.f;
}
static u128_t f3_normalise(int32_t *exp, u128_t mnt)
static void f3_normalise(int32_t *exp, u128_t *mnt)
{
int sh;
if (!(mnt.x0 | mnt.x1))
return mnt;
if (!mnt.x1) {
mnt.x1 = mnt.x0;
mnt.x0 = 0;
if (!(mnt->x0 | mnt->x1))
return;
if (!mnt->x1) {
mnt->x1 = mnt->x0;
mnt->x0 = 0;
*exp -= 64;
}
for (sh = 32; sh; sh >>= 1) {
if (!(mnt.x1 >> (64 - sh))) {
mnt.x1 = mnt.x1 << sh | mnt.x0 >> (64 - sh);
mnt.x0 = mnt.x0 << sh;
if (!(mnt->x1 >> (64 - sh))) {
mnt->x1 = mnt->x1 << sh | mnt->x0 >> (64 - sh);
mnt->x0 = mnt->x0 << sh;
*exp -= sh;
}
}
return mnt;
}
static u128_t f3_sticky_shift(int32_t sh, u128_t x)
static void f3_sticky_shift(int32_t sh, u128_t *x)
{
if (sh >= 128) {
x.x0 = !!(x.x0 | x.x1);
x.x1 = 0;
return x;
x->x0 = !!(x->x0 | x->x1);
x->x1 = 0;
return;
}
if (sh >= 64) {
x.x0 = x.x1 | !!x.x0;
x.x1 = 0;
x->x0 = x->x1 | !!x->x0;
x->x1 = 0;
sh -= 64;
}
if (sh > 0) {
x.x0 = x.x0 >> sh | x.x1 << (64 - sh) | !!(x.x0 << (64 - sh));
x.x1 = x.x1 >> sh;
x->x0 = x->x0 >> sh | x->x1 << (64 - sh) | !!(x->x0 << (64 - sh));
x->x1 = x->x1 >> sh;
}
return x;
}
static long double f3_round(int sgn, int32_t exp, u128_t x)
static long double f3_round(int sgn, int32_t exp, u128_t *x)
{
long double f;
int error;
if (exp > 0) {
x = f3_sticky_shift(13, x);
f3_sticky_shift(13, x);
}
else {
x = f3_sticky_shift(14 - exp, x);
f3_sticky_shift(14 - exp, x);
exp = 0;
}
error = x.x0 & 3;
x.x0 = x.x0 >> 2 | x.x1 << 62;
x.x1 = x.x1 >> 2;
error = x->x0 & 3;
x->x0 = x->x0 >> 2 | x->x1 << 62;
x->x1 = x->x1 >> 2;
if (error == 3 || ((error == 2) & (x.x0 & 1))) {
if (!++x.x0) {
++x.x1;
if (x.x1 == (uint64_t)1 << 48)
if (error == 3 || ((error == 2) & (x->x0 & 1))) {
if (!++x->x0) {
++x->x1;
if (x->x1 == (uint64_t)1 << 48)
exp = 1;
else if (x.x1 == (uint64_t)1 << 49) {
else if (x->x1 == (uint64_t)1 << 49) {
++exp;
x.x0 = x.x0 >> 1 | x.x1 << 63;
x.x1 = x.x1 >> 1;
x->x0 = x->x0 >> 1 | x->x1 << 63;
x->x1 = x->x1 >> 1;
}
}
}
@ -176,9 +181,8 @@ static long double f3_round(int sgn, int32_t exp, u128_t x)
if (exp >= 32767)
return f3_infinity(sgn);
x.x1 = x.x1 << 16 >> 16 | (uint64_t)exp << 48 | (uint64_t)sgn << 63;
memcpy(&f, &x, 16);
return f;
x->x1 = x->x1 << 16 >> 16 | (uint64_t)exp << 48 | (uint64_t)sgn << 63;
return x->f;
}
static long double f3_add(long double fa, long double fb, int neg)
@ -212,11 +216,11 @@ static long double f3_add(long double fa, long double fb, int neg)
b.x0 = b.x0 << 3;
if (a_exp <= b_exp) {
a = f3_sticky_shift(b_exp - a_exp, a);
f3_sticky_shift(b_exp - a_exp, &a);
a_exp = b_exp;
}
else {
b = f3_sticky_shift(a_exp - b_exp, b);
f3_sticky_shift(a_exp - b_exp, &b);
b_exp = a_exp;
}
@ -239,9 +243,9 @@ static long double f3_add(long double fa, long double fb, int neg)
if (!(x.x0 | x.x1))
return f3_zero(0);
x = f3_normalise(&x_exp, x);
f3_normalise(&x_exp, &x);
return f3_round(x_sgn, x_exp + 12, x);
return f3_round(x_sgn, x_exp + 12, &x);
}
long double __addtf3(long double a, long double b)
@ -276,8 +280,8 @@ long double __multf3(long double fa, long double fb)
if (!(a.x0 | a.x1) || !(b.x0 | b.x1))
return f3_zero(a_sgn ^ b_sgn);
a = f3_normalise(&a_exp, a);
b = f3_normalise(&b_exp, b);
f3_normalise(&a_exp, &a);
f3_normalise(&b_exp, &b);
x_sgn = a_sgn ^ b_sgn;
x_exp = a_exp + b_exp - 16352;
@ -316,7 +320,7 @@ long double __multf3(long double fa, long double fb)
x.x1 = y1;
}
return f3_round(x_sgn, x_exp, x);
return f3_round(x_sgn, x_exp, &x);
}
long double __divtf3(long double fa, long double fb)
@ -341,8 +345,8 @@ long double __divtf3(long double fa, long double fb)
if (!(a.x0 | a.x1) || b_exp == 32767)
return f3_zero(a_sgn ^ b_sgn);
a = f3_normalise(&a_exp, a);
b = f3_normalise(&b_exp, b);
f3_normalise(&a_exp, &a);
f3_normalise(&b_exp, &b);
x_sgn = a_sgn ^ b_sgn;
x_exp = a_exp - b_exp + 16395;
@ -366,48 +370,67 @@ long double __divtf3(long double fa, long double fb)
}
x.x0 |= !!(a.x0 | a.x1);
x = f3_normalise(&x_exp, x);
f3_normalise(&x_exp, &x);
return f3_round(x_sgn, x_exp, x);
return f3_round(x_sgn, x_exp, &x);
}
long double __negtf2(long double f)
{
((u128_t*)&f)->x1 ^= 1UL << 63;
return f;
}
long double __extendsftf2(float f)
{
long double fx;
u128_t x;
u32_t u;
uint32_t a;
uint64_t aa;
memcpy(&a, &f, 4);
u.f = f, a = u.x;
aa = a;
x.x0 = 0;
if (!(a << 1))
x.x1 = aa << 32;
else if (a << 1 >> 24 == 255)
x.x1 = (0x7fff000000000000 | aa >> 31 << 63 | aa << 41 >> 16 |
(uint64_t)!!(a << 9) << 47);
else
else if (a << 1 >> 24 == 0) {
uint64_t adj = 0;
while (!(a << 1 >> 1 >> (23 - adj)))
adj++;
x.x1 = aa >> 31 << 63 | (16256 - adj + 1) << 48 | aa << adj << 41 >> 16;
} else
x.x1 = (aa >> 31 << 63 | ((aa >> 23 & 255) + 16256) << 48 |
aa << 41 >> 16);
memcpy(&fx, &x, 16);
return fx;
return x.f;
}
long double __extenddftf2(double f)
{
long double fx;
u128_t x;
u64_t u;
uint64_t a;
memcpy(&a, &f, 8);
u.f = f, a = u.x;
x.x0 = a << 60;
if (!(a << 1))
x.x1 = a;
else if (a << 1 >> 53 == 2047)
x.x1 = (0x7fff000000000000 | a >> 63 << 63 | a << 12 >> 16 |
(uint64_t)!!(a << 12) << 47);
else
else if (a << 1 >> 53 == 0) {
uint64_t adj = 0;
while (!(a << 1 >> 1 >> (52 - adj)))
adj++;
x.x0 <<= adj;
x.x1 = a >> 63 << 63 | (15360 - adj + 1) << 48 | a << adj << 12 >> 16;
} else
x.x1 = a >> 63 << 63 | ((a >> 52 & 2047) + 15360) << 48 | a << 12 >> 16;
memcpy(&fx, &x, 16);
return fx;
return x.f;
}
float __trunctfsf2(long double f)
@ -415,11 +438,10 @@ float __trunctfsf2(long double f)
u128_t mnt;
int32_t exp;
int sgn;
uint32_t x;
float fx;
u32_t x;
#define x x.x
f3_unpack(&sgn, &exp, &mnt, f);
if (exp == 32767 && (mnt.x0 | mnt.x1 << 16))
x = 0x7fc00000 | (uint32_t)sgn << 31 | (mnt.x1 >> 25 & 0x007fffff);
else if (exp > 16510)
@ -437,8 +459,8 @@ float __trunctfsf2(long double f)
x += 4;
x = ((x >> 2) + (exp << 23)) | (uint32_t)sgn << 31;
}
memcpy(&fx, &x, 4);
return fx;
#undef x
return x.f;
}
double __trunctfdf2(long double f)
@ -446,11 +468,10 @@ double __trunctfdf2(long double f)
u128_t mnt;
int32_t exp;
int sgn;
uint64_t x;
double fx;
u64_t x;
#define x x.x
f3_unpack(&sgn, &exp, &mnt, f);
if (exp == 32767 && (mnt.x0 | mnt.x1 << 16))
x = (0x7ff8000000000000 | (uint64_t)sgn << 63 |
mnt.x1 << 16 >> 12 | mnt.x0 >> 60);
@ -469,8 +490,8 @@ double __trunctfdf2(long double f)
x += 4;
x = ((x >> 2) + ((uint64_t)exp << 52)) | (uint64_t)sgn << 63;
}
memcpy(&fx, &x, 8);
return fx;
#undef x
return x.f;
}
int32_t __fixtfsi(long double fa)
@ -535,7 +556,6 @@ long double __floatsitf(int32_t a)
int exp = 16414;
uint32_t mnt = a;
u128_t x = { 0, 0 };
long double f;
int i;
if (a) {
if (a < 0) {
@ -550,8 +570,7 @@ long double __floatsitf(int32_t a)
x.x1 = ((uint64_t)sgn << 63 | (uint64_t)exp << 48 |
(uint64_t)(mnt << 1) << 16);
}
memcpy(&f, &x, 16);
return f;
return x.f;
}
long double __floatditf(int64_t a)
@ -560,7 +579,6 @@ long double __floatditf(int64_t a)
int exp = 16446;
uint64_t mnt = a;
u128_t x = { 0, 0 };
long double f;
int i;
if (a) {
if (a < 0) {
@ -575,8 +593,7 @@ long double __floatditf(int64_t a)
x.x0 = mnt << 49;
x.x1 = (uint64_t)sgn << 63 | (uint64_t)exp << 48 | mnt << 1 >> 16;
}
memcpy(&f, &x, 16);
return f;
return x.f;
}
long double __floatunsitf(uint32_t a)
@ -584,7 +601,6 @@ long double __floatunsitf(uint32_t a)
int exp = 16414;
uint32_t mnt = a;
u128_t x = { 0, 0 };
long double f;
int i;
if (a) {
for (i = 16; i; i >>= 1)
@ -594,8 +610,7 @@ long double __floatunsitf(uint32_t a)
}
x.x1 = (uint64_t)exp << 48 | (uint64_t)(mnt << 1) << 16;
}
memcpy(&f, &x, 16);
return f;
return x.f;
}
long double __floatunditf(uint64_t a)
@ -614,15 +629,14 @@ long double __floatunditf(uint64_t a)
x.x0 = mnt << 49;
x.x1 = (uint64_t)exp << 48 | mnt << 1 >> 16;
}
memcpy(&f, &x, 16);
return f;
return x.f;
}
static int f3_cmp(long double fa, long double fb)
{
u128_t a, b;
memcpy(&a, &fa, 16);
memcpy(&b, &fb, 16);
a.f = fa;
b.f = fb;
return (!(a.x0 | a.x1 << 1 | b.x0 | b.x1 << 1) ? 0 :
((a.x1 << 1 >> 49 == 0x7fff && (a.x0 | a.x1 << 16)) ||
(b.x1 << 1 >> 49 == 0x7fff && (b.x0 | b.x1 << 16))) ? 2 :

20
lib/lib-riscv.c Normal file
View File

@ -0,0 +1,20 @@
/*
* TCC runtime library for riscv64.
*
* Copyright (c) 2026
*
* Copying and distribution of this file, with or without modification,
* are permitted in any medium without royalty provided the copyright
* notice and this notice are preserved. This file is offered as-is,
* without any warranty.
*/
/* ------------------------------------------------------------- */
/* __clear_cache is used in tccrun.c. It is a built-in
intrinsic with gcc. However tcc in order to compile
itself needs this function */
void __clear_cache(void *beg, void *end)
{
__riscv64_clear_cache(beg, end);
}

View File

@ -107,10 +107,9 @@ union float_long {
};
/* XXX: we don't support several builtin supports for now */
#if !defined __x86_64__ && !defined __arm__
#if defined __i386__
/* XXX: use gcc/tcc intrinsic ? */
#if defined __i386__
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
__asm__ ("subl %5,%1\n\tsbbl %3,%0" \
: "=r" ((USItype) (sh)), \
@ -139,9 +138,6 @@ union float_long {
: "=r" (__cbtmp) : "rm" ((USItype) (x))); \
(count) = __cbtmp ^ 31; \
} while (0)
#else
#error unsupported CPU type
#endif
/* most of this code is taken from libgcc2.c from gcc */
@ -478,7 +474,7 @@ long long __ashldi3(long long a, int b)
#endif
}
#endif /* !__x86_64__ */
#endif /* __i386__ */
/* XXX: fix tcc's code generator to do this instead */
float __floatundisf(unsigned long long a)
@ -530,7 +526,7 @@ unsigned long long __fixunssfdi (float a1)
{
register union float_long fl1;
register int exp;
register unsigned long l;
register unsigned long long l;
fl1.f = a1;
@ -538,16 +534,19 @@ unsigned long long __fixunssfdi (float a1)
return (0);
exp = EXP (fl1.l) - EXCESS - 24;
l = MANT(fl1.l);
if (exp >= 41)
return (unsigned long long)-1;
return 1ULL << 63;
else if (exp >= 0)
return (unsigned long long)l << exp;
l <<= exp;
else if (exp >= -23)
return l >> -exp;
l >>= -exp;
else
return 0;
if (SIGN(fl1.l))
l = (unsigned long long)-l;
return l;
}
long long __fixsfdi (float a1)
@ -569,17 +568,19 @@ unsigned long long __fixunsdfdi (double a1)
return (0);
exp = EXPD (dl1) - EXCESSD - 53;
l = MANTD_LL(dl1);
if (exp >= 12)
return (unsigned long long)-1;
return 1ULL << 63; /* overflow result (like gcc, somewhat) */
else if (exp >= 0)
return l << exp;
l <<= exp;
else if (exp >= -52)
return l >> -exp;
l >>= -exp;
else
return 0;
if (SIGND(dl1))
l = (unsigned long long)-l;
return l;
}
long long __fixdfdi (double a1)
@ -602,15 +603,15 @@ unsigned long long __fixunsxfdi (long double a1)
return (0);
exp = EXPLD (dl1) - EXCESSLD - 64;
l = dl1.l.lower;
if (exp > 0)
return (unsigned long long)-1;
else if (exp >= -63)
return l >> -exp;
else
return 1ULL << 63;
if (exp < -63)
return 0;
l >>= -exp;
if (SIGNLD(dl1))
l = (unsigned long long)-l;
return l;
}
long long __fixxfdi (long double a1)

39
lib/pic86.S Normal file
View File

@ -0,0 +1,39 @@
/* ---------------------------------------------- */
/* get_pc_thunk.S */
#ifdef __leading_underscore
# define _(s) _##s
#else
# define _(s) s
#endif
/* ---------------------------------------------- */
.text
.globl _(__x86.get_pc_thunk.ax)
.hidden _(__x86.get_pc_thunk.ax)
_(__x86.get_pc_thunk.ax):
mov (%esp),%eax
ret
.size _(__x86.get_pc_thunk.ax), .-_(__x86.get_pc_thunk.ax)
.globl _(__x86.get_pc_thunk.bx)
.hidden _(__x86.get_pc_thunk.bx)
_(__x86.get_pc_thunk.bx):
mov (%esp),%ebx
ret
.size _(__x86.get_pc_thunk.bx), .-_(__x86.get_pc_thunk.bx)
.globl _(__x86.get_pc_thunk.cx)
.hidden _(__x86.get_pc_thunk.cx)
_(__x86.get_pc_thunk.cx):
mov (%esp),%ecx
ret
.size _(__x86.get_pc_thunk.cx), .-_(__x86.get_pc_thunk.cx)
.globl _(__x86.get_pc_thunk.dx)
.hidden _(__x86.get_pc_thunk.dx)
_(__x86.get_pc_thunk.dx):
mov (%esp),%edx
ret
.size _(__x86.get_pc_thunk.dx), .-_(__x86.get_pc_thunk.dx)

86
lib/runmain.c Normal file
View File

@ -0,0 +1,86 @@
/* ------------------------------------------------------------- */
/* support for tcc_run() */
#ifdef __leading_underscore
# define _(s) s
#else
# define _(s) _##s
#endif
#ifndef _WIN32
extern void (*_(_init_array_start)[]) (int argc, char **argv, char **envp);
extern void (*_(_init_array_end)[]) (int argc, char **argv, char **envp);
static void run_ctors(int argc, char **argv, char **env)
{
int i = 0;
while (&_(_init_array_start)[i] != _(_init_array_end))
(*_(_init_array_start)[i++])(argc, argv, env);
}
#endif
extern void (*_(_fini_array_start)[]) (void);
extern void (*_(_fini_array_end)[]) (void);
static void run_dtors(void)
{
int i = 0;
while (&_(_fini_array_end)[i] != _(_fini_array_start))
(*_(_fini_array_end)[--i])();
}
static void *rt_exitfunc[32];
static void *rt_exitarg[32];
static int __rt_nr_exit;
void __run_on_exit(int ret)
{
int n = __rt_nr_exit;
while (n)
--n, ((void(*)(int,void*))rt_exitfunc[n])(ret, rt_exitarg[n]);
}
int on_exit(void *function, void *arg)
{
int n = __rt_nr_exit;
if (n < 32) {
rt_exitfunc[n] = function;
rt_exitarg[n] = arg;
__rt_nr_exit = n + 1;
return 0;
}
return 1;
}
int atexit(void (*function)(void))
{
return on_exit(function, 0);
}
typedef struct rt_frame {
void *ip, *fp, *sp;
} rt_frame;
__attribute__((noreturn)) void __rt_exit(rt_frame *, int);
void exit(int code)
{
rt_frame f;
run_dtors();
__run_on_exit(code);
f.fp = 0;
f.ip = exit;
__rt_exit(&f, code);
}
#ifndef _WIN32
int main(int, char**, char**);
int _runmain(int argc, char **argv, char **envp)
{
int ret;
run_ctors(argc, argv, envp);
ret = main(argc, argv, envp);
run_dtors();
__run_on_exit(ret);
return ret;
}
#endif

104
lib/stdatomic.c Normal file
View File

@ -0,0 +1,104 @@
// for libtcc1, avoid including files that are not part of tcc
// #include <stdint.h>
#define uint8_t unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int
#define uint64_t unsigned long long
#define bool _Bool
#define false 0
#define true 1
#define __ATOMIC_RELAXED 0
#define __ATOMIC_CONSUME 1
#define __ATOMIC_ACQUIRE 2
#define __ATOMIC_RELEASE 3
#define __ATOMIC_ACQ_REL 4
#define __ATOMIC_SEQ_CST 5
typedef __SIZE_TYPE__ size_t;
#define ATOMIC_GEN_OP(TYPE, MODE, NAME, OP, RET) \
TYPE __atomic_##NAME##_##MODE(volatile void *atom, TYPE value, int memorder) \
{ \
TYPE xchg, cmp; \
__atomic_load((TYPE *)atom, (TYPE *)&cmp, __ATOMIC_RELAXED); \
do { \
xchg = (OP); \
} while (!__atomic_compare_exchange((TYPE *)atom, &cmp, &xchg, true, \
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); \
return RET; \
}
#define ATOMIC_EXCHANGE(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, exchange, value, cmp)
#define ATOMIC_ADD_FETCH(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, add_fetch, (cmp + value), xchg)
#define ATOMIC_SUB_FETCH(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, sub_fetch, (cmp - value), xchg)
#define ATOMIC_AND_FETCH(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, and_fetch, (cmp & value), xchg)
#define ATOMIC_OR_FETCH(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, or_fetch, (cmp | value), xchg)
#define ATOMIC_XOR_FETCH(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, xor_fetch, (cmp ^ value), xchg)
#define ATOMIC_NAND_FETCH(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, nand_fetch, ~(cmp & value), xchg)
#define ATOMIC_FETCH_ADD(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, fetch_add, (cmp + value), cmp)
#define ATOMIC_FETCH_SUB(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, fetch_sub, (cmp - value), cmp)
#define ATOMIC_FETCH_AND(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, fetch_and, (cmp & value), cmp)
#define ATOMIC_FETCH_OR(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, fetch_or, (cmp | value), cmp)
#define ATOMIC_FETCH_XOR(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, fetch_xor, (cmp ^ value), cmp)
#define ATOMIC_FETCH_NAND(TYPE, MODE) \
ATOMIC_GEN_OP(TYPE, MODE, fetch_nand, ~(cmp & value), cmp)
#define ATOMIC_GEN(TYPE, SIZE) \
ATOMIC_EXCHANGE(TYPE, SIZE) \
ATOMIC_ADD_FETCH(TYPE, SIZE) \
ATOMIC_SUB_FETCH(TYPE, SIZE) \
ATOMIC_AND_FETCH(TYPE, SIZE) \
ATOMIC_OR_FETCH(TYPE, SIZE) \
ATOMIC_XOR_FETCH(TYPE, SIZE) \
ATOMIC_NAND_FETCH(TYPE, SIZE) \
ATOMIC_FETCH_ADD(TYPE, SIZE) \
ATOMIC_FETCH_SUB(TYPE, SIZE) \
ATOMIC_FETCH_AND(TYPE, SIZE) \
ATOMIC_FETCH_OR(TYPE, SIZE) \
ATOMIC_FETCH_XOR(TYPE, SIZE) \
ATOMIC_FETCH_NAND(TYPE, SIZE)
ATOMIC_GEN(uint8_t, 1)
ATOMIC_GEN(uint16_t, 2)
ATOMIC_GEN(uint32_t, 4)
ATOMIC_GEN(uint64_t, 8)
/* uses alias to allow building with gcc/clang */
#ifdef __TINYC__
#define ATOMIC(x) __atomic_##x
#else
#define ATOMIC(x) __tcc_atomic_##x
#endif
bool ATOMIC(is_lock_free) (unsigned long size, const volatile void *ptr)
{
bool ret;
switch (size) {
case 1: ret = true; break;
case 2: ret = true; break;
case 4: ret = true; break;
#if defined __x86_64__ || defined __aarch64__ || defined __riscv
case 8: ret = true; break;
#else
case 8: ret = false; break;
#endif
default: ret = false; break;
}
return ret;
}
#ifndef __TINYC__
bool __atomic_is_lock_free(unsigned long size, const volatile void *ptr) __attribute__((alias("__tcc_atomic_is_lock_free")));
#endif

428
lib/tcov.c Normal file
View File

@ -0,0 +1,428 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#ifndef _WIN32
#include <unistd.h>
#include <errno.h>
#else
#include <windows.h>
#include <io.h>
#endif
/* section layout (all little endian):
32bit offset to executable/so file name
filename \0
function name \0
align to 64 bits
64bit function start line
64bits end_line(28bits) / start_line(28bits) / flag=0xff(8bits)
64bits counter
\0
\0
\0
executable/so file name \0
*/
typedef struct tcov_line {
unsigned int fline;
unsigned int lline;
unsigned long long count;
} tcov_line;
typedef struct tcov_function {
char *function;
unsigned int first_line;
unsigned int n_line;
unsigned int m_line;
tcov_line *line;
} tcov_function;
typedef struct tcov_file {
char *filename;
unsigned int n_func;
unsigned int m_func;
tcov_function *func;
struct tcov_file *next;
} tcov_file;
static FILE *open_tcov_file (char *cov_filename)
{
int fd;
#ifndef _WIN32
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0; /* Until EOF. */
lock.l_pid = getpid ();
#endif
fd = open (cov_filename, O_RDWR | O_CREAT, 0666);
if (fd < 0)
return NULL;
#ifndef _WIN32
while (fcntl (fd, F_SETLKW, &lock) && errno == EINTR)
continue;
#else
{
OVERLAPPED overlapped = { 0 };
LockFileEx((HANDLE)_get_osfhandle(fd), LOCKFILE_EXCLUSIVE_LOCK,
0, 1, 0, &overlapped);
}
#endif
return fdopen (fd, "r+");
}
static unsigned long long get_value(unsigned char *p, int size)
{
unsigned long long value = 0;
p += size;
while (size--)
value = (value << 8) | *--p;
return value;
}
static int sort_func (const void *p, const void *q)
{
const tcov_function *pp = (const tcov_function *) p;
const tcov_function *pq = (const tcov_function *) q;
return pp->first_line > pq->first_line ? 1 :
pp->first_line < pq->first_line ? -1 : 0;
}
static int sort_line (const void *p, const void *q)
{
const tcov_line *pp = (const tcov_line *) p;
const tcov_line *pq = (const tcov_line *) q;
return pp->fline > pq->fline ? 1 :
pp->fline < pq->fline ? -1 :
pp->count < pq->count ? 1 :
pp->count > pq->count ? -1 : 0;
}
/* sort to let inline functions work */
static tcov_file *sort_test_coverage (unsigned char *p)
{
int i, j, k;
unsigned char *start = p;
tcov_file *file = NULL;
tcov_file *nfile;
p += 4;
while (*p) {
char *filename = (char *)p;
size_t len = strlen (filename);
nfile = file;
while (nfile) {
if (strcmp (nfile->filename, filename) == 0)
break;
nfile = nfile->next;
}
if (nfile == NULL) {
nfile = malloc (sizeof(tcov_file));
if (nfile == NULL) {
fprintf (stderr, "Malloc error test_coverage\n");
return file;
}
nfile->filename = filename;
nfile->n_func = 0;
nfile->m_func = 0;
nfile->func = NULL;
nfile->next = NULL;
if (file == NULL)
file = nfile;
else {
tcov_file *lfile = file;
while (lfile->next)
lfile = lfile->next;
lfile->next = nfile;
}
}
p += len + 1;
while (*p) {
int i;
char *function = (char *)p;
tcov_function *func;
p += strlen (function) + 1;
p += -(p - start) & 7;
for (i = 0; i < nfile->n_func; i++) {
func = &nfile->func[i];
if (strcmp (func->function, function) == 0)
break;
}
if (i == nfile->n_func) {
if (nfile->n_func >= nfile->m_func) {
nfile->m_func = nfile->m_func == 0 ? 4 : nfile->m_func * 2;
nfile->func = realloc (nfile->func,
nfile->m_func *
sizeof (tcov_function));
if (nfile->func == NULL) {
fprintf (stderr, "Realloc error test_coverage\n");
return file;
}
}
func = &nfile->func[nfile->n_func++];
func->function = function;
func->first_line = get_value (p, 8);
func->n_line = 0;
func->m_line = 0;
func->line = NULL;
}
p += 8;
while (*p) {
tcov_line *line;
unsigned long long val;
if (func->n_line >= func->m_line) {
func->m_line = func->m_line == 0 ? 4 : func->m_line * 2;
func->line = realloc (func->line,
func->m_line * sizeof (tcov_line));
if (func->line == NULL) {
fprintf (stderr, "Realloc error test_coverage\n");
return file;
}
}
line = &func->line[func->n_line++];
val = get_value (p, 8);
line->fline = (val >> 8) & 0xfffffffULL;
line->lline = val >> 36;
line->count = get_value (p + 8, 8);
p += 16;
}
p++;
}
p++;
}
nfile = file;
while (nfile) {
qsort (nfile->func, nfile->n_func, sizeof (tcov_function), sort_func);
for (i = 0; i < nfile->n_func; i++) {
tcov_function *func = &nfile->func[i];
qsort (func->line, func->n_line, sizeof (tcov_line), sort_line);
}
nfile = nfile->next;
}
return file;
}
/* merge with previous tcov file */
static void merge_test_coverage (tcov_file *file, FILE *fp,
unsigned int *pruns)
{
unsigned int runs;
char *p;
char str[10000];
*pruns = 1;
if (fp == NULL)
return;
if (fgets(str, sizeof(str), fp) &&
(p = strrchr (str, ':')) &&
(sscanf (p + 1, "%u", &runs) == 1))
*pruns = runs + 1;
while (file) {
int i;
size_t len = strlen (file->filename);
while (fgets(str, sizeof(str), fp) &&
(p = strstr(str, "0:File:")) == NULL) {}
if ((p = strstr(str, "0:File:")) == NULL ||
strncmp (p + strlen("0:File:"), file->filename, len) != 0 ||
p[strlen("0:File:") + len] != ' ')
break;
for (i = 0; i < file->n_func; i++) {
int j;
tcov_function *func = &file->func[i];
unsigned int next_zero = 0;
unsigned int curline = 0;
for (j = 0; j < func->n_line; j++) {
tcov_line *line = &func->line[j];
unsigned int fline = line->fline;
unsigned long long count;
unsigned int tmp;
char c;
while (curline < fline &&
fgets(str, sizeof(str), fp))
if ((p = strchr(str, ':')) &&
sscanf (p + 1, "%u", &tmp) == 1)
curline = tmp;
if (sscanf (str, "%llu%c\n", &count, &c) == 2) {
if (next_zero == 0)
line->count += count;
next_zero = c == '*';
}
}
}
file = file->next;
}
}
/* store tcov data in file */
void __store_test_coverage (unsigned char * p)
{
int i, j;
unsigned int files;
unsigned int funcs;
unsigned int blocks;
unsigned int blocks_run;
unsigned int runs;
char *cov_filename = (char *)p + get_value (p, 4);
FILE *fp;
char *q;
tcov_file *file;
tcov_file *nfile;
tcov_function *func;
fp = open_tcov_file (cov_filename);
if (fp == NULL) {
fprintf (stderr, "Cannot create coverage file: %s\n", cov_filename);
return;
}
file = sort_test_coverage (p);
merge_test_coverage (file, fp, &runs);
fseek (fp, 0, SEEK_SET);
fprintf (fp, " -: 0:Runs:%u\n", runs);
files = 0;
funcs = 0;
blocks = 0;
blocks_run = 0;
nfile = file;
while (nfile) {
files++;
for (i = 0; i < nfile->n_func; i++) {
func = &nfile->func[i];
funcs++;
for (j = 0; j < func->n_line; j++) {
blocks++;
blocks_run += func->line[j].count != 0;
}
}
nfile = nfile->next;
}
if (blocks == 0)
blocks = 1;
fprintf (fp, " -: 0:All:%s Files:%u Functions:%u %.02f%%\n",
cov_filename, files, funcs, 100.0 * (double) blocks_run / blocks);
nfile = file;
while (nfile) {
FILE *src = fopen (nfile->filename, "r");
unsigned int curline = 1;
char str[10000];
if (src == NULL)
goto next;
funcs = 0;
blocks = 0;
blocks_run = 0;
for (i = 0; i < nfile->n_func; i++) {
func = &nfile->func[i];
funcs++;
for (j = 0; j < func->n_line; j++) {
blocks++;
blocks_run += func->line[j].count != 0;
}
}
if (blocks == 0)
blocks = 1;
fprintf (fp, " -: 0:File:%s Functions:%u %.02f%%\n",
nfile->filename, funcs, 100.0 * (double) blocks_run / blocks);
for (i = 0; i < nfile->n_func; i++) {
func = &nfile->func[i];
while (curline < func->first_line &&
fgets(str, sizeof(str), src))
fprintf (fp, " -:%5u:%s", curline++, str);
blocks = 0;
blocks_run = 0;
for (j = 0; j < func->n_line; j++) {
blocks++;
blocks_run += func->line[j].count != 0;
}
if (blocks == 0)
blocks = 1;
fprintf (fp, " -: 0:Function:%s %.02f%%\n",
func->function, 100.0 * (double) blocks_run / blocks);
#if 0
for (j = 0; j < func->n_line; j++) {
unsigned int fline = func->line[j].fline;
unsigned int lline = func->line[j].lline;
unsigned long long count = func->line[j].count;
fprintf (fp, "%u %u %llu\n", fline, lline, count);
}
#endif
for (j = 0; j < func->n_line;) {
unsigned int fline = func->line[j].fline;
unsigned int lline = func->line[j].lline;
unsigned long long count = func->line[j].count;
unsigned int has_zero = 0;
unsigned int same_line = fline == lline;
j++;
while (j < func->n_line) {
unsigned int nfline = func->line[j].fline;
unsigned int nlline = func->line[j].lline;
unsigned long long ncount = func->line[j].count;
if (fline == nfline) {
if (ncount == 0)
has_zero = 1;
else if (ncount > count)
count = ncount;
same_line = nfline == nlline;
lline = nlline;
j++;
}
else
break;
}
if (same_line)
lline++;
while (curline < fline &&
fgets(str, sizeof(str), src))
fprintf (fp, " -:%5u:%s", curline++, str);
while (curline < lline &&
fgets(str, sizeof(str), src)) {
if (count == 0)
fprintf (fp, " #####:%5u:%s",
curline, str);
else if (has_zero)
fprintf (fp, "%8llu*:%5u:%s",
count, curline, str);
else
fprintf (fp, "%9llu:%5u:%s",
count, curline, str);
curline++;
}
}
}
while (fgets(str, sizeof(str), src))
fprintf (fp, " -:%5u:%s", curline++, str);
fclose (src);
next:
nfile = nfile->next;
}
while (file) {
for (i = 0; i < file->n_func; i++) {
func = &file->func[i];
free (func->line);
}
free (file->func);
nfile = file;
file = file->next;
free (nfile);
}
fclose (fp);
}

View File

@ -3,7 +3,6 @@
#if defined __x86_64__
/* Avoid include files, they may not be available when cross compiling */
extern void *memset(void *s, int c, __SIZE_TYPE__ n);
extern void abort(void);
/* This should be in sync with our include/stdarg.h */
@ -12,6 +11,7 @@ enum __va_arg_type {
};
/* GCC compatible definition of va_list. */
/*predefined by TCC (tcc_predefs.h):
typedef struct {
unsigned int gp_offset;
unsigned int fp_offset;
@ -20,23 +20,18 @@ typedef struct {
char *overflow_arg_area;
};
char *reg_save_area;
} __va_list_struct;
} __builtin_va_list[1];
*/
void __va_start(__va_list_struct *ap, void *fp)
{
memset(ap, 0, sizeof(__va_list_struct));
*ap = *(__va_list_struct *)((char *)fp - 16);
ap->overflow_arg_area = (char *)fp + ap->overflow_offset;
ap->reg_save_area = (char *)fp - 176 - 16;
}
extern void *memcpy(void *dest, const void *src, unsigned long n);
void *__va_arg(__va_list_struct *ap,
enum __va_arg_type arg_type,
void *__va_arg(__builtin_va_list ap,
int arg_type,
int size, int align)
{
size = (size + 7) & ~7;
align = (align + 7) & ~7;
switch (arg_type) {
switch ((enum __va_arg_type)arg_type) {
case __va_gen_reg:
if (ap->gp_offset + size <= 48) {
ap->gp_offset += size;
@ -47,9 +42,15 @@ void *__va_arg(__va_list_struct *ap,
case __va_float_reg:
if (ap->fp_offset < 128 + 48) {
ap->fp_offset += 16;
if (size == 8)
return ap->reg_save_area + ap->fp_offset - 16;
if (ap->fp_offset < 128 + 48) {
memcpy(ap->reg_save_area + ap->fp_offset - 8,
ap->reg_save_area + ap->fp_offset, 8);
ap->fp_offset += 16;
return ap->reg_save_area + ap->fp_offset - 32;
}
}
size = 8;
goto use_overflow_area;
case __va_stack:
@ -60,6 +61,7 @@ void *__va_arg(__va_list_struct *ap,
default: /* should never happen */
abort();
return 0;
}
}
#endif

2114
libtcc.c

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,12 @@
extern "C" {
#endif
struct TCCState;
/*****************************/
/* set custom allocator for all allocations (optional), NULL for default. */
typedef void *TCCReallocFunc(void *ptr, unsigned long size);
LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *my_realloc);
/*****************************/
typedef struct TCCState TCCState;
/* create a new TCC compilation context */
@ -22,12 +26,12 @@ LIBTCCAPI void tcc_delete(TCCState *s);
/* set CONFIG_TCCDIR at runtime */
LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path);
/* set error/warning display callback */
LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
void (*error_func)(void *opaque, const char *msg));
/* set error/warning callback (optional) */
typedef void TCCErrorFunc(void *opaque, const char *msg);
LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc *error_func);
/* set options as from command line (multiple supported) */
LIBTCCAPI void tcc_set_options(TCCState *s, const char *str);
LIBTCCAPI int tcc_set_options(TCCState *s, const char *str);
/*****************************/
/* preprocessor */
@ -38,7 +42,7 @@ LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname);
/* add in system include path */
LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname);
/* define preprocessor symbol 'sym'. Can put optional value */
/* define preprocessor symbol 'sym'. value can be NULL, sym can be "sym=val" */
LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value);
/* undefine preprocess symbol 'sym' */
@ -53,16 +57,19 @@ LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename);
/* compile a string containing a C source. Return -1 if error. */
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);
/* Tip: to have more specific errors/warnings from tcc_compile_string(),
you can prefix the string with "#line <num> \"<filename>\"\n" */
/*****************************/
/* linking commands */
/* set output type. MUST BE CALLED before any compilation */
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type);
#define TCC_OUTPUT_MEMORY 1 /* output will be run in memory (default) */
#define TCC_OUTPUT_MEMORY 1 /* output will be run in memory */
#define TCC_OUTPUT_EXE 2 /* executable file */
#define TCC_OUTPUT_DLL 3 /* dynamic library */
#define TCC_OUTPUT_OBJ 4 /* object file */
#define TCC_OUTPUT_PREPROCESS 5 /* only preprocess (used internally) */
#define TCC_OUTPUT_DLL 4 /* dynamic library */
#define TCC_OUTPUT_OBJ 3 /* object file */
#define TCC_OUTPUT_PREPROCESS 5 /* only preprocess */
/* equivalent to -Lpath option */
LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname);
@ -82,17 +89,26 @@ LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename);
LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv);
/* do all relocations (needed before using tcc_get_symbol()) */
LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr);
/* possible values for 'ptr':
- TCC_RELOCATE_AUTO : Allocate and manage memory internally
- NULL : return required memory size for the step below
- memory address : copy code to memory passed by the caller
returns -1 if error. */
#define TCC_RELOCATE_AUTO (void*)1
LIBTCCAPI int tcc_relocate(TCCState *s1);
/* return symbol value or NULL if not found */
LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name);
/* list all (global) symbols and their values via 'symbol_cb()' */
LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx,
void (*symbol_cb)(void *ctx, const char *name, const void *val));
/* experimental/advanced section (see libtcc_test_mt.c for an example) */
/* catch runtime exceptions (optionally limit backtraces at top_func),
when using tcc_set_options("-bt") and when not using tcc_run() */
LIBTCCAPI void *_tcc_setjmp(TCCState *s1, void *jmp_buf, void *top_func, void *longjmp);
#define tcc_setjmp(s1,jb,f) setjmp(_tcc_setjmp(s1, jb, f, longjmp))
/* custom error printer for runtime exceptions. Returning 0 stops backtrace */
typedef int TCCBtFunc(void *udata, void *pc, const char *file, int line, const char* func, const char *msg);
LIBTCCAPI void tcc_set_backtrace_func(TCCState *s1, void* userdata, TCCBtFunc*);
#ifdef __cplusplus
}
#endif

3059
riscv64-asm.c Normal file

File diff suppressed because it is too large Load Diff

1496
riscv64-gen.c Normal file

File diff suppressed because it is too large Load Diff

440
riscv64-link.c Normal file
View File

@ -0,0 +1,440 @@
#ifdef TARGET_DEFS_ONLY
#define EM_TCC_TARGET EM_RISCV
#define R_DATA_32 R_RISCV_32
#define R_DATA_PTR R_RISCV_64
#define R_JMP_SLOT R_RISCV_JUMP_SLOT
#define R_GLOB_DAT R_RISCV_64
#define R_COPY R_RISCV_COPY
#define R_RELATIVE R_RISCV_RELATIVE
#define R_NUM R_RISCV_NUM
#define ELF_START_ADDR 0x00010000
#define ELF_PAGE_SIZE 0x1000
#define PCRELATIVE_DLLPLT 1
#define RELOCATE_DLLPLT 1
#else /* !TARGET_DEFS_ONLY */
//#define DEBUG_RELOC
#include "tcc.h"
/* Returns 1 for a code relocation, 0 for a data relocation. For unknown
relocations, returns -1. */
ST_FUNC int code_reloc (int reloc_type)
{
switch (reloc_type) {
case R_RISCV_BRANCH:
case R_RISCV_CALL:
case R_RISCV_JAL:
return 1;
case R_RISCV_GOT_HI20:
case R_RISCV_PCREL_HI20:
case R_RISCV_PCREL_LO12_I:
case R_RISCV_PCREL_LO12_S:
case R_RISCV_32_PCREL:
case R_RISCV_SET6:
case R_RISCV_SET8:
case R_RISCV_SET16:
case R_RISCV_SUB6:
case R_RISCV_ADD16:
case R_RISCV_ADD32:
case R_RISCV_ADD64:
case R_RISCV_SUB8:
case R_RISCV_SUB16:
case R_RISCV_SUB32:
case R_RISCV_SUB64:
case R_RISCV_32:
case R_RISCV_64:
case R_RISCV_SET_ULEB128:
case R_RISCV_SUB_ULEB128:
case R_RISCV_TPREL_HI20:
case R_RISCV_TPREL_LO12_I:
return 0;
case R_RISCV_CALL_PLT:
return 1;
}
return -1;
}
/* Returns an enumerator to describe whether and when the relocation needs a
GOT and/or PLT entry to be created. See tcc.h for a description of the
different values. */
ST_FUNC int gotplt_entry_type (int reloc_type)
{
switch (reloc_type) {
case R_RISCV_ALIGN:
case R_RISCV_RELAX:
case R_RISCV_RVC_BRANCH:
case R_RISCV_RVC_JUMP:
case R_RISCV_JUMP_SLOT:
case R_RISCV_SET6:
case R_RISCV_SET8:
case R_RISCV_SET16:
case R_RISCV_SUB6:
case R_RISCV_ADD16:
case R_RISCV_SUB8:
case R_RISCV_SUB16:
case R_RISCV_SET_ULEB128:
case R_RISCV_SUB_ULEB128:
return NO_GOTPLT_ENTRY;
case R_RISCV_BRANCH:
case R_RISCV_CALL:
case R_RISCV_PCREL_HI20:
case R_RISCV_PCREL_LO12_I:
case R_RISCV_PCREL_LO12_S:
case R_RISCV_32_PCREL:
case R_RISCV_ADD32:
case R_RISCV_ADD64:
case R_RISCV_SUB32:
case R_RISCV_SUB64:
case R_RISCV_32:
case R_RISCV_64:
case R_RISCV_JAL:
case R_RISCV_CALL_PLT:
return AUTO_GOTPLT_ENTRY;
case R_RISCV_GOT_HI20:
return ALWAYS_GOTPLT_ENTRY;
case R_RISCV_TPREL_HI20:
case R_RISCV_TPREL_LO12_I:
return NO_GOTPLT_ENTRY;
}
return -1;
}
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
{
Section *plt = s1->plt;
uint8_t *p;
unsigned plt_offset;
if (plt->data_offset == 0)
section_ptr_add(plt, 32);
plt_offset = plt->data_offset;
p = section_ptr_add(plt, 16);
write64le(p, got_offset);
return plt_offset;
}
/* relocate the PLT: compute addresses and offsets in the PLT now that final
address for PLT and GOT are known (see fill_program_header) */
ST_FUNC void relocate_plt(TCCState *s1)
{
uint8_t *p, *p_end;
if (!s1->plt)
return;
p = s1->plt->data;
p_end = p + s1->plt->data_offset;
if (p < p_end) {
uint64_t plt = s1->plt->sh_addr;
uint64_t got = s1->got->sh_addr;
uint64_t off = (got - plt + 0x800) >> 12;
if ((off + ((uint32_t)1 << 20)) >> 21)
tcc_error_noabort("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off, (long)got, (long)plt);
write32le(p, 0x397 | (off << 12)); // auipc t2, %pcrel_hi(got)
write32le(p + 4, 0x41c30333); // sub t1, t1, t3
write32le(p + 8, 0x0003be03 // ld t3, %pcrel_lo(got)(t2)
| (((got - plt) & 0xfff) << 20));
write32le(p + 12, 0xfd430313); // addi t1, t1, -(32+12)
write32le(p + 16, 0x00038293 // addi t0, t2, %pcrel_lo(got)
| (((got - plt) & 0xfff) << 20));
write32le(p + 20, 0x00135313); // srli t1, t1, log2(16/PTRSIZE)
write32le(p + 24, 0x0082b283); // ld t0, PTRSIZE(t0)
write32le(p + 28, 0x000e0067); // jr t3
p += 32;
while (p < p_end) {
uint64_t pc = plt + (p - s1->plt->data);
uint64_t addr = got + read64le(p);
uint64_t off = (addr - pc + 0x800) >> 12;
if ((off + ((uint32_t)1 << 20)) >> 21)
tcc_error_noabort("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", (long)off, (long)addr, (long)pc);
write32le(p, 0xe17 | (off << 12)); // auipc t3, %pcrel_hi(func@got)
write32le(p + 4, 0x000e3e03 // ld t3, %pcrel_lo(func@got)(t3)
| (((addr - pc) & 0xfff) << 20));
write32le(p + 8, 0x000e0367); // jalr t1, t3
write32le(p + 12, 0x00000013); // nop
p += 16;
}
}
if (s1->plt->reloc) {
ElfW_Rel *rel;
p = s1->got->data;
for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
write64le(p + rel->r_offset, s1->plt->sh_addr);
}
}
}
static void riscv64_record_pcrel_hi(TCCState *s1, addr_t addr, addr_t val)
{
struct pcrel_hi *entry = tcc_malloc(sizeof *entry);
entry->addr = addr;
entry->val = val;
dynarray_add(&s1->pcrel_hi_entries, &s1->nb_pcrel_hi_entries, entry);
}
static int riscv64_lookup_pcrel_hi(TCCState *s1, addr_t hi_addr, addr_t *hi_val)
{
int i;
for (i = s1->nb_pcrel_hi_entries; i > 0; ) {
struct pcrel_hi *entry = s1->pcrel_hi_entries[--i];
if (entry->addr == hi_addr) {
*hi_val = entry->val;
return 0;
}
}
return tcc_error_noabort("unsupported hi/lo pcrel reloc scheme");
}
ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
addr_t addr, addr_t val)
{
uint64_t off64;
uint32_t off32;
int sym_index = ELFW(R_SYM)(rel->r_info), esym_index;
ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
switch(type) {
case R_RISCV_ALIGN:
case R_RISCV_RELAX:
return;
case R_RISCV_BRANCH:
off64 = val - addr;
if ((off64 + (1 << 12)) & ~(uint64_t)0x1ffe)
tcc_error_noabort("R_RISCV_BRANCH relocation failed"
" (val=%lx, addr=%lx)", (long)val, (long)addr);
off32 = off64 >> 1;
write32le(ptr, (read32le(ptr) & ~0xfe000f80)
| ((off32 & 0x800) << 20)
| ((off32 & 0x3f0) << 21)
| ((off32 & 0x00f) << 8)
| ((off32 & 0x400) >> 3));
return;
case R_RISCV_JAL:
off64 = val - addr;
if ((off64 + (1 << 21)) & ~(((uint64_t)1 << 22) - 2))
tcc_error_noabort("R_RISCV_JAL relocation failed"
" (val=%lx, addr=%lx)", (long)val, (long)addr);
off32 = off64;
write32le(ptr, (read32le(ptr) & 0xfff)
| (((off32 >> 12) & 0xff) << 12)
| (((off32 >> 11) & 1) << 20)
| (((off32 >> 1) & 0x3ff) << 21)
| (((off32 >> 20) & 1) << 31));
return;
case R_RISCV_CALL:
case R_RISCV_CALL_PLT:
write32le(ptr, (read32le(ptr) & 0xfff)
| ((val - addr + 0x800) & ~0xfff));
write32le(ptr + 4, (read32le(ptr + 4) & 0xfffff)
| (((val - addr) & 0xfff) << 20));
return;
case R_RISCV_PCREL_HI20:
#ifdef DEBUG_RELOC
printf("PCREL_HI20: val=%lx addr=%lx\n", (long)val, (long)addr);
#endif
off64 = (int64_t)(val - addr + 0x800) >> 12;
if ((off64 + ((uint64_t)1 << 20)) >> 21)
tcc_error_noabort("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx sym=%s",
(long)off64, (long)((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21),
symtab_section->link->data + sym->st_name);
write32le(ptr, (read32le(ptr) & 0xfff)
| ((off64 & 0xfffff) << 12));
riscv64_record_pcrel_hi(s1, addr, val);
return;
case R_RISCV_GOT_HI20:
val = s1->got->sh_addr + get_sym_attr(s1, sym_index, 0)->got_offset;
off64 = (int64_t)(val - addr + 0x800) >> 12;
if ((off64 + ((uint64_t)1 << 20)) >> 21)
tcc_error_noabort("R_RISCV_GOT_HI20 relocation failed");
write32le(ptr, (read32le(ptr) & 0xfff)
| ((off64 & 0xfffff) << 12));
riscv64_record_pcrel_hi(s1, addr, val);
return;
case R_RISCV_PCREL_LO12_I:
#ifdef DEBUG_RELOC
printf("PCREL_LO12_I: val=%lx addr=%lx\n", (long)val, (long)addr);
#endif
addr = val;
riscv64_lookup_pcrel_hi(s1, addr, &val);
write32le(ptr, (read32le(ptr) & 0xfffff)
| (((val - addr) & 0xfff) << 20));
return;
case R_RISCV_PCREL_LO12_S:
addr = val;
riscv64_lookup_pcrel_hi(s1, addr, &val);
off32 = val - addr;
write32le(ptr, (read32le(ptr) & ~0xfe000f80)
| ((off32 & 0xfe0) << 20)
| ((off32 & 0x01f) << 7));
return;
case R_RISCV_RVC_BRANCH:
off64 = (val - addr);
if ((off64 + (1 << 8)) & ~(uint64_t)0x1fe)
tcc_error_noabort("R_RISCV_RVC_BRANCH relocation failed"
" (val=%lx, addr=%lx)", (long)val, (long)addr);
off32 = off64;
write16le(ptr, (read16le(ptr) & 0xe383)
| (((off32 >> 5) & 1) << 2)
| (((off32 >> 1) & 3) << 3)
| (((off32 >> 6) & 3) << 5)
| (((off32 >> 3) & 3) << 10)
| (((off32 >> 8) & 1) << 12));
return;
case R_RISCV_RVC_JUMP:
off64 = (val - addr);
if ((off64 + (1 << 11)) & ~(uint64_t)0xffe)
tcc_error_noabort("R_RISCV_RVC_BRANCH relocation failed"
" (val=%lx, addr=%lx)", (long)val, (long)addr);
off32 = off64;
write16le(ptr, (read16le(ptr) & 0xe003)
| (((off32 >> 5) & 1) << 2)
| (((off32 >> 1) & 7) << 3)
| (((off32 >> 7) & 1) << 6)
| (((off32 >> 6) & 1) << 7)
| (((off32 >> 10) & 1) << 8)
| (((off32 >> 8) & 3) << 9)
| (((off32 >> 4) & 1) << 11)
| (((off32 >> 11) & 1) << 12));
return;
case R_RISCV_32:
if (s1->output_type & TCC_OUTPUT_DYN) {
/* XXX: this logic may depend on TCC's codegen
now TCC uses R_RISCV_RELATIVE even for a 64bit pointer */
qrel->r_offset = rel->r_offset;
qrel->r_info = ELFW(R_INFO)(0, R_RISCV_RELATIVE);
/* Use sign extension! */
qrel->r_addend = (int)read32le(ptr) + val;
qrel++;
}
add32le(ptr, val);
return;
case R_RISCV_64:
if (s1->output_type & TCC_OUTPUT_DYN) {
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
qrel->r_offset = rel->r_offset;
if (esym_index) {
qrel->r_info = ELFW(R_INFO)(esym_index, R_RISCV_64);
qrel->r_addend = rel->r_addend;
qrel++;
break;
} else {
qrel->r_info = ELFW(R_INFO)(0, R_RISCV_RELATIVE);
qrel->r_addend = read64le(ptr) + val;
qrel++;
}
}
case R_RISCV_JUMP_SLOT:
add64le(ptr, val);
return;
case R_RISCV_ADD64:
write64le(ptr, read64le(ptr) + val);
return;
case R_RISCV_ADD32:
write32le(ptr, read32le(ptr) + val);
return;
case R_RISCV_SUB64:
write64le(ptr, read64le(ptr) - val);
return;
case R_RISCV_SUB32:
write32le(ptr, read32le(ptr) - val);
return;
case R_RISCV_ADD16:
write16le(ptr, read16le(ptr) + val);
return;
case R_RISCV_SUB8:
*ptr -= val;
return;
case R_RISCV_SUB16:
write16le(ptr, read16le(ptr) - val);
return;
case R_RISCV_SET6:
*ptr = (*ptr & ~0x3f) | (val & 0x3f);
return;
case R_RISCV_SET8:
*ptr = (*ptr & ~0xff) | (val & 0xff);
return;
case R_RISCV_SET16:
write16le(ptr, val);
return;
case R_RISCV_SUB6:
*ptr = (*ptr & ~0x3f) | ((*ptr - val) & 0x3f);
return;
case R_RISCV_32_PCREL:
if (s1->output_type & TCC_OUTPUT_DYN) {
/* DLL relocation */
esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
if (esym_index) {
qrel->r_offset = rel->r_offset;
qrel->r_info = ELFW(R_INFO)(esym_index, R_RISCV_32_PCREL);
/* Use sign extension! */
qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
qrel++;
break;
}
}
add32le(ptr, val - addr);
return;
case R_RISCV_SET_ULEB128:
case R_RISCV_SUB_ULEB128:
/* ignore. used in section .debug_loclists */
return;
case R_RISCV_COPY:
/* XXX */
return;
case R_RISCV_RELATIVE:
/* R_RISCV_RELATIVE value is already applied in R_RISCV_32/64
dynamic output paths, but we need this case for incoming
RELATIVE relocations from object files. */
return;
case R_RISCV_TPREL_HI20:
case R_RISCV_TPREL_LO12_I: {
addr_t tls_start = 0;
int64_t tp_offset;
int i;
for (i = 1; i < s1->nb_sections; i++) {
Section *s = s1->sections[i];
if (s->sh_flags & SHF_TLS && s->sh_size) {
if (!tls_start || s->sh_addr < tls_start)
tls_start = s->sh_addr;
}
}
tp_offset = val - tls_start;
if (type == R_RISCV_TPREL_HI20) {
off64 = (int64_t)(tp_offset + 0x800) >> 12;
if ((off64 + ((uint64_t)1 << 20)) >> 21)
tcc_error_noabort("R_RISCV_TPREL_HI20 relocation failed");
write32le(ptr, (read32le(ptr) & 0xfff)
| ((off64 & 0xfffff) << 12));
} else {
write32le(ptr, (read32le(ptr) & 0xfffff)
| (((tp_offset) & 0xfff) << 20));
}
return;
}
default:
fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
type, (unsigned)addr, ptr, (unsigned)val);
return;
}
}
#endif

612
riscv64-tok.h Normal file
View File

@ -0,0 +1,612 @@
/* ------------------------------------------------------------------ */
/* WARNING: relative order of tokens is important. */
/*
* The specifications are available under https://riscv.org/technical/specifications/
*/
#define DEF_ASM_WITH_SUFFIX(x, y) \
DEF(TOK_ASM_ ## x ## _ ## y, #x "." #y)
#define DEF_ASM_WITH_SUFFIXES(x, y, z) \
DEF(TOK_ASM_ ## x ## _ ## y ## _ ## z, #x "." #y "." #z)
#define DEF_ASM_FENCE(x) \
DEF(TOK_ASM_ ## x ## _fence, #x)
/* register */
/* integer */
DEF_ASM(x0)
DEF_ASM(x1)
DEF_ASM(x2)
DEF_ASM(x3)
DEF_ASM(x4)
DEF_ASM(x5)
DEF_ASM(x6)
DEF_ASM(x7)
DEF_ASM(x8)
DEF_ASM(x9)
DEF_ASM(x10)
DEF_ASM(x11)
DEF_ASM(x12)
DEF_ASM(x13)
DEF_ASM(x14)
DEF_ASM(x15)
DEF_ASM(x16)
DEF_ASM(x17)
DEF_ASM(x18)
DEF_ASM(x19)
DEF_ASM(x20)
DEF_ASM(x21)
DEF_ASM(x22)
DEF_ASM(x23)
DEF_ASM(x24)
DEF_ASM(x25)
DEF_ASM(x26)
DEF_ASM(x27)
DEF_ASM(x28)
DEF_ASM(x29)
DEF_ASM(x30)
DEF_ASM(x31)
/* float */
DEF_ASM(f0)
DEF_ASM(f1)
DEF_ASM(f2)
DEF_ASM(f3)
DEF_ASM(f4)
DEF_ASM(f5)
DEF_ASM(f6)
DEF_ASM(f7)
DEF_ASM(f8)
DEF_ASM(f9)
DEF_ASM(f10)
DEF_ASM(f11)
DEF_ASM(f12)
DEF_ASM(f13)
DEF_ASM(f14)
DEF_ASM(f15)
DEF_ASM(f16)
DEF_ASM(f17)
DEF_ASM(f18)
DEF_ASM(f19)
DEF_ASM(f20)
DEF_ASM(f21)
DEF_ASM(f22)
DEF_ASM(f23)
DEF_ASM(f24)
DEF_ASM(f25)
DEF_ASM(f26)
DEF_ASM(f27)
DEF_ASM(f28)
DEF_ASM(f29)
DEF_ASM(f30)
DEF_ASM(f31)
/* register ABI mnemonics, refer to RISC-V ABI 1.0 */
/* integer */
DEF_ASM(zero)
DEF_ASM(ra)
DEF_ASM(sp)
DEF_ASM(gp)
DEF_ASM(tp)
DEF_ASM(t0)
DEF_ASM(t1)
DEF_ASM(t2)
DEF_ASM(s0)
DEF_ASM(s1)
DEF_ASM(a0)
DEF_ASM(a1)
DEF_ASM(a2)
DEF_ASM(a3)
DEF_ASM(a4)
DEF_ASM(a5)
DEF_ASM(a6)
DEF_ASM(a7)
DEF_ASM(s2)
DEF_ASM(s3)
DEF_ASM(s4)
DEF_ASM(s5)
DEF_ASM(s6)
DEF_ASM(s7)
DEF_ASM(s8)
DEF_ASM(s9)
DEF_ASM(s10)
DEF_ASM(s11)
DEF_ASM(t3)
DEF_ASM(t4)
DEF_ASM(t5)
DEF_ASM(t6)
/* float */
DEF_ASM(ft0)
DEF_ASM(ft1)
DEF_ASM(ft2)
DEF_ASM(ft3)
DEF_ASM(ft4)
DEF_ASM(ft5)
DEF_ASM(ft6)
DEF_ASM(ft7)
DEF_ASM(fs0)
DEF_ASM(fs1)
DEF_ASM(fa0)
DEF_ASM(fa1)
DEF_ASM(fa2)
DEF_ASM(fa3)
DEF_ASM(fa4)
DEF_ASM(fa5)
DEF_ASM(fa6)
DEF_ASM(fa7)
DEF_ASM(fs2)
DEF_ASM(fs3)
DEF_ASM(fs4)
DEF_ASM(fs5)
DEF_ASM(fs6)
DEF_ASM(fs7)
DEF_ASM(fs8)
DEF_ASM(fs9)
DEF_ASM(fs10)
DEF_ASM(fs11)
DEF_ASM(ft8)
DEF_ASM(ft9)
DEF_ASM(ft10)
DEF_ASM(ft11)
/* not in the ABI */
DEF_ASM(pc)
/* Loads */
DEF_ASM(lb)
DEF_ASM(lh)
DEF_ASM(lw)
DEF_ASM(lbu)
DEF_ASM(lhu)
/* RV64 */
DEF_ASM(ld)
DEF_ASM(lwu)
/* Stores */
DEF_ASM(sb)
DEF_ASM(sh)
DEF_ASM(sw)
/* RV64 */
DEF_ASM(sd)
/* Shifts */
DEF_ASM(sll)
DEF_ASM(srl)
DEF_ASM(sra)
/* RV64 */
DEF_ASM(slli)
DEF_ASM(srli)
DEF_ASM(sllw)
DEF_ASM(slliw)
DEF_ASM(srlw)
DEF_ASM(srliw)
DEF_ASM(srai)
DEF_ASM(sraw)
DEF_ASM(sraiw)
/* Arithmetic */
DEF_ASM(add)
DEF_ASM(addi)
DEF_ASM(sub)
DEF_ASM(lui)
DEF_ASM(auipc)
/* RV64 */
DEF_ASM(addw)
DEF_ASM(addiw)
DEF_ASM(subw)
/* Logical */
DEF_ASM(xor)
DEF_ASM(xori)
DEF_ASM(or)
DEF_ASM(ori)
DEF_ASM(and)
DEF_ASM(andi)
/* Compare */
DEF_ASM(slt)
DEF_ASM(slti)
DEF_ASM(sltu)
DEF_ASM(sltiu)
/* Branch */
DEF_ASM(beq)
DEF_ASM(bne)
DEF_ASM(blt)
DEF_ASM(bge)
DEF_ASM(bltu)
DEF_ASM(bgeu)
/* Jump */
DEF_ASM(jal)
DEF_ASM(jalr)
/* Sync */
DEF_ASM(fence)
/* Zifencei extension */
DEF_ASM_WITH_SUFFIX(fence, i)
/* System call */
/* used to be called scall and sbreak */
DEF_ASM(ecall)
DEF_ASM(ebreak)
/* Counters */
DEF_ASM(rdcycle)
DEF_ASM(rdcycleh)
DEF_ASM(rdtime)
DEF_ASM(rdtimeh)
DEF_ASM(rdinstret)
DEF_ASM(rdinstreth)
/* “M” Standard Extension for Integer Multiplication and Division, V2.0 */
DEF_ASM(mul)
DEF_ASM(mulh)
DEF_ASM(mulhsu)
DEF_ASM(mulhu)
DEF_ASM(div)
DEF_ASM(divu)
DEF_ASM(rem)
DEF_ASM(remu)
/* RV64 */
DEF_ASM(mulw)
DEF_ASM(divw)
DEF_ASM(divuw)
DEF_ASM(remw)
DEF_ASM(remuw)
/* "F"/"D" Extension for Single/Double-Precision Floating Point Arithmetic, V2.2 */
/* enough implemented for musl */
DEF_ASM_WITH_SUFFIX(fsgnj, s)
DEF_ASM_WITH_SUFFIX(fsgnj, d)
DEF_ASM_WITH_SUFFIX(fadd, s)
DEF_ASM_WITH_SUFFIX(fadd, d)
DEF_ASM_WITH_SUFFIX(fsub, s)
DEF_ASM_WITH_SUFFIX(fsub, d)
DEF_ASM_WITH_SUFFIX(fmul, s)
DEF_ASM_WITH_SUFFIX(fmul, d)
DEF_ASM_WITH_SUFFIX(fdiv, s)
DEF_ASM_WITH_SUFFIX(fdiv, d)
DEF_ASM_WITH_SUFFIX(fmadd, s)
DEF_ASM_WITH_SUFFIX(fmadd, d)
DEF_ASM_WITH_SUFFIX(fmax, s)
DEF_ASM_WITH_SUFFIX(fmax, d)
DEF_ASM_WITH_SUFFIX(fmin, s)
DEF_ASM_WITH_SUFFIX(fmin, d)
DEF_ASM_WITH_SUFFIX(fsqrt, s)
DEF_ASM_WITH_SUFFIX(fsqrt, d)
/* F/D comparison and conversion (not needed by musl, added for completeness) */
DEF_ASM_WITH_SUFFIX(feq, s)
DEF_ASM_WITH_SUFFIX(feq, d)
DEF_ASM_WITH_SUFFIX(flt, s)
DEF_ASM_WITH_SUFFIX(flt, d)
DEF_ASM_WITH_SUFFIX(fle, s)
DEF_ASM_WITH_SUFFIX(fle, d)
DEF_ASM_WITH_SUFFIX(fclass, s)
DEF_ASM_WITH_SUFFIX(fclass, d)
DEF_ASM_WITH_SUFFIXES(fcvt, w, s)
DEF_ASM_WITH_SUFFIXES(fcvt, wu, s)
DEF_ASM_WITH_SUFFIXES(fcvt, l, s)
DEF_ASM_WITH_SUFFIXES(fcvt, lu, s)
DEF_ASM_WITH_SUFFIXES(fcvt, s, w)
DEF_ASM_WITH_SUFFIXES(fcvt, s, wu)
DEF_ASM_WITH_SUFFIXES(fcvt, s, l)
DEF_ASM_WITH_SUFFIXES(fcvt, s, lu)
DEF_ASM_WITH_SUFFIXES(fcvt, w, d)
DEF_ASM_WITH_SUFFIXES(fcvt, wu, d)
DEF_ASM_WITH_SUFFIXES(fcvt, l, d)
DEF_ASM_WITH_SUFFIXES(fcvt, lu, d)
DEF_ASM_WITH_SUFFIXES(fcvt, d, w)
DEF_ASM_WITH_SUFFIXES(fcvt, d, wu)
DEF_ASM_WITH_SUFFIXES(fcvt, d, l)
DEF_ASM_WITH_SUFFIXES(fcvt, d, lu)
DEF_ASM_WITH_SUFFIXES(fcvt, s, d)
DEF_ASM_WITH_SUFFIXES(fcvt, d, s)
/* "C" Extension for Compressed Instructions, V2.0 */
DEF_ASM_WITH_SUFFIX(c, nop)
/* Loads */
DEF_ASM_WITH_SUFFIX(c, li)
DEF_ASM_WITH_SUFFIX(c, lw)
DEF_ASM_WITH_SUFFIX(c, lwsp)
/* single float */
DEF_ASM_WITH_SUFFIX(c, flw)
DEF_ASM_WITH_SUFFIX(c, flwsp)
/* double float */
DEF_ASM_WITH_SUFFIX(c, fld)
DEF_ASM_WITH_SUFFIX(c, fldsp)
/* RV64 */
DEF_ASM_WITH_SUFFIX(c, ld)
DEF_ASM_WITH_SUFFIX(c, ldsp)
/* Stores */
DEF_ASM_WITH_SUFFIX(c, sw)
DEF_ASM_WITH_SUFFIX(c, sd)
DEF_ASM_WITH_SUFFIX(c, swsp)
DEF_ASM_WITH_SUFFIX(c, sdsp)
/* single float */
DEF_ASM_WITH_SUFFIX(c, fsw)
DEF_ASM_WITH_SUFFIX(c, fswsp)
/* double float */
DEF_ASM_WITH_SUFFIX(c, fsd)
DEF_ASM_WITH_SUFFIX(c, fsdsp)
/* Shifts */
DEF_ASM_WITH_SUFFIX(c, slli)
DEF_ASM_WITH_SUFFIX(c, srli)
DEF_ASM_WITH_SUFFIX(c, srai)
/* Arithmetic */
DEF_ASM_WITH_SUFFIX(c, add)
DEF_ASM_WITH_SUFFIX(c, addi)
DEF_ASM_WITH_SUFFIX(c, addi16sp)
DEF_ASM_WITH_SUFFIX(c, addi4spn)
DEF_ASM_WITH_SUFFIX(c, lui)
DEF_ASM_WITH_SUFFIX(c, sub)
DEF_ASM_WITH_SUFFIX(c, mv)
/* RV64 */
DEF_ASM_WITH_SUFFIX(c, addw)
DEF_ASM_WITH_SUFFIX(c, addiw)
DEF_ASM_WITH_SUFFIX(c, subw)
/* Logical */
DEF_ASM_WITH_SUFFIX(c, xor)
DEF_ASM_WITH_SUFFIX(c, or)
DEF_ASM_WITH_SUFFIX(c, and)
DEF_ASM_WITH_SUFFIX(c, andi)
/* Branch */
DEF_ASM_WITH_SUFFIX(c, beqz)
DEF_ASM_WITH_SUFFIX(c, bnez)
/* Jump */
DEF_ASM_WITH_SUFFIX(c, j)
DEF_ASM_WITH_SUFFIX(c, jr)
DEF_ASM_WITH_SUFFIX(c, jal)
DEF_ASM_WITH_SUFFIX(c, jalr)
/* System call */
DEF_ASM_WITH_SUFFIX(c, ebreak)
/* XXX F Extension: Single-Precision Floating Point */
/* XXX D Extension: Double-Precision Floating Point */
/* from the spec: Tables 16.516.7 list the RVC instructions. */
/* “Zicsr”, Control and Status Register (CSR) Instructions, V2.0 */
DEF_ASM(csrrw)
DEF_ASM(csrrs)
DEF_ASM(csrrc)
DEF_ASM(csrrwi)
DEF_ASM(csrrsi)
DEF_ASM(csrrci)
/* registers */
DEF_ASM(cycle)
DEF_ASM(fcsr)
DEF_ASM(fflags)
DEF_ASM(frm)
DEF_ASM(instret)
DEF_ASM(time)
/* RV32I-only */
DEF_ASM(cycleh)
DEF_ASM(instreth)
DEF_ASM(timeh)
/* pseudo */
DEF_ASM(csrc)
DEF_ASM(csrci)
DEF_ASM(csrr)
DEF_ASM(csrs)
DEF_ASM(csrsi)
DEF_ASM(csrw)
DEF_ASM(csrwi)
DEF_ASM(frcsr)
DEF_ASM(frflags)
DEF_ASM(frrm)
DEF_ASM(fscsr)
DEF_ASM(fsflags)
DEF_ASM(fsrm)
/* Privileged Instructions */
DEF_ASM(mrts)
DEF_ASM(mrth)
DEF_ASM(hrts)
DEF_ASM(wfi)
/* pseudoinstructions */
DEF_ASM(beqz)
DEF_ASM(bgez)
DEF_ASM(bgt)
DEF_ASM(bgtu)
DEF_ASM(bgtz)
DEF_ASM(ble)
DEF_ASM(bleu)
DEF_ASM(blez)
DEF_ASM(bltz)
DEF_ASM(bnez)
DEF_ASM(call)
DEF_ASM_WITH_SUFFIX(fabs, d)
DEF_ASM_WITH_SUFFIX(fabs, s)
DEF_ASM(fld)
DEF_ASM(flw)
DEF_ASM_WITH_SUFFIX(fmv, d)
DEF_ASM_WITH_SUFFIX(fmv, s)
DEF_ASM_WITH_SUFFIX(fneg, d)
DEF_ASM_WITH_SUFFIX(fneg, s)
DEF_ASM(fsd)
DEF_ASM(fsw)
DEF_ASM(j)
DEF_ASM(jump)
DEF_ASM(jr)
DEF_ASM(la)
DEF_ASM(li)
DEF_ASM(lla)
DEF_ASM(mv)
DEF_ASM(neg)
DEF_ASM(negw)
DEF_ASM(nop)
DEF_ASM(not)
DEF_ASM(ret)
DEF_ASM(seqz)
DEF_ASM_WITH_SUFFIX(sext, w)
DEF_ASM(sgtz)
DEF_ASM(sltz)
DEF_ASM(snez)
DEF_ASM(tail)
/* Possible values for .option directive */
DEF_ASM(arch)
DEF_ASM(rvc)
DEF_ASM(norvc)
DEF_ASM(pic)
DEF_ASM(nopic)
DEF_ASM(relax)
DEF_ASM(norelax)
DEF_ASM(push)
DEF_ASM(pop)
/* “A” Standard Extension for Atomic Instructions, Version 2.1 */
/* XXX: Atomic memory operations */
DEF_ASM_WITH_SUFFIX(lr, w)
DEF_ASM_WITH_SUFFIXES(lr, w, aq)
DEF_ASM_WITH_SUFFIXES(lr, w, rl)
DEF_ASM_WITH_SUFFIXES(lr, w, aqrl)
DEF_ASM_WITH_SUFFIX(lr, d)
DEF_ASM_WITH_SUFFIXES(lr, d, aq)
DEF_ASM_WITH_SUFFIXES(lr, d, rl)
DEF_ASM_WITH_SUFFIXES(lr, d, aqrl)
DEF_ASM_WITH_SUFFIX(sc, w)
DEF_ASM_WITH_SUFFIXES(sc, w, aq)
DEF_ASM_WITH_SUFFIXES(sc, w, rl)
DEF_ASM_WITH_SUFFIXES(sc, w, aqrl)
DEF_ASM_WITH_SUFFIX(sc, d)
DEF_ASM_WITH_SUFFIXES(sc, d, aq)
DEF_ASM_WITH_SUFFIXES(sc, d, rl)
DEF_ASM_WITH_SUFFIXES(sc, d, aqrl)
/* "A" Extension for Atomic Operations, V2.1 (base, no aq/rl suffixes) */
DEF_ASM_WITH_SUFFIX(amoadd, w)
DEF_ASM_WITH_SUFFIX(amoadd, d)
DEF_ASM_WITH_SUFFIX(amoswap, w)
DEF_ASM_WITH_SUFFIX(amoswap, d)
DEF_ASM_WITH_SUFFIX(amoand, w)
DEF_ASM_WITH_SUFFIX(amoand, d)
DEF_ASM_WITH_SUFFIX(amoor, w)
DEF_ASM_WITH_SUFFIX(amoor, d)
DEF_ASM_WITH_SUFFIX(amoxor, w)
DEF_ASM_WITH_SUFFIX(amoxor, d)
DEF_ASM_WITH_SUFFIX(amomax, w)
DEF_ASM_WITH_SUFFIX(amomax, d)
DEF_ASM_WITH_SUFFIX(amomaxu, w)
DEF_ASM_WITH_SUFFIX(amomaxu, d)
DEF_ASM_WITH_SUFFIX(amomin, w)
DEF_ASM_WITH_SUFFIX(amomin, d)
DEF_ASM_WITH_SUFFIX(amominu, w)
DEF_ASM_WITH_SUFFIX(amominu, d)
/* AMO aq/rl ordering suffixes */
DEF_ASM_WITH_SUFFIXES(amoadd, w, aq)
DEF_ASM_WITH_SUFFIXES(amoadd, w, rl)
DEF_ASM_WITH_SUFFIXES(amoadd, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amoadd, d, aq)
DEF_ASM_WITH_SUFFIXES(amoadd, d, rl)
DEF_ASM_WITH_SUFFIXES(amoadd, d, aqrl)
/* Complete AMO aq/rl ordering suffixes (all ops) */
DEF_ASM_WITH_SUFFIXES(amoswap, w, aq)
DEF_ASM_WITH_SUFFIXES(amoswap, w, rl)
DEF_ASM_WITH_SUFFIXES(amoswap, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amoswap, d, aq)
DEF_ASM_WITH_SUFFIXES(amoswap, d, rl)
DEF_ASM_WITH_SUFFIXES(amoswap, d, aqrl)
DEF_ASM_WITH_SUFFIXES(amoand, w, aq)
DEF_ASM_WITH_SUFFIXES(amoand, w, rl)
DEF_ASM_WITH_SUFFIXES(amoand, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amoand, d, aq)
DEF_ASM_WITH_SUFFIXES(amoand, d, rl)
DEF_ASM_WITH_SUFFIXES(amoand, d, aqrl)
DEF_ASM_WITH_SUFFIXES(amoor, w, aq)
DEF_ASM_WITH_SUFFIXES(amoor, w, rl)
DEF_ASM_WITH_SUFFIXES(amoor, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amoor, d, aq)
DEF_ASM_WITH_SUFFIXES(amoor, d, rl)
DEF_ASM_WITH_SUFFIXES(amoor, d, aqrl)
DEF_ASM_WITH_SUFFIXES(amoxor, w, aq)
DEF_ASM_WITH_SUFFIXES(amoxor, w, rl)
DEF_ASM_WITH_SUFFIXES(amoxor, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amoxor, d, aq)
DEF_ASM_WITH_SUFFIXES(amoxor, d, rl)
DEF_ASM_WITH_SUFFIXES(amoxor, d, aqrl)
DEF_ASM_WITH_SUFFIXES(amomax, w, aq)
DEF_ASM_WITH_SUFFIXES(amomax, w, rl)
DEF_ASM_WITH_SUFFIXES(amomax, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amomax, d, aq)
DEF_ASM_WITH_SUFFIXES(amomax, d, rl)
DEF_ASM_WITH_SUFFIXES(amomax, d, aqrl)
DEF_ASM_WITH_SUFFIXES(amomaxu, w, aq)
DEF_ASM_WITH_SUFFIXES(amomaxu, w, rl)
DEF_ASM_WITH_SUFFIXES(amomaxu, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amomaxu, d, aq)
DEF_ASM_WITH_SUFFIXES(amomaxu, d, rl)
DEF_ASM_WITH_SUFFIXES(amomaxu, d, aqrl)
DEF_ASM_WITH_SUFFIXES(amomin, w, aq)
DEF_ASM_WITH_SUFFIXES(amomin, w, rl)
DEF_ASM_WITH_SUFFIXES(amomin, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amomin, d, aq)
DEF_ASM_WITH_SUFFIXES(amomin, d, rl)
DEF_ASM_WITH_SUFFIXES(amomin, d, aqrl)
DEF_ASM_WITH_SUFFIXES(amominu, w, aq)
DEF_ASM_WITH_SUFFIXES(amominu, w, rl)
DEF_ASM_WITH_SUFFIXES(amominu, w, aqrl)
DEF_ASM_WITH_SUFFIXES(amominu, d, aq)
DEF_ASM_WITH_SUFFIXES(amominu, d, rl)
DEF_ASM_WITH_SUFFIXES(amominu, d, aqrl)
/* rounding mode keywords (used as fcvt operand: fcvt.w.s rd, rs1, rtz) */
DEF_ASM(rne)
DEF_ASM(rtz)
DEF_ASM(rdn)
DEF_ASM(rup)
DEF_ASM(rmm)
/* `fence` arguments */
/* NOTE: Order is important */
DEF_ASM_FENCE(w)
DEF_ASM_FENCE(r)
DEF_ASM_FENCE(rw)
DEF_ASM_FENCE(o)
DEF_ASM_FENCE(ow)
DEF_ASM_FENCE(or)
DEF_ASM_FENCE(orw)
DEF_ASM_FENCE(i)
DEF_ASM_FENCE(iw)
DEF_ASM_FENCE(ir)
DEF_ASM_FENCE(irw)
DEF_ASM_FENCE(io)
DEF_ASM_FENCE(iow)
DEF_ASM_FENCE(ior)
DEF_ASM_FENCE(iorw)
#undef DEF_ASM_FENCE
#undef DEF_ASM_WITH_SUFFIX
#undef DEF_ASM_WITH_SUFFIXES

View File

@ -83,7 +83,7 @@ usage: tcc [options] [@var{infile1} @var{infile2}@dots{}] [@option{-run} @var{in
@noindent
@c man begin DESCRIPTION
TCC options are a very much like gcc options. The main difference is that TCC
TCC options are very much like gcc options. The main difference is that TCC
can also execute directly the resulting program and give it runtime
arguments.
@ -199,6 +199,16 @@ include paths are: @file{/usr/local/include}, @file{/usr/include}
and @file{PREFIX/lib/tcc/include}. (@file{PREFIX} is usually
@file{/usr} or @file{/usr/local}).
@item -isystem dir
Specify a system include path to be added to the defaults.
@item -nostdinc
Do not search the default system include paths; only search include paths
provided on the command line.
@item -include file
Include @option{file} above each input file.
@item -Dsym[=val]
Define preprocessor symbol @samp{sym} to
val. If val is not present, its value is @samp{1}. Function-like macros can
@ -210,6 +220,18 @@ Undefine preprocessor symbol @samp{sym}.
@item -E
Preprocess only, to stdout or file (with -o).
@item -P
Do not output @code{#line} directives.
@item -P1
Output alternative @code{#line} directives.
@item -dD, -dM
Output @code{#define} directives.
@item -Wp,-opt
Same as @option{-opt}.
@end table
Compilation flags:
@ -238,6 +260,19 @@ behaves like an unnamed one.
@item -fdollars-in-identifiers
Allow dollar signs in identifiers
@item -freverse-funcargs
Evaluate function arguments right to left.
@item -fgnu89-inline
@code{extern inline} is like @code{static inline}.
@item -fasynchronous-unwind-tables
Create eh_frame section [on]
@item -ftest-coverage
Create code coverage code. After running the resulting code an executable.tcov
or sofile.tcov file is generated with code coverage.
@end table
Warning options:
@ -253,7 +288,10 @@ Note: each of the following warning options has a negative form beginning with
@table @option
@item -Wimplicit-function-declaration
Warn about implicit function declaration.
Warn about implicit function declaration (missing prototype).
@item -Wdiscarded-qualifiers
Warn when const is dropped.
@item -Wunsupported
Warn about unsupported GCC features that are ignored by TCC.
@ -263,11 +301,13 @@ Make string constants be of type @code{const char *} instead of @code{char
*}.
@item -Werror
Abort compilation if warnings are issued.
Abort compilation if a warning is issued. Can be given an option to enable
the specified warning and turn it into an error, for example
@option{-Werror=unsupported}.
@item -Wall
Activate all warnings, except @option{-Werror}, @option{-Wunusupported} and
@option{-Wwrite-strings}.
Activate some useful warnings (@option{-Wimplicit-function-declaration},
@option{-Wdiscard-qualifiers}).
@end table
@ -304,9 +344,22 @@ opened with @code{dlopen()} needs to access executable symbols.
@item -r
Generate an object file combining all input files.
@item -nostdlib
Don't implicitly link with libc, the C runtime files, and libtcc1.
@item -Wl,-nostdlib
Don't search the default paths for libraries (@file{/usr/local/lib},
@file{/usr/lib} and @file{/lib}). Only the paths specified with @option{-L}
and @env{LIBRARY_PATH} are searched.
@item -Wl,-rpath=path
Put custom search path for dynamic libraries into executable.
@item -Wl,-Ipath
@item -Wl,--dynamic-linker=path
Set the ELF interpreter (dynamic linker). This defaults to the value of the
environment variable @env{LD_SO} if set, or a compiled-in default.
@item -Wl,--enable-new-dtags
When putting a custom search path for dynamic libraries into the executable,
create the new ELF dynamic tag DT_RUNPATH instead of the old legacy DT_RPATH.
@ -322,12 +375,26 @@ Binary image (only for executable output)
COFF output format (only for executable output for TMS320C67xx target)
@end table
@item -Wl,--export-all-symbols
@item -Wl,--export-dynamic
Export global symbols to the dynamic linker. It is useful when a library
opened with @code{dlopen()} needs to access executable symbols.
@item -Wl,-subsystem=console/gui/wince/...
Set type for PE (Windows) executables.
@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]
Modify executable layout.
@item -Wl,-[dynamicbase | nxcompat | high-entropy-va | tsaware]
@item -Wl,-[no-dynamicbase | no-nxcompat | no-high-entropy-va | no-tsaware]
@item -Wl,-[disable-dynamicbase | disable-nxcompat | disable-high-entropy-va | disable-tsaware]
Set or clear PE (Windows) executable header hardening flags. The
@option{-Wl,-high-entropy-va} option is supported on x86-64 and ARM64 PE
targets and implies @option{-Wl,-dynamicbase}. Clearing dynamicbase also
clears high-entropy-va. When @option{-Wl,-dynamicbase} is used for an
executable, TCC also enables base relocation emission for Windows ASLR.
@item -Wl,-Bsymbolic
Set DT_SYMBOLIC tag.
@ -340,33 +407,63 @@ Debugger options:
@table @option
@item -g
Generate run time debug information so that you get clear run time
Generate run time stab debug information so that you get clear run time
error messages: @code{ test.c:68: in function 'test5()': dereferencing
invalid pointer} instead of the laconic @code{Segmentation
fault}.
@item -gdwarf[-x]
Generate run time dwarf debug information instead of stab debug information.
@item -b
Generate additional support code to check
memory allocations and array/pointer bounds. @option{-g} is implied. Note
that the generated code is slower and bigger in this case.
Generate additional support code to check memory allocations and array/pointer
bounds (@pxref{Bounds}). @option{-g} is implied.
Note: @option{-b} is only available on i386 when using libtcc for the moment.
@item -bt[N]
Display N callers in stack traces. This is useful with @option{-g} or @option{-b}.
When activated, @code{__TCC_BACKTRACE__} is defined.
@item -bt N
Display N callers in stack traces. This is useful with @option{-g} or
@option{-b}.
With executables, additional support for stack traces is included. A function
@code{ int tcc_backtrace(const char *fmt, ...); }
is provided to trigger a stack trace with a message on demand.
@end table
Misc options:
@table @option
@item -std=version
Define @code{__STDC_VERSION__}: @code{201112} if @option{version} is c11 or
gnu11; @code{199901} otherwise.
@item -x[c|a|b|n]
Specify content of next input file: respectively C, assembly, binary, or none.
@item -O[n]
Same as @option{-D__OPTIMIZE__} except for -O0.
@item -pthread
Preprocess with @option{-D_REENTRANT}, link with @option{-lpthread}.
@item -M
Just output makefile fragment with dependencies
@item -MM
Like -M except mention only user header files, not system header files.
@item -MD
Generate makefile fragment with dependencies.
@item -MMD
Like -MD except mention only user header files, not system header files.
@item -MF depfile
Use @file{depfile} as output for -MD.
@item -MP
Mention all dependencies as targets too.
@item -print-search-dirs
Print the configured installation directory and a list of library
and include directories tcc will search.
@ -374,6 +471,9 @@ and include directories tcc will search.
@item -dumpversion
Print version.
@item -dt
With @option{-run}/@option{-E}: auto-define 'test_...' macros
@end table
Target specific options:
@ -394,8 +494,7 @@ Pass command line to the i386/x86_64 cross compiler.
@end table
Note: GCC options @option{-Ox}, @option{-fx} and @option{-mx} are
ignored.
Note: GCC options @option{-fx} and @option{-mx} are ignored.
@c man end
@c man begin ENVIRONMENT
@ -543,6 +642,7 @@ instead of
@cindex stdcall attribute
@cindex regparm attribute
@cindex dllexport attribute
@cindex nodecorate attribute
@item The keyword @code{__attribute__} is handled to specify variable or
function attributes. The following attributes are supported:
@ -570,6 +670,8 @@ registers @code{%eax}, @code{%edx} and @code{%ecx}.
@item @code{dllexport}: export function from dll/executable (win32 only)
@item @code{nodecorate}: do not apply any decorations that would otherwise be applied when exporting function from dll/executable (win32 only)
@end itemize
Here are some examples:
@ -660,8 +762,6 @@ are supported.
@item Binary digits can be entered (@code{0b101} instead of
@code{5}).
@item @code{__BOUNDS_CHECKING_ON} is defined if bound checking is activated.
@end itemize
@node asm
@ -855,16 +955,7 @@ GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
@cindex bound checks
@cindex memory checks
This feature is activated with the @option{-b} (@pxref{Invoke}).
Note that pointer size is @emph{unchanged} and that code generated
with bound checks is @emph{fully compatible} with unchecked
code. When a pointer comes from unchecked code, it is assumed to be
valid. Even very obscure C code with casts should work correctly.
For more information about the ideas behind this method, see
@url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}.
This feature is activated with the @option{-b} option (@pxref{Invoke}).
Here are some examples of caught errors:
@table @asis
@ -893,7 +984,7 @@ Here are some examples of caught errors:
int *tab;
tab = malloc(20 * sizeof(int));
for(i=0;i<21;i++) @{
sum += tab4[i];
sum += tab[i];
@}
free(tab);
@}
@ -906,7 +997,7 @@ Here are some examples of caught errors:
tab = malloc(20 * sizeof(int));
free(tab);
for(i=0;i<20;i++) @{
sum += tab4[i];
sum += tab[i];
@}
@}
@end example
@ -920,9 +1011,68 @@ Here are some examples of caught errors:
free(tab);
@}
@end example
@end table
TCC defines @code{__TCC_BCHECK__} if activated.
There are five environment variables that can be used to control the behavior:
@itemize
@item TCC_BOUNDS_WARN_POINTER_ADD
- Print warning when pointer add creates an illegal pointer.
@item TCC_BOUNDS_PRINT_CALLS
- Print bound checking calls. Can be used for debugging.
@item TCC_BOUNDS_PRINT_HEAP
- Print heap objects that are not freed at exit of program.
@item TCC_BOUNDS_PRINT_STATISTIC
- Print statistic information at exit of program.
@item TCC_BOUNDS_NEVER_FATAL
- Try to continue in case of a bound checking error.
@end itemize
Also, a function @code{__bounds_checking(x)} can be used to turn off/on bounds
checking from usercode (see below).
Notes:
@itemize
@item Only available on i386 (linux and windows), x86_64 (linux and windows),
arm, arm64 and riscv64 for the moment.
@item The generated code is slower and bigger.
@item The bound checking code is not included in shared libraries. The main
executable should always be compiled with the @option{-b}.
@item Pointer size is @emph{unchanged} and code generated with bound checks is
@emph{fully compatible} with unchecked code. When a pointer comes from
unchecked code, it is assumed to be valid. Even very obscure C code with
casts should work correctly.
@item Signal handlers are not compatible with bounds checking. The
bounds checking code disables checking in signal/sigaction handlers.
The fork() function call in a multi threaded application is also a problem.
The bound checking code fixes this for the child process.
@item The reason that signals and fork have problems is that we use locking
inside the bounds checking code.
Inside a signal handler we can not use locks. Also in a multi threaded
application after a fork the child process can have the lock set
by another thread.
@item The BOUNDS_CHECKING_OFF and BOUNDS_CHECKING_ON can also be used to
disable bounds checking for some code.
@item The __bounds_checking call adds a value to a thread local value.
The value starts at 0. If the value is not 0 the code is not checked
for bounds checking errors.
@end itemize
@example
#ifdef __TCC_BCHECK__
extern void __bounds_checking (int x);
# define BOUNDS_CHECKING_OFF __bounds_checking(1)
# define BOUNDS_CHECKING_ON __bounds_checking(-1)
#else
# define BOUNDS_CHECKING_OFF
# define BOUNDS_CHECKING_ON
#endif
@end example
For more information about the ideas behind this method, see
@url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}.
@node Libtcc
@chapter The @code{libtcc} library
@ -1294,10 +1444,6 @@ floating point to integer conversion.
@item gen_cvt_ftof()
floating point to floating point of different size conversion.
@item gen_bounded_ptr_add()
@item gen_bounded_ptr_deref()
are only used for bounds checking.
@end table
@section Optimizations done

239
tcc.c
View File

@ -18,6 +18,10 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef ONE_SOURCE
# define ONE_SOURCE 1
#endif
#include "tcc.h"
#if ONE_SOURCE
# include "libtcc.c"
@ -27,7 +31,7 @@
static const char help[] =
"Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
"Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
" tcc [options...] -run infile [arguments...]\n"
" tcc [options...] -run infile (or --) [arguments...]\n"
"General options:\n"
" -c compile only - generate an object file\n"
" -o outfile set output filename\n"
@ -35,7 +39,8 @@ static const char help[] =
" -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
" -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
" -w disable all warnings\n"
" -v -vv show version, show search paths or loaded files\n"
" -v --version show version\n"
" -vv show search paths or loaded files\n"
" -h -hh show this, show more help\n"
" -bench show compilation statistics\n"
" - use stdin pipe as infile\n"
@ -45,35 +50,45 @@ static const char help[] =
" -Dsym[=val] define 'sym' with value 'val'\n"
" -Usym undefine 'sym'\n"
" -E preprocess only\n"
" -nostdinc do not use standard system include paths\n"
"Linker options:\n"
" -Ldir add library path 'dir'\n"
" -llib link with dynamic or static library 'lib'\n"
" -nostdlib do not link with standard crt and libraries\n"
" -r generate (relocatable) object file\n"
" -shared generate a shared library/dll\n"
" -rdynamic export all global symbols to dynamic linker\n"
" -shared generate a shared library/dll\n"
" -soname set name for shared library to be used at runtime\n"
" -Wl,-opt[=val] set linker option (see tcc -hh)\n"
"Debugger options:\n"
" -g generate runtime debug info\n"
" -g generate stab runtime debug info\n"
" -gdwarf[-x] generate dwarf runtime debug info\n"
#ifdef TCC_TARGET_PE
" -g.pdb create .pdb debug database\n"
#endif
#ifdef CONFIG_TCC_BCHECK
" -b compile with built-in memory and bounds checker (implies -g)\n"
#endif
#ifdef CONFIG_TCC_BACKTRACE
" -bt N show N callers in stack traces\n"
" -bt[N] link with backtrace (stack dump) support [show max N callers]\n"
#endif
"Misc. options:\n"
" -x[c|a|n] specify type of the next infile\n"
" -nostdinc do not use standard system include paths\n"
" -nostdlib do not link with standard crt and libraries\n"
" -std=version define __STDC_VERSION__ according to version (c11/gnu11)\n"
" -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
" -Bdir set tcc's private include/library dir\n"
" -MD generate dependency file for make\n"
" -M[M]D generate make dependency file [ignore system files]\n"
" -M[M] as above but no other output\n"
" -MF file specify dependency file name\n"
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
" -m32/64 defer to i386/x86_64 cross compiler\n"
#endif
"Tools:\n"
" create library : tcc -ar [rcsv] lib.a files\n"
" create library : tcc -ar [crstvx] lib [files]\n"
#ifdef TCC_TARGET_PE
" create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
#endif
"Discussion & bug reports:\n"
" https://lists.nongnu.org/mailman/listinfo/tinycc-devel\n"
;
static const char help2[] =
@ -85,19 +100,22 @@ static const char help2[] =
" -On same as -D__OPTIMIZE__ for n > 0\n"
" -Wp,-opt same as -opt\n"
" -include file include 'file' above each input file\n"
" -nostdlib do not link with standard crt/libs\n"
" -isystem dir add 'dir' to system include path\n"
" -static link to static libraries (not recommended)\n"
" -dumpversion print version\n"
" -print-search-dirs print search paths\n"
" -rstdin file with -run: use 'file' as custom stdin\n"
" -dt with -run/-E: auto-define 'test_...' macros\n"
"Ignored options:\n"
" --param -pedantic -pipe -s -std -traditional\n"
"-W... warnings:\n"
" -arch -C --param -pedantic -pipe -s -traditional\n"
"-W[no-]... warnings:\n"
" all turn on some (*) warnings\n"
" error stop after first warning\n"
" unsupported warn about ignored options, pragmas, etc.\n"
" error[=warning] stop after warning (any or specified)\n"
" write-strings strings are const\n"
" unsupported warn about ignored options, pragmas, etc.\n"
" implicit-function-declaration warn for missing prototype (*)\n"
" discarded-qualifiers warn when const is dropped (*)\n"
"-f[no-]... flags:\n"
" unsigned-char default char is unsigned\n"
" signed-char default char is signed\n"
@ -105,6 +123,10 @@ static const char help2[] =
" leading-underscore decorate extern symbols\n"
" ms-extensions allow anonymous struct in struct\n"
" dollars-in-identifiers allow '$' in C symbols\n"
" reverse-funcargs evaluate function arguments right to left\n"
" gnu89-inline 'extern inline' is like 'static inline'\n"
" asynchronous-unwind-tables create eh_frame section [on]\n"
" test-coverage create code coverage code\n"
"-m... target specific options:\n"
" ms-bitfields use MSVC bitfield layout\n"
#ifdef TCC_TARGET_ARM
@ -114,9 +136,10 @@ static const char help2[] =
" no-sse disable floats on x86_64\n"
#endif
"-Wl,... linker options:\n"
" -nostdlib do not link with standard crt/libs\n"
" -nostdlib do not search standard library paths\n"
" -[no-]whole-archive load lib(s) fully/only as needed\n"
" -export-all-symbols same as -rdynamic\n"
" -export-dynamic same as -rdynamic\n"
" -image-base= -Ttext= set base address of executable\n"
" -section-alignment= set section alignment in executable\n"
#ifdef TCC_TARGET_PE
@ -131,9 +154,14 @@ static const char help2[] =
" -rpath= set dynamic library search path\n"
" -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
" -soname= set DT_SONAME elf tag\n"
#if defined(TCC_TARGET_MACHO)
" -install_name= set DT_SONAME elf tag (soname macOS alias)\n"
#else
" -Ipath, -dynamic-linker=path set ELF interpreter to path\n"
#endif
" -Bsymbolic set DT_SYMBOLIC elf tag\n"
" -oformat=[elf32/64-* binary] set executable output format\n"
" -init= -fini= -as-needed -O (ignored)\n"
" -init= -fini= -Map= -as-needed -O -z= (ignored)\n"
"Predefined macros:\n"
" tcc -E -dM - < /dev/null\n"
#endif
@ -141,7 +169,11 @@ static const char help2[] =
;
static const char version[] =
"tcc version "TCC_VERSION" ("
"tcc version "TCC_VERSION
#ifdef TCC_GITHASH
" "TCC_GITHASH
#endif
" ("
#ifdef TCC_TARGET_I386
"i386"
#elif defined TCC_TARGET_X86_64
@ -150,18 +182,27 @@ static const char version[] =
"C67"
#elif defined TCC_TARGET_ARM
"ARM"
# ifdef TCC_ARM_EABI
" eabi"
# ifdef TCC_ARM_HARDFLOAT
"hf"
# endif
# endif
#elif defined TCC_TARGET_ARM64
"AArch64"
#endif
#ifdef TCC_ARM_HARDFLOAT
" Hard Float"
#elif defined TCC_TARGET_RISCV64
"riscv64"
#endif
#ifdef TCC_TARGET_PE
" Windows"
#elif defined(TCC_TARGET_MACHO)
" Darwin"
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#elif TARGETOS_FreeBSD || TARGETOS_FreeBSD_kernel
" FreeBSD"
#elif TARGETOS_OpenBSD
" OpenBSD"
#elif TARGETOS_NetBSD
" NetBSD"
#else
" Linux"
#endif
@ -182,10 +223,10 @@ static void print_search_dirs(TCCState *s)
/* print_dirs("programs", NULL, 0); */
print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
print_dirs("libraries", s->library_paths, s->nb_library_paths);
printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
#ifndef TCC_TARGET_PE
printf("libtcc1:\n %s/%s\n", s->library_paths[0], CONFIG_TCC_CROSSPREFIX TCC_LIBTCC1);
#ifdef TCC_TARGET_UNIX
print_dirs("crt", s->crt_paths, s->nb_crt_paths);
printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
printf("elfinterp:\n %s\n", s->elfint);
#endif
}
@ -215,7 +256,9 @@ static char *default_outputfile(TCCState *s, const char *first_file)
if (first_file && strcmp(first_file, "-"))
name = tcc_basename(first_file);
snprintf(buf, sizeof(buf), "%s", name);
if (strlen(name) + 4 >= sizeof buf)
name = "a";
strcpy(buf, name);
ext = tcc_fileextension(buf);
#ifdef TCC_TARGET_PE
if (s->output_type == TCC_OUTPUT_DLL)
@ -225,7 +268,7 @@ static char *default_outputfile(TCCState *s, const char *first_file)
strcpy(ext, ".exe");
else
#endif
if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
if ((s->just_deps || s->output_type == TCC_OUTPUT_OBJ) && !s->option_r && *ext)
strcpy(ext, ".o");
else
strcpy(buf, "a.out");
@ -243,65 +286,68 @@ static unsigned getclock_ms(void)
#endif
}
int main(int argc0, char **argv0)
int main(int argc, char **argv)
{
TCCState *s;
int ret, opt, n = 0, t = 0;
unsigned start_time = 0;
TCCState *s, *s1;
int ret, opt, n = 0, t = 0, done;
unsigned start_time = 0, end_time = 0;
const char *first_file;
int argc; char **argv;
FILE *ppfp = stdout;
int argc0 = argc;
char **argv0 = argv;
FILE *ppfp = NULL;
redo:
argc = argc0, argv = argv0;
s = tcc_new();
opt = tcc_parse_args(s, &argc, &argv, 1);
s = s1 = tcc_new();
opt = tcc_parse_args(s, &argc, &argv);
if ((n | t) == 0) {
if (opt == OPT_HELP)
return printf(help), 1;
if (opt == OPT_HELP2)
return printf(help2), 1;
if (opt == OPT_M32 || opt == OPT_M64)
tcc_tool_cross(s, argv, opt); /* never returns */
if (n == 0) {
ret = 0;
if (opt == OPT_HELP) {
fputs(help, stdout);
if (s->verbose)
printf(version);
goto help2;
} else if (opt == OPT_HELP2) {
help2: fputs(help2, stdout);
} else if (opt == OPT_M32 || opt == OPT_M64) {
ret = tcc_tool_cross(argv, opt);
} else if (s->verbose)
printf("%s", version);
if (opt == OPT_AR)
return tcc_tool_ar(s, argc, argv);
ret = tcc_tool_ar(argc, argv);
#ifdef TCC_TARGET_PE
if (opt == OPT_IMPDEF)
return tcc_tool_impdef(s, argc, argv);
ret = tcc_tool_impdef(argc, argv);
#endif
if (opt == OPT_V)
return 0;
if (opt == OPT_PRINT_DIRS) {
/* initialize search dirs */
set_environment(s);
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
print_search_dirs(s);
return 0;
}
n = s->nb_files;
if (n == 0)
tcc_error("no input files\n");
if (s->output_type == TCC_OUTPUT_PREPROCESS) {
if (s->outfile) {
ppfp = fopen(s->outfile, "w");
if (opt) {
if (opt < 0) err:
ret = 1;
tcc_delete(s);
return ret;
}
if (s->nb_files == 0) {
tcc_error_noabort("no input files");
} else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
if (s->outfile && 0!=strcmp("-",s->outfile)) {
ppfp = tcc_fopen(s->outfile, "wb");
if (!ppfp)
tcc_error("could not write '%s'", s->outfile);
tcc_error_noabort("could not write '%s'", s->outfile);
}
} else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
if (s->nb_libraries)
tcc_error("cannot specify libraries with -c");
if (n > 1 && s->outfile)
tcc_error("cannot specify output file with -c many files");
} else {
if (s->option_pthread)
tcc_set_options(s, "-lpthread");
tcc_error_noabort("cannot specify libraries with -c");
else if (s->nb_files > 1 && s->outfile)
tcc_error_noabort("cannot specify output file with -c many files");
}
if (s->nb_errors)
goto err;
if (s->do_bench)
start_time = getclock_ms();
}
@ -309,36 +355,43 @@ redo:
set_environment(s);
if (s->output_type == 0)
s->output_type = TCC_OUTPUT_EXE;
tcc_set_output_type(s, s->output_type);
ret = tcc_set_output_type(s, s->output_type);
if (ppfp)
s->ppfp = ppfp;
if ((s->output_type == TCC_OUTPUT_MEMORY
|| s->output_type == TCC_OUTPUT_PREPROCESS) && (s->dflag & 16))
s->dflag |= t ? 32 : 0, s->run_test = ++t, n = s->nb_files;
|| s->output_type == TCC_OUTPUT_PREPROCESS)
&& (s->dflag & 16)) { /* -dt option */
if (t)
s->dflag |= 32;
s->run_test = ++t;
if (n)
--n;
}
/* compile or add each files or library */
for (first_file = NULL, ret = 0;;) {
struct filespec *f = s->files[s->nb_files - n];
first_file = NULL;
while (0 == ret) {
struct filespec *f = s->files[n];
s->filetype = f->type;
s->alacarte_link = f->alacarte;
if (f->type == AFF_TYPE_LIB) {
if (tcc_add_library_err(s, f->name) < 0)
ret = 1;
if (f->type & AFF_TYPE_LIB) {
ret = tcc_add_library(s, f->name);
} else {
if (1 == s->verbose)
printf("-> %s\n", f->name);
if (!first_file)
first_file = f->name;
if (tcc_add_file(s, f->name) < 0)
ret = 1;
ret = tcc_add_file(s, f->name);
}
s->filetype = 0;
s->alacarte_link = 1;
if (--n == 0 || ret
|| (s->output_type == TCC_OUTPUT_OBJ && !s->option_r))
if (++n == s->nb_files)
break;
if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r)
break;
}
if (s->do_bench)
end_time = getclock_ms();
if (s->run_test) {
t = 0;
} else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
@ -351,21 +404,29 @@ redo:
} else {
if (!s->outfile)
s->outfile = default_outputfile(s, first_file);
if (tcc_output_file(s, s->outfile))
ret = 1;
else if (s->gen_deps)
if (!s->just_deps)
ret = tcc_output_file(s, s->outfile);
if (!ret && s->gen_deps)
gen_makedeps(s, s->outfile, s->deps_outfile);
}
}
if (s->do_bench && (n | t | ret) == 0)
tcc_print_stats(s, getclock_ms() - start_time);
tcc_delete(s);
if (ret == 0 && n)
goto redo; /* compile more files with -c */
done = 1;
if (t)
goto redo; /* run more tests with -dt -run */
if (ppfp && ppfp != stdout)
fclose(ppfp);
done = 0; /* run more tests with -dt -run */
else if (ret) {
if (s->nb_errors)
ret = 1;
/* else keep the original exit code from tcc_run() */
} else if (n < s->nb_files)
done = 0; /* compile more files with -c */
else if (s->do_bench)
tcc_print_stats(s, end_time - start_time);
tcc_delete(s);
if (!done)
goto redo;
if (ppfp)
tcc_fclose(ppfp);
return ret;
}

1337
tcc.h

File diff suppressed because it is too large Load Diff

456
tccasm.c
View File

@ -18,38 +18,91 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define USING_GLOBALS
#include "tcc.h"
#ifdef CONFIG_TCC_ASM
ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n)
{
char buf[64];
TokenSym *ts;
snprintf(buf, sizeof(buf), "L..%u", n);
ts = tok_alloc(buf, strlen(buf));
return ts->tok;
}
static Section *last_text_section; /* to handle .previous asm directive */
static int asmgoto_n;
static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global);
static Sym* asm_new_label(TCCState *s1, int label, int is_local);
static Sym* asm_new_label1(TCCState *s1, int label, int is_local, int sh_num, int value);
#if PTR_SIZE == 8
/* output constant with relocation if 'r & VT_SYM' is true */
ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c)
{
if (r & VT_SYM)
greloca(cur_text_section, sym, ind, R_DATA_PTR, c), c=0;
gen_le32(c);
gen_le32(c>>32);
}
ST_FUNC void gen_expr64(ExprValue *pe)
{
gen_addr64(pe->sym ? VT_SYM : 0, pe->sym, pe->v);
}
#endif
static int asm_get_prefix_name(TCCState *s1, const char *prefix, unsigned int n)
{
char buf[64];
snprintf(buf, sizeof(buf), "%s%u", prefix, n);
return tok_alloc_const(buf);
}
ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n)
{
return asm_get_prefix_name(s1, "L..", n);
}
/* If a C name has an _ prepended then only asm labels that start
with _ are representable in C, by removing the first _. ASM names
without _ at the beginning don't correspond to C names, but we use
the global C symbol table to track ASM names as well, so we need to
transform those into ones that don't conflict with a C name,
so prepend a '.' for them, but force the ELF asm name to be set. */
static int asm2cname(int v, int *addeddot)
{
const char *name;
*addeddot = 0;
if (!tcc_state->leading_underscore)
return v;
name = get_tok_str(v, NULL);
if (!name)
return v;
if (name[0] == '_') {
v = tok_alloc_const(name + 1);
} else if (!strchr(name, '.')) {
char newname[256];
snprintf(newname, sizeof newname, ".%s", name);
v = tok_alloc_const(newname);
*addeddot = 1;
}
return v;
}
static Sym *asm_label_find(int v)
{
Sym *sym = sym_find(v);
while (sym && sym->sym_scope)
Sym *sym;
int addeddot;
v = asm2cname(v, &addeddot);
sym = sym_find(v);
while (sym && sym->sym_scope && !(sym->type.t & VT_STATIC))
sym = sym->prev_tok;
return sym;
}
static Sym *asm_label_push(int v)
{
int addeddot, v2 = asm2cname(v, &addeddot);
/* We always add VT_EXTERN, for sym definition that's tentative
(for .set, removed for real defs), for mere references it's correct
as is. */
Sym *sym = global_identifier_push(v, VT_ASM | VT_EXTERN | VT_STATIC, 0);
sym->r = VT_CONST | VT_SYM;
Sym *sym = global_identifier_push(v2, VT_ASM | VT_EXTERN | VT_STATIC, 0);
if (addeddot)
sym->asm_label = v;
return sym;
}
@ -76,11 +129,10 @@ ST_FUNC Sym* get_asm_sym(int name, Sym *csym)
static Sym* asm_section_sym(TCCState *s1, Section *sec)
{
char buf[100];
int label = tok_alloc(buf,
snprintf(buf, sizeof buf, "L.%s", sec->name)
)->tok;
Sym *sym = asm_label_find(label);
char buf[100]; int label; Sym *sym;
snprintf(buf, sizeof buf, "L.%s", sec->name);
label = tok_alloc_const(buf);
sym = asm_label_find(label);
return sym ? sym : asm_new_label1(s1, label, 1, sec->sh_num, 0);
}
@ -107,7 +159,7 @@ static void asm_expr_unary(TCCState *s1, ExprValue *pe)
if (sym && (!sym->c || elfsym(sym)->st_shndx == SHN_UNDEF))
sym = sym->prev_tok;
if (!sym)
tcc_error("local label '%d' not found backward", n);
tcc_error("local label '%d' not found backward", (int)n);
} else {
/* forward */
if (!sym || (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF)) {
@ -287,15 +339,17 @@ static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
ElfSym *esym1, *esym2;
esym1 = elfsym(pe->sym);
esym2 = elfsym(e2.sym);
if (!esym2)
goto cannot_relocate;
if (esym1 && esym1->st_shndx == esym2->st_shndx
&& esym1->st_shndx != SHN_UNDEF) {
/* we also accept defined symbols in the same section */
pe->v += esym1->st_value - esym2->st_value;
pe->v += (int)(esym1->st_value - esym2->st_value);
pe->sym = NULL;
} else if (esym2->st_shndx == cur_text_section->sh_num) {
/* When subtracting a defined symbol in current section
this actually makes the value PC-relative. */
pe->v -= esym2->st_value - ind - 4;
pe->v += (int)(0 - esym2->st_value);
pe->pcrel = 1;
e2.sym = NULL;
} else {
@ -360,6 +414,8 @@ ST_FUNC int asm_int_expr(TCCState *s1)
asm_expr(s1, &e);
if (e.sym)
expect("constant");
if ((int)e.v != e.v)
tcc_error("integer out of range %lld", (long long)e.v);
return e.v;
}
@ -389,7 +445,7 @@ static Sym* asm_new_label1(TCCState *s1, int label, int is_local,
sym = asm_label_push(label);
}
if (!sym->c)
put_extern_sym2(sym, SHN_UNDEF, 0, 0, 0);
put_extern_sym2(sym, SHN_UNDEF, 0, 0, 1);
esym = elfsym(sym);
esym->st_shndx = sh_num;
esym->st_value = value;
@ -454,7 +510,7 @@ static void pop_section(TCCState *s1)
static void asm_parse_directive(TCCState *s1, int global)
{
int n, offset, v, size, tok1;
int n, offset, v, size, tok1, c;
Section *sec;
uint8_t *ptr;
@ -477,25 +533,32 @@ static void asm_parse_directive(TCCState *s1, int global)
tok1 = TOK_ASMDIR_align;
}
if (tok1 == TOK_ASMDIR_align || tok1 == TOK_ASMDIR_balign) {
if (n < 0 || (n & (n-1)) != 0)
if (n <= 0 || (n & (n-1)) != 0)
tcc_error("alignment must be a positive power of two");
offset = (ind + n - 1) & -n;
size = offset - ind;
/* the section must have a compatible alignment */
if (sec->sh_addralign < n)
sec->sh_addralign = n;
c = sec->sh_flags & SHF_EXECINSTR;
} else {
if (n < 0)
n = 0;
size = n;
size = n, c = 0;
}
v = 0;
if (tok == ',') {
next();
v = asm_int_expr(s1);
v = asm_int_expr(s1), c = 0;
}
zero_pad:
if ((uint64_t)ind + size >= 1<<30)
tcc_error("too much data");
if (sec->sh_type != SHT_NOBITS) {
if (c) {
gen_fill_nops(size);
break;
}
sec->data_offset = ind;
ptr = section_ptr_add(sec, size);
memset(ptr, v, size);
@ -503,7 +566,7 @@ static void asm_parse_directive(TCCState *s1, int global)
ind += size;
break;
case TOK_ASMDIR_quad:
#ifdef TCC_TARGET_X86_64
#if PTR_SIZE == 8
size = 8;
goto asm_data;
#else
@ -552,7 +615,7 @@ static void asm_parse_directive(TCCState *s1, int global)
if (sec->sh_type != SHT_NOBITS) {
if (size == 4) {
gen_expr32(&e);
#ifdef TCC_TARGET_X86_64
#if PTR_SIZE == 8
} else if (size == 8) {
gen_expr64(&e);
#endif
@ -626,8 +689,7 @@ static void asm_parse_directive(TCCState *s1, int global)
tcc_error("we at end of file, .endr not found");
tok_str_add_tok(init_str);
}
tok_str_add(init_str, -1);
tok_str_add(init_str, 0);
tok_str_add(init_str, TOK_EOF);
begin_macro(init_str, 1);
while (repeat-- > 0) {
tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS),
@ -640,7 +702,6 @@ static void asm_parse_directive(TCCState *s1, int global)
}
case TOK_ASMDIR_org:
{
unsigned long n;
ExprValue e;
ElfSym *esym;
next();
@ -654,7 +715,7 @@ static void asm_parse_directive(TCCState *s1, int global)
}
if (n < ind)
tcc_error("attempt to .org backwards");
v = 0;
v = c = 0;
size = n - ind;
goto zero_pad;
}
@ -676,6 +737,8 @@ static void asm_parse_directive(TCCState *s1, int global)
do {
Sym *sym;
next();
if (tok < TOK_IDENT)
expect("identifier");
sym = get_asm_sym(tok, NULL);
if (tok1 != TOK_ASMDIR_hidden)
sym->type.t &= ~VT_STATIC;
@ -691,7 +754,7 @@ static void asm_parse_directive(TCCState *s1, int global)
case TOK_ASMDIR_ascii:
case TOK_ASMDIR_asciz:
{
const uint8_t *p;
const char *p;
int i, size, t;
t = tok;
@ -735,19 +798,21 @@ static void asm_parse_directive(TCCState *s1, int global)
break;
case TOK_ASMDIR_file:
{
char filename[512];
filename[0] = '\0';
const char *p;
parse_flags &= ~PARSE_FLAG_TOK_STR;
next();
if (tok == TOK_STR)
pstrcat(filename, sizeof(filename), tokc.str.data);
else
pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
if (s1->warn_unsupported)
tcc_warning("ignoring .file %s", filename);
if (tok == TOK_PPNUM)
next();
if (tok == TOK_PPSTR && tokc.str.data[0] == '"') {
tokc.str.data[tokc.str.size - 2] = 0;
p = tokc.str.data + 1;
} else if (tok >= TOK_IDENT) {
p = get_tok_str(tok, &tokc);
} else {
skip_to_eol(0);
break;
}
tccpp_putfile(p);
next();
}
break;
@ -757,36 +822,33 @@ static void asm_parse_directive(TCCState *s1, int global)
ident[0] = '\0';
next();
if (tok == TOK_STR)
pstrcat(ident, sizeof(ident), tokc.str.data);
else
pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
if (s1->warn_unsupported)
tcc_warning("ignoring .ident %s", ident);
pstrcat(ident, sizeof(ident), get_tok_str(tok, &tokc));
tcc_warning_c(warn_unsupported)("ignoring .ident %s", ident);
next();
}
break;
case TOK_ASMDIR_size:
{
Sym *sym;
ElfSym *esym;
next();
if (tok < TOK_IDENT)
expect("identifier");
sym = asm_label_find(tok);
if (!sym) {
if (!sym)
tcc_error("label not found: %s", get_tok_str(tok, NULL));
}
/* XXX .size name,label2-label1 */
if (s1->warn_unsupported)
tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL));
tcc_warning_c(warn_unsupported)("ignoring .size %s,*", get_tok_str(tok, NULL));
next();
skip(',');
while (tok != TOK_LINEFEED && tok != ';' && tok != CH_EOF) {
next();
n = asm_int_expr(s1);
esym = elfsym(sym);
if (esym) {
esym->st_size = n;
}
}
break;
@ -794,8 +856,11 @@ static void asm_parse_directive(TCCState *s1, int global)
{
Sym *sym;
const char *newtype;
int st_type;
next();
if (tok < TOK_IDENT)
expect("identifier");
sym = get_asm_sym(tok, NULL);
next();
skip(',');
@ -808,10 +873,19 @@ static void asm_parse_directive(TCCState *s1, int global)
}
if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
if (IS_ASM_SYM(sym))
sym->type.t |= VT_ASM_FUNC;
st_type = STT_FUNC;
set_st_type:
if (sym->c) {
ElfSym *esym = elfsym(sym);
esym->st_info = ELFW(ST_INFO)(ELFW(ST_BIND)(esym->st_info), st_type);
}
else if (s1->warn_unsupported)
tcc_warning("change type of '%s' from 0x%x to '%s' ignored",
} else if (!strcmp(newtype, "object") || !strcmp(newtype, "STT_OBJECT")) {
st_type = STT_OBJECT;
goto set_st_type;
} else
tcc_warning_c(warn_unsupported)("change type of '%s' from 0x%x to '%s' ignored",
get_tok_str(sym->v, NULL), sym->type.t, newtype);
next();
@ -822,6 +896,7 @@ static void asm_parse_directive(TCCState *s1, int global)
{
char sname[256];
int old_nb_section = s1->nb_sections;
int flags = SHF_ALLOC;
tok1 = tok;
/* XXX: support more options */
@ -835,10 +910,17 @@ static void asm_parse_directive(TCCState *s1, int global)
next();
}
if (tok == ',') {
const char *p;
/* skip section options */
next();
if (tok != TOK_STR)
expect("string constant");
for (p = tokc.str.data; *p; ++p) {
if (*p == 'w')
flags |= SHF_WRITE;
if (*p == 'x')
flags |= SHF_EXECINSTR;
}
next();
if (tok == ',') {
next();
@ -855,8 +937,14 @@ static void asm_parse_directive(TCCState *s1, int global)
/* If we just allocated a new section reset its alignment to
1. new_section normally acts for GCC compatibility and
sets alignment to PTR_SIZE. The assembler behaves different. */
if (old_nb_section != s1->nb_sections)
if (old_nb_section != s1->nb_sections) {
cur_text_section->sh_addralign = 1;
/* Make .init and .fini sections executable by default.
GAS does so, too, and musl relies on it. */
if (!strcmp(sname, ".init") || !strcmp(sname, ".fini"))
flags |= SHF_EXECINSTR;
cur_text_section->sh_flags = flags;
}
}
break;
case TOK_ASMDIR_previous:
@ -888,12 +976,87 @@ static void asm_parse_directive(TCCState *s1, int global)
}
break;
#endif
#ifdef TCC_TARGET_X86_64
#if PTR_SIZE == 8
/* added for compatibility with GAS */
case TOK_ASMDIR_code64:
next();
break;
#endif
#ifdef TCC_TARGET_RISCV64
case TOK_ASMDIR_option:
next();
switch(tok){
case TOK_ASM_rvc: /* Will be deprecated soon in favor of arch */
case TOK_ASM_norvc: /* Will be deprecated soon in favor of arch */
case TOK_ASM_pic:
case TOK_ASM_nopic:
case TOK_ASM_relax:
case TOK_ASM_norelax:
case TOK_ASM_push:
case TOK_ASM_pop:
/* TODO: unimplemented */
next();
break;
case TOK_ASM_arch:
/* TODO: unimplemented, requires extra parsing */
tcc_error("unimp .option '.%s'", get_tok_str(tok, NULL));
break;
default:
tcc_error("unknown .option '.%s'", get_tok_str(tok, NULL));
break;
}
break;
#endif
/* TODO: Implement symvar support. FreeBSD >= 14 needs this */
case TOK_ASMDIR_symver:
next();
next();
skip(',');
next();
skip('@');
next();
break;
case TOK_ASMDIR_reloc:
{
ExprValue e;
const char *reloc_name;
int reloc_type = -1;
next();
asm_expr(s1, &e);
skip(',');
reloc_name = get_tok_str(tok, NULL);
#if defined(TCC_TARGET_ARM64)
if (!strcmp(reloc_name, "R_AARCH64_CALL26"))
reloc_type = R_AARCH64_CALL26;
#elif defined(TCC_TARGET_RISCV64)
if (!strcmp(reloc_name, "R_RISCV_CALL") || !strcmp(reloc_name, "R_RISCV_CALL_PLT"))
reloc_type = R_RISCV_CALL;
else if (!strcmp(reloc_name, "R_RISCV_BRANCH"))
reloc_type = R_RISCV_BRANCH;
else if (!strcmp(reloc_name, "R_RISCV_JAL"))
reloc_type = R_RISCV_JAL;
else if (!strcmp(reloc_name, "R_RISCV_PCREL_HI20"))
reloc_type = R_RISCV_PCREL_HI20;
else if (!strcmp(reloc_name, "R_RISCV_PCREL_LO12_I"))
reloc_type = R_RISCV_PCREL_LO12_I;
else if (!strcmp(reloc_name, "R_RISCV_PCREL_LO12_S"))
reloc_type = R_RISCV_PCREL_LO12_S;
else if (!strcmp(reloc_name, "R_RISCV_32_PCREL"))
reloc_type = R_RISCV_32_PCREL;
else if (!strcmp(reloc_name, "R_RISCV_32"))
reloc_type = R_RISCV_32;
else if (!strcmp(reloc_name, "R_RISCV_64"))
reloc_type = R_RISCV_64;
#endif
if (reloc_type < 0)
tcc_error("unimp: reloc '%s' unknown", reloc_name);
next();
skip(',');
greloca(cur_text_section, get_asm_sym(tok, NULL), e.v, reloc_type, 0);
next();
}
break;
default:
tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
break;
@ -914,16 +1077,17 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global)
next();
if (tok == TOK_EOF)
break;
/* generate line number info */
if (global && s1->do_debug)
tcc_debug_line(s1);
parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
redo:
#if !defined(TCC_TARGET_ARM64)
if (tok == '#') {
/* horrible gas comment */
while (tok != TOK_LINEFEED)
next();
} else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
} else
#endif
if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
asm_parse_directive(s1, global);
} else if (tok == TOK_PPNUM) {
const char *p;
@ -982,12 +1146,14 @@ ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
/* GCC inline asm support */
/* assemble the string 'str' in the current C compilation unit without
C preprocessing. NOTE: str is modified by modifying the '\0' at the
end */
static void tcc_assemble_inline(TCCState *s1, char *str, int len, int global)
C preprocessing. */
static void tcc_assemble_inline(TCCState *s1, const char *str, int len, int global)
{
const int *saved_macro_ptr = macro_ptr;
int dotid = set_idnum('.', IS_ID);
#if !defined(TCC_TARGET_RISCV64) && !defined(TCC_TARGET_X86_64)
int dolid = set_idnum('$', 0);
#endif
tcc_open_bf(s1, ":asm:", len);
memcpy(file->buffer, str, len);
@ -995,6 +1161,9 @@ static void tcc_assemble_inline(TCCState *s1, char *str, int len, int global)
tcc_assemble_internal(s1, 0, global);
tcc_close();
#if !defined(TCC_TARGET_RISCV64) && !defined(TCC_TARGET_X86_64)
set_idnum('$', dolid);
#endif
set_idnum('.', dotid);
macro_ptr = saved_macro_ptr;
}
@ -1041,15 +1210,12 @@ ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
}
static void subst_asm_operands(ASMOperand *operands, int nb_operands,
CString *out_str, CString *in_str)
CString *out_str, const char *str)
{
int c, index, modifier;
const char *str;
ASMOperand *op;
SValue sv;
cstr_new(out_str);
str = in_str->data;
for(;;) {
c = *str++;
if (c == '%') {
@ -1060,22 +1226,35 @@ static void subst_asm_operands(ASMOperand *operands, int nb_operands,
modifier = 0;
if (*str == 'c' || *str == 'n' ||
*str == 'b' || *str == 'w' || *str == 'h' || *str == 'k' ||
*str == 'q' ||
*str == 'q' || *str == 'l' ||
#ifdef TCC_TARGET_ARM64
*str == 'x' || *str == 's' || *str == 'd' || *str == 'Z' ||
#endif
#ifdef TCC_TARGET_RISCV64
*str == 'z' ||
#endif
/* P in GCC would add "@PLT" to symbol refs in PIC mode,
and make literal operands not be decorated with '$'. */
*str == 'P')
modifier = *str++;
index = find_constraint(operands, nb_operands, str, &str);
if (index < 0)
error:
tcc_error("invalid operand reference after %%");
op = &operands[index];
if (modifier == 'l') {
cstr_cat(out_str, get_tok_str(op->is_label, NULL), -1);
} else {
if (op->vt == NULL)
goto error;
sv = *op->vt;
if (op->reg >= 0) {
sv.r = op->reg;
if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
if (op->is_memory)
sv.r |= VT_LVAL;
}
subst_asm_operand(out_str, &sv, modifier);
}
} else {
add_char:
cstr_ccat(out_str, c);
@ -1091,11 +1270,11 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
{
ASMOperand *op;
int nb_operands;
char* astr;
if (tok != ':') {
nb_operands = *nb_operands_ptr;
for(;;) {
CString astr;
if (nb_operands >= MAX_ASM_OPERANDS)
tcc_error("too many asm operands");
op = &operands[nb_operands++];
@ -1108,10 +1287,8 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
next();
skip(']');
}
parse_mult_str(&astr, "string constant");
op->constraint = tcc_malloc(astr.size);
strcpy(op->constraint, astr.data);
cstr_free(&astr);
astr = parse_mult_str("string constant")->data;
pstrcpy(op->constraint, sizeof op->constraint, astr);
skip('(');
gexpr();
if (is_output) {
@ -1125,7 +1302,12 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
if ((vtop->r & VT_LVAL) &&
((vtop->r & VT_VALMASK) == VT_LLOCAL ||
(vtop->r & VT_VALMASK) < VT_CONST) &&
!strchr(op->constraint, 'm')) {
!strchr(op->constraint, 'm')
#ifdef TCC_TARGET_ARM64
&& !strchr(op->constraint, 'Q')
&& !strstr(op->constraint, "Ump")
#endif
) {
gv(RC_INT);
}
}
@ -1144,20 +1326,27 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
/* parse the GCC asm() instruction */
ST_FUNC void asm_instr(void)
{
CString astr, astr1;
ASMOperand operands[MAX_ASM_OPERANDS];
int nb_outputs, nb_operands, i, must_subst, out_reg;
uint8_t clobber_regs[NB_ASM_REGS];
CString astr, *astr1;
ASMOperand operands[MAX_ASM_OPERANDS];
int nb_outputs, nb_operands, i, must_subst, out_reg, nb_labels;
uint8_t clobber_regs[NB_ASM_REGS];
Section *sec;
next();
/* since we always generate the asm() instruction, we can ignore
volatile */
if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
while (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3
|| tok == TOK_GOTO) {
next();
}
parse_asm_str(&astr);
astr1 = parse_asm_str();
cstr_new_s(&astr);
cstr_cat(&astr, astr1->data, astr1->size);
nb_operands = 0;
nb_outputs = 0;
nb_labels = 0;
must_subst = 0;
memset(clobber_regs, 0, sizeof(clobber_regs));
if (tok == ':') {
@ -1176,6 +1365,8 @@ ST_FUNC void asm_instr(void)
/* XXX: handle registers */
next();
for(;;) {
if (tok == ':')
break;
if (tok != TOK_STR)
expect("string constant");
asm_clobber(clobber_regs, tokc.str.data);
@ -1187,6 +1378,41 @@ ST_FUNC void asm_instr(void)
}
}
}
if (tok == ':') {
/* goto labels */
next();
for (;;) {
Sym *csym;
int asmname;
if (nb_operands + nb_labels >= MAX_ASM_OPERANDS)
tcc_error("too many asm operands");
if (tok < TOK_UIDENT)
expect("label identifier");
memset(operands + nb_operands + nb_labels, 0,
sizeof(operands[0]));
operands[nb_operands + nb_labels++].id = tok;
csym = label_find(tok);
if (!csym) {
csym = label_push(&global_label_stack, tok,
LABEL_FORWARD);
} else {
if (csym->r == LABEL_DECLARED)
csym->r = LABEL_FORWARD;
}
next();
asmname = asm_get_prefix_name(tcc_state, "LG.",
++asmgoto_n);
if (!csym->c)
put_extern_sym2(csym, SHN_UNDEF, 0, 0, 1);
get_asm_sym(asmname, csym);
operands[nb_operands + nb_labels - 1].is_label = asmname;
if (tok != ',')
break;
next();
}
}
}
}
}
@ -1209,21 +1435,30 @@ ST_FUNC void asm_instr(void)
printf("asm: \"%s\"\n", (char *)astr.data);
#endif
if (must_subst) {
subst_asm_operands(operands, nb_operands, &astr1, &astr);
cstr_free(&astr);
} else {
astr1 = astr;
cstr_reset(astr1);
cstr_cat(astr1, astr.data, astr.size);
cstr_reset(&astr);
subst_asm_operands(operands, nb_operands + nb_labels, &astr, astr1->data);
}
#ifdef ASM_DEBUG
printf("subst_asm: \"%s\"\n", (char *)astr1.data);
printf("subst_asm: \"%s\"\n", (char *)astr.data);
#endif
/* generate loads */
asm_gen_code(operands, nb_operands, nb_outputs, 0,
clobber_regs, out_reg);
/* We don't allow switching section within inline asm to
bleed out to surrounding code. */
sec = cur_text_section;
/* assemble the string with tcc internal assembler */
tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1, 0);
tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 0);
cstr_free_s(&astr);
if (sec != cur_text_section) {
tcc_warning("inline asm tries to change current section");
use_section1(tcc_state, sec);
}
/* restore the current C token */
next();
@ -1234,23 +1469,20 @@ ST_FUNC void asm_instr(void)
/* free everything */
for(i=0;i<nb_operands;i++) {
ASMOperand *op;
op = &operands[i];
tcc_free(op->constraint);
vpop();
}
cstr_free(&astr1);
}
ST_FUNC void asm_global_instr(void)
{
CString astr;
CString *astr;
int saved_nocode_wanted = nocode_wanted;
/* Global asm blocks are always emitted. */
nocode_wanted = 0;
next();
parse_asm_str(&astr);
astr = parse_asm_str();
skip(')');
/* NOTE: we do not eat the ';' so that we can restore the current
token after the assembler parsing */
@ -1258,20 +1490,36 @@ ST_FUNC void asm_global_instr(void)
expect("';'");
#ifdef ASM_DEBUG
printf("asm_global: \"%s\"\n", (char *)astr.data);
printf("asm_global: \"%s\"\n", (char *)astr->data);
#endif
cur_text_section = text_section;
ind = cur_text_section->data_offset;
/* assemble the string with tcc internal assembler */
tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 1);
tcc_assemble_inline(tcc_state, astr->data, astr->size - 1, 1);
cur_text_section->data_offset = ind;
/* restore the current C token */
next();
cstr_free(&astr);
nocode_wanted = saved_nocode_wanted;
}
/********************************************************/
#else
ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
{
tcc_error("asm not supported");
}
ST_FUNC void asm_instr(void)
{
tcc_error("inline asm() not supported");
}
ST_FUNC void asm_global_instr(void)
{
tcc_error("inline asm() not supported");
}
#endif /* CONFIG_TCC_ASM */

View File

@ -21,6 +21,9 @@
#include "tcc.h"
/* XXX: this file uses tcc_error() to the effect of exit(1) */
#undef _tcc_error
#define MAXNSCNS 255 /* MAXIMUM NUMBER OF SECTIONS */
#define MAX_STR_TABLE 1000000
AOUTHDR o_filehdr; /* OPTIONAL (A.OUT) FILE HEADER */
@ -40,12 +43,12 @@ int FuncEntries[MAX_FUNCS];
int OutputTheSection(Section * sect);
short int GetCoffFlags(const char *s);
void SortSymbolTable(void);
void SortSymbolTable(TCCState *s1);
Section *FindSection(TCCState * s1, const char *sname);
int C67_main_entry_point;
int FindCoffSymbolIndex(const char *func_name);
int FindCoffSymbolIndex(TCCState * s1, const char *func_name);
int nb_syms;
typedef struct {
@ -92,7 +95,7 @@ ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
sbss = FindSection(s1, ".bss");
nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1");
coff_nb_syms = FindCoffSymbolIndex(s1, "XXXXXXXXXX1");
file_hdr.f_magic = COFF_C67_MAGIC; /* magic number */
file_hdr.f_timdat = 0; /* time & date stamp */
@ -366,7 +369,7 @@ ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
// finally global symbols
if (s1->do_debug)
SortSymbolTable();
SortSymbolTable(s1);
// write line no data
@ -437,7 +440,7 @@ ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
// output a function begin
CoffLineNo.l_addr.l_symndx =
FindCoffSymbolIndex(func_name);
FindCoffSymbolIndex(s1, func_name);
CoffLineNo.l_lnno = 0;
fwrite(&CoffLineNo, 6, 1, f);
@ -690,7 +693,7 @@ ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
// group the symbols in order of filename, func1, func2, etc
// finally global symbols
void SortSymbolTable(void)
void SortSymbolTable(TCCState *s1)
{
int i, j, k, n = 0;
Elf32_Sym *p, *p2, *NewTable;
@ -770,7 +773,7 @@ void SortSymbolTable(void)
}
int FindCoffSymbolIndex(const char *func_name)
int FindCoffSymbolIndex(TCCState *s1, const char *func_name)
{
int i, n = 0;
Elf32_Sym *p;

2676
tccdbg.c Normal file

File diff suppressed because it is too large Load Diff

3290
tccelf.c

File diff suppressed because it is too large Load Diff

7119
tccgen.c

File diff suppressed because it is too large Load Diff

View File

@ -19,6 +19,7 @@ int atoi(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
unsigned long int strtoul(const char *nptr, char **endptr, int base);
void exit(int);
void *alloca(size_t);
/* stdio.h */
typedef struct __FILE FILE;
@ -39,6 +40,7 @@ int getchar(void);
char *gets(char *s);
int ungetc(int c, FILE *stream);
int fflush(FILE *stream);
int puts(const char *s);
int putchar (int c);
int printf(const char *format, ...);

2477
tccmacho.c Normal file

File diff suppressed because it is too large Load Diff

1289
tccpe.c

File diff suppressed because it is too large Load Diff

2456
tccpp.c

File diff suppressed because it is too large Load Diff

1766
tccrun.c

File diff suppressed because it is too large Load Diff

163
tcctok.h
View File

@ -1,29 +1,31 @@
/*********************************************************************/
/* keywords */
DEF(TOK_INT, "int")
DEF(TOK_VOID, "void")
DEF(TOK_CHAR, "char")
DEF(TOK_IF, "if")
DEF(TOK_ELSE, "else")
DEF(TOK_WHILE, "while")
DEF(TOK_FOR, "for")
DEF(TOK_DO, "do")
DEF(TOK_CONTINUE, "continue")
DEF(TOK_BREAK, "break")
DEF(TOK_RETURN, "return")
DEF(TOK_FOR, "for")
DEF(TOK_GOTO, "goto")
DEF(TOK_SWITCH, "switch")
DEF(TOK_CASE, "case")
DEF(TOK_DEFAULT, "default")
DEF(TOK_ASM1, "asm")
DEF(TOK_ASM2, "__asm")
DEF(TOK_ASM3, "__asm__")
DEF(TOK_EXTERN, "extern")
DEF(TOK_STATIC, "static")
DEF(TOK_UNSIGNED, "unsigned")
DEF(TOK_GOTO, "goto")
DEF(TOK_DO, "do")
DEF(TOK_CONTINUE, "continue")
DEF(TOK_SWITCH, "switch")
DEF(TOK_CASE, "case")
DEF(TOK__Atomic, "_Atomic")
DEF(TOK_CONST1, "const")
DEF(TOK_CONST2, "__const") /* gcc keyword */
DEF(TOK_CONST3, "__const__") /* gcc keyword */
DEF(TOK_VOLATILE1, "volatile")
DEF(TOK_VOLATILE2, "__volatile") /* gcc keyword */
DEF(TOK_VOLATILE3, "__volatile__") /* gcc keyword */
DEF(TOK_LONG, "long")
DEF(TOK_REGISTER, "register")
DEF(TOK_SIGNED1, "signed")
DEF(TOK_SIGNED2, "__signed") /* gcc keyword */
@ -36,34 +38,36 @@
DEF(TOK_RESTRICT2, "__restrict")
DEF(TOK_RESTRICT3, "__restrict__")
DEF(TOK_EXTENSION, "__extension__") /* gcc keyword */
DEF(TOK_THREAD_LOCAL, "_Thread_local") /* C11 thread-local storage */
DEF(TOK___thread, "__thread") /* GCC thread-local storage extension */
DEF(TOK_GENERIC, "_Generic")
DEF(TOK_STATIC_ASSERT, "_Static_assert")
DEF(TOK_VOID, "void")
DEF(TOK_CHAR, "char")
DEF(TOK_INT, "int")
DEF(TOK_FLOAT, "float")
DEF(TOK_DOUBLE, "double")
DEF(TOK_BOOL, "_Bool")
DEF(TOK_COMPLEX, "_Complex")
DEF(TOK_SHORT, "short")
DEF(TOK_LONG, "long")
DEF(TOK_STRUCT, "struct")
DEF(TOK_UNION, "union")
DEF(TOK_TYPEDEF, "typedef")
DEF(TOK_DEFAULT, "default")
DEF(TOK_ENUM, "enum")
DEF(TOK_SIZEOF, "sizeof")
DEF(TOK_ATTRIBUTE1, "__attribute")
DEF(TOK_ATTRIBUTE2, "__attribute__")
DEF(TOK_ALIGNOF1, "__alignof")
DEF(TOK_ALIGNOF2, "__alignof__")
DEF(TOK_ALIGNOF3, "_Alignof")
DEF(TOK_ALIGNAS, "_Alignas")
DEF(TOK_TYPEOF1, "typeof")
DEF(TOK_TYPEOF2, "__typeof")
DEF(TOK_TYPEOF3, "__typeof__")
DEF(TOK_LABEL, "__label__")
DEF(TOK_ASM1, "asm")
DEF(TOK_ASM2, "__asm")
DEF(TOK_ASM3, "__asm__")
#ifdef TCC_TARGET_ARM64
DEF(TOK_UINT128, "__uint128_t")
#endif
/*********************************************************************/
/* the following are not keywords. They are included to ease parsing */
@ -88,6 +92,8 @@
DEF(TOK___FUNCTION__, "__FUNCTION__")
DEF(TOK___VA_ARGS__, "__VA_ARGS__")
DEF(TOK___COUNTER__, "__COUNTER__")
DEF(TOK___HAS_INCLUDE, "__has_include")
DEF(TOK___HAS_INCLUDE_NEXT, "__has_include_next")
/* special identifiers */
DEF(TOK___FUNC__, "__func__")
@ -109,8 +115,14 @@
DEF(TOK_WEAK2, "__weak__")
DEF(TOK_ALIAS1, "alias")
DEF(TOK_ALIAS2, "__alias__")
DEF(TOK_USED1, "used")
DEF(TOK_USED2, "__used__")
DEF(TOK_UNUSED1, "unused")
DEF(TOK_UNUSED2, "__unused__")
DEF(TOK_FORMAT1, "format")
DEF(TOK_FORMAT2, "__format__")
DEF(TOK_NODEBUG1, "nodebug")
DEF(TOK_NODEBUG2, "__nodebug__")
DEF(TOK_CDECL1, "cdecl")
DEF(TOK_CDECL2, "__cdecl")
DEF(TOK_CDECL3, "__cdecl__")
@ -120,8 +132,22 @@
DEF(TOK_FASTCALL1, "fastcall")
DEF(TOK_FASTCALL2, "__fastcall")
DEF(TOK_FASTCALL3, "__fastcall__")
DEF(TOK_THISCALL1, "thiscall")
DEF(TOK_THISCALL2, "__thiscall")
DEF(TOK_THISCALL3, "__thiscall__")
DEF(TOK_REGPARM1, "regparm")
DEF(TOK_REGPARM2, "__regparm__")
DEF(TOK_CLEANUP1, "cleanup")
DEF(TOK_CLEANUP2, "__cleanup__")
DEF(TOK_CONSTRUCTOR1, "constructor")
DEF(TOK_CONSTRUCTOR2, "__constructor__")
DEF(TOK_DESTRUCTOR1, "destructor")
DEF(TOK_DESTRUCTOR2, "__destructor__")
DEF(TOK_ALWAYS_INLINE1, "always_inline")
DEF(TOK_ALWAYS_INLINE2, "__always_inline__")
DEF(TOK_NOINLINE, "__noinline__")
DEF(TOK_PURE1, "pure")
DEF(TOK_PURE2, "__pure__")
DEF(TOK_MODE, "__mode__")
DEF(TOK_MODE_QI, "__QI__")
@ -132,8 +158,10 @@
DEF(TOK_DLLEXPORT, "dllexport")
DEF(TOK_DLLIMPORT, "dllimport")
DEF(TOK_NODECORATE, "nodecorate")
DEF(TOK_NORETURN1, "noreturn")
DEF(TOK_NORETURN2, "__noreturn__")
DEF(TOK_NORETURN3, "_Noreturn")
DEF(TOK_VISIBILITY1, "visibility")
DEF(TOK_VISIBILITY2, "__visibility__")
@ -143,19 +171,43 @@
DEF(TOK_builtin_frame_address, "__builtin_frame_address")
DEF(TOK_builtin_return_address, "__builtin_return_address")
DEF(TOK_builtin_expect, "__builtin_expect")
DEF(TOK_builtin_unreachable, "__builtin_unreachable")
/*DEF(TOK_builtin_va_list, "__builtin_va_list")*/
#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
DEF(TOK_builtin_va_start, "__builtin_va_start")
#elif defined TCC_TARGET_X86_64
DEF(TOK_builtin_va_arg_types, "__builtin_va_arg_types")
#elif defined TCC_TARGET_ARM64
DEF(TOK___va_start, "__va_start")
DEF(TOK___va_arg, "__va_arg")
DEF(TOK_builtin_va_start, "__builtin_va_start")
DEF(TOK_builtin_va_arg, "__builtin_va_arg")
#elif defined TCC_TARGET_RISCV64
DEF(TOK_builtin_va_start, "__builtin_va_start")
#endif
/* atomic operations */
#define DEF_ATOMIC(ID) DEF(TOK_##__##ID, "__"#ID)
DEF_ATOMIC(atomic_store)
DEF_ATOMIC(atomic_load)
DEF_ATOMIC(atomic_exchange)
DEF_ATOMIC(atomic_compare_exchange)
DEF_ATOMIC(atomic_fetch_add)
DEF_ATOMIC(atomic_fetch_sub)
DEF_ATOMIC(atomic_fetch_or)
DEF_ATOMIC(atomic_fetch_xor)
DEF_ATOMIC(atomic_fetch_and)
DEF_ATOMIC(atomic_fetch_nand)
DEF_ATOMIC(atomic_add_fetch)
DEF_ATOMIC(atomic_sub_fetch)
DEF_ATOMIC(atomic_or_fetch)
DEF_ATOMIC(atomic_xor_fetch)
DEF_ATOMIC(atomic_and_fetch)
DEF_ATOMIC(atomic_nand_fetch)
/* pragma */
DEF(TOK_pack, "pack")
#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_X86_64)
#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_X86_64) && \
!defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_ARM64) && \
!defined(TCC_TARGET_RISCV64)
/* already defined for assembler */
DEF(TOK_ASM_push, "push")
DEF(TOK_ASM_pop, "pop")
@ -192,9 +244,9 @@
#if defined TCC_TARGET_ARM
# ifdef TCC_ARM_EABI
DEF(TOK_memcpy, "__aeabi_memcpy")
DEF(TOK_memcpy4, "__aeabi_memcpy4")
DEF(TOK_memcpy8, "__aeabi_memcpy8")
DEF(TOK_memmove, "__aeabi_memmove")
DEF(TOK_memmove4, "__aeabi_memmove4")
DEF(TOK_memmove8, "__aeabi_memmove8")
DEF(TOK_memset, "__aeabi_memset")
DEF(TOK___aeabi_ldivmod, "__aeabi_ldivmod")
DEF(TOK___aeabi_uldivmod, "__aeabi_uldivmod")
@ -246,16 +298,22 @@
DEF(TOK___fixdfdi, "__fixdfdi")
DEF(TOK___fixxfdi, "__fixxfdi")
#endif
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
DEF(TOK_alloca, "alloca")
#if defined TCC_TARGET_X86_64
DEF(TOK___fixxfdi, "__fixxfdi")
#endif
DEF(TOK_alloca, "alloca")
#if defined TCC_TARGET_PE
DEF(TOK___chkstk, "__chkstk")
#endif
#ifdef TCC_TARGET_ARM64
DEF(TOK___arm64_clear_cache, "__arm64_clear_cache")
#endif
#ifdef TCC_TARGET_RISCV64
DEF(TOK___riscv64_clear_cache, "__riscv64_clear_cache")
#endif
#if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
DEF(TOK___addtf3, "__addtf3")
DEF(TOK___subtf3, "__subtf3")
DEF(TOK___multf3, "__multf3")
@ -264,6 +322,7 @@
DEF(TOK___extenddftf2, "__extenddftf2")
DEF(TOK___trunctfsf2, "__trunctfsf2")
DEF(TOK___trunctfdf2, "__trunctfdf2")
DEF(TOK___negtf2, "__negtf2")
DEF(TOK___fixtfsi, "__fixtfsi")
DEF(TOK___fixtfdi, "__fixtfdi")
DEF(TOK___fixunstfsi, "__fixunstfsi")
@ -292,18 +351,33 @@
DEF(TOK___bound_main_arg, "__bound_main_arg")
DEF(TOK___bound_local_new, "__bound_local_new")
DEF(TOK___bound_local_delete, "__bound_local_delete")
DEF(TOK___bound_setjmp, "__bound_setjmp")
DEF(TOK___bound_longjmp, "__bound_longjmp")
DEF(TOK___bound_new_region, "__bound_new_region")
# ifdef TCC_TARGET_PE
DEF(TOK_malloc, "malloc")
DEF(TOK_free, "free")
DEF(TOK_realloc, "realloc")
DEF(TOK_memalign, "memalign")
DEF(TOK_calloc, "calloc")
# ifdef TCC_TARGET_X86_64
DEF(TOK___bound_alloca_nr, "__bound_alloca_nr")
# endif
DEF(TOK_strlen, "strlen")
DEF(TOK_strcpy, "strcpy")
# else
DEF(TOK_sigsetjmp, "sigsetjmp")
DEF(TOK___sigsetjmp, "__sigsetjmp")
DEF(TOK_siglongjmp, "siglongjmp")
# endif
DEF(TOK_setjmp, "setjmp")
DEF(TOK__setjmp, "_setjmp")
DEF(TOK_longjmp, "longjmp")
#endif
/*********************************************************************/
/* Tiny Assembler */
#define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
#define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
#define TOK_ASM_int TOK_INT
#define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
#define TOK_ASMDIR_LAST TOK_ASMDIR_section
DEF_ASMDIR(byte) /* must be first directive */
DEF_ASMDIR(word)
DEF_ASMDIR(align)
@ -334,17 +408,34 @@
DEF_ASMDIR(endr)
DEF_ASMDIR(org)
DEF_ASMDIR(quad)
#if defined(TCC_TARGET_I386)
#if PTR_SIZE == 4
DEF_ASMDIR(code16)
DEF_ASMDIR(code32)
#elif defined(TCC_TARGET_X86_64)
#else
DEF_ASMDIR(code64)
#endif
#if defined(TCC_TARGET_RISCV64)
DEF_ASMDIR(option)
#endif
DEF_ASMDIR(short)
DEF_ASMDIR(long)
DEF_ASMDIR(int)
DEF_ASMDIR(symver)
DEF_ASMDIR(reloc)
DEF_ASMDIR(section) /* must be last directive */
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
#include "i386-tok.h"
#endif
#if defined TCC_TARGET_ARM
#include "arm-tok.h"
#endif
#if defined TCC_TARGET_ARM64
#include "arm64-tok.h"
#endif
#if defined TCC_TARGET_RISCV64
#include "riscv64-tok.h"
#endif

View File

@ -48,52 +48,34 @@ static unsigned long le2belong(unsigned long ul) {
((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
}
/* Returns 1 if s contains any of the chars of list, else 0 */
static int contains_any(const char *s, const char *list) {
const char *l;
for (; *s; s++) {
for (l = list; *l; l++) {
if (*s == *l)
return 1;
}
}
return 0;
}
static int ar_usage(int ret) {
fprintf(stderr, "usage: tcc -ar [rcsv] lib file...\n");
fprintf(stderr, "create library ([abdioptxN] not supported).\n");
fprintf(stderr, "usage: tcc -ar [crstvx] lib [files]\n");
fprintf(stderr, "create library ([abdiopN] not supported).\n");
return ret;
}
ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
ST_FUNC int tcc_tool_ar(int argc, char **argv)
{
static ArHdr arhdr = {
static const ArHdr arhdr_init = {
"/ ",
" ",
"0 ",
"0 ",
"0 ",
" ",
"0 ",
"0 ",
ARFMAG
};
static ArHdr arhdro = {
" ",
" ",
"0 ",
"0 ",
"0 ",
" ",
ARFMAG
};
ArHdr arhdr = arhdr_init;
ArHdr arhdro = arhdr_init;
FILE *fi, *fh = NULL, *fo = NULL;
const char *created_file = NULL; // must delete on error
ElfW(Ehdr) *ehdr;
ElfW(Shdr) *shdr;
ElfW(Sym) *sym;
int i, fsize, i_lib, i_obj;
char *buf, *shstr, *symtab = NULL, *strtab = NULL;
char *buf, *shstr, *symtab, *strtab;
int symtabsize = 0;//, strtabsize = 0;
char *anames = NULL;
int *afpos = NULL;
@ -101,18 +83,24 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
char tfile[260], stmp[20];
char *file, *name;
int ret = 2;
const char *ops_conflict = "habdioptxN"; // unsupported but destructive if ignored.
const char *ops_conflict = "habdiopN"; // unsupported but destructive if ignored.
int extract = 0;
int table = 0;
int verbose = 0;
i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
for (i = 1; i < argc; i++) {
const char *a = argv[i];
if (*a == '-' && strstr(a, "."))
if (*a == '-' && strchr(a, '.'))
ret = 1; // -x.y is always invalid (same as gnu ar)
if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
if (contains_any(a, ops_conflict))
if ((*a == '-') || (i == 1 && !strchr(a, '.'))) { // options argument
if (strpbrk(a, ops_conflict))
ret = 1;
if (strstr(a, "v"))
if (strchr(a, 'x'))
extract = 1;
if (strchr(a, 't'))
table = 1;
if (strchr(a, 'v'))
verbose = 1;
} else { // lib or obj files: don't abort - keep validating all args.
if (!i_lib) // first file is the lib
@ -122,17 +110,75 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
}
}
if (!i_obj) // i_obj implies also i_lib. we require both.
if (!i_lib) // i_obj implies also i_lib.
ret = 1;
i_obj = i_obj ? i_obj : argc; // An empty archive will be generated if no input file is given
if (ret == 1)
return ar_usage(ret);
if ((fh = fopen(argv[i_lib], "wb")) == NULL)
if (extract || table) {
if ((fh = fopen(argv[i_lib], "rb")) == NULL)
{
fprintf(stderr, "tcc: ar: can't open file %s\n", argv[i_lib]);
goto finish;
}
fread(stmp, 1, 8, fh);
if (memcmp(stmp,ARMAG,8))
{
no_ar:
fprintf(stderr, "tcc: ar: not an ar archive %s\n", argv[i_lib]);
goto finish;
}
while (fread(&arhdr, 1, sizeof(arhdr), fh) == sizeof(arhdr)) {
char *p, *e;
if (memcmp(arhdr.ar_fmag, ARFMAG, 2))
goto no_ar;
p = arhdr.ar_name;
for (e = p + sizeof arhdr.ar_name; e > p && e[-1] == ' ';)
e--;
*e = '\0';
arhdr.ar_size[sizeof arhdr.ar_size-1] = 0;
fsize = atoi(arhdr.ar_size);
buf = tcc_malloc(fsize + 1);
fread(buf, fsize, 1, fh);
if (strcmp(arhdr.ar_name,"/") && strcmp(arhdr.ar_name,"/SYM64/")) {
if (e > p && e[-1] == '/')
e[-1] = '\0';
/* tv not implemented */
if (table || verbose)
printf("%s%s\n", extract ? "x - " : "", arhdr.ar_name);
if (extract) {
if ((fo = fopen(arhdr.ar_name, "wb")) == NULL)
{
fprintf(stderr, "tcc: ar: can't create file %s\n",
arhdr.ar_name);
tcc_free(buf);
goto finish;
}
fwrite(buf, fsize, 1, fo);
fclose(fo);
/* ignore date/uid/gid/mode */
}
}
if (fsize & 1)
fgetc(fh);
tcc_free(buf);
}
ret = 0;
finish:
if (fh)
fclose(fh);
return ret;
}
if ((fh = fopen(argv[i_lib], "wb")) == NULL)
{
fprintf(stderr, "tcc: ar: can't create file %s\n", argv[i_lib]);
goto the_end;
}
created_file = argv[i_lib];
sprintf(tfile, "%s.tmp", argv[i_lib]);
if ((fo = fopen(tfile, "wb+")) == NULL)
@ -143,7 +189,7 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
funcmax = 250;
afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func
memcpy(&arhdro.ar_mode, "100666", 6);
memcpy(&arhdro.ar_mode, "100644", 6);
// i_obj = first input object file
while (i_obj < argc)
@ -176,6 +222,7 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
shstr = (char *)(buf + shdr->sh_offset);
symtab = strtab = NULL;
for (i = 0; i < ehdr->e_shnum; i++)
{
shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
@ -196,7 +243,7 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
}
}
if (symtab && symtabsize)
if (symtab && strtab)
{
int nsym = symtabsize / sizeof(ElfW(Sym));
//printf("symtab: info size shndx name\n");
@ -207,6 +254,9 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
(sym->st_info == 0x10
|| sym->st_info == 0x11
|| sym->st_info == 0x12
|| sym->st_info == 0x20
|| sym->st_info == 0x21
|| sym->st_info == 0x22
)) {
//printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
istrlen = strlen(strtab + sym->st_name)+1;
@ -239,14 +289,21 @@ ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv)
tcc_free(buf);
i_obj++;
fpos += (fsize + sizeof(arhdro));
if (fpos & 1)
fputc(0, fo), ++fpos;
}
hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
fpos = 0;
if ((hofs & 1)) // align
hofs++, fpos = 1;
// write header
fwrite("!<arch>\n", 8, 1, fh);
sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
fwrite(ARMAG, 8, 1, fh);
// create an empty archive
if (!funccnt) {
ret = 0;
goto the_end;
}
sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)) + fpos);
memcpy(&arhdr.ar_size, stmp, 10);
fwrite(&arhdr, sizeof(arhdr), 1, fh);
afpos[0] = le2belong(funccnt);
@ -272,6 +329,8 @@ the_end:
tcc_free(afpos);
if (fh)
fclose(fh);
if (created_file && ret != 0)
remove(created_file);
if (fo)
fclose(fo), remove(tfile);
return ret;
@ -301,7 +360,7 @@ the_end:
#ifdef TCC_TARGET_PE
ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv)
ST_FUNC int tcc_tool_impdef(int argc, char **argv)
{
int ret, v, i;
char infile[260];
@ -392,10 +451,8 @@ usage:
ret = 0;
the_end:
/* cannot free memory received from tcc_get_dllexports
if it came from a dll */
/* if (p)
tcc_free(p); */
if (p)
tcc_free(p);
if (fp)
fclose(fp);
if (op)
@ -430,37 +487,48 @@ the_end:
#if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64
ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option)
ST_FUNC int tcc_tool_cross(char **argv, int option)
{
tcc_error("-m%d not implemented.", option);
fprintf(stderr, "tcc -m%d not implemented\n", option);
return 1;
}
#else
#ifdef _WIN32
#include <process.h>
static char *str_replace(const char *str, const char *p, const char *r)
/* - Empty argument or with space/tab (not newline) requires quoting.
* - Double-quotes at the value require '\'-escape, regardless of quoting.
* - Consecutive (or 1) backslashes at the value all need '\'-escape only if
* followed by [escaped] double quote, else taken literally, e.g. <x\\y\>
* remains literal without quoting or esc, but <x\\"y\> becomes <x\\\\\"y\>.
* - This "before double quote" rule applies also before delimiting quoting,
* e.g. <x\y \"z\> becomes <"x\y \\\"z\\"> (quoting required because space).
*
* https://learn.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments
*/
static char *quote_win32(const char *s)
{
const char *s, *s0;
char *d, *d0;
int sl, pl, rl;
char *o, *r = tcc_malloc(2 * strlen(s) + 3); /* max-esc, quotes, \0 */
int cbs = 0, quoted = !*s; /* consecutive backslashes before current */
sl = strlen(str);
pl = strlen(p);
rl = strlen(r);
for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
if (d) {
memcpy(d, s0, sl = s - s0), d += sl;
memcpy(d, r, rl), d += rl;
} else
sl += rl - pl;
}
if (d) {
strcpy(d, s0);
return d0;
for (o = r; *s; *o++ = *s++) {
quoted |= *s == ' ' || *s == '\t';
if (*s == '\\' || *s == '"')
*o++ = '\\';
else
o -= cbs; /* undo cbs escapes, if any (not followed by DQ) */
cbs = *s == '\\' ? cbs + 1 : 0;
}
if (quoted) {
memmove(r + 1, r, o++ - r);
*r = *o++ = '"';
} else {
o -= cbs;
}
*o = 0;
return r; /* don't bother with realloc(r, o-r+1) */
}
static int execvp_win32(const char *prog, char **argv)
@ -468,8 +536,7 @@ static int execvp_win32(const char *prog, char **argv)
int ret; char **p;
/* replace all " by \" */
for (p = argv; *p; ++p)
if (strchr(*p, '"'))
*p = str_replace(*p, "\"", "\\\"");
*p = quote_win32(*p);
ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
if (-1 == ret)
return ret;
@ -479,7 +546,7 @@ static int execvp_win32(const char *prog, char **argv)
#define execvp execvp_win32
#endif /* _WIN32 */
ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int target)
ST_FUNC int tcc_tool_cross(char **argv, int target)
{
char program[4096];
char *a0 = argv[0];
@ -498,7 +565,8 @@ ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int target)
if (strcmp(a0, program))
execvp(argv[0] = program, argv);
tcc_error("could not run '%s'", program);
fprintf(stderr, "tcc: could not run '%s'\n", program);
return 1;
}
#endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */
@ -506,20 +574,34 @@ ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int target)
/* enable commandline wildcard expansion (tcc -o x.exe *.c) */
#ifdef _WIN32
int _CRT_glob = 1;
const int _CRT_glob = 1;
#ifndef _CRT_glob
int _dowildcard = 1;
const int _dowildcard = 1;
#endif
#endif
/* -------------------------------------------------------------- */
/* generate xxx.d file */
ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename)
static char *escape_target_dep(const char *s) {
char *res = tcc_malloc(strlen(s) * 2 + 1);
int j;
for (j = 0; *s; s++, j++) {
if (is_space(*s)) {
res[j++] = '\\';
}
res[j] = *s;
}
res[j] = '\0';
return res;
}
ST_FUNC int gen_makedeps(TCCState *s1, const char *target, const char *filename)
{
FILE *depout;
char buf[1024];
int i;
char **escaped_targets;
int i, k, num_targets;
if (!filename) {
/* compute filename automatically: dir/file.o -> dir/file.d */
@ -528,19 +610,42 @@ ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename)
filename = buf;
}
if (s->verbose)
printf("<- %s\n", filename);
if(!strcmp(filename, "-"))
depout = fdopen(1, "w");
else
/* XXX return err codes instead of error() ? */
depout = fopen(filename, "w");
if (!depout)
tcc_error("could not open '%s'", filename);
return tcc_error_noabort("could not open '%s'", filename);
if (s1->verbose)
printf("<- %s\n", filename);
fprintf(depout, "%s: \\\n", target);
for (i=0; i<s->nb_target_deps; ++i)
fprintf(depout, " %s \\\n", s->target_deps[i]);
escaped_targets = tcc_malloc(s1->nb_target_deps * sizeof(*escaped_targets));
num_targets = 0;
for (i = 0; i<s1->nb_target_deps; ++i) {
for (k = 0; k < i; ++k)
if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
goto next;
escaped_targets[num_targets++] = escape_target_dep(s1->target_deps[i]);
next:;
}
fprintf(depout, "%s:", target);
for (i = 0; i < num_targets; ++i)
fprintf(depout, " \\\n %s", escaped_targets[i]);
fprintf(depout, "\n");
if (s1->gen_phony_deps) {
/* Skip first file, which is the c file.
* Only works for single file give on command-line,
* but other compilers have the same limitation */
for (i = 1; i < num_targets; ++i)
fprintf(depout, "%s:\n", escaped_targets[i]);
}
for (i = 0; i < num_targets; ++i)
tcc_free(escaped_targets[i]);
tcc_free(escaped_targets);
fclose(depout);
return 0;
}
/* -------------------------------------------------------------- */

View File

@ -4,62 +4,87 @@
TOP = ..
include $(TOP)/Makefile
VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP)
CFLAGS := $(filter-out -W% -g% -O%,$(CFLAGS)) -I$(TOPSRC) $(LDFLAGS)
VPATH = $(TOPSRC)/tests $(TOPSRC)
CFLAGS := $(filter-out -g% -O%,$(CFLAGS)) -I$(TOPSRC) -I$(TOP) $(LDFLAGS)
# what tests to run
TESTS = \
hello-exe \
hello-run \
libtest \
libtest_mt \
test3 \
memtest \
dlltest \
abitest \
asm-c-connect-test \
vla_test-run \
cross-test \
tests2-dir \
pp-dir
pp-dir \
memtest \
dlltest \
cross-test
BTESTS = test1b test3b btest
# test4 -- problem with -static
# test4_static -- Not all relocation types are implemented yet.
# asmtest / asmtest2 -- minor differences with gcc
# btest -- works on i386 (including win32)
# bounds-checking is supported only on i386
ifneq ($(ARCH),i386)
TESTS := $(filter-out $(BTESTS),$(TESTS))
ifeq ($(CONFIG_backtrace),no)
TESTS := $(filter-out libtest_mt, $(TESTS))
else ifneq ($(CONFIG_bcheck),no)
TESTS += btest test1b tccb
endif
ifdef CONFIG_WIN32
TESTS := $(filter-out $(BTESTS),$(TESTS))
ifeq ($(CONFIG_dll),no)
TESTS := $(filter-out dlltest, $(TESTS))
endif
ifdef CONFIG_OSX # -run only
TESTS := hello-run libtest tests2-dir pp-dir
endif
ifeq (,$(filter arm64 i386 x86_64,$(ARCH)))
TESTS := $(filter-out vla_test-run,$(TESTS))
endif
ifeq ($(CONFIG_arm_eabi),yes)
TESTS := $(filter-out test3,$(TESTS))
ifeq (-$(CONFIG_arm_eabi)-$(CONFIG_arm_vfp)-,-yes--)
TESTS := $(filter-out test3 test1b,$(TESTS))
endif
ifeq (,$(filter i386 x86_64,$(ARCH)))
TESTS := $(filter-out dlltest asm-c-connect-test,$(TESTS))
TESTS := $(filter-out asm-c-connect-test,$(TESTS))
endif
ifndef CONFIG_cross
TESTS := $(filter-out cross-%,$(TESTS))
endif
ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll
PATH := $(CURDIR)/$(TOP)$(if $(findstring :\,$(PATH)),;,:)$(PATH)
PATH := $(CURDIR)/$(TOP)$(if $(findstring ;,$(PATH)),;,:)$(PATH)
endif
ifdef CONFIG_OSX
LIBS += $(LINK_LIBTCC)
endif
ifeq ($(ARCH),arm)
# tcctest refers to the alignment of functions, and with thumb mode
# the low bit of code addresses selects the mode, so the "alignment"
# of functions via bit masking comes out as 1. Just disable thumb.
test.ref: CFLAGS+=-marm
endif
ifeq ($(ARCH)$(CONFIG_WIN32),i386)
# tcctest.c:get_asm_string uses a construct that is checked too strictly
# by GCC in 32bit mode when PIC is enabled.
test.ref: CFLAGS+=-fno-PIC -fno-PIE -Wl,-z,notext
endif
ifeq ($(CC_NAME),msvc)
test.ref abitest : CC = gcc
endif
ifeq ($(TARGETOS),OpenBSD)
dlltest: CFLAGS+=-fno-stack-protector
endif
ifneq (,$(filter FreeBSD NetBSD,$(TARGETOS)))
# test3 has dlsym problems
TESTS := $(filter-out test3,$(TESTS))
TESTS += test1
endif
RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS)
RUN_TCC = -run $(TOPSRC)/tcc.c $(TCCFLAGS)
DISAS = objdump -d
DUMPTCC = (set -x; $(TOP)/tcc -vv; ldd $(TOP)/tcc; exit 1)
ifdef CONFIG_OSX
DUMPTCC = (set -x; $(TCC_LOCAL) -vv; otool -L $(TCC_LOCAL); exit 1)
else
DUMPTCC = (set -x; $(TCC_LOCAL) -vv; ldd $(TCC_LOCAL); exit 1)
endif
all test : clean-s $(TESTS)
all test :
@echo ------------ version ------------
@$(TCC_LOCAL) -v
@$(MAKE) --no-print-directory -s clean
@$(MAKE) --no-print-directory -s -r _all
@echo ------- ALL TESTS PASSED --------
_all : $(TESTS)
hello-exe: ../examples/ex1.c
@echo ------------ $@ ------------
@ -69,12 +94,15 @@ hello-run: ../examples/ex1.c
@echo ------------ $@ ------------
$(TCC) -run $< || $(DUMPTCC)
libtest: libtcc_test$(EXESUF)
libtes%: libtcc_tes%$(EXESUF)
@echo ------------ $@ ------------
./libtcc_test$(EXESUF) $(TCCFLAGS)
./libtcc_tes$*$(EXESUF) $(TOPSRC)/tcc.c $(TCCFLAGS)
libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
libtcc_test$(EXESUF): libtcc_test.c
$(CC) -o $@ $< $(CFLAGS) $(-LTCC) $(LIBS)
libtcc_test_mt$(EXESUF): libtcc_test_mt.c
$(CC) -o $@ $< $(CFLAGS) $(-LTCC) $(LIBS)
%-dir:
@echo ------------ $@ ------------
@ -82,28 +110,30 @@ libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC)
# test.ref - generate using cc
test.ref: tcctest.c
$(CC) -o tcctest.gcc $< $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
./tcctest.gcc > $@
$(CC) -o tcctest.gcc$(EXESUF) $< $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
./tcctest.gcc$(EXESUF) > $@
# auto test
test1 test1b: tcctest.c test.ref
@echo ------------ $@ ------------
$(TCC) -run $< > test.out1
@diff -u test.ref test.out1 && echo "Auto Test OK"
$(TCC) $(RUN_TCC) -w -run $< > test.out1
@diff -u test.ref test.out1 && echo "$(AUTO_TEST) OK"
# iterated test2 (compile tcc then compile tcctest.c !)
test2 test2b: tcctest.c test.ref
@echo ------------ $@ ------------
$(TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out2
@diff -u test.ref test.out2 && echo "Auto Test2 OK"
$(TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out2
@diff -u test.ref test.out2 && echo "$(AUTO_TEST)2 OK"
# iterated test3 (compile tcc then compile tcc then compile tcctest.c !)
test3 test3b: tcctest.c test.ref
@echo ------------ $@ ------------
$(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out3
@diff -u test.ref test.out3 && echo "Auto Test3 OK"
$(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -w -run $< > test.out3
@diff -u test.ref test.out3 && echo "$(AUTO_TEST)3 OK"
test%b : TCCFLAGS += -b
AUTO_TEST = Auto Test
test%b : TCCFLAGS += -b -bt1
test%b : AUTO_TEST = Auto Bound-Test
# binary output test
test4: tcctest.c test.ref
@ -112,65 +142,74 @@ test4: tcctest.c test.ref
$(TCC) -c -o tcctest3.o $<
$(TCC) -o tcctest3 tcctest3.o
./tcctest3 > test3.out
@if diff -u test.ref test3.out ; then echo "Object Auto Test OK"; fi
@if diff -u test.ref test3.out ; then echo "Object $(AUTO_TEST) OK"; fi
# dynamic output
$(TCC) -o tcctest1 $<
./tcctest1 > test1.out
@if diff -u test.ref test1.out ; then echo "Dynamic Auto Test OK"; fi
@if diff -u test.ref test1.out ; then echo "Dynamic $(AUTO_TEST) OK"; fi
# dynamic output + bound check
$(TCC) -b -o tcctest4 $<
./tcctest4 > test4.out
@if diff -u test.ref test4.out ; then echo "BCheck Auto Test OK"; fi
# static output
@if diff -u test.ref test4.out ; then echo "BCheck $(AUTO_TEST) OK"; fi
test4_static: tcctest.c test.ref
@echo ------------ $@ ------------
# static output.
$(TCC) -static -o tcctest2 $<
./tcctest2 > test2.out
@if diff -u test.ref test2.out ; then echo "Static Auto Test OK"; fi
@if diff -u test.ref test2.out ; then echo "Static $(AUTO_TEST) OK"; fi
# use tcc to create libtcc.so/.dll and the tcc(.exe) frontend and run them
dlltest:
@echo ------------ $@ ------------
$(TCC) $(NATIVE_DEFINES) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF)
$(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
$(TCC) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF)
$(TCC) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
ifndef CONFIG_WIN32
ifeq (,$(filter Darwin WIN32,$(TARGETOS)))
@echo ------------ $@ with PIC ------------
$(CC) $(CFLAGS) -fPIC $(NATIVE_DEFINES) -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
$(CC) $(CFLAGS) -fPIC -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
$(TCC) libtcc.o $(LIBS) -shared -o libtcc2$(DLLSUF)
$(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
$(TCC) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
endif
@rm tcc2$(EXESUF) libtcc2$(DLLSUF)
memtest:
@echo ------------ $@ ------------
$(CC) $(CFLAGS) $(NATIVE_DEFINES) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) $(TOPSRC)/tcc.c $(LIBS)
./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) $(TOPSRC)/tests/tcctest.c
$(CC) $(CFLAGS) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
./memtest-tcc$(EXESUF) $(TCCFLAGS) $(TOPSRC)/tcc.c $(LIBS)
./memtest-tcc$(EXESUF) $(TCCFLAGS) -run $(TOPSRC)/tcc.c $(TCCFLAGS) -w $(TOPSRC)/tests/tcctest.c
@echo OK
# memory and bound check auto test
BOUNDS_OK = 1 4 8 10 14
BOUNDS_FAIL= 2 5 7 9 11 12 13 15
BOUNDS_OK = 1 4 8 10 14 16
BOUNDS_FAIL= 2 5 6 7 9 11 12 13 15 17 18
btest: boundtest.c
@echo ------------ $@ ------------
@for i in $(BOUNDS_OK); do \
echo ; echo --- boundtest $$i ---; \
if $(TCC) -b -run $< $$i ; then \
echo succeeded as expected; \
if $(TCC) -b -run $< $$i >/dev/null 2>&1 ; then \
echo "Test $$i succeeded as expected" ; \
else\
echo Failed positive test $$i ; exit 1 ; \
echo "Failed positive test $$i" ; exit 1 ; \
fi ;\
done ;\
for i in $(BOUNDS_FAIL); do \
echo ; echo --- boundtest $$i ---; \
if $(TCC) -b -run $< $$i ; then \
echo Failed negative test $$i ; exit 1 ;\
if $(TCC) -b -bt1 -run $< $$i >/dev/null 2>&1 ; then \
echo "Failed negative test $$i" ; exit 1 ;\
else\
echo failed as expected; \
echo "Test $$i failed as expected" ; \
fi ;\
done ;\
echo; echo Bound test OK
echo Bound-Test OK
tccb:
@echo ------------ $@ ------------
$(TCC) -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(LIBS) -o tccb1.exe
mv tccb1.exe tccb2.exe
./tccb2.exe -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(LIBS) -o tccb1.exe
cmp -s tccb1.exe tccb2.exe && echo "Exe Bound-Test OK"
# speed test
speedtest: ex2 ex3
@ -181,8 +220,9 @@ speedtest: ex2 ex3
time $(TCC) -run $(TOPSRC)/examples/ex3.c 35
weaktest: tcctest.c test.ref
@echo ------------ $@ ------------
$(TCC) -c $< -o weaktest.tcc.o
$(CC) -c $< -o weaktest.gcc.o $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
$(CC) -c $< -o weaktest.gcc.o $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt
objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt
diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK"
@ -192,36 +232,38 @@ ex%: $(TOPSRC)/examples/ex%.c
# tiny assembler testing
asmtest.ref: asmtest.S
$(CC) -Wa,-W -o asmtest.ref.o -c asmtest.S
$(CC) -Wa,-W -Wa,-mx86-used-note=no -o asmtest.ref.o -c asmtest.S
objdump -D asmtest.ref.o > asmtest.ref
ifeq ($(ARCH),arm)
asmtest asmtest2:
TCC="${TCC}" ./arm-asm-testsuite.sh
else
asmtest asmtest2: asmtest.ref
@echo ------------ $@ ------------
$(TCC) $(MAYBE_RUN_TCC) -c asmtest.S
objdump -D asmtest.o > asmtest.out
@if diff -u --ignore-matching-lines="file format" asmtest.ref asmtest.out ; then echo "ASM Auto Test OK"; fi
endif
# test assembler with tcc compiled by itself
asmtest2: MAYBE_RUN_TCC = $(RUN_TCC)
# Check that code generated by libtcc is binary compatible with
# that generated by CC
abitest-cc$(EXESUF): abitest.c $(LIBTCC)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) -w
abitest-cc.exe: abitest.c
$(CC) -o $@ $^ $(CFLAGS) $(-LTCC) $(LIBS) -w
abitest-tcc$(EXESUF): abitest.c libtcc.c
$(TCC) -o $@ $^ $(NATIVE_DEFINES) $(LIBS)
abitest-tcc.exe: abitest.c libtcc.c
$(TCC) -o $@ $^ $(LIBS)
ABITESTS := abitest-cc$(EXESUF)
ifneq ($(CONFIG_arm_eabi),yes) # not ARM soft-float
ABITESTS += abitest-tcc$(EXESUF)
endif
abitest: $(ABITESTS)
abitest-% : abitest-%.exe
@echo ------------ $@ ------------
./abitest-cc$(EXESUF) $(TCCFLAGS)
ifneq ($(CONFIG_arm_eabi),yes) # not ARM soft-float
./abitest-tcc$(EXESUF) $(TCCFLAGS)
./$< $(TCCFLAGS)
abitest: abitest-cc
ifneq (-$(CONFIG_arm_eabi)-$(CONFIG_arm_vfp)-,-yes--)
abitest: abitest-tcc
endif
vla_test$(EXESUF): vla_test.c
@ -231,6 +273,8 @@ vla_test-run: vla_test$(EXESUF)
@echo ------------ $@ ------------
./vla_test$(EXESUF)
.PHONY: abitest vla_test tccb
asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c
$(TCC) -o $@ $^
@ -244,21 +288,42 @@ asm-c-connect-test: asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)
@echo ------------ $@ ------------
./asm-c-connect$(EXESUF) > asm-c-connect.out1 && cat asm-c-connect.out1
./asm-c-connect-sep$(EXESUF) > asm-c-connect.out2 && cat asm-c-connect.out2
@diff -u asm-c-connect.out1 asm-c-connect.out2 && echo "ok"
@diff -u asm-c-connect.out1 asm-c-connect.out2 || (echo "error"; exit 1)
cross-test :
# quick sanity check for cross-compilers
cross-test : tcctest.c examples/ex3.c
@echo ------------ $@ ------------
$(TOP)/i386-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/i386-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/x86_64-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/x86_64-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/arm-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/arm-wince-tcc$(EXESUF) $(TCCFLAGS-win) -c $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/arm64-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/c67-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok"
$(TOP)/i386-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/win32/examples/hello_win.c && echo "ok"
$(TOP)/x86_64-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/win32/examples/hello_win.c && echo "ok"
$(TOP)/arm-wince-tcc$(EXESUF) $(TCCFLAGS-win) -c $(TOPSRC)/win32/examples/hello_win.c && echo "ok"
$(foreach T,$(CROSS-TGTS),$(call CROSS-COMPILE,$T))
CROSS-TGTS = \
i386 \
i386-win32 \
i386-OpenBSD \
x86_64 \
x86_64-win32 \
x86_64-osx \
x86_64-FreeBSD \
x86_64-NetBSD \
x86_64-OpenBSD \
arm-fpa \
arm-eabihf \
arm-NetBSD \
arm-wince \
arm64 \
arm64-win32 \
arm64-osx \
arm64-FreeBSD \
arm64-NetBSD \
arm64-OpenBSD \
riscv64 \
c67
define CROSS-COMPILE
@echo " . $(1)"
$(TCC) $(DEF-$1) -DTCC_CROSS_TEST -run $(TOPSRC)/tcc.c \
-c $(if $(findstring c67,$1),$(filter %/ex3.c,$^),$<) -w $(TCCFLAGS)
endef
# targets for development
%.bin: %.c tcc
@ -279,11 +344,8 @@ cache: tcc_g
clean:
rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc
rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234]
rm -f asm-c-connect$(EXESUF)
rm -f ex? tcc_g weaktest.*.txt *.def
rm -f asm-c-connect asm-c-connect-sep
rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt
@$(MAKE) -C tests2 $@
@$(MAKE) -C pp $@
# silent clean, used before running tests
clean-s:
@$(MAKE) -s --no-print-directory clean

View File

@ -52,7 +52,7 @@ static int run_callback(const char *src, callback_type callback) {
return -1;
if (tcc_compile_string(s, src) == -1)
return -1;
if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1)
if (tcc_relocate(s) == -1)
return -1;
ptr = tcc_get_symbol(s, "f");

243
tests/arm-asm-testsuite.sh Executable file
View File

@ -0,0 +1,243 @@
#!/bin/sh
set -e
# Note: "{r3}" is definitely different--but would complicate the assembler.
state="`mktemp -d`"
cat ../arm-tok.h | \
grep DEF_ASM | \
grep -v 'not useful' | \
grep -v '#define' | \
grep -v '/[*]' | \
grep -v 'DEF_ASM_CONDED_WITH_SUFFIX(x' | \
sed -e 's;^[ ]*DEF_ASM_CONDED_VFP_F32_F64[^(]*(\(.*\)).*$; DEF_ASM_CONDED(\1.f32)\
DEF_ASM_CONDED(\1.f64);g' | \
sed -e 's;^[ ]*DEF_ASM[^(]*(\(.*\)).*$;\1;g' -e 's;, ;.;g' | \
egrep -v '^((r|c|p|s|d)[0-9]+|fp|ip|sp|lr|pc|asl|apsr_nzcv|fpsid|fpscr|fpexc)$' | while read s
do
as_opts=""
if [ "${s#v}" != "${s}" ]
then
if grep -q "CONFIG_arm_vfp=yes" ../config.mak
then
as_opts="${as_opts} -mfpu=vfp"
else
echo "note: skipping VFP instruction: $s (because VFP is disabled)">&2
continue
fi
fi
ok=0
for args in "r3, r4, r5, r6" \
"r3, r4, r5" \
"r3, r4, r5, asl #7" \
"r3, r4, r5, lsl #7" \
"r3, r4, r5, asr #7" \
"r3, r4, r5, lsr #7" \
"r3, r4, r5, ror #7" \
"r3, r4, r5, rrx" \
"r3, r4, r5, asl r6" \
"r3, r4, r5, lsl r6" \
"r3, r4, r5, asr r6" \
"r3, r4, r5, lsr r6" \
"r3, r4, r5, ror r6" \
"r3, r4, #5, asl #7" \
"r3, r4, #5, lsl #7" \
"r3, r4, #5, asr #7" \
"r3, r4, #5, lsr #7" \
"r3, r4, #5, ror #7" \
"r3, r4, #5, rrx" \
"r3, #5, r4" \
"r3, #4, #8" \
"r3, r4, asl #5" \
"r3, r4, lsl #5" \
"r3, r4, asr #5" \
"r3, r4, lsr #5" \
"r3, r4, ror #5" \
"r3, r4, ror #8" \
"r3, r4, asl r5" \
"r3, r4, lsl r5" \
"r3, r4, asr r5" \
"r3, r4, lsr r5" \
"r3, r4, ror r5" \
"r3, r4, ror #8" \
"r3, r4, ror #16" \
"r3, r4, ror #24" \
"r3, r4, rrx" \
"r3, #4, asl #5" \
"r3, #4, lsl #5" \
"r3, #4, asr #5" \
"r3, #4, lsr #5" \
"r3, #4, ror #5" \
"r3, r4, rrx" \
"r3, r4" \
"r3" \
"{r3,r4,r5}" \
"{r3,r5,r4}" \
"r2!, {r3,r4,r5}" \
"r2!, {r3,r5,r4}" \
"r2, {r3,r4,r5}" \
"r2, {r3,r5,r4}" \
"r2, [r3, r4]" \
"r2, [r3, r4]!" \
"r2, [r3, -r4]" \
"r2, [r3, -r4]!" \
"r2, [r3], r4" \
"r2, [r3], -r4" \
"r2, [r3]" \
"r2, r3, [r4, lsl# 2]" \
"r2, [r3, r4, lsr# 1]" \
"r2, [r3, r4, lsr# 2]!" \
"r2, [r3, -r4, ror# 3]" \
"r2, [r3, -r4, lsl# 1]!" \
"r2, [r3], r4, lsl# 3" \
"r2, [r3], -r4, asr# 31" \
"r2, [r3], -r4, asl# 1" \
"r2, [r3], -r4, rrx" \
"r2, [r3]" \
"r2, r3, [r4]" \
"r2, [r3, #4]" \
"r2, [r3, #-4]" \
"r2, [r3, #0x45]" \
"r2, [r3, #-0x45]" \
"r2, r3, #4" \
"r2, r3, #-4" \
"p10, #7, c2, c0, c1, #4" \
"p10, #7, r2, c0, c1, #4" \
"p10, #0, c2, c0, c1, #4" \
"p10, #0, r2, c0, c1, #4" \
"r2, #4" \
"r2, #-4" \
"r2, #0xEFFF" \
"r3, #0x0000" \
"r4, #0x0201" \
"r4, #0xFFFFFF00" \
"r2, #-4" \
"p10, #7, c2, c0, c1, #4" \
"p10, #7, r2, c0, c1, #4" \
"#4" \
"#-4" \
"p5, c2, [r3]" \
"p5, c3, [r4]" \
"p5, c2, [r3, #4]" \
"p5, c2, [r3, #-4]" \
"p5, c2, [r3, #0x45]" \
"p5, c2, [r3, #-0x45]" \
"s2, [r3]" \
"s3, [r4]" \
"s2, [r3, #4]" \
"s2, [r3, #-4]" \
"s2, [r3, #0x45]" \
"s2, [r3, #-0x45]" \
"r1, {d3-d4}" \
"r1!, {d3-d4}" \
"r2, {d4-d15}" \
"r3!, {d4-d15}" \
"r3!, {d4}" \
"r2, {s4-s31}" \
"r3!, {s4}" \
"{d3-d4}" \
"{d4-d15}" \
"{d4}" \
"{s4-s31}" \
"{s4}" \
"s2, s3, s4" \
"s2, s3" \
"d2, d3, d4" \
"d2, d3" \
"s2, #0" \
"d2, #0" \
"s3, #0.0" \
"d3, #0.0" \
"s4, #-0.1796875" \
"d4, #0.1796875" \
"r2, r3, d1" \
"d1, r2, r3" \
"s1, r2" \
"r2, s1" \
"r2, fpexc" \
"r2, fpscr" \
"r2, fpsid" \
"apsr_nzcv, fpscr" \
"fpexc, r2" \
"fpscr, r2" \
"fpsid, r2" \
"s3, d4" \
"d4, s3" \
""
do
#echo ".syntax unified" > a.s
err="`mktemp --suffix=-stderr.log`"
as_object="${state}/as-$s $args.o"
tcc_object="${state}/tcc-$s $args.o"
expected="${state}/expected-$s $args"
got="${state}/got-$s $args"
if echo "$s $args" | "${CROSS_COMPILE}as" -mlittle-endian ${as_opts} -o "${as_object}" - 2>"${err}"
then
cat "${err}"
rm -f "${err}"
total_count=`expr $total_count + 1`
"${CROSS_COMPILE}objdump" -S "${as_object}" |grep "^[ ]*0:" >"${expected}"
#echo '__asm__("'"$s ${args}"'");' > "${csource}"
if echo '__asm__("'"$s ${args}"'");'| ${TCC} -o "${tcc_object}" -c -
then
"${CROSS_COMPILE}objdump" -S "${tcc_object}" |grep "^[ ]*0:" >"${got}"
if diff -u "${got}" "${expected}"
then
touch "${state}/ok-$s $args"
else
echo "warning: '$s $args' did not work in tcc (see above)">&2
fi
else
rm -f "${tcc_object}"
echo "warning: '$s $args' did not work in tcc">&2
fi
ok=1
else # GNU as can't do it either--so we don't care
rm -f "${as_object}"
fi
rm -f "${err}"
done
if [ "${ok}" -eq "0" ]
then
echo "warning: $s could not be used.">&2
continue
fi
done
successful_count="$(ls -1 "${state}/ok-"* |wc -l)"
total_count="$(ls -1 "${state}/as-"*.o |wc -l)"
echo "${successful_count} of ${total_count} tests succeeded.">&2
if [ "${successful_count}" -eq "${total_count}" ]
then
rm -rf "${state}"
exit 0
else
status=0
for s in "${state}/as-"*.o
do
test="$(basename "$s")"
test="${test%.o}"
test="${test#as-}"
t="${state}/ok-${test}"
if [ ! -f "$t" ]
then
case "${test}" in
"bl r3"|"b r3"|"mov r2, #0xEFFF"|"mov r4, #0x0201")
known_failure=" (known failure)"
;;
"vmov.f32 r2, r3, d1"|"vmov.f32 d1, r2, r3") # GNU as bug
known_failure=" (known failure)"
;;
*)
known_failure=""
status=1
;;
esac
echo "Failed test: ${test}${known_failure}">&2
fi
done
rm -rf "${state}"
exit "${status}"
fi

View File

@ -1,18 +1,30 @@
#include <stdio.h>
#if defined _WIN32 && !defined __TINYC__
#if (defined _WIN32 || defined __APPLE__) && (!defined __TINYC__ || defined __leading_underscore)
# define _ "_"
#else
# define _
#endif
static int x1_c(void)
#ifdef __clang__
/* clang needs some help tp not throw functions away even at -O0 */
#define __USED __attribute__((__used__))
#else
#define __USED
#endif
static int __USED x1_c (void)
{
printf(" x1");
return 1;
}
#if __i386__
asm(".text;"_"x1: call "_"x1_c; ret");
#else
/* Keep stack aligned */
asm(".text;"_"x1: sub $8,%rsp; call "_"x1_c; add $8,%rsp; ret");
#endif
void callx4(void);
void callx5_again(void);
@ -37,7 +49,7 @@ int main(int argc, char *argv[])
}
static
int x2(void)
int __USED x2(void)
{
printf(" x2");
return 2;

View File

@ -1,11 +1,18 @@
#include <stdio.h>
#if defined _WIN32 && !defined __TINYC__
#if (defined _WIN32 || defined __APPLE__) && (!defined __TINYC__ || defined __leading_underscore)
# define _ "_"
#else
# define _
#endif
#ifdef __clang__
/* clang needs some help tp not throw functions away even at -O0 */
#define __USED __attribute__((__used__))
#else
#define __USED
#endif
int x3(void)
{
printf(" x3");
@ -15,7 +22,12 @@ int x3(void)
/* That callx4 is defined globally (as if ".globl callx4")
is a TCC extension. GCC doesn't behave like this. */
void callx4(void);
#if __i386__
__asm__(_"callx4: call "_"x4; ret;"
#else
/* Keep stack aligned */
__asm__(_"callx4: sub $8,%rsp; call "_"x4; add $8,%rsp; ret;"
#endif
#ifndef __TINYC__
" .global "_"callx4"
#endif
@ -30,7 +42,7 @@ void callx5_again(void)
asm("call "_"x6");
}
static void x6()
static void __USED x6()
{
printf(" x6-2");
}

View File

@ -425,7 +425,7 @@ L3:
fsubp %st(5)
fsubp
fsubp %st(1), %st
//fsubp %st(1), %st # not accepted by new GAS anymore
fsubs 0x1000
fisubs 0x1002
@ -438,7 +438,7 @@ L3:
fsubrp %st(5)
fsubrp
fsubrp %st(1), %st
//fsubrp %st(1), %st # not accepted by new GAS anymore
fsubrs 0x1000
fisubrs 0x1002
@ -451,7 +451,7 @@ L3:
fdivp %st(5)
fdivp
fdivp %st(1), %st
//fdivp %st(1), %st # not accepted by new GAS anymore
fdivs 0x1000
fidivs 0x1002
@ -632,10 +632,19 @@ int $0x10
leave
int3
iret
iretw
iretl
#ifdef __x86_64__
iretq
#endif
rsm
hlt
wait
nop
vmcall
vmlaunch
vmresume
vmxoff
/* XXX: handle prefixes */
#if 0
@ -692,6 +701,10 @@ int $0x10
fxsaveq (%r11)
fxrstorq (%rcx)
fxrstorq (%r10)
movnti %ebx, (%rdi)
movntil %ecx, (%rdi)
movnti %rax, (%rdi)
movntiq %r8, (%rdi)
#endif
@ -745,7 +758,36 @@ int $0x10
bts %edx, 0x1000
btsl $2, 0x1000
popcnt %ax, %si
popcntw %ax, %si
popcnt 0x1000, %edx
popcntl 0x1000, %edx
#ifdef __x86_64__
popcnt %rbx, %rdi
popcntq %rcx, %r8
#endif
lzcnt %cx, %ax
lzcntw %cx, %ax
lzcnt %edx, %ebx
lzcntl 8(%edi), %ecx
#ifdef __x86_64__
lzcnt %rdi, %rdx
lzcntq %r12, %r15
lzcnt 0x40(%rcx), %r11
lzcntq (%r8), %rsi
#endif
tzcnt %cx, %ax
tzcntw %cx, %ax
tzcnt %edx, %ebx
tzcntl -24(%edi), %ecx
#ifdef __x86_64__
tzcnt %rbp, %rdx
tzcntq %rax, %r15
tzcnt -8(%rbp), %rcx
tzcntq (%r8), %r12
#endif
#ifdef __i386__
boundl %edx, 0x10000
@ -768,8 +810,13 @@ int $0x10
swapgs
str %rdx
str %r9
/* Newer gas assemble 'str %rdx' as 'str %edx', based on the observation
that the 16bit value of the task register is zero-extended into the
destination anyway, and hence storing into %edx is the same as storing
into %rdx. TCC doesn't do that micro-optimization, hence just store
into the 32bit reg as well. */
str %edx
str %r9d
#endif
lmsw 0x1000

View File

@ -50,15 +50,12 @@ int test4(void)
int i, sum = 0;
int *tab4;
fprintf(stderr, "%s start\n", __FUNCTION__);
tab4 = malloc(20 * sizeof(int));
for(i=0;i<20;i++) {
sum += tab4[i];
}
free(tab4);
fprintf(stderr, "%s end\n", __FUNCTION__);
return sum;
}
@ -68,20 +65,16 @@ int test5(void)
int i, sum = 0;
int *tab4;
fprintf(stderr, "%s start\n", __FUNCTION__);
tab4 = malloc(20 * sizeof(int));
for(i=0;i<21;i++) {
sum += tab4[i];
}
free(tab4);
fprintf(stderr, "%s end\n", __FUNCTION__);
return sum;
}
/* error */
/* XXX: currently: bug */
int test6(void)
{
int i, sum = 0;
@ -179,54 +172,62 @@ int test13(void)
int test14(void)
{
char *p = alloca(TAB_SIZE);
size_t ret;
memset(p, 'a', TAB_SIZE);
p[TAB_SIZE-1] = 0;
return strlen(p);
ret = strlen(p);
return ret;
}
/* error */
int test15(void)
{
char *p = alloca(TAB_SIZE-1);
size_t ret;
memset(p, 'a', TAB_SIZE);
p[TAB_SIZE-1] = 0;
return strlen(p);
ret = strlen(p);
return ret;
}
/* ok */
int test16()
{
char *demo = "This is only a test.";
char *p;
fprintf(stderr, "%s start\n", __FUNCTION__);
char *p, *q;
p = alloca(16);
strcpy(p,"12345678901234");
printf("alloca: p is %s\n", p);
/* Test alloca embedded in a larger expression */
printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)+1),demo) );
fprintf(stderr, "%s end\n", __FUNCTION__);
return 0;
}
/* error */
int test17()
{
char *demo = "This is only a test.";
char *p;
fprintf(stderr, "%s start\n", __FUNCTION__);
char *p, *q;
p = alloca(16);
strcpy(p,"12345678901234");
printf("alloca: p is %s\n", p);
/* Test alloca embedded in a larger expression */
printf("alloca: %s\n", strcpy(alloca(strlen(demo)),demo) );
printf("alloca : %s : %s\n", p, strcpy(q=alloca(strlen(demo)),demo) );
fprintf(stderr, "%s end\n", __FUNCTION__);
return 0;
}
int test18(void)
{
int i, sum = 0, n = TAB_SIZE;
int tab[n];
for(i=0;i<TAB_SIZE+1;i++) {
sum += tab[i];
}
return sum;
}
int (*table_test[])(void) = {
@ -247,14 +248,25 @@ int (*table_test[])(void) = {
test15,
test16,
test17,
test18
};
int main(int argc, char **argv)
{
int i;
char *cp;
int index;
int (*ftest)(void);
int index_max = sizeof(table_test)/sizeof(table_test[0]);
/* check bounds checking main arg */
for (i = 0; i < argc; i++) {
cp = argv[i];
while (*cp) {
cp++;
}
}
if (argc < 2) {
printf(
"test TCC bound checking system\n"

35
tests/bug.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdarg.h>
int compile_errors(void)
{
#if TEST == 1
{
/* Not constant */
static int i = (&"Foobar"[1] - &"Foobar"[0]);
}
#endif
#if TEST == 2
{
/* Not constant */
struct{int c;}v;
static long i=((char*)&(v.c)-(char*)&v);
}
#endif
#if TEST == 3
{
/* Not constant */
static const short ar[] = { &&l1 - &&l1, &&l2 - &&l1 };
void *p = &&l1 + ar[0];
goto *p;
l1: return 1;
l2: return 2;
}
#endif
return 0;
}
int
main(void)
{
}

163
tests/gcctestsuite.sh Normal file → Executable file
View File

@ -1,33 +1,164 @@
#!/bin/sh
TESTSUITE_PATH=$HOME/gcc/gcc-3.2/gcc/testsuite/gcc.c-torture
TCC="./tcc -B. -I. -DNO_TRAMPOLINES"
rm -f tcc.sum tcc.log
if [ -z "$TESTSUITE_PATH" ]
then
if [ -d "$HOME/gcc/gcc-3.2/gcc/testsuite/gcc.c-torture" ]
then
TESTSUITE_PATH="$HOME/gcc/gcc-3.2/gcc/testsuite/gcc.c-torture"
fi
fi
if [ -z "$TESTSUITE_PATH" ]
then
echo "gcc testsuite not found."
echo "define TESTSUITE_PATH to point to the gcc.c-torture directory"
exit 1
fi
if [ -z "$TCC_SOURCE_PATH" ]
then
if [ -f "include/tccdefs.h" ]
then
TCC_SOURCE_PATH="."
elif [ -f "../include/tccdefs.h" ]
then
TCC_SOURCE_PATH=".."
elif [ -f "../tinycc/include/tccdefs.h" ]
then
TCC_SOURCE_PATH="../tinycc"
fi
fi
if [ -z "$RUNTIME_DIR" ]
then
RUNTIME_DIR=$XDG_RUNTIME_DIR
fi
if [ -z "$RUNTIME_DIR" ]
then
RUNTIME_DIR="/tmp"
fi
if [ -z "$CC" ]
then
if [ -z "$TCC_SOURCE_PATH" ]
then
echo "tcc not found."
echo "define TCC_SOURCE_PATH to point to the tcc source path"
exit 1
fi
TCC="./tcc -B. -I$TCC_SOURCE_PATH/ -I$TCC_SOURCE_PATH/include -DNO_TRAMPOLINES"
else
TCC="$CC -O1 -Wno-implicit-int $CFLAGS"
fi
rm -f tcc.sum tcc.fail
nb_ok="0"
nb_skipped="0"
nb_failed="0"
nb_exe_failed="0"
# skip some failed tests not implemented in tcc
# builtin: gcc "__builtins_*"
# ieee: gcc "__builtins_*" in the ieee directory
# complex: C99 "_Complex" and gcc "__complex__"
# misc: stdc features, other arch, gcc extensions (example: gnu_inline in c89)
#
old_pwd="`pwd`"
cd "$TESTSUITE_PATH"
skip_builtin="`grep '_builtin_' compile/*.c execute/*.c execute/ieee/*.c | cut -d ':' -f1 | cut -d '/' -f2 | sort -u `"
skip_ieee="`grep '_builtin_' execute/ieee/*.c | cut -d ':' -f1 | cut -d '/' -f3 | sort -u `"
skip_complex="`grep -i '_Complex' compile/*.c execute/*.c execute/ieee/*.c | cut -d ':' -f1 | cut -d '/' -f2 | sort -u `"
skip_int128="`grep -Eiw '__int128_t|__uint128_t' compile/*.c execute/*.c execute/ieee/*.c | cut -d ':' -f1 | cut -d '/' -f2 | sort -u `"
skip_vector="`grep -Eiw 'vector|vector_size|__vector_size__' compile/*.c execute/*.c execute/ieee/*.c | cut -d ':' -f1 | cut -d '/' -f2 | sort -u `"
skip_misc="20000120-2.c mipscop-1.c mipscop-2.c mipscop-3.c mipscop-4.c
fp-cmp-4f.c fp-cmp-4l.c fp-cmp-8f.c fp-cmp-8l.c pr38016.c "
cd "$old_pwd"
for src in $TESTSUITE_PATH/compile/*.c ; do
echo $TCC -o /tmp/test.o -c $src
$TCC -o /tmp/test.o -c $src >> tcc.log 2>&1
echo $TCC -o $RUNTIME_DIR/tst.o -c $src
if $TCC -o $RUNTIME_DIR/tst.o -c $src 2>&1 | grep 'cannot use local functions' >/dev/null 2>&1
then
result="SKIP"
nb_skipped=$(( $nb_skipped + 1 ))
else
$TCC -o $RUNTIME_DIR/tst.o -c $src >> tcc.fail 2>&1
if [ "$?" = "0" ] ; then
result="PASS"
nb_ok=$(( $nb_ok + 1 ))
else
base=`basename "$src"`
skip_me="`echo $skip_builtin $skip_ieee $skip_complex $skip_int128 $skip_misc $skip_vector | grep -w $base`"
if [ -n "$skip_me" ]
then
result="SKIP"
nb_skipped=$(( $nb_skipped + 1 ))
else
result="FAIL"
nb_failed=$(( $nb_failed + 1 ))
fi
echo "$result: $src" >> tcc.sum
done
for src in $TESTSUITE_PATH/execute/*.c ; do
echo $TCC $src
$TCC $src >> tcc.log 2>&1
if [ "$?" = "0" ] ; then
result="PASS"
else
result="FAIL"
nb_failed=$(( $nb_failed + 1 ))
fi
fi
echo "$result: $src" >> tcc.sum
done
if [ -f "$RUNTIME_DIR/tst.o" ]
then
rm -f "$RUNTIME_DIR/tst.o"
fi
for src in $TESTSUITE_PATH/execute/*.c $TESTSUITE_PATH/execute/ieee/*.c ; do
echo $TCC $src -o $RUNTIME_DIR/tst -lm
if $TCC $src -o $RUNTIME_DIR/tst -lm 2>&1 | grep 'cannot use local functions' >/dev/null 2>&1
then
result="SKIP"
nb_skipped=$(( $nb_skipped + 1 ))
else
$TCC $src -o $RUNTIME_DIR/tst -lm >> tcc.fail 2>&1
if [ "$?" = "0" ] ; then
if $RUNTIME_DIR/tst >> tcc.fail 2>&1
then
result="PASS"
nb_ok=$(( $nb_ok + 1 ))
else
result="FAILEXE"
nb_exe_failed=$(( $nb_exe_failed + 1 ))
fi
else
base=`basename "$src"`
skip_me="`echo $skip_builtin $skip_ieee $skip_complex $skip_int128 $skip_misc $skip_vector | grep -w $base`"
if [ -n "$skip_me" ]
then
result="SKIP"
nb_skipped=$(( $nb_skipped + 1 ))
else
result="FAIL"
nb_failed=$(( $nb_failed + 1 ))
fi
fi
fi
echo "$result: $src" >> tcc.sum
done
if [ -f "$RUNTIME_DIR/tst.o" ]
then
rm -f "$RUNTIME_DIR/tst.o"
fi
if [ -f "$RUNTIME_DIR/tst" ]
then
rm -f "$RUNTIME_DIR/tst"
fi
echo "$nb_ok test(s) ok." >> tcc.sum
echo "$nb_ok test(s) ok."
echo "$nb_skipped test(s) skipped." >> tcc.sum
echo "$nb_skipped test(s) skipped."
echo "$nb_failed test(s) failed." >> tcc.sum
echo "$nb_failed test(s) failed."
echo "$nb_exe_failed test(s) exe failed." >> tcc.sum
echo "$nb_exe_failed test(s) exe failed."

View File

@ -6,9 +6,13 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libtcc.h"
void handle_error(void *opaque, const char *msg)
{
fprintf(opaque, "%s\n", msg);
}
/* this function is called by the generated code */
int add(int a, int b)
{
@ -53,6 +57,9 @@ int main(int argc, char **argv)
exit(1);
}
/* set custom error/warning printer */
tcc_set_error_func(s, stderr, handle_error);
/* if tcclib.h and libtcc1.a are not installed, where can we find them */
for (i = 1; i < argc; ++i) {
char *a = argv[i];
@ -78,7 +85,7 @@ int main(int argc, char **argv)
tcc_add_symbol(s, "hello", hello);
/* relocate the code */
if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
if (tcc_relocate(s) < 0)
return 1;
/* get entry symbol */

363
tests/libtcc_test_mt.c Normal file
View File

@ -0,0 +1,363 @@
/*
* Multi-thread Test for libtcc
*/
#ifndef FIB
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "libtcc.h"
#define M 20 /* number of states */
#define F(n) (n % 20 + 2) /* fib argument */
#ifdef _WIN32
#include <windows.h>
#define TF_TYPE(func, param) DWORD WINAPI func(void *param)
typedef TF_TYPE(ThreadFunc, x);
HANDLE hh[M];
void create_thread(ThreadFunc f, int n)
{
DWORD tid;
hh[n] = CreateThread(NULL, 0, f, (void*)(size_t)n, 0, &tid);
}
void wait_threads(int n)
{
WaitForMultipleObjects(n, hh, TRUE, INFINITE);
while (n)
CloseHandle(hh[--n]);
}
void sleep_ms(unsigned n)
{
Sleep(n);
}
#else
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#define TF_TYPE(func, param) void* func(void *param)
typedef TF_TYPE(ThreadFunc, x);
pthread_t hh[M];
void create_thread(ThreadFunc f, int n)
{
pthread_create(&hh[n], NULL, f, (void*)(size_t)n);
}
void wait_threads(int n)
{
while (n)
pthread_join(hh[--n], NULL);
}
void sleep_ms(unsigned n)
{
usleep(n * 1000);
}
#endif
void handle_error(void *opaque, const char *msg)
{
fprintf(opaque, "%s\n", msg);
}
/* this function is called by the generated code */
int add(int a, int b)
{
return a + b;
}
#define _str(s) #s
#define str(s) _str(s)
/* as a trick, prepend #line directive for better error/warning messages */
#define PROG(lbl) \
char lbl[] = "#line " str(__LINE__) " " str(__FILE__) "\n\n"
PROG(my_program)
"#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
"int add(int a, int b);\n"
"int fib(int n)\n"
"{\n"
" if (n <= 2)\n"
" return 1;\n"
" else\n"
" return add(fib(n-1),fib(n-2));\n"
"}\n"
"\n"
"void bar(void) { *(void**)0 = 0; }\n"
"\n"
"int foo(int n)\n"
"{\n"
" printf(\" %d\", fib(n));\n"
" if (n >= N_CRASH && n < N_CRASH + 8)\n"
" bar();\n"
" return 0;\n"
"# warning is this the correct file:line...\n"
"}\n";
int g_argc; char **g_argv;
void parse_args(TCCState *s)
{
int i;
/* if tcclib.h and libtcc1.a are not installed, where can we find them */
for (i = 1; i < g_argc; ++i) {
char *a = g_argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
tcc_set_lib_path(s, a+2);
else if (a[1] == 'I')
tcc_add_include_path(s, a+2);
else if (a[1] == 'L')
tcc_add_library_path(s, a+2);
else if (a[1] == 'D')
tcc_define_symbol(s, a+2, NULL);
}
}
}
int backtrace_func(
void *ud,
void *pc,
const char *file,
int line,
const char *func,
const char *msg)
{
#if 0
printf("\n *** %p %s %s:%d in '%s'",
pc,
msg ? "at" : "by",
file ? file : "?",
line,
func ? func : "?");
return 1; // want more backtrace levels
#else
//printf(" [%d]", *(int*)ud);
printf("!");
return 0; // cancel backtrace
#endif
}
TCCState *new_state(int w)
{
TCCState *s = tcc_new();
if (!s) {
fprintf(stderr, __FILE__ ": could not create tcc state\n");
exit(1);
}
tcc_set_error_func(s, stdout, handle_error);
parse_args(s);
if (0 == (w & 1))
tcc_set_options(s, "-w");
if (w & 2) {
tcc_set_options(s, "-bt");
tcc_define_symbol(s, "N_CRASH", str(M/2));
} else
tcc_define_symbol(s, "N_CRASH", "-1000");
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
return s;
}
void *reloc_state(TCCState *s, const char *entry)
{
void *func;
tcc_add_symbol(s, "add", add);
tcc_add_symbol(s, "printf", printf);
if (tcc_relocate(s) < 0) {
fprintf(stderr, __FILE__ ": could not relocate tcc state.\n");
return NULL;
}
func = tcc_get_symbol(s, entry);
if (!func)
fprintf(stderr, __FILE__ ": could not get entry symbol.\n");
return func;
}
/* work with several states at the same time */
int state_test(int w)
{
TCCState *s[M];
int (*funcs[M])(int);
int n;
jmp_buf jb;
for (n = 0; n < M + 4; ++n) {
unsigned a = n, b = n - 1, c = n - 2, d = n - 3, e = n - 4;
if (a < M)
s[a] = new_state(w);
if (b < M)
if (tcc_compile_string(s[b], my_program) == -1)
break;
if (c < M)
funcs[c] = reloc_state(s[c], "foo");
if (d < M && funcs[d]) {
tcc_set_backtrace_func(s[d], &d, backtrace_func);
if (0 == tcc_setjmp(s[d], jb, funcs[d]))
funcs[d](F(d));
}
if (e < M)
tcc_delete(s[e]);
}
return 0;
}
/* simple compilation in threads */
TF_TYPE(thread_test_simple, vn)
{
TCCState *s;
int (*func)(int);
int ret;
int n = (size_t)vn;
jmp_buf jb;
s = new_state(0); /* '2' for exceptions */
sleep_ms(1);
ret = tcc_compile_string(s, my_program);
sleep_ms(1);
if (ret >= 0) {
func = reloc_state(s, "foo");
tcc_set_backtrace_func(s, &n, backtrace_func);
if (func) {
if (0 == tcc_setjmp(s, jb, func))
func(F(n));
}
}
tcc_delete(s);
return 0;
}
/* more complex compilation in threads */
TF_TYPE(thread_test_complex, vn)
{
TCCState *s;
int ret;
int n = (size_t)vn;
char *argv[30], b[10];
int argc = 0, i;
sprintf(b, "%d", F(n));
for (i = 1; i < g_argc; ++i) argv[argc++] = g_argv[i];
#if 0
argv[argc++] = "-run";
for (i = 1; i < g_argc; ++i) argv[argc++] = g_argv[i];
#endif
argv[argc++] = "-DFIB";
argv[argc++] = "-run";
argv[argc++] = __FILE__;
argv[argc++] = b;
argv[argc] = NULL;
s = new_state(1);
sleep_ms(2);
ret = tcc_add_file(s, argv[0]);
sleep_ms(3);
if (ret == 0)
tcc_run(s, argc, argv);
tcc_delete(s);
fflush(stdout);
return 0;
}
void time_tcc(int n, const char *src)
{
TCCState *s;
int ret, i = 0;
while (i++ < n) {
s = new_state(1);
printf(" %d", i), fflush(stdout);
ret = tcc_add_file(s, src);
tcc_delete(s);
if (ret < 0)
exit(1);
}
}
static unsigned getclock_ms(void)
{
#ifdef _WIN32
return GetTickCount();
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
#endif
}
int main(int argc, char **argv)
{
int n;
unsigned t;
g_argc = argc;
g_argv = argv;
if (argc < 2) {
fprintf(stderr, "usage: libtcc_test_mt tcc.c <options>\n");
return 1;
}
#if 1
printf("running fib with mixed calls\n "), fflush(stdout);
t = getclock_ms();
state_test(0);
printf("\n (%u ms)\n", getclock_ms() - t);
#endif
#if 1
printf("producing some exceptions (!)\n "), fflush(stdout);
t = getclock_ms();
state_test(2);
printf("\n (%u ms)\n", getclock_ms() - t);
#endif
#if 1
//{ int i; for (i = 0; i < 100; ++i) { printf("(%d) ", i);
printf("running fib in threads\n "), fflush(stdout);
t = getclock_ms();
for (n = 0; n < M; ++n)
create_thread(thread_test_simple, n);
wait_threads(n);
printf("\n (%u ms)\n", getclock_ms() - t);
//}}
#endif
#if 1
printf("running tcc.c in threads to run fib\n "), fflush(stdout);
t = getclock_ms();
for (n = 0; n < M; ++n)
create_thread(thread_test_complex, n);
wait_threads(n);
printf("\n (%u ms)\n", getclock_ms() - t);
#endif
#if 1
printf("compiling tcc.c 10 times\n "), fflush(stdout);
t = getclock_ms();
time_tcc(10, argv[1]);
printf("\n (%u ms)\n", getclock_ms() - t), fflush(stdout);
#endif
return 0;
}
#else
#include <tcclib.h>
#ifdef _WIN32
# ifndef _WIN64
__declspec(stdcall)
# endif
void Sleep(unsigned);
# define sleep_ms Sleep
#else
int usleep(unsigned long);
# define sleep_ms(x) usleep((x)*1000);
#endif
int fib(n)
{
return (n <= 2) ? 1 : fib(n-1) + fib(n-2);
}
int main(int argc, char **argv)
{
sleep_ms(333);
printf(" %d", fib(atoi(argv[1])));
return 0;
}
#endif

164
tests/msvcrt_start.c Normal file
View File

@ -0,0 +1,164 @@
/* ------------------------------------------------------------- */
/* minimal startup with runtime linker to msvcrt */
#if 0
#define REDIR_ALL \
REDIR(__set_app_type)\
REDIR(__getmainargs)\
REDIR(_controlfp)\
REDIR(_vsnprintf)\
REDIR(exit)\
\
REDIR(puts)\
REDIR(printf)\
REDIR(putchar)\
REDIR(strtod)\
REDIR(memset)\
REDIR(strcpy)\
REDIR(strlen)\
REDIR(malloc)\
REDIR(free)\
#if defined __i386__ && !defined __TINYC__
# define __leading_underscore 1
#endif
#ifdef __leading_underscore
# define _(s) "_"#s
#else
# define _(s) #s
#endif
#define REDIR(s) void *s;
static struct { REDIR_ALL } all_ptrs;
#undef REDIR
#define REDIR(s) #s"\0"
static const char all_names[] = REDIR_ALL;
#undef REDIR
#if __aarch64__
#if defined __TINYC__
# define ALIGN ".align 8"
#else
# define ALIGN ".align 3" /* .align is power of 2 on non-ELF platforms */
#endif
# define REDIR(s) \
__asm__("\n"_(s)":"); \
__asm__(".int 0x58000090"); /* ldr x16, [pc, #16] */ \
__asm__(".int 0xf9400210"); /* ldr x16, [x16] */ \
__asm__(".int 0xd61f0200"); /* br x16 */ \
__asm__(".int 0xd503201f"); /* nop for alignment */ \
__asm__(".quad all_ptrs + (. - all_jmps - 16) / 24 * 8"); \
__asm__(".global "_(s));
__asm__("\t.text\n\t"ALIGN"\nall_jmps:");
REDIR_ALL
#else
# define REDIR(s) \
__asm__("\n"_(s)":");\
__asm__("jmp *%0"::"m"(all_ptrs.s));\
__asm__(".global "_(s));
static void all_jmps() { REDIR_ALL }
#endif
#undef REDIR
#if 0
# include <windows.h>
#else
# if __i386__
# define STDCALL __declspec(stdcall)
# else
# define STDCALL
# endif
# define DWORD long unsigned
# define HMODULE void*
# define HANDLE void*
S TDCALL HMODULE LoadLibraryA(const char *);
S TDCALL HMODULE GetProcAddress(HMODULE , char*);
S TDCALL void ExitProcess(int);
S TDCALL int WriteFile(HANDLE, const void*, DWORD, DWORD*, void*);
S TDCALL HANDLE GetStdHandle(DWORD);
S TDCALL int FlushFileBuffers(HANDLE);
# define STD_ERROR_HANDLE -12
#endif
static void eput(const char *s)
{
DWORD n_out;
int n = 0;
while (s[n])
++n;
WriteFile(GetStdHandle(STD_ERROR_HANDLE), s, n, &n_out, 0);
}
static void rt_reloc()
{
const char *s = all_names;
void **p = (void**)&all_ptrs;
void *dll = LoadLibraryA("msvcrt.dll");
do {
char buf[100], *d = buf;
*p = (void*)GetProcAddress(dll, (char*)s);
*d++ = '_'; do *d++ = *s; while (*s++);
if (0 == *p)
*p = (void*)GetProcAddress(dll, buf);
if (0 == *p) {
eput("MSVCRT_START.C: RUNTIME RELOCATION ERROR: '");
eput(buf+1);
eput("'\n");
ExitProcess(-1);
}
++p;
} while (*s);
}
#else
# define rt_reloc()
#endif
int main(int argc, char **argv, char **env);
void exit(int);
void __set_app_type(int apptype);
typedef struct { int newmode; } _startupinfo;
int __getmainargs(int *pargc, char ***pargv, char ***penv, int globb, _startupinfo*);
void _controlfp(unsigned a, unsigned b);
#define _MCW_PC 0x00030000 // Precision Control
#define _PC_53 0x00010000 // 53 bits
int __argc;
char **__argv;
char **environ;
_startupinfo start_info = {0};
void mainCRTStartup(void)
{
rt_reloc();
#if defined __i386__ || defined __x86_64__
_controlfp(_PC_53, _MCW_PC);
#endif
__set_app_type(1);
__getmainargs(&__argc, &__argv, &environ, 0, &start_info);
exit(main(__argc, __argv, environ));
}
#include <stdarg.h>
#define size_t __SIZE_TYPE__
int printf(const char *, ...);
int _vsnprintf(char *, size_t, const char *, va_list);
/* undefined on windows-11-arm64 */
int vprintf(const char *format, va_list ap)
{
char buf[1000];
_vsnprintf(buf, sizeof buf, format, ap);
return printf("%s", buf);
}
void __main() {} /* for gcc */
void _pei386_runtime_relocator(void) {} /* for gcc */
void __chkstk(unsigned n) {} /* for clang */

View File

@ -29,3 +29,8 @@ __NORETURN
#define X(...)
#define Y(...) 1 __VA_ARGS__ 2
Y(X X() ())
#define DDD(A, B) D_ ## B ## _D_ ## A
#define CCC(X, ...) DDD(X,## __VA_ARGS__)
/* must be D_B_D_A (not D_B _D_A) */
CCC(A,B)

View File

@ -13,3 +13,4 @@ x A y
x y
__attribute__((__noreturn__))
1 2
D_B_D_A

View File

@ -13,3 +13,18 @@
X181 M_EMPTYI_P_C1()
X182 M_EMPTYI_P_C1(x)
X183 usefnlike()
#define ABC(x) ABC : x
#define A(a,b,c) a ## b ## c
#define B(a,b,c) A(a,b,c)
#define C(a,b,c) a b c
B(
C(A,C(,,),),
C(B(,,),B,B(,,)),
C(B(,,),B(,,),C)
)/* */(a b c)
#define TEST(x) TEST : x
#define K(a,b) a ## b
#define L(a,b) K( TE a , b ST )
L(,)(t e s t)

View File

@ -1,3 +1,5 @@
X181 1
X182 0
X183 yeah
ABC : a b c
TEST : t e s t

View File

@ -34,3 +34,29 @@ OK
#else
NOT OK
#endif
----- 5 ------
line __LINE__
#define __LINE__ # ## #
line __LINE__
----- 10 ------
/* preprocessor numbers are (u)intmax_t */
#if -2147483648 < 0
1 true
#endif
#if -0x80000000 < 0
2 true
#endif
#if -9223372036854775808U > 0
3 true
#endif
#if -0x8000000000000000 > 0 // unsigned by overflow
4 true
#endif
#if 1 << 31 > 2 && 1 << 32 > 2 && 1 << 63 < 2 && 1U << 63 > 2
5 true
#endif
#if (1<<29) * 11 >= 1<<32 && defined DDD << 63 < 0
6 true
#endif

Some files were not shown because too many files have changed in this diff Show More