Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame^] | 1 | #include <stdlib.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/mman.h> |
| 6 | |
| 7 | #include "symbol_table.h" |
| 8 | |
| 9 | #include <linux/elf.h> |
| 10 | |
| 11 | // Compare func for qsort |
| 12 | static int qcompar(const void *a, const void *b) |
| 13 | { |
| 14 | return ((struct symbol*)a)->addr - ((struct symbol*)b)->addr; |
| 15 | } |
| 16 | |
| 17 | // Compare func for bsearch |
| 18 | static int bcompar(const void *addr, const void *element) |
| 19 | { |
| 20 | struct symbol *symbol = (struct symbol*)element; |
| 21 | |
| 22 | if((unsigned int)addr < symbol->addr) { |
| 23 | return -1; |
| 24 | } |
| 25 | |
| 26 | if((unsigned int)addr - symbol->addr >= symbol->size) { |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Create a symbol table from a given file |
| 35 | * |
| 36 | * Parameters: |
| 37 | * filename - Filename to process |
| 38 | * |
| 39 | * Returns: |
| 40 | * A newly-allocated SymbolTable structure, or NULL if error. |
| 41 | * Free symbol table with symbol_table_free() |
| 42 | */ |
| 43 | struct symbol_table *symbol_table_create(const char *filename) |
| 44 | { |
| 45 | struct symbol_table *table = NULL; |
| 46 | |
| 47 | // Open the file, and map it into memory |
| 48 | struct stat sb; |
| 49 | int length; |
| 50 | char *base; |
| 51 | |
| 52 | int fd = open(filename, O_RDONLY); |
| 53 | |
| 54 | if(fd < 0) { |
| 55 | goto out; |
| 56 | } |
| 57 | |
| 58 | fstat(fd, &sb); |
| 59 | length = sb.st_size; |
| 60 | |
| 61 | base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 62 | |
| 63 | if(!base) { |
| 64 | goto out_close; |
| 65 | } |
| 66 | |
| 67 | // Parse the file header |
| 68 | Elf32_Ehdr *hdr = (Elf32_Ehdr*)base; |
| 69 | Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff); |
| 70 | |
| 71 | // Search for the dynamic symbols section |
| 72 | int dynsym_idx = -1; |
| 73 | int i; |
| 74 | |
| 75 | for(i = 0; i < hdr->e_shnum; i++) { |
| 76 | if(shdr[i].sh_type == SHT_DYNSYM ) { |
| 77 | dynsym_idx = i; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if(dynsym_idx == -1) { |
| 82 | goto out_unmap; |
| 83 | } |
| 84 | |
| 85 | Elf32_Sym *dynsyms = (Elf32_Sym*)(base + shdr[dynsym_idx].sh_offset); |
| 86 | int numsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize; |
| 87 | |
| 88 | table = malloc(sizeof(struct symbol_table)); |
| 89 | if(!table) { |
| 90 | goto out_unmap; |
| 91 | } |
| 92 | table->num_symbols = 0; |
| 93 | |
| 94 | // Iterate through the dynamic symbol table, and count how many symbols |
| 95 | // are actually defined |
| 96 | for(i = 0; i < numsyms; i++) { |
| 97 | if(dynsyms[i].st_shndx != SHN_UNDEF) { |
| 98 | table->num_symbols++; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | int dynstr_idx = shdr[dynsym_idx].sh_link; |
| 103 | char *dynstr = base + shdr[dynstr_idx].sh_offset; |
| 104 | |
| 105 | // Now, create an entry in our symbol table structure for each symbol... |
| 106 | table->symbols = malloc(table->num_symbols * sizeof(struct symbol)); |
| 107 | if(!table->symbols) { |
| 108 | free(table); |
| 109 | table = NULL; |
| 110 | goto out_unmap; |
| 111 | } |
| 112 | |
| 113 | // ...and populate them |
| 114 | int j = 0; |
| 115 | for(i = 0; i < numsyms; i++) { |
| 116 | if(dynsyms[i].st_shndx != SHN_UNDEF) { |
| 117 | table->symbols[j].name = strdup(dynstr + dynsyms[i].st_name); |
| 118 | table->symbols[j].addr = dynsyms[i].st_value; |
| 119 | table->symbols[j].size = dynsyms[i].st_size; |
| 120 | j++; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Sort the symbol table entries, so they can be bsearched later |
| 125 | qsort(table->symbols, table->num_symbols, sizeof(struct symbol), qcompar); |
| 126 | |
| 127 | out_unmap: |
| 128 | munmap(base, length); |
| 129 | |
| 130 | out_close: |
| 131 | close(fd); |
| 132 | |
| 133 | out: |
| 134 | return table; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Free a symbol table |
| 139 | * |
| 140 | * Parameters: |
| 141 | * table - Table to free |
| 142 | */ |
| 143 | void symbol_table_free(struct symbol_table *table) |
| 144 | { |
| 145 | int i; |
| 146 | |
| 147 | if(!table) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | for(i=0; i<table->num_symbols; i++) { |
| 152 | free(table->symbols[i].name); |
| 153 | } |
| 154 | |
| 155 | free(table->symbols); |
| 156 | free(table); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Search for an address in the symbol table |
| 161 | * |
| 162 | * Parameters: |
| 163 | * table - Table to search in |
| 164 | * addr - Address to search for. |
| 165 | * |
| 166 | * Returns: |
| 167 | * A pointer to the Symbol structure corresponding to the |
| 168 | * symbol which contains this address, or NULL if no symbol |
| 169 | * contains it. |
| 170 | */ |
| 171 | const struct symbol *symbol_table_lookup(struct symbol_table *table, unsigned int addr) |
| 172 | { |
| 173 | if(!table) { |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | return bsearch((void*)addr, table->symbols, table->num_symbols, sizeof(struct symbol), bcompar); |
| 178 | } |