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