Added example scripts missing from last commit (NPOTB).
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::MemoryUtils()
49 /* Get pointer to struct that describes all loaded mach-o images in process */
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;
59 MemoryUtils::~MemoryUtils()
61 #if defined PLATFORM_LINUX || defined PLATFORM_APPLE
62 for (size_t i = 0; i < m_SymTables.size(); i++)
64 delete m_SymTables[i];
70 void *MemoryUtils::FindPatternInFile(int fd, const char *pattern, size_t len)
73 size = lseek (fd , 0 , SEEK_END);
75 lseek(fd, 0, SEEK_SET);
80 map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
88 for (register size_t i = 0; i < len; i++)
90 if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
106 void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
112 memset(&lib, 0, sizeof(DynLibInfo));
114 if (!GetLibraryInfo(libPtr, lib))
119 ptr = reinterpret_cast<char *>(lib.baseAddress);
120 end = ptr + lib.memorySize - 1;
125 for (register size_t i = 0; i < len; i++)
127 if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
143 void *MemoryUtils::ResolveSymbol(void *handle, const char *symbol)
145 #ifdef PLATFORM_WINDOWS
147 return GetProcAddress((HMODULE)handle, symbol);
149 #elif defined PLATFORM_LINUX
151 struct link_map *dlmap;
155 Elf32_Ehdr *file_hdr;
156 Elf32_Shdr *sections, *shstrtab_hdr, *symtab_hdr, *strtab_hdr;
158 const char *shstrtab, *strtab;
159 uint16_t section_count;
160 uint32_t symbol_count;
161 LibSymbolTable *libtable;
164 Symbol *symbol_entry;
166 dlmap = (struct link_map *)handle;
172 /* See if we already have a symbol table for this library */
173 for (size_t i = 0; i < m_SymTables.size(); i++)
175 libtable = m_SymTables[i];
176 if (libtable->lib_base == dlmap->l_addr)
178 table = &libtable->table;
179 table2 = &libtable->table2;
184 /* If we don't have a symbol table for this library, then create one */
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);
197 /* See if the symbol is already cached in our table */
198 symbol_entry = table->FindSymbol(symbol, strlen(symbol));
199 if (symbol_entry != NULL)
201 return symbol_entry->address;
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)
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)
222 if (file_hdr->e_shoff == 0 || file_hdr->e_shstrndx == SHN_UNDEF)
224 munmap(file_hdr, dlstat.st_size);
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 = §ions[file_hdr->e_shstrndx];
232 shstrtab = (const char *)(map_base + shstrtab_hdr->sh_offset);
234 /* Iterate sections while looking for ELF symbol table and string table */
235 for (uint16_t i = 0; i < section_count; i++)
237 Elf32_Shdr &hdr = sections[i];
238 const char *section_name = shstrtab + hdr.sh_name;
240 if (strcmp(section_name, ".symtab") == 0)
244 else if (strcmp(section_name, ".strtab") == 0)
250 /* Uh oh, we don't have a symbol table or a string table */
251 if (symtab_hdr == NULL || strtab_hdr == NULL)
253 munmap(file_hdr, dlstat.st_size);
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;
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++)
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;
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))
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)
280 symbol_entry = cur_sym;
281 libtable->last_pos = ++i;
286 libtable->last_pos = symbol_count;
288 munmap(file_hdr, dlstat.st_size);
289 return symbol_entry ? symbol_entry->address : NULL;
291 #elif defined PLATFORM_APPLE
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;
301 uint32_t loadcmd_count;
302 uint32_t symbol_count;
303 LibSymbolTable *libtable;
305 Symbol *symbol_entry;
308 image_count = m_ImageList->infoArrayCount;
313 /* Loop through mach-o images in process.
314 * We can skip index 0 since that is just the executable.
316 for (uint32_t i = 1; i < image_count; i++)
318 const struct dyld_image_info &info = m_ImageList->infoArray[i];
320 /* "Load" each one until we get a matching handle */
321 void *h = dlopen(info.imageFilePath, RTLD_NOLOAD);
324 dlbase = (uintptr_t)info.imageLoadAddress;
334 /* Uh oh, we couldn't find a matching handle */
338 /* See if we already have a symbol table for this library */
339 for (size_t i = 0; i < m_SymTables.size(); i++)
341 libtable = m_SymTables[i];
342 if (libtable->lib_base == dlbase)
344 table = &libtable->table;
349 /* If we don't have a symbol table for this library, then create one */
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);
360 /* See if the symbol is already cached in our table */
361 symbol_entry = table->FindSymbol(symbol, strlen(symbol));
362 if (symbol_entry != NULL)
364 return symbol_entry->address;
367 /* If symbol isn't in our table, then we have to locate it in memory */
369 file_hdr = (struct mach_header *)dlbase;
370 loadcmds = (struct load_command *)(dlbase + sizeof(struct mach_header));
371 loadcmd_count = file_hdr->ncmds;
373 /* Loop through load commands until we find the ones for the symbol table */
374 for (uint32_t i = 0; i < loadcmd_count; i++)
376 if (loadcmds->cmd == LC_SEGMENT && !linkedit_hdr)
378 struct segment_command *seg = (struct segment_command *)loadcmds;
379 if (strcmp(seg->segname, "__LINKEDIT") == 0)
388 else if (loadcmds->cmd == LC_SYMTAB)
390 symtab_hdr = (struct symtab_command *)loadcmds;
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);
401 if (!linkedit_hdr || !symtab_hdr || !symtab_hdr->symoff || !symtab_hdr->stroff)
403 /* Uh oh, no symbol table */
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;
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++)
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;
420 /* Skip symbols that are undefined */
421 if (sym.n_sect == NO_SECT)
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)
430 symbol_entry = cur_sym;
431 libtable->last_pos = ++i;
436 return symbol_entry ? symbol_entry->address : NULL;
441 bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
450 #ifdef PLATFORM_WINDOWS
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;
458 if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
463 baseAddr = reinterpret_cast<uintptr_t>(info.AllocationBase);
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;
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)
477 /* Check architecture, which is 32-bit/x86 right now
478 * Should change this for 64-bit if Valve gets their act together
480 if (file->Machine != IMAGE_FILE_MACHINE_I386)
485 /* For our purposes, this must be a dynamic library */
486 if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
491 /* Finally, we can do this */
492 lib.memorySize = opt->SizeOfImage;
494 #elif defined PLATFORM_LINUX
501 if (!dladdr(libPtr, &info))
506 if (!info.dli_fbase || !info.dli_fname)
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);
515 /* Check ELF magic */
516 if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
521 /* Check ELF version */
522 if (file->e_ident[EI_VERSION] != EV_CURRENT)
527 /* Check ELF architecture, which is 32-bit/x86 right now
528 * Should change this for 64-bit if Valve gets their act together
530 if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
535 /* For our purposes, this must be a dynamic library/shared object */
536 if (file->e_type != ET_DYN)
541 phdrCount = file->e_phnum;
542 phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
544 for (uint16_t i = 0; i < phdrCount; i++)
546 Elf32_Phdr &hdr = phdr[i];
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))
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));
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.
558 lib.memorySize = PAGE_ALIGN_UP(hdr.p_filesz);
563 #elif defined PLATFORM_APPLE
566 struct mach_header *file;
567 struct segment_command *seg;
570 if (!dladdr(libPtr, &info))
575 if (!info.dli_fbase || !info.dli_fname)
580 /* This is for our insane sanity checks :o */
581 baseAddr = (uintptr_t)info.dli_fbase;
582 file = (struct mach_header *)baseAddr;
584 /* Check Mach-O magic */
585 if (file->magic != MH_MAGIC)
590 /* Check architecture (32-bit/x86) */
591 if (file->cputype != CPU_TYPE_I386 || file->cpusubtype != CPU_SUBTYPE_I386_ALL)
596 /* For our purposes, this must be a dynamic library */
597 if (file->filetype != MH_DYLIB)
602 cmd_count = file->ncmds;
603 seg = (struct segment_command *)(baseAddr + sizeof(struct mach_header));
605 /* Add up memory sizes of mapped segments */
606 for (uint32_t i = 0; i < cmd_count; i++)
608 if (seg->cmd == LC_SEGMENT)
610 lib.memorySize += seg->vmsize;
613 seg = (struct segment_command *)((uintptr_t)seg + seg->cmdsize);
618 lib.baseAddress = reinterpret_cast<void *>(baseAddr);
623 const char* MemoryUtils::ResolveAddr(void *handle, void* addr)
625 struct link_map *dlmap;
626 dlmap = (struct link_map *)handle;
628 LibSymbolTable *libtable;
631 for (size_t i = 0; i < m_SymTables.size(); i++)
633 libtable = m_SymTables[i];
634 if (libtable->lib_base == dlmap->l_addr)
636 table2 = &libtable->table2;
641 if (table2 == NULL) return NULL;
642 Symbol *sym = table2->FindAddr(addr);
643 return sym ? sym->buffer() : NULL;