[3698e44] | 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 |
|
---|
[5baf209] | 38 | #include <elf/elf.h>
|
---|
[3698e44] | 39 | #include <stdio.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 41 | #include <stddef.h>
|
---|
[3698e44] | 42 | #include <errno.h>
|
---|
[b19e892] | 43 | #include <vfs/vfs.h>
|
---|
[3698e44] | 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);
|
---|
[ed903174] | 50 | static int chunk_load(int fd, off64_t start, size_t size, void **ptr);
|
---|
[3698e44] | 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;
|
---|
[ed903174] | 65 | off64_t shstrt_start;
|
---|
| 66 | size_t shstrt_size;
|
---|
[3698e44] | 67 | char *shstrt, *sec_name;
|
---|
| 68 | void *data;
|
---|
[58898d1d] | 69 | aoff64_t pos = 0;
|
---|
[3698e44] | 70 |
|
---|
| 71 | int fd;
|
---|
| 72 | int rc;
|
---|
[8e3498b] | 73 | size_t nread;
|
---|
[3698e44] | 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 |
|
---|
[f77c1c9] | 84 | rc = vfs_lookup_open(file_name, WALK_REGULAR, MODE_READ, &fd);
|
---|
| 85 | if (rc != EOK) {
|
---|
| 86 | printf("failed opening file '%s': %s\n", file_name, str_error(rc));
|
---|
[3698e44] | 87 | free(stab);
|
---|
| 88 | return ENOENT;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[8e3498b] | 91 | rc = vfs_read(fd, &pos, &elf_hdr, sizeof(elf_header_t), &nread);
|
---|
| 92 | if (rc != EOK || nread != sizeof(elf_header_t)) {
|
---|
[3698e44] | 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);
|
---|
[9c4cf0d] | 168 | vfs_put(fd);
|
---|
[3698e44] | 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 | */
|
---|
| 186 | void 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 | */
|
---|
[c1b979a] | 205 | int symtab_name_to_addr(symtab_t *st, const char *name, uintptr_t *addr)
|
---|
[3698e44] | 206 | {
|
---|
| 207 | size_t i;
|
---|
| 208 | char *sname;
|
---|
[83349b03] | 209 | unsigned stype;
|
---|
[3698e44] | 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 |
|
---|
[83349b03] | 215 | stype = ELF_ST_TYPE(st->sym[i].st_info);
|
---|
| 216 | if (stype != STT_OBJECT && stype != STT_FUNC)
|
---|
| 217 | continue;
|
---|
| 218 |
|
---|
[3698e44] | 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 | */
|
---|
| 242 | int 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;
|
---|
[83349b03] | 248 | unsigned stype;
|
---|
[3698e44] | 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 |
|
---|
[83349b03] | 257 | stype = ELF_ST_TYPE(st->sym[i].st_info);
|
---|
[e067dcd] | 258 | if (stype != STT_OBJECT && stype != STT_FUNC &&
|
---|
| 259 | stype != STT_NOTYPE) {
|
---|
[83349b03] | 260 | continue;
|
---|
[e067dcd] | 261 | }
|
---|
[83349b03] | 262 |
|
---|
[3698e44] | 263 | saddr = st->sym[i].st_value;
|
---|
| 264 | sname = st->strtab + st->sym[i].st_name;
|
---|
| 265 |
|
---|
[e067dcd] | 266 | /* An ugly hack to filter out some special ARM symbols. */
|
---|
| 267 | if (sname[0] == '$')
|
---|
| 268 | continue;
|
---|
| 269 |
|
---|
[077bc931] | 270 | if (saddr <= addr && (best_name == NULL || saddr > best_addr)) {
|
---|
[3698e44] | 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 | */
|
---|
| 288 | static 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 | */
|
---|
| 303 | static 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;
|
---|
[8e3498b] | 307 | size_t nread;
|
---|
[58898d1d] | 308 | aoff64_t pos = elf_hdr->e_shoff + idx * sizeof(elf_section_header_t);
|
---|
[3698e44] | 309 |
|
---|
[8e3498b] | 310 | rc = vfs_read(fd, &pos, sec_hdr, sizeof(elf_section_header_t), &nread);
|
---|
| 311 | if (rc != EOK || nread != sizeof(elf_section_header_t))
|
---|
[3698e44] | 312 | return EIO;
|
---|
| 313 |
|
---|
| 314 | return EOK;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /** Load a segment of bytes from a file and return it as a new memory block.
|
---|
| 318 | *
|
---|
| 319 | * This function fails if it cannot read exactly @a size bytes from the file.
|
---|
| 320 | *
|
---|
| 321 | * @param fd File to read from.
|
---|
| 322 | * @param start Position in file where to start reading.
|
---|
| 323 | * @param size Number of bytes to read.
|
---|
| 324 | * @param ptr Place to store pointer to newly allocated block.
|
---|
| 325 | *
|
---|
| 326 | * @return EOK on success or EIO on failure.
|
---|
| 327 | */
|
---|
[ed903174] | 328 | static int chunk_load(int fd, off64_t start, size_t size, void **ptr)
|
---|
[3698e44] | 329 | {
|
---|
[8e3498b] | 330 | int rc;
|
---|
| 331 | size_t nread;
|
---|
[58898d1d] | 332 | aoff64_t pos = start;
|
---|
[3698e44] | 333 |
|
---|
| 334 | *ptr = malloc(size);
|
---|
| 335 | if (*ptr == NULL) {
|
---|
| 336 | printf("failed allocating memory\n");
|
---|
| 337 | return ENOMEM;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[8e3498b] | 340 | rc = vfs_read(fd, &pos, *ptr, size, &nread);
|
---|
| 341 | if (rc != EOK || nread != size) {
|
---|
[3698e44] | 342 | printf("failed reading chunk\n");
|
---|
| 343 | free(*ptr);
|
---|
| 344 | *ptr = NULL;
|
---|
| 345 | return EIO;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | return EOK;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /** @}
|
---|
| 352 | */
|
---|