macos: ldd does not exit, use otool instead
[tinycc.git] / lib / bt-dll.c
blob3389bd72128ff2c7f9f67c0cde3a2125d9f92c11
1 /* ------------------------------------------------------------- */
2 /* stubs for calling bcheck functions from a dll. */
4 #include <windows.h>
5 #include <stdio.h>
7 #define REDIR_ALL \
8 REDIR(__bt_init) \
9 REDIR(tcc_backtrace) \
11 REDIR(__bound_ptr_add) \
12 REDIR(__bound_ptr_indir1) \
13 REDIR(__bound_ptr_indir2) \
14 REDIR(__bound_ptr_indir4) \
15 REDIR(__bound_ptr_indir8) \
16 REDIR(__bound_ptr_indir12) \
17 REDIR(__bound_ptr_indir16) \
18 REDIR(__bound_local_new) \
19 REDIR(__bound_local_delete) \
20 REDIR(__bound_new_region) \
22 REDIR(__bound_free) \
23 REDIR(__bound_malloc) \
24 REDIR(__bound_realloc) \
25 REDIR(__bound_memcpy) \
26 REDIR(__bound_memcmp) \
27 REDIR(__bound_memmove) \
28 REDIR(__bound_memset) \
29 REDIR(__bound_strlen) \
30 REDIR(__bound_strcpy) \
31 REDIR(__bound_strncpy) \
32 REDIR(__bound_strcmp) \
33 REDIR(__bound_strncmp) \
34 REDIR(__bound_strcat) \
35 REDIR(__bound_strchr) \
36 REDIR(__bound_strdup)
38 #ifdef __leading_underscore
39 #define _(s) "_"#s
40 #else
41 #define _(s) #s
42 #endif
44 #define REDIR(s) void *s;
45 static struct { REDIR_ALL } all_ptrs;
46 #undef REDIR
47 #define REDIR(s) #s"\0"
48 static const char all_names[] = REDIR_ALL;
49 #undef REDIR
50 #define REDIR(s) __asm__(".global " _(s) ";" _(s) ": jmp *%0" : : "m" (all_ptrs.s) );
51 static void all_jmps() { REDIR_ALL }
52 #undef REDIR
54 void __bt_init_dll(int bcheck)
56 const char *s = all_names;
57 void **p = (void**)&all_ptrs;
58 do {
59 *p = (void*)GetProcAddress(GetModuleHandle(NULL), (char*)s);
60 if (NULL == *p) {
61 char buf[100];
62 sprintf(buf,
63 "Error: function '%s()' not found in executable. "
64 "(Need -bt or -b for linking the exe.)", s);
65 if (GetStdHandle(STD_ERROR_HANDLE))
66 fprintf(stderr, "TCC/BCHECK: %s\n", buf), fflush(stderr);
67 else
68 MessageBox(NULL, buf, "TCC/BCHECK", MB_ICONERROR);
69 ExitProcess(1);
71 s = strchr(s,'\0') + 1, ++p;
72 } while (*s && (bcheck || p < &all_ptrs.__bound_ptr_add));