xref: /aoo42x/main/sal/osl/unx/module.c (revision 509a48ff)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
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
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
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.
19647f063dSAndrew Rist  *
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 
44cdf0e10cSrcweir oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     oslModule pModule=0;
47cdf0e10cSrcweir     rtl_uString* ustrTmp = NULL;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     /* ensure ustrTmp hold valid string */
52cdf0e10cSrcweir     if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
53cdf0e10cSrcweir         rtl_uString_assign(&ustrTmp, ustrModuleName);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     if (ustrTmp)
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         char buffer[PATH_MAX];
58cdf0e10cSrcweir 
59cdf0e10cSrcweir         if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
60ca2659a9SHerbert Dürr             pModule = osl_loadAsciiModule(buffer, nRtldMode);
61cdf0e10cSrcweir         rtl_uString_release(ustrTmp);
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     return pModule;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir /*****************************************************************************/
68ca2659a9SHerbert Dürr /* osl_loadAsciiModule */
69cdf0e10cSrcweir /*****************************************************************************/
70cdf0e10cSrcweir 
71ca2659a9SHerbert Dürr oslModule SAL_CALL osl_loadAsciiModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     OSL_ASSERT(
74cdf0e10cSrcweir         (nRtldMode & SAL_LOADMODULE_LAZY) == 0 ||
75cdf0e10cSrcweir         (nRtldMode & SAL_LOADMODULE_NOW) == 0); /* only either LAZY or NOW */
76cdf0e10cSrcweir 	if (pszModuleName)
77cdf0e10cSrcweir 	{
78cdf0e10cSrcweir #ifndef NO_DL_FUNCTIONS
79cdf0e10cSrcweir         int rtld_mode =
80cdf0e10cSrcweir             ((nRtldMode & SAL_LOADMODULE_NOW) ? RTLD_NOW : RTLD_LAZY) |
81cdf0e10cSrcweir             ((nRtldMode & SAL_LOADMODULE_GLOBAL) ? RTLD_GLOBAL : RTLD_LOCAL);
82cdf0e10cSrcweir 		void* pLib = dlopen(pszModuleName, rtld_mode);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
85*509a48ffSpfg 		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 
91cdf0e10cSrcweir #else   /* NO_DL_FUNCTIONS */
92cdf0e10cSrcweir 		printf("No DL Functions\n");
93cdf0e10cSrcweir #endif  /* NO_DL_FUNCTIONS */
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir 	return NULL;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir /*****************************************************************************/
99cdf0e10cSrcweir /* osl_getModuleHandle */
100cdf0e10cSrcweir /*****************************************************************************/
101cdf0e10cSrcweir 
102cdf0e10cSrcweir sal_Bool SAL_CALL
103cdf0e10cSrcweir osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     (void) pModuleName; /* avoid warning about unused parameter */
106cdf0e10cSrcweir     *pResult = (oslModule) RTLD_DEFAULT;
107cdf0e10cSrcweir     return sal_True;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir /*****************************************************************************/
111cdf0e10cSrcweir /* osl_unloadModule */
112cdf0e10cSrcweir /*****************************************************************************/
113cdf0e10cSrcweir void SAL_CALL osl_unloadModule(oslModule hModule)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	if (hModule)
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir #ifndef NO_DL_FUNCTIONS
118cdf0e10cSrcweir         int nRet = dlclose(hModule);
119cdf0e10cSrcweir 
120cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
121cdf0e10cSrcweir         if (nRet != 0)
122cdf0e10cSrcweir         {
12313e77a6aSHerbert Dürr 			fprintf(stderr, "error: osl_unloadModule failed with %s\n", dlerror());
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir #else
126cdf0e10cSrcweir         (void) nRet;
127cdf0e10cSrcweir #endif /* if OSL_DEBUG_LEVEL */
128cdf0e10cSrcweir 
129cdf0e10cSrcweir #endif /* ifndef NO_DL_FUNCTIONS */
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir /*****************************************************************************/
134cdf0e10cSrcweir /* osl_getSymbol */
135cdf0e10cSrcweir /*****************************************************************************/
136cdf0e10cSrcweir void* SAL_CALL
137cdf0e10cSrcweir osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir /*****************************************************************************/
144cdf0e10cSrcweir /* osl_getAsciiFunctionSymbol */
145cdf0e10cSrcweir /*****************************************************************************/
146cdf0e10cSrcweir oslGenericFunction SAL_CALL
147cdf0e10cSrcweir osl_getAsciiFunctionSymbol(oslModule Module, const sal_Char *pSymbol)
148cdf0e10cSrcweir {
149cdf0e10cSrcweir     void *fcnAddr = NULL;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir #ifndef NO_DL_FUNCTIONS
152cdf0e10cSrcweir     if (pSymbol)
153cdf0e10cSrcweir 	{
154cdf0e10cSrcweir         fcnAddr = dlsym(Module, pSymbol);
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         if (!fcnAddr)
15713e77a6aSHerbert Dürr             OSL_TRACE("error: osl_getAsciiFunctionSymbol failed with %s\n", dlerror());
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir #endif
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     return (oslGenericFunction) fcnAddr;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir /*****************************************************************************/
165cdf0e10cSrcweir /* osl_getFunctionSymbol */
166cdf0e10cSrcweir /*****************************************************************************/
167cdf0e10cSrcweir oslGenericFunction SAL_CALL
168cdf0e10cSrcweir osl_getFunctionSymbol(oslModule module, rtl_uString *puFunctionSymbolName)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     oslGenericFunction pSymbol = NULL;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     if( puFunctionSymbolName )
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         rtl_String* pSymbolName = NULL;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         rtl_uString2String( &pSymbolName,
177cdf0e10cSrcweir             rtl_uString_getStr(puFunctionSymbolName),
178cdf0e10cSrcweir             rtl_uString_getLength(puFunctionSymbolName),
179cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8,
180cdf0e10cSrcweir             OUSTRING_TO_OSTRING_CVTFLAGS );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         if( pSymbolName != NULL )
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             pSymbol = osl_getAsciiFunctionSymbol(module, rtl_string_getStr(pSymbolName));
185cdf0e10cSrcweir             rtl_string_release(pSymbolName);
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     return pSymbol;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir /*****************************************************************************/
193cdf0e10cSrcweir /* osl_getModuleURLFromAddress */
194cdf0e10cSrcweir /*****************************************************************************/
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);
204cdf0e10cSrcweir         if (workDir)
205cdf0e10cSrcweir         {
206cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
207cdf0e10cSrcweir             OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", dl_info.dli_fname);
208cdf0e10cSrcweir #endif
209cdf0e10cSrcweir             rtl_string2UString(ppLibraryUrl,
210cdf0e10cSrcweir                                dl_info.dli_fname,
211cdf0e10cSrcweir                                strlen(dl_info.dli_fname),
212cdf0e10cSrcweir                                osl_getThreadTextEncoding(),
213cdf0e10cSrcweir                                OSTRING_TO_OUSTRING_CVTFLAGS);
214cdf0e10cSrcweir 
215cdf0e10cSrcweir             OSL_ASSERT(*ppLibraryUrl != NULL);
216cdf0e10cSrcweir             osl_getFileURLFromSystemPath(*ppLibraryUrl, ppLibraryUrl);
217cdf0e10cSrcweir             osl_getAbsoluteFileURL(workDir, *ppLibraryUrl, ppLibraryUrl);
218cdf0e10cSrcweir 
219cdf0e10cSrcweir             rtl_uString_release(workDir);
220cdf0e10cSrcweir             result = sal_True;
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir         else
223cdf0e10cSrcweir         {
224cdf0e10cSrcweir             result = sal_False;
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir 	}
227cdf0e10cSrcweir 	return result;
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir /*****************************************************************************/
231cdf0e10cSrcweir /* osl_getModuleURLFromFunctionAddress */
232cdf0e10cSrcweir /*****************************************************************************/
233cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress(oslGenericFunction addr, rtl_uString ** ppLibraryUrl)
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl);
236cdf0e10cSrcweir }
23713e77a6aSHerbert Dürr 
238