xref: /aoo42x/main/sal/osl/os2/module.c (revision faaf15b6)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19647f063dSAndrew Rist  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <osl/module.h>
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include <osl/file.h>
28cdf0e10cSrcweir #include <osl/thread.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <stdlib.h>
31dcc6e752SPedro Giffuni #include <dlfcn.h>
32cdf0e10cSrcweir 
33e372d74dSYuri Dario #include "system.h"
34cdf0e10cSrcweir 
35e372d74dSYuri Dario /* implemented in file.cxx */
36e372d74dSYuri Dario int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /*****************************************************************************/
39cdf0e10cSrcweir /* osl_loadModule */
40cdf0e10cSrcweir /*****************************************************************************/
41cdf0e10cSrcweir 
42cdf0e10cSrcweir oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     oslModule pModule=0;
45cdf0e10cSrcweir     rtl_uString* ustrTmp = NULL;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     /* ensure ustrTmp hold valid string */
50e372d74dSYuri Dario     if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
51e372d74dSYuri Dario         rtl_uString_assign(&ustrTmp, ustrModuleName);
52cdf0e10cSrcweir 
53e372d74dSYuri Dario     if (ustrTmp)
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         char buffer[PATH_MAX];
56cdf0e10cSrcweir 
57e372d74dSYuri Dario         if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
58e372d74dSYuri Dario             pModule = osl_loadAsciiModule(buffer, nRtldMode);
59e372d74dSYuri Dario         rtl_uString_release(ustrTmp);
60e372d74dSYuri Dario     }
61e372d74dSYuri Dario 
62e372d74dSYuri Dario     return pModule;
63e372d74dSYuri Dario }
64e372d74dSYuri Dario 
65e372d74dSYuri Dario /*****************************************************************************/
66e372d74dSYuri Dario /* osl_loadAsciiModule */
67e372d74dSYuri Dario /*****************************************************************************/
68e372d74dSYuri Dario 
69e372d74dSYuri Dario oslModule SAL_CALL osl_loadAsciiModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode)
70e372d74dSYuri Dario {
71e372d74dSYuri Dario 	char drive[_MAX_DRIVE], dir[_MAX_DIR];
72e372d74dSYuri Dario 	char fname[_MAX_FNAME], ext[_MAX_EXT];
73e372d74dSYuri Dario 	char buffer[PATH_MAX];
74e372d74dSYuri Dario 	char* dot;
75e372d74dSYuri Dario 	void* hModule;
76e372d74dSYuri Dario 
77e372d74dSYuri Dario 	if (!pszModuleName)
78e372d74dSYuri Dario 		return NULL;
79e372d74dSYuri Dario 
80e372d74dSYuri Dario 	// 21/02/2006 YD dll names must be 8.3: since .uno.dll files
81e372d74dSYuri Dario 	// have hardcoded names, I'm truncating names here and also in
82e372d74dSYuri Dario 	// the build system
83e372d74dSYuri Dario 	_splitpath (pszModuleName, drive, dir, fname, ext);
84e372d74dSYuri Dario 	if (strlen(fname)>8)
85e372d74dSYuri Dario 		fname[8] = 0;	// truncate to 8.3
86e372d74dSYuri Dario 	dot = strchr( fname, '.');
87e372d74dSYuri Dario 	if (dot)
88e372d74dSYuri Dario 		*dot = '\0';	// truncate on dot
89e372d74dSYuri Dario 
90e372d74dSYuri Dario 	// if drive is not specified, remove starting \ from dir name
91e372d74dSYuri Dario 	// so dll is loaded from LIBPATH
92e372d74dSYuri Dario 	if (drive[0] == 0 && dir[0] == '\\' && dir[1] == '\\') {
93e372d74dSYuri Dario 		while( dir[0] == '\\')
94e372d74dSYuri Dario 			strcpy( dir, dir+1);
95e372d74dSYuri Dario 	}
96e372d74dSYuri Dario 	_makepath( buffer, drive, dir, fname, ext);
97e372d74dSYuri Dario 
98e372d74dSYuri Dario #if OSL_DEBUG_LEVEL>1
99e372d74dSYuri Dario 	debug_printf("osl_loadModule module %s", buffer);
100dcc6e752SPedro Giffuni #endif
101*faaf15b6SYuri Dario 
102e372d74dSYuri Dario 	hModule = dlopen( buffer, RTLD_LOCAL);
103e372d74dSYuri Dario 	if (hModule != NULL)
104*faaf15b6SYuri Dario 		return (oslModule)hModule;
105*faaf15b6SYuri Dario 
106*faaf15b6SYuri Dario 	// do not show in case rc=2 ENOENT, we must parse dlerror
107*faaf15b6SYuri Dario 	// string to detect it
108*faaf15b6SYuri Dario 	char* err = dlerror();
109*faaf15b6SYuri Dario 	if (!err)
110*faaf15b6SYuri Dario 		return NULL;
111*faaf15b6SYuri Dario 
112*faaf15b6SYuri Dario 	if (strstr( err, "rc=2") != NULL)
113*faaf15b6SYuri Dario 		return NULL;
114*faaf15b6SYuri Dario 
115*faaf15b6SYuri Dario 	sal_Char szError[ PATH_MAX*2 ];
116*faaf15b6SYuri Dario 	sprintf( szError, "Module: %s;\n error: %s;\n\n"
117*faaf15b6SYuri Dario 			 "Please contact technical support and report above informations.\n\n",
118*faaf15b6SYuri Dario 			 buffer, err);
119cdf0e10cSrcweir #if OSL_DEBUG_LEVEL>0
120*faaf15b6SYuri Dario 	debug_printf("osl_loadModule error %s", szError);
121cdf0e10cSrcweir #endif
122*faaf15b6SYuri Dario 
1237f70c6caSYuri Dario #if (OSL_DEBUG_LEVEL==0) || !defined(OSL_DEBUG_LEVEL)
124*faaf15b6SYuri Dario 	WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
125*faaf15b6SYuri Dario 				  szError, "Critical error: DosLoadModule failed",
126*faaf15b6SYuri Dario 				  0, MB_ERROR | MB_OK | MB_MOVEABLE);
127cdf0e10cSrcweir #endif
128cdf0e10cSrcweir 
129*faaf15b6SYuri Dario 	return NULL;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir /*****************************************************************************/
133cdf0e10cSrcweir /* osl_getModuleHandle */
134cdf0e10cSrcweir /*****************************************************************************/
135cdf0e10cSrcweir 
136cdf0e10cSrcweir sal_Bool SAL_CALL
137cdf0e10cSrcweir osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     HMODULE hmod;
140cdf0e10cSrcweir     APIRET  rc;
141e372d74dSYuri Dario 
142e372d74dSYuri Dario     OSL_ENSURE(pModuleName,"osl_loadModule : string is not valid");
143e372d74dSYuri Dario 
144e372d74dSYuri Dario     if (pModuleName)
145cdf0e10cSrcweir     {
146e372d74dSYuri Dario         char buffer[PATH_MAX];
147e372d74dSYuri Dario 
148e372d74dSYuri Dario         if (UnicodeToText(buffer, PATH_MAX, pModuleName->buffer,
149e372d74dSYuri Dario                           pModuleName->length))
150e372d74dSYuri Dario         {
151e372d74dSYuri Dario             rc = DosQueryModuleHandle(buffer, &hmod);
152e372d74dSYuri Dario             if( rc == NO_ERROR)
153e372d74dSYuri Dario             {
154e372d74dSYuri Dario                 *pResult = (oslModule) hmod;
155e372d74dSYuri Dario                 return sal_True;
156e372d74dSYuri Dario             }
157e372d74dSYuri Dario         }
158cdf0e10cSrcweir     }
159e372d74dSYuri Dario 
160cdf0e10cSrcweir     return sal_False;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir /*****************************************************************************/
164cdf0e10cSrcweir /* osl_unloadModule */
165cdf0e10cSrcweir /*****************************************************************************/
166e372d74dSYuri Dario void SAL_CALL osl_unloadModule(oslModule hModule)
167cdf0e10cSrcweir {
168e372d74dSYuri Dario 	if (hModule)
169e372d74dSYuri Dario 	{
170e372d74dSYuri Dario 		int nRet = dlclose(hModule);
171e372d74dSYuri Dario #if OSL_DEBUG_LEVEL > 1
172e372d74dSYuri Dario         if (nRet != 0)
173e372d74dSYuri Dario         {
174e372d74dSYuri Dario             debug_printf( "osl_unloadModule failed with %s\n", dlerror());
175e372d74dSYuri Dario         }
176e372d74dSYuri Dario #else
177e372d74dSYuri Dario         (void) nRet;
178e372d74dSYuri Dario #endif /* if OSL_DEBUG_LEVEL */
179e372d74dSYuri Dario     }
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir /*****************************************************************************/
183cdf0e10cSrcweir /* osl_getSymbol */
184cdf0e10cSrcweir /*****************************************************************************/
185cdf0e10cSrcweir void* SAL_CALL
186cdf0e10cSrcweir osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir /*****************************************************************************/
192cdf0e10cSrcweir /* osl_getFunctionSymbol */
193cdf0e10cSrcweir /*****************************************************************************/
194cdf0e10cSrcweir oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     rtl_String *symbolName = NULL;
197cdf0e10cSrcweir 	oslGenericFunction address;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	OSL_ASSERT(Module);
200cdf0e10cSrcweir 	OSL_ASSERT(strSymbolName);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	rtl_uString2String(
203cdf0e10cSrcweir 		&symbolName,
204cdf0e10cSrcweir 		strSymbolName->buffer,
205cdf0e10cSrcweir 		strSymbolName->length,
206cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8,
207cdf0e10cSrcweir 		OUSTRING_TO_OSTRING_CVTFLAGS
208cdf0e10cSrcweir 	);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
211cdf0e10cSrcweir 	rtl_string_release(symbolName);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     return address;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir /*****************************************************************************/
217cdf0e10cSrcweir /* osl_getAsciiFunctionSymbol */
218cdf0e10cSrcweir /*****************************************************************************/
219cdf0e10cSrcweir oslGenericFunction SAL_CALL
220cdf0e10cSrcweir osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir     PFN  pFunction;
223cdf0e10cSrcweir     APIRET rc;
224cdf0e10cSrcweir     void* pHandle=0;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
227cdf0e10cSrcweir     OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     if ( Module!= 0 && pSymbol != 0 )
230cdf0e10cSrcweir     {
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 		rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
233cdf0e10cSrcweir 		if( rc == NO_ERROR )
234cdf0e10cSrcweir 		{
235cdf0e10cSrcweir 			pHandle = (void*)pFunction;
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir 		else
238cdf0e10cSrcweir 		{
239cdf0e10cSrcweir 			// YD try again adding the '_' prefix
240cdf0e10cSrcweir 			char _pszSymbolName[255];
241cdf0e10cSrcweir 			strcpy( _pszSymbolName, "_");
242cdf0e10cSrcweir 			strcat( _pszSymbolName, pSymbol);
243cdf0e10cSrcweir 			rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
244cdf0e10cSrcweir 			if( rc == NO_ERROR )
245cdf0e10cSrcweir 				pHandle = (void*)pFunction;
246cdf0e10cSrcweir 		}
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     return pHandle;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir /*****************************************************************************/
254cdf0e10cSrcweir /* osl_getModuleURLFromAddress */
255cdf0e10cSrcweir /*****************************************************************************/
256cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
257cdf0e10cSrcweir {
258cdf0e10cSrcweir 	//APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
259cdf0e10cSrcweir 	//          ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
260cdf0e10cSrcweir 	HMODULE	hMod;
261cdf0e10cSrcweir 	ULONG	ObjNum;
262cdf0e10cSrcweir 	CHAR	Buff[2*_MAX_PATH];
263cdf0e10cSrcweir 	ULONG	Offset;
264cdf0e10cSrcweir 	APIRET	rc;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 	// get module handle (and name)
267cdf0e10cSrcweir 	rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
268cdf0e10cSrcweir 	if (rc)
269cdf0e10cSrcweir 		return sal_False;
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 	// get module full path
272cdf0e10cSrcweir 	rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
273cdf0e10cSrcweir 	if (rc)
274cdf0e10cSrcweir 		return sal_False;
275cdf0e10cSrcweir 
276cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
277cdf0e10cSrcweir 	OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
278cdf0e10cSrcweir #endif
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	// convert to URL
281cdf0e10cSrcweir 	rtl_uString	*ustrSysPath = NULL;
282cdf0e10cSrcweir 	rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
283cdf0e10cSrcweir 	OSL_ASSERT(ustrSysPath != NULL);
284cdf0e10cSrcweir 	osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
285cdf0e10cSrcweir 	rtl_uString_release( ustrSysPath );
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 	return sal_True;
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir /*****************************************************************************/
291cdf0e10cSrcweir /* osl_getModuleURLFromFunctionAddress */
292cdf0e10cSrcweir /*****************************************************************************/
293cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
294cdf0e10cSrcweir {
295cdf0e10cSrcweir     return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
296cdf0e10cSrcweir }
297cdf0e10cSrcweir 
298cdf0e10cSrcweir /*****************************************************************************/
299cdf0e10cSrcweir 
300