blob: e76df3355d3cde8125aa4e66f2ce650ce73bf371 [file] [log] [blame]
Meng Huae7b91b2009-11-05 16:10:50 -06001#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"
Bruce Beare6cc49232010-10-13 16:11:15 -07008#include "utility.h"
Meng Huae7b91b2009-11-05 16:10:50 -06009
10#include <linux/elf.h>
11
12// Compare func for qsort
13static int qcompar(const void *a, const void *b)
14{
15 return ((struct symbol*)a)->addr - ((struct symbol*)b)->addr;
16}
17
18// Compare func for bsearch
19static int bcompar(const void *addr, const void *element)
20{
21 struct symbol *symbol = (struct symbol*)element;
22
23 if((unsigned int)addr < symbol->addr) {
24 return -1;
25 }
26
27 if((unsigned int)addr - symbol->addr >= symbol->size) {
28 return 1;
29 }
30
31 return 0;
32}
33
34/*
35 * Create a symbol table from a given file
36 *
37 * Parameters:
38 * filename - Filename to process
39 *
40 * Returns:
41 * A newly-allocated SymbolTable structure, or NULL if error.
42 * Free symbol table with symbol_table_free()
43 */
44struct symbol_table *symbol_table_create(const char *filename)
45{
46 struct symbol_table *table = NULL;
47
48 // Open the file, and map it into memory
49 struct stat sb;
50 int length;
51 char *base;
52
Bruce Beare6cc49232010-10-13 16:11:15 -070053 XLOG("Creating symbol table for %s\n", filename);
Meng Huae7b91b2009-11-05 16:10:50 -060054 int fd = open(filename, O_RDONLY);
55
56 if(fd < 0) {
57 goto out;
58 }
59
60 fstat(fd, &sb);
61 length = sb.st_size;
62
63 base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
64
65 if(!base) {
66 goto out_close;
67 }
68
69 // Parse the file header
70 Elf32_Ehdr *hdr = (Elf32_Ehdr*)base;
71 Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff);
72
73 // Search for the dynamic symbols section
Bruce Beare6cc49232010-10-13 16:11:15 -070074 int sym_idx = -1;
Meng Huae7b91b2009-11-05 16:10:50 -060075 int dynsym_idx = -1;
76 int i;
77
78 for(i = 0; i < hdr->e_shnum; i++) {
Bruce Beare6cc49232010-10-13 16:11:15 -070079 if(shdr[i].sh_type == SHT_SYMTAB ) {
80 sym_idx = i;
81 }
Meng Huae7b91b2009-11-05 16:10:50 -060082 if(shdr[i].sh_type == SHT_DYNSYM ) {
83 dynsym_idx = i;
84 }
85 }
Bruce Beare6cc49232010-10-13 16:11:15 -070086 if ((dynsym_idx == -1) && (sym_idx == -1)) {
Meng Huae7b91b2009-11-05 16:10:50 -060087 goto out_unmap;
88 }
89
Meng Huae7b91b2009-11-05 16:10:50 -060090 table = malloc(sizeof(struct symbol_table));
91 if(!table) {
92 goto out_unmap;
93 }
Bruce Beare6cc49232010-10-13 16:11:15 -070094 table->name = strdup(filename);
Meng Huae7b91b2009-11-05 16:10:50 -060095 table->num_symbols = 0;
96
Bruce Beare6cc49232010-10-13 16:11:15 -070097 Elf32_Sym *dynsyms = (Elf32_Sym*)(base + shdr[dynsym_idx].sh_offset);
98 Elf32_Sym *syms = (Elf32_Sym*)(base + shdr[sym_idx].sh_offset);
99
100 int dynnumsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize;
101 int numsyms = shdr[sym_idx].sh_size / shdr[sym_idx].sh_entsize;
Meng Huae7b91b2009-11-05 16:10:50 -0600102
103 int dynstr_idx = shdr[dynsym_idx].sh_link;
Bruce Beare6cc49232010-10-13 16:11:15 -0700104 int str_idx = shdr[sym_idx].sh_link;
105
Meng Huae7b91b2009-11-05 16:10:50 -0600106 char *dynstr = base + shdr[dynstr_idx].sh_offset;
Bruce Beare6cc49232010-10-13 16:11:15 -0700107 char *str = base + shdr[str_idx].sh_offset;
108
109 int symbol_count = 0;
110 int dynsymbol_count = 0;
111
112 if (dynsym_idx != -1) {
113 // Iterate through the dynamic symbol table, and count how many symbols
114 // are actually defined
115 for(i = 0; i < dynnumsyms; i++) {
116 if(dynsyms[i].st_shndx != SHN_UNDEF) {
117 dynsymbol_count++;
118 }
119 }
120 XLOG("Dynamic Symbol count: %d\n", dynsymbol_count);
121 }
122
123 if (sym_idx != -1) {
124 // Iterate through the symbol table, and count how many symbols
125 // are actually defined
126 for(i = 0; i < numsyms; i++) {
127 if((syms[i].st_shndx != SHN_UNDEF) &&
128 (strlen(str+syms[i].st_name)) &&
129 (syms[i].st_value != 0) && (syms[i].st_size != 0)) {
130 symbol_count++;
131 }
132 }
133 XLOG("Symbol count: %d\n", symbol_count);
134 }
Meng Huae7b91b2009-11-05 16:10:50 -0600135
136 // Now, create an entry in our symbol table structure for each symbol...
Bruce Beare6cc49232010-10-13 16:11:15 -0700137 table->num_symbols += symbol_count + dynsymbol_count;;
Meng Huae7b91b2009-11-05 16:10:50 -0600138 table->symbols = malloc(table->num_symbols * sizeof(struct symbol));
139 if(!table->symbols) {
140 free(table);
141 table = NULL;
142 goto out_unmap;
143 }
144
Bruce Beare6cc49232010-10-13 16:11:15 -0700145
Meng Huae7b91b2009-11-05 16:10:50 -0600146 int j = 0;
Bruce Beare6cc49232010-10-13 16:11:15 -0700147 if (dynsym_idx != -1) {
148 // ...and populate them
149 for(i = 0; i < dynnumsyms; i++) {
150 if(dynsyms[i].st_shndx != SHN_UNDEF) {
151 table->symbols[j].name = strdup(dynstr + dynsyms[i].st_name);
152 table->symbols[j].addr = dynsyms[i].st_value;
153 table->symbols[j].size = dynsyms[i].st_size;
154 XLOG("name: %s, addr: %x, size: %x\n",
155 table->symbols[j].name, table->symbols[j].addr, table->symbols[j].size);
156 j++;
157 }
158 }
159 }
160
161 if (sym_idx != -1) {
162 // ...and populate them
163 for(i = 0; i < numsyms; i++) {
164 if((syms[i].st_shndx != SHN_UNDEF) &&
165 (strlen(str+syms[i].st_name)) &&
166 (syms[i].st_value != 0) && (syms[i].st_size != 0)) {
167 table->symbols[j].name = strdup(str + syms[i].st_name);
168 table->symbols[j].addr = syms[i].st_value;
169 table->symbols[j].size = syms[i].st_size;
170 XLOG("name: %s, addr: %x, size: %x\n",
171 table->symbols[j].name, table->symbols[j].addr, table->symbols[j].size);
172 j++;
173 }
Meng Huae7b91b2009-11-05 16:10:50 -0600174 }
175 }
176
177 // Sort the symbol table entries, so they can be bsearched later
178 qsort(table->symbols, table->num_symbols, sizeof(struct symbol), qcompar);
179
180out_unmap:
181 munmap(base, length);
182
183out_close:
184 close(fd);
185
186out:
187 return table;
188}
189
190/*
191 * Free a symbol table
192 *
193 * Parameters:
194 * table - Table to free
195 */
196void symbol_table_free(struct symbol_table *table)
197{
198 int i;
199
200 if(!table) {
201 return;
202 }
203
204 for(i=0; i<table->num_symbols; i++) {
205 free(table->symbols[i].name);
206 }
207
208 free(table->symbols);
209 free(table);
210}
211
212/*
213 * Search for an address in the symbol table
214 *
215 * Parameters:
216 * table - Table to search in
217 * addr - Address to search for.
218 *
219 * Returns:
220 * A pointer to the Symbol structure corresponding to the
221 * symbol which contains this address, or NULL if no symbol
222 * contains it.
223 */
224const struct symbol *symbol_table_lookup(struct symbol_table *table, unsigned int addr)
225{
226 if(!table) {
227 return NULL;
228 }
229
230 return bsearch((void*)addr, table->symbols, table->num_symbols, sizeof(struct symbol), bcompar);
231}