xref: /aoo42x/main/sal/osl/os2/module.c (revision 7f70c6ca)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19647f063dSAndrew Rist  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include "system.h"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <osl/module.h>
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <osl/file.h>
30cdf0e10cSrcweir #include <osl/thread.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <stdlib.h>
33dcc6e752SPedro Giffuni #include <dlfcn.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // static data for holding SAL dll module and full path
38cdf0e10cSrcweir static HMODULE hModSal;
39cdf0e10cSrcweir static char	szSalDir[ _MAX_PATH];
40cdf0e10cSrcweir static char	szSalDrive[ _MAX_PATH];
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /*****************************************************************************/
43cdf0e10cSrcweir /* osl_loadModule */
44cdf0e10cSrcweir /*****************************************************************************/
45cdf0e10cSrcweir 
46cdf0e10cSrcweir ULONG APIENTRY _DosLoadModule (PSZ pszObject, ULONG uObjectLen, PCSZ pszModule,
47cdf0e10cSrcweir     PHMODULE phmod)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	APIRET	rc;
50cdf0e10cSrcweir 	rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
51cdf0e10cSrcweir 	// YD 22/05/06 issue again if first call fails (why?)
52cdf0e10cSrcweir 	if (rc == ERROR_INVALID_PARAMETER)
53cdf0e10cSrcweir 		rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
54cdf0e10cSrcweir 	return rc;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57ca2659a9SHerbert Dürr oslModule SAL_CALL osl_loadAsciiModule( const sal_Char* pModuleName, sal_Int32 nRtldMode )
58ca2659a9SHerbert Dürr {
59ca2659a9SHerbert Dürr 	rtl_uString* pUniName = NULL;
60ca2659a9SHerbert Dürr 	rtl_uString_newFromAscii( &pUniName, pModuleName );
61ca2659a9SHerbert Dürr 	oslModule aModule = osl_loadModule( pUniName, nRtldMode );
62ca2659a9SHerbert Dürr 	rtl_uString_release( pUniName );
63ca2659a9SHerbert Dürr 	return aModule;
64ca2659a9SHerbert Dürr }
65ca2659a9SHerbert Dürr 
66cdf0e10cSrcweir oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     HMODULE hModule;
69cdf0e10cSrcweir     BYTE szErrorMessage[256];
70cdf0e10cSrcweir     APIRET rc;
71cdf0e10cSrcweir     oslModule pModule=0;
72cdf0e10cSrcweir     rtl_uString* ustrTmp = NULL;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     /* ensure ustrTmp hold valid string */
77cdf0e10cSrcweir     if( osl_File_E_None != osl_getSystemPathFromFileURL( ustrModuleName, &ustrTmp ) )
78cdf0e10cSrcweir         rtl_uString_assign( &ustrTmp, ustrModuleName );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     if( ustrTmp )
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         char buffer[PATH_MAX];
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         if( UnicodeToText( buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length ) )
85cdf0e10cSrcweir 		{
86cdf0e10cSrcweir 			char drive[_MAX_DRIVE], dir[_MAX_DIR];
87cdf0e10cSrcweir 			char fname[_MAX_FNAME], ext[_MAX_EXT];
88cdf0e10cSrcweir 			char* dot;
89cdf0e10cSrcweir 			// 21/02/2006 YD dll names must be 8.3: since .uno.dll files
90cdf0e10cSrcweir 			// have hardcoded names, I'm truncating names here and also in
91cdf0e10cSrcweir 			// the build system
92cdf0e10cSrcweir 			_splitpath (buffer, drive, dir, fname, ext);
93cdf0e10cSrcweir 			if (strlen(fname)>8)
94cdf0e10cSrcweir 				fname[8] = 0;	// truncate to 8.3
95cdf0e10cSrcweir 			dot = strchr( fname, '.');
96cdf0e10cSrcweir 			if (dot)
97cdf0e10cSrcweir 				*dot = '\0';	// truncate on dot
98cdf0e10cSrcweir 			// if drive is not specified, remove starting \ from dir name
99cdf0e10cSrcweir 			// so dll is loaded from LIBPATH
100cdf0e10cSrcweir 			if (drive[0] == 0 && dir[0] == '\\' && dir[1] == '\\') {
101cdf0e10cSrcweir 				while( dir[0] == '\\')
102cdf0e10cSrcweir 					strcpy( dir, dir+1);
103cdf0e10cSrcweir 			}
104cdf0e10cSrcweir 			_makepath( buffer, drive, dir, fname, ext);
105cdf0e10cSrcweir 
106abfc9023SYuri Dario #if OSL_DEBUG_LEVEL>10
107336e7b3aSPedro Giffuni 			debug_printf("osl_loadModule module %s", buffer);
108dcc6e752SPedro Giffuni #endif
109dcc6e752SPedro Giffuni 			hModule = dlopen( buffer, RTLD_LOCAL);
110dcc6e752SPedro Giffuni 			if (hModule != NULL )
111cdf0e10cSrcweir 				pModule = (oslModule)hModule;
112cdf0e10cSrcweir 			else
113cdf0e10cSrcweir 			{
114cdf0e10cSrcweir 					sal_Char szError[ PATH_MAX*2 ];
11554f0139eSYuri Dario 					sprintf( szError, "Module: %s; errno: %d;\n"
116cdf0e10cSrcweir 							"Please contact technical support and report above informations.\n\n",
11754f0139eSYuri Dario 							buffer, errno );
118cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
119cdf0e10cSrcweir 					fprintf( stderr, szError);
120cdf0e10cSrcweir #endif
121abfc9023SYuri Dario 					debug_printf("osl_loadModule error %s", szError);
122abfc9023SYuri Dario 
123*7f70c6caSYuri Dario #if (OSL_DEBUG_LEVEL==0) || !defined(OSL_DEBUG_LEVEL)
124cdf0e10cSrcweir 					WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
125cdf0e10cSrcweir 						szError, "Critical error: DosLoadModule failed",
126cdf0e10cSrcweir 						0, MB_ERROR | MB_OK | MB_MOVEABLE);
127cdf0e10cSrcweir #endif
128cdf0e10cSrcweir 			}
129cdf0e10cSrcweir 		}
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     rtl_uString_release( ustrTmp );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     return pModule;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir /*****************************************************************************/
138cdf0e10cSrcweir /* osl_getModuleHandle */
139cdf0e10cSrcweir /*****************************************************************************/
140cdf0e10cSrcweir 
141cdf0e10cSrcweir sal_Bool SAL_CALL
142cdf0e10cSrcweir osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir     HMODULE hmod;
145cdf0e10cSrcweir     APIRET  rc;
146cdf0e10cSrcweir     rc = DosQueryModuleHandle(pModuleName->buffer, &hmod);
147cdf0e10cSrcweir     if( rc == NO_ERROR)
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         *pResult = (oslModule) hmod;
150cdf0e10cSrcweir         return sal_True;
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     return sal_False;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir /*****************************************************************************/
157cdf0e10cSrcweir /* osl_unloadModule */
158cdf0e10cSrcweir /*****************************************************************************/
159cdf0e10cSrcweir void SAL_CALL osl_unloadModule(oslModule Module)
160cdf0e10cSrcweir {
161cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
162cdf0e10cSrcweir     if (!Module)
163cdf0e10cSrcweir        fprintf( stderr, "osl_unloadModule NULL HANDLE.\n");
164cdf0e10cSrcweir #endif
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     DosFreeModule((HMODULE)Module);
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir /*****************************************************************************/
170cdf0e10cSrcweir /* osl_getSymbol */
171cdf0e10cSrcweir /*****************************************************************************/
172cdf0e10cSrcweir void* SAL_CALL
173cdf0e10cSrcweir osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
174cdf0e10cSrcweir {
175cdf0e10cSrcweir     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir /*****************************************************************************/
179cdf0e10cSrcweir /* osl_getFunctionSymbol */
180cdf0e10cSrcweir /*****************************************************************************/
181cdf0e10cSrcweir oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     rtl_String *symbolName = NULL;
184cdf0e10cSrcweir 	oslGenericFunction address;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	OSL_ASSERT(Module);
187cdf0e10cSrcweir 	OSL_ASSERT(strSymbolName);
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 	rtl_uString2String(
190cdf0e10cSrcweir 		&symbolName,
191cdf0e10cSrcweir 		strSymbolName->buffer,
192cdf0e10cSrcweir 		strSymbolName->length,
193cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8,
194cdf0e10cSrcweir 		OUSTRING_TO_OSTRING_CVTFLAGS
195cdf0e10cSrcweir 	);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
198cdf0e10cSrcweir 	rtl_string_release(symbolName);
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     return address;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir /*****************************************************************************/
204cdf0e10cSrcweir /* osl_getAsciiFunctionSymbol */
205cdf0e10cSrcweir /*****************************************************************************/
206cdf0e10cSrcweir oslGenericFunction SAL_CALL
207cdf0e10cSrcweir osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir     PFN  pFunction;
210cdf0e10cSrcweir     APIRET rc;
211cdf0e10cSrcweir     void* pHandle=0;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
214cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     if ( Module!= 0 && pSymbol != 0 )
217cdf0e10cSrcweir     {
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 		rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
220cdf0e10cSrcweir 		if( rc == NO_ERROR )
221cdf0e10cSrcweir 		{
222cdf0e10cSrcweir 			pHandle = (void*)pFunction;
223cdf0e10cSrcweir 		}
224cdf0e10cSrcweir 		else
225cdf0e10cSrcweir 		{
226cdf0e10cSrcweir 			// YD try again adding the '_' prefix
227cdf0e10cSrcweir 			char _pszSymbolName[255];
228cdf0e10cSrcweir 			strcpy( _pszSymbolName, "_");
229cdf0e10cSrcweir 			strcat( _pszSymbolName, pSymbol);
230cdf0e10cSrcweir 			rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
231cdf0e10cSrcweir 			if( rc == NO_ERROR )
232cdf0e10cSrcweir 				pHandle = (void*)pFunction;
233cdf0e10cSrcweir 		}
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     return pHandle;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir /*****************************************************************************/
241cdf0e10cSrcweir /* osl_getModuleURLFromAddress */
242cdf0e10cSrcweir /*****************************************************************************/
243cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
244cdf0e10cSrcweir {
245cdf0e10cSrcweir 	//APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
246cdf0e10cSrcweir 	//          ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
247cdf0e10cSrcweir 	HMODULE	hMod;
248cdf0e10cSrcweir 	ULONG	ObjNum;
249cdf0e10cSrcweir 	CHAR	Buff[2*_MAX_PATH];
250cdf0e10cSrcweir 	ULONG	Offset;
251cdf0e10cSrcweir 	APIRET	rc;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 	// get module handle (and name)
254cdf0e10cSrcweir 	rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
255cdf0e10cSrcweir 	if (rc)
256cdf0e10cSrcweir 		return sal_False;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	// get module full path
259cdf0e10cSrcweir 	rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
260cdf0e10cSrcweir 	if (rc)
261cdf0e10cSrcweir 		return sal_False;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
264cdf0e10cSrcweir 	OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
265cdf0e10cSrcweir #endif
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	// convert to URL
268cdf0e10cSrcweir 	rtl_uString	*ustrSysPath = NULL;
269cdf0e10cSrcweir 	rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
270cdf0e10cSrcweir 	OSL_ASSERT(ustrSysPath != NULL);
271cdf0e10cSrcweir 	osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
272cdf0e10cSrcweir 	rtl_uString_release( ustrSysPath );
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 	return sal_True;
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir /*****************************************************************************/
278cdf0e10cSrcweir /* osl_getModuleURLFromFunctionAddress */
279cdf0e10cSrcweir /*****************************************************************************/
280cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
281cdf0e10cSrcweir {
282cdf0e10cSrcweir     return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir /*****************************************************************************/
286cdf0e10cSrcweir 
287