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 } else {
173 *function = sym;
174 ret = ERL_DE_NO_ERROR;
175 }
176 return ret;
177 #else
178 return ERL_DE_ERROR_NO_DDLL_FUNCTIONALITY;
179 #endif
180 }
181
182 /* XXX:PaN These two will be changed with new driver interface! */
183
184 /*
185 * Load the driver init function, might appear under different names depending on object arch...
186 */
187
erts_sys_ddll_load_driver_init(void * handle,void ** function)188 int erts_sys_ddll_load_driver_init(void *handle, void **function)
189 {
190 void *fn;
191 int res;
192 if ((res = erts_sys_ddll_sym2(handle, "driver_init", &fn, NULL)) != ERL_DE_NO_ERROR) {
193 res = erts_sys_ddll_sym2(handle, "_driver_init", &fn, NULL);
194 }
195 if (res == ERL_DE_NO_ERROR) {
196 *function = fn;
197 }
198 return res;
199 }
200
erts_sys_ddll_load_nif_init(void * handle,void ** function,ErtsSysDdllError * err)201 int erts_sys_ddll_load_nif_init(void *handle, void **function, ErtsSysDdllError* err)
202 {
203 void *fn;
204 int res;
205 if ((res = erts_sys_ddll_sym2(handle, "nif_init", &fn, err)) != ERL_DE_NO_ERROR) {
206 res = erts_sys_ddll_sym2(handle, "_nif_init", &fn, err);
207 }
208 if (res == ERL_DE_NO_ERROR) {
209 *function = fn;
210 }
211 return res;
212 }
213
214 /*
215 * Call the driver_init function, whatever it's really called, simple on unix...
216 */
erts_sys_ddll_call_init(void * function)217 void *erts_sys_ddll_call_init(void *function) {
218 void *(*initfn)(void) = function;
219 return (*initfn)();
220 }
erts_sys_ddll_call_nif_init(void * function)221 void *erts_sys_ddll_call_nif_init(void *function) {
222 return erts_sys_ddll_call_init(function);
223 }
224
225
226
227 /*
228 * Close a chared object
229 */
erts_sys_ddll_close2(void * handle,ErtsSysDdllError * err)230 int erts_sys_ddll_close2(void *handle, ErtsSysDdllError* err)
231 {
232 #if defined(HAVE_DLOPEN)
233 int ret;
234 char *s;
235 dlerror();
236 if (dlclose(handle) == 0) {
237 ret = ERL_DE_NO_ERROR;
238 } else {
239 if ((s = dlerror()) == NULL) {
240 find_errcode("unspecified error", err);
241 ret = ERL_DE_ERROR_UNSPECIFIED;
242 } else {
243 ret = ERL_DE_DYNAMIC_ERROR_OFFSET - find_errcode(s, err);
244 }
245 }
246 return ret;
247 #else
248 return ERL_DE_ERROR_NO_DDLL_FUNCTIONALITY;
249 #endif
250 }
251
252
253 /*
254 * Return string that describes the (current) error
255 */
erts_sys_ddll_error(int code)256 char *erts_sys_ddll_error(int code)
257 {
258 int actual_code;
259
260 if (code > ERL_DE_DYNAMIC_ERROR_OFFSET) {
261 return "Unspecified error";
262 }
263 actual_code = -1*(code - ERL_DE_DYNAMIC_ERROR_OFFSET);
264 #if defined(HAVE_DLOPEN)
265 {
266 char *msg;
267
268 if (actual_code >= num_errcodes) {
269 msg = "Unknown dlload error";
270 } else {
271 msg = errcodes[actual_code];
272 }
273 return msg;
274 }
275 #endif
276 return "no error";
277 }
278
erts_sys_ddll_free_error(ErtsSysDdllError * err)279 void erts_sys_ddll_free_error(ErtsSysDdllError* err)
280 {
281 if (err->str != NULL) {
282 erts_free(ERTS_ALC_T_DDLL_TMP_BUF, err->str);
283 }
284 }
285
286