core/MemoryUtils.cpp
changeset 700 2b715374ce25
child 701 85c77b23ae47
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/MemoryUtils.cpp	Fri Apr 20 18:32:33 2007 +0000
     1.3 @@ -0,0 +1,199 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.8 + * ===============================================================
     1.9 + *
    1.10 + * This file is not open source and may not be copied without explicit
    1.11 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.12 + * in whole or significant part.
    1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.14 + *
    1.15 + * Version: $Id$
    1.16 + */
    1.17 +
    1.18 +#include "MemoryUtils.h"
    1.19 +#include "sm_platform.h"
    1.20 +#ifdef PLATFORM_LINUX
    1.21 +#include <string.h>
    1.22 +#include <elf.h>
    1.23 +#endif
    1.24 +
    1.25 +MemoryUtils g_MemUtils;
    1.26 +
    1.27 +MemoryUtils::MemoryUtils()
    1.28 +{
    1.29 +#ifdef PLATFORM_WINDOWS
    1.30 +
    1.31 +	/* This might be used in the future? */
    1.32 +	#if 0
    1.33 +	SYSTEM_INFO info;
    1.34 +	GetSystemInfo(&info);
    1.35 +
    1.36 +	m_PageSize = info.dwPageSize;
    1.37 +	#endif
    1.38 +
    1.39 +#elif defined PLATFORM_POSIX
    1.40 +
    1.41 +	m_PageSize = sysconf(_SC_PAGE_SIZE);
    1.42 +
    1.43 +#endif
    1.44 +}
    1.45 +
    1.46 +void *MemoryUtils::FindPattern(void *libPtr, const char *pattern, size_t len)
    1.47 +{
    1.48 +	DynLibInfo lib;
    1.49 +	bool found;
    1.50 +	char *ptr, *end;
    1.51 +
    1.52 +	memset(&lib, 0, sizeof(DynLibInfo));
    1.53 +
    1.54 +	if (!GetLibraryInfo(libPtr, lib))
    1.55 +	{
    1.56 +		return NULL;
    1.57 +	}
    1.58 +
    1.59 +	ptr = reinterpret_cast<char *>(lib.baseAddress);
    1.60 +	end = ptr + lib.memorySize;
    1.61 +
    1.62 +	while (ptr < end)
    1.63 +	{
    1.64 +		found = true;
    1.65 +		for (register size_t i = 0; i < len; i++)
    1.66 +		{
    1.67 +			if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
    1.68 +			{
    1.69 +				found = false;
    1.70 +				break;
    1.71 +			}
    1.72 +		}
    1.73 +
    1.74 +		if (found)
    1.75 +			return ptr;
    1.76 +
    1.77 +		ptr++;
    1.78 +	}
    1.79 +
    1.80 +	return NULL;
    1.81 +}
    1.82 +
    1.83 +bool MemoryUtils::GetLibraryInfo(void *libPtr, DynLibInfo &lib)
    1.84 +{
    1.85 +	unsigned long baseAddr;
    1.86 +
    1.87 +	if (libPtr == NULL)
    1.88 +	{
    1.89 +		return false;
    1.90 +	}
    1.91 +
    1.92 +#ifdef PLATFORM_WINDOWS
    1.93 +
    1.94 +	MEMORY_BASIC_INFORMATION info;
    1.95 +	IMAGE_DOS_HEADER *dos;
    1.96 +	IMAGE_NT_HEADERS *pe;
    1.97 +	IMAGE_FILE_HEADER *file;
    1.98 +	IMAGE_OPTIONAL_HEADER *opt;
    1.99 +
   1.100 +	if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
   1.101 +	{
   1.102 +		return false;
   1.103 +	}
   1.104 +
   1.105 +	baseAddr = reinterpret_cast<unsigned long>(info.AllocationBase);
   1.106 +
   1.107 +	/* All this is for our insane sanity checks :o */
   1.108 +	dos = reinterpret_cast<IMAGE_DOS_HEADER *>(baseAddr);
   1.109 +	pe = reinterpret_cast<IMAGE_NT_HEADERS *>(baseAddr + dos->e_lfanew);
   1.110 +	file = &pe->FileHeader;
   1.111 +	opt = &pe->OptionalHeader;
   1.112 +
   1.113 +	/* Check PE magic and signature */
   1.114 +	if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
   1.115 +	{
   1.116 +		return false;
   1.117 +	}
   1.118 +
   1.119 +	/* Check architecture, which is 32-bit/x86 right now
   1.120 +	 * Should change this for 64-bit if Valve gets their act together
   1.121 +	 */
   1.122 +	if (file->Machine != IMAGE_FILE_MACHINE_I386)
   1.123 +	{
   1.124 +		return false;
   1.125 +	}
   1.126 +
   1.127 +	/* For our purposes, this must be a dynamic library */
   1.128 +	if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
   1.129 +	{
   1.130 +		return false;
   1.131 +	}
   1.132 +
   1.133 +	/* Finally, we can do this */
   1.134 +	lib.memorySize = opt->SizeOfImage;
   1.135 +
   1.136 +#elif defined PLATFORM_LINUX
   1.137 +
   1.138 +	Dl_info info;
   1.139 +	Elf32_Ehdr *file;
   1.140 +	Elf32_Phdr *phdr;
   1.141 +	uint16_t phdrCount;
   1.142 +
   1.143 +	if (!dladdr(libPtr, &info))
   1.144 +	{
   1.145 +		return false;
   1.146 +	}
   1.147 +
   1.148 +	if (!info.dli_fbase || !info.dli_fname)
   1.149 +	{
   1.150 +		return false;
   1.151 +	}
   1.152 +
   1.153 +	/* This is for our insane sanity checks :o */
   1.154 +	baseAddr = reinterpret_cast<unsigned long>(info.dli_fbase);
   1.155 +	file = reinterpret_cast<Elf32_Ehdr *>(baseAddr);
   1.156 +
   1.157 +	/* Check ELF magic */
   1.158 +	if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
   1.159 +	{
   1.160 +		return false;
   1.161 +	}
   1.162 +
   1.163 +	/* Check ELF version */
   1.164 +	if (file->e_ident[EI_VERSION] != EV_CURRENT)
   1.165 +	{
   1.166 +		return false;
   1.167 +	}
   1.168 +
   1.169 +	/* Check ELF architecture, which is 32-bit/x86 right now
   1.170 +	 * Should change this for 64-bit if Valve gets their act together
   1.171 +	 */
   1.172 +	if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
   1.173 +	{
   1.174 +		return false;
   1.175 +	}
   1.176 +
   1.177 +	/* For our purposes, this must be a dynamic library/shared object */
   1.178 +	if (file->e_type != ET_DYN)
   1.179 +	{
   1.180 +		return false;
   1.181 +	}
   1.182 +
   1.183 +	phdrCount = file->e_phnum;
   1.184 +	phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
   1.185 +	
   1.186 +	/* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */
   1.187 +	for (uint16_t i = 0; i < phdrCount; i++)
   1.188 +	{
   1.189 +		Elf32_Phdr &hdr = phdr[i];
   1.190 +
   1.191 +		if (hdr.p_type == PT_LOAD)
   1.192 +		{
   1.193 +			lib.memorySize += hdr.p_memsz;
   1.194 +		}
   1.195 +	}
   1.196 +
   1.197 +#endif
   1.198 +
   1.199 +	lib.baseAddress = reinterpret_cast<void *>(baseAddr);
   1.200 +
   1.201 +	return true;
   1.202 +}