Commit graph

3508 commits

Author SHA1 Message Date
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