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