xref: /aoo42x/main/sal/osl/os2/module.c (revision 7f70c6ca)
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>10
107 			debug_printf("osl_loadModule module %s", buffer);
108 #endif
109 			hModule = dlopen( buffer, RTLD_LOCAL);
110 			if (hModule != NULL )
111 				pModule = (oslModule)hModule;
112 			else
113 			{
114 					sal_Char szError[ PATH_MAX*2 ];
115 					sprintf( szError, "Module: %s; errno: %d;\n"
116 							"Please contact technical support and report above informations.\n\n",
117 							buffer, errno );
118 #if OSL_DEBUG_LEVEL>0
119 					fprintf( stderr, szError);
120 #endif
121 					debug_printf("osl_loadModule error %s", szError);
122 
123 #if (OSL_DEBUG_LEVEL==0) || !defined(OSL_DEBUG_LEVEL)
124 					WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
125 						szError, "Critical error: DosLoadModule failed",
126 						0, MB_ERROR | MB_OK | MB_MOVEABLE);
127 #endif
128 			}
129 		}
130     }
131 
132     rtl_uString_release( ustrTmp );
133 
134     return pModule;
135 }
136 
137 /*****************************************************************************/
138 /* osl_getModuleHandle */
139 /*****************************************************************************/
140 
141 sal_Bool SAL_CALL
142 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
143 {
144     HMODULE hmod;
145     APIRET  rc;
146     rc = DosQueryModuleHandle(pModuleName->buffer, &hmod);
147     if( rc == NO_ERROR)
148     {
149         *pResult = (oslModule) hmod;
150         return sal_True;
151     }
152 
153     return sal_False;
154 }
155 
156 /*****************************************************************************/
157 /* osl_unloadModule */
158 /*****************************************************************************/
159 void SAL_CALL osl_unloadModule(oslModule Module)
160 {
161 #if OSL_DEBUG_LEVEL>0
162     if (!Module)
163        fprintf( stderr, "osl_unloadModule NULL HANDLE.\n");
164 #endif
165 
166     DosFreeModule((HMODULE)Module);
167 }
168 
169 /*****************************************************************************/
170 /* osl_getSymbol */
171 /*****************************************************************************/
172 void* SAL_CALL
173 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
174 {
175     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
176 }
177 
178 /*****************************************************************************/
179 /* osl_getFunctionSymbol */
180 /*****************************************************************************/
181 oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
182 {
183     rtl_String *symbolName = NULL;
184 	oslGenericFunction address;
185 
186 	OSL_ASSERT(Module);
187 	OSL_ASSERT(strSymbolName);
188 
189 	rtl_uString2String(
190 		&symbolName,
191 		strSymbolName->buffer,
192 		strSymbolName->length,
193 		RTL_TEXTENCODING_UTF8,
194 		OUSTRING_TO_OSTRING_CVTFLAGS
195 	);
196 
197 	address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
198 	rtl_string_release(symbolName);
199 
200     return address;
201 }
202 
203 /*****************************************************************************/
204 /* osl_getAsciiFunctionSymbol */
205 /*****************************************************************************/
206 oslGenericFunction SAL_CALL
207 osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
208 {
209     PFN  pFunction;
210     APIRET rc;
211     void* pHandle=0;
212 
213     OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
214     OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
215 
216     if ( Module!= 0 && pSymbol != 0 )
217     {
218 
219 		rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
220 		if( rc == NO_ERROR )
221 		{
222 			pHandle = (void*)pFunction;
223 		}
224 		else
225 		{
226 			// YD try again adding the '_' prefix
227 			char _pszSymbolName[255];
228 			strcpy( _pszSymbolName, "_");
229 			strcat( _pszSymbolName, pSymbol);
230 			rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
231 			if( rc == NO_ERROR )
232 				pHandle = (void*)pFunction;
233 		}
234 
235     }
236 
237     return pHandle;
238 }
239 
240 /*****************************************************************************/
241 /* osl_getModuleURLFromAddress */
242 /*****************************************************************************/
243 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
244 {
245 	//APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
246 	//          ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
247 	HMODULE	hMod;
248 	ULONG	ObjNum;
249 	CHAR	Buff[2*_MAX_PATH];
250 	ULONG	Offset;
251 	APIRET	rc;
252 
253 	// get module handle (and name)
254 	rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
255 	if (rc)
256 		return sal_False;
257 
258 	// get module full path
259 	rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
260 	if (rc)
261 		return sal_False;
262 
263 #if OSL_DEBUG_LEVEL > 1
264 	OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
265 #endif
266 
267 	// convert to URL
268 	rtl_uString	*ustrSysPath = NULL;
269 	rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
270 	OSL_ASSERT(ustrSysPath != NULL);
271 	osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
272 	rtl_uString_release( ustrSysPath );
273 
274 	return sal_True;
275 }
276 
277 /*****************************************************************************/
278 /* osl_getModuleURLFromFunctionAddress */
279 /*****************************************************************************/
280 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
281 {
282     return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
283 }
284 
285 /*****************************************************************************/
286 
287