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