skamonkey@3513: /** skamonkey@3513: * vim: set ts=4 sw=4 tw=99 noet : skamonkey@3513: * ============================================================================= skamonkey@3513: * SourceMod skamonkey@3513: * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved. skamonkey@3513: * ============================================================================= skamonkey@3513: * skamonkey@3513: * This program is free software; you can redistribute it and/or modify it under skamonkey@3513: * the terms of the GNU General Public License, version 3.0, as published by the skamonkey@3513: * Free Software Foundation. skamonkey@3513: * skamonkey@3513: * This program is distributed in the hope that it will be useful, but WITHOUT skamonkey@3513: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS skamonkey@3513: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more skamonkey@3513: * details. skamonkey@3513: * skamonkey@3513: * You should have received a copy of the GNU General Public License along with skamonkey@3513: * this program. If not, see . skamonkey@3513: * skamonkey@3513: * As a special exception, AlliedModders LLC gives you permission to link the skamonkey@3513: * code of this program (as well as its derivative works) to "Half-Life 2," the skamonkey@3513: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software skamonkey@3513: * by the Valve Corporation. You must obey the GNU General Public License in skamonkey@3513: * all respects for all other code used. Additionally, AlliedModders LLC grants skamonkey@3513: * this exception to all derivative works. AlliedModders LLC defines further skamonkey@3513: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), skamonkey@3513: * or . skamonkey@3513: */ skamonkey@3513: skamonkey@3513: #include "MemoryUtils.h" skamonkey@3513: #ifdef PLATFORM_LINUX skamonkey@3513: #include skamonkey@3513: #include skamonkey@3513: #include skamonkey@3513: skamonkey@3513: #define PAGE_SIZE 4096 skamonkey@3513: #define PAGE_ALIGN_UP(x) ((x + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) skamonkey@3513: #endif skamonkey@3513: #ifdef PLATFORM_APPLE skamonkey@3513: #include skamonkey@3513: #include skamonkey@3513: #include skamonkey@3513: #endif skamonkey@3513: skamonkey@3513: MemoryUtils::MemoryUtils() skamonkey@3513: { skamonkey@3513: #ifdef PLATFORM_APPLE skamonkey@3513: skamonkey@3513: /* Get pointer to struct that describes all loaded mach-o images in process */ skamonkey@3513: struct nlist list[2]; skamonkey@3513: memset(list, 0, sizeof(list)); skamonkey@3513: list[0].n_un.n_name = (char *)"_dyld_all_image_infos"; skamonkey@3513: nlist("/usr/lib/dyld", list); skamonkey@3513: m_ImageList = (struct dyld_all_image_infos *)list[0].n_value; skamonkey@3513: skamonkey@3513: #endif skamonkey@3513: } skamonkey@3513: skamonkey@3513: MemoryUtils::~MemoryUtils() skamonkey@3513: { skamonkey@3513: #if defined PLATFORM_LINUX || defined PLATFORM_APPLE skamonkey@3513: for (size_t i = 0; i < m_SymTables.size(); i++) skamonkey@3513: { skamonkey@3513: delete m_SymTables[i]; skamonkey@3513: } skamonkey@3513: m_SymTables.clear(); skamonkey@3513: #endif skamonkey@3513: } skamonkey@3513: skamonkey@3513: void *MemoryUtils::FindPatternInFile(int fd, const char *pattern, size_t len) skamonkey@3513: { skamonkey@3513: size_t size; skamonkey@3513: size = lseek (fd , 0 , SEEK_END); skamonkey@3513: skamonkey@3513: lseek(fd, 0, SEEK_SET); skamonkey@3513: skamonkey@3513: void *map; skamonkey@3513: char *ptr, *end; skamonkey@3513: bool found = true; skamonkey@3513: map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); skamonkey@3513: skamonkey@3513: ptr = (char *)map; skamonkey@3513: end = ptr + size; skamonkey@3513: skamonkey@3513: while (ptr < end) skamonkey@3513: { skamonkey@3513: found = true; skamonkey@3513: for (register size_t i = 0; i < len; i++) skamonkey@3513: { skamonkey@3513: if (pattern[i] != '\x2A' && pattern[i] != ptr[i]) skamonkey@3513: { skamonkey@3513: found = false; skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: if (found) skamonkey@3513: return ptr; skamonkey@3513: skamonkey@3513: ptr++; skamonkey@3513: } skamonkey@3513: skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len) skamonkey@3513: { skamonkey@3513: DynLibInfo lib; skamonkey@3513: bool found; skamonkey@3513: char *ptr, *end; skamonkey@3513: skamonkey@3513: memset(&lib, 0, sizeof(DynLibInfo)); skamonkey@3513: skamonkey@3513: if (!GetLibraryInfo(libPtr, lib)) skamonkey@3513: { skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: ptr = reinterpret_cast(lib.baseAddress); skamonkey@3513: end = ptr + lib.memorySize - 1; skamonkey@3513: skamonkey@3513: while (ptr < end) skamonkey@3513: { skamonkey@3513: found = true; skamonkey@3513: for (register size_t i = 0; i < len; i++) skamonkey@3513: { skamonkey@3513: if (pattern[i] != '\x2A' && pattern[i] != ptr[i]) skamonkey@3513: { skamonkey@3513: found = false; skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: if (found) skamonkey@3513: return ptr; skamonkey@3513: skamonkey@3513: ptr++; skamonkey@3513: } skamonkey@3513: skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: void *MemoryUtils::ResolveSymbol(void *handle, const char *symbol) skamonkey@3513: { skamonkey@3513: #ifdef PLATFORM_WINDOWS skamonkey@3513: skamonkey@3513: return GetProcAddress((HMODULE)handle, symbol); skamonkey@3513: skamonkey@3513: #elif defined PLATFORM_LINUX skamonkey@3513: skamonkey@3513: struct link_map *dlmap; skamonkey@3513: struct stat dlstat; skamonkey@3513: int dlfile; skamonkey@3513: uintptr_t map_base; skamonkey@3513: Elf32_Ehdr *file_hdr; skamonkey@3513: Elf32_Shdr *sections, *shstrtab_hdr, *symtab_hdr, *strtab_hdr; skamonkey@3513: Elf32_Sym *symtab; skamonkey@3513: const char *shstrtab, *strtab; skamonkey@3513: uint16_t section_count; skamonkey@3513: uint32_t symbol_count; skamonkey@3513: LibSymbolTable *libtable; skamonkey@3513: SymbolTable *table; skamonkey@3513: AddrTable *table2; skamonkey@3513: Symbol *symbol_entry; skamonkey@3513: skamonkey@3513: dlmap = (struct link_map *)handle; skamonkey@3513: symtab_hdr = NULL; skamonkey@3513: strtab_hdr = NULL; skamonkey@3513: table = NULL; skamonkey@3513: table2 = NULL; skamonkey@3513: skamonkey@3513: /* See if we already have a symbol table for this library */ skamonkey@3513: for (size_t i = 0; i < m_SymTables.size(); i++) skamonkey@3513: { skamonkey@3513: libtable = m_SymTables[i]; skamonkey@3513: if (libtable->lib_base == dlmap->l_addr) skamonkey@3513: { skamonkey@3513: table = &libtable->table; skamonkey@3513: table2 = &libtable->table2; skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* If we don't have a symbol table for this library, then create one */ skamonkey@3513: if (table == NULL) skamonkey@3513: { skamonkey@3513: libtable = new LibSymbolTable(); skamonkey@3513: libtable->table.Initialize(); skamonkey@3513: libtable->table2.Initialize(); skamonkey@3513: libtable->lib_base = dlmap->l_addr; skamonkey@3513: libtable->last_pos = 0; skamonkey@3513: table = &libtable->table; skamonkey@3513: table2 = &libtable->table2; skamonkey@3513: m_SymTables.push_back(libtable); skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* See if the symbol is already cached in our table */ skamonkey@3513: symbol_entry = table->FindSymbol(symbol, strlen(symbol)); skamonkey@3513: if (symbol_entry != NULL) skamonkey@3513: { skamonkey@3513: return symbol_entry->address; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* If symbol isn't in our table, then we have open the actual library */ skamonkey@3513: dlfile = open(dlmap->l_name, O_RDONLY); skamonkey@3513: if (dlfile == -1 || fstat(dlfile, &dlstat) == -1) skamonkey@3513: { skamonkey@3513: close(dlfile); skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Map library file into memory */ skamonkey@3513: file_hdr = (Elf32_Ehdr *)mmap(NULL, dlstat.st_size, PROT_READ, MAP_PRIVATE, dlfile, 0); skamonkey@3513: map_base = (uintptr_t)file_hdr; skamonkey@3513: if (file_hdr == MAP_FAILED) skamonkey@3513: { skamonkey@3513: close(dlfile); skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: close(dlfile); skamonkey@3513: skamonkey@3513: if (file_hdr->e_shoff == 0 || file_hdr->e_shstrndx == SHN_UNDEF) skamonkey@3513: { skamonkey@3513: munmap(file_hdr, dlstat.st_size); skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: sections = (Elf32_Shdr *)(map_base + file_hdr->e_shoff); skamonkey@3513: section_count = file_hdr->e_shnum; skamonkey@3513: /* Get ELF section header string table */ skamonkey@3513: shstrtab_hdr = §ions[file_hdr->e_shstrndx]; skamonkey@3513: shstrtab = (const char *)(map_base + shstrtab_hdr->sh_offset); skamonkey@3513: skamonkey@3513: /* Iterate sections while looking for ELF symbol table and string table */ skamonkey@3513: for (uint16_t i = 0; i < section_count; i++) skamonkey@3513: { skamonkey@3513: Elf32_Shdr &hdr = sections[i]; skamonkey@3513: const char *section_name = shstrtab + hdr.sh_name; skamonkey@3513: skamonkey@3513: if (strcmp(section_name, ".symtab") == 0) skamonkey@3513: { skamonkey@3513: symtab_hdr = &hdr; skamonkey@3513: } skamonkey@3513: else if (strcmp(section_name, ".strtab") == 0) skamonkey@3513: { skamonkey@3513: strtab_hdr = &hdr; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Uh oh, we don't have a symbol table or a string table */ skamonkey@3513: if (symtab_hdr == NULL || strtab_hdr == NULL) skamonkey@3513: { skamonkey@3513: munmap(file_hdr, dlstat.st_size); skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: symtab = (Elf32_Sym *)(map_base + symtab_hdr->sh_offset); skamonkey@3513: strtab = (const char *)(map_base + strtab_hdr->sh_offset); skamonkey@3513: symbol_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize; skamonkey@3513: skamonkey@3513: /* Iterate symbol table starting from the position we were at last time */ skamonkey@3513: for (uint32_t i = libtable->last_pos; i < symbol_count; i++) skamonkey@3513: { skamonkey@3513: Elf32_Sym &sym = symtab[i]; skamonkey@3513: unsigned char sym_type = ELF32_ST_TYPE(sym.st_info); skamonkey@3513: const char *sym_name = strtab + sym.st_name; skamonkey@3513: Symbol *cur_sym; skamonkey@3513: skamonkey@3513: /* Skip symbols that are undefined or do not refer to functions or objects */ skamonkey@3513: if (sym.st_shndx == SHN_UNDEF || (sym_type != STT_FUNC && sym_type != STT_OBJECT)) skamonkey@3513: { skamonkey@3513: continue; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Caching symbols as we go along */ skamonkey@3513: cur_sym = table->InternSymbol(sym_name, strlen(sym_name), (void *)(dlmap->l_addr + sym.st_value)); skamonkey@3513: table2->InternSymbol(sym_name, strlen(sym_name), (void *)(dlmap->l_addr + sym.st_value)); skamonkey@3513: if (strcmp(symbol, sym_name) == 0) skamonkey@3513: { skamonkey@3513: symbol_entry = cur_sym; skamonkey@3513: libtable->last_pos = ++i; skamonkey@3513: // break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: libtable->last_pos = symbol_count; skamonkey@3513: skamonkey@3513: munmap(file_hdr, dlstat.st_size); skamonkey@3513: return symbol_entry ? symbol_entry->address : NULL; skamonkey@3513: skamonkey@3513: #elif defined PLATFORM_APPLE skamonkey@3513: skamonkey@3513: uintptr_t dlbase, linkedit_addr; skamonkey@3513: uint32_t image_count; skamonkey@3513: struct mach_header *file_hdr; skamonkey@3513: struct load_command *loadcmds; skamonkey@3513: struct segment_command *linkedit_hdr; skamonkey@3513: struct symtab_command *symtab_hdr; skamonkey@3513: struct nlist *symtab; skamonkey@3513: const char *strtab; skamonkey@3513: uint32_t loadcmd_count; skamonkey@3513: uint32_t symbol_count; skamonkey@3513: LibSymbolTable *libtable; skamonkey@3513: SymbolTable *table; skamonkey@3513: Symbol *symbol_entry; skamonkey@3513: skamonkey@3513: dlbase = 0; skamonkey@3513: image_count = m_ImageList->infoArrayCount; skamonkey@3513: linkedit_hdr = NULL; skamonkey@3513: symtab_hdr = NULL; skamonkey@3513: table = NULL; skamonkey@3513: skamonkey@3513: /* Loop through mach-o images in process. skamonkey@3513: * We can skip index 0 since that is just the executable. skamonkey@3513: */ skamonkey@3513: for (uint32_t i = 1; i < image_count; i++) skamonkey@3513: { skamonkey@3513: const struct dyld_image_info &info = m_ImageList->infoArray[i]; skamonkey@3513: skamonkey@3513: /* "Load" each one until we get a matching handle */ skamonkey@3513: void *h = dlopen(info.imageFilePath, RTLD_NOLOAD); skamonkey@3513: if (h == handle) skamonkey@3513: { skamonkey@3513: dlbase = (uintptr_t)info.imageLoadAddress; skamonkey@3513: dlclose(h); skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: skamonkey@3513: dlclose(h); skamonkey@3513: } skamonkey@3513: skamonkey@3513: if (!dlbase) skamonkey@3513: { skamonkey@3513: /* Uh oh, we couldn't find a matching handle */ skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* See if we already have a symbol table for this library */ skamonkey@3513: for (size_t i = 0; i < m_SymTables.size(); i++) skamonkey@3513: { skamonkey@3513: libtable = m_SymTables[i]; skamonkey@3513: if (libtable->lib_base == dlbase) skamonkey@3513: { skamonkey@3513: table = &libtable->table; skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* If we don't have a symbol table for this library, then create one */ skamonkey@3513: if (table == NULL) skamonkey@3513: { skamonkey@3513: libtable = new LibSymbolTable(); skamonkey@3513: libtable->table.Initialize(); skamonkey@3513: libtable->lib_base = dlbase; skamonkey@3513: libtable->last_pos = 0; skamonkey@3513: table = &libtable->table; skamonkey@3513: m_SymTables.push_back(libtable); skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* See if the symbol is already cached in our table */ skamonkey@3513: symbol_entry = table->FindSymbol(symbol, strlen(symbol)); skamonkey@3513: if (symbol_entry != NULL) skamonkey@3513: { skamonkey@3513: return symbol_entry->address; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* If symbol isn't in our table, then we have to locate it in memory */ skamonkey@3513: skamonkey@3513: file_hdr = (struct mach_header *)dlbase; skamonkey@3513: loadcmds = (struct load_command *)(dlbase + sizeof(struct mach_header)); skamonkey@3513: loadcmd_count = file_hdr->ncmds; skamonkey@3513: skamonkey@3513: /* Loop through load commands until we find the ones for the symbol table */ skamonkey@3513: for (uint32_t i = 0; i < loadcmd_count; i++) skamonkey@3513: { skamonkey@3513: if (loadcmds->cmd == LC_SEGMENT && !linkedit_hdr) skamonkey@3513: { skamonkey@3513: struct segment_command *seg = (struct segment_command *)loadcmds; skamonkey@3513: if (strcmp(seg->segname, "__LINKEDIT") == 0) skamonkey@3513: { skamonkey@3513: linkedit_hdr = seg; skamonkey@3513: if (symtab_hdr) skamonkey@3513: { skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: } skamonkey@3513: else if (loadcmds->cmd == LC_SYMTAB) skamonkey@3513: { skamonkey@3513: symtab_hdr = (struct symtab_command *)loadcmds; skamonkey@3513: if (linkedit_hdr) skamonkey@3513: { skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Load commands are not of a fixed size which is why we add the size */ skamonkey@3513: loadcmds = (struct load_command *)((uintptr_t)loadcmds + loadcmds->cmdsize); skamonkey@3513: } skamonkey@3513: skamonkey@3513: if (!linkedit_hdr || !symtab_hdr || !symtab_hdr->symoff || !symtab_hdr->stroff) skamonkey@3513: { skamonkey@3513: /* Uh oh, no symbol table */ skamonkey@3513: return NULL; skamonkey@3513: } skamonkey@3513: skamonkey@3513: linkedit_addr = dlbase + linkedit_hdr->vmaddr; skamonkey@3513: symtab = (struct nlist *)(linkedit_addr + symtab_hdr->symoff - linkedit_hdr->fileoff); skamonkey@3513: strtab = (const char *)(linkedit_addr + symtab_hdr->stroff - linkedit_hdr->fileoff); skamonkey@3513: symbol_count = symtab_hdr->nsyms; skamonkey@3513: skamonkey@3513: /* Iterate symbol table starting from the position we were at last time */ skamonkey@3513: for (uint32_t i = libtable->last_pos; i < symbol_count; i++) skamonkey@3513: { skamonkey@3513: struct nlist &sym = symtab[i]; skamonkey@3513: /* Ignore the prepended underscore on all symbols, so +1 here */ skamonkey@3513: const char *sym_name = strtab + sym.n_un.n_strx + 1; skamonkey@3513: Symbol *cur_sym; skamonkey@3513: skamonkey@3513: /* Skip symbols that are undefined */ skamonkey@3513: if (sym.n_sect == NO_SECT) skamonkey@3513: { skamonkey@3513: continue; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Caching symbols as we go along */ skamonkey@3513: cur_sym = table->InternSymbol(sym_name, strlen(sym_name), (void *)(dlbase + sym.n_value)); skamonkey@3513: if (strcmp(symbol, sym_name) == 0) skamonkey@3513: { skamonkey@3513: symbol_entry = cur_sym; skamonkey@3513: libtable->last_pos = ++i; skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: return symbol_entry ? symbol_entry->address : NULL; skamonkey@3513: skamonkey@3513: #endif skamonkey@3513: } skamonkey@3513: skamonkey@3513: bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib) skamonkey@3513: { skamonkey@3513: uintptr_t baseAddr; skamonkey@3513: skamonkey@3513: if (libPtr == NULL) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: #ifdef PLATFORM_WINDOWS skamonkey@3513: skamonkey@3513: MEMORY_BASIC_INFORMATION info; skamonkey@3513: IMAGE_DOS_HEADER *dos; skamonkey@3513: IMAGE_NT_HEADERS *pe; skamonkey@3513: IMAGE_FILE_HEADER *file; skamonkey@3513: IMAGE_OPTIONAL_HEADER *opt; skamonkey@3513: skamonkey@3513: if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION))) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: baseAddr = reinterpret_cast(info.AllocationBase); skamonkey@3513: skamonkey@3513: /* All this is for our insane sanity checks :o */ skamonkey@3513: dos = reinterpret_cast(baseAddr); skamonkey@3513: pe = reinterpret_cast(baseAddr + dos->e_lfanew); skamonkey@3513: file = &pe->FileHeader; skamonkey@3513: opt = &pe->OptionalHeader; skamonkey@3513: skamonkey@3513: /* Check PE magic and signature */ skamonkey@3513: if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Check architecture, which is 32-bit/x86 right now skamonkey@3513: * Should change this for 64-bit if Valve gets their act together skamonkey@3513: */ skamonkey@3513: if (file->Machine != IMAGE_FILE_MACHINE_I386) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* For our purposes, this must be a dynamic library */ skamonkey@3513: if ((file->Characteristics & IMAGE_FILE_DLL) == 0) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Finally, we can do this */ skamonkey@3513: lib.memorySize = opt->SizeOfImage; skamonkey@3513: skamonkey@3513: #elif defined PLATFORM_LINUX skamonkey@3513: skamonkey@3513: Dl_info info; skamonkey@3513: Elf32_Ehdr *file; skamonkey@3513: Elf32_Phdr *phdr; skamonkey@3513: uint16_t phdrCount; skamonkey@3513: skamonkey@3513: if (!dladdr(libPtr, &info)) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: if (!info.dli_fbase || !info.dli_fname) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* This is for our insane sanity checks :o */ skamonkey@3513: baseAddr = reinterpret_cast(info.dli_fbase); skamonkey@3513: file = reinterpret_cast(baseAddr); skamonkey@3513: skamonkey@3513: /* Check ELF magic */ skamonkey@3513: if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Check ELF version */ skamonkey@3513: if (file->e_ident[EI_VERSION] != EV_CURRENT) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Check ELF architecture, which is 32-bit/x86 right now skamonkey@3513: * Should change this for 64-bit if Valve gets their act together skamonkey@3513: */ skamonkey@3513: if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* For our purposes, this must be a dynamic library/shared object */ skamonkey@3513: if (file->e_type != ET_DYN) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: phdrCount = file->e_phnum; skamonkey@3513: phdr = reinterpret_cast(baseAddr + file->e_phoff); skamonkey@3513: skamonkey@3513: for (uint16_t i = 0; i < phdrCount; i++) skamonkey@3513: { skamonkey@3513: Elf32_Phdr &hdr = phdr[i]; skamonkey@3513: skamonkey@3513: /* We only really care about the segment with executable code */ skamonkey@3513: if (hdr.p_type == PT_LOAD && hdr.p_flags == (PF_X|PF_R)) skamonkey@3513: { skamonkey@3513: /* From glibc, elf/dl-load.c: skamonkey@3513: * c->mapend = ((ph->p_vaddr + ph->p_filesz + GLRO(dl_pagesize) - 1) skamonkey@3513: * & ~(GLRO(dl_pagesize) - 1)); skamonkey@3513: * skamonkey@3513: * In glibc, the segment file size is aligned up to the nearest page size and skamonkey@3513: * added to the virtual address of the segment. We just want the size here. skamonkey@3513: */ skamonkey@3513: lib.memorySize = PAGE_ALIGN_UP(hdr.p_filesz); skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: #elif defined PLATFORM_APPLE skamonkey@3513: skamonkey@3513: Dl_info info; skamonkey@3513: struct mach_header *file; skamonkey@3513: struct segment_command *seg; skamonkey@3513: uint32_t cmd_count; skamonkey@3513: skamonkey@3513: if (!dladdr(libPtr, &info)) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: if (!info.dli_fbase || !info.dli_fname) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* This is for our insane sanity checks :o */ skamonkey@3513: baseAddr = (uintptr_t)info.dli_fbase; skamonkey@3513: file = (struct mach_header *)baseAddr; skamonkey@3513: skamonkey@3513: /* Check Mach-O magic */ skamonkey@3513: if (file->magic != MH_MAGIC) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* Check architecture (32-bit/x86) */ skamonkey@3513: if (file->cputype != CPU_TYPE_I386 || file->cpusubtype != CPU_SUBTYPE_I386_ALL) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: /* For our purposes, this must be a dynamic library */ skamonkey@3513: if (file->filetype != MH_DYLIB) skamonkey@3513: { skamonkey@3513: return false; skamonkey@3513: } skamonkey@3513: skamonkey@3513: cmd_count = file->ncmds; skamonkey@3513: seg = (struct segment_command *)(baseAddr + sizeof(struct mach_header)); skamonkey@3513: skamonkey@3513: /* Add up memory sizes of mapped segments */ skamonkey@3513: for (uint32_t i = 0; i < cmd_count; i++) skamonkey@3513: { skamonkey@3513: if (seg->cmd == LC_SEGMENT) skamonkey@3513: { skamonkey@3513: lib.memorySize += seg->vmsize; skamonkey@3513: } skamonkey@3513: skamonkey@3513: seg = (struct segment_command *)((uintptr_t)seg + seg->cmdsize); skamonkey@3513: } skamonkey@3513: skamonkey@3513: #endif skamonkey@3513: skamonkey@3513: lib.baseAddress = reinterpret_cast(baseAddr); skamonkey@3513: skamonkey@3513: return true; skamonkey@3513: } skamonkey@3513: skamonkey@3513: const char* MemoryUtils::ResolveAddr(void *handle, void* addr) skamonkey@3513: { skamonkey@3513: struct link_map *dlmap; skamonkey@3513: dlmap = (struct link_map *)handle; skamonkey@3513: skamonkey@3513: LibSymbolTable *libtable; skamonkey@3513: AddrTable *table2; skamonkey@3513: skamonkey@3513: for (size_t i = 0; i < m_SymTables.size(); i++) skamonkey@3513: { skamonkey@3513: libtable = m_SymTables[i]; skamonkey@3513: if (libtable->lib_base == dlmap->l_addr) skamonkey@3513: { skamonkey@3513: table2 = &libtable->table2; skamonkey@3513: break; skamonkey@3513: } skamonkey@3513: } skamonkey@3513: skamonkey@3513: if (table2 == NULL) return NULL; skamonkey@3513: Symbol *sym = table2->FindAddr(addr); skamonkey@3513: return sym ? sym->buffer() : NULL; skamonkey@3513: }