ds@700: /** ds@700: * vim: set ts=4 : ds@1330: * ============================================================================= dvander@1231: * SourceMod ds@1947: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1330: * ============================================================================= ds@700: * ds@1330: * This program is free software; you can redistribute it and/or modify it under ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the ds@1330: * Free Software Foundation. dvander@1231: * ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1330: * details. dvander@1231: * ds@1330: * You should have received a copy of the GNU General Public License along with ds@1330: * this program. If not, see . dvander@1231: * ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1330: * this exception to all derivative works. AlliedModders LLC defines further ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1330: * or . ds@700: * ds@700: * Version: $Id$ ds@700: */ ds@700: ds@700: #include "MemoryUtils.h" ds@702: #include "ShareSys.h" ds@700: #ifdef PLATFORM_LINUX ds@700: #include ds@700: #include ds@700: #endif ds@700: ds@700: MemoryUtils g_MemUtils; ds@700: ds@701: #if 0 ds@700: MemoryUtils::MemoryUtils() ds@700: { ds@700: #ifdef PLATFORM_WINDOWS ds@700: ds@700: SYSTEM_INFO info; ds@700: GetSystemInfo(&info); ds@700: ds@700: m_PageSize = info.dwPageSize; ds@700: ds@700: #elif defined PLATFORM_POSIX ds@700: ds@700: m_PageSize = sysconf(_SC_PAGE_SIZE); ds@700: ds@700: #endif ds@700: } ds@701: #endif ds@700: ds@702: void MemoryUtils::OnSourceModAllInitialized() ds@702: { ds@702: g_ShareSys.AddInterface(NULL, this); ds@702: } ds@702: ds@701: void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len) ds@700: { ds@700: DynLibInfo lib; ds@700: bool found; ds@700: char *ptr, *end; ds@700: ds@700: memset(&lib, 0, sizeof(DynLibInfo)); ds@700: ds@700: if (!GetLibraryInfo(libPtr, lib)) ds@700: { ds@700: return NULL; ds@700: } ds@700: ds@700: ptr = reinterpret_cast(lib.baseAddress); ds@700: end = ptr + lib.memorySize; ds@700: ds@700: while (ptr < end) ds@700: { ds@700: found = true; ds@700: for (register size_t i = 0; i < len; i++) ds@700: { ds@700: if (pattern[i] != '\x2A' && pattern[i] != ptr[i]) ds@700: { ds@700: found = false; ds@700: break; ds@700: } ds@700: } ds@700: ds@700: if (found) ds@700: return ptr; ds@700: ds@700: ptr++; ds@700: } ds@700: ds@700: return NULL; ds@700: } ds@700: ds@701: bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib) ds@700: { ds@700: unsigned long baseAddr; ds@700: ds@700: if (libPtr == NULL) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: #ifdef PLATFORM_WINDOWS ds@700: ds@700: MEMORY_BASIC_INFORMATION info; ds@700: IMAGE_DOS_HEADER *dos; ds@700: IMAGE_NT_HEADERS *pe; ds@700: IMAGE_FILE_HEADER *file; ds@700: IMAGE_OPTIONAL_HEADER *opt; ds@700: ds@700: if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION))) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: baseAddr = reinterpret_cast(info.AllocationBase); ds@700: ds@700: /* All this is for our insane sanity checks :o */ ds@700: dos = reinterpret_cast(baseAddr); ds@700: pe = reinterpret_cast(baseAddr + dos->e_lfanew); ds@700: file = &pe->FileHeader; ds@700: opt = &pe->OptionalHeader; ds@700: ds@700: /* Check PE magic and signature */ ds@700: if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: /* Check architecture, which is 32-bit/x86 right now ds@700: * Should change this for 64-bit if Valve gets their act together ds@700: */ ds@700: if (file->Machine != IMAGE_FILE_MACHINE_I386) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: /* For our purposes, this must be a dynamic library */ ds@700: if ((file->Characteristics & IMAGE_FILE_DLL) == 0) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: /* Finally, we can do this */ ds@700: lib.memorySize = opt->SizeOfImage; ds@700: ds@700: #elif defined PLATFORM_LINUX ds@700: ds@700: Dl_info info; ds@700: Elf32_Ehdr *file; ds@700: Elf32_Phdr *phdr; ds@700: uint16_t phdrCount; ds@700: ds@700: if (!dladdr(libPtr, &info)) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: if (!info.dli_fbase || !info.dli_fname) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: /* This is for our insane sanity checks :o */ ds@700: baseAddr = reinterpret_cast(info.dli_fbase); ds@700: file = reinterpret_cast(baseAddr); ds@700: ds@700: /* Check ELF magic */ ds@700: if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: /* Check ELF version */ ds@700: if (file->e_ident[EI_VERSION] != EV_CURRENT) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: /* Check ELF architecture, which is 32-bit/x86 right now ds@700: * Should change this for 64-bit if Valve gets their act together ds@700: */ ds@700: if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: /* For our purposes, this must be a dynamic library/shared object */ ds@700: if (file->e_type != ET_DYN) ds@700: { ds@700: return false; ds@700: } ds@700: ds@700: phdrCount = file->e_phnum; ds@700: phdr = reinterpret_cast(baseAddr + file->e_phoff); ds@700: ds@700: /* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */ ds@700: for (uint16_t i = 0; i < phdrCount; i++) ds@700: { ds@700: Elf32_Phdr &hdr = phdr[i]; ds@700: ds@700: if (hdr.p_type == PT_LOAD) ds@700: { ds@700: lib.memorySize += hdr.p_memsz; ds@700: } ds@700: } ds@700: ds@700: #endif ds@700: ds@700: lib.baseAddress = reinterpret_cast(baseAddr); ds@700: ds@700: return true; ds@700: }