Clean up more code

- Found some bugs in LLVM compiler-rt library
- The useless LIBC_STUBS package is now deleted
- Improve the overflow checking story even further
- Get chibicc tests working in MODE=dbg mode again
- The libc/isystem/ headers now have correctly named guards
This commit is contained in:
Justine Tunney 2023-06-18 00:55:09 -07:00
parent afc58a8b41
commit d7c79f43ef
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
294 changed files with 912 additions and 1208 deletions

View file

@ -17,11 +17,12 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
void __cxa_pure_virtual(void) {
#ifndef NDEBUG
kprintf("__cxa_pure_virtual() called\n"
"Did you call a virtual method from a destructor?\n");
#endif
__builtin_trap();
abort();
}

23
libc/intrin/abort.c Normal file
View file

@ -0,0 +1,23 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
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.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
// stub version of abort() for low-dependency apps
__attribute__((__noreturn__, __weak__)) void abort(void) {
__builtin_trap();
}

View file

@ -176,7 +176,7 @@ static bool __asan_once(void) {
#define __asan_unreachable() \
do { \
kprintf("%s:%d: __asan_unreachable()\n", __FILE__, __LINE__); \
for (;;) __builtin_trap(); \
__builtin_trap(); \
} while (0)
static int __asan_bsf(uint64_t x) {

View file

@ -15,8 +15,6 @@
#include "third_party/compiler_rt/int_lib.h"
#ifdef CRT_HAS_128BIT
/* Returns: a << b */
/* Precondition: 0 <= b < bits_in_tword */
@ -42,5 +40,3 @@ __ashlti3(ti_int a, si_int b)
}
return result.all;
}
#endif /* CRT_HAS_128BIT */

View file

@ -20,6 +20,7 @@
#include "libc/atomic.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
privileged void __assert_fail(const char *expr, const char *file, int line) {
static atomic_bool once;

View file

@ -29,13 +29,13 @@ COMPILER_RT_ABI ti_int __divmodti4(ti_int a, ti_int b, tu_int *opt_out_rem) {
}
sa = a >> k; // sa = a < 0 ? -1 : 0
sb = b >> k; // sb = b < 0 ? -1 : 0
x = (a ^ sa) - sa; // negate if sa == -1
y = (b ^ sb) - sb; // negate if sb == -1
x = (tu_int)(a ^ sa) - sa; // negate if sa == -1
y = (tu_int)(b ^ sb) - sb; // negate if sb == -1
sq = sa ^ sb; // sign of quotient
sr = sa; // sign of remainder
q = __udivmodti4(x, y, &r); // unsigned divide
q = (q ^ sq) - sq; // fix quotient sign
r = (r ^ sr) - sr; // fix remainder sign
q = (tu_int)(q ^ sq) - sq; // fix quotient sign
r = (tu_int)(r ^ sr) - sr; // fix remainder sign
if (opt_out_rem) *opt_out_rem = r;
return q;
}

61
libc/intrin/gcov.S Normal file
View file

@ -0,0 +1,61 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
// Magic words to unbreak build if GCOV flags are passed.
__gcov_init:
ret
.endfn __gcov_init,globl,weak
__gcov_exit:
ret
.endfn __gcov_exit,globl,weak
__gcov_merge_add:
ret
.endfn __gcov_merge_add,globl,weak
__gcov_fork:
ret
.endfn __gcov_fork,globl,weak
__gcov_execle:
ret
.endfn __gcov_execle,globl,weak
__gcov_execlp:
ret
.endfn __gcov_execlp,globl,weak
__gcov_execl:
ret
.endfn __gcov_execl,globl,weak
__gcov_execv:
ret
.endfn __gcov_execv,globl,weak
__gcov_execvp:
ret
.endfn __gcov_execvp,globl,weak
__gcov_execve:
ret
.endfn __gcov_execve,globl,weak

View file

@ -28,12 +28,11 @@ LIBC_INTRIN_A_CHECKS = \
$(LIBC_INTRIN_A_HDRS:%=o/$(MODE)/%.ok)
LIBC_INTRIN_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_NT_WS2_32
LIBC_NT_WS2_32 \
LIBC_SYSV \
LIBC_SYSV_CALLS
LIBC_INTRIN_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_INTRIN_A_DIRECTDEPS),$($(x))))
@ -218,8 +217,12 @@ o/$(MODE)/libc/intrin/aarch64/%.o: libc/intrin/aarch64/%.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/fenv.o: libc/intrin/fenv.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/gcov.o: libc/intrin/gcov.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/futex.o: libc/intrin/futex.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/typeinfo.o: libc/intrin/typeinfo.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/kclocknames.o: libc/intrin/kclocknames.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/intrin/kdos2errno.o: libc/intrin/kdos2errno.S

View file

@ -26,7 +26,7 @@ __mulodi4(di_int a, di_int b, int* overflow)
const di_int MIN = (du_int)1 << (N-1);
const di_int MAX = ~MIN;
*overflow = 0;
di_int result = (du_int)a * (du_int)b;
di_int result = (du_int)a * b;
if (a == MIN)
{
if (b != 0 && b != 1)

View file

@ -26,7 +26,7 @@ __mulosi4(si_int a, si_int b, int* overflow)
const si_int MIN = (su_int)1 << (N-1);
const si_int MAX = ~MIN;
*overflow = 0;
si_int result = (su_int)a * (su_int)b;
si_int result = (su_int)a * b;
if (a == MIN)
{
if (b != 0 && b != 1)

View file

@ -28,7 +28,7 @@ __muloti4(ti_int a, ti_int b, int* overflow)
const ti_int MIN = (tu_int)1 << (N-1);
const ti_int MAX = ~MIN;
*overflow = 0;
ti_int result = (tu_int)a * (tu_int)b;
ti_int result = (tu_int)a * b;
if (a == MIN)
{
if (b != 0 && b != 1)

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
/**
* Arithmetic overflow handler.
@ -31,5 +32,5 @@
*/
__attribute__((__weak__)) void __on_arithmetic_overflow(void) {
kprintf("error: -ftrapv caught arithmetic overflow\n");
__builtin_trap();
abort();
}

View file

@ -0,0 +1,25 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
__attribute__((__weak__)) void __stack_chk_fail(void) {
kprintf("%s: stack smashed\n", program_invocation_name);
__builtin_trap();
}

View file

@ -0,0 +1,23 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/internal.h"
__attribute__((__weak__)) void __stack_chk_fail_local(void) {
__stack_chk_fail();
}

View file

@ -0,0 +1,39 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
// Canary for -fstack-protector.
//
// This global is referenced by synthetic code generated by GCC.
// The -mstack-protector-guard=global flag might need to be passed.
//
// @note this value is protected by piro
.initbss 200,_init___stack_chk_guard
__stack_chk_guard:
.quad 0
.endobj __stack_chk_guard,globl
.previous
.init.start 301,_init___stack_chk_guard
rdtsc
stosl
xchg %edx,%eax
stosl
.init.end 301,_init___stack_chk_guard

26
libc/intrin/typeinfo.S Normal file
View file

@ -0,0 +1,26 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
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.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
// __cxxabiv1::__function_type_info (?)
// Because Clang in MODE=dbg doesn't respect -fno-rtti
.balign 8
_ZTVN10__cxxabiv120__function_type_infoE:
.quad 0
.endobj _ZTVN10__cxxabiv120__function_type_infoE,globl

View file

@ -199,7 +199,7 @@ static uintptr_t __ubsan_extend(struct UbsanTypeDescriptor *t, uintptr_t x) {
}
static wontreturn void __ubsan_unreachable(void) {
for (;;) __builtin_trap();
for (;;) abort();
}
static void __ubsan_exit(void) {