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