Debian Codegraph @

signature/internal/sequoia/gosequoia.c

raw
1 /*
2 * Copying and distribution of this file, with or without modification,
3 * are permitted in any medium without royalty provided the copyright
4 * notice and this notice are preserved. This file is offered as-is,
5 * without any warranty.
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include "gosequoia.h"
13
14 #if defined(GO_SEQUOIA_ENABLE_DLOPEN) && GO_SEQUOIA_ENABLE_DLOPEN
15
16 #include <assert.h>
17 #include <dlfcn.h>
18 #include <errno.h>
19 #include <stdlib.h>
20
21 /* If SEQUOIA_SONAME is defined, dlopen handle can be automatically
22 * set; otherwise, the caller needs to call
23 * go_sequoia_ensure_library with soname determined at run time.
24 */
25 #ifdef SEQUOIA_SONAME
26
27 static void
28 ensure_library (void)
29 {
30 if (go_sequoia_ensure_library (SEQUOIA_SONAME, RTLD_LAZY | RTLD_LOCAL) < 0)
31 abort ();
32 }
33
34 #if defined(GO_SEQUOIA_ENABLE_PTHREAD) && GO_SEQUOIA_ENABLE_PTHREAD
35 #include <pthread.h>
36
37 static pthread_once_t dlopen_once = PTHREAD_ONCE_INIT;
38
39 #define ENSURE_LIBRARY pthread_once(&dlopen_once, ensure_library)
40
41 #else /* GO_SEQUOIA_ENABLE_PTHREAD */
42
43 #define ENSURE_LIBRARY do { \
44 if (!go_sequoia_dlhandle) \
45 ensure_library(); \
46 } while (0)
47
48 #endif /* !GO_SEQUOIA_ENABLE_PTHREAD */
49
50 #else /* SEQUOIA_SONAME */
51
52 #define ENSURE_LIBRARY do {} while (0)
53
54 #endif /* !SEQUOIA_SONAME */
55
56 static void *go_sequoia_dlhandle;
57
58 /* Define redirection symbols */
59 #pragma GCC diagnostic push
60 #pragma GCC diagnostic ignored "-Wunused-macros"
61
62 #if (2 <= __GNUC__ || (4 <= __clang_major__))
63 #define FUNC(ret, name, args, cargs) \
64 static __typeof__(name)(*go_sequoia_sym_##name);
65 #else
66 #define FUNC(ret, name, args, cargs) \
67 static ret(*go_sequoia_sym_##name)args;
68 #endif
69 #define VOID_FUNC FUNC
70 #include "gosequoiafuncs.h"
71 #undef VOID_FUNC
72 #undef FUNC
73
74 #pragma GCC diagnostic pop
75
76 /* Define redirection wrapper functions */
77 #pragma GCC diagnostic push
78 #pragma GCC diagnostic ignored "-Wunused-macros"
79
80 #define FUNC(ret, name, args, cargs) \
81 ret go_##name args \
82 { \
83 ENSURE_LIBRARY; \
84 assert (go_sequoia_sym_##name); \
85 return go_sequoia_sym_##name cargs; \
86 }
87 #define VOID_FUNC(ret, name, args, cargs) \
88 ret go_##name args \
89 { \
90 ENSURE_LIBRARY; \
91 assert (go_sequoia_sym_##name); \
92 go_sequoia_sym_##name cargs; \
93 }
94 #include "gosequoiafuncs.h"
95 #undef VOID_FUNC
96 #undef FUNC
97
98 #pragma GCC diagnostic pop
99
100 static int
101 ensure_symbol (const char *name, void **symp)
102 {
103 if (!*symp)
104 {
105 void *sym = dlsym (go_sequoia_dlhandle, name);
106 if (!sym)
107 return -EINVAL;
108 *symp = sym;
109 }
110 return 0;
111 }
112
113 int
114 go_sequoia_ensure_library (const char *soname, int flags)
115 {
116 int err;
117
118 if (!go_sequoia_dlhandle)
119 {
120 go_sequoia_dlhandle = dlopen (soname, flags);
121 if (!go_sequoia_dlhandle)
122 return -EINVAL;
123 }
124
125 #define ENSURE_SYMBOL(name) \
126 ensure_symbol(#name, (void **)&go_sequoia_sym_##name)
127
128 #pragma GCC diagnostic push
129 #pragma GCC diagnostic ignored "-Wunused-macros"
130
131 #define FUNC(ret, name, args, cargs) \
132 err = ENSURE_SYMBOL(name); \
133 if (err < 0) \
134 { \
135 dlclose (go_sequoia_dlhandle); \
136 go_sequoia_dlhandle = NULL; \
137 return err; \
138 }
139 #define VOID_FUNC FUNC
140 #include "gosequoiafuncs.h"
141 #undef VOID_FUNC
142 #undef FUNC
143
144 #pragma GCC diagnostic pop
145
146 #undef ENSURE_SYMBOL
147 return 0;
148 }
149
150 void
151 go_sequoia_unload_library (void)
152 {
153 if (go_sequoia_dlhandle)
154 {
155 dlclose (go_sequoia_dlhandle);
156 go_sequoia_dlhandle = NULL;
157 }
158
159 #pragma GCC diagnostic push
160 #pragma GCC diagnostic ignored "-Wunused-macros"
161
162 #define FUNC(ret, name, args, cargs) \
163 go_sequoia_sym_##name = NULL;
164 #define VOID_FUNC FUNC
165 #include "gosequoiafuncs.h"
166 #undef VOID_FUNC
167 #undef FUNC
168
169 #pragma GCC diagnostic pop
170 }
171
172 unsigned
173 go_sequoia_is_usable (void)
174 {
175 return go_sequoia_dlhandle != NULL;
176 }
177
178 #else /* GO_SEQUOIA_ENABLE_DLOPEN */
179
180 int
181 go_sequoia_ensure_library (const char *soname, int flags)
182 {
183 (void) soname;
184 (void) flags;
185 return 0;
186 }
187
188 void
189 go_sequoia_unload_library (void)
190 {
191 }
192
193 unsigned
194 go_sequoia_is_usable (void)
195 {
196 /* The library is linked at build time, thus always usable */
197 return 1;
198 }
199
200 #endif /* !GO_SEQUOIA_ENABLE_DLOPEN */