core/MemoryUtils.cpp
changeset 2863 d740d9cdf53a
parent 2289 44045ead61cd
child 2992 affc67b319f4
     1.1 --- a/core/MemoryUtils.cpp	Wed Nov 04 13:52:12 2009 -0500
     1.2 +++ b/core/MemoryUtils.cpp	Fri Nov 06 16:24:40 2009 -0600
     1.3 @@ -1,8 +1,8 @@
     1.4  /**
     1.5 - * vim: set ts=4 :
     1.6 + * vim: set ts=4 sw=4 tw=99 noet :
     1.7   * =============================================================================
     1.8   * SourceMod
     1.9 - * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
    1.10 + * Copyright (C) 2004-2009 AlliedModders LLC.  All rights reserved.
    1.11   * =============================================================================
    1.12   *
    1.13   * This program is free software; you can redistribute it and/or modify it under
    1.14 @@ -25,15 +25,14 @@
    1.15   * this exception to all derivative works.  AlliedModders LLC defines further
    1.16   * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.17   * or <http://www.sourcemod.net/license.php>.
    1.18 - *
    1.19 - * Version: $Id$
    1.20   */
    1.21  
    1.22  #include "MemoryUtils.h"
    1.23  #include "ShareSys.h"
    1.24  #ifdef PLATFORM_LINUX
    1.25 -#include <string.h>
    1.26 -#include <elf.h>
    1.27 +#include <fcntl.h>
    1.28 +#include <link.h>
    1.29 +#include <sys/mman.h>
    1.30  #endif
    1.31  
    1.32  MemoryUtils g_MemUtils;
    1.33 @@ -56,6 +55,17 @@
    1.34  }
    1.35  #endif
    1.36  
    1.37 +MemoryUtils::~MemoryUtils()
    1.38 +{
    1.39 +#ifdef PLATFORM_LINUX
    1.40 +	for (size_t i = 0; i < m_SymTables.size(); i++)
    1.41 +	{
    1.42 +		delete m_SymTables[i];
    1.43 +	}
    1.44 +	m_SymTables.clear();
    1.45 +#endif
    1.46 +}
    1.47 +
    1.48  void MemoryUtils::OnSourceModAllInitialized()
    1.49  {
    1.50  	g_ShareSys.AddInterface(NULL, this);
    1.51 @@ -98,6 +108,149 @@
    1.52  	return NULL;
    1.53  }
    1.54  
    1.55 +void *MemoryUtils::ResolveSymbol(void *handle, const char *symbol)
    1.56 +{
    1.57 +#ifdef PLATFORM_WINDOWS
    1.58 +
    1.59 +	return GetProcAddress((HMODULE)handle, symbol);
    1.60 +
    1.61 +#elif defined PLATFORM_LINUX
    1.62 +
    1.63 +	struct link_map *dlmap;
    1.64 +	struct stat dlstat;
    1.65 +	int dlfile;
    1.66 +	uintptr_t map_base;
    1.67 +	Elf32_Ehdr *file_hdr;
    1.68 +	Elf32_Shdr *sections, *shstrtab_hdr, *symtab_hdr, *strtab_hdr;
    1.69 +	Elf32_Sym *symtab;
    1.70 +	const char *shstrtab, *strtab;
    1.71 +	uint16_t section_count;
    1.72 +	uint32_t symbol_count;
    1.73 +	LibSymbolTable *libtable;
    1.74 +	SymbolTable *table;
    1.75 +	Symbol *symbol_entry;
    1.76 +
    1.77 +	dlmap = (struct link_map *)handle;
    1.78 +	symtab_hdr = NULL;
    1.79 +	strtab_hdr = NULL;
    1.80 +	table = NULL;
    1.81 +	
    1.82 +	/* See if we already have a symbol table for this library */
    1.83 +	for (size_t i = 0; i < m_SymTables.size(); i++)
    1.84 +	{
    1.85 +		libtable = m_SymTables[i];
    1.86 +		if (libtable->lib_base == dlmap->l_addr)
    1.87 +		{
    1.88 +			table = &libtable->table;
    1.89 +			break;
    1.90 +		}
    1.91 +	}
    1.92 +
    1.93 +	/* If we don't have a symbol table for this library, then create one */
    1.94 +	if (table == NULL)
    1.95 +	{
    1.96 +		libtable = new LibSymbolTable();
    1.97 +		libtable->table.Initialize();
    1.98 +		libtable->lib_base = dlmap->l_addr;
    1.99 +		libtable->last_pos = 0;
   1.100 +		table = &libtable->table;
   1.101 +		m_SymTables.push_back(libtable);
   1.102 +	}
   1.103 +
   1.104 +	/* See if the symbol is already cached in our table */
   1.105 +	symbol_entry = table->FindSymbol(symbol, strlen(symbol));
   1.106 +	if (symbol_entry != NULL)
   1.107 +	{
   1.108 +		return symbol_entry->address;
   1.109 +	}
   1.110 +
   1.111 +	/* If symbol isn't in our table, then we have open the actual library */
   1.112 +	dlfile = open(dlmap->l_name, O_RDONLY);
   1.113 +	if (dlfile == -1 || fstat(dlfile, &dlstat) == -1)
   1.114 +	{
   1.115 +		close(dlfile);
   1.116 +		return NULL;
   1.117 +	}
   1.118 +
   1.119 +	/* Map library file into memory */
   1.120 +	file_hdr = (Elf32_Ehdr *)mmap(NULL, dlstat.st_size, PROT_READ, MAP_PRIVATE, dlfile, 0);
   1.121 +	map_base = (uintptr_t)file_hdr;
   1.122 +	if (file_hdr == MAP_FAILED)
   1.123 +	{
   1.124 +		close(dlfile);
   1.125 +		return NULL;
   1.126 +	}
   1.127 +	close(dlfile);
   1.128 +
   1.129 +	if (file_hdr->e_shoff == 0 || file_hdr->e_shstrndx == SHN_UNDEF)
   1.130 +	{
   1.131 +		munmap(file_hdr, dlstat.st_size);
   1.132 +		return NULL;
   1.133 +	}
   1.134 +
   1.135 +	sections = (Elf32_Shdr *)(map_base + file_hdr->e_shoff);
   1.136 +	section_count = file_hdr->e_shnum;
   1.137 +	/* Get ELF section header string table */
   1.138 +	shstrtab_hdr = &sections[file_hdr->e_shstrndx];
   1.139 +	shstrtab = (const char *)(map_base + shstrtab_hdr->sh_offset);
   1.140 +
   1.141 +	/* Iterate sections while looking for ELF symbol table and string table */
   1.142 +	for (uint16_t i = 0; i < section_count; i++)
   1.143 +	{
   1.144 +		Elf32_Shdr &hdr = sections[i];
   1.145 +		const char *section_name = shstrtab + hdr.sh_name;
   1.146 +
   1.147 +		if (strcmp(section_name, ".symtab") == 0)
   1.148 +		{
   1.149 +			symtab_hdr = &hdr;
   1.150 +		}
   1.151 +		else if (strcmp(section_name, ".strtab") == 0)
   1.152 +		{
   1.153 +			strtab_hdr = &hdr;
   1.154 +		}
   1.155 +	}
   1.156 +
   1.157 +	/* Uh oh, we don't have a symbol table or a string table */
   1.158 +	if (symtab_hdr == NULL || strtab_hdr == NULL)
   1.159 +	{
   1.160 +		munmap(file_hdr, dlstat.st_size);
   1.161 +		return NULL;
   1.162 +	}
   1.163 +
   1.164 +	symtab = (Elf32_Sym *)(map_base + symtab_hdr->sh_offset);
   1.165 +	strtab = (const char *)(map_base + strtab_hdr->sh_offset);
   1.166 +	symbol_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
   1.167 +
   1.168 +	/* Iterate symbol table starting from the position we were at last time */
   1.169 +	for (uint32_t i = libtable->last_pos; i < symbol_count; i++)
   1.170 +	{
   1.171 +		Elf32_Sym &sym = symtab[i];
   1.172 +		unsigned char sym_type = ELF32_ST_TYPE(sym.st_info);
   1.173 +		const char *sym_name = strtab + sym.st_name;
   1.174 +		Symbol *cur_sym;
   1.175 +
   1.176 +		/* Skip symbols that are undefined or do not refer to functions or objects */
   1.177 +		if (sym.st_shndx == SHN_UNDEF || (sym_type != STT_FUNC && sym_type != STT_OBJECT))
   1.178 +		{
   1.179 +			continue;
   1.180 +		}
   1.181 +
   1.182 +		/* Caching symbols as we go along */
   1.183 +		cur_sym = table->InternSymbol(sym_name, strlen(sym_name), (void *)(dlmap->l_addr + sym.st_value));
   1.184 +		if (strcmp(symbol, sym_name) == 0)
   1.185 +		{
   1.186 +			symbol_entry = cur_sym;
   1.187 +			libtable->last_pos = ++i;
   1.188 +			break;
   1.189 +		}
   1.190 +	}
   1.191 +
   1.192 +	munmap(file_hdr, dlstat.st_size);
   1.193 +	return symbol_entry ? symbol_entry->address : NULL;
   1.194 +
   1.195 +#endif
   1.196 +}
   1.197 +
   1.198  bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
   1.199  {
   1.200  	unsigned long baseAddr;