1 /*
2 * %CopyrightBegin%
3 *
4 * Copyright Ericsson AB 2006-2016. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * %CopyrightEnd%
19 */
20
21 /*
22 * Interface functions to the dynamic linker using dl* functions.
23 * (As far as I know it works on SunOS 4, 5, Linux and FreeBSD. /Seb)
24 */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "sys.h"
31 #include "erl_vm.h"
32 #include "global.h"
33 #ifdef HAVE_DLFCN_H
34 #include <dlfcn.h>
35 #endif
36
37
38 /* some systems do not have RTLD_NOW defined, and require the "mode"
39 * argument to dload() always be 1.
40 */
41 #ifndef RTLD_NOW
42 # define RTLD_NOW 1
43 #endif
44
45 #define MAX_NAME_LEN 255 /* XXX should we get the system path size? */
46 #define EXT_LEN 3
47 #define FILE_EXT ".so" /* extension appended to the filename */
48
49 static char **errcodes = NULL;
50 static int num_errcodes = 0;
51 static int num_errcodes_allocated = 0;
52
53 #define my_strdup(WHAT) my_strdup_in(ERTS_ALC_T_DDLL_ERRCODES, WHAT);
54
my_strdup_in(ErtsAlcType_t type,char * what)55 static char *my_strdup_in(ErtsAlcType_t type, char *what)
56 {
57 char *res = erts_alloc(type, strlen(what) + 1);
58 strcpy(res, what);
59 return res;
60 }
61
62
find_errcode(char * string,ErtsSysDdllError * err)63 static int find_errcode(char *string, ErtsSysDdllError* err)
64 {
65 int i;
66
67 if (err != NULL) {
68 erts_sys_ddll_free_error(err); /* in case we ignored an earlier error */
69 err->str = my_strdup_in(ERTS_ALC_T_DDLL_TMP_BUF, string);
70 return 0;
71 }
72 for(i=0;i<num_errcodes;++i) {
73 if (!strcmp(string, errcodes[i])) {
74 return i;
75 }
76 }
77 if (num_errcodes_allocated == num_errcodes) {
78 errcodes = (num_errcodes_allocated == 0)
79 ? erts_alloc(ERTS_ALC_T_DDLL_ERRCODES,
80 (num_errcodes_allocated = 10) * sizeof(char *))
81 : erts_realloc(ERTS_ALC_T_DDLL_ERRCODES, errcodes,
82 (num_errcodes_allocated += 10) * sizeof(char *));
83 }
84 errcodes[num_errcodes++] = my_strdup(string);
85 return (num_errcodes - 1);
86 }
87
erl_sys_ddll_init(void)88 void erl_sys_ddll_init(void) {
89 #if defined(HAVE_DLOPEN) && defined(ERTS_NEED_DLOPEN_BEFORE_DLERROR)
90 /*
91 * dlopen() needs to be called before we make the first call to
92 * dlerror(); otherwise, dlerror() might dump core. At least
93 * some versions of linuxthread suffer from this bug.
94 */
95 void *handle = dlopen("/nonexistinglib", RTLD_NOW);
96 if (handle)
97 dlclose(handle);
98 #endif
99 return;
100 }
101
102 /*
103 * Open a shared object
104 */
erts_sys_ddll_open(const char * full_name,void ** handle,ErtsSysDdllError * err)105 int erts_sys_ddll_open(const char *full_name, void **handle, ErtsSysDdllError* err)
106 {
107 #if defined(HAVE_DLOPEN)
108 char* dlname;
109 int len = sys_strlen(full_name);
110 int ret;
111
112 dlname = erts_alloc(ERTS_ALC_T_TMP, len + EXT_LEN + 1);
113 sys_strcpy(dlname, full_name);
114 sys_strcpy(dlname+len, FILE_EXT);
115
116 ret = erts_sys_ddll_open_noext(dlname, handle, err);
117
118 erts_free(ERTS_ALC_T_TMP, (void *) dlname);
119 return ret;
120 #else
121 return ERL_DE_ERROR_NO_DDLL_FUNCTIONALITY;
122 #endif
123 }
124
erts_sys_ddll_open_noext(char * dlname,void ** handle,ErtsSysDdllError * err)125 int erts_sys_ddll_open_noext(char *dlname, void **handle, ErtsSysDdllError* err)
126 {
127 #if defined(HAVE_DLOPEN)
128 int ret = ERL_DE_NO_ERROR;
129 char *str;
130 dlerror();
131 if ((*handle = dlopen(dlname, RTLD_NOW)) == NULL) {
132 str = dlerror();
133
134 if (err == NULL) {
135 /*
136 * Remove prefix filename to avoid exploading number of
137 * error codes on extreme usage.
138 */
139 if (strstr(str,dlname) == str) {
140 char *save_str = str;
141 str += strlen(dlname);
142 while (*str == ':' || *str == ' ') {
143 ++str;
144 }
145 if (*str == '\0') { /* Better with filename than nothing... */
146 str = save_str;
147 }
148 }
149 }
150 ret = ERL_DE_DYNAMIC_ERROR_OFFSET - find_errcode(str, err);
151 }
152 return ret;
153 #else
154 return ERL_DE_ERROR_NO_DDLL_FUNCTIONALITY;
155 #endif
156 }
157
158 /*
159 * Find a symbol in the shared object
160 */
erts_sys_ddll_sym2(void * handle,const char * func_name,void ** function,ErtsSysDdllError * err)161 int erts_sys_ddll_sym2(void *handle, const char *func_name, void **function,
162 ErtsSysDdllError* err)
163 {
164 #if defined(HAVE_DLOPEN)
165 void *sym;
166 char *e;
167 int ret;
168 dlerror();
169 sym = dlsym(handle, func_name);
170 if ((e = dlerror()) != NULL) {
171 ret = ERL_DE_DYNAMIC_ERROR_OFFSET - find_errcode(e, err);
172 ASSERT(ret != ERL_DE_NO_ERROR);
173 } else {
174 *function = sym;
175 ret = ERL_DE_NO_ERROR;
176 }
177 return ret;
178 #else
179 return ERL_DE_ERROR_NO_DDLL_FUNCTIONALITY;
180 #endif
181 }
182
183 /* XXX:PaN These two will be changed with new driver interface! */
184
185 /*
186 * Load the driver init function, might appear under different names depending on object arch...
187 */
188
erts_sys_ddll_load_driver_init(void * handle,void ** function)189 int erts_sys_ddll_load_driver_init(void *handle, void **function)
190 {
191 void *fn = NULL;
192 int res;
193 if ((res = erts_sys_ddll_sym2(handle, "driver_init", &fn, NULL)) != ERL_DE_NO_ERROR) {
194 res = erts_sys_ddll_sym2(handle, "_driver_init", &fn, NULL);
195 }
196 if (res == ERL_DE_NO_ERROR) {
197 ASSERT(fn);
198 *function = fn;
199 }
200 return res;
201 }
202
erts_sys_ddll_load_nif_init(void * handle,void ** function,ErtsSysDdllError * err)203 int erts_sys_ddll_load_nif_init(void *handle, void **function, ErtsSysDdllError* err)
204 {
205 void *fn = NULL;
206 int res;
207 if ((res = erts_sys_ddll_sym2(handle, "nif_init", &fn, err)) != ERL_DE_NO_ERROR) {
208 res = erts_sys_ddll_sym2(handle, "_nif_init", &fn, err);
209 }
210 if (res == ERL_DE_NO_ERROR) {
211 ASSERT(fn);
212 *function = fn;
213 }
214 return res;
215 }
216
217 /*
218 * Call the driver_init function, whatever it's really called, simple on unix...
219 */
erts_sys_ddll_call_init(void * function)220 void *erts_sys_ddll_call_init(void *function) {
221 void *(*initfn)(void) = function;
222 return (*initfn)();
223 }
erts_sys_ddll_call_nif_init(void * function)224 void *erts_sys_ddll_call_nif_init(void *function) {
225 return erts_sys_ddll_call_init(function);
226 }
227
228
229
230 /*
231 * Close a chared object
232 */
erts_sys_ddll_close2(void * handle,ErtsSysDdllError * err)233 int erts_sys_ddll_close2(void *handle, ErtsSysDdllError* err)
234 {
235 #if defined(HAVE_DLOPEN)
236 int ret;
237 char *s;
238 dlerror();
239 if (dlclose(handle) == 0) {
240 ret = ERL_DE_NO_ERROR;
241 } else {
242 if ((s = dlerror()) == NULL) {
243 find_errcode("unspecified error", err);
244 ret = ERL_DE_ERROR_UNSPECIFIED;
245 } else {
246 ret = ERL_DE_DYNAMIC_ERROR_OFFSET - find_errcode(s, err);
247 }
248 }
249 return ret;
250 #else
251 return ERL_DE_ERROR_NO_DDLL_FUNCTIONALITY;
252 #endif
253 }
254
255
256 /*
257 * Return string that describes the (current) error
258 */
erts_sys_ddll_error(int code)259 char *erts_sys_ddll_error(int code)
260 {
261 int actual_code;
262
263 if (code > ERL_DE_DYNAMIC_ERROR_OFFSET) {
264 return "Unspecified error";
265 }
266 actual_code = -1*(code - ERL_DE_DYNAMIC_ERROR_OFFSET);
267 #if defined(HAVE_DLOPEN)
268 {
269 char *msg;
270
271 if (actual_code >= num_errcodes) {
272 msg = "Unknown dlload error";
273 } else {
274 msg = errcodes[actual_code];
275 }
276 return msg;
277 }
278 #endif
279 return "no error";
280 }
281
erts_sys_ddll_free_error(ErtsSysDdllError * err)282 void erts_sys_ddll_free_error(ErtsSysDdllError* err)
283 {
284 if (err->str != NULL) {
285 erts_free(ERTS_ALC_T_DDLL_TMP_BUF, err->str);
286 }
287 }
288
289