1 | /*
|
---|
2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup debug
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Handling of ELF symbol tables.
|
---|
33 | *
|
---|
34 | * This module allows one to load a symbol table from an ELF file and
|
---|
35 | * use it to lookup symbol names/addresses in both directions.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <elf/elf.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <stddef.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <vfs/vfs.h>
|
---|
44 |
|
---|
45 | #include "include/symtab.h"
|
---|
46 |
|
---|
47 | static int elf_hdr_check(elf_header_t *hdr);
|
---|
48 | static int section_hdr_load(int fd, const elf_header_t *ehdr, int idx,
|
---|
49 | elf_section_header_t *shdr);
|
---|
50 | static int chunk_load(int fd, off64_t start, size_t size, void **ptr);
|
---|
51 |
|
---|
52 | /** Load symbol table from an ELF file.
|
---|
53 | *
|
---|
54 | * @param file_name Name of the ELF file to read from.
|
---|
55 | * @param symtab Place to save pointer to new symtab structure.
|
---|
56 | *
|
---|
57 | * @return EOK on success, ENOENT if file could not be open,
|
---|
58 | * ENOTSUP if file parsing failed.
|
---|
59 | */
|
---|
60 | int symtab_load(const char *file_name, symtab_t **symtab)
|
---|
61 | {
|
---|
62 | symtab_t *stab;
|
---|
63 | elf_header_t elf_hdr;
|
---|
64 | elf_section_header_t sec_hdr;
|
---|
65 | off64_t shstrt_start;
|
---|
66 | size_t shstrt_size;
|
---|
67 | char *shstrt, *sec_name;
|
---|
68 | void *data;
|
---|
69 | aoff64_t pos = 0;
|
---|
70 |
|
---|
71 | int fd;
|
---|
72 | int rc;
|
---|
73 | int i;
|
---|
74 |
|
---|
75 | bool load_sec, sec_is_symtab;
|
---|
76 |
|
---|
77 | *symtab = NULL;
|
---|
78 |
|
---|
79 | stab = calloc(1, sizeof(symtab_t));
|
---|
80 | if (stab == NULL)
|
---|
81 | return ENOMEM;
|
---|
82 |
|
---|
83 | fd = vfs_lookup_open(file_name, WALK_REGULAR, MODE_READ);
|
---|
84 | if (fd < 0) {
|
---|
85 | printf("failed opening file\n");
|
---|
86 | free(stab);
|
---|
87 | return ENOENT;
|
---|
88 | }
|
---|
89 |
|
---|
90 | rc = vfs_read(fd, &pos, &elf_hdr, sizeof(elf_header_t));
|
---|
91 | if (rc != sizeof(elf_header_t)) {
|
---|
92 | printf("failed reading elf header\n");
|
---|
93 | free(stab);
|
---|
94 | return EIO;
|
---|
95 | }
|
---|
96 |
|
---|
97 | rc = elf_hdr_check(&elf_hdr);
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf("failed header check\n");
|
---|
100 | free(stab);
|
---|
101 | return ENOTSUP;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Load section header string table.
|
---|
106 | */
|
---|
107 |
|
---|
108 | rc = section_hdr_load(fd, &elf_hdr, elf_hdr.e_shstrndx, &sec_hdr);
|
---|
109 | if (rc != EOK) {
|
---|
110 | printf("failed reading shstrt header\n");
|
---|
111 | free(stab);
|
---|
112 | return ENOTSUP;
|
---|
113 | }
|
---|
114 |
|
---|
115 | shstrt_start = sec_hdr.sh_offset;
|
---|
116 | shstrt_size = sec_hdr.sh_size;
|
---|
117 |
|
---|
118 | rc = chunk_load(fd, shstrt_start, shstrt_size, (void **) &shstrt);
|
---|
119 | if (rc != EOK) {
|
---|
120 | printf("failed loading shstrt\n");
|
---|
121 | free(stab);
|
---|
122 | return ENOTSUP;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* Read all section headers. */
|
---|
126 | for (i = 0; i < elf_hdr.e_shnum; ++i) {
|
---|
127 | rc = section_hdr_load(fd, &elf_hdr, i, &sec_hdr);
|
---|
128 | if (rc != EOK) {
|
---|
129 | free(shstrt);
|
---|
130 | free(stab);
|
---|
131 | return ENOTSUP;
|
---|
132 | }
|
---|
133 |
|
---|
134 | sec_name = shstrt + sec_hdr.sh_name;
|
---|
135 | if (str_cmp(sec_name, ".symtab") == 0 &&
|
---|
136 | sec_hdr.sh_type == SHT_SYMTAB) {
|
---|
137 | load_sec = true;
|
---|
138 | sec_is_symtab = true;
|
---|
139 | } else if (str_cmp(sec_name, ".strtab") == 0 &&
|
---|
140 | sec_hdr.sh_type == SHT_STRTAB) {
|
---|
141 | load_sec = true;
|
---|
142 | sec_is_symtab = false;
|
---|
143 | } else {
|
---|
144 | load_sec = false;
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (load_sec) {
|
---|
148 | rc = chunk_load(fd, sec_hdr.sh_offset, sec_hdr.sh_size,
|
---|
149 | &data);
|
---|
150 | if (rc != EOK) {
|
---|
151 | free(shstrt);
|
---|
152 | free(stab);
|
---|
153 | return ENOTSUP;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (sec_is_symtab) {
|
---|
157 | stab->sym = data;
|
---|
158 | stab->sym_size = sec_hdr.sh_size;
|
---|
159 | } else {
|
---|
160 | stab->strtab = data;
|
---|
161 | stab->strtab_size = sec_hdr.sh_size;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | free(shstrt);
|
---|
167 | vfs_put(fd);
|
---|
168 |
|
---|
169 | if (stab->sym == NULL || stab->strtab == NULL) {
|
---|
170 | /* Tables not found. */
|
---|
171 | printf("Symbol table or string table section not found\n");
|
---|
172 | free(stab);
|
---|
173 | return ENOTSUP;
|
---|
174 | }
|
---|
175 |
|
---|
176 | *symtab = stab;
|
---|
177 |
|
---|
178 | return EOK;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Delete a symtab structure.
|
---|
182 | *
|
---|
183 | * Deallocates all resources used by the symbol table.
|
---|
184 | */
|
---|
185 | void symtab_delete(symtab_t *st)
|
---|
186 | {
|
---|
187 | free(st->sym);
|
---|
188 | st->sym = NULL;
|
---|
189 |
|
---|
190 | free(st->strtab);
|
---|
191 | st->strtab = NULL;
|
---|
192 |
|
---|
193 | free(st);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Convert symbol name to address.
|
---|
197 | *
|
---|
198 | * @param st Symbol table.
|
---|
199 | * @param name Name of the symbol.
|
---|
200 | * @param addr Place to store address for symbol, if found.
|
---|
201 | *
|
---|
202 | * @return EOK on success, ENOENT if no such symbol was found.
|
---|
203 | */
|
---|
204 | int symtab_name_to_addr(symtab_t *st, const char *name, uintptr_t *addr)
|
---|
205 | {
|
---|
206 | size_t i;
|
---|
207 | char *sname;
|
---|
208 | unsigned stype;
|
---|
209 |
|
---|
210 | for (i = 0; i < st->sym_size / sizeof(elf_symbol_t); ++i) {
|
---|
211 | if (st->sym[i].st_name == 0)
|
---|
212 | continue;
|
---|
213 |
|
---|
214 | stype = ELF_ST_TYPE(st->sym[i].st_info);
|
---|
215 | if (stype != STT_OBJECT && stype != STT_FUNC)
|
---|
216 | continue;
|
---|
217 |
|
---|
218 | sname = st->strtab + st->sym[i].st_name;
|
---|
219 |
|
---|
220 | if (str_cmp(sname, name) == 0) {
|
---|
221 | *addr = st->sym[i].st_value;
|
---|
222 | return EOK;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | return ENOENT;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Convert symbol address to name.
|
---|
230 | *
|
---|
231 | * This function finds the symbol which starts at the highest address
|
---|
232 | * less than or equal to @a addr.
|
---|
233 | *
|
---|
234 | * @param st Symbol table.
|
---|
235 | * @param addr Address for lookup.
|
---|
236 | * @param name Place to store pointer name of symbol, if found.
|
---|
237 | * This is valid while @a st exists.
|
---|
238 | *
|
---|
239 | * @return EOK on success or ENOENT if no matching symbol was found.
|
---|
240 | */
|
---|
241 | int symtab_addr_to_name(symtab_t *st, uintptr_t addr, char **name,
|
---|
242 | size_t *offs)
|
---|
243 | {
|
---|
244 | size_t i;
|
---|
245 | uintptr_t saddr, best_addr;
|
---|
246 | char *sname, *best_name;
|
---|
247 | unsigned stype;
|
---|
248 |
|
---|
249 | best_name = NULL;
|
---|
250 | best_addr = 0;
|
---|
251 |
|
---|
252 | for (i = 0; i < st->sym_size / sizeof(elf_symbol_t); ++i) {
|
---|
253 | if (st->sym[i].st_name == 0)
|
---|
254 | continue;
|
---|
255 |
|
---|
256 | stype = ELF_ST_TYPE(st->sym[i].st_info);
|
---|
257 | if (stype != STT_OBJECT && stype != STT_FUNC &&
|
---|
258 | stype != STT_NOTYPE) {
|
---|
259 | continue;
|
---|
260 | }
|
---|
261 |
|
---|
262 | saddr = st->sym[i].st_value;
|
---|
263 | sname = st->strtab + st->sym[i].st_name;
|
---|
264 |
|
---|
265 | /* An ugly hack to filter out some special ARM symbols. */
|
---|
266 | if (sname[0] == '$')
|
---|
267 | continue;
|
---|
268 |
|
---|
269 | if (saddr <= addr && (best_name == NULL || saddr > best_addr)) {
|
---|
270 | best_name = sname;
|
---|
271 | best_addr = saddr;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (best_name == NULL)
|
---|
276 | return ENOENT;
|
---|
277 |
|
---|
278 | *name = best_name;
|
---|
279 | *offs = addr - best_addr;
|
---|
280 | return EOK;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /** Check if ELF header is valid.
|
---|
284 | *
|
---|
285 | * @return EOK on success or negative error code.
|
---|
286 | */
|
---|
287 | static int elf_hdr_check(elf_header_t *ehdr)
|
---|
288 | {
|
---|
289 | /* TODO */
|
---|
290 | return EOK;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Load ELF section header.
|
---|
294 | *
|
---|
295 | * @param fd File descriptor of ELF file.
|
---|
296 | * @param elf_hdr Pointer to ELF file header in memory.
|
---|
297 | * @param idx Index of section whose header to load (0 = first).
|
---|
298 | * @param sec_hdr Place to store section header data.
|
---|
299 | *
|
---|
300 | * @return EOK on success or EIO if I/O failed.
|
---|
301 | */
|
---|
302 | static int section_hdr_load(int fd, const elf_header_t *elf_hdr, int idx,
|
---|
303 | elf_section_header_t *sec_hdr)
|
---|
304 | {
|
---|
305 | int rc;
|
---|
306 | aoff64_t pos = elf_hdr->e_shoff + idx * sizeof(elf_section_header_t);
|
---|
307 |
|
---|
308 | rc = vfs_read(fd, &pos, sec_hdr, sizeof(elf_section_header_t));
|
---|
309 | if (rc != sizeof(elf_section_header_t))
|
---|
310 | return EIO;
|
---|
311 |
|
---|
312 | return EOK;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /** Load a segment of bytes from a file and return it as a new memory block.
|
---|
316 | *
|
---|
317 | * This function fails if it cannot read exactly @a size bytes from the file.
|
---|
318 | *
|
---|
319 | * @param fd File to read from.
|
---|
320 | * @param start Position in file where to start reading.
|
---|
321 | * @param size Number of bytes to read.
|
---|
322 | * @param ptr Place to store pointer to newly allocated block.
|
---|
323 | *
|
---|
324 | * @return EOK on success or EIO on failure.
|
---|
325 | */
|
---|
326 | static int chunk_load(int fd, off64_t start, size_t size, void **ptr)
|
---|
327 | {
|
---|
328 | ssize_t rc;
|
---|
329 | aoff64_t pos = start;
|
---|
330 |
|
---|
331 | *ptr = malloc(size);
|
---|
332 | if (*ptr == NULL) {
|
---|
333 | printf("failed allocating memory\n");
|
---|
334 | return ENOMEM;
|
---|
335 | }
|
---|
336 |
|
---|
337 | rc = vfs_read(fd, &pos, *ptr, size);
|
---|
338 | if (rc != (ssize_t) size) {
|
---|
339 | printf("failed reading chunk\n");
|
---|
340 | free(*ptr);
|
---|
341 | *ptr = NULL;
|
---|
342 | return EIO;
|
---|
343 | }
|
---|
344 |
|
---|
345 | return EOK;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /** @}
|
---|
349 | */
|
---|