tools/gdc-psyfork/MemoryUtils.h
author Nicholas Hastings <skamonkey@gmail.com>
Sun May 27 11:36:49 2012 -0400 (2012-05-27)
changeset 3513 50b5b98b3400
child 3574 b51285ca203f
permissions -rw-r--r--
Added "psyfork" of gdc and example scripts to tools/ (NPOTB).
skamonkey@3513
     1
/**
skamonkey@3513
     2
 * vim: set ts=4 sw=4 tw=99 noet :
skamonkey@3513
     3
 * =============================================================================
skamonkey@3513
     4
 * SourceMod
skamonkey@3513
     5
 * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
skamonkey@3513
     6
 * =============================================================================
skamonkey@3513
     7
 *
skamonkey@3513
     8
 * This program is free software; you can redistribute it and/or modify it under
skamonkey@3513
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
skamonkey@3513
    10
 * Free Software Foundation.
skamonkey@3513
    11
 * 
skamonkey@3513
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
skamonkey@3513
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
skamonkey@3513
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
skamonkey@3513
    15
 * details.
skamonkey@3513
    16
 *
skamonkey@3513
    17
 * You should have received a copy of the GNU General Public License along with
skamonkey@3513
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
skamonkey@3513
    19
 *
skamonkey@3513
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
skamonkey@3513
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
skamonkey@3513
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
skamonkey@3513
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
skamonkey@3513
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
skamonkey@3513
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
skamonkey@3513
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
skamonkey@3513
    27
 * or <http://www.sourcemod.net/license.php>.
skamonkey@3513
    28
 */
skamonkey@3513
    29
skamonkey@3513
    30
#ifndef _INCLUDE_SOURCEMOD_MEMORYUTILS_H_
skamonkey@3513
    31
#define _INCLUDE_SOURCEMOD_MEMORYUTILS_H_
skamonkey@3513
    32
skamonkey@3513
    33
#include <IMemoryUtils.h>
skamonkey@3513
    34
#include <string.h>
skamonkey@3513
    35
#include <vector>
skamonkey@3513
    36
#if defined PLATFORM_LINUX || defined PLATFORM_APPLE
skamonkey@3513
    37
#include "sm_symtable.h"
skamonkey@3513
    38
#include <unistd.h>
skamonkey@3513
    39
skamonkey@3513
    40
#endif
skamonkey@3513
    41
skamonkey@3513
    42
using namespace SourceMod;
skamonkey@3513
    43
using std::vector;
skamonkey@3513
    44
skamonkey@3513
    45
struct DynLibInfo
skamonkey@3513
    46
{
skamonkey@3513
    47
	void *baseAddress;
skamonkey@3513
    48
	size_t memorySize;
skamonkey@3513
    49
};
skamonkey@3513
    50
skamonkey@3513
    51
#if defined PLATFORM_LINUX || defined PLATFORM_APPLE
skamonkey@3513
    52
struct LibSymbolTable
skamonkey@3513
    53
{
skamonkey@3513
    54
	SymbolTable table;
skamonkey@3513
    55
	AddrTable table2;
skamonkey@3513
    56
	uintptr_t lib_base;
skamonkey@3513
    57
	uint32_t last_pos;
skamonkey@3513
    58
};
skamonkey@3513
    59
#endif
skamonkey@3513
    60
skamonkey@3513
    61
class MemoryUtils
skamonkey@3513
    62
{
skamonkey@3513
    63
public:
skamonkey@3513
    64
	MemoryUtils();
skamonkey@3513
    65
	~MemoryUtils();
skamonkey@3513
    66
public: // IMemoryUtils
skamonkey@3513
    67
	void *FindPattern(const void *libPtr, const char *pattern, size_t len);
skamonkey@3513
    68
	void *ResolveSymbol(void *handle, const char *symbol);
skamonkey@3513
    69
	const char *ResolveAddr(void *handle, void *addr);
skamonkey@3513
    70
public:
skamonkey@3513
    71
	void *FindPatternInFile(int fd, const char *pattern, size_t len);
skamonkey@3513
    72
	bool GetLibraryInfo(const void *libPtr, DynLibInfo &lib);
skamonkey@3513
    73
#if defined PLATFORM_LINUX || defined PLATFORM_APPLE
skamonkey@3513
    74
private:
skamonkey@3513
    75
	vector<LibSymbolTable *> m_SymTables;
skamonkey@3513
    76
#ifdef PLATFORM_APPLE
skamonkey@3513
    77
	struct dyld_all_image_infos *m_ImageList;
skamonkey@3513
    78
#endif
skamonkey@3513
    79
#endif
skamonkey@3513
    80
};
skamonkey@3513
    81
skamonkey@3513
    82
#endif // _INCLUDE_SOURCEMOD_MEMORYUTILS_H_