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 int dl_pathbyaddr(void *addr, char *path, int sz);
26 static void *dl_globallookup(const char *name);
28 static DSO_METHOD dso_meth_dl = {
29 "OpenSSL 'dl' shared library method",
42 DSO_METHOD *DSO_METHOD_openssl(void)
48 * For this DSO_METHOD, our meth_data STACK will contain; (i) the handle
49 * (shl_t) returned from shl_load(). NB: I checked on HPUX11 and shl_t is
50 * itself a pointer type so the cast is safe.
53 static int dl_load(DSO *dso)
57 * We don't do any fancy retries or anything, just take the method's (or
58 * DSO's if it has the callback set) best translation of the
59 * platform-independent filename and try once with that.
61 char *filename = DSO_convert_filename(dso, NULL);
63 if (filename == NULL) {
64 DSOerr(DSO_F_DL_LOAD, DSO_R_NO_FILENAME);
67 ptr = shl_load(filename, BIND_IMMEDIATE |
68 (dso->flags & DSO_FLAG_NO_NAME_TRANSLATION ? 0 :
72 DSOerr(DSO_F_DL_LOAD, DSO_R_LOAD_FAILED);
73 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
74 ERR_add_error_data(4, "filename(", filename, "): ", errbuf);
77 if (!sk_push(dso->meth_data, (char *)ptr)) {
78 DSOerr(DSO_F_DL_LOAD, DSO_R_STACK_ERROR);
82 * Success, stick the converted filename we've loaded under into the DSO
83 * (it also serves as the indicator that we are currently loaded).
85 dso->loaded_filename = filename;
89 OPENSSL_free(filename);
95 static int dl_unload(DSO *dso)
99 DSOerr(DSO_F_DL_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
102 if (sk_num(dso->meth_data) < 1)
104 /* Is this statement legal? */
105 ptr = (shl_t) sk_pop(dso->meth_data);
107 DSOerr(DSO_F_DL_UNLOAD, DSO_R_NULL_HANDLE);
109 * Should push the value back onto the stack in case of a retry.
111 sk_push(dso->meth_data, (char *)ptr);
118 static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname)
123 if ((dso == NULL) || (symname == NULL)) {
124 DSOerr(DSO_F_DL_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
127 if (sk_num(dso->meth_data) < 1) {
128 DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_STACK_ERROR);
131 ptr = (shl_t) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
133 DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_NULL_HANDLE);
136 if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) {
138 DSOerr(DSO_F_DL_BIND_FUNC, DSO_R_SYM_FAILURE);
139 if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
140 ERR_add_error_data(4, "symname(", symname, "): ", errbuf);
143 return (DSO_FUNC_TYPE)sym;
146 static char *dl_merger(DSO *dso, const char *filespec1, const char *filespec2)
150 if (!filespec1 && !filespec2) {
151 DSOerr(DSO_F_DL_MERGER, ERR_R_PASSED_NULL_PARAMETER);
155 * If the first file specification is a rooted path, it rules. same goes
156 * if the second file specification is missing.
158 if (!filespec2 || filespec1[0] == '/') {
159 merged = OPENSSL_strdup(filespec1);
160 if (merged == NULL) {
161 DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
166 * If the first file specification is missing, the second one rules.
168 else if (!filespec1) {
169 merged = OPENSSL_strdup(filespec2);
170 if (merged == NULL) {
171 DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
176 * This part isn't as trivial as it looks. It assumes that the
177 * second file specification really is a directory, and makes no
178 * checks whatsoever. Therefore, the result becomes the
179 * concatenation of filespec2 followed by a slash followed by
185 spec2len = (filespec2 ? strlen(filespec2) : 0);
186 len = spec2len + (filespec1 ? strlen(filespec1) : 0);
188 if (spec2len && filespec2[spec2len - 1] == '/') {
192 merged = OPENSSL_malloc(len + 2);
193 if (merged == NULL) {
194 DSOerr(DSO_F_DL_MERGER, ERR_R_MALLOC_FAILURE);
197 strcpy(merged, filespec2);
198 merged[spec2len] = '/';
199 strcpy(&merged[spec2len + 1], filespec1);
205 * This function is identical to the one in dso_dlfcn.c, but as it is highly
206 * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at
207 * the same time, there's no great duplicating the code. Figuring out an
208 * elegant way to share one copy of the code would be more difficult and
209 * would not leave the implementations independent.
211 static char *dl_name_converter(DSO *dso, const char *filename)
214 int len, rsize, transform;
216 len = strlen(filename);
218 transform = (strstr(filename, "/") == NULL);
220 /* We will convert this to "%s.s?" or "lib%s.s?" */
221 rsize += strlen(DSO_EXTENSION); /* The length of ".s?" */
222 if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
223 rsize += 3; /* The length of "lib" */
225 translated = OPENSSL_malloc(rsize);
226 if (translated == NULL) {
227 DSOerr(DSO_F_DL_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
231 if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
232 sprintf(translated, "lib%s%s", filename, DSO_EXTENSION);
234 sprintf(translated, "%s%s", filename, DSO_EXTENSION);
236 sprintf(translated, "%s", filename);
240 static int dl_pathbyaddr(void *addr, char *path, int sz)
242 struct shl_descriptor inf;
247 int (*f) (void *, char *, int);
255 for (i = -1; shl_get_r(i, &inf) == 0; i++) {
256 if (((size_t)addr >= inf.tstart && (size_t)addr < inf.tend) ||
257 ((size_t)addr >= inf.dstart && (size_t)addr < inf.dend)) {
258 len = (int)strlen(inf.filename);
263 memcpy(path, inf.filename, len);
272 static void *dl_globallookup(const char *name)
277 return shl_findsym(&h, name, TYPE_UNDEFINED, &ret) ? NULL : ret;