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