source: mainline/uspace/app/taskdump/symtab.c@ f27ada7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f27ada7 was 83349b03, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Ignore symbols with type other than 'object' or 'function'.

  • Property mode set to 100644
File size: 8.8 KB
Line 
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 <stdio.h>
39#include <stdlib.h>
40#include <errno.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <fcntl.h>
44
45#include <elf.h>
46#include "include/symtab.h"
47
48static int elf_hdr_check(elf_header_t *hdr);
49static int section_hdr_load(int fd, const elf_header_t *ehdr, int idx,
50 elf_section_header_t *shdr);
51static int chunk_load(int fd, off_t start, off_t size, void **ptr);
52static int read_all(int fd, void *buf, size_t len);
53
54/** Load symbol table from an ELF file.
55 *
56 * @param file_name Name of the ELF file to read from.
57 * @param symtab Place to save pointer to new symtab structure.
58 *
59 * @return EOK on success, ENOENT if file could not be open,
60 * ENOTSUP if file parsing failed.
61 */
62int symtab_load(const char *file_name, symtab_t **symtab)
63{
64 symtab_t *stab;
65 elf_header_t elf_hdr;
66 elf_section_header_t sec_hdr;
67 off_t shstrt_start, shstrt_size;
68 char *shstrt, *sec_name;
69 void *data;
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 = open(file_name, O_RDONLY);
84 if (fd < 0) {
85 printf("failed opening file\n");
86 free(stab);
87 return ENOENT;
88 }
89
90 rc = read_all(fd, &elf_hdr, sizeof(elf_header_t));
91 if (rc != EOK) {
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 close(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 */
185void 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 */
204int symtab_name_to_addr(symtab_t *st, 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 */
241int 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 continue;
259
260 saddr = st->sym[i].st_value;
261 sname = st->strtab + st->sym[i].st_name;
262
263 if (best_name == NULL || (saddr <= addr && saddr > best_addr)) {
264 best_name = sname;
265 best_addr = saddr;
266 }
267 }
268
269 if (best_name == NULL)
270 return ENOENT;
271
272 *name = best_name;
273 *offs = addr - best_addr;
274 return EOK;
275}
276
277/** Check if ELF header is valid.
278 *
279 * @return EOK on success or negative error code.
280 */
281static int elf_hdr_check(elf_header_t *ehdr)
282{
283 /* TODO */
284 return EOK;
285}
286
287/** Load ELF section header.
288 *
289 * @param fd File descriptor of ELF file.
290 * @param elf_hdr Pointer to ELF file header in memory.
291 * @param idx Index of section whose header to load (0 = first).
292 * @param sec_hdr Place to store section header data.
293 *
294 * @return EOK on success or EIO if I/O failed.
295 */
296static int section_hdr_load(int fd, const elf_header_t *elf_hdr, int idx,
297 elf_section_header_t *sec_hdr)
298{
299 int rc;
300
301 rc = lseek(fd, elf_hdr->e_shoff + idx * sizeof(elf_section_header_t),
302 SEEK_SET);
303 if (rc == (off_t) -1)
304 return EIO;
305
306 rc = read_all(fd, sec_hdr, sizeof(elf_section_header_t));
307 if (rc != EOK)
308 return EIO;
309
310 return EOK;
311}
312
313/** Load a segment of bytes from a file and return it as a new memory block.
314 *
315 * This function fails if it cannot read exactly @a size bytes from the file.
316 *
317 * @param fd File to read from.
318 * @param start Position in file where to start reading.
319 * @param size Number of bytes to read.
320 * @param ptr Place to store pointer to newly allocated block.
321 *
322 * @return EOK on success or EIO on failure.
323 */
324static int chunk_load(int fd, off_t start, off_t size, void **ptr)
325{
326 int rc;
327
328 rc = lseek(fd, start, SEEK_SET);
329 if (rc == (off_t) -1) {
330 printf("failed seeking chunk\n");
331 *ptr = NULL;
332 return EIO;
333 }
334
335 *ptr = malloc(size);
336 if (*ptr == NULL) {
337 printf("failed allocating memory\n");
338 return ENOMEM;
339 }
340
341 rc = read_all(fd, *ptr, size);
342 if (rc != EOK) {
343 printf("failed reading chunk\n");
344 free(*ptr);
345 *ptr = NULL;
346 return EIO;
347 }
348
349 return EOK;
350}
351
352/** Read until the buffer is read in its entirety.
353 *
354 * This function fails if it cannot read exactly @a len bytes from the file.
355 *
356 * @param fd The file to read from.
357 * @param buf Buffer for storing data, @a len bytes long.
358 * @param len Number of bytes to read.
359 *
360 * @return EOK on error, EIO if file is short or return value
361 * from read() if reading failed.
362 */
363static int read_all(int fd, void *buf, size_t len)
364{
365 int cnt = 0;
366
367 do {
368 buf += cnt;
369 len -= cnt;
370 cnt = read(fd, buf, len);
371 } while (cnt > 0 && (len - cnt) > 0);
372
373 if (cnt < 0)
374 return cnt;
375
376 if (len - cnt > 0)
377 return EIO;
378
379 return EOK;
380}
381
382/** @}
383 */
Note: See TracBrowser for help on using the repository browser.