merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / unx / module.c
blob8f8f76a8656c47b2ee557331ceae500d341d9ce3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <sal/types.h>
29 #include <osl/diagnose.h>
30 #include <osl/module.h>
31 #include <osl/thread.h>
32 #include <osl/process.h>
33 #include <osl/file.h>
35 #include "system.h"
37 #if OSL_DEBUG_LEVEL > 1
38 #include <stdio.h>
39 #endif
41 /* implemented in file.c */
42 extern int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
44 oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode);
46 /*****************************************************************************/
47 /* osl_loadModule */
48 /*****************************************************************************/
50 oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
52 oslModule pModule=0;
53 rtl_uString* ustrTmp = NULL;
55 OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
57 /* ensure ustrTmp hold valid string */
58 if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
59 rtl_uString_assign(&ustrTmp, ustrModuleName);
61 if (ustrTmp)
63 char buffer[PATH_MAX];
65 if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
66 pModule = osl_psz_loadModule(buffer, nRtldMode);
67 rtl_uString_release(ustrTmp);
70 return pModule;
73 /*****************************************************************************/
74 /* osl_psz_loadModule */
75 /*****************************************************************************/
77 oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode)
79 OSL_ASSERT(
80 (nRtldMode & SAL_LOADMODULE_LAZY) == 0 ||
81 (nRtldMode & SAL_LOADMODULE_NOW) == 0); /* only either LAZY or NOW */
82 if (pszModuleName)
84 #ifndef NO_DL_FUNCTIONS
85 int rtld_mode =
86 ((nRtldMode & SAL_LOADMODULE_NOW) ? RTLD_NOW : RTLD_LAZY) |
87 ((nRtldMode & SAL_LOADMODULE_GLOBAL) ? RTLD_GLOBAL : RTLD_LOCAL);
88 void* pLib = dlopen(pszModuleName, rtld_mode);
90 #if OSL_DEBUG_LEVEL > 1
91 if (pLib == 0)
92 OSL_TRACE("Error osl_loadModule: %s\n", dlerror());
93 #endif /* OSL_DEBUG_LEVEL */
95 return ((oslModule)(pLib));
97 #else /* NO_DL_FUNCTIONS */
98 printf("No DL Functions\n");
99 #endif /* NO_DL_FUNCTIONS */
101 return NULL;
104 /*****************************************************************************/
105 /* osl_getModuleHandle */
106 /*****************************************************************************/
108 sal_Bool SAL_CALL
109 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
111 (void) pModuleName; /* avoid warning about unused parameter */
112 *pResult = (oslModule) RTLD_DEFAULT;
113 return sal_True;
116 /*****************************************************************************/
117 /* osl_unloadModule */
118 /*****************************************************************************/
119 void SAL_CALL osl_unloadModule(oslModule hModule)
121 if (hModule)
123 #ifndef NO_DL_FUNCTIONS
124 int nRet = dlclose(hModule);
126 #if OSL_DEBUG_LEVEL > 1
127 if (nRet != 0)
129 fprintf(stderr, "Error osl_unloadModule: %s\n", dlerror());
131 #else
132 (void) nRet;
133 #endif /* if OSL_DEBUG_LEVEL */
135 #endif /* ifndef NO_DL_FUNCTIONS */
139 /*****************************************************************************/
140 /* osl_getSymbol */
141 /*****************************************************************************/
142 void* SAL_CALL
143 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
145 return (void *) osl_getFunctionSymbol(Module, pSymbolName);
149 /*****************************************************************************/
150 /* osl_getAsciiFunctionSymbol */
151 /*****************************************************************************/
152 oslGenericFunction SAL_CALL
153 osl_getAsciiFunctionSymbol(oslModule Module, const sal_Char *pSymbol)
155 void *fcnAddr = NULL;
157 #ifndef NO_DL_FUNCTIONS
158 if (pSymbol)
160 fcnAddr = dlsym(Module, pSymbol);
162 if (!fcnAddr)
163 OSL_TRACE("Error osl_getAsciiFunctionSymbol: %s\n", dlerror());
165 #endif
167 return (oslGenericFunction) fcnAddr;
170 /*****************************************************************************/
171 /* osl_getFunctionSymbol */
172 /*****************************************************************************/
173 oslGenericFunction SAL_CALL
174 osl_getFunctionSymbol(oslModule module, rtl_uString *puFunctionSymbolName)
176 oslGenericFunction pSymbol = NULL;
178 if( puFunctionSymbolName )
180 rtl_String* pSymbolName = NULL;
182 rtl_uString2String( &pSymbolName,
183 rtl_uString_getStr(puFunctionSymbolName),
184 rtl_uString_getLength(puFunctionSymbolName),
185 RTL_TEXTENCODING_UTF8,
186 OUSTRING_TO_OSTRING_CVTFLAGS );
188 if( pSymbolName != NULL )
190 pSymbol = osl_getAsciiFunctionSymbol(module, rtl_string_getStr(pSymbolName));
191 rtl_string_release(pSymbolName);
195 return pSymbol;
198 /*****************************************************************************/
199 /* osl_getModuleURLFromAddress */
200 /*****************************************************************************/
201 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
203 sal_Bool result = sal_False;
204 Dl_info dl_info;
206 if ((result = dladdr(addr, &dl_info)) != 0)
208 rtl_uString * workDir = NULL;
209 osl_getProcessWorkingDir(&workDir);
210 if (workDir)
212 #if OSL_DEBUG_LEVEL > 1
213 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", dl_info.dli_fname);
214 #endif
215 rtl_string2UString(ppLibraryUrl,
216 dl_info.dli_fname,
217 strlen(dl_info.dli_fname),
218 osl_getThreadTextEncoding(),
219 OSTRING_TO_OUSTRING_CVTFLAGS);
221 OSL_ASSERT(*ppLibraryUrl != NULL);
222 osl_getFileURLFromSystemPath(*ppLibraryUrl, ppLibraryUrl);
223 osl_getAbsoluteFileURL(workDir, *ppLibraryUrl, ppLibraryUrl);
225 rtl_uString_release(workDir);
226 result = sal_True;
228 else
230 result = sal_False;
233 return result;
236 /*****************************************************************************/
237 /* osl_getModuleURLFromFunctionAddress */
238 /*****************************************************************************/
239 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress(oslGenericFunction addr, rtl_uString ** ppLibraryUrl)
241 return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl);