xref: /aoo41x/main/sal/osl/unx/module.c (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
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>
34 
35 #include "system.h"
36 
37 #if OSL_DEBUG_LEVEL > 1
38 #include <stdio.h>
39 #endif
40 
41 /* implemented in file.c */
42 extern int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
43 
44 oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode);
45 
46 /*****************************************************************************/
47 /* osl_loadModule */
48 /*****************************************************************************/
49 
50 oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
51 {
52     oslModule pModule=0;
53     rtl_uString* ustrTmp = NULL;
54 
55     OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
56 
57     /* ensure ustrTmp hold valid string */
58     if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
59         rtl_uString_assign(&ustrTmp, ustrModuleName);
60 
61     if (ustrTmp)
62     {
63         char buffer[PATH_MAX];
64 
65         if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
66             pModule = osl_psz_loadModule(buffer, nRtldMode);
67         rtl_uString_release(ustrTmp);
68     }
69 
70     return pModule;
71 }
72 
73 /*****************************************************************************/
74 /* osl_psz_loadModule */
75 /*****************************************************************************/
76 
77 oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode)
78 {
79     OSL_ASSERT(
80         (nRtldMode & SAL_LOADMODULE_LAZY) == 0 ||
81         (nRtldMode & SAL_LOADMODULE_NOW) == 0); /* only either LAZY or NOW */
82 	if (pszModuleName)
83 	{
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);
89 
90 #if OSL_DEBUG_LEVEL > 1
91 		if (pLib == 0)
92 			OSL_TRACE("Error osl_loadModule: %s\n", dlerror());
93 #endif /* OSL_DEBUG_LEVEL */
94 
95 		return ((oslModule)(pLib));
96 
97 #else   /* NO_DL_FUNCTIONS */
98 		printf("No DL Functions\n");
99 #endif  /* NO_DL_FUNCTIONS */
100 	}
101 	return NULL;
102 }
103 
104 /*****************************************************************************/
105 /* osl_getModuleHandle */
106 /*****************************************************************************/
107 
108 sal_Bool SAL_CALL
109 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
110 {
111     (void) pModuleName; /* avoid warning about unused parameter */
112     *pResult = (oslModule) RTLD_DEFAULT;
113     return sal_True;
114 }
115 
116 /*****************************************************************************/
117 /* osl_unloadModule */
118 /*****************************************************************************/
119 void SAL_CALL osl_unloadModule(oslModule hModule)
120 {
121 	if (hModule)
122 	{
123 #ifndef NO_DL_FUNCTIONS
124         int nRet = dlclose(hModule);
125 
126 #if OSL_DEBUG_LEVEL > 1
127         if (nRet != 0)
128         {
129 			fprintf(stderr, "Error osl_unloadModule: %s\n", dlerror());
130         }
131 #else
132         (void) nRet;
133 #endif /* if OSL_DEBUG_LEVEL */
134 
135 #endif /* ifndef NO_DL_FUNCTIONS */
136 	}
137 }
138 
139 /*****************************************************************************/
140 /* osl_getSymbol */
141 /*****************************************************************************/
142 void* SAL_CALL
143 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
144 {
145     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
146 }
147 
148 
149 /*****************************************************************************/
150 /* osl_getAsciiFunctionSymbol */
151 /*****************************************************************************/
152 oslGenericFunction SAL_CALL
153 osl_getAsciiFunctionSymbol(oslModule Module, const sal_Char *pSymbol)
154 {
155     void *fcnAddr = NULL;
156 
157 #ifndef NO_DL_FUNCTIONS
158     if (pSymbol)
159 	{
160         fcnAddr = dlsym(Module, pSymbol);
161 
162         if (!fcnAddr)
163             OSL_TRACE("Error osl_getAsciiFunctionSymbol: %s\n", dlerror());
164 	}
165 #endif
166 
167     return (oslGenericFunction) fcnAddr;
168 }
169 
170 /*****************************************************************************/
171 /* osl_getFunctionSymbol */
172 /*****************************************************************************/
173 oslGenericFunction SAL_CALL
174 osl_getFunctionSymbol(oslModule module, rtl_uString *puFunctionSymbolName)
175 {
176     oslGenericFunction pSymbol = NULL;
177 
178     if( puFunctionSymbolName )
179     {
180         rtl_String* pSymbolName = NULL;
181 
182         rtl_uString2String( &pSymbolName,
183             rtl_uString_getStr(puFunctionSymbolName),
184             rtl_uString_getLength(puFunctionSymbolName),
185             RTL_TEXTENCODING_UTF8,
186             OUSTRING_TO_OSTRING_CVTFLAGS );
187 
188         if( pSymbolName != NULL )
189         {
190             pSymbol = osl_getAsciiFunctionSymbol(module, rtl_string_getStr(pSymbolName));
191             rtl_string_release(pSymbolName);
192         }
193     }
194 
195     return pSymbol;
196 }
197 
198 /*****************************************************************************/
199 /* osl_getModuleURLFromAddress */
200 /*****************************************************************************/
201 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
202 {
203 	sal_Bool result = sal_False;
204 	Dl_info dl_info;
205 
206 	if ((result = dladdr(addr, &dl_info)) != 0)
207 	{
208 		rtl_uString * workDir = NULL;
209 		osl_getProcessWorkingDir(&workDir);
210         if (workDir)
211         {
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);
220 
221             OSL_ASSERT(*ppLibraryUrl != NULL);
222             osl_getFileURLFromSystemPath(*ppLibraryUrl, ppLibraryUrl);
223             osl_getAbsoluteFileURL(workDir, *ppLibraryUrl, ppLibraryUrl);
224 
225             rtl_uString_release(workDir);
226             result = sal_True;
227         }
228         else
229         {
230             result = sal_False;
231         }
232 	}
233 	return result;
234 }
235 
236 /*****************************************************************************/
237 /* osl_getModuleURLFromFunctionAddress */
238 /*****************************************************************************/
239 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress(oslGenericFunction addr, rtl_uString ** ppLibraryUrl)
240 {
241     return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl);
242 }
243