1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_arch_dso.h"
18 #include "apr_strings.h"
19 #include "apr_portable.h"
23 #if !defined(DSO_USE_DLFCN) && !defined(DSO_USE_SHL) && !defined(DSO_USE_DYLD)
24 #error No DSO implementation specified.
31 #include <stdlib.h> /* malloc(), free() */
34 #include <string.h> /* for strerror() on HP-UX */
37 #if defined(DSO_USE_DYLD)
38 #define DYLD_LIBRARY_HANDLE (void *)-1
41 APR_DECLARE(apr_status_t) apr_os_dso_handle_put(apr_dso_handle_t **aprdso,
42 apr_os_dso_handle_t osdso,
45 *aprdso = apr_pcalloc(pool, sizeof **aprdso);
46 (*aprdso)->handle = osdso;
47 (*aprdso)->pool = pool;
51 APR_DECLARE(apr_status_t) apr_os_dso_handle_get(apr_os_dso_handle_t *osdso,
52 apr_dso_handle_t *aprdso)
54 *osdso = aprdso->handle;
58 static apr_status_t dso_cleanup(void *thedso)
60 apr_dso_handle_t *dso = thedso;
62 if (dso->handle == NULL)
65 #if defined(DSO_USE_SHL)
66 shl_unload((shl_t)dso->handle);
67 #elif defined(DSO_USE_DYLD)
68 if (dso->handle != DYLD_LIBRARY_HANDLE) {
69 NSUnLinkModule(dso->handle, FALSE);
71 #elif defined(DSO_USE_DLFCN)
72 if (dlclose(dso->handle) != 0)
80 APR_DECLARE(apr_status_t) apr_dso_load(apr_dso_handle_t **res_handle,
81 const char *path, apr_pool_t *pool)
83 #if defined(DSO_USE_SHL)
84 shl_t os_handle = shl_load(path, BIND_IMMEDIATE, 0L);
86 #elif defined(DSO_USE_DYLD)
87 NSObjectFileImage image;
88 NSModule os_handle = NULL;
89 NSObjectFileImageReturnCode dsoerr;
90 const char* err_msg = NULL;
91 dsoerr = NSCreateObjectFileImageFromFile(path, &image);
93 if (dsoerr == NSObjectFileImageSuccess) {
94 #if defined(NSLINKMODULE_OPTION_RETURN_ON_ERROR) && defined(NSLINKMODULE_OPTION_NONE)
95 os_handle = NSLinkModule(image, path,
96 NSLINKMODULE_OPTION_RETURN_ON_ERROR |
97 NSLINKMODULE_OPTION_NONE);
98 /* If something went wrong, get the errors... */
100 NSLinkEditErrors errors;
102 const char *fileName;
103 NSLinkEditError(&errors, &errorNumber, &fileName, &err_msg);
106 os_handle = NSLinkModule(image, path, FALSE);
108 NSDestroyObjectFileImage(image);
110 else if ((dsoerr == NSObjectFileImageFormat ||
111 dsoerr == NSObjectFileImageInappropriateFile) &&
112 NSAddLibrary(path) == TRUE) {
113 os_handle = (NSModule)DYLD_LIBRARY_HANDLE;
116 err_msg = "cannot create object file image or add library";
119 #elif defined(DSO_USE_DLFCN)
120 #if defined(OSF1) || defined(SEQUENT) || defined(SNI) ||\
121 (defined(__FreeBSD_version) && (__FreeBSD_version >= 220000))
122 void *os_handle = dlopen((char *)path, RTLD_NOW | RTLD_GLOBAL);
125 int flags = RTLD_NOW | RTLD_GLOBAL;
128 if (strchr(path + 1, '(') && path[strlen(path) - 1] == ')')
130 /* This special archive.a(dso.so) syntax is required for
131 * the way libtool likes to build shared libraries on AIX.
132 * dlopen() support for such a library requires that the
133 * RTLD_MEMBER flag be enabled.
135 flags |= RTLD_MEMBER;
138 os_handle = dlopen(path, flags);
140 #endif /* DSO_USE_x */
142 *res_handle = apr_pcalloc(pool, sizeof(**res_handle));
144 if(os_handle == NULL) {
145 #if defined(DSO_USE_SHL)
146 (*res_handle)->errormsg = strerror(errno);
148 #elif defined(DSO_USE_DYLD)
149 (*res_handle)->errormsg = (err_msg) ? err_msg : "link failed";
151 #elif defined(DSO_USE_DLFCN)
152 (*res_handle)->errormsg = dlerror();
157 (*res_handle)->handle = (void*)os_handle;
158 (*res_handle)->pool = pool;
159 (*res_handle)->errormsg = NULL;
161 apr_pool_cleanup_register(pool, *res_handle, dso_cleanup, apr_pool_cleanup_null);
166 APR_DECLARE(apr_status_t) apr_dso_unload(apr_dso_handle_t *handle)
168 return apr_pool_cleanup_run(handle->pool, handle, dso_cleanup);
171 APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym,
172 apr_dso_handle_t *handle,
175 #if defined(DSO_USE_SHL)
176 void *symaddr = NULL;
180 status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_PROCEDURE, &symaddr);
181 if (status == -1 && errno == 0) /* try TYPE_DATA instead */
182 status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_DATA, &symaddr);
184 return APR_ESYMNOTFOUND;
188 #elif defined(DSO_USE_DYLD)
191 char *symname2 = (char*)malloc(sizeof(char)*(strlen(symname)+2));
192 sprintf(symname2, "_%s", symname);
193 #ifdef NSLINKMODULE_OPTION_PRIVATE
194 if (handle->handle == DYLD_LIBRARY_HANDLE) {
195 symbol = NSLookupAndBindSymbol(symname2);
198 symbol = NSLookupSymbolInModule((NSModule)handle->handle, symname2);
201 symbol = NSLookupAndBindSymbol(symname2);
204 if (symbol == NULL) {
205 handle->errormsg = "undefined symbol";
206 return APR_ESYMNOTFOUND;
208 retval = NSAddressOfSymbol(symbol);
209 if (retval == NULL) {
210 handle->errormsg = "cannot resolve symbol";
211 return APR_ESYMNOTFOUND;
215 #elif defined(DSO_USE_DLFCN)
217 #if defined(DLSYM_NEEDS_UNDERSCORE)
219 char *symbol = (char*)malloc(sizeof(char)*(strlen(symname)+2));
220 sprintf(symbol, "_%s", symname);
221 retval = dlsym(handle->handle, symbol);
223 #elif defined(SEQUENT) || defined(SNI)
224 void *retval = dlsym(handle->handle, (char *)symname);
226 void *retval = dlsym(handle->handle, symname);
227 #endif /* DLSYM_NEEDS_UNDERSCORE */
229 if (retval == NULL) {
230 handle->errormsg = dlerror();
231 return APR_ESYMNOTFOUND;
237 #endif /* DSO_USE_x */
240 APR_DECLARE(const char *) apr_dso_error(apr_dso_handle_t *dso, char *buffer,
244 apr_cpystrn(buffer, dso->errormsg, buflen);
245 return dso->errormsg;