Commit Graph

3438 Commits

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