xref: /aoo42x/main/sal/osl/unx/module.c (revision 8f88de38)
1647f063dSAndrew Rist /**************************************************************
2*8f88de38Smseidel  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*8f88de38Smseidel  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*8f88de38Smseidel  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19*8f88de38Smseidel  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <sal/types.h>
25cdf0e10cSrcweir #include <osl/diagnose.h>
26cdf0e10cSrcweir #include <osl/module.h>
27cdf0e10cSrcweir #include <osl/thread.h>
28cdf0e10cSrcweir #include <osl/process.h>
29cdf0e10cSrcweir #include <osl/file.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "system.h"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
34cdf0e10cSrcweir #include <stdio.h>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /* implemented in file.c */
38cdf0e10cSrcweir extern int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /*****************************************************************************/
41cdf0e10cSrcweir /* osl_loadModule */
42cdf0e10cSrcweir /*****************************************************************************/
43cdf0e10cSrcweir 
osl_loadModule(rtl_uString * ustrModuleName,sal_Int32 nRtldMode)44cdf0e10cSrcweir oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
45cdf0e10cSrcweir {
46*8f88de38Smseidel 	oslModule pModule=0;
47*8f88de38Smseidel 	rtl_uString* ustrTmp = NULL;
48cdf0e10cSrcweir 
49*8f88de38Smseidel 	OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
50cdf0e10cSrcweir 
51*8f88de38Smseidel 	/* ensure ustrTmp holds valid string */
52*8f88de38Smseidel 	if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
53*8f88de38Smseidel 		rtl_uString_assign(&ustrTmp, ustrModuleName);
54cdf0e10cSrcweir 
55*8f88de38Smseidel 	if (ustrTmp)
56*8f88de38Smseidel 	{
57*8f88de38Smseidel 		char buffer[PATH_MAX];
58cdf0e10cSrcweir 
59*8f88de38Smseidel 		if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
60*8f88de38Smseidel 			pModule = osl_loadAsciiModule(buffer, nRtldMode);
61*8f88de38Smseidel 		rtl_uString_release(ustrTmp);
62*8f88de38Smseidel 	}
63cdf0e10cSrcweir 
64*8f88de38Smseidel 	return pModule;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir /*****************************************************************************/
68ca2659a9SHerbert Dürr /* osl_loadAsciiModule */
69cdf0e10cSrcweir /*****************************************************************************/
70cdf0e10cSrcweir 
osl_loadAsciiModule(const sal_Char * pszModuleName,sal_Int32 nRtldMode)71ca2659a9SHerbert Dürr oslModule SAL_CALL osl_loadAsciiModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode)
72cdf0e10cSrcweir {
73*8f88de38Smseidel 	OSL_ASSERT(
74*8f88de38Smseidel 		(nRtldMode & SAL_LOADMODULE_LAZY) == 0 ||
75*8f88de38Smseidel 		(nRtldMode & SAL_LOADMODULE_NOW) == 0); /* only either LAZY or NOW */
76cdf0e10cSrcweir 	if (pszModuleName)
77cdf0e10cSrcweir 	{
78cdf0e10cSrcweir #ifndef NO_DL_FUNCTIONS
79*8f88de38Smseidel 		int rtld_mode =
80*8f88de38Smseidel 			((nRtldMode & SAL_LOADMODULE_NOW) ? RTLD_NOW : RTLD_LAZY) |
81*8f88de38Smseidel 			((nRtldMode & SAL_LOADMODULE_GLOBAL) ? RTLD_GLOBAL : RTLD_LOCAL);
82cdf0e10cSrcweir 		void* pLib = dlopen(pszModuleName, rtld_mode);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
85509a48ffSpfg 		if (pLib == NULL)
8613e77a6aSHerbert Dürr 			OSL_TRACE("error: osl_loadModule failed with %s\n", dlerror());
87cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 		return ((oslModule)(pLib));
90cdf0e10cSrcweir 
91*8f88de38Smseidel #else /* NO_DL_FUNCTIONS */
92cdf0e10cSrcweir 		printf("No DL Functions\n");
93*8f88de38Smseidel #endif /* NO_DL_FUNCTIONS */
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir 	return NULL;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir /*****************************************************************************/
99cdf0e10cSrcweir /* osl_getModuleHandle */
100cdf0e10cSrcweir /*****************************************************************************/
101cdf0e10cSrcweir 
102*8f88de38Smseidel sal_Bool SAL_CALL
osl_getModuleHandle(rtl_uString * pModuleName,oslModule * pResult)103cdf0e10cSrcweir osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
104cdf0e10cSrcweir {
105*8f88de38Smseidel 	(void) pModuleName; /* avoid warning about unused parameter */
106*8f88de38Smseidel 	*pResult = (oslModule) RTLD_DEFAULT;
107*8f88de38Smseidel 	return sal_True;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir /*****************************************************************************/
111cdf0e10cSrcweir /* osl_unloadModule */
112cdf0e10cSrcweir /*****************************************************************************/
osl_unloadModule(oslModule hModule)113cdf0e10cSrcweir void SAL_CALL osl_unloadModule(oslModule hModule)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	if (hModule)
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir #ifndef NO_DL_FUNCTIONS
118*8f88de38Smseidel 		int nRet = dlclose(hModule);
119*8f88de38Smseidel 
120cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
121*8f88de38Smseidel 		if (nRet != 0)
122*8f88de38Smseidel 		{
12313e77a6aSHerbert Dürr 			fprintf(stderr, "error: osl_unloadModule failed with %s\n", dlerror());
124*8f88de38Smseidel 		}
125cdf0e10cSrcweir #else
126*8f88de38Smseidel 		(void) nRet;
127cdf0e10cSrcweir #endif /* if OSL_DEBUG_LEVEL */
128cdf0e10cSrcweir 
129cdf0e10cSrcweir #endif /* ifndef NO_DL_FUNCTIONS */
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir /*****************************************************************************/
134cdf0e10cSrcweir /* osl_getSymbol */
135cdf0e10cSrcweir /*****************************************************************************/
136*8f88de38Smseidel void* SAL_CALL
osl_getSymbol(oslModule Module,rtl_uString * pSymbolName)137cdf0e10cSrcweir osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
138cdf0e10cSrcweir {
139*8f88de38Smseidel 	return (void *) osl_getFunctionSymbol(Module, pSymbolName);
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir /*****************************************************************************/
144cdf0e10cSrcweir /* osl_getAsciiFunctionSymbol */
145cdf0e10cSrcweir /*****************************************************************************/
146*8f88de38Smseidel oslGenericFunction SAL_CALL
osl_getAsciiFunctionSymbol(oslModule Module,const sal_Char * pSymbol)147cdf0e10cSrcweir osl_getAsciiFunctionSymbol(oslModule Module, const sal_Char *pSymbol)
148cdf0e10cSrcweir {
149*8f88de38Smseidel 	void *fcnAddr = NULL;
150*8f88de38Smseidel 
151cdf0e10cSrcweir #ifndef NO_DL_FUNCTIONS
152*8f88de38Smseidel 	if (pSymbol)
153cdf0e10cSrcweir 	{
154*8f88de38Smseidel 		fcnAddr = dlsym(Module, pSymbol);
155*8f88de38Smseidel 
156*8f88de38Smseidel 		if (!fcnAddr)
157*8f88de38Smseidel 			OSL_TRACE("error: osl_getAsciiFunctionSymbol failed with %s\n", dlerror());
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir #endif
160cdf0e10cSrcweir 
161*8f88de38Smseidel 	return (oslGenericFunction) fcnAddr;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir /*****************************************************************************/
165cdf0e10cSrcweir /* osl_getFunctionSymbol */
166cdf0e10cSrcweir /*****************************************************************************/
167*8f88de38Smseidel oslGenericFunction SAL_CALL
osl_getFunctionSymbol(oslModule module,rtl_uString * puFunctionSymbolName)168cdf0e10cSrcweir osl_getFunctionSymbol(oslModule module, rtl_uString *puFunctionSymbolName)
169cdf0e10cSrcweir {
170*8f88de38Smseidel 	oslGenericFunction pSymbol = NULL;
171*8f88de38Smseidel 
172*8f88de38Smseidel 	if( puFunctionSymbolName )
173*8f88de38Smseidel 	{
174*8f88de38Smseidel 		rtl_String* pSymbolName = NULL;
175*8f88de38Smseidel 
176*8f88de38Smseidel 		rtl_uString2String( &pSymbolName,
177*8f88de38Smseidel 			rtl_uString_getStr(puFunctionSymbolName),
178*8f88de38Smseidel 			rtl_uString_getLength(puFunctionSymbolName),
179*8f88de38Smseidel 			RTL_TEXTENCODING_UTF8,
180*8f88de38Smseidel 			OUSTRING_TO_OSTRING_CVTFLAGS );
181*8f88de38Smseidel 
182*8f88de38Smseidel 		if( pSymbolName != NULL )
183*8f88de38Smseidel 		{
184*8f88de38Smseidel 			pSymbol = osl_getAsciiFunctionSymbol(module, rtl_string_getStr(pSymbolName));
185*8f88de38Smseidel 			rtl_string_release(pSymbolName);
186*8f88de38Smseidel 		}
187*8f88de38Smseidel 	}
188*8f88de38Smseidel 
189*8f88de38Smseidel 	return pSymbol;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir /*****************************************************************************/
193cdf0e10cSrcweir /* osl_getModuleURLFromAddress */
194cdf0e10cSrcweir /*****************************************************************************/
osl_getModuleURLFromAddress(void * addr,rtl_uString ** ppLibraryUrl)195cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	sal_Bool result = sal_False;
198cdf0e10cSrcweir 	Dl_info dl_info;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 	if ((result = dladdr(addr, &dl_info)) != 0)
201cdf0e10cSrcweir 	{
202cdf0e10cSrcweir 		rtl_uString * workDir = NULL;
203cdf0e10cSrcweir 		osl_getProcessWorkingDir(&workDir);
204*8f88de38Smseidel 		if (workDir)
205*8f88de38Smseidel 		{
206cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
207*8f88de38Smseidel 			OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", dl_info.dli_fname);
208cdf0e10cSrcweir #endif
209*8f88de38Smseidel 			rtl_string2UString(ppLibraryUrl,
210*8f88de38Smseidel 							   dl_info.dli_fname,
211*8f88de38Smseidel 							   strlen(dl_info.dli_fname),
212*8f88de38Smseidel 							   osl_getThreadTextEncoding(),
213*8f88de38Smseidel 							   OSTRING_TO_OUSTRING_CVTFLAGS);
214*8f88de38Smseidel 
215*8f88de38Smseidel 			OSL_ASSERT(*ppLibraryUrl != NULL);
216*8f88de38Smseidel 			osl_getFileURLFromSystemPath(*ppLibraryUrl, ppLibraryUrl);
217*8f88de38Smseidel 			osl_getAbsoluteFileURL(workDir, *ppLibraryUrl, ppLibraryUrl);
218*8f88de38Smseidel 
219*8f88de38Smseidel 			rtl_uString_release(workDir);
220*8f88de38Smseidel 			result = sal_True;
221*8f88de38Smseidel 		}
222*8f88de38Smseidel 		else
223*8f88de38Smseidel 		{
224*8f88de38Smseidel 			result = sal_False;
225*8f88de38Smseidel 		}
226cdf0e10cSrcweir 	}
227cdf0e10cSrcweir 	return result;
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir /*****************************************************************************/
231cdf0e10cSrcweir /* osl_getModuleURLFromFunctionAddress */
232cdf0e10cSrcweir /*****************************************************************************/
osl_getModuleURLFromFunctionAddress(oslGenericFunction addr,rtl_uString ** ppLibraryUrl)233cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress(oslGenericFunction addr, rtl_uString ** ppLibraryUrl)
234cdf0e10cSrcweir {
235*8f88de38Smseidel 	return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl);
236cdf0e10cSrcweir }
237