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