Add tcov support in Makefile
[tinycc.git] / lib / bt-exe.c
blob7cd56e700e1742bc426f0741aeefd96a69ccbac6
1 /* ------------------------------------------------------------- */
2 /* for linking rt_printline and the signal/exception handler
3 from tccrun.c into executables. */
5 #define CONFIG_TCC_BACKTRACE_ONLY
6 #define ONE_SOURCE 1
7 #define pstrcpy tcc_pstrcpy
8 #include "../tccrun.c"
10 int (*__rt_error)(void*, void*, const char *, va_list);
11 void (*__rt_location)(void*, void*, const char *);
12 __attribute__((weak)) void __bound_checking_lock(void);
13 __attribute__((weak)) void __bound_checking_unlock(void);
15 #ifndef _WIN32
16 # define __declspec(n)
17 #endif
19 __declspec(dllexport)
20 void __bt_init(rt_context *p, int num_callers)
22 __attribute__((weak)) int main();
23 __attribute__((weak)) void __bound_init(void*, int);
24 struct rt_context *rc = &g_rtctxt;
25 //fprintf(stderr, "__bt_init %d %p %p\n", num_callers, p->stab_sym, p->bounds_start), fflush(stderr);
26 /* call __bound_init here due to redirection of sigaction */
27 /* needed to add global symbols */
28 if (p->bounds_start) {
29 __bound_init(p->bounds_start, -1);
30 __bound_checking_lock();
32 if (num_callers) {
33 memcpy(rc, p, offsetof(rt_context, next));
34 rc->num_callers = num_callers - 1;
35 rc->top_func = main;
36 __rt_error = _rt_error;
37 __rt_location = _rt_location;
38 set_exception_handler();
39 } else {
40 p->next = rc->next, rc->next = p;
42 if (p->bounds_start)
43 __bound_checking_unlock();
46 __declspec(dllexport)
47 void __bt_exit(rt_context *p)
49 __attribute__((weak)) void __bound_exit_dll(void*);
50 struct rt_context *rc = &g_rtctxt;
52 if (p->bounds_start) {
53 __bound_exit_dll(p->bounds_start);
54 __bound_checking_lock();
56 while (rc) {
57 if (rc->next == p) {
58 rc->next = rc->next->next;
59 break;
61 rc = rc->next;
63 if (p->bounds_start)
64 __bound_checking_unlock();
67 /* copy a string and truncate it. */
68 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
70 int l = strlen(s);
71 if (l >= buf_size)
72 l = buf_size - 1;
73 memcpy(buf, s, l);
74 buf[l] = 0;
75 return buf;