fixup! riscv: Implement large addend for global address
[tinycc.git] / lib / bt-exe.c
blob447eaf975d1d0d0cbf16a7d6a207a2722a46494a
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 #define TCC_SEM_IMPL 1
9 #include "../tccrun.c"
11 #ifndef _WIN32
12 # define __declspec(n)
13 #endif
15 __declspec(dllexport)
16 void __bt_init(rt_context *p, int is_exe)
18 __attribute__((weak)) int main();
19 __attribute__((weak)) void __bound_init(void*, int);
21 //fprintf(stderr, "__bt_init %d %p %p %p\n", is_exe, p, p->stab_sym, p->bounds_start), fflush(stderr);
23 /* call __bound_init here due to redirection of sigaction */
24 /* needed to add global symbols */
25 if (p->bounds_start)
26 __bound_init(p->bounds_start, -1);
28 /* add to chain */
29 rt_wait_sem();
30 p->next = g_rc, g_rc = p;
31 rt_post_sem();
32 if (is_exe) {
33 /* we are the executable (not a dll) */
34 p->top_func = main;
35 set_exception_handler();
39 __declspec(dllexport)
40 void __bt_exit(rt_context *p)
42 struct rt_context *rc, **pp;
43 __attribute__((weak)) void __bound_exit_dll(void*);
45 //fprintf(stderr, "__bt_exit %d %p\n", !!p->top_func, p);
47 if (p->bounds_start)
48 __bound_exit_dll(p->bounds_start);
50 /* remove from chain */
51 rt_wait_sem();
52 for (pp = &g_rc; rc = *pp, rc; pp = &rc->next)
53 if (rc == p) {
54 *pp = rc->next;
55 break;
57 rt_post_sem();
60 /* copy a string and truncate it. */
61 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s)
63 int l = strlen(s);
64 if (l >= buf_size)
65 l = buf_size - 1;
66 memcpy(buf, s, l);
67 buf[l] = 0;
68 return buf;