INT32 _ossModuleHandle::resolveAddress ( const CHAR *pFunctionName, OSS_MODULE_PFUNCTION *pFunctionAddress ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY ( SDB_OSSMODULEHANDLE_RESOLVEADDRESS ) ; if ( !pFunctionName || !pFunctionAddress ) { rc = SDB_INVALIDARG ; goto error ; } PD_TRACE2 ( SDB_OSSMODULEHANDLE_RESOLVEADDRESS, PD_PACK_STRING ( _moduleName ), PD_PACK_STRING ( pFunctionName )) ; SDB_ASSERT ( _isInitialized, "handle must be initialized before resolve address" ) ; #if defined (_LINUX) *pFunctionAddress = (OSS_MODULE_PFUNCTION)dlsym ( _moduleHandle, (CHAR*)pFunctionName ) ; if ( !*pFunctionAddress ) { PD_LOG ( PDERROR, "Failed to find function %s: %s", pFunctionName, dlerror () ) ; rc = SDB_SYS ; goto error ; } #elif defined (_WINDOWS) *pFunctionAddress = (OSS_MODULE_PFUNCTION)GetProcAddress ( _moduleHandle, (LPCSTR)pFunctionName ) ; if ( !*pFunctionAddress ) { rc = ossGetLastError () ; PD_LOG ( PDERROR, "Failed to find function %s: %d", pFunctionName, rc ) ; OSSMODULEHANDLE_ERR(rc) ; goto error ; } #endif done : PD_TRACE_EXITRC ( SDB_OSSMODULEHANDLE_RESOLVEADDRESS, rc ) ; return rc ; error : goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( SDB_OSSMCHCODE, "ossMachineCode" ) CHAR *ossMachineCode( UINT32 instruction, CHAR * mCode ) { PD_TRACE_ENTRY ( SDB_OSSMCHCODE ); char mcode[4] = { 0 } ; if ( NULL != mCode ) { memcpy( mcode, &instruction, sizeof(mcode) ) ; ossSnprintf( mCode, OSS_MCODE_LEN, "%02X%02X%02X%02X", 0xff & mcode[0], 0xff & mcode[1], 0xff & mcode[2], 0xff & mcode[3]) ; PD_TRACE1 ( SDB_OSSMCHCODE, PD_PACK_STRING(mCode) ); } PD_TRACE_EXIT ( SDB_OSSMCHCODE ); return mCode ; }
INT32 _ossModuleHandle::init () { INT32 rc = SDB_OK ; CHAR strPath [ 2*OSS_MAX_PATHSIZE + 1 ] = {0} ; CHAR strModule [ OSS_MAX_PATHSIZE + 1 ] = {0} ; OSS_MHANDLE handle = 0 ; CHAR *p = NULL ; #if defined (_WINDOWS) UINT32 errorMode ; #endif PD_TRACE_ENTRY ( SDB_OSSMODULEHANDLE_INIT ) ; if ( _moduleName[0] == '\0' ) { PD_LOG ( PDERROR, "Module name can't be empty" ) ; rc = SDB_INVALIDARG ; goto error ; } PD_TRACE3 ( SDB_OSSMODULEHANDLE_INIT, PD_PACK_STRING(_moduleName), PD_PACK_STRING(_libPath), PD_PACK_UINT(_flags) ) ; ossStrncpy ( strModule, _moduleName, sizeof(strModule) ) ; p = ossStrchr ( strModule, '(' ) ; if ( p ) { *p = '\0' ; } rc = patchModuleName( strModule, _moduleName, sizeof(_moduleName) ); PD_RC_CHECK ( rc, PDERROR, "Failed to patch module name, rc = %d", rc ) ; if ( _libPath[0] ) { INT32 pathLen = 0 ; rc = ossAccess ( _libPath, #if defined (_LINUX) F_OK #elif defined (_WINDOWS) 0 #endif ) ; PD_RC_CHECK ( rc, PDERROR, "Failed to access path %s, rc = %d", _libPath, rc ) ; ossStrncat ( strPath, _libPath, sizeof(strPath) ) ; pathLen = ossStrlen ( strPath ) ; if ( strPath[pathLen-1] != OSS_FILE_SEP_CHAR ) { if ( pathLen >= OSS_MAX_PATHSIZE ) { PD_LOG ( PDERROR, "library path is too long: %s", _libPath ) ; rc = SDB_INVALIDARG ; goto error ; } strPath[pathLen-1] = OSS_FILE_SEP_CHAR ; strPath[pathLen] = '\0' ; } } if ( ossStrlen ( strPath ) + ossStrlen ( _moduleName ) >= sizeof(strPath) ) { PD_LOG ( PDERROR, "path + module name is too long: %s:%s", strPath, _moduleName ) ; rc = SDB_INVALIDARG ; goto error ; } ossStrncat ( strPath, _moduleName, OSS_MAX_PATHSIZE ) ; #if defined (_LINUX) handle = dlopen ( strPath, _flags | RTLD_NOW ) ; if ( !handle ) { PD_LOG ( PDERROR, "Failed to load module %s, error = %s", strPath, dlerror() ) ; rc = SDB_SYS ; goto error ; } _isInitialized = TRUE ; _moduleHandle = handle ; dlerror() ; #elif defined (_WINDOWS) errorMode = SetErrorMode ( SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS ) ; _moduleHandle = LoadLibrary ( (LPCTSTR)strPath ) ; SetErrorMode ( errorMode ) ; if ( NULL == _moduleHandle ) { rc = ossGetLastError () ; PD_LOG ( PDERROR, "Failed to load module %s, error = %d", _moduleName, rc ) ; OSSMODULEHANDLE_ERR(rc) ; goto error ; } #endif done : PD_TRACE_EXITRC ( SDB_OSSMODULEHANDLE_INIT, rc ) ; return rc ; error : _isInitialized = FALSE ; _moduleHandle = OSS_MINVALIDHANDLE ; goto done ; }