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