3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "MemoryUtils.h"
16 #include "sm_platform.h"
22 MemoryUtils g_MemUtils;
24 MemoryUtils::MemoryUtils()
26 #ifdef PLATFORM_WINDOWS
28 /* This might be used in the future? */
33 m_PageSize = info.dwPageSize;
36 #elif defined PLATFORM_POSIX
38 m_PageSize = sysconf(_SC_PAGE_SIZE);
43 void *MemoryUtils::FindPattern(void *libPtr, const char *pattern, size_t len)
49 memset(&lib, 0, sizeof(DynLibInfo));
51 if (!GetLibraryInfo(libPtr, lib))
56 ptr = reinterpret_cast<char *>(lib.baseAddress);
57 end = ptr + lib.memorySize;
62 for (register size_t i = 0; i < len; i++)
64 if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
80 bool MemoryUtils::GetLibraryInfo(void *libPtr, DynLibInfo &lib)
82 unsigned long baseAddr;
89 #ifdef PLATFORM_WINDOWS
91 MEMORY_BASIC_INFORMATION info;
92 IMAGE_DOS_HEADER *dos;
94 IMAGE_FILE_HEADER *file;
95 IMAGE_OPTIONAL_HEADER *opt;
97 if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
102 baseAddr = reinterpret_cast<unsigned long>(info.AllocationBase);
104 /* All this is for our insane sanity checks :o */
105 dos = reinterpret_cast<IMAGE_DOS_HEADER *>(baseAddr);
106 pe = reinterpret_cast<IMAGE_NT_HEADERS *>(baseAddr + dos->e_lfanew);
107 file = &pe->FileHeader;
108 opt = &pe->OptionalHeader;
110 /* Check PE magic and signature */
111 if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
116 /* Check architecture, which is 32-bit/x86 right now
117 * Should change this for 64-bit if Valve gets their act together
119 if (file->Machine != IMAGE_FILE_MACHINE_I386)
124 /* For our purposes, this must be a dynamic library */
125 if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
130 /* Finally, we can do this */
131 lib.memorySize = opt->SizeOfImage;
133 #elif defined PLATFORM_LINUX
140 if (!dladdr(libPtr, &info))
145 if (!info.dli_fbase || !info.dli_fname)
150 /* This is for our insane sanity checks :o */
151 baseAddr = reinterpret_cast<unsigned long>(info.dli_fbase);
152 file = reinterpret_cast<Elf32_Ehdr *>(baseAddr);
154 /* Check ELF magic */
155 if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
160 /* Check ELF version */
161 if (file->e_ident[EI_VERSION] != EV_CURRENT)
166 /* Check ELF architecture, which is 32-bit/x86 right now
167 * Should change this for 64-bit if Valve gets their act together
169 if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
174 /* For our purposes, this must be a dynamic library/shared object */
175 if (file->e_type != ET_DYN)
180 phdrCount = file->e_phnum;
181 phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
183 /* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */
184 for (uint16_t i = 0; i < phdrCount; i++)
186 Elf32_Phdr &hdr = phdr[i];
188 if (hdr.p_type == PT_LOAD)
190 lib.memorySize += hdr.p_memsz;
196 lib.baseAddress = reinterpret_cast<void *>(baseAddr);