Merge.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "MemoryUtils.h"
39 MemoryUtils g_MemUtils;
42 MemoryUtils::MemoryUtils()
44 #ifdef PLATFORM_WINDOWS
49 m_PageSize = info.dwPageSize;
51 #elif defined PLATFORM_POSIX
53 m_PageSize = sysconf(_SC_PAGE_SIZE);
59 void MemoryUtils::OnSourceModAllInitialized()
61 g_ShareSys.AddInterface(NULL, this);
64 void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
70 memset(&lib, 0, sizeof(DynLibInfo));
72 if (!GetLibraryInfo(libPtr, lib))
77 ptr = reinterpret_cast<char *>(lib.baseAddress);
78 end = ptr + lib.memorySize;
83 for (register size_t i = 0; i < len; i++)
85 if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
101 bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
103 unsigned long baseAddr;
110 #ifdef PLATFORM_WINDOWS
112 MEMORY_BASIC_INFORMATION info;
113 IMAGE_DOS_HEADER *dos;
114 IMAGE_NT_HEADERS *pe;
115 IMAGE_FILE_HEADER *file;
116 IMAGE_OPTIONAL_HEADER *opt;
118 if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
123 baseAddr = reinterpret_cast<unsigned long>(info.AllocationBase);
125 /* All this is for our insane sanity checks :o */
126 dos = reinterpret_cast<IMAGE_DOS_HEADER *>(baseAddr);
127 pe = reinterpret_cast<IMAGE_NT_HEADERS *>(baseAddr + dos->e_lfanew);
128 file = &pe->FileHeader;
129 opt = &pe->OptionalHeader;
131 /* Check PE magic and signature */
132 if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
137 /* Check architecture, which is 32-bit/x86 right now
138 * Should change this for 64-bit if Valve gets their act together
140 if (file->Machine != IMAGE_FILE_MACHINE_I386)
145 /* For our purposes, this must be a dynamic library */
146 if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
151 /* Finally, we can do this */
152 lib.memorySize = opt->SizeOfImage;
154 #elif defined PLATFORM_LINUX
161 if (!dladdr(libPtr, &info))
166 if (!info.dli_fbase || !info.dli_fname)
171 /* This is for our insane sanity checks :o */
172 baseAddr = reinterpret_cast<unsigned long>(info.dli_fbase);
173 file = reinterpret_cast<Elf32_Ehdr *>(baseAddr);
175 /* Check ELF magic */
176 if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
181 /* Check ELF version */
182 if (file->e_ident[EI_VERSION] != EV_CURRENT)
187 /* Check ELF architecture, which is 32-bit/x86 right now
188 * Should change this for 64-bit if Valve gets their act together
190 if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
195 /* For our purposes, this must be a dynamic library/shared object */
196 if (file->e_type != ET_DYN)
201 phdrCount = file->e_phnum;
202 phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
204 /* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */
205 for (uint16_t i = 0; i < phdrCount; i++)
207 Elf32_Phdr &hdr = phdr[i];
209 if (hdr.p_type == PT_LOAD)
211 lib.memorySize += hdr.p_memsz;
217 lib.baseAddress = reinterpret_cast<void *>(baseAddr);