darope@217: darope@217: // vi: set ts=4 sw=4 : darope@217: // vim: set tw=75 : darope@217: darope@217: // engineinfo.cpp - get info about HL engine, like file used darope@217: // and test segment range darope@217: // darope@217: darope@217: darope@217: #ifdef _WIN32 darope@217: // Don't include winspool.h; clashes with SERVER_EXECUTE from engine darope@217: # define _WINSPOOL_H darope@217: # include darope@217: # include // Header structures darope@217: #else darope@217: # ifndef _GNU_SOURCE darope@217: # define _GNU_SOURCE darope@217: # endif darope@217: # include // dladdr() darope@217: # include // ElfW(Phdr/Ehdr) macros. darope@217: // _DYNAMIC, r_debug, link_map, etc. darope@217: #endif /* _WIN32 */ darope@217: darope@217: #include // strlen(), strrchr(), strcmp() darope@217: #include // printf() darope@217: darope@217: #include "engineinfo.h" // me darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: #include "log_meta.h" // META_LOG() darope@217: darope@217: //#define FLDBG_PRINTF(s) dbg_logprintf s darope@217: #define FLDBG_PRINTF(s) META_LOG s darope@217: //#define FLDBG_PRINTF(s) printf s darope@217: //#define FLDBG_PRINTF(s) darope@217: #endif darope@217: //-TBR darope@217: darope@217: // Current mask for checking engine function addresses darope@217: // in VAC protected engine dlls on Win32. This is gonna fail darope@217: // on Win64. darope@217: const unsigned long c_VacDllEngineFuncsRangeMask = 0xFFF00000; darope@217: const unsigned long c_VacDllEngineFuncsRangeMark = 0x01D00000; darope@217: void* const c_VacDllEngineFuncsRangeStart = (void*)0x01D00000; darope@217: void* const c_VacDllEngineFuncsRangeEnd = (void*)0x01E00000; darope@217: darope@217: darope@217: bool EngineInfo::check_for_engine_module( const char* _pName ) darope@217: { darope@217: const char* pC; darope@217: size_t size; darope@217: darope@217: if ( NULL == _pName ) return false; darope@217: darope@217: #ifdef _WIN32 darope@217: darope@217: // The engine module is either sw.dll or hw.dll or swds.dll darope@217: pC = strrchr( _pName, '.' ); darope@217: darope@217: pC -= 2; darope@217: if ( 0 != strcmp(pC, "sw.dll") && 0 != strcmp(pC, "hw.dll") ) { darope@217: pC -= 2; darope@217: if ( 0 != strcmp(pC, "swds.dll") ) return false; darope@217: } darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("Locating module %s.", pC) ); darope@217: #endif darope@217: //-TBR darope@217: HMODULE hModule = GetModuleHandle( pC ); darope@217: if ( NULL == hModule ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("Could not get handle to module %s.", pC) ); darope@217: #endif darope@217: //-TBR darope@217: return false; darope@217: } darope@217: darope@217: // Ok, we found the string sw(ds).dll, thus we deduct that this is the darope@217: // name of the engine's shared object. We copy the string for future darope@217: // reference and return successfully. darope@217: size = 0; darope@217: while ( *pC != '.' && size < c_EngineInfo__typeLen-1 ) { darope@217: m_type[size++] = *pC++; darope@217: } darope@217: m_type[size] = '\0'; darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Found engine dll name %s.", m_type )); darope@217: #endif darope@217: //-TBR darope@217: darope@217: #else /* _WIN32 */ darope@217: darope@217: const char* pType; darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Checking for engine name in '%s'", _pName )); darope@217: #endif darope@217: //-TBR darope@217: darope@217: size = strlen( _pName ); darope@217: if ( size < 11 ) { darope@217: // Forget it, this string is too short to even be 'engine_.so', so darope@217: // it can't the name of the engine shared library. darope@217: return false; darope@217: } darope@217: darope@217: // Start parsing at the end. Since we know that the string is at least darope@217: // 11 characters long we can safely do our parsing without being afraid darope@217: // of leaving the string. darope@217: pC = _pName + size -1; darope@217: darope@217: // First we see if the string ends in '.so' darope@217: if ( *pC-- != 'o' || *pC-- != 's' || *pC-- != '.' ) return false; darope@217: darope@217: // Now find the '_' which would be the seperator between 'engine' and darope@217: // the architecture. darope@217: while ( *pC != '_' && pC != _pName ) --pC; darope@217: if ( pC == _pName ) return false; darope@217: darope@217: // We are at the '_', thus the architecture identifier must start at darope@217: // the next character. darope@217: pType = pC +1; darope@217: darope@217: // Now we walk further back and check if we find the string 'engine' darope@217: // backwards. darope@217: --pC; darope@217: if ( *pC-- != 'e' || *pC-- != 'n' || *pC-- != 'i' || darope@217: *pC-- != 'g' || *pC-- != 'n' || *pC != 'e' ) { darope@217: return false; darope@217: } darope@217: darope@217: // Ok, we found the string engine_*.so, thus we deduct that this is the darope@217: // name of the engine's shared object. We copy the architecture string darope@217: // for future reference and return successfully. darope@217: size = 0; darope@217: while ( *pType != '.' && size < c_EngineInfo__typeLen-1 ) { darope@217: m_type[size++] = *pType++; darope@217: } darope@217: m_type[size] = '\0'; darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Found engine dso name. Architecture is %s.", m_type )); darope@217: #endif darope@217: //-TBR darope@217: darope@217: #endif /* _WIN32 */ darope@217: darope@217: darope@217: return true; darope@217: } darope@217: darope@217: darope@217: #ifdef _WIN32 darope@217: darope@217: int EngineInfo::nthdr_module_name( void ) darope@217: { darope@217: PIMAGE_DOS_HEADER pDosHeader; darope@217: PIMAGE_NT_HEADERS pNTHeader; darope@217: unsigned char* pBaseAddr; darope@217: const char* pName = "sw.dll"; darope@217: darope@217: if ( ! check_for_engine_module(pName) ) { darope@217: pName = "hw.dll"; darope@217: if ( ! check_for_engine_module(pName) ) { darope@217: pName = "swds.dll"; darope@217: if ( ! check_for_engine_module(pName) ) { darope@217: return MODULE_NAME_NOTFOUND; darope@217: } darope@217: } darope@217: } darope@217: darope@217: // Get the module handle for the engine dll we found. darope@217: // This is also the modules base address. darope@217: HMODULE hModule = GetModuleHandle( pName ); darope@217: darope@217: pBaseAddr = (unsigned char*)hModule; darope@217: darope@217: darope@217: // Check if we find a DOS header darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("Checking for DOS header at address %p.", hModule) ); darope@217: #endif darope@217: //-TBR darope@217: pDosHeader = (PIMAGE_DOS_HEADER)hModule; darope@217: if ( pDosHeader->e_magic != IMAGE_DOS_SIGNATURE ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("DOS header magic doesn't match: 0x%x.", pDosHeader->e_magic) ); darope@217: #endif darope@217: //-TBR darope@217: return INVALID_DOS_SIGN; darope@217: } darope@217: darope@217: // Check if we find a PE header darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("DOS header points to NT header at 0x%lx.", pDosHeader->e_lfanew) ); darope@217: #endif darope@217: //-TBR darope@217: pNTHeader = (PIMAGE_NT_HEADERS)(pBaseAddr + pDosHeader->e_lfanew); darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("Checking for NT header at %p.", pNTHeader) ); darope@217: #endif darope@217: //-TBR darope@217: if ( pNTHeader->Signature != IMAGE_NT_SIGNATURE ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("NT header signature doesn't match: 0x%lx.", pNTHeader->Signature) ); darope@217: #endif darope@217: //-TBR darope@217: return INVALID_NT_SIGN; darope@217: } darope@217: darope@217: set_code_range( pBaseAddr, pNTHeader ); darope@217: return 0; darope@217: } darope@217: darope@217: darope@217: int EngineInfo::vac_pe_approx( enginefuncs_t* _pFuncs ) darope@217: { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("==DEBUG==> Approximating engine functions address range in VAC dll from 0x%p.", _pFuncs) ); darope@217: #endif darope@217: //-TBR darope@217: darope@217: if ( NULL == _pFuncs ) return INVALID_ARG; darope@217: darope@217: // There is really no good and easy way to do this. Right now what darope@217: // we do is assume that Steam listenservers will normally be darope@217: // up-to-date and hence have all function pointers set correctly. darope@217: // So we just check for some anomality and then set some approximated darope@217: // values. darope@217: darope@217: // The known addresses at the time of writing all start with 0x01D and darope@217: // this lie in the range between 0x01D00000 and 0x01E00000. What we do darope@217: // is check all functions pointers that are known to be good to start darope@217: // with 0x01D. If that is the case then we know that the loading address darope@217: // of the engine functions hasn't changed and we assume the above stated darope@217: // range to be the range of valid engine function addresses. darope@217: // If we find functions outside this range we can't determine a valid darope@217: // range and have to just give up. darope@217: darope@217: unsigned long* pengfuncs = (unsigned long*)_pFuncs; darope@217: unsigned int i, invals = 0; darope@217: for ( i = 0, pengfuncs += i; i < 140; i++, pengfuncs++ ) { darope@221: if ( ((*pengfuncs) & c_VacDllEngineFuncsRangeMask) != c_VacDllEngineFuncsRangeMark ) { darope@217: invals++; darope@217: break; darope@217: } darope@217: } darope@217: darope@217: if ( invals > 0 ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF( ("==DEBUG==> Found %u functions out of range 0x%08lx.", invals, c_VacDllEngineFuncsRangeMark) ); darope@217: FLDBG_PRINTF( (" Unable to determine valid function address range.") ); darope@217: #endif darope@217: //-TBR darope@217: strncpy( m_type, "vacdll+?", c_EngineInfo__typeLen ); darope@217: m_state = STATE_INVALID; darope@217: return STATE_INVALID; darope@217: } darope@217: darope@217: m_codeStart = c_VacDllEngineFuncsRangeStart; darope@217: m_codeEnd = c_VacDllEngineFuncsRangeEnd; darope@217: darope@217: strncpy( m_type, "vacdll", c_EngineInfo__typeLen ); darope@217: darope@217: m_state = STATE_VALID; darope@217: darope@217: return 0; darope@217: } darope@217: darope@217: darope@217: void EngineInfo::set_code_range( unsigned char* _pBase, PIMAGE_NT_HEADERS _pNThdr ) darope@217: { darope@217: m_codeStart = _pBase + _pNThdr->OptionalHeader.BaseOfCode; darope@217: m_codeEnd = _pBase + _pNThdr->OptionalHeader.BaseOfCode + _pNThdr->OptionalHeader.SizeOfCode; darope@217: darope@217: m_state = STATE_VALID; darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "==DEBUG==> Setting code range:" )); darope@217: FLDBG_PRINTF(( " pNThdr at %p, BaseOfCode = 0x%lx, end = 0x%lx, SizeOfCode = %ld 0x%lx", darope@217: _pNThdr, _pNThdr->OptionalHeader.BaseOfCode, darope@217: _pNThdr->OptionalHeader.BaseOfCode + _pNThdr->OptionalHeader.SizeOfCode, darope@217: _pNThdr->OptionalHeader.SizeOfCode, darope@217: _pNThdr->OptionalHeader.SizeOfCode darope@217: )); darope@217: FLDBG_PRINTF(( " start address = 0x%p, end address = 0x%p", darope@217: m_codeStart, m_codeEnd )); darope@217: #endif darope@217: //-TBR darope@217: } darope@217: darope@217: darope@217: #else /* _WIN32 */ darope@217: darope@217: darope@217: int EngineInfo::phdr_elfhdr( void* _pElfHdr ) darope@217: { darope@217: ElfW(Ehdr)* pEhdr = (ElfW(Ehdr)*)_pElfHdr; darope@217: ElfW(Phdr)* pPhdr; darope@217: darope@217: if ( NULL == _pElfHdr ) return INVALID_ARG; darope@217: darope@217: if ( pEhdr->e_ident[0] == 0x7f darope@217: && pEhdr->e_ident[1] == 'E' darope@217: && pEhdr->e_ident[2] == 'L' darope@217: && pEhdr->e_ident[3] == 'F' ) { darope@217: darope@217: // Looking good, we found an ELF signature. darope@217: // Let's see if the rest of the data at this address would fit an ELH header, too. darope@217: if ( (pEhdr->e_ident[EI_CLASS] == ELFCLASS32 //ELFW(CLASS) darope@217: || pEhdr->e_ident[EI_CLASS] == ELFCLASS64) darope@217: darope@217: && (pEhdr->e_ident[EI_DATA] == ELFDATA2LSB darope@217: || pEhdr->e_ident[EI_DATA] == ELFDATA2MSB) darope@217: darope@217: && pEhdr->e_type == ET_DYN ) { darope@217: darope@217: // Ok, we believe that this is a shared object's ELF header. darope@217: // Let's find the program header for the segment that includes darope@217: // the text section and return it. darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(("%d segments", pEhdr->e_phnum)); darope@217: #endif darope@217: //-TBR darope@217: pPhdr = (ElfW(Phdr) *) ((char *) pEhdr + pEhdr->e_phoff); darope@217: for ( int i = 0; i < pEhdr->e_phnum; i++, pPhdr++ ) { darope@217: darope@217: if ( PT_LOAD == pPhdr->p_type darope@217: && (pPhdr->p_flags & PF_R) darope@217: && (pPhdr->p_flags & PF_X) ) { darope@217: darope@217: // The text section gets loaded and has read and darope@217: // execute flags set. So this must be it. darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: #ifdef __amd64__ darope@217: FLDBG_PRINTF(("\t\t header %2d: address=%10p size=%8ld type=%d flags=", i, darope@217: (void *) (pEhdr + pPhdr->p_vaddr), darope@217: pPhdr->p_memsz, (int)(pPhdr->p_type)) ); darope@217: #else darope@217: FLDBG_PRINTF(("\t\t header %2d: address=%10p size=%8d type=%d flags=", i, darope@217: (void *) (pEhdr + pPhdr->p_vaddr), darope@217: pPhdr->p_memsz, (int)(pPhdr->p_type)) ); darope@217: #endif darope@217: FLDBG_PRINTF(("%c%c%c", darope@217: pPhdr->p_flags & PF_R ? 'R' : ' ', darope@217: pPhdr->p_flags & PF_W ? 'W' : ' ', darope@217: pPhdr->p_flags & PF_X ? 'X' : ' ' )); darope@217: FLDBG_PRINTF(( "" )); darope@217: #endif darope@217: //-TBR darope@217: set_code_range( pEhdr, pPhdr ); darope@217: return 0; darope@217: } darope@217: } darope@217: } darope@217: } darope@217: darope@217: return HEADER_NOTFOUND; darope@217: } darope@217: darope@217: darope@217: darope@217: darope@217: int EngineInfo::phdr_dladdr( void* _pMem ) darope@217: { darope@217: Dl_info info; darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Trying to find Program Header via dladdr." )); darope@217: FLDBG_PRINTF(( "Looking up dlinfo for %p", _pMem )); darope@217: #endif darope@217: //-TBR darope@217: if ( 0 != dladdr(_pMem, &info) ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Found info: " )); darope@217: FLDBG_PRINTF(( " name: %s", info.dli_fname )); darope@217: FLDBG_PRINTF(( " base: %p", info.dli_fbase )); darope@217: #endif darope@217: //-TBR darope@217: // Check if this is the engine module darope@217: if ( check_for_engine_module(info.dli_fname) ) { darope@217: return phdr_elfhdr( info.dli_fbase ); darope@217: } darope@217: } darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: else { darope@217: FLDBG_PRINTF(( "No info found." )); darope@217: } darope@217: #endif darope@217: //-TBR darope@217: darope@217: return NOTFOUND; darope@217: } darope@217: darope@217: darope@217: int EngineInfo::phdr_r_debug( void ) darope@217: { darope@217: ElfW(Dyn)* pDyn; darope@217: struct r_debug* pr_debug; darope@217: struct link_map* pMap; darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Trying to find Program Header via r_debug struct." )); darope@217: #endif darope@217: //-TBR darope@217: darope@217: // Search if we have a DT_DEBUG symbol in our DYNAMIC segment, which darope@217: // ought to be set to the r_debug structure's address. darope@217: for (pDyn = _DYNAMIC; pDyn->d_tag != DT_NULL; ++pDyn) { darope@217: if (pDyn->d_tag == DT_DEBUG) { darope@217: pr_debug = (struct r_debug *) pDyn->d_un.d_ptr; darope@217: break; darope@217: } darope@217: } darope@217: darope@217: if ( DT_NULL == pDyn->d_tag ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Unable to find my DT_DEBUG symbol." )); darope@217: #endif darope@217: //-TBR darope@217: } darope@217: else if ( NULL == pr_debug ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "DT_DEBUG found but is NULL." )); darope@217: #endif darope@217: //-TBR darope@217: } darope@217: else { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "Found DT_DEBUG symbol." )); darope@217: FLDBG_PRINTF(( "r_debug is at %p", pr_debug )); darope@217: #endif darope@217: //-TBR darope@217: pMap = pr_debug->r_map; darope@217: darope@217: // Walk to the start of the list darope@217: while ( pMap->l_prev != NULL ) pMap = pMap->l_prev; darope@217: do { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "link map is at %p", pMap )); darope@217: darope@217: #ifdef __amd64__ darope@217: FLDBG_PRINTF(( "base addr = %lx", pMap->l_addr )); darope@217: #else darope@217: FLDBG_PRINTF(( "base addr = %x", pMap->l_addr )); darope@217: #endif darope@217: FLDBG_PRINTF(( "name is %s\n", pMap->l_name )); darope@217: #endif darope@217: //-TBR darope@217: if ( check_for_engine_module(pMap->l_name) ) { darope@217: return phdr_elfhdr( (void*)pMap->l_addr ); darope@217: } darope@217: darope@217: pMap = pMap->l_next; darope@217: } while ( NULL != pMap ); darope@217: darope@217: } darope@217: darope@217: return NOTFOUND; darope@217: } darope@217: darope@217: void EngineInfo::set_code_range( void* _pBase, ElfW(Phdr)* _pPhdr ) darope@217: { darope@217: unsigned char* pBase = (unsigned char*)_pBase; darope@217: m_codeStart = pBase + _pPhdr->p_vaddr; darope@217: m_codeEnd = pBase + _pPhdr->p_vaddr + _pPhdr->p_memsz; darope@217: darope@217: m_state = STATE_VALID; darope@217: darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "==DEBUG==> Setting code range:" )); darope@217: #ifdef __amd64__ darope@217: FLDBG_PRINTF(( " pPhdr at %p, p_vaddr = 0x%lx, vend = 0x%lx, p_memsz = %ld", darope@217: _pPhdr, _pPhdr->p_vaddr, _pPhdr->p_vaddr + _pPhdr->p_memsz, _pPhdr->p_memsz )); darope@217: #else darope@217: FLDBG_PRINTF(( " pPhdr at %p, p_vaddr = 0x%x, vend = 0x%x, p_memsz = %d", darope@217: _pPhdr, _pPhdr->p_vaddr, _pPhdr->p_vaddr + _pPhdr->p_memsz, _pPhdr->p_memsz )); darope@217: #endif darope@217: FLDBG_PRINTF(( " start address = %p, end address = %p", darope@217: m_codeStart, m_codeEnd )); darope@217: #endif darope@217: //-TBR darope@217: } darope@217: darope@217: darope@217: #endif /* _WIN32 */ darope@217: darope@217: darope@217: int EngineInfo::initialise( enginefuncs_t* _pFuncs ) darope@217: { darope@217: int ret = 0; darope@217: darope@217: // Have to do this only once. darope@217: if ( NULL != m_codeStart ) { darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "==DEBUG==> ---------- Engine info already initialised --" )); darope@217: FLDBG_PRINTF(( "==DEBUG==> codeStart: %p codeEnd: %p", m_codeStart, m_codeEnd )); darope@217: #endif darope@217: //-TBR darope@217: return 0; darope@217: } darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: FLDBG_PRINTF(( "==DEBUG==> ---------- Initialising engine info --" )); darope@217: #endif darope@217: //-TBR darope@217: darope@217: #ifdef _WIN32 darope@217: darope@217: ret = nthdr_module_name(); darope@217: if ( MODULE_NAME_NOTFOUND == ret && (! _pFuncs->pfnIsDedicatedServer()) ) { darope@217: // We could not find the engine dll by name and we are running on darope@217: // a listen server. This usually means that that we are dealing with darope@217: // a VAC protected engine dll. No other way than approximating the darope@217: // range of valid engine function pointers in this case. darope@217: ret = vac_pe_approx( _pFuncs ); darope@217: } darope@217: darope@217: #else /* _WIN32 */ darope@217: darope@217: // If we have no reference pointer to start from we can only try to use darope@217: // the r_debug symbol. darope@217: if ( NULL == _pFuncs ) { darope@217: ret = phdr_r_debug(); darope@217: } darope@217: darope@217: // If we have a refererence pointer we try to use it first. darope@217: if ( 0 != phdr_dladdr(_pFuncs) ) { darope@217: ret = phdr_r_debug(); darope@217: } darope@217: darope@217: #endif /* _WIN32 */ darope@217: //+TBR darope@217: #ifdef MM_DEBUG darope@217: if ( 0 != ret ) { darope@217: FLDBG_PRINTF(( "==DEBUG==> Unable to determine functions address range!" )); darope@217: } darope@217: else { darope@217: FLDBG_PRINTF(( "==DEBUG==> codeStart: %p codeEnd: %p", m_codeStart, m_codeEnd )); darope@217: } darope@217: #endif darope@217: //-TBR darope@217: darope@217: return 0; darope@217: } darope@217: darope@217: