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
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).
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.
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
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)
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
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.
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
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
- 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
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
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.
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'
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
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
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'
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)
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
- 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
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
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
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
lib/bt-exe.c:
- call __bound_init before sigset_exception_handler because sigaction
is redirected.
tests/tests2/Makefile:
- run testcase 114 on macos again
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
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