xref: /aoo41x/main/sal/osl/os2/module.c (revision e372d74d)
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 
25 #include <osl/module.h>
26 #include <osl/diagnose.h>
27 #include <osl/file.h>
28 #include <osl/thread.h>
29 
30 #include <stdlib.h>
31 #include <dlfcn.h>
32 
33 #include "system.h"
34 
35 /* implemented in file.cxx */
36 int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
37 
38 /*****************************************************************************/
39 /* osl_loadModule */
40 /*****************************************************************************/
41 
42 oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
43 {
44     oslModule pModule=0;
45     rtl_uString* ustrTmp = NULL;
46 
47     OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
48 
49     /* ensure ustrTmp hold valid string */
50     if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
51         rtl_uString_assign(&ustrTmp, ustrModuleName);
52 
53     if (ustrTmp)
54     {
55         char buffer[PATH_MAX];
56 
57         if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
58             pModule = osl_loadAsciiModule(buffer, nRtldMode);
59         rtl_uString_release(ustrTmp);
60     }
61 
62     return pModule;
63 }
64 
65 /*****************************************************************************/
66 /* osl_loadAsciiModule */
67 /*****************************************************************************/
68 
69 oslModule SAL_CALL osl_loadAsciiModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode)
70 {
71 	char drive[_MAX_DRIVE], dir[_MAX_DIR];
72 	char fname[_MAX_FNAME], ext[_MAX_EXT];
73 	char buffer[PATH_MAX];
74 	char* dot;
75 	void* hModule;
76 	oslModule pModule = NULL;
77 
78 	if (!pszModuleName)
79 		return NULL;
80 
81 	// 21/02/2006 YD dll names must be 8.3: since .uno.dll files
82 	// have hardcoded names, I'm truncating names here and also in
83 	// the build system
84 	_splitpath (pszModuleName, drive, dir, fname, ext);
85 	if (strlen(fname)>8)
86 		fname[8] = 0;	// truncate to 8.3
87 	dot = strchr( fname, '.');
88 	if (dot)
89 		*dot = '\0';	// truncate on dot
90 
91 	// if drive is not specified, remove starting \ from dir name
92 	// so dll is loaded from LIBPATH
93 	if (drive[0] == 0 && dir[0] == '\\' && dir[1] == '\\') {
94 		while( dir[0] == '\\')
95 			strcpy( dir, dir+1);
96 	}
97 	_makepath( buffer, drive, dir, fname, ext);
98 
99 #if OSL_DEBUG_LEVEL>1
100 	debug_printf("osl_loadModule module %s", buffer);
101 #endif
102 
103 	hModule = dlopen( buffer, RTLD_LOCAL);
104 	if (hModule != NULL)
105 		pModule = (oslModule)hModule;
106 	else
107 	{
108 		sal_Char szError[ PATH_MAX*2 ];
109 		sprintf( szError, "Module: %s;\n error: %s;\n\n"
110 				 "Please contact technical support and report above informations.\n\n",
111 				 buffer, dlerror() );
112 #if OSL_DEBUG_LEVEL>0
113 		debug_printf("osl_loadModule error %s", szError);
114 #endif
115 
116 #if (OSL_DEBUG_LEVEL==0) || !defined(OSL_DEBUG_LEVEL)
117 		WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
118 					  szError, "Critical error: DosLoadModule failed",
119 					  0, MB_ERROR | MB_OK | MB_MOVEABLE);
120 #endif
121 	}
122 
123 	return pModule;
124 }
125 
126 /*****************************************************************************/
127 /* osl_getModuleHandle */
128 /*****************************************************************************/
129 
130 sal_Bool SAL_CALL
131 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
132 {
133     HMODULE hmod;
134     APIRET  rc;
135 
136     OSL_ENSURE(pModuleName,"osl_loadModule : string is not valid");
137 
138     if (pModuleName)
139     {
140         char buffer[PATH_MAX];
141 
142         if (UnicodeToText(buffer, PATH_MAX, pModuleName->buffer,
143                           pModuleName->length))
144         {
145             rc = DosQueryModuleHandle(buffer, &hmod);
146             if( rc == NO_ERROR)
147             {
148                 *pResult = (oslModule) hmod;
149                 return sal_True;
150             }
151         }
152     }
153 
154     return sal_False;
155 }
156 
157 /*****************************************************************************/
158 /* osl_unloadModule */
159 /*****************************************************************************/
160 void SAL_CALL osl_unloadModule(oslModule hModule)
161 {
162 	if (hModule)
163 	{
164 		int nRet = dlclose(hModule);
165 #if OSL_DEBUG_LEVEL > 1
166         if (nRet != 0)
167         {
168             debug_printf( "osl_unloadModule failed with %s\n", dlerror());
169         }
170 #else
171         (void) nRet;
172 #endif /* if OSL_DEBUG_LEVEL */
173     }
174 }
175 
176 /*****************************************************************************/
177 /* osl_getSymbol */
178 /*****************************************************************************/
179 void* SAL_CALL
180 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
181 {
182     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
183 }
184 
185 /*****************************************************************************/
186 /* osl_getFunctionSymbol */
187 /*****************************************************************************/
188 oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
189 {
190     rtl_String *symbolName = NULL;
191 	oslGenericFunction address;
192 
193 	OSL_ASSERT(Module);
194 	OSL_ASSERT(strSymbolName);
195 
196 	rtl_uString2String(
197 		&symbolName,
198 		strSymbolName->buffer,
199 		strSymbolName->length,
200 		RTL_TEXTENCODING_UTF8,
201 		OUSTRING_TO_OSTRING_CVTFLAGS
202 	);
203 
204 	address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
205 	rtl_string_release(symbolName);
206 
207     return address;
208 }
209 
210 /*****************************************************************************/
211 /* osl_getAsciiFunctionSymbol */
212 /*****************************************************************************/
213 oslGenericFunction SAL_CALL
214 osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
215 {
216     PFN  pFunction;
217     APIRET rc;
218     void* pHandle=0;
219 
220     OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
221     OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
222 
223     if ( Module!= 0 && pSymbol != 0 )
224     {
225 
226 		rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
227 		if( rc == NO_ERROR )
228 		{
229 			pHandle = (void*)pFunction;
230 		}
231 		else
232 		{
233 			// YD try again adding the '_' prefix
234 			char _pszSymbolName[255];
235 			strcpy( _pszSymbolName, "_");
236 			strcat( _pszSymbolName, pSymbol);
237 			rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
238 			if( rc == NO_ERROR )
239 				pHandle = (void*)pFunction;
240 		}
241 
242     }
243 
244     return pHandle;
245 }
246 
247 /*****************************************************************************/
248 /* osl_getModuleURLFromAddress */
249 /*****************************************************************************/
250 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
251 {
252 	//APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
253 	//          ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
254 	HMODULE	hMod;
255 	ULONG	ObjNum;
256 	CHAR	Buff[2*_MAX_PATH];
257 	ULONG	Offset;
258 	APIRET	rc;
259 
260 	// get module handle (and name)
261 	rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
262 	if (rc)
263 		return sal_False;
264 
265 	// get module full path
266 	rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
267 	if (rc)
268 		return sal_False;
269 
270 #if OSL_DEBUG_LEVEL > 1
271 	OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
272 #endif
273 
274 	// convert to URL
275 	rtl_uString	*ustrSysPath = NULL;
276 	rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
277 	OSL_ASSERT(ustrSysPath != NULL);
278 	osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
279 	rtl_uString_release( ustrSysPath );
280 
281 	return sal_True;
282 }
283 
284 /*****************************************************************************/
285 /* osl_getModuleURLFromFunctionAddress */
286 /*****************************************************************************/
287 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
288 {
289     return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
290 }
291 
292 /*****************************************************************************/
293 
294