Updated some CS:GO gamedata.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
30 #include "MemoryUtils.h"
36 #define PAGE_SIZE 4096
37 #define PAGE_ALIGN_UP(x) ((x + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
40 #include <mach-o/dyld_images.h>
41 #include <mach-o/loader.h>
42 #include <mach-o/nlist.h>
45 //MemoryUtils g_MemUtils;
47 MemoryUtils::MemoryUtils()
51 /* Get pointer to struct that describes all loaded mach-o images in process */
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;
61 MemoryUtils::~MemoryUtils()
63 #if defined PLATFORM_LINUX || defined PLATFORM_APPLE
64 for (size_t i = 0; i < m_SymTables.size(); i++)
66 delete m_SymTables[i];
72 /*void MemoryUtils::OnSourceModAllInitialized()
74 sharesys->AddInterface(NULL, this);
77 void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
83 memset(&lib, 0, sizeof(DynLibInfo));
85 if (!GetLibraryInfo(libPtr, lib))
90 ptr = reinterpret_cast<char *>(lib.baseAddress);
91 end = ptr + lib.memorySize;
96 for (register size_t i = 0; i < len; i++)
98 if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
114 void *MemoryUtils::ResolveSymbol(void *handle, const char *symbol)
116 #ifdef PLATFORM_WINDOWS
118 return GetProcAddress((HMODULE)handle, symbol);
120 #elif defined PLATFORM_LINUX
122 struct link_map *dlmap;
126 Elf32_Ehdr *file_hdr;
127 Elf32_Shdr *sections, *shstrtab_hdr, *symtab_hdr, *strtab_hdr;
129 const char *shstrtab, *strtab;
130 uint16_t section_count;
131 uint32_t symbol_count;
132 LibSymbolTable *libtable;
135 Symbol *symbol_entry;
137 dlmap = (struct link_map *)handle;
143 /* See if we already have a symbol table for this library */
144 for (size_t i = 0; i < m_SymTables.size(); i++)
146 libtable = m_SymTables[i];
147 if (libtable->lib_base == dlmap->l_addr)
149 table = &libtable->table;
150 table2 = &libtable->table2;
155 /* If we don't have a symbol table for this library, then create one */
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);
168 /* See if the symbol is already cached in our table */
169 symbol_entry = table->FindSymbol(symbol, strlen(symbol));
170 if (symbol_entry != NULL)
172 return symbol_entry->address;
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)
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)
193 if (file_hdr->e_shoff == 0 || file_hdr->e_shstrndx == SHN_UNDEF)
195 munmap(file_hdr, dlstat.st_size);
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 = §ions[file_hdr->e_shstrndx];
203 shstrtab = (const char *)(map_base + shstrtab_hdr->sh_offset);
205 /* Iterate sections while looking for ELF symbol table and string table */
206 for (uint16_t i = 0; i < section_count; i++)
208 Elf32_Shdr &hdr = sections[i];
209 const char *section_name = shstrtab + hdr.sh_name;
211 if (strcmp(section_name, ".symtab") == 0)
215 else if (strcmp(section_name, ".strtab") == 0)
221 /* Uh oh, we don't have a symbol table or a string table */
222 if (symtab_hdr == NULL || strtab_hdr == NULL)
224 munmap(file_hdr, dlstat.st_size);
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;
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++)
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;
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))
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)
251 symbol_entry = cur_sym;
252 libtable->last_pos = ++i;
257 libtable->last_pos = symbol_count;
259 munmap(file_hdr, dlstat.st_size);
260 return symbol_entry ? symbol_entry->address : NULL;
262 #elif defined PLATFORM_APPLE
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;
272 uint32_t loadcmd_count;
273 uint32_t symbol_count;
274 LibSymbolTable *libtable;
276 Symbol *symbol_entry;
279 image_count = m_ImageList->infoArrayCount;
284 /* Loop through mach-o images in process.
285 * We can skip index 0 since that is just the executable.
287 for (uint32_t i = 1; i < image_count; i++)
289 const struct dyld_image_info &info = m_ImageList->infoArray[i];
291 /* "Load" each one until we get a matching handle */
292 void *h = dlopen(info.imageFilePath, RTLD_NOLOAD);
295 dlbase = (uintptr_t)info.imageLoadAddress;
305 /* Uh oh, we couldn't find a matching handle */
309 /* See if we already have a symbol table for this library */
310 for (size_t i = 0; i < m_SymTables.size(); i++)
312 libtable = m_SymTables[i];
313 if (libtable->lib_base == dlbase)
315 table = &libtable->table;
320 /* If we don't have a symbol table for this library, then create one */
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);
331 /* See if the symbol is already cached in our table */
332 symbol_entry = table->FindSymbol(symbol, strlen(symbol));
333 if (symbol_entry != NULL)
335 return symbol_entry->address;
338 /* If symbol isn't in our table, then we have to locate it in memory */
340 file_hdr = (struct mach_header *)dlbase;
341 loadcmds = (struct load_command *)(dlbase + sizeof(struct mach_header));
342 loadcmd_count = file_hdr->ncmds;
344 /* Loop through load commands until we find the ones for the symbol table */
345 for (uint32_t i = 0; i < loadcmd_count; i++)
347 if (loadcmds->cmd == LC_SEGMENT && !linkedit_hdr)
349 struct segment_command *seg = (struct segment_command *)loadcmds;
350 if (strcmp(seg->segname, "__LINKEDIT") == 0)
359 else if (loadcmds->cmd == LC_SYMTAB)
361 symtab_hdr = (struct symtab_command *)loadcmds;
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);
372 if (!linkedit_hdr || !symtab_hdr || !symtab_hdr->symoff || !symtab_hdr->stroff)
374 /* Uh oh, no symbol table */
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;
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++)
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;
391 /* Skip symbols that are undefined */
392 if (sym.n_sect == NO_SECT)
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)
401 symbol_entry = cur_sym;
402 libtable->last_pos = ++i;
407 return symbol_entry ? symbol_entry->address : NULL;
412 bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
421 #ifdef PLATFORM_WINDOWS
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;
429 if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
434 baseAddr = reinterpret_cast<uintptr_t>(info.AllocationBase);
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;
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)
448 /* Check architecture, which is 32-bit/x86 right now
449 * Should change this for 64-bit if Valve gets their act together
451 if (file->Machine != IMAGE_FILE_MACHINE_I386)
456 /* For our purposes, this must be a dynamic library */
457 if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
462 /* Finally, we can do this */
463 lib.memorySize = opt->SizeOfImage;
465 #elif defined PLATFORM_LINUX
472 if (!dladdr(libPtr, &info))
477 if (!info.dli_fbase || !info.dli_fname)
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);
486 /* Check ELF magic */
487 if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
492 /* Check ELF version */
493 if (file->e_ident[EI_VERSION] != EV_CURRENT)
498 /* Check ELF architecture, which is 32-bit/x86 right now
499 * Should change this for 64-bit if Valve gets their act together
501 if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
506 /* For our purposes, this must be a dynamic library/shared object */
507 if (file->e_type != ET_DYN)
512 phdrCount = file->e_phnum;
513 phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
515 for (uint16_t i = 0; i < phdrCount; i++)
517 Elf32_Phdr &hdr = phdr[i];
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))
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));
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.
529 lib.memorySize = PAGE_ALIGN_UP(hdr.p_filesz);
534 #elif defined PLATFORM_APPLE
537 struct mach_header *file;
538 struct segment_command *seg;
541 if (!dladdr(libPtr, &info))
546 if (!info.dli_fbase || !info.dli_fname)
551 /* This is for our insane sanity checks :o */
552 baseAddr = (uintptr_t)info.dli_fbase;
553 file = (struct mach_header *)baseAddr;
555 /* Check Mach-O magic */
556 if (file->magic != MH_MAGIC)
561 /* Check architecture (32-bit/x86) */
562 if (file->cputype != CPU_TYPE_I386 || file->cpusubtype != CPU_SUBTYPE_I386_ALL)
567 /* For our purposes, this must be a dynamic library */
568 if (file->filetype != MH_DYLIB)
573 cmd_count = file->ncmds;
574 seg = (struct segment_command *)(baseAddr + sizeof(struct mach_header));
576 /* Add up memory sizes of mapped segments */
577 for (uint32_t i = 0; i < cmd_count; i++)
579 if (seg->cmd == LC_SEGMENT)
581 lib.memorySize += seg->vmsize;
584 seg = (struct segment_command *)((uintptr_t)seg + seg->cmdsize);
589 lib.baseAddress = reinterpret_cast<void *>(baseAddr);
594 const char* MemoryUtils::ResolveAddr(void *handle, void* addr)
596 struct link_map *dlmap;
597 dlmap = (struct link_map *)handle;
599 LibSymbolTable *libtable;
602 for (size_t i = 0; i < m_SymTables.size(); i++)
604 libtable = m_SymTables[i];
605 if (libtable->lib_base == dlmap->l_addr)
607 table2 = &libtable->table2;
612 if (table2 == NULL) return NULL;
613 Symbol *sym = table2->FindAddr(addr);
614 return sym ? sym->buffer() : NULL;