Fix bound checking dlcose problem
[tinycc.git] / lib / bt-exe.c
blobb3bf2e672214ceec4ad0bedaf40bb515da301cc5
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 0
7 #include "../tccrun.c"
9 int (*__rt_error)(void*, void*, const char *, va_list);
11 #ifndef _WIN32
12 # define __declspec(n)
13 #endif
15 __declspec(dllexport)
16 void __bt_init(rt_context *p, int num_callers)
18 __attribute__((weak)) int main();
19 __attribute__((weak)) void __bound_init(void*, int);
20 struct rt_context *rc = &g_rtctxt;
21 //fprintf(stderr, "__bt_init %d %p %p\n", num_callers, p->stab_sym, p->bounds_start), fflush(stderr);
22 /* call __bound_init here due to redirection of sigaction */
23 /* needed to add global symbols */
24 if (__bound_init && p->bounds_start)
25 __bound_init(p->bounds_start, -1);
26 if (num_callers) {
27 memcpy(rc, p, offsetof(rt_context, next));
28 rc->num_callers = num_callers - 1;
29 rc->top_func = main;
30 __rt_error = _rt_error;
31 set_exception_handler();
32 } else {
33 p->next = rc->next, rc->next = p;
37 __declspec(dllexport)
38 void __bt_exit(rt_context *p)
40 __attribute__((weak)) void __bound_exit_dll(void*);
41 struct rt_context *rc = &g_rtctxt;
43 if (__bound_exit_dll && p->bounds_start)
44 __bound_exit_dll(p->bounds_start);
45 while (rc) {
46 if (rc->next == p) {
47 rc->next = rc->next->next;
48 break;
50 rc = rc->next;
54 /* copy a string and truncate it. */
55 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
57 int l = strlen(s);
58 if (l >= buf_size)
59 l = buf_size - 1;
60 memcpy(buf, s, l);
61 buf[l] = 0;
62 return buf;