|
darope@250
|
1 |
|
|
darope@250
|
2 |
// vi: set ts=4 sw=4 : |
|
darope@250
|
3 |
// vim: set tw=75 : |
|
darope@250
|
4 |
|
|
darope@250
|
5 |
// engineinfo.cpp - get info about HL engine, like file used |
|
darope@250
|
6 |
// and test segment range |
|
darope@250
|
7 |
// |
|
darope@250
|
8 |
|
|
darope@250
|
9 |
|
|
darope@250
|
10 |
#ifdef _WIN32 |
|
darope@250
|
11 |
// Don't include winspool.h; clashes with SERVER_EXECUTE from engine |
|
darope@250
|
12 |
# define _WINSPOOL_H |
|
darope@250
|
13 |
# include <windows.h> |
|
darope@250
|
14 |
# include <winnt.h> // Header structures |
|
ds@267
|
15 |
#elif defined(linux) |
|
darope@250
|
16 |
# ifndef _GNU_SOURCE |
|
darope@250
|
17 |
# define _GNU_SOURCE |
|
darope@250
|
18 |
# endif |
|
darope@250
|
19 |
# include <dlfcn.h> // dladdr() |
|
darope@250
|
20 |
# include <link.h> // ElfW(Phdr/Ehdr) macros. |
|
darope@250
|
21 |
// _DYNAMIC, r_debug, link_map, etc. |
|
ds@267
|
22 |
#else |
|
ds@267
|
23 |
# include <CoreFoundation/CoreFoundation.h> |
|
ds@267
|
24 |
# include <dlfcn.h> // dladdr() |
|
ds@267
|
25 |
# include <mach-o/loader.h> // Mach-O structures |
|
ds@267
|
26 |
# include <mach-o/fat.h> // Mach-O structures for fat binaries |
|
darope@250
|
27 |
#endif /* _WIN32 */ |
|
darope@250
|
28 |
|
|
darope@250
|
29 |
#include <string.h> // strlen(), strrchr(), strcmp() |
|
darope@250
|
30 |
#include <stdio.h> // printf() |
|
darope@250
|
31 |
|
|
darope@250
|
32 |
#include "engineinfo.h" // me |
|
darope@250
|
33 |
#include "log_meta.h" // META_DEV() |
|
darope@250
|
34 |
|
|
darope@250
|
35 |
|
|
darope@250
|
36 |
// Current mask for checking engine function addresses |
|
darope@250
|
37 |
// in VAC protected engine dlls on Win32. This is gonna fail |
|
darope@250
|
38 |
// on Win64. |
|
darope@250
|
39 |
const unsigned long c_VacDllEngineFuncsRangeMask = 0xFFF00000; |
|
darope@250
|
40 |
const unsigned long c_VacDllEngineFuncsRangeMark = 0x01D00000; |
|
darope@250
|
41 |
void* const c_VacDllEngineFuncsRangeStart = (void*)0x01D00000; |
|
darope@250
|
42 |
void* const c_VacDllEngineFuncsRangeEnd = (void*)0x01E00000; |
|
darope@250
|
43 |
|
|
darope@250
|
44 |
|
|
darope@250
|
45 |
bool EngineInfo::check_for_engine_module( const char* _pName ) |
|
darope@250
|
46 |
{ |
|
darope@250
|
47 |
const char* pC; |
|
darope@250
|
48 |
size_t size; |
|
darope@250
|
49 |
|
|
darope@250
|
50 |
if ( NULL == _pName ) return false; |
|
darope@250
|
51 |
|
|
darope@250
|
52 |
#ifdef _WIN32 |
|
darope@250
|
53 |
|
|
darope@250
|
54 |
// The engine module is either sw.dll or hw.dll or swds.dll |
|
darope@250
|
55 |
pC = strrchr( _pName, '.' ); |
|
darope@250
|
56 |
|
|
darope@250
|
57 |
pC -= 2; |
|
darope@250
|
58 |
if ( 0 != strcmp(pC, "sw.dll") && 0 != strcmp(pC, "hw.dll") ) { |
|
darope@250
|
59 |
pC -= 2; |
|
darope@250
|
60 |
if ( 0 != strcmp(pC, "swds.dll") ) return false; |
|
darope@250
|
61 |
} |
|
darope@250
|
62 |
|
|
darope@250
|
63 |
HMODULE hModule = GetModuleHandle( pC ); |
|
darope@250
|
64 |
if ( NULL == hModule ) { |
|
darope@250
|
65 |
return false; |
|
darope@250
|
66 |
} |
|
darope@250
|
67 |
|
|
darope@250
|
68 |
// Ok, we found the string sw(ds).dll, thus we deduct that this is the |
|
darope@250
|
69 |
// name of the engine's shared object. We copy the string for future |
|
darope@250
|
70 |
// reference and return successfully. |
|
darope@250
|
71 |
size = 0; |
|
darope@250
|
72 |
while ( *pC != '.' && size < c_EngineInfo__typeLen-1 ) { |
|
darope@250
|
73 |
m_type[size++] = *pC++; |
|
darope@250
|
74 |
} |
|
darope@250
|
75 |
m_type[size] = '\0'; |
|
darope@250
|
76 |
|
|
ds@267
|
77 |
#elif defined(linux) |
|
darope@250
|
78 |
|
|
darope@250
|
79 |
const char* pType; |
|
darope@250
|
80 |
|
|
ds@273
|
81 |
// The engine module is either hw.so or engine_.*so |
|
ds@273
|
82 |
pC = strrchr( _pName, '.' ); |
|
ds@273
|
83 |
pC -= 2; |
|
darope@250
|
84 |
|
|
ds@273
|
85 |
// Check for listen server module first |
|
ds@273
|
86 |
if ( 0 == strcmp(pC, "hw.so") ) { |
|
ds@273
|
87 |
return true; |
|
ds@273
|
88 |
} |
|
ds@273
|
89 |
|
|
ds@273
|
90 |
// Check for engine_*.so |
|
darope@250
|
91 |
size = strlen( _pName ); |
|
darope@250
|
92 |
if ( size < 11 ) { |
|
darope@250
|
93 |
// Forget it, this string is too short to even be 'engine_.so', so |
|
darope@250
|
94 |
// it can't the name of the engine shared library. |
|
darope@250
|
95 |
return false; |
|
darope@250
|
96 |
} |
|
darope@250
|
97 |
|
|
darope@250
|
98 |
// Start parsing at the end. Since we know that the string is at least |
|
darope@250
|
99 |
// 11 characters long we can safely do our parsing without being afraid |
|
darope@250
|
100 |
// of leaving the string. |
|
darope@250
|
101 |
pC = _pName + size -1; |
|
darope@250
|
102 |
|
|
darope@250
|
103 |
// First we see if the string ends in '.so' |
|
darope@250
|
104 |
if ( *pC-- != 'o' || *pC-- != 's' || *pC-- != '.' ) return false; |
|
darope@250
|
105 |
|
|
darope@250
|
106 |
// Now find the '_' which would be the seperator between 'engine' and |
|
darope@250
|
107 |
// the architecture. |
|
darope@250
|
108 |
while ( *pC != '_' && pC != _pName ) --pC; |
|
darope@250
|
109 |
if ( pC == _pName ) return false; |
|
darope@250
|
110 |
|
|
darope@250
|
111 |
// We are at the '_', thus the architecture identifier must start at |
|
darope@250
|
112 |
// the next character. |
|
darope@250
|
113 |
pType = pC +1; |
|
darope@250
|
114 |
|
|
darope@250
|
115 |
// Now we walk further back and check if we find the string 'engine' |
|
darope@250
|
116 |
// backwards. |
|
darope@250
|
117 |
--pC; |
|
darope@250
|
118 |
if ( *pC-- != 'e' || *pC-- != 'n' || *pC-- != 'i' || |
|
darope@250
|
119 |
*pC-- != 'g' || *pC-- != 'n' || *pC != 'e' ) { |
|
darope@250
|
120 |
return false; |
|
darope@250
|
121 |
} |
|
darope@250
|
122 |
|
|
darope@250
|
123 |
// Ok, we found the string engine_*.so, thus we deduct that this is the |
|
darope@250
|
124 |
// name of the engine's shared object. We copy the architecture string |
|
darope@250
|
125 |
// for future reference and return successfully. |
|
darope@250
|
126 |
size = 0; |
|
darope@250
|
127 |
while ( *pType != '.' && size < c_EngineInfo__typeLen-1 ) { |
|
darope@250
|
128 |
m_type[size++] = *pType++; |
|
darope@250
|
129 |
} |
|
darope@250
|
130 |
m_type[size] = '\0'; |
|
darope@250
|
131 |
|
|
ds@267
|
132 |
#else |
|
ds@267
|
133 |
|
|
ds@273
|
134 |
// The engine module is either hw.dylib or engine.dylib |
|
ds@267
|
135 |
pC = strrchr( _pName, '.' ); |
|
ds@267
|
136 |
|
|
ds@273
|
137 |
pC -= 2; |
|
ds@273
|
138 |
if ( 0 != strcmp(pC, "hw.dylib") ) { |
|
ds@273
|
139 |
pC -= 4; |
|
ds@273
|
140 |
if ( 0 != strcmp(pC, "engine.dylib") ) return false; |
|
ds@267
|
141 |
} |
|
ds@267
|
142 |
|
|
ds@267
|
143 |
size = 0; |
|
ds@267
|
144 |
while ( *pC != '.' && size < c_EngineInfo__typeLen-1 ) { |
|
ds@267
|
145 |
m_type[size++] = *pC++; |
|
ds@267
|
146 |
} |
|
ds@267
|
147 |
m_type[size] = '\0'; |
|
darope@250
|
148 |
|
|
darope@250
|
149 |
#endif /* _WIN32 */ |
|
darope@250
|
150 |
|
|
darope@250
|
151 |
|
|
darope@250
|
152 |
return true; |
|
darope@250
|
153 |
} |
|
darope@250
|
154 |
|
|
darope@250
|
155 |
|
|
darope@250
|
156 |
#ifdef _WIN32 |
|
darope@250
|
157 |
|
|
darope@250
|
158 |
int EngineInfo::nthdr_module_name( void ) |
|
darope@250
|
159 |
{ |
|
darope@250
|
160 |
PIMAGE_DOS_HEADER pDosHeader; |
|
darope@250
|
161 |
PIMAGE_NT_HEADERS pNTHeader; |
|
darope@250
|
162 |
unsigned char* pBaseAddr; |
|
darope@250
|
163 |
const char* pName = "sw.dll"; |
|
darope@250
|
164 |
|
|
darope@250
|
165 |
if ( ! check_for_engine_module(pName) ) { |
|
darope@250
|
166 |
pName = "hw.dll"; |
|
darope@250
|
167 |
if ( ! check_for_engine_module(pName) ) { |
|
darope@250
|
168 |
pName = "swds.dll"; |
|
darope@250
|
169 |
if ( ! check_for_engine_module(pName) ) { |
|
darope@250
|
170 |
return MODULE_NAME_NOTFOUND; |
|
darope@250
|
171 |
} |
|
darope@250
|
172 |
} |
|
darope@250
|
173 |
} |
|
darope@250
|
174 |
|
|
darope@250
|
175 |
// Get the module handle for the engine dll we found. |
|
darope@250
|
176 |
// This is also the modules base address. |
|
darope@250
|
177 |
HMODULE hModule = GetModuleHandle( pName ); |
|
darope@250
|
178 |
|
|
darope@250
|
179 |
pBaseAddr = (unsigned char*)hModule; |
|
darope@250
|
180 |
|
|
darope@250
|
181 |
|
|
darope@250
|
182 |
// Check if we find a DOS header |
|
darope@250
|
183 |
pDosHeader = (PIMAGE_DOS_HEADER)hModule; |
|
darope@250
|
184 |
if ( pDosHeader->e_magic != IMAGE_DOS_SIGNATURE ) { |
|
darope@250
|
185 |
return INVALID_DOS_SIGN; |
|
darope@250
|
186 |
} |
|
darope@250
|
187 |
|
|
darope@250
|
188 |
// Check if we find a PE header |
|
darope@250
|
189 |
pNTHeader = (PIMAGE_NT_HEADERS)(pBaseAddr + pDosHeader->e_lfanew); |
|
darope@250
|
190 |
if ( pNTHeader->Signature != IMAGE_NT_SIGNATURE ) { |
|
darope@250
|
191 |
return INVALID_NT_SIGN; |
|
darope@250
|
192 |
} |
|
darope@250
|
193 |
|
|
darope@250
|
194 |
set_code_range( pBaseAddr, pNTHeader ); |
|
darope@250
|
195 |
return 0; |
|
darope@250
|
196 |
} |
|
darope@250
|
197 |
|
|
darope@250
|
198 |
|
|
darope@250
|
199 |
int EngineInfo::vac_pe_approx( enginefuncs_t* _pFuncs ) |
|
darope@250
|
200 |
{ |
|
darope@250
|
201 |
|
|
darope@250
|
202 |
if ( NULL == _pFuncs ) return INVALID_ARG; |
|
darope@250
|
203 |
|
|
darope@250
|
204 |
// There is really no good and easy way to do this. Right now what |
|
darope@250
|
205 |
// we do is assume that Steam listenservers will normally be |
|
darope@250
|
206 |
// up-to-date and hence have all function pointers set correctly. |
|
darope@250
|
207 |
// So we just check for some anomality and then set some approximated |
|
darope@250
|
208 |
// values. |
|
darope@250
|
209 |
|
|
darope@250
|
210 |
// The known addresses at the time of writing all start with 0x01D and |
|
darope@250
|
211 |
// this lie in the range between 0x01D00000 and 0x01E00000. What we do |
|
darope@250
|
212 |
// is check all functions pointers that are known to be good to start |
|
darope@250
|
213 |
// with 0x01D. If that is the case then we know that the loading address |
|
darope@250
|
214 |
// of the engine functions hasn't changed and we assume the above stated |
|
darope@250
|
215 |
// range to be the range of valid engine function addresses. |
|
darope@250
|
216 |
// If we find functions outside this range we can't determine a valid |
|
darope@250
|
217 |
// range and have to just give up. |
|
darope@250
|
218 |
|
|
darope@250
|
219 |
unsigned long* pengfuncs = (unsigned long*)_pFuncs; |
|
darope@250
|
220 |
unsigned int i, invals = 0; |
|
darope@250
|
221 |
for ( i = 0, pengfuncs += i; i < 140; i++, pengfuncs++ ) { |
|
darope@250
|
222 |
if ( ((*pengfuncs) & c_VacDllEngineFuncsRangeMask) != c_VacDllEngineFuncsRangeMark ) { |
|
darope@250
|
223 |
invals++; |
|
darope@250
|
224 |
break; |
|
darope@250
|
225 |
} |
|
darope@250
|
226 |
} |
|
darope@250
|
227 |
|
|
darope@250
|
228 |
if ( invals > 0 ) { |
|
darope@250
|
229 |
META_DEV( "Found %d engine functions out of range 0x%08lx. " |
|
darope@250
|
230 |
"Unable to determine valid engine code address range.", |
|
darope@250
|
231 |
invals, c_VacDllEngineFuncsRangeMark ); |
|
darope@250
|
232 |
|
|
darope@250
|
233 |
strncpy( m_type, "vacdll+?", c_EngineInfo__typeLen ); |
|
darope@250
|
234 |
m_state = STATE_INVALID; |
|
darope@250
|
235 |
return STATE_INVALID; |
|
darope@250
|
236 |
} |
|
darope@250
|
237 |
|
|
darope@250
|
238 |
m_codeStart = c_VacDllEngineFuncsRangeStart; |
|
darope@250
|
239 |
m_codeEnd = c_VacDllEngineFuncsRangeEnd; |
|
darope@250
|
240 |
|
|
darope@250
|
241 |
strncpy( m_type, "vacdll", c_EngineInfo__typeLen ); |
|
darope@250
|
242 |
|
|
darope@250
|
243 |
m_state = STATE_VALID; |
|
darope@250
|
244 |
|
|
darope@250
|
245 |
return 0; |
|
darope@250
|
246 |
} |
|
darope@250
|
247 |
|
|
darope@250
|
248 |
|
|
darope@250
|
249 |
void EngineInfo::set_code_range( unsigned char* _pBase, PIMAGE_NT_HEADERS _pNThdr ) |
|
darope@250
|
250 |
{ |
|
darope@250
|
251 |
m_codeStart = _pBase + _pNThdr->OptionalHeader.BaseOfCode; |
|
darope@250
|
252 |
m_codeEnd = _pBase + _pNThdr->OptionalHeader.BaseOfCode + _pNThdr->OptionalHeader.SizeOfCode; |
|
darope@250
|
253 |
|
|
darope@250
|
254 |
m_state = STATE_VALID; |
|
darope@250
|
255 |
|
|
darope@250
|
256 |
} |
|
darope@250
|
257 |
|
|
darope@250
|
258 |
|
|
ds@267
|
259 |
#elif defined(linux) /* _WIN32 */ |
|
darope@250
|
260 |
|
|
darope@250
|
261 |
|
|
darope@250
|
262 |
int EngineInfo::phdr_elfhdr( void* _pElfHdr ) |
|
darope@250
|
263 |
{ |
|
darope@250
|
264 |
ElfW(Ehdr)* pEhdr = (ElfW(Ehdr)*)_pElfHdr; |
|
darope@250
|
265 |
ElfW(Phdr)* pPhdr; |
|
darope@250
|
266 |
|
|
darope@250
|
267 |
if ( NULL == _pElfHdr ) return INVALID_ARG; |
|
darope@250
|
268 |
|
|
darope@250
|
269 |
if ( pEhdr->e_ident[0] == 0x7f |
|
darope@250
|
270 |
&& pEhdr->e_ident[1] == 'E' |
|
darope@250
|
271 |
&& pEhdr->e_ident[2] == 'L' |
|
darope@250
|
272 |
&& pEhdr->e_ident[3] == 'F' ) { |
|
darope@250
|
273 |
|
|
darope@250
|
274 |
// Looking good, we found an ELF signature. |
|
darope@250
|
275 |
// Let's see if the rest of the data at this address would fit an ELH header, too. |
|
darope@250
|
276 |
if ( (pEhdr->e_ident[EI_CLASS] == ELFCLASS32 //ELFW(CLASS) |
|
darope@250
|
277 |
|| pEhdr->e_ident[EI_CLASS] == ELFCLASS64) |
|
darope@250
|
278 |
|
|
darope@250
|
279 |
&& (pEhdr->e_ident[EI_DATA] == ELFDATA2LSB |
|
darope@250
|
280 |
|| pEhdr->e_ident[EI_DATA] == ELFDATA2MSB) |
|
darope@250
|
281 |
|
|
darope@250
|
282 |
&& pEhdr->e_type == ET_DYN ) { |
|
darope@250
|
283 |
|
|
darope@250
|
284 |
// Ok, we believe that this is a shared object's ELF header. |
|
darope@250
|
285 |
// Let's find the program header for the segment that includes |
|
darope@250
|
286 |
// the text section and return it. |
|
darope@250
|
287 |
|
|
darope@250
|
288 |
pPhdr = (ElfW(Phdr) *) ((char *) pEhdr + pEhdr->e_phoff); |
|
darope@250
|
289 |
for ( int i = 0; i < pEhdr->e_phnum; i++, pPhdr++ ) { |
|
darope@250
|
290 |
|
|
darope@250
|
291 |
if ( PT_LOAD == pPhdr->p_type |
|
darope@250
|
292 |
&& (pPhdr->p_flags & PF_R) |
|
darope@250
|
293 |
&& (pPhdr->p_flags & PF_X) ) { |
|
darope@250
|
294 |
|
|
darope@250
|
295 |
// The text section gets loaded and has read and |
|
darope@250
|
296 |
// execute flags set. So this must be it. |
|
darope@250
|
297 |
set_code_range( pEhdr, pPhdr ); |
|
darope@250
|
298 |
return 0; |
|
darope@250
|
299 |
} |
|
darope@250
|
300 |
} |
|
darope@250
|
301 |
} |
|
darope@250
|
302 |
} |
|
darope@250
|
303 |
|
|
darope@250
|
304 |
return HEADER_NOTFOUND; |
|
darope@250
|
305 |
} |
|
darope@250
|
306 |
|
|
darope@250
|
307 |
|
|
darope@250
|
308 |
|
|
darope@250
|
309 |
|
|
darope@250
|
310 |
int EngineInfo::phdr_dladdr( void* _pMem ) |
|
darope@250
|
311 |
{ |
|
darope@250
|
312 |
Dl_info info; |
|
darope@250
|
313 |
|
|
darope@250
|
314 |
if ( 0 != dladdr(_pMem, &info) ) { |
|
darope@250
|
315 |
// Check if this is the engine module |
|
darope@250
|
316 |
if ( check_for_engine_module(info.dli_fname) ) { |
|
darope@250
|
317 |
return phdr_elfhdr( info.dli_fbase ); |
|
darope@250
|
318 |
} |
|
darope@250
|
319 |
} |
|
darope@250
|
320 |
|
|
darope@250
|
321 |
return NOTFOUND; |
|
darope@250
|
322 |
} |
|
darope@250
|
323 |
|
|
darope@250
|
324 |
|
|
darope@250
|
325 |
int EngineInfo::phdr_r_debug( void ) |
|
darope@250
|
326 |
{ |
|
darope@250
|
327 |
ElfW(Dyn)* pDyn; |
|
darope@250
|
328 |
struct r_debug* pr_debug; |
|
darope@250
|
329 |
struct link_map* pMap; |
|
darope@250
|
330 |
|
|
darope@250
|
331 |
|
|
darope@250
|
332 |
// Search if we have a DT_DEBUG symbol in our DYNAMIC segment, which |
|
darope@250
|
333 |
// ought to be set to the r_debug structure's address. |
|
darope@250
|
334 |
for (pDyn = _DYNAMIC; pDyn->d_tag != DT_NULL; ++pDyn) { |
|
darope@250
|
335 |
if (pDyn->d_tag == DT_DEBUG) { |
|
darope@250
|
336 |
pr_debug = (struct r_debug *) pDyn->d_un.d_ptr; |
|
darope@250
|
337 |
break; |
|
darope@250
|
338 |
} |
|
darope@250
|
339 |
} |
|
darope@250
|
340 |
|
|
darope@250
|
341 |
if ( DT_NULL == pDyn->d_tag ) { |
|
darope@250
|
342 |
} |
|
darope@250
|
343 |
else if ( NULL == pr_debug ) { |
|
darope@250
|
344 |
} |
|
darope@250
|
345 |
else { |
|
darope@250
|
346 |
pMap = pr_debug->r_map; |
|
darope@250
|
347 |
|
|
darope@250
|
348 |
// Walk to the start of the list |
|
darope@250
|
349 |
while ( pMap->l_prev != NULL ) pMap = pMap->l_prev; |
|
darope@250
|
350 |
do { |
|
darope@250
|
351 |
if ( check_for_engine_module(pMap->l_name) ) { |
|
darope@250
|
352 |
return phdr_elfhdr( (void*)pMap->l_addr ); |
|
darope@250
|
353 |
} |
|
darope@250
|
354 |
|
|
darope@250
|
355 |
pMap = pMap->l_next; |
|
darope@250
|
356 |
} while ( NULL != pMap ); |
|
darope@250
|
357 |
|
|
darope@250
|
358 |
} |
|
darope@250
|
359 |
|
|
darope@250
|
360 |
return NOTFOUND; |
|
darope@250
|
361 |
} |
|
darope@250
|
362 |
|
|
darope@250
|
363 |
void EngineInfo::set_code_range( void* _pBase, ElfW(Phdr)* _pPhdr ) |
|
darope@250
|
364 |
{ |
|
darope@250
|
365 |
unsigned char* pBase = (unsigned char*)_pBase; |
|
darope@250
|
366 |
m_codeStart = pBase + _pPhdr->p_vaddr; |
|
darope@250
|
367 |
m_codeEnd = pBase + _pPhdr->p_vaddr + _pPhdr->p_memsz; |
|
darope@250
|
368 |
|
|
darope@250
|
369 |
m_state = STATE_VALID; |
|
darope@250
|
370 |
} |
|
darope@250
|
371 |
|
|
darope@250
|
372 |
|
|
ds@267
|
373 |
#else |
|
ds@267
|
374 |
|
|
ds@267
|
375 |
int EngineInfo::mhdr_dladdr( void* pMem ) |
|
ds@267
|
376 |
{ |
|
ds@267
|
377 |
Dl_info info; |
|
ds@267
|
378 |
|
|
ds@267
|
379 |
if ( 0 != dladdr(pMem, &info) ) { |
|
ds@267
|
380 |
// Check if this is the engine module |
|
ds@267
|
381 |
if ( check_for_engine_module(info.dli_fname) ) { |
|
ds@267
|
382 |
return mhdr_machohdr( info.dli_fbase ); |
|
ds@267
|
383 |
} |
|
ds@267
|
384 |
} |
|
ds@267
|
385 |
|
|
ds@267
|
386 |
return NOTFOUND; |
|
ds@267
|
387 |
} |
|
ds@267
|
388 |
|
|
ds@267
|
389 |
int EngineInfo::mhdr_machohdr( void* pMachoHdr ) |
|
ds@267
|
390 |
{ |
|
ds@267
|
391 |
fat_header* pFatHdr = (fat_header *)pMachoHdr; |
|
ds@267
|
392 |
mach_header* pHdr = (mach_header *)pMachoHdr; |
|
ds@267
|
393 |
fat_arch* pFatArchs; |
|
ds@267
|
394 |
segment_command *pSegment; |
|
ds@267
|
395 |
section *pSection; |
|
ds@267
|
396 |
uint32_t nArchs; |
|
ds@267
|
397 |
uint32_t nCmds; |
|
ds@267
|
398 |
uint32_t nSections; |
|
ds@267
|
399 |
|
|
ds@267
|
400 |
if ( NULL == pMachoHdr ) return INVALID_ARG; |
|
ds@267
|
401 |
|
|
ds@267
|
402 |
// Check for fat binary first. |
|
ds@267
|
403 |
// Note that fat_header and fat_arch are always stored in big-endian. |
|
ds@267
|
404 |
if ( CFSwapInt32BigToHost( pFatHdr->magic ) == FAT_MAGIC ) { |
|
ds@267
|
405 |
nArchs = CFSwapInt32BigToHost( pFatHdr->nfat_arch ); |
|
ds@267
|
406 |
pFatArchs = (fat_arch *) ((char *) pMachoHdr + sizeof(fat_header)); |
|
ds@267
|
407 |
|
|
ds@267
|
408 |
// Look for the 32-bit Mach-O header |
|
ds@267
|
409 |
for (uint32_t i = 0; i < nArchs; i++) { |
|
ds@267
|
410 |
if ( CFSwapInt32BigToHost( pFatArchs[i].cputype ) == CPU_TYPE_I386 |
|
ds@267
|
411 |
&& CFSwapInt32BigToHost ( pFatArchs[i].cpusubtype ) == CPU_SUBTYPE_I386_ALL ) { |
|
ds@267
|
412 |
|
|
ds@267
|
413 |
pHdr = (mach_header *) ((char *) pMachoHdr + CFSwapInt32BigToHost( pFatArchs[i].offset )); |
|
ds@267
|
414 |
break; |
|
ds@267
|
415 |
} |
|
ds@267
|
416 |
} |
|
ds@267
|
417 |
|
|
ds@267
|
418 |
} |
|
ds@267
|
419 |
|
|
ds@267
|
420 |
// Find __TEXT segment and __text section |
|
ds@267
|
421 |
if ( pHdr->magic == MH_MAGIC |
|
ds@267
|
422 |
&& pHdr->filetype == MH_DYLIB ) { |
|
ds@267
|
423 |
|
|
ds@267
|
424 |
nCmds = pHdr->ncmds; |
|
ds@267
|
425 |
pSegment = (segment_command *) ((char *) pHdr + sizeof(mach_header)); |
|
ds@267
|
426 |
|
|
ds@267
|
427 |
for (uint32_t i = 0; i < nCmds; i++) { |
|
ds@267
|
428 |
|
|
ds@267
|
429 |
if ( pSegment->cmd == LC_SEGMENT |
|
ds@267
|
430 |
&& strcmp( pSegment->segname, "__TEXT" ) == 0 |
|
ds@267
|
431 |
&& pSegment->initprot == (VM_PROT_READ | VM_PROT_EXECUTE) ) { |
|
ds@267
|
432 |
|
|
ds@267
|
433 |
nSections = pSegment->nsects; |
|
ds@267
|
434 |
pSection = (section *) ((char *) pSegment + sizeof(segment_command)); |
|
ds@267
|
435 |
|
|
ds@267
|
436 |
for (uint32_t j = 0; j < nSections; j++) { |
|
ds@267
|
437 |
if ( strcmp( pSection->sectname, "__text" ) == 0 ) { |
|
ds@267
|
438 |
set_code_range( pHdr, pSection ); |
|
ds@267
|
439 |
return 0; |
|
ds@267
|
440 |
} |
|
ds@267
|
441 |
} |
|
ds@267
|
442 |
} |
|
ds@267
|
443 |
|
|
ds@267
|
444 |
pSegment = (segment_command *) ((char *) pSegment + pSegment->cmdsize); |
|
ds@267
|
445 |
|
|
ds@267
|
446 |
} |
|
ds@267
|
447 |
|
|
ds@267
|
448 |
} |
|
ds@267
|
449 |
|
|
ds@267
|
450 |
return HEADER_NOTFOUND; |
|
ds@267
|
451 |
} |
|
ds@267
|
452 |
|
|
ds@267
|
453 |
void EngineInfo::set_code_range( void* _pBase, section* pCodeSection ) |
|
ds@267
|
454 |
{ |
|
ds@267
|
455 |
unsigned char* pBase = (unsigned char*)_pBase; |
|
ds@267
|
456 |
|
|
ds@267
|
457 |
m_codeStart = pBase + pCodeSection->addr; |
|
ds@267
|
458 |
m_codeEnd = pBase + pCodeSection->addr + pCodeSection->size - 1; |
|
ds@267
|
459 |
m_state = STATE_VALID; |
|
ds@267
|
460 |
} |
|
ds@267
|
461 |
|
|
ds@267
|
462 |
|
|
darope@250
|
463 |
#endif /* _WIN32 */ |
|
darope@250
|
464 |
|
|
darope@250
|
465 |
|
|
darope@250
|
466 |
int EngineInfo::initialise( enginefuncs_t* _pFuncs ) |
|
darope@250
|
467 |
{ |
|
darope@250
|
468 |
int ret = 0; |
|
darope@250
|
469 |
|
|
darope@250
|
470 |
// Have to do this only once. |
|
darope@250
|
471 |
if ( NULL != m_codeStart ) { |
|
darope@250
|
472 |
return 0; |
|
darope@250
|
473 |
} |
|
darope@250
|
474 |
|
|
darope@250
|
475 |
#ifdef _WIN32 |
|
darope@250
|
476 |
|
|
darope@250
|
477 |
ret = nthdr_module_name(); |
|
darope@250
|
478 |
if ( MODULE_NAME_NOTFOUND == ret && (! _pFuncs->pfnIsDedicatedServer()) ) { |
|
darope@250
|
479 |
// We could not find the engine dll by name and we are running on |
|
darope@250
|
480 |
// a listen server. This usually means that that we are dealing with |
|
darope@250
|
481 |
// a VAC protected engine dll. No other way than approximating the |
|
darope@250
|
482 |
// range of valid engine function pointers in this case. |
|
darope@250
|
483 |
ret = vac_pe_approx( _pFuncs ); |
|
darope@250
|
484 |
} |
|
darope@250
|
485 |
|
|
ds@267
|
486 |
#elif defined(linux) /* _WIN32 */ |
|
ds@267
|
487 |
|
|
darope@250
|
488 |
// If we have no reference pointer to start from we can only try to use |
|
darope@250
|
489 |
// the r_debug symbol. |
|
darope@250
|
490 |
if ( NULL == _pFuncs ) { |
|
darope@250
|
491 |
ret = phdr_r_debug(); |
|
darope@250
|
492 |
} |
|
darope@250
|
493 |
|
|
darope@250
|
494 |
// If we have a refererence pointer we try to use it first. |
|
darope@250
|
495 |
if ( 0 != phdr_dladdr(_pFuncs) ) { |
|
darope@250
|
496 |
ret = phdr_r_debug(); |
|
darope@250
|
497 |
} |
|
darope@250
|
498 |
|
|
ds@267
|
499 |
#else |
|
ds@267
|
500 |
|
|
ds@267
|
501 |
ret = mhdr_dladdr(_pFuncs); |
|
ds@267
|
502 |
|
|
darope@250
|
503 |
#endif /* _WIN32 */ |
|
darope@250
|
504 |
|
|
darope@250
|
505 |
if ( 0 != ret ) { |
|
darope@250
|
506 |
META_DEV( "Unable to determine engine code address range!" ); |
|
darope@250
|
507 |
} |
|
darope@250
|
508 |
else { |
|
darope@250
|
509 |
META_DEV( "Set engine code range: start address = %p, end address = %p", |
|
darope@250
|
510 |
m_codeStart, m_codeEnd ); |
|
darope@250
|
511 |
} |
|
darope@250
|
512 |
|
|
darope@250
|
513 |
return 0; |
|
darope@250
|
514 |
} |
|
darope@250
|
515 |
|
|
darope@250
|
516 |
|