metamod/engineinfo.cpp
author darope
Sat Apr 08 19:29:39 2006 +0000 (2006-04-08)
branchB-work
changeset 217 43ad553832f6
child 221 c2ece1309fdf
permissions -rw-r--r--
Fixed erroneous handling of the interface to the engine, gamedll and
plugins in the cases that Metamod was compiled against a different version
of the HL SDK.
For one, Metamod now uses zeroed dummy space at the end of function pointer
structures so that units copying into Metamod do not overwrite anything
valuable and that units copying from Metamod get zeroes and not arbitrary
values when the sizes of the structures don't match. We achieve this within
Metamod and without having to change the SDK.
Secondly, Metamod now tries to determine what engine interface it deals
with. It then adjusts the function pointers it got from the engine
accordingly and also will not overwrite engine memory when copying its own
function pointers into engine memory space.
The way this was done before was inaccurate and clumsy and resulted in
errors when using Metamod with older engine versions. Since the method of
using the IS_VALID_PTR() for checking for valid function pointers doesn't
yield correct results, a new method was implemented. We now try to
determine the valid range of memory addresses for engine functions more
accurately in order to check if a function pointer is a valid pointer for
a function living in the engine.
darope@217
     1
darope@217
     2
// vi: set ts=4 sw=4 :
darope@217
     3
// vim: set tw=75 :
darope@217
     4
darope@217
     5
// engineinfo.cpp - get info about HL engine, like file used
darope@217
     6
//                  and test segment range
darope@217
     7
//
darope@217
     8
darope@217
     9
darope@217
    10
#ifdef _WIN32
darope@217
    11
   // Don't include winspool.h; clashes with SERVER_EXECUTE from engine
darope@217
    12
#  define _WINSPOOL_H	
darope@217
    13
#  include <windows.h>
darope@217
    14
#  include <winnt.h>     // Header structures
darope@217
    15
#else 
darope@217
    16
#  ifndef _GNU_SOURCE
darope@217
    17
#    define _GNU_SOURCE
darope@217
    18
#  endif
darope@217
    19
#  include <dlfcn.h>			// dladdr()
darope@217
    20
#  include <link.h>				// ElfW(Phdr/Ehdr) macros.
darope@217
    21
                  				// _DYNAMIC, r_debug, link_map, etc.
darope@217
    22
#endif /* _WIN32 */ 
darope@217
    23
darope@217
    24
#include <string.h>				// strlen(), strrchr(), strcmp()
darope@217
    25
#include <stdio.h>				// printf()
darope@217
    26
darope@217
    27
#include "engineinfo.h"			// me
darope@217
    28
darope@217
    29
//+TBR
darope@217
    30
#ifdef MM_DEBUG
darope@217
    31
#include "log_meta.h"			// META_LOG()
darope@217
    32
darope@217
    33
//#define FLDBG_PRINTF(s) dbg_logprintf s
darope@217
    34
#define FLDBG_PRINTF(s) META_LOG s
darope@217
    35
//#define FLDBG_PRINTF(s) printf s
darope@217
    36
//#define FLDBG_PRINTF(s) 
darope@217
    37
#endif
darope@217
    38
//-TBR
darope@217
    39
darope@217
    40
// Current mask for checking engine function addresses 
darope@217
    41
// in VAC protected engine dlls on Win32. This is gonna fail
darope@217
    42
// on Win64.
darope@217
    43
const unsigned long c_VacDllEngineFuncsRangeMask  = 0xFFF00000;
darope@217
    44
const unsigned long c_VacDllEngineFuncsRangeMark  = 0x01D00000;
darope@217
    45
void* const c_VacDllEngineFuncsRangeStart = (void*)0x01D00000;
darope@217
    46
void* const c_VacDllEngineFuncsRangeEnd   = (void*)0x01E00000;
darope@217
    47
darope@217
    48
darope@217
    49
bool EngineInfo::check_for_engine_module( const char* _pName )
darope@217
    50
{
darope@217
    51
	const char* pC;
darope@217
    52
	size_t size;
darope@217
    53
darope@217
    54
	if ( NULL == _pName ) return false;
darope@217
    55
darope@217
    56
#ifdef _WIN32
darope@217
    57
darope@217
    58
	// The engine module is either sw.dll or hw.dll or swds.dll
darope@217
    59
	pC = strrchr( _pName, '.' );
darope@217
    60
darope@217
    61
	pC -= 2;
darope@217
    62
	if ( 0 != strcmp(pC, "sw.dll") && 0 != strcmp(pC, "hw.dll") ) {
darope@217
    63
		pC -= 2;
darope@217
    64
		if ( 0 != strcmp(pC, "swds.dll") ) return false;
darope@217
    65
	}
darope@217
    66
darope@217
    67
//+TBR
darope@217
    68
#ifdef MM_DEBUG
darope@217
    69
	FLDBG_PRINTF( ("Locating module %s.", pC) );
darope@217
    70
#endif
darope@217
    71
//-TBR
darope@217
    72
	HMODULE hModule = GetModuleHandle( pC );
darope@217
    73
	if ( NULL == hModule ) {
darope@217
    74
//+TBR
darope@217
    75
#ifdef MM_DEBUG
darope@217
    76
		FLDBG_PRINTF( ("Could not get handle to module %s.", pC) );
darope@217
    77
#endif
darope@217
    78
//-TBR
darope@217
    79
		return false;
darope@217
    80
	}
darope@217
    81
darope@217
    82
	// Ok, we found the string sw(ds).dll, thus we deduct that this is the
darope@217
    83
	// name of the engine's shared object. We copy the string for future 
darope@217
    84
	// reference and return successfully.
darope@217
    85
	size = 0;
darope@217
    86
	while ( *pC != '.' && size < c_EngineInfo__typeLen-1 ) {
darope@217
    87
		m_type[size++] = *pC++;
darope@217
    88
	}
darope@217
    89
	m_type[size] = '\0';
darope@217
    90
//+TBR
darope@217
    91
#ifdef MM_DEBUG
darope@217
    92
	FLDBG_PRINTF(( "Found engine dll name %s.", m_type ));
darope@217
    93
#endif
darope@217
    94
//-TBR
darope@217
    95
darope@217
    96
#else /* _WIN32 */
darope@217
    97
darope@217
    98
	const char* pType;
darope@217
    99
darope@217
   100
//+TBR
darope@217
   101
#ifdef MM_DEBUG
darope@217
   102
	FLDBG_PRINTF(( "Checking for engine name in '%s'", _pName ));
darope@217
   103
#endif
darope@217
   104
//-TBR
darope@217
   105
darope@217
   106
	size = strlen( _pName );
darope@217
   107
	if ( size < 11 ) {
darope@217
   108
		// Forget it, this string is too short to even be 'engine_.so', so
darope@217
   109
		// it can't the name of the engine shared library.
darope@217
   110
		return false;
darope@217
   111
	}
darope@217
   112
darope@217
   113
	// Start parsing at the end. Since we know that the string is at least
darope@217
   114
	// 11 characters long we can safely do our parsing without being afraid
darope@217
   115
	// of leaving the string.
darope@217
   116
	pC = _pName + size -1;
darope@217
   117
darope@217
   118
	// First we see if the string ends in '.so'
darope@217
   119
	if ( *pC-- != 'o' || *pC-- != 's' || *pC-- != '.' ) return false;
darope@217
   120
darope@217
   121
	// Now find the '_' which would be the seperator between 'engine' and
darope@217
   122
	// the architecture.
darope@217
   123
	while ( *pC != '_' && pC != _pName ) --pC;
darope@217
   124
	if ( pC == _pName ) return false;
darope@217
   125
darope@217
   126
	// We are at the '_', thus the architecture identifier must start at
darope@217
   127
	// the next character.
darope@217
   128
	pType = pC +1;
darope@217
   129
darope@217
   130
	// Now we walk further back and check if we find the string 'engine'
darope@217
   131
	// backwards.
darope@217
   132
	--pC;
darope@217
   133
	if ( *pC-- != 'e' || *pC-- != 'n' || *pC-- != 'i' ||
darope@217
   134
		 *pC-- != 'g' || *pC-- != 'n' || *pC != 'e' ) {
darope@217
   135
		return false;
darope@217
   136
	}
darope@217
   137
darope@217
   138
	// Ok, we found the string engine_*.so, thus we deduct that this is the
darope@217
   139
	// name of the engine's shared object. We copy the architecture string
darope@217
   140
	// for future reference and return successfully.
darope@217
   141
	size = 0;
darope@217
   142
	while ( *pType != '.' && size < c_EngineInfo__typeLen-1 ) {
darope@217
   143
		m_type[size++] = *pType++;
darope@217
   144
	}
darope@217
   145
	m_type[size] = '\0';
darope@217
   146
darope@217
   147
//+TBR
darope@217
   148
#ifdef MM_DEBUG
darope@217
   149
	FLDBG_PRINTF(( "Found engine dso name. Architecture is %s.", m_type ));
darope@217
   150
#endif
darope@217
   151
//-TBR
darope@217
   152
darope@217
   153
#endif /* _WIN32 */
darope@217
   154
darope@217
   155
darope@217
   156
	return true;
darope@217
   157
}
darope@217
   158
darope@217
   159
darope@217
   160
#ifdef _WIN32
darope@217
   161
darope@217
   162
int EngineInfo::nthdr_module_name( void )
darope@217
   163
{
darope@217
   164
	PIMAGE_DOS_HEADER pDosHeader;
darope@217
   165
	PIMAGE_NT_HEADERS pNTHeader;
darope@217
   166
	unsigned char* pBaseAddr;
darope@217
   167
	const char* pName = "sw.dll";
darope@217
   168
darope@217
   169
	if ( ! check_for_engine_module(pName) ) {
darope@217
   170
		pName = "hw.dll";
darope@217
   171
		if ( ! check_for_engine_module(pName) ) {
darope@217
   172
			pName = "swds.dll";
darope@217
   173
			if ( ! check_for_engine_module(pName) ) {
darope@217
   174
				return MODULE_NAME_NOTFOUND;
darope@217
   175
			}
darope@217
   176
		}
darope@217
   177
	}
darope@217
   178
	
darope@217
   179
	// Get the module handle for the engine dll we found.
darope@217
   180
	// This is also the modules base address.
darope@217
   181
	HMODULE hModule = GetModuleHandle( pName );
darope@217
   182
darope@217
   183
	pBaseAddr = (unsigned char*)hModule;
darope@217
   184
darope@217
   185
darope@217
   186
	// Check if we find a DOS header
darope@217
   187
//+TBR
darope@217
   188
#ifdef MM_DEBUG
darope@217
   189
	FLDBG_PRINTF( ("Checking for DOS header at address %p.", hModule) );
darope@217
   190
#endif
darope@217
   191
//-TBR
darope@217
   192
	pDosHeader = (PIMAGE_DOS_HEADER)hModule;
darope@217
   193
    if ( pDosHeader->e_magic != IMAGE_DOS_SIGNATURE ) {
darope@217
   194
//+TBR
darope@217
   195
#ifdef MM_DEBUG
darope@217
   196
		FLDBG_PRINTF( ("DOS header magic doesn't match: 0x%x.", pDosHeader->e_magic) );
darope@217
   197
#endif
darope@217
   198
//-TBR
darope@217
   199
		return INVALID_DOS_SIGN;
darope@217
   200
	}
darope@217
   201
darope@217
   202
	// Check if we find a PE header
darope@217
   203
//+TBR
darope@217
   204
#ifdef MM_DEBUG
darope@217
   205
	FLDBG_PRINTF( ("DOS header points to NT header at 0x%lx.", pDosHeader->e_lfanew) );
darope@217
   206
#endif
darope@217
   207
//-TBR
darope@217
   208
	pNTHeader = (PIMAGE_NT_HEADERS)(pBaseAddr + pDosHeader->e_lfanew);
darope@217
   209
//+TBR
darope@217
   210
#ifdef MM_DEBUG
darope@217
   211
	FLDBG_PRINTF( ("Checking for NT header at %p.", pNTHeader) );
darope@217
   212
#endif
darope@217
   213
//-TBR
darope@217
   214
	if ( pNTHeader->Signature != IMAGE_NT_SIGNATURE ) {
darope@217
   215
//+TBR
darope@217
   216
#ifdef MM_DEBUG
darope@217
   217
		FLDBG_PRINTF( ("NT header signature doesn't match: 0x%lx.", pNTHeader->Signature) );
darope@217
   218
#endif
darope@217
   219
//-TBR
darope@217
   220
		return INVALID_NT_SIGN;
darope@217
   221
	}
darope@217
   222
darope@217
   223
	set_code_range( pBaseAddr, pNTHeader );
darope@217
   224
	return 0;
darope@217
   225
}
darope@217
   226
darope@217
   227
darope@217
   228
int EngineInfo::vac_pe_approx( enginefuncs_t* _pFuncs )
darope@217
   229
{
darope@217
   230
//+TBR
darope@217
   231
#ifdef MM_DEBUG
darope@217
   232
	FLDBG_PRINTF( ("==DEBUG==> Approximating engine functions address range in VAC dll from 0x%p.", _pFuncs) );
darope@217
   233
#endif
darope@217
   234
//-TBR
darope@217
   235
darope@217
   236
    if ( NULL == _pFuncs ) return INVALID_ARG;
darope@217
   237
	
darope@217
   238
	// There is really no good and easy way to do this. Right now what 
darope@217
   239
	// we do is assume that Steam listenservers will normally be 
darope@217
   240
	// up-to-date and hence have all function pointers set correctly.
darope@217
   241
	// So we just check for some anomality and then set some approximated
darope@217
   242
	// values.
darope@217
   243
darope@217
   244
	// The known addresses at the time of writing all start with 0x01D and
darope@217
   245
	// this lie in the range between 0x01D00000 and 0x01E00000. What we do
darope@217
   246
	// is check all functions pointers that are known to be good to start
darope@217
   247
	// with 0x01D. If that is the case then we know that the loading address
darope@217
   248
	// of the engine functions hasn't changed and we assume the above stated
darope@217
   249
	// range to be the range of valid engine function addresses.
darope@217
   250
	// If we find functions outside this range we can't determine a valid
darope@217
   251
	// range and have to just give up.
darope@217
   252
darope@217
   253
	unsigned long* pengfuncs = (unsigned long*)_pFuncs;
darope@217
   254
	unsigned int i, invals = 0;
darope@217
   255
	for ( i = 0, pengfuncs += i; i < 140; i++, pengfuncs++ ) {
darope@217
   256
		if ( *pengfuncs & c_VacDllEngineFuncsRangeMask != c_VacDllEngineFuncsRangeMark ) {
darope@217
   257
			invals++;
darope@217
   258
			break;
darope@217
   259
		}
darope@217
   260
	}
darope@217
   261
darope@217
   262
	if ( invals > 0 ) {
darope@217
   263
//+TBR
darope@217
   264
#ifdef MM_DEBUG
darope@217
   265
		FLDBG_PRINTF( ("==DEBUG==> Found %u functions out of range 0x%08lx.", invals, c_VacDllEngineFuncsRangeMark) );
darope@217
   266
		FLDBG_PRINTF( ("           Unable to determine valid function address range.") );
darope@217
   267
#endif
darope@217
   268
//-TBR
darope@217
   269
		strncpy( m_type, "vacdll+?", c_EngineInfo__typeLen );
darope@217
   270
        m_state = STATE_INVALID;
darope@217
   271
		return STATE_INVALID;
darope@217
   272
	}
darope@217
   273
darope@217
   274
	m_codeStart = c_VacDllEngineFuncsRangeStart;
darope@217
   275
	m_codeEnd = c_VacDllEngineFuncsRangeEnd;
darope@217
   276
darope@217
   277
	strncpy( m_type, "vacdll", c_EngineInfo__typeLen );
darope@217
   278
darope@217
   279
    m_state = STATE_VALID;
darope@217
   280
darope@217
   281
	return 0;
darope@217
   282
}
darope@217
   283
darope@217
   284
darope@217
   285
void EngineInfo::set_code_range( unsigned char* _pBase, PIMAGE_NT_HEADERS _pNThdr )
darope@217
   286
{
darope@217
   287
	m_codeStart = _pBase + _pNThdr->OptionalHeader.BaseOfCode;
darope@217
   288
	m_codeEnd = _pBase + _pNThdr->OptionalHeader.BaseOfCode + _pNThdr->OptionalHeader.SizeOfCode;
darope@217
   289
darope@217
   290
    m_state = STATE_VALID;
darope@217
   291
darope@217
   292
//+TBR
darope@217
   293
#ifdef MM_DEBUG
darope@217
   294
	FLDBG_PRINTF(( "==DEBUG==> Setting code range:" ));
darope@217
   295
	FLDBG_PRINTF(( "           pNThdr at %p, BaseOfCode = 0x%lx, end = 0x%lx, SizeOfCode = %ld 0x%lx",
darope@217
   296
			_pNThdr, _pNThdr->OptionalHeader.BaseOfCode, 
darope@217
   297
			_pNThdr->OptionalHeader.BaseOfCode + _pNThdr->OptionalHeader.SizeOfCode,
darope@217
   298
			_pNThdr->OptionalHeader.SizeOfCode,
darope@217
   299
			_pNThdr->OptionalHeader.SizeOfCode
darope@217
   300
			));
darope@217
   301
	FLDBG_PRINTF(( "                         start address = 0x%p, end address = 0x%p",
darope@217
   302
			m_codeStart, m_codeEnd ));
darope@217
   303
#endif
darope@217
   304
//-TBR
darope@217
   305
}
darope@217
   306
darope@217
   307
darope@217
   308
#else /* _WIN32 */
darope@217
   309
darope@217
   310
darope@217
   311
int EngineInfo::phdr_elfhdr( void* _pElfHdr )
darope@217
   312
{
darope@217
   313
	ElfW(Ehdr)* pEhdr = (ElfW(Ehdr)*)_pElfHdr;
darope@217
   314
	ElfW(Phdr)* pPhdr;
darope@217
   315
darope@217
   316
    if ( NULL == _pElfHdr ) return INVALID_ARG;
darope@217
   317
	
darope@217
   318
    if ( pEhdr->e_ident[0] == 0x7f
darope@217
   319
	  && pEhdr->e_ident[1] == 'E'
darope@217
   320
	  && pEhdr->e_ident[2] == 'L'
darope@217
   321
	  && pEhdr->e_ident[3] == 'F' ) {
darope@217
   322
darope@217
   323
		// Looking good, we found an ELF signature.
darope@217
   324
		// Let's see if the rest of the data at this address would fit an ELH header, too.
darope@217
   325
		if (  (pEhdr->e_ident[EI_CLASS] == ELFCLASS32 //ELFW(CLASS)
darope@217
   326
			|| pEhdr->e_ident[EI_CLASS] == ELFCLASS64)
darope@217
   327
darope@217
   328
		   && (pEhdr->e_ident[EI_DATA] == ELFDATA2LSB
darope@217
   329
            || pEhdr->e_ident[EI_DATA] == ELFDATA2MSB)
darope@217
   330
darope@217
   331
           && pEhdr->e_type == ET_DYN ) {
darope@217
   332
darope@217
   333
			// Ok, we believe that this is a shared object's ELF header.
darope@217
   334
			// Let's find the program header for the segment that includes
darope@217
   335
			// the text section and return it.
darope@217
   336
        
darope@217
   337
//+TBR
darope@217
   338
#ifdef MM_DEBUG
darope@217
   339
			FLDBG_PRINTF(("%d segments", pEhdr->e_phnum));
darope@217
   340
#endif
darope@217
   341
//-TBR
darope@217
   342
			pPhdr = (ElfW(Phdr) *) ((char *) pEhdr + pEhdr->e_phoff);
darope@217
   343
			for ( int i = 0; i < pEhdr->e_phnum; i++, pPhdr++ ) {
darope@217
   344
darope@217
   345
				if ( PT_LOAD == pPhdr->p_type 
darope@217
   346
					&& (pPhdr->p_flags & PF_R) 
darope@217
   347
					&& (pPhdr->p_flags & PF_X) ) {
darope@217
   348
darope@217
   349
					// The text section gets loaded and has read and
darope@217
   350
					// execute flags set. So this must be it.
darope@217
   351
//+TBR
darope@217
   352
#ifdef MM_DEBUG
darope@217
   353
#ifdef __amd64__
darope@217
   354
		            FLDBG_PRINTF(("\t\t header %2d: address=%10p size=%8ld type=%d flags=", i,
darope@217
   355
						(void *) (pEhdr + pPhdr->p_vaddr),
darope@217
   356
		                pPhdr->p_memsz, (int)(pPhdr->p_type)) );
darope@217
   357
#else
darope@217
   358
		            FLDBG_PRINTF(("\t\t header %2d: address=%10p size=%8d type=%d flags=", i,
darope@217
   359
						(void *) (pEhdr + pPhdr->p_vaddr),
darope@217
   360
		                pPhdr->p_memsz, (int)(pPhdr->p_type)) );
darope@217
   361
#endif
darope@217
   362
				    FLDBG_PRINTF(("%c%c%c",
darope@217
   363
						pPhdr->p_flags & PF_R ? 'R' : ' ',
darope@217
   364
					    pPhdr->p_flags & PF_W ? 'W' : ' ',
darope@217
   365
						pPhdr->p_flags & PF_X ? 'X' : ' ' ));
darope@217
   366
					FLDBG_PRINTF(( "" ));
darope@217
   367
#endif
darope@217
   368
//-TBR
darope@217
   369
					set_code_range( pEhdr, pPhdr );
darope@217
   370
					return 0;
darope@217
   371
				}
darope@217
   372
			}
darope@217
   373
        }
darope@217
   374
    }
darope@217
   375
darope@217
   376
	return HEADER_NOTFOUND;
darope@217
   377
}
darope@217
   378
darope@217
   379
darope@217
   380
darope@217
   381
darope@217
   382
int EngineInfo::phdr_dladdr( void* _pMem )
darope@217
   383
{
darope@217
   384
	Dl_info info;
darope@217
   385
darope@217
   386
//+TBR
darope@217
   387
#ifdef MM_DEBUG
darope@217
   388
	FLDBG_PRINTF(( "Trying to find Program Header via dladdr." ));
darope@217
   389
	FLDBG_PRINTF(( "Looking up dlinfo for %p", _pMem ));
darope@217
   390
#endif
darope@217
   391
//-TBR
darope@217
   392
	if ( 0 != dladdr(_pMem, &info) ) {
darope@217
   393
//+TBR
darope@217
   394
#ifdef MM_DEBUG
darope@217
   395
		FLDBG_PRINTF(( "Found info: " ));
darope@217
   396
		FLDBG_PRINTF(( "  name: %s", info.dli_fname ));
darope@217
   397
		FLDBG_PRINTF(( "  base: %p", info.dli_fbase ));
darope@217
   398
#endif
darope@217
   399
//-TBR
darope@217
   400
		// Check if this is the engine module
darope@217
   401
		if ( check_for_engine_module(info.dli_fname) ) {
darope@217
   402
		   return phdr_elfhdr( info.dli_fbase );
darope@217
   403
		}
darope@217
   404
	}
darope@217
   405
//+TBR
darope@217
   406
#ifdef MM_DEBUG
darope@217
   407
	else {
darope@217
   408
		FLDBG_PRINTF(( "No info found." ));
darope@217
   409
	}
darope@217
   410
#endif
darope@217
   411
//-TBR
darope@217
   412
darope@217
   413
	return NOTFOUND;
darope@217
   414
}
darope@217
   415
darope@217
   416
darope@217
   417
int EngineInfo::phdr_r_debug( void )
darope@217
   418
{
darope@217
   419
    ElfW(Dyn)* pDyn; 
darope@217
   420
    struct r_debug* pr_debug;
darope@217
   421
    struct link_map* pMap;
darope@217
   422
darope@217
   423
//+TBR
darope@217
   424
#ifdef MM_DEBUG
darope@217
   425
	FLDBG_PRINTF(( "Trying to find Program Header via r_debug struct." ));
darope@217
   426
#endif
darope@217
   427
//-TBR
darope@217
   428
darope@217
   429
	// Search if we have a DT_DEBUG symbol in our DYNAMIC segment, which
darope@217
   430
	// ought to be set to the r_debug structure's address.
darope@217
   431
    for (pDyn = _DYNAMIC; pDyn->d_tag != DT_NULL; ++pDyn) {
darope@217
   432
        if (pDyn->d_tag == DT_DEBUG) {
darope@217
   433
            pr_debug = (struct r_debug *) pDyn->d_un.d_ptr;
darope@217
   434
            break;
darope@217
   435
        }
darope@217
   436
    }
darope@217
   437
darope@217
   438
    if ( DT_NULL == pDyn->d_tag ) {
darope@217
   439
//+TBR
darope@217
   440
#ifdef MM_DEBUG
darope@217
   441
		FLDBG_PRINTF(( "Unable to find my DT_DEBUG symbol." ));
darope@217
   442
#endif
darope@217
   443
//-TBR
darope@217
   444
    }
darope@217
   445
	else if ( NULL == pr_debug ) {
darope@217
   446
//+TBR
darope@217
   447
#ifdef MM_DEBUG
darope@217
   448
		FLDBG_PRINTF(( "DT_DEBUG found but is NULL." ));
darope@217
   449
#endif
darope@217
   450
//-TBR
darope@217
   451
	}
darope@217
   452
    else {
darope@217
   453
//+TBR
darope@217
   454
#ifdef MM_DEBUG
darope@217
   455
        FLDBG_PRINTF(( "Found DT_DEBUG symbol." ));
darope@217
   456
        FLDBG_PRINTF(( "r_debug is at %p", pr_debug ));
darope@217
   457
#endif
darope@217
   458
//-TBR
darope@217
   459
		pMap = pr_debug->r_map;
darope@217
   460
darope@217
   461
		// Walk to the start of the list
darope@217
   462
        while ( pMap->l_prev != NULL ) pMap = pMap->l_prev;
darope@217
   463
        do {
darope@217
   464
//+TBR
darope@217
   465
#ifdef MM_DEBUG
darope@217
   466
            FLDBG_PRINTF(( "link map is at %p", pMap ));
darope@217
   467
        
darope@217
   468
#ifdef __amd64__
darope@217
   469
            FLDBG_PRINTF(( "base addr = %lx", pMap->l_addr ));
darope@217
   470
#else
darope@217
   471
            FLDBG_PRINTF(( "base addr = %x", pMap->l_addr ));
darope@217
   472
#endif
darope@217
   473
            FLDBG_PRINTF(( "name is %s\n", pMap->l_name ));
darope@217
   474
#endif
darope@217
   475
//-TBR
darope@217
   476
			if ( check_for_engine_module(pMap->l_name) ) {
darope@217
   477
				return phdr_elfhdr( (void*)pMap->l_addr );
darope@217
   478
			}
darope@217
   479
darope@217
   480
            pMap = pMap->l_next;
darope@217
   481
        } while ( NULL != pMap );
darope@217
   482
        
darope@217
   483
    }
darope@217
   484
darope@217
   485
	return NOTFOUND;
darope@217
   486
}
darope@217
   487
darope@217
   488
void EngineInfo::set_code_range( void* _pBase, ElfW(Phdr)* _pPhdr )
darope@217
   489
{
darope@217
   490
	unsigned char* pBase = (unsigned char*)_pBase;
darope@217
   491
	m_codeStart = pBase + _pPhdr->p_vaddr;
darope@217
   492
	m_codeEnd   = pBase + _pPhdr->p_vaddr + _pPhdr->p_memsz;
darope@217
   493
darope@217
   494
    m_state = STATE_VALID;
darope@217
   495
darope@217
   496
//+TBR
darope@217
   497
#ifdef MM_DEBUG
darope@217
   498
	FLDBG_PRINTF(( "==DEBUG==> Setting code range:" ));
darope@217
   499
#ifdef __amd64__
darope@217
   500
	FLDBG_PRINTF(( "              pPhdr at %p, p_vaddr = 0x%lx, vend = 0x%lx, p_memsz = %ld",
darope@217
   501
			_pPhdr, _pPhdr->p_vaddr, _pPhdr->p_vaddr + _pPhdr->p_memsz, _pPhdr->p_memsz ));
darope@217
   502
#else
darope@217
   503
	FLDBG_PRINTF(( "              pPhdr at %p, p_vaddr = 0x%x, vend = 0x%x, p_memsz = %d",
darope@217
   504
			_pPhdr, _pPhdr->p_vaddr, _pPhdr->p_vaddr + _pPhdr->p_memsz, _pPhdr->p_memsz ));
darope@217
   505
#endif
darope@217
   506
	FLDBG_PRINTF(( "                               start address = %p, end address = %p",
darope@217
   507
			m_codeStart, m_codeEnd ));
darope@217
   508
#endif
darope@217
   509
//-TBR
darope@217
   510
}
darope@217
   511
darope@217
   512
darope@217
   513
#endif /* _WIN32 */
darope@217
   514
darope@217
   515
darope@217
   516
int EngineInfo::initialise( enginefuncs_t* _pFuncs )
darope@217
   517
{
darope@217
   518
	int ret = 0;
darope@217
   519
darope@217
   520
	// Have to do this only once.
darope@217
   521
	if ( NULL != m_codeStart ) {
darope@217
   522
//+TBR
darope@217
   523
#ifdef MM_DEBUG
darope@217
   524
		FLDBG_PRINTF(( "==DEBUG==> ---------- Engine info already initialised --" ));
darope@217
   525
		FLDBG_PRINTF(( "==DEBUG==> codeStart: %p  codeEnd: %p", m_codeStart, m_codeEnd ));
darope@217
   526
#endif
darope@217
   527
//-TBR
darope@217
   528
		return 0;
darope@217
   529
	}
darope@217
   530
//+TBR
darope@217
   531
#ifdef MM_DEBUG
darope@217
   532
	FLDBG_PRINTF(( "==DEBUG==> ---------- Initialising engine info --" ));
darope@217
   533
#endif
darope@217
   534
//-TBR
darope@217
   535
darope@217
   536
#ifdef _WIN32
darope@217
   537
darope@217
   538
    ret = nthdr_module_name();
darope@217
   539
	if ( MODULE_NAME_NOTFOUND == ret && (! _pFuncs->pfnIsDedicatedServer()) ) {
darope@217
   540
		// We could not find the engine dll by name and we are running on
darope@217
   541
		// a listen server. This usually means that that we are dealing with
darope@217
   542
		// a VAC protected engine dll. No other way than approximating the 
darope@217
   543
		// range of valid engine function pointers in this case.
darope@217
   544
		ret = vac_pe_approx( _pFuncs );
darope@217
   545
	}
darope@217
   546
darope@217
   547
#else /* _WIN32 */
darope@217
   548
darope@217
   549
	// If we have no reference pointer to start from we can only try to use
darope@217
   550
	// the r_debug symbol.
darope@217
   551
	if ( NULL == _pFuncs ) {
darope@217
   552
		ret = phdr_r_debug();
darope@217
   553
	}
darope@217
   554
darope@217
   555
	// If we have a refererence pointer we try to use it first.
darope@217
   556
	if ( 0 != phdr_dladdr(_pFuncs) ) {
darope@217
   557
		ret = phdr_r_debug();
darope@217
   558
	}
darope@217
   559
darope@217
   560
#endif /* _WIN32 */
darope@217
   561
//+TBR
darope@217
   562
#ifdef MM_DEBUG
darope@217
   563
	if ( 0 != ret ) {
darope@217
   564
		FLDBG_PRINTF(( "==DEBUG==> Unable to determine functions address range!" ));
darope@217
   565
	}
darope@217
   566
	else {
darope@217
   567
		FLDBG_PRINTF(( "==DEBUG==> codeStart: %p  codeEnd: %p", m_codeStart, m_codeEnd ));
darope@217
   568
	}
darope@217
   569
#endif
darope@217
   570
//-TBR
darope@217
   571
darope@217
   572
    return 0;
darope@217
   573
}
darope@217
   574
darope@217
   575