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