2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
16 /* Part of the hack in "dl_load" ... */
17 # define DSO_MAX_TRANSLATED_SIZE 256
19 static int dl_load(DSO *dso);
20 static int dl_unload(DSO *dso);
21 static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname);
22 static char *dl_name_converter(DSO *dso, const char *filename);
23 static char *dl_merger(DSO *dso, const char *filespec1,
24 const char *filespec2);
25 static void *dl_globallookup(const char *name);
27 static DSO_METHOD dso_meth_dl = {
28 "OpenSSL 'dl' shared library method",
40 DSO_METHOD *DSO_METHOD_openssl(void)
46 * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
47 * (shl_t) returned from shl_load(). NB: I checked on HPUX11 and shl_t is
48 * itself a pointer type so the cast is safe.
51 static int dl_load(DSO *dso)
55 * We don't do any fancy retries or anything, just take the method's (or
56 * DSO's if it has the callback set) best translation of the
57 * platform-independent filename and try once with that.
59 char *filename = DSO_convert_filename(dso, NULL);
61 if (filename == NULL) {
62 DSOerr(DSO_F_DL_LOAD, DSO_R_NO_FILENAME);
65 ptr = shl_load(filename, BIND_IMMEDIATE |
66 (dso->flags & DSO_FLAG_NO_NAME_TRANSLATION ? 0 :
69 DSOerr(DSO_F_DL_LOAD, DSO_R_LOAD_FAILED);
70 ERR_add_error_data(4, "filename(", filename, "): ", strerror(errno));
73 if (!sk_push(dso->meth_data, (char *)ptr)) {
74 DSOerr(DSO_F_DL_LOAD, DSO_R_STACK_ERROR);
78 * Success, stick the converted filename we've loaded under into the DSO
79 * (it also serves as the indicator that we are currently loaded).
81 dso->loaded_filename = filename;
85 OPENSSL_free(filename);
91 static int dl_unload(DSO *dso)
95 DSOerr(DSO_F_DL_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
98 if (sk_num(dso->meth_data) < 1)
100 /* Is this statement legal? */
101 ptr = (shl_t) sk_pop(dso->meth_data);
103 DSOerr(DSO_F_DL_UNLOAD, DSO_R_NULL_HANDLE);
105 * Should push the value back onto the stack in case of a retry.
107 sk_push(dso->meth_data, (char *)ptr);
114 static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
119 if ((dso == NULL) || (symname == NULL)) {
120 DSOerr(DSO_F_DL_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
123 if (sk_num(dso->meth_data) < 1) {
124 DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_STACK_ERROR);
127 ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
129 DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_NULL_HANDLE);
132 if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
133 DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_SYM_FAILURE);
134 ERR_add_error_data(4, "symname(", symname, "): ", strerror(errno));
137 return ((DSO_FUNC_TYPE)sym);
140 static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
144 if (!filespec1 && !filespec2) {
145 DSOerr(DSO_F_DL_MERGER, ERR_R_PASSED_NULL_PARAMETER);
149 * If the first file specification is a rooted path, it rules. same goes
150 * if the second file specification is missing.
152 if (!filespec2 || filespec1[0] == '/') {
153 merged = OPENSSL_strdup(filespec1);
154 if (merged == NULL) {
155 DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
160 * If the first file specification is missing, the second one rules.
162 else if (!filespec1) {
163 merged = OPENSSL_strdup(filespec2);
164 if (merged == NULL) {
165 DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
170 * This part isn't as trivial as it looks. It assumes that the
171 * second file specification really is a directory, and makes no
172 * checks whatsoever. Therefore, the result becomes the
173 * concatenation of filespec2 followed by a slash followed by
179 spec2len = (filespec2 ? strlen(filespec2) : 0);
180 len = spec2len + (filespec1 ? strlen(filespec1) : 0);
182 if (spec2len && filespec2[spec2len - 1] == '/') {
186 merged = OPENSSL_malloc(len + 2);
187 if (merged == NULL) {
188 DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
191 strcpy(merged, filespec2);
192 merged[spec2len] = '/';
193 strcpy(&merged[spec2len + 1], filespec1);
199 * This function is identical to the one in dso_dlfcn.c, but as it is highly
200 * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at
201 * the same time, there's no great duplicating the code. Figuring out an
202 * elegant way to share one copy of the code would be more difficult and
203 * would not leave the implementations independent.
205 static char *dl_name_converter(DSO *dso, const char *filename)
208 int len, rsize, transform;
210 len = strlen(filename);
212 transform = (strstr(filename, "/") == NULL);
214 /* We will convert this to "%s.s?" or "lib%s.s?" */
215 rsize += strlen(DSO_EXTENSION); /* The length of ".s?" */
216 if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
217 rsize += 3; /* The length of "lib" */
219 translated = OPENSSL_malloc(rsize);
220 if (translated == NULL) {
221 DSOerr(DSO_F_DL_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
225 if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
226 sprintf(translated, "lib%s%s", filename, DSO_EXTENSION);
228 sprintf(translated, "%s%s", filename, DSO_EXTENSION);
230 sprintf(translated, "%s", filename);
234 static void *dl_globallookup(const char *name)
239 return shl_findsym(&h, name, TYPE_UNDEFINED, &ret) ? NULL : ret;