xref: /aoo42x/main/sal/osl/os2/module.c (revision dcc6e752)
1cdf0e10cSrcweir /*************************************************************************
2cdf0e10cSrcweir  *
3cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4cdf0e10cSrcweir  *
5cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6cdf0e10cSrcweir  *
7cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8cdf0e10cSrcweir  *
9cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10cdf0e10cSrcweir  *
11cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14cdf0e10cSrcweir  *
15cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20cdf0e10cSrcweir  *
21cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  ************************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "system.h"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <osl/module.h>
32cdf0e10cSrcweir #include <osl/diagnose.h>
33cdf0e10cSrcweir #include <osl/file.h>
34cdf0e10cSrcweir #include <osl/thread.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <stdlib.h>
37*dcc6e752SPedro Giffuni #include <dlfcn.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // static data for holding SAL dll module and full path
42cdf0e10cSrcweir static HMODULE hModSal;
43cdf0e10cSrcweir static char	szSalDir[ _MAX_PATH];
44cdf0e10cSrcweir static char	szSalDrive[ _MAX_PATH];
45cdf0e10cSrcweir 
46cdf0e10cSrcweir /*****************************************************************************/
47cdf0e10cSrcweir /* osl_loadModule */
48cdf0e10cSrcweir /*****************************************************************************/
49cdf0e10cSrcweir 
50cdf0e10cSrcweir ULONG APIENTRY _DosLoadModule (PSZ pszObject, ULONG uObjectLen, PCSZ pszModule,
51cdf0e10cSrcweir     PHMODULE phmod)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 	APIRET	rc;
54cdf0e10cSrcweir 	rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
55cdf0e10cSrcweir 	// YD 22/05/06 issue again if first call fails (why?)
56cdf0e10cSrcweir 	if (rc == ERROR_INVALID_PARAMETER)
57cdf0e10cSrcweir 		rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
58cdf0e10cSrcweir 	return rc;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     HMODULE hModule;
64cdf0e10cSrcweir     BYTE szErrorMessage[256];
65cdf0e10cSrcweir     APIRET rc;
66cdf0e10cSrcweir     oslModule pModule=0;
67cdf0e10cSrcweir     rtl_uString* ustrTmp = NULL;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /* ensure ustrTmp hold valid string */
72cdf0e10cSrcweir     if( osl_File_E_None != osl_getSystemPathFromFileURL( ustrModuleName, &ustrTmp ) )
73cdf0e10cSrcweir         rtl_uString_assign( &ustrTmp, ustrModuleName );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     if( ustrTmp )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         char buffer[PATH_MAX];
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         if( UnicodeToText( buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length ) )
80cdf0e10cSrcweir 		{
81cdf0e10cSrcweir 			char drive[_MAX_DRIVE], dir[_MAX_DIR];
82cdf0e10cSrcweir 			char fname[_MAX_FNAME], ext[_MAX_EXT];
83cdf0e10cSrcweir 			char* dot;
84cdf0e10cSrcweir 			// 21/02/2006 YD dll names must be 8.3: since .uno.dll files
85cdf0e10cSrcweir 			// have hardcoded names, I'm truncating names here and also in
86cdf0e10cSrcweir 			// the build system
87cdf0e10cSrcweir 			_splitpath (buffer, drive, dir, fname, ext);
88cdf0e10cSrcweir 			if (strlen(fname)>8)
89cdf0e10cSrcweir 				fname[8] = 0;	// truncate to 8.3
90cdf0e10cSrcweir 			dot = strchr( fname, '.');
91cdf0e10cSrcweir 			if (dot)
92cdf0e10cSrcweir 				*dot = '\0';	// truncate on dot
93cdf0e10cSrcweir 			// if drive is not specified, remove starting \ from dir name
94cdf0e10cSrcweir 			// so dll is loaded from LIBPATH
95cdf0e10cSrcweir 			if (drive[0] == 0 && dir[0] == '\\' && dir[1] == '\\') {
96cdf0e10cSrcweir 				while( dir[0] == '\\')
97cdf0e10cSrcweir 					strcpy( dir, dir+1);
98cdf0e10cSrcweir 			}
99cdf0e10cSrcweir 			_makepath( buffer, drive, dir, fname, ext);
100cdf0e10cSrcweir 
101*dcc6e752SPedro Giffuni #if OSL_DEBUG_LEVEL>0
102*dcc6e752SPedro Giffuni 			debug_printf("osl_loadModule module %s\n", buffer);
103*dcc6e752SPedro Giffuni #endif
104*dcc6e752SPedro Giffuni             //rc = _DosLoadModule( szErrorMessage, sizeof( szErrorMessage), (PCSZ)buffer, &hModule);
105*dcc6e752SPedro Giffuni 	    //if (rc == NO_ERROR )
106*dcc6e752SPedro Giffuni 			hModule = dlopen( buffer, RTLD_LOCAL);
107*dcc6e752SPedro Giffuni 			if (hModule != NULL )
108cdf0e10cSrcweir 				pModule = (oslModule)hModule;
109cdf0e10cSrcweir 			else
110cdf0e10cSrcweir 			{
111cdf0e10cSrcweir 				if (rc == NO_ERROR )
112cdf0e10cSrcweir 					pModule = (oslModule)hModule;
113cdf0e10cSrcweir 				else
114cdf0e10cSrcweir 				{
115cdf0e10cSrcweir 					sal_Char szError[ PATH_MAX*2 ];
116cdf0e10cSrcweir 					sprintf( szError, "Module: %s; rc: %d;\nReason: %s;\n"
117cdf0e10cSrcweir 							"Please contact technical support and report above informations.\n\n",
118cdf0e10cSrcweir 							buffer, rc, szErrorMessage );
119cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
120cdf0e10cSrcweir 					fprintf( stderr, szError);
121cdf0e10cSrcweir #endif
122cdf0e10cSrcweir 					//OSL_TRACE(szError);
123cdf0e10cSrcweir #ifndef 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 
133cdf0e10cSrcweir     rtl_uString_release( ustrTmp );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     return pModule;
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir /*****************************************************************************/
139cdf0e10cSrcweir /* osl_getModuleHandle */
140cdf0e10cSrcweir /*****************************************************************************/
141cdf0e10cSrcweir 
142cdf0e10cSrcweir sal_Bool SAL_CALL
143cdf0e10cSrcweir osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     HMODULE hmod;
146cdf0e10cSrcweir     APIRET  rc;
147cdf0e10cSrcweir     rc = DosQueryModuleHandle(pModuleName->buffer, &hmod);
148cdf0e10cSrcweir     if( rc == NO_ERROR)
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         *pResult = (oslModule) hmod;
151cdf0e10cSrcweir         return sal_True;
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     return sal_False;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir /*****************************************************************************/
158cdf0e10cSrcweir /* osl_unloadModule */
159cdf0e10cSrcweir /*****************************************************************************/
160cdf0e10cSrcweir void SAL_CALL osl_unloadModule(oslModule Module)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
163cdf0e10cSrcweir     if (!Module)
164cdf0e10cSrcweir        fprintf( stderr, "osl_unloadModule NULL HANDLE.\n");
165cdf0e10cSrcweir #endif
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     DosFreeModule((HMODULE)Module);
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir /*****************************************************************************/
171cdf0e10cSrcweir /* osl_getSymbol */
172cdf0e10cSrcweir /*****************************************************************************/
173cdf0e10cSrcweir void* SAL_CALL
174cdf0e10cSrcweir osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir /*****************************************************************************/
180cdf0e10cSrcweir /* osl_getFunctionSymbol */
181cdf0e10cSrcweir /*****************************************************************************/
182cdf0e10cSrcweir oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     rtl_String *symbolName = NULL;
185cdf0e10cSrcweir 	oslGenericFunction address;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	OSL_ASSERT(Module);
188cdf0e10cSrcweir 	OSL_ASSERT(strSymbolName);
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	rtl_uString2String(
191cdf0e10cSrcweir 		&symbolName,
192cdf0e10cSrcweir 		strSymbolName->buffer,
193cdf0e10cSrcweir 		strSymbolName->length,
194cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8,
195cdf0e10cSrcweir 		OUSTRING_TO_OSTRING_CVTFLAGS
196cdf0e10cSrcweir 	);
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
199cdf0e10cSrcweir 	rtl_string_release(symbolName);
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     return address;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir /*****************************************************************************/
205cdf0e10cSrcweir /* osl_getAsciiFunctionSymbol */
206cdf0e10cSrcweir /*****************************************************************************/
207cdf0e10cSrcweir oslGenericFunction SAL_CALL
208cdf0e10cSrcweir osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     PFN  pFunction;
211cdf0e10cSrcweir     APIRET rc;
212cdf0e10cSrcweir     void* pHandle=0;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
215cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     if ( Module!= 0 && pSymbol != 0 )
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 		rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
221cdf0e10cSrcweir 		if( rc == NO_ERROR )
222cdf0e10cSrcweir 		{
223cdf0e10cSrcweir 			pHandle = (void*)pFunction;
224cdf0e10cSrcweir 		}
225cdf0e10cSrcweir 		else
226cdf0e10cSrcweir 		{
227cdf0e10cSrcweir 			// YD try again adding the '_' prefix
228cdf0e10cSrcweir 			char _pszSymbolName[255];
229cdf0e10cSrcweir 			strcpy( _pszSymbolName, "_");
230cdf0e10cSrcweir 			strcat( _pszSymbolName, pSymbol);
231cdf0e10cSrcweir 			rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
232cdf0e10cSrcweir 			if( rc == NO_ERROR )
233cdf0e10cSrcweir 				pHandle = (void*)pFunction;
234cdf0e10cSrcweir 		}
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     return pHandle;
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir /*****************************************************************************/
242cdf0e10cSrcweir /* osl_getModuleURLFromAddress */
243cdf0e10cSrcweir /*****************************************************************************/
244cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	//APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
247cdf0e10cSrcweir 	//          ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
248cdf0e10cSrcweir 	HMODULE	hMod;
249cdf0e10cSrcweir 	ULONG	ObjNum;
250cdf0e10cSrcweir 	CHAR	Buff[2*_MAX_PATH];
251cdf0e10cSrcweir 	ULONG	Offset;
252cdf0e10cSrcweir 	APIRET	rc;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 	// get module handle (and name)
255cdf0e10cSrcweir 	rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
256cdf0e10cSrcweir 	if (rc)
257cdf0e10cSrcweir 		return sal_False;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	// get module full path
260cdf0e10cSrcweir 	rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
261cdf0e10cSrcweir 	if (rc)
262cdf0e10cSrcweir 		return sal_False;
263cdf0e10cSrcweir 
264cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
265cdf0e10cSrcweir 	OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
266cdf0e10cSrcweir #endif
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 	// convert to URL
269cdf0e10cSrcweir 	rtl_uString	*ustrSysPath = NULL;
270cdf0e10cSrcweir 	rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
271cdf0e10cSrcweir 	OSL_ASSERT(ustrSysPath != NULL);
272cdf0e10cSrcweir 	osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
273cdf0e10cSrcweir 	rtl_uString_release( ustrSysPath );
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	return sal_True;
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
278cdf0e10cSrcweir /*****************************************************************************/
279cdf0e10cSrcweir /* osl_getModuleURLFromFunctionAddress */
280cdf0e10cSrcweir /*****************************************************************************/
281cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
282cdf0e10cSrcweir {
283cdf0e10cSrcweir     return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
284cdf0e10cSrcweir }
285cdf0e10cSrcweir 
286cdf0e10cSrcweir /*****************************************************************************/
287cdf0e10cSrcweir 
288