source: mainline/uspace/app/taskdump/symtab.c@ 79ae36dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ae36dd was ed903174, checked in by Martin Decky <martin@…>, 15 years ago

implement support for 64bit file offsets

  • the libc API is a small deviation from standard, but we have no reason to keep a strict backward compatibility with ancient code so far
    • the basic signed 64bit offset type is called off64_t
      • lseek() and fseek() take off64_t arguments (since the argument represents a relative offset)
      • ftell() returns off64_t values (since it is a wrapper of lseek())
      • vfs_seek() implementation supports negative offsets when SEEK_CUR and SEEK_END is used
    • aoff64_t is used for internal unsigned representation of sizes (in bytes, blocks, etc.) and absolute offsets
      • mmap() and ftruncate() take aoff64_t arguments (since the full range of the absolute file offset should be used here)
      • struct stat stores the file size as aoff64_t
    • in both cases the explicit range of the types shown in the names is helpful for proper filesystem and IPC interaction
    • note: size_t should be used only for representing in-memory sizes and offsets, not device and file-related information, and vice versa
      • the code base still needs a thorough revision with respect to this rule
    • PRIdOFF64 and PRIuOFF64 can be used for printing the offsets
  • VFS_OUT_LOOKUP returns the 64bit file size in two IPC arguments
    • since all 5 IPC arguments have already been taken, the fs_handle is returned as the return value (fs_handle has only 16 bits, thus the return value can be used for both indicating errors as negative values and returning positive handles)
  • VFS_OUT_READ and VFS_OUT_WRITE use aoff64_t absolute offsets split into two IPC arguments

replace bn_t with aoff64_t as a generic 64bit bytes/block counter type

note: filesystem drivers need to be revised with respect to make sure that all out-of-range checks are correct (especially w.r.t. file and block offsets)

  • Property mode set to 100644
File size: 9.0 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, off64_t start, size_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 off64_t shstrt_start;
68 size_t shstrt_size;
69 char *shstrt, *sec_name;
70 void *data;
71
72 int fd;
73 int rc;
74 int i;
75
76 bool load_sec, sec_is_symtab;
77
78 *symtab = NULL;
79
80 stab = calloc(1, sizeof(symtab_t));
81 if (stab == NULL)
82 return ENOMEM;
83
84 fd = open(file_name, O_RDONLY);
85 if (fd < 0) {
86 printf("failed opening file\n");
87 free(stab);
88 return ENOENT;
89 }
90
91 rc = read_all(fd, &elf_hdr, sizeof(elf_header_t));
92 if (rc != EOK) {
93 printf("failed reading elf header\n");
94 free(stab);
95 return EIO;
96 }
97
98 rc = elf_hdr_check(&elf_hdr);
99 if (rc != EOK) {
100 printf("failed header check\n");
101 free(stab);
102 return ENOTSUP;
103 }
104
105 /*
106 * Load section header string table.
107 */
108
109 rc = section_hdr_load(fd, &elf_hdr, elf_hdr.e_shstrndx, &sec_hdr);
110 if (rc != EOK) {
111 printf("failed reading shstrt header\n");
112 free(stab);
113 return ENOTSUP;
114 }
115
116 shstrt_start = sec_hdr.sh_offset;
117 shstrt_size = sec_hdr.sh_size;
118
119 rc = chunk_load(fd, shstrt_start, shstrt_size, (void **) &shstrt);
120 if (rc != EOK) {
121 printf("failed loading shstrt\n");
122 free(stab);
123 return ENOTSUP;
124 }
125
126 /* Read all section headers. */
127 for (i = 0; i < elf_hdr.e_shnum; ++i) {
128 rc = section_hdr_load(fd, &elf_hdr, i, &sec_hdr);
129 if (rc != EOK) {
130 free(shstrt);
131 free(stab);
132 return ENOTSUP;
133 }
134
135 sec_name = shstrt + sec_hdr.sh_name;
136 if (str_cmp(sec_name, ".symtab") == 0 &&
137 sec_hdr.sh_type == SHT_SYMTAB) {
138 load_sec = true;
139 sec_is_symtab = true;
140 } else if (str_cmp(sec_name, ".strtab") == 0 &&
141 sec_hdr.sh_type == SHT_STRTAB) {
142 load_sec = true;
143 sec_is_symtab = false;
144 } else {
145 load_sec = false;
146 }
147
148 if (load_sec) {
149 rc = chunk_load(fd, sec_hdr.sh_offset, sec_hdr.sh_size,
150 &data);
151 if (rc != EOK) {
152 free(shstrt);
153 free(stab);
154 return ENOTSUP;
155 }
156
157 if (sec_is_symtab) {
158 stab->sym = data;
159 stab->sym_size = sec_hdr.sh_size;
160 } else {
161 stab->strtab = data;
162 stab->strtab_size = sec_hdr.sh_size;
163 }
164 }
165 }
166
167 free(shstrt);
168 close(fd);
169
170 if (stab->sym == NULL || stab->strtab == NULL) {
171 /* Tables not found. */
172 printf("Symbol table or string table section not found\n");
173 free(stab);
174 return ENOTSUP;
175 }
176
177 *symtab = stab;
178
179 return EOK;
180}
181
182/** Delete a symtab structure.
183 *
184 * Deallocates all resources used by the symbol table.
185 */
186void symtab_delete(symtab_t *st)
187{
188 free(st->sym);
189 st->sym = NULL;
190
191 free(st->strtab);
192 st->strtab = NULL;
193
194 free(st);
195}
196
197/** Convert symbol name to address.
198 *
199 * @param st Symbol table.
200 * @param name Name of the symbol.
201 * @param addr Place to store address for symbol, if found.
202 *
203 * @return EOK on success, ENOENT if no such symbol was found.
204 */
205int symtab_name_to_addr(symtab_t *st, char *name, uintptr_t *addr)
206{
207 size_t i;
208 char *sname;
209 unsigned stype;
210
211 for (i = 0; i < st->sym_size / sizeof(elf_symbol_t); ++i) {
212 if (st->sym[i].st_name == 0)
213 continue;
214
215 stype = ELF_ST_TYPE(st->sym[i].st_info);
216 if (stype != STT_OBJECT && stype != STT_FUNC)
217 continue;
218
219 sname = st->strtab + st->sym[i].st_name;
220
221 if (str_cmp(sname, name) == 0) {
222 *addr = st->sym[i].st_value;
223 return EOK;
224 }
225 }
226
227 return ENOENT;
228}
229
230/** Convert symbol address to name.
231 *
232 * This function finds the symbol which starts at the highest address
233 * less than or equal to @a addr.
234 *
235 * @param st Symbol table.
236 * @param addr Address for lookup.
237 * @param name Place to store pointer name of symbol, if found.
238 * This is valid while @a st exists.
239 *
240 * @return EOK on success or ENOENT if no matching symbol was found.
241 */
242int symtab_addr_to_name(symtab_t *st, uintptr_t addr, char **name,
243 size_t *offs)
244{
245 size_t i;
246 uintptr_t saddr, best_addr;
247 char *sname, *best_name;
248 unsigned stype;
249
250 best_name = NULL;
251 best_addr = 0;
252
253 for (i = 0; i < st->sym_size / sizeof(elf_symbol_t); ++i) {
254 if (st->sym[i].st_name == 0)
255 continue;
256
257 stype = ELF_ST_TYPE(st->sym[i].st_info);
258 if (stype != STT_OBJECT && stype != STT_FUNC &&
259 stype != STT_NOTYPE) {
260 continue;
261 }
262
263 saddr = st->sym[i].st_value;
264 sname = st->strtab + st->sym[i].st_name;
265
266 /* An ugly hack to filter out some special ARM symbols. */
267 if (sname[0] == '$')
268 continue;
269
270 if (best_name == NULL || (saddr <= addr && saddr > best_addr)) {
271 best_name = sname;
272 best_addr = saddr;
273 }
274 }
275
276 if (best_name == NULL)
277 return ENOENT;
278
279 *name = best_name;
280 *offs = addr - best_addr;
281 return EOK;
282}
283
284/** Check if ELF header is valid.
285 *
286 * @return EOK on success or negative error code.
287 */
288static int elf_hdr_check(elf_header_t *ehdr)
289{
290 /* TODO */
291 return EOK;
292}
293
294/** Load ELF section header.
295 *
296 * @param fd File descriptor of ELF file.
297 * @param elf_hdr Pointer to ELF file header in memory.
298 * @param idx Index of section whose header to load (0 = first).
299 * @param sec_hdr Place to store section header data.
300 *
301 * @return EOK on success or EIO if I/O failed.
302 */
303static int section_hdr_load(int fd, const elf_header_t *elf_hdr, int idx,
304 elf_section_header_t *sec_hdr)
305{
306 int rc;
307
308 rc = lseek(fd, elf_hdr->e_shoff + idx * sizeof(elf_section_header_t),
309 SEEK_SET);
310 if (rc == (off64_t) -1)
311 return EIO;
312
313 rc = read_all(fd, sec_hdr, sizeof(elf_section_header_t));
314 if (rc != EOK)
315 return EIO;
316
317 return EOK;
318}
319
320/** Load a segment of bytes from a file and return it as a new memory block.
321 *
322 * This function fails if it cannot read exactly @a size bytes from the file.
323 *
324 * @param fd File to read from.
325 * @param start Position in file where to start reading.
326 * @param size Number of bytes to read.
327 * @param ptr Place to store pointer to newly allocated block.
328 *
329 * @return EOK on success or EIO on failure.
330 */
331static int chunk_load(int fd, off64_t start, size_t size, void **ptr)
332{
333 int rc;
334
335 rc = lseek(fd, start, SEEK_SET);
336 if (rc == (off64_t) -1) {
337 printf("failed seeking chunk\n");
338 *ptr = NULL;
339 return EIO;
340 }
341
342 *ptr = malloc(size);
343 if (*ptr == NULL) {
344 printf("failed allocating memory\n");
345 return ENOMEM;
346 }
347
348 rc = read_all(fd, *ptr, size);
349 if (rc != EOK) {
350 printf("failed reading chunk\n");
351 free(*ptr);
352 *ptr = NULL;
353 return EIO;
354 }
355
356 return EOK;
357}
358
359/** Read until the buffer is read in its entirety.
360 *
361 * This function fails if it cannot read exactly @a len bytes from the file.
362 *
363 * @param fd The file to read from.
364 * @param buf Buffer for storing data, @a len bytes long.
365 * @param len Number of bytes to read.
366 *
367 * @return EOK on error, EIO if file is short or return value
368 * from read() if reading failed.
369 */
370static int read_all(int fd, void *buf, size_t len)
371{
372 int cnt = 0;
373
374 do {
375 buf += cnt;
376 len -= cnt;
377 cnt = read(fd, buf, len);
378 } while (cnt > 0 && (len - cnt) > 0);
379
380 if (cnt < 0)
381 return cnt;
382
383 if (len - cnt > 0)
384 return EIO;
385
386 return EOK;
387}
388
389/** @}
390 */
Note: See TracBrowser for help on using the repository browser.