metamod/engineinfo.cpp
changeset 267 b75724ad6b58
parent 250 1f5fb7d1d9b3
child 273 93300395e5c6
     1.1 --- a/metamod/engineinfo.cpp	Tue Feb 12 21:07:51 2013 -0600
     1.2 +++ b/metamod/engineinfo.cpp	Tue Feb 12 21:08:19 2013 -0600
     1.3 @@ -12,13 +12,18 @@
     1.4  #  define _WINSPOOL_H	
     1.5  #  include <windows.h>
     1.6  #  include <winnt.h>     // Header structures
     1.7 -#else 
     1.8 +#elif defined(linux) 
     1.9  #  ifndef _GNU_SOURCE
    1.10  #    define _GNU_SOURCE
    1.11  #  endif
    1.12  #  include <dlfcn.h>			// dladdr()
    1.13  #  include <link.h>				// ElfW(Phdr/Ehdr) macros.
    1.14                    				// _DYNAMIC, r_debug, link_map, etc.
    1.15 +#else
    1.16 +# include <CoreFoundation/CoreFoundation.h>
    1.17 +# include <dlfcn.h>				// dladdr()
    1.18 +# include <mach-o/loader.h>		// Mach-O structures
    1.19 +# include <mach-o/fat.h>		// Mach-O structures for fat binaries
    1.20  #endif /* _WIN32 */ 
    1.21  
    1.22  #include <string.h>				// strlen(), strrchr(), strcmp()
    1.23 @@ -69,7 +74,7 @@
    1.24  	}
    1.25  	m_type[size] = '\0';
    1.26  
    1.27 -#else /* _WIN32 */
    1.28 +#elif defined(linux)
    1.29  
    1.30  	const char* pType;
    1.31  
    1.32 @@ -115,6 +120,21 @@
    1.33  	}
    1.34  	m_type[size] = '\0';
    1.35  
    1.36 +#else
    1.37 +
    1.38 +	// The engine module is engine.dylib
    1.39 +	pC = strrchr( _pName, '.' );
    1.40 +
    1.41 +	pC -= 6;
    1.42 +	if ( 0 != strcmp( pC, "engine.dylib" ) ) {
    1.43 +		return false;
    1.44 +	}
    1.45 +
    1.46 +	size = 0;
    1.47 +	while ( *pC != '.' && size < c_EngineInfo__typeLen-1 ) {
    1.48 +		m_type[size++] = *pC++;
    1.49 +	}
    1.50 +	m_type[size] = '\0';
    1.51  
    1.52  #endif /* _WIN32 */
    1.53  
    1.54 @@ -226,7 +246,7 @@
    1.55  }
    1.56  
    1.57  
    1.58 -#else /* _WIN32 */
    1.59 +#elif defined(linux) /* _WIN32 */
    1.60  
    1.61  
    1.62  int EngineInfo::phdr_elfhdr( void* _pElfHdr )
    1.63 @@ -340,6 +360,96 @@
    1.64  }
    1.65  
    1.66  
    1.67 +#else
    1.68 +
    1.69 +int EngineInfo::mhdr_dladdr( void* pMem )
    1.70 +{
    1.71 +	Dl_info info;
    1.72 +
    1.73 +	if ( 0 != dladdr(pMem, &info) ) {
    1.74 +		// Check if this is the engine module
    1.75 +		if ( check_for_engine_module(info.dli_fname) ) {
    1.76 +			return mhdr_machohdr( info.dli_fbase );
    1.77 +		}
    1.78 +	}
    1.79 +
    1.80 +	return NOTFOUND;
    1.81 +}
    1.82 +
    1.83 +int EngineInfo::mhdr_machohdr( void* pMachoHdr )
    1.84 +{
    1.85 +	fat_header* pFatHdr = (fat_header *)pMachoHdr;
    1.86 +	mach_header* pHdr = (mach_header *)pMachoHdr;
    1.87 +	fat_arch* pFatArchs;
    1.88 +	segment_command *pSegment;
    1.89 +	section *pSection;
    1.90 +	uint32_t nArchs;
    1.91 +	uint32_t nCmds;
    1.92 +	uint32_t nSections;
    1.93 +
    1.94 +	if ( NULL == pMachoHdr ) return INVALID_ARG;
    1.95 +
    1.96 +	// Check for fat binary first.
    1.97 +	// Note that fat_header and fat_arch are always stored in big-endian.
    1.98 +	if ( CFSwapInt32BigToHost( pFatHdr->magic ) == FAT_MAGIC ) {
    1.99 +		nArchs = CFSwapInt32BigToHost( pFatHdr->nfat_arch );
   1.100 +		pFatArchs = (fat_arch *) ((char *) pMachoHdr + sizeof(fat_header));
   1.101 +
   1.102 +		// Look for the 32-bit Mach-O header
   1.103 +		for (uint32_t i = 0; i < nArchs; i++) {
   1.104 +			if ( CFSwapInt32BigToHost( pFatArchs[i].cputype ) == CPU_TYPE_I386
   1.105 +				&& CFSwapInt32BigToHost ( pFatArchs[i].cpusubtype ) == CPU_SUBTYPE_I386_ALL ) {
   1.106 +
   1.107 +				pHdr = (mach_header *) ((char *) pMachoHdr + CFSwapInt32BigToHost( pFatArchs[i].offset ));
   1.108 +				break;
   1.109 +			}
   1.110 +		}
   1.111 +
   1.112 +	}
   1.113 +
   1.114 +	// Find __TEXT segment and __text section
   1.115 +	if ( pHdr->magic == MH_MAGIC
   1.116 +		&& pHdr->filetype == MH_DYLIB ) {
   1.117 +
   1.118 +		nCmds = pHdr->ncmds;
   1.119 +		pSegment = (segment_command *) ((char *) pHdr + sizeof(mach_header));
   1.120 +
   1.121 +		for (uint32_t i = 0; i < nCmds; i++) {
   1.122 +
   1.123 +			if ( pSegment->cmd == LC_SEGMENT
   1.124 +				&& strcmp( pSegment->segname, "__TEXT" ) == 0
   1.125 +				&& pSegment->initprot == (VM_PROT_READ | VM_PROT_EXECUTE) ) {
   1.126 +
   1.127 +				nSections = pSegment->nsects;
   1.128 +				pSection = (section *) ((char *) pSegment + sizeof(segment_command));
   1.129 +
   1.130 +				for (uint32_t j = 0; j < nSections; j++) {
   1.131 +					if ( strcmp( pSection->sectname, "__text" ) == 0 ) {
   1.132 +						set_code_range( pHdr, pSection );
   1.133 +						return 0;
   1.134 +					}
   1.135 +				}
   1.136 +			}
   1.137 +
   1.138 +			pSegment = (segment_command *) ((char *) pSegment + pSegment->cmdsize);
   1.139 +
   1.140 +		}
   1.141 +
   1.142 +	}
   1.143 +
   1.144 +	return HEADER_NOTFOUND;
   1.145 +}
   1.146 +
   1.147 +void EngineInfo::set_code_range( void* _pBase, section* pCodeSection )	
   1.148 +{
   1.149 +	unsigned char* pBase = (unsigned char*)_pBase;
   1.150 +
   1.151 +	m_codeStart = pBase + pCodeSection->addr;
   1.152 +	m_codeEnd = pBase + pCodeSection->addr + pCodeSection->size - 1;
   1.153 +	m_state = STATE_VALID;
   1.154 +}
   1.155 +
   1.156 +
   1.157  #endif /* _WIN32 */
   1.158  
   1.159  
   1.160 @@ -363,8 +473,8 @@
   1.161  		ret = vac_pe_approx( _pFuncs );
   1.162  	}
   1.163  
   1.164 -#else /* _WIN32 */
   1.165 -
   1.166 +#elif defined(linux) /* _WIN32 */
   1.167 +	
   1.168  	// If we have no reference pointer to start from we can only try to use
   1.169  	// the r_debug symbol.
   1.170  	if ( NULL == _pFuncs ) {
   1.171 @@ -376,6 +486,10 @@
   1.172  		ret = phdr_r_debug();
   1.173  	}
   1.174  
   1.175 +#else
   1.176 +
   1.177 +	ret = mhdr_dladdr(_pFuncs);	
   1.178 +
   1.179  #endif /* _WIN32 */
   1.180  
   1.181  	if ( 0 != ret ) {