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