tools/gdc/MemoryUtils.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3129 76208f2e5ef0
permissions -rw-r--r--
Updated some CS:GO gamedata.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  */
    29 
    30 #include "MemoryUtils.h"
    31 #ifdef PLATFORM_LINUX
    32 #include <fcntl.h>
    33 #include <link.h>
    34 #include <sys/mman.h>
    35 
    36 #define PAGE_SIZE			4096
    37 #define PAGE_ALIGN_UP(x)	((x + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
    38 #endif
    39 #ifdef PLATFORM_APPLE
    40 #include <mach-o/dyld_images.h>
    41 #include <mach-o/loader.h>
    42 #include <mach-o/nlist.h>
    43 #endif
    44 
    45 //MemoryUtils g_MemUtils;
    46 
    47 MemoryUtils::MemoryUtils()
    48 {
    49 #ifdef PLATFORM_APPLE
    50 
    51 	/* Get pointer to struct that describes all loaded mach-o images in process */
    52 	struct nlist list[2];
    53 	memset(list, 0, sizeof(list));
    54 	list[0].n_un.n_name = (char *)"_dyld_all_image_infos";
    55 	nlist("/usr/lib/dyld", list);
    56 	m_ImageList = (struct dyld_all_image_infos *)list[0].n_value;
    57 
    58 #endif
    59 }
    60 
    61 MemoryUtils::~MemoryUtils()
    62 {
    63 #if defined PLATFORM_LINUX || defined PLATFORM_APPLE
    64 	for (size_t i = 0; i < m_SymTables.size(); i++)
    65 	{
    66 		delete m_SymTables[i];
    67 	}
    68 	m_SymTables.clear();
    69 #endif
    70 }
    71 
    72 /*void MemoryUtils::OnSourceModAllInitialized()
    73 {
    74 	sharesys->AddInterface(NULL, this);
    75 }*/
    76 
    77 void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
    78 {
    79 	DynLibInfo lib;
    80 	bool found;
    81 	char *ptr, *end;
    82 
    83 	memset(&lib, 0, sizeof(DynLibInfo));
    84 
    85 	if (!GetLibraryInfo(libPtr, lib))
    86 	{
    87 		return NULL;
    88 	}
    89 
    90 	ptr = reinterpret_cast<char *>(lib.baseAddress);
    91 	end = ptr + lib.memorySize;
    92 
    93 	while (ptr < end)
    94 	{
    95 		found = true;
    96 		for (register size_t i = 0; i < len; i++)
    97 		{
    98 			if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
    99 			{
   100 				found = false;
   101 				break;
   102 			}
   103 		}
   104 
   105 		if (found)
   106 			return ptr;
   107 
   108 		ptr++;
   109 	}
   110 
   111 	return NULL;
   112 }
   113 
   114 void *MemoryUtils::ResolveSymbol(void *handle, const char *symbol)
   115 {
   116 #ifdef PLATFORM_WINDOWS
   117 
   118 	return GetProcAddress((HMODULE)handle, symbol);
   119 	
   120 #elif defined PLATFORM_LINUX
   121 
   122 	struct link_map *dlmap;
   123 	struct stat dlstat;
   124 	int dlfile;
   125 	uintptr_t map_base;
   126 	Elf32_Ehdr *file_hdr;
   127 	Elf32_Shdr *sections, *shstrtab_hdr, *symtab_hdr, *strtab_hdr;
   128 	Elf32_Sym *symtab;
   129 	const char *shstrtab, *strtab;
   130 	uint16_t section_count;
   131 	uint32_t symbol_count;
   132 	LibSymbolTable *libtable;
   133 	SymbolTable *table;
   134 	AddrTable *table2;
   135 	Symbol *symbol_entry;
   136 
   137 	dlmap = (struct link_map *)handle;
   138 	symtab_hdr = NULL;
   139 	strtab_hdr = NULL;
   140 	table = NULL;
   141 	table2 = NULL;
   142 	
   143 	/* See if we already have a symbol table for this library */
   144 	for (size_t i = 0; i < m_SymTables.size(); i++)
   145 	{
   146 		libtable = m_SymTables[i];
   147 		if (libtable->lib_base == dlmap->l_addr)
   148 		{
   149 			table = &libtable->table;
   150 			table2 = &libtable->table2;
   151 			break;
   152 		}
   153 	}
   154 
   155 	/* If we don't have a symbol table for this library, then create one */
   156 	if (table == NULL)
   157 	{
   158 		libtable = new LibSymbolTable();
   159 		libtable->table.Initialize();
   160 		libtable->table2.Initialize();
   161 		libtable->lib_base = dlmap->l_addr;
   162 		libtable->last_pos = 0;
   163 		table = &libtable->table;
   164 		table2 = &libtable->table2;
   165 		m_SymTables.push_back(libtable);
   166 	}
   167 
   168 	/* See if the symbol is already cached in our table */
   169 	symbol_entry = table->FindSymbol(symbol, strlen(symbol));
   170 	if (symbol_entry != NULL)
   171 	{
   172 		return symbol_entry->address;
   173 	}
   174 
   175 	/* If symbol isn't in our table, then we have open the actual library */
   176 	dlfile = open(dlmap->l_name, O_RDONLY);
   177 	if (dlfile == -1 || fstat(dlfile, &dlstat) == -1)
   178 	{
   179 		close(dlfile);
   180 		return NULL;
   181 	}
   182 
   183 	/* Map library file into memory */
   184 	file_hdr = (Elf32_Ehdr *)mmap(NULL, dlstat.st_size, PROT_READ, MAP_PRIVATE, dlfile, 0);
   185 	map_base = (uintptr_t)file_hdr;
   186 	if (file_hdr == MAP_FAILED)
   187 	{
   188 		close(dlfile);
   189 		return NULL;
   190 	}
   191 	close(dlfile);
   192 
   193 	if (file_hdr->e_shoff == 0 || file_hdr->e_shstrndx == SHN_UNDEF)
   194 	{
   195 		munmap(file_hdr, dlstat.st_size);
   196 		return NULL;
   197 	}
   198 
   199 	sections = (Elf32_Shdr *)(map_base + file_hdr->e_shoff);
   200 	section_count = file_hdr->e_shnum;
   201 	/* Get ELF section header string table */
   202 	shstrtab_hdr = &sections[file_hdr->e_shstrndx];
   203 	shstrtab = (const char *)(map_base + shstrtab_hdr->sh_offset);
   204 
   205 	/* Iterate sections while looking for ELF symbol table and string table */
   206 	for (uint16_t i = 0; i < section_count; i++)
   207 	{
   208 		Elf32_Shdr &hdr = sections[i];
   209 		const char *section_name = shstrtab + hdr.sh_name;
   210 
   211 		if (strcmp(section_name, ".symtab") == 0)
   212 		{
   213 			symtab_hdr = &hdr;
   214 		}
   215 		else if (strcmp(section_name, ".strtab") == 0)
   216 		{
   217 			strtab_hdr = &hdr;
   218 		}
   219 	}
   220 
   221 	/* Uh oh, we don't have a symbol table or a string table */
   222 	if (symtab_hdr == NULL || strtab_hdr == NULL)
   223 	{
   224 		munmap(file_hdr, dlstat.st_size);
   225 		return NULL;
   226 	}
   227 
   228 	symtab = (Elf32_Sym *)(map_base + symtab_hdr->sh_offset);
   229 	strtab = (const char *)(map_base + strtab_hdr->sh_offset);
   230 	symbol_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
   231 
   232 	/* Iterate symbol table starting from the position we were at last time */
   233 	for (uint32_t i = libtable->last_pos; i < symbol_count; i++)
   234 	{
   235 		Elf32_Sym &sym = symtab[i];
   236 		unsigned char sym_type = ELF32_ST_TYPE(sym.st_info);
   237 		const char *sym_name = strtab + sym.st_name;
   238 		Symbol *cur_sym;
   239 
   240 		/* Skip symbols that are undefined or do not refer to functions or objects */
   241 		if (sym.st_shndx == SHN_UNDEF || (sym_type != STT_FUNC && sym_type != STT_OBJECT))
   242 		{
   243 			continue;
   244 		}
   245 
   246 		/* Caching symbols as we go along */
   247 		cur_sym = table->InternSymbol(sym_name, strlen(sym_name), (void *)(dlmap->l_addr + sym.st_value));
   248 		table2->InternSymbol(sym_name, strlen(sym_name), (void *)(dlmap->l_addr + sym.st_value));
   249 		if (strcmp(symbol, sym_name) == 0)
   250 		{
   251 			symbol_entry = cur_sym;
   252 			libtable->last_pos = ++i;
   253 	//		break;
   254 		}
   255 	}
   256 
   257 	libtable->last_pos = symbol_count;
   258 
   259 	munmap(file_hdr, dlstat.st_size);
   260 	return symbol_entry ? symbol_entry->address : NULL;
   261 
   262 #elif defined PLATFORM_APPLE
   263 	
   264 	uintptr_t dlbase, linkedit_addr;
   265 	uint32_t image_count;
   266 	struct mach_header *file_hdr;
   267 	struct load_command *loadcmds;
   268 	struct segment_command *linkedit_hdr;
   269 	struct symtab_command *symtab_hdr;
   270 	struct nlist *symtab;
   271 	const char *strtab;
   272 	uint32_t loadcmd_count;
   273 	uint32_t symbol_count;
   274 	LibSymbolTable *libtable;
   275 	SymbolTable *table;
   276 	Symbol *symbol_entry;
   277 	
   278 	dlbase = 0;
   279 	image_count = m_ImageList->infoArrayCount;
   280 	linkedit_hdr = NULL;
   281 	symtab_hdr = NULL;
   282 	table = NULL;
   283 	
   284 	/* Loop through mach-o images in process.
   285 	 * We can skip index 0 since that is just the executable.
   286 	 */
   287 	for (uint32_t i = 1; i < image_count; i++)
   288 	{
   289 		const struct dyld_image_info &info = m_ImageList->infoArray[i];
   290 		
   291 		/* "Load" each one until we get a matching handle */
   292 		void *h = dlopen(info.imageFilePath, RTLD_NOLOAD);
   293 		if (h == handle)
   294 		{
   295 			dlbase = (uintptr_t)info.imageLoadAddress;
   296 			dlclose(h);
   297 			break;
   298 		}
   299 		
   300 		dlclose(h);
   301 	}
   302 	
   303 	if (!dlbase)
   304 	{
   305 		/* Uh oh, we couldn't find a matching handle */
   306 		return NULL;
   307 	}
   308 	
   309 	/* See if we already have a symbol table for this library */
   310 	for (size_t i = 0; i < m_SymTables.size(); i++)
   311 	{
   312 		libtable = m_SymTables[i];
   313 		if (libtable->lib_base == dlbase)
   314 		{
   315 			table = &libtable->table;
   316 			break;
   317 		}
   318 	}
   319 	
   320 	/* If we don't have a symbol table for this library, then create one */
   321 	if (table == NULL)
   322 	{
   323 		libtable = new LibSymbolTable();
   324 		libtable->table.Initialize();
   325 		libtable->lib_base = dlbase;
   326 		libtable->last_pos = 0;
   327 		table = &libtable->table;
   328 		m_SymTables.push_back(libtable);
   329 	}
   330 	
   331 	/* See if the symbol is already cached in our table */
   332 	symbol_entry = table->FindSymbol(symbol, strlen(symbol));
   333 	if (symbol_entry != NULL)
   334 	{
   335 		return symbol_entry->address;
   336 	}
   337 	
   338 	/* If symbol isn't in our table, then we have to locate it in memory */
   339 	
   340 	file_hdr = (struct mach_header *)dlbase;
   341 	loadcmds = (struct load_command *)(dlbase + sizeof(struct mach_header));
   342 	loadcmd_count = file_hdr->ncmds;
   343 	
   344 	/* Loop through load commands until we find the ones for the symbol table */
   345 	for (uint32_t i = 0; i < loadcmd_count; i++)
   346 	{
   347 		if (loadcmds->cmd == LC_SEGMENT && !linkedit_hdr)
   348 		{
   349 			struct segment_command *seg = (struct segment_command *)loadcmds;
   350 			if (strcmp(seg->segname, "__LINKEDIT") == 0)
   351 			{
   352 				linkedit_hdr = seg;
   353 				if (symtab_hdr)
   354 				{
   355 					break;
   356 				}
   357 			}
   358 		}
   359 		else if (loadcmds->cmd == LC_SYMTAB)
   360 		{
   361 			symtab_hdr = (struct symtab_command *)loadcmds;
   362 			if (linkedit_hdr)
   363 			{
   364 				break;
   365 			}
   366 		}
   367 
   368 		/* Load commands are not of a fixed size which is why we add the size */
   369 		loadcmds = (struct load_command *)((uintptr_t)loadcmds + loadcmds->cmdsize);
   370 	}
   371 	
   372 	if (!linkedit_hdr || !symtab_hdr || !symtab_hdr->symoff || !symtab_hdr->stroff)
   373 	{
   374 		/* Uh oh, no symbol table */
   375 		return NULL;
   376 	}
   377 
   378 	linkedit_addr = dlbase + linkedit_hdr->vmaddr;
   379 	symtab = (struct nlist *)(linkedit_addr + symtab_hdr->symoff - linkedit_hdr->fileoff);
   380 	strtab = (const char *)(linkedit_addr + symtab_hdr->stroff - linkedit_hdr->fileoff);
   381 	symbol_count = symtab_hdr->nsyms;
   382 	
   383 	/* Iterate symbol table starting from the position we were at last time */
   384 	for (uint32_t i = libtable->last_pos; i < symbol_count; i++)
   385 	{
   386 		struct nlist &sym = symtab[i];
   387 		/* Ignore the prepended underscore on all symbols, so +1 here */
   388 		const char *sym_name = strtab + sym.n_un.n_strx + 1;
   389 		Symbol *cur_sym;
   390 		
   391 		/* Skip symbols that are undefined */
   392 		if (sym.n_sect == NO_SECT)
   393 		{
   394 			continue;
   395 		}
   396 		
   397 		/* Caching symbols as we go along */
   398 		cur_sym = table->InternSymbol(sym_name, strlen(sym_name), (void *)(dlbase + sym.n_value));
   399 		if (strcmp(symbol, sym_name) == 0)
   400 		{
   401 			symbol_entry = cur_sym;
   402 			libtable->last_pos = ++i;
   403 			break;
   404 		}
   405 	}
   406 	
   407 	return symbol_entry ? symbol_entry->address : NULL;
   408 
   409 #endif
   410 }
   411 
   412 bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
   413 {
   414 	uintptr_t baseAddr;
   415 
   416 	if (libPtr == NULL)
   417 	{
   418 		return false;
   419 	}
   420 
   421 #ifdef PLATFORM_WINDOWS
   422 
   423 	MEMORY_BASIC_INFORMATION info;
   424 	IMAGE_DOS_HEADER *dos;
   425 	IMAGE_NT_HEADERS *pe;
   426 	IMAGE_FILE_HEADER *file;
   427 	IMAGE_OPTIONAL_HEADER *opt;
   428 
   429 	if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
   430 	{
   431 		return false;
   432 	}
   433 
   434 	baseAddr = reinterpret_cast<uintptr_t>(info.AllocationBase);
   435 
   436 	/* All this is for our insane sanity checks :o */
   437 	dos = reinterpret_cast<IMAGE_DOS_HEADER *>(baseAddr);
   438 	pe = reinterpret_cast<IMAGE_NT_HEADERS *>(baseAddr + dos->e_lfanew);
   439 	file = &pe->FileHeader;
   440 	opt = &pe->OptionalHeader;
   441 
   442 	/* Check PE magic and signature */
   443 	if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
   444 	{
   445 		return false;
   446 	}
   447 
   448 	/* Check architecture, which is 32-bit/x86 right now
   449 	 * Should change this for 64-bit if Valve gets their act together
   450 	 */
   451 	if (file->Machine != IMAGE_FILE_MACHINE_I386)
   452 	{
   453 		return false;
   454 	}
   455 
   456 	/* For our purposes, this must be a dynamic library */
   457 	if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
   458 	{
   459 		return false;
   460 	}
   461 
   462 	/* Finally, we can do this */
   463 	lib.memorySize = opt->SizeOfImage;
   464 
   465 #elif defined PLATFORM_LINUX
   466 
   467 	Dl_info info;
   468 	Elf32_Ehdr *file;
   469 	Elf32_Phdr *phdr;
   470 	uint16_t phdrCount;
   471 
   472 	if (!dladdr(libPtr, &info))
   473 	{
   474 		return false;
   475 	}
   476 
   477 	if (!info.dli_fbase || !info.dli_fname)
   478 	{
   479 		return false;
   480 	}
   481 
   482 	/* This is for our insane sanity checks :o */
   483 	baseAddr = reinterpret_cast<uintptr_t>(info.dli_fbase);
   484 	file = reinterpret_cast<Elf32_Ehdr *>(baseAddr);
   485 
   486 	/* Check ELF magic */
   487 	if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
   488 	{
   489 		return false;
   490 	}
   491 
   492 	/* Check ELF version */
   493 	if (file->e_ident[EI_VERSION] != EV_CURRENT)
   494 	{
   495 		return false;
   496 	}
   497 
   498 	/* Check ELF architecture, which is 32-bit/x86 right now
   499 	 * Should change this for 64-bit if Valve gets their act together
   500 	 */
   501 	if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
   502 	{
   503 		return false;
   504 	}
   505 
   506 	/* For our purposes, this must be a dynamic library/shared object */
   507 	if (file->e_type != ET_DYN)
   508 	{
   509 		return false;
   510 	}
   511 
   512 	phdrCount = file->e_phnum;
   513 	phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
   514 
   515 	for (uint16_t i = 0; i < phdrCount; i++)
   516 	{
   517 		Elf32_Phdr &hdr = phdr[i];
   518 
   519 		/* We only really care about the segment with executable code */
   520 		if (hdr.p_type == PT_LOAD && hdr.p_flags == (PF_X|PF_R))
   521 		{
   522 			/* From glibc, elf/dl-load.c:
   523 			 * c->mapend = ((ph->p_vaddr + ph->p_filesz + GLRO(dl_pagesize) - 1) 
   524 			 *             & ~(GLRO(dl_pagesize) - 1));
   525 			 *
   526 			 * In glibc, the segment file size is aligned up to the nearest page size and
   527 			 * added to the virtual address of the segment. We just want the size here.
   528 			 */
   529 			lib.memorySize = PAGE_ALIGN_UP(hdr.p_filesz);
   530 			break;
   531 		}
   532 	}
   533 
   534 #elif defined PLATFORM_APPLE
   535 
   536 	Dl_info info;
   537 	struct mach_header *file;
   538 	struct segment_command *seg;
   539 	uint32_t cmd_count;
   540 
   541 	if (!dladdr(libPtr, &info))
   542 	{
   543 		return false;
   544 	}
   545 
   546 	if (!info.dli_fbase || !info.dli_fname)
   547 	{
   548 		return false;
   549 	}
   550 
   551 	/* This is for our insane sanity checks :o */
   552 	baseAddr = (uintptr_t)info.dli_fbase;
   553 	file = (struct mach_header *)baseAddr;
   554 
   555 	/* Check Mach-O magic */
   556 	if (file->magic != MH_MAGIC)
   557 	{
   558 		return false;
   559 	}
   560 
   561 	/* Check architecture (32-bit/x86) */
   562 	if (file->cputype != CPU_TYPE_I386 || file->cpusubtype != CPU_SUBTYPE_I386_ALL)
   563 	{
   564 		return false;
   565 	}
   566 
   567 	/* For our purposes, this must be a dynamic library */
   568 	if (file->filetype != MH_DYLIB)
   569 	{
   570 		return false;
   571 	}
   572 
   573 	cmd_count = file->ncmds;
   574 	seg = (struct segment_command *)(baseAddr + sizeof(struct mach_header));
   575 	
   576 	/* Add up memory sizes of mapped segments */
   577 	for (uint32_t i = 0; i < cmd_count; i++)
   578 	{		
   579 		if (seg->cmd == LC_SEGMENT)
   580 		{
   581 			lib.memorySize += seg->vmsize;
   582 		}
   583 		
   584 		seg = (struct segment_command *)((uintptr_t)seg + seg->cmdsize);
   585 	}
   586 
   587 #endif
   588 
   589 	lib.baseAddress = reinterpret_cast<void *>(baseAddr);
   590 
   591 	return true;
   592 }
   593 
   594 const char* MemoryUtils::ResolveAddr(void *handle, void* addr)
   595 {
   596 	struct link_map *dlmap;
   597 	dlmap = (struct link_map *)handle;
   598 
   599 	LibSymbolTable *libtable;
   600 	AddrTable *table2;
   601 
   602 	for (size_t i = 0; i < m_SymTables.size(); i++)
   603 	{
   604 		libtable = m_SymTables[i];
   605 		if (libtable->lib_base == dlmap->l_addr)
   606 		{
   607 			table2 = &libtable->table2;
   608 			break;
   609 		}
   610 	}
   611 
   612 	if (table2 == NULL) return NULL;
   613 	Symbol *sym = table2->FindAddr(addr);
   614 	return sym ? sym->buffer() : NULL;
   615 }