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 <stdbool.h>
23#include <stdlib.h>
24#include <elf.h>
25#include <fcntl.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <sys/mman.h>
29#include <cutils/log.h>
30
31static bool is_elf(Elf32_Ehdr* e) {
32    return (e->e_ident[EI_MAG0] == ELFMAG0 &&
33            e->e_ident[EI_MAG1] == ELFMAG1 &&
34            e->e_ident[EI_MAG2] == ELFMAG2 &&
35            e->e_ident[EI_MAG3] == ELFMAG3);
36}
37
38// Compare function for qsort
39static int qcompar(const void *a, const void *b) {
40    const symbol_t* asym = (const symbol_t*)a;
41    const symbol_t* bsym = (const symbol_t*)b;
42    if (asym->start > bsym->start) return 1;
43    if (asym->start < bsym->start) return -1;
44    return 0;
45}
46
47// Compare function for bsearch
48static int bcompar(const void *key, const void *element) {
49    uintptr_t addr = *(const uintptr_t*)key;
50    const symbol_t* symbol = (const symbol_t*)element;
51    if (addr < symbol->start) return -1;
52    if (addr >= symbol->end) return 1;
53    return 0;
54}
55
56symbol_table_t* load_symbol_table(const char *filename) {
57    symbol_table_t* table = NULL;
58    ALOGV("Loading symbol table from '%s'.", filename);
59
60    int fd = open(filename, O_RDONLY);
61    if (fd < 0) {
62        goto out;
63    }
64
65    struct stat sb;
66    if (fstat(fd, &sb)) {
67        goto out_close;
68    }
69
70    size_t length = sb.st_size;
71    char* base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
72    if (base == MAP_FAILED) {
73        goto out_close;
74    }
75
76    // Parse the file header
77    Elf32_Ehdr *hdr = (Elf32_Ehdr*)base;
78    if (!is_elf(hdr)) {
79        goto out_close;
80    }
81    Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff);
82
83    // Search for the dynamic symbols section
84    int sym_idx = -1;
85    int dynsym_idx = -1;
86    for (Elf32_Half i = 0; i < hdr->e_shnum; i++) {
87        if (shdr[i].sh_type == SHT_SYMTAB) {
88            sym_idx = i;
89        }
90        if (shdr[i].sh_type == SHT_DYNSYM) {
91            dynsym_idx = i;
92        }
93    }
94    if (dynsym_idx == -1 && sym_idx == -1) {
95        goto out_unmap;
96    }
97
98    table = malloc(sizeof(symbol_table_t));
99    if(!table) {
100        goto out_unmap;
101    }
102    table->num_symbols = 0;
103
104    Elf32_Sym *dynsyms = NULL;
105    int dynnumsyms = 0;
106    char *dynstr = NULL;
107    if (dynsym_idx != -1) {
108        dynsyms = (Elf32_Sym*)(base + shdr[dynsym_idx].sh_offset);
109        dynnumsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize;
110        int dynstr_idx = shdr[dynsym_idx].sh_link;
111        dynstr = base + shdr[dynstr_idx].sh_offset;
112    }
113
114    Elf32_Sym *syms = NULL;
115    int numsyms = 0;
116    char *str = NULL;
117    if (sym_idx != -1) {
118        syms = (Elf32_Sym*)(base + shdr[sym_idx].sh_offset);
119        numsyms = shdr[sym_idx].sh_size / shdr[sym_idx].sh_entsize;
120        int str_idx = shdr[sym_idx].sh_link;
121        str = base + shdr[str_idx].sh_offset;
122    }
123
124    int dynsymbol_count = 0;
125    if (dynsym_idx != -1) {
126        // Iterate through the dynamic symbol table, and count how many symbols
127        // are actually defined
128        for (int i = 0; i < dynnumsyms; i++) {
129            if (dynsyms[i].st_shndx != SHN_UNDEF) {
130                dynsymbol_count++;
131            }
132        }
133    }
134
135    size_t symbol_count = 0;
136    if (sym_idx != -1) {
137        // Iterate through the symbol table, and count how many symbols
138        // are actually defined
139        for (int i = 0; i < numsyms; i++) {
140            if (syms[i].st_shndx != SHN_UNDEF
141                    && str[syms[i].st_name]
142                    && syms[i].st_value
143                    && syms[i].st_size) {
144                symbol_count++;
145            }
146        }
147    }
148
149    // Now, create an entry in our symbol table structure for each symbol...
150    table->num_symbols += symbol_count + dynsymbol_count;
151    table->symbols = malloc(table->num_symbols * sizeof(symbol_t));
152    if (!table->symbols) {
153        free(table);
154        table = NULL;
155        goto out_unmap;
156    }
157
158    size_t symbol_index = 0;
159    if (dynsym_idx != -1) {
160        // ...and populate them
161        for (int i = 0; i < dynnumsyms; i++) {
162            if (dynsyms[i].st_shndx != SHN_UNDEF) {
163                table->symbols[symbol_index].name = strdup(dynstr + dynsyms[i].st_name);
164                table->symbols[symbol_index].start = dynsyms[i].st_value;
165                table->symbols[symbol_index].end = dynsyms[i].st_value + dynsyms[i].st_size;
166                ALOGV("  [%d] '%s' 0x%08x-0x%08x (DYNAMIC)",
167                        symbol_index, table->symbols[symbol_index].name,
168                        table->symbols[symbol_index].start, table->symbols[symbol_index].end);
169                symbol_index += 1;
170            }
171        }
172    }
173
174    if (sym_idx != -1) {
175        // ...and populate them
176        for (int i = 0; i < numsyms; i++) {
177            if (syms[i].st_shndx != SHN_UNDEF
178                    && str[syms[i].st_name]
179                    && syms[i].st_value
180                    && syms[i].st_size) {
181                table->symbols[symbol_index].name = strdup(str + syms[i].st_name);
182                table->symbols[symbol_index].start = syms[i].st_value;
183                table->symbols[symbol_index].end = syms[i].st_value + syms[i].st_size;
184                ALOGV("  [%d] '%s' 0x%08x-0x%08x",
185                        symbol_index, table->symbols[symbol_index].name,
186                        table->symbols[symbol_index].start, table->symbols[symbol_index].end);
187                symbol_index += 1;
188            }
189        }
190    }
191
192    // Sort the symbol table entries, so they can be bsearched later
193    qsort(table->symbols, table->num_symbols, sizeof(symbol_t), qcompar);
194
195out_unmap:
196    munmap(base, length);
197
198out_close:
199    close(fd);
200
201out:
202    return table;
203}
204
205void free_symbol_table(symbol_table_t* table) {
206    if (table) {
207        for (size_t i = 0; i < table->num_symbols; i++) {
208            free(table->symbols[i].name);
209        }
210        free(table->symbols);
211        free(table);
212    }
213}
214
215const symbol_t* find_symbol(const symbol_table_t* table, uintptr_t addr) {
216    if (!table) return NULL;
217    return (const symbol_t*)bsearch(&addr, table->symbols, table->num_symbols,
218            sizeof(symbol_t), bcompar);
219}
220