xref: /trunk/main/sal/osl/os2/module.c (revision 647f063d49501903f1667b75f5634541fc603283)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew 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 
57cdf0e10cSrcweir oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     HMODULE hModule;
60cdf0e10cSrcweir     BYTE szErrorMessage[256];
61cdf0e10cSrcweir     APIRET rc;
62cdf0e10cSrcweir     oslModule pModule=0;
63cdf0e10cSrcweir     rtl_uString* ustrTmp = NULL;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /* ensure ustrTmp hold valid string */
68cdf0e10cSrcweir     if( osl_File_E_None != osl_getSystemPathFromFileURL( ustrModuleName, &ustrTmp ) )
69cdf0e10cSrcweir         rtl_uString_assign( &ustrTmp, ustrModuleName );
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     if( ustrTmp )
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         char buffer[PATH_MAX];
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         if( UnicodeToText( buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length ) )
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             char drive[_MAX_DRIVE], dir[_MAX_DIR];
78cdf0e10cSrcweir             char fname[_MAX_FNAME], ext[_MAX_EXT];
79cdf0e10cSrcweir             char* dot;
80cdf0e10cSrcweir             // 21/02/2006 YD dll names must be 8.3: since .uno.dll files
81cdf0e10cSrcweir             // have hardcoded names, I'm truncating names here and also in
82cdf0e10cSrcweir             // the build system
83cdf0e10cSrcweir             _splitpath (buffer, drive, dir, fname, ext);
84cdf0e10cSrcweir             if (strlen(fname)>8)
85cdf0e10cSrcweir                 fname[8] = 0;   // truncate to 8.3
86cdf0e10cSrcweir             dot = strchr( fname, '.');
87cdf0e10cSrcweir             if (dot)
88cdf0e10cSrcweir                 *dot = '\0';    // truncate on dot
89cdf0e10cSrcweir             // if drive is not specified, remove starting \ from dir name
90cdf0e10cSrcweir             // so dll is loaded from LIBPATH
91cdf0e10cSrcweir             if (drive[0] == 0 && dir[0] == '\\' && dir[1] == '\\') {
92cdf0e10cSrcweir                 while( dir[0] == '\\')
93cdf0e10cSrcweir                     strcpy( dir, dir+1);
94cdf0e10cSrcweir             }
95cdf0e10cSrcweir             _makepath( buffer, drive, dir, fname, ext);
96cdf0e10cSrcweir 
97dcc6e752SPedro Giffuni #if OSL_DEBUG_LEVEL>0
98336e7b3aSPedro Giffuni             debug_printf("osl_loadModule module %s", buffer);
99dcc6e752SPedro Giffuni #endif
100dcc6e752SPedro Giffuni             //rc = _DosLoadModule( szErrorMessage, sizeof( szErrorMessage), (PCSZ)buffer, &hModule);
101dcc6e752SPedro Giffuni         //if (rc == NO_ERROR )
102dcc6e752SPedro Giffuni             hModule = dlopen( buffer, RTLD_LOCAL);
103dcc6e752SPedro Giffuni             if (hModule != NULL )
104cdf0e10cSrcweir                 pModule = (oslModule)hModule;
105cdf0e10cSrcweir             else
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 if (rc == NO_ERROR )
108cdf0e10cSrcweir                     pModule = (oslModule)hModule;
109cdf0e10cSrcweir                 else
110cdf0e10cSrcweir                 {
111cdf0e10cSrcweir                     sal_Char szError[ PATH_MAX*2 ];
112cdf0e10cSrcweir                     sprintf( szError, "Module: %s; rc: %d;\nReason: %s;\n"
113cdf0e10cSrcweir                             "Please contact technical support and report above informations.\n\n",
114cdf0e10cSrcweir                             buffer, rc, szErrorMessage );
115cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
116cdf0e10cSrcweir                     fprintf( stderr, szError);
117cdf0e10cSrcweir #endif
118cdf0e10cSrcweir                     //OSL_TRACE(szError);
119cdf0e10cSrcweir #ifndef OSL_DEBUG_LEVEL
120cdf0e10cSrcweir                     WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
121cdf0e10cSrcweir                         szError, "Critical error: DosLoadModule failed",
122cdf0e10cSrcweir                         0, MB_ERROR | MB_OK | MB_MOVEABLE);
123cdf0e10cSrcweir #endif
124cdf0e10cSrcweir                 }
125cdf0e10cSrcweir             }
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     rtl_uString_release( ustrTmp );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     return pModule;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir /*****************************************************************************/
135cdf0e10cSrcweir /* osl_getModuleHandle */
136cdf0e10cSrcweir /*****************************************************************************/
137cdf0e10cSrcweir 
138cdf0e10cSrcweir sal_Bool SAL_CALL
139cdf0e10cSrcweir osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     HMODULE hmod;
142cdf0e10cSrcweir     APIRET  rc;
143cdf0e10cSrcweir     rc = DosQueryModuleHandle(pModuleName->buffer, &hmod);
144cdf0e10cSrcweir     if( rc == NO_ERROR)
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         *pResult = (oslModule) hmod;
147cdf0e10cSrcweir         return sal_True;
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     return sal_False;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir /*****************************************************************************/
154cdf0e10cSrcweir /* osl_unloadModule */
155cdf0e10cSrcweir /*****************************************************************************/
156cdf0e10cSrcweir void SAL_CALL osl_unloadModule(oslModule Module)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
159cdf0e10cSrcweir     if (!Module)
160cdf0e10cSrcweir        fprintf( stderr, "osl_unloadModule NULL HANDLE.\n");
161cdf0e10cSrcweir #endif
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     DosFreeModule((HMODULE)Module);
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir /*****************************************************************************/
167cdf0e10cSrcweir /* osl_getSymbol */
168cdf0e10cSrcweir /*****************************************************************************/
169cdf0e10cSrcweir void* SAL_CALL
170cdf0e10cSrcweir osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir /*****************************************************************************/
176cdf0e10cSrcweir /* osl_getFunctionSymbol */
177cdf0e10cSrcweir /*****************************************************************************/
178cdf0e10cSrcweir oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     rtl_String *symbolName = NULL;
181cdf0e10cSrcweir     oslGenericFunction address;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     OSL_ASSERT(Module);
184cdf0e10cSrcweir     OSL_ASSERT(strSymbolName);
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     rtl_uString2String(
187cdf0e10cSrcweir         &symbolName,
188cdf0e10cSrcweir         strSymbolName->buffer,
189cdf0e10cSrcweir         strSymbolName->length,
190cdf0e10cSrcweir         RTL_TEXTENCODING_UTF8,
191cdf0e10cSrcweir         OUSTRING_TO_OSTRING_CVTFLAGS
192cdf0e10cSrcweir     );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
195cdf0e10cSrcweir     rtl_string_release(symbolName);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     return address;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir /*****************************************************************************/
201cdf0e10cSrcweir /* osl_getAsciiFunctionSymbol */
202cdf0e10cSrcweir /*****************************************************************************/
203cdf0e10cSrcweir oslGenericFunction SAL_CALL
204cdf0e10cSrcweir osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     PFN  pFunction;
207cdf0e10cSrcweir     APIRET rc;
208cdf0e10cSrcweir     void* pHandle=0;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
211cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     if ( Module!= 0 && pSymbol != 0 )
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
217cdf0e10cSrcweir         if( rc == NO_ERROR )
218cdf0e10cSrcweir         {
219cdf0e10cSrcweir             pHandle = (void*)pFunction;
220cdf0e10cSrcweir         }
221cdf0e10cSrcweir         else
222cdf0e10cSrcweir         {
223cdf0e10cSrcweir             // YD try again adding the '_' prefix
224cdf0e10cSrcweir             char _pszSymbolName[255];
225cdf0e10cSrcweir             strcpy( _pszSymbolName, "_");
226cdf0e10cSrcweir             strcat( _pszSymbolName, pSymbol);
227cdf0e10cSrcweir             rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
228cdf0e10cSrcweir             if( rc == NO_ERROR )
229cdf0e10cSrcweir                 pHandle = (void*)pFunction;
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     return pHandle;
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir /*****************************************************************************/
238cdf0e10cSrcweir /* osl_getModuleURLFromAddress */
239cdf0e10cSrcweir /*****************************************************************************/
240cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
241cdf0e10cSrcweir {
242cdf0e10cSrcweir     //APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
243cdf0e10cSrcweir     //          ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
244cdf0e10cSrcweir     HMODULE hMod;
245cdf0e10cSrcweir     ULONG   ObjNum;
246cdf0e10cSrcweir     CHAR    Buff[2*_MAX_PATH];
247cdf0e10cSrcweir     ULONG   Offset;
248cdf0e10cSrcweir     APIRET  rc;
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     // get module handle (and name)
251cdf0e10cSrcweir     rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
252cdf0e10cSrcweir     if (rc)
253cdf0e10cSrcweir         return sal_False;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     // get module full path
256cdf0e10cSrcweir     rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
257cdf0e10cSrcweir     if (rc)
258cdf0e10cSrcweir         return sal_False;
259cdf0e10cSrcweir 
260cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
261cdf0e10cSrcweir     OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
262cdf0e10cSrcweir #endif
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     // convert to URL
265cdf0e10cSrcweir     rtl_uString *ustrSysPath = NULL;
266cdf0e10cSrcweir     rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
267cdf0e10cSrcweir     OSL_ASSERT(ustrSysPath != NULL);
268cdf0e10cSrcweir     osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
269cdf0e10cSrcweir     rtl_uString_release( ustrSysPath );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     return sal_True;
272cdf0e10cSrcweir }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir /*****************************************************************************/
275cdf0e10cSrcweir /* osl_getModuleURLFromFunctionAddress */
276cdf0e10cSrcweir /*****************************************************************************/
277cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir     return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir /*****************************************************************************/
283cdf0e10cSrcweir 
284