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