symbol_table.c revision 501edd29b823ce1301d2effdd3a9e4b6e2b20b76
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Corkscrew"
18//#define LOG_NDEBUG 0
19
20#include <corkscrew/symbol_table.h>
21
22#include <stdlib.h>
23#include <fcntl.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/mman.h>
27#include <sys/exec_elf.h>
28#include <cutils/log.h>
29
30// Compare function for qsort
31static int qcompar(const void *a, const void *b) {
32    const symbol_t* asym = (const symbol_t*)a;
33    const symbol_t* bsym = (const symbol_t*)b;
34    if (asym->start > bsym->start) return 1;
35    if (asym->start < bsym->start) return -1;
36    return 0;
37}
38
39// Compare function for bsearch
40static int bcompar(const void *key, const void *element) {
41    uintptr_t addr = *(const uintptr_t*)key;
42    const symbol_t* symbol = (const symbol_t*)element;
43    if (addr < symbol->start) return -1;
44    if (addr >= symbol->end) return 1;
45    return 0;
46}
47
48symbol_table_t* load_symbol_table(const char *filename) {
49    symbol_table_t* table = NULL;
50
51    int fd = open(filename, O_RDONLY);
52    if (fd < 0) {
53        goto out;
54    }
55
56    struct stat sb;
57    if (fstat(fd, &sb)) {
58        goto out_close;
59    }
60
61    size_t length = sb.st_size;
62    char* base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
63    if (base == MAP_FAILED) {
64        goto out_close;
65    }
66
67    // Parse the file header
68    Elf32_Ehdr *hdr = (Elf32_Ehdr*)base;
69    if (!IS_ELF(*hdr)) {
70        goto out_close;
71    }
72    Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff);
73
74    // Search for the dynamic symbols section
75    int sym_idx = -1;
76    int dynsym_idx = -1;
77    for (Elf32_Half i = 0; i < hdr->e_shnum; i++) {
78        if (shdr[i].sh_type == SHT_SYMTAB) {
79            sym_idx = i;
80        }
81        if (shdr[i].sh_type == SHT_DYNSYM) {
82            dynsym_idx = i;
83        }
84    }
85    if (dynsym_idx == -1 && sym_idx == -1) {
86        goto out_unmap;
87    }
88
89    table = malloc(sizeof(symbol_table_t));
90    if(!table) {
91        goto out_unmap;
92    }
93    table->num_symbols = 0;
94
95    Elf32_Sym *dynsyms = NULL;
96    int dynnumsyms = 0;
97    char *dynstr = NULL;
98    if (dynsym_idx != -1) {
99        dynsyms = (Elf32_Sym*)(base + shdr[dynsym_idx].sh_offset);
100        dynnumsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize;
101        int dynstr_idx = shdr[dynsym_idx].sh_link;
102        dynstr = base + shdr[dynstr_idx].sh_offset;
103    }
104
105    Elf32_Sym *syms = NULL;
106    int numsyms = 0;
107    char *str = NULL;
108    if (sym_idx != -1) {
109        syms = (Elf32_Sym*)(base + shdr[sym_idx].sh_offset);
110        numsyms = shdr[sym_idx].sh_size / shdr[sym_idx].sh_entsize;
111        int str_idx = shdr[sym_idx].sh_link;
112        str = base + shdr[str_idx].sh_offset;
113    }
114
115    int dynsymbol_count = 0;
116    if (dynsym_idx != -1) {
117        // Iterate through the dynamic symbol table, and count how many symbols
118        // are actually defined
119        for (int i = 0; i < dynnumsyms; i++) {
120            if (dynsyms[i].st_shndx != SHN_UNDEF) {
121                dynsymbol_count++;
122            }
123        }
124    }
125
126    size_t symbol_count = 0;
127    if (sym_idx != -1) {
128        // Iterate through the symbol table, and count how many symbols
129        // are actually defined
130        for (int i = 0; i < numsyms; i++) {
131            if (syms[i].st_shndx != SHN_UNDEF
132                    && str[syms[i].st_name]
133                    && syms[i].st_value
134                    && syms[i].st_size) {
135                symbol_count++;
136            }
137        }
138    }
139
140    // Now, create an entry in our symbol table structure for each symbol...
141    table->num_symbols += symbol_count + dynsymbol_count;
142    table->symbols = malloc(table->num_symbols * sizeof(symbol_t));
143    if (!table->symbols) {
144        free(table);
145        table = NULL;
146        goto out_unmap;
147    }
148
149    size_t symbol_index = 0;
150    if (dynsym_idx != -1) {
151        // ...and populate them
152        for (int i = 0; i < dynnumsyms; i++) {
153            if (dynsyms[i].st_shndx != SHN_UNDEF) {
154                table->symbols[symbol_index].name = strdup(dynstr + dynsyms[i].st_name);
155                table->symbols[symbol_index].start = dynsyms[i].st_value;
156                table->symbols[symbol_index].end = dynsyms[i].st_value + dynsyms[i].st_size;
157                symbol_index += 1;
158            }
159        }
160    }
161
162    if (sym_idx != -1) {
163        // ...and populate them
164        for (int i = 0; i < numsyms; i++) {
165            if (syms[i].st_shndx != SHN_UNDEF
166                    && str[syms[i].st_name]
167                    && syms[i].st_value
168                    && syms[i].st_size) {
169                table->symbols[symbol_index].name = strdup(str + syms[i].st_name);
170                table->symbols[symbol_index].start = syms[i].st_value;
171                table->symbols[symbol_index].end = syms[i].st_value + syms[i].st_size;
172                symbol_index += 1;
173            }
174        }
175    }
176
177    // Sort the symbol table entries, so they can be bsearched later
178    qsort(table->symbols, table->num_symbols, sizeof(symbol_t), qcompar);
179
180out_unmap:
181    munmap(base, length);
182
183out_close:
184    close(fd);
185
186out:
187    return table;
188}
189
190void free_symbol_table(symbol_table_t* table) {
191    if (table) {
192        for (size_t i = 0; i < table->num_symbols; i++) {
193            free(table->symbols[i].name);
194        }
195        free(table->symbols);
196        free(table);
197    }
198}
199
200const symbol_t* find_symbol(const symbol_table_t* table, uintptr_t addr) {
201    if (!table) return NULL;
202    return (const symbol_t*)bsearch(&addr, table->symbols, table->num_symbols,
203            sizeof(symbol_t), bcompar);
204}
205