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