update dev300-m57
[ooovba.git] / sal / osl / unx / module.c
blob5b791d3ca7aee863335ea36aa97915abb08bd718
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: module.c,v $
10 * $Revision: 1.39 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <sal/types.h>
32 #include <osl/diagnose.h>
33 #include <osl/module.h>
34 #include <osl/thread.h>
35 #include <osl/process.h>
36 #include <osl/file.h>
38 #ifdef IRIX
39 #ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
40 #define _RLD_INTERFACE_DLFCN_H_DLADDR
41 typedef struct DL_INFO {
42 const char * dli_fname;
43 void * dli_fbase;
44 const char * dli_sname;
45 void * dli_saddr;
46 int dli_version;
47 int dli_reserved1;
48 long dli_reserved[4];
49 } Dl_info;
50 #endif
51 #include <rld_interface.h>
52 #define _RLD_DLADDR 14
53 int dladdr(void *address, Dl_info *dl);
55 int dladdr(void *address, Dl_info *dl)
57 void *v;
58 v = _rld_new_interface(_RLD_DLADDR,address,dl);
60 return (int)v;
62 #endif
64 #include "system.h"
66 #if OSL_DEBUG_LEVEL > 1
67 #include <stdio.h>
68 #endif
70 /* implemented in file.c */
71 extern int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
73 /*****************************************************************************/
74 /* osl_loadModule */
75 /*****************************************************************************/
77 oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
79 oslModule pModule=0;
80 rtl_uString* ustrTmp = NULL;
82 OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
84 /* ensure ustrTmp hold valid string */
85 if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
86 rtl_uString_assign(&ustrTmp, ustrModuleName);
88 if (ustrTmp)
90 char buffer[PATH_MAX];
92 if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
93 pModule = osl_loadModuleAscii(buffer, nRtldMode);
94 rtl_uString_release(ustrTmp);
97 return pModule;
100 /*****************************************************************************/
101 /* osl_loadModuleAscii */
102 /*****************************************************************************/
104 oslModule SAL_CALL osl_loadModuleAscii(const sal_Char *pModuleName, sal_Int32 nRtldMode)
106 OSL_ASSERT(
107 (nRtldMode & SAL_LOADMODULE_LAZY) == 0 ||
108 (nRtldMode & SAL_LOADMODULE_NOW) == 0); /* only either LAZY or NOW */
109 if (pModuleName)
111 #ifndef NO_DL_FUNCTIONS
112 int rtld_mode =
113 ((nRtldMode & SAL_LOADMODULE_NOW) ? RTLD_NOW : RTLD_LAZY) |
114 ((nRtldMode & SAL_LOADMODULE_GLOBAL) ? RTLD_GLOBAL : RTLD_LOCAL);
115 void* pLib = dlopen(pModuleName, rtld_mode);
117 #if OSL_DEBUG_LEVEL > 1
118 if (pLib == 0)
119 OSL_TRACE("Error osl_loadModule: %s\n", dlerror());
120 #endif /* OSL_DEBUG_LEVEL */
122 return ((oslModule)(pLib));
124 #else /* NO_DL_FUNCTIONS */
125 printf("No DL Functions\n");
126 #endif /* NO_DL_FUNCTIONS */
128 return NULL;
131 /*****************************************************************************/
132 /* osl_getModuleHandle */
133 /*****************************************************************************/
135 sal_Bool SAL_CALL
136 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
138 (void) pModuleName; /* avoid warning about unused parameter */
139 *pResult = (oslModule) RTLD_DEFAULT;
140 return sal_True;
143 /*****************************************************************************/
144 /* osl_unloadModule */
145 /*****************************************************************************/
146 void SAL_CALL osl_unloadModule(oslModule hModule)
148 if (hModule)
150 #ifndef NO_DL_FUNCTIONS
151 int nRet = dlclose(hModule);
153 #if OSL_DEBUG_LEVEL > 1
154 if (nRet != 0)
156 fprintf(stderr, "Error osl_unloadModule: %s\n", dlerror());
158 #else
159 (void) nRet;
160 #endif /* if OSL_DEBUG_LEVEL */
162 #endif /* ifndef NO_DL_FUNCTIONS */
166 /*****************************************************************************/
167 /* osl_getSymbol */
168 /*****************************************************************************/
169 void* SAL_CALL
170 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
172 return (void *) osl_getFunctionSymbol(Module, pSymbolName);
176 /*****************************************************************************/
177 /* osl_getAsciiFunctionSymbol */
178 /*****************************************************************************/
179 oslGenericFunction SAL_CALL
180 osl_getAsciiFunctionSymbol(oslModule Module, const sal_Char *pSymbol)
182 void *fcnAddr = NULL;
184 #ifndef NO_DL_FUNCTIONS
185 if (pSymbol)
187 fcnAddr = dlsym(Module, pSymbol);
189 if (!fcnAddr)
190 OSL_TRACE("Error osl_getAsciiFunctionSymbol: %s\n", dlerror());
192 #endif
194 return (oslGenericFunction) fcnAddr;
197 /*****************************************************************************/
198 /* osl_getFunctionSymbol */
199 /*****************************************************************************/
200 oslGenericFunction SAL_CALL
201 osl_getFunctionSymbol(oslModule module, rtl_uString *puFunctionSymbolName)
203 oslGenericFunction pSymbol = NULL;
205 if( puFunctionSymbolName )
207 rtl_String* pSymbolName = NULL;
209 rtl_uString2String( &pSymbolName,
210 rtl_uString_getStr(puFunctionSymbolName),
211 rtl_uString_getLength(puFunctionSymbolName),
212 RTL_TEXTENCODING_UTF8,
213 OUSTRING_TO_OSTRING_CVTFLAGS );
215 if( pSymbolName != NULL )
217 pSymbol = osl_getAsciiFunctionSymbol(module, rtl_string_getStr(pSymbolName));
218 rtl_string_release(pSymbolName);
222 return pSymbol;
225 /*****************************************************************************/
226 /* osl_getModuleURLFromAddress */
227 /*****************************************************************************/
228 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
230 sal_Bool result = sal_False;
231 Dl_info dl_info;
233 if ((result = dladdr(addr, &dl_info)) != 0)
235 rtl_uString * workDir = NULL;
236 osl_getProcessWorkingDir(&workDir);
237 if (workDir)
239 #if OSL_DEBUG_LEVEL > 1
240 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", dl_info.dli_fname);
241 #endif
242 rtl_string2UString(ppLibraryUrl,
243 dl_info.dli_fname,
244 strlen(dl_info.dli_fname),
245 osl_getThreadTextEncoding(),
246 OSTRING_TO_OUSTRING_CVTFLAGS);
248 OSL_ASSERT(*ppLibraryUrl != NULL);
249 osl_getFileURLFromSystemPath(*ppLibraryUrl, ppLibraryUrl);
250 osl_getAbsoluteFileURL(workDir, *ppLibraryUrl, ppLibraryUrl);
252 rtl_uString_release(workDir);
253 result = sal_True;
255 else
257 result = sal_False;
260 return result;
263 /*****************************************************************************/
264 /* osl_getModuleURLFromFunctionAddress */
265 /*****************************************************************************/
266 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress(oslGenericFunction addr, rtl_uString ** ppLibraryUrl)
268 return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl);