[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 |
|
---|
| 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 |
|
---|
| 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, off_t start, off_t size, void **ptr);
|
---|
| 52 | static 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 | */
|
---|
| 62 | int 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 | */
|
---|
| 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, char *name, uintptr_t *addr)
|
---|
| 205 | {
|
---|
| 206 | size_t i;
|
---|
| 207 | char *sname;
|
---|
| 208 |
|
---|
| 209 | for (i = 0; i < st->sym_size / sizeof(elf_symbol_t); ++i) {
|
---|
| 210 | if (st->sym[i].st_name == 0)
|
---|
| 211 | continue;
|
---|
| 212 |
|
---|
| 213 | sname = st->strtab + st->sym[i].st_name;
|
---|
| 214 |
|
---|
| 215 | if (str_cmp(sname, name) == 0) {
|
---|
| 216 | *addr = st->sym[i].st_value;
|
---|
| 217 | return EOK;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | return ENOENT;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /** Convert symbol address to name.
|
---|
| 225 | *
|
---|
| 226 | * This function finds the symbol which starts at the highest address
|
---|
| 227 | * less than or equal to @a addr.
|
---|
| 228 | *
|
---|
| 229 | * @param st Symbol table.
|
---|
| 230 | * @param addr Address for lookup.
|
---|
| 231 | * @param name Place to store pointer name of symbol, if found.
|
---|
| 232 | * This is valid while @a st exists.
|
---|
| 233 | *
|
---|
| 234 | * @return EOK on success or ENOENT if no matching symbol was found.
|
---|
| 235 | */
|
---|
| 236 | int symtab_addr_to_name(symtab_t *st, uintptr_t addr, char **name,
|
---|
| 237 | size_t *offs)
|
---|
| 238 | {
|
---|
| 239 | size_t i;
|
---|
| 240 | uintptr_t saddr, best_addr;
|
---|
| 241 | char *sname, *best_name;
|
---|
| 242 |
|
---|
| 243 | best_name = NULL;
|
---|
| 244 | best_addr = 0;
|
---|
| 245 |
|
---|
| 246 | for (i = 0; i < st->sym_size / sizeof(elf_symbol_t); ++i) {
|
---|
| 247 | if (st->sym[i].st_name == 0)
|
---|
| 248 | continue;
|
---|
| 249 |
|
---|
| 250 | saddr = st->sym[i].st_value;
|
---|
| 251 | sname = st->strtab + st->sym[i].st_name;
|
---|
| 252 |
|
---|
| 253 | if (best_name == NULL || (saddr <= addr && saddr > best_addr)) {
|
---|
| 254 | best_name = sname;
|
---|
| 255 | best_addr = saddr;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | if (best_name == NULL)
|
---|
| 260 | return ENOENT;
|
---|
| 261 |
|
---|
| 262 | *name = best_name;
|
---|
| 263 | *offs = addr - best_addr;
|
---|
| 264 | return EOK;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /** Check if ELF header is valid.
|
---|
| 268 | *
|
---|
| 269 | * @return EOK on success or negative error code.
|
---|
| 270 | */
|
---|
| 271 | static int elf_hdr_check(elf_header_t *ehdr)
|
---|
| 272 | {
|
---|
| 273 | /* TODO */
|
---|
| 274 | return EOK;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /** Load ELF section header.
|
---|
| 278 | *
|
---|
| 279 | * @param fd File descriptor of ELF file.
|
---|
| 280 | * @param elf_hdr Pointer to ELF file header in memory.
|
---|
| 281 | * @param idx Index of section whose header to load (0 = first).
|
---|
| 282 | * @param sec_hdr Place to store section header data.
|
---|
| 283 | *
|
---|
| 284 | * @return EOK on success or EIO if I/O failed.
|
---|
| 285 | */
|
---|
| 286 | static int section_hdr_load(int fd, const elf_header_t *elf_hdr, int idx,
|
---|
| 287 | elf_section_header_t *sec_hdr)
|
---|
| 288 | {
|
---|
| 289 | int rc;
|
---|
| 290 |
|
---|
| 291 | rc = lseek(fd, elf_hdr->e_shoff + idx * sizeof(elf_section_header_t),
|
---|
| 292 | SEEK_SET);
|
---|
| 293 | if (rc == (off_t) -1)
|
---|
| 294 | return EIO;
|
---|
| 295 |
|
---|
| 296 | rc = read_all(fd, sec_hdr, sizeof(elf_section_header_t));
|
---|
| 297 | if (rc != EOK)
|
---|
| 298 | return EIO;
|
---|
| 299 |
|
---|
| 300 | return EOK;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | /** Load a segment of bytes from a file and return it as a new memory block.
|
---|
| 304 | *
|
---|
| 305 | * This function fails if it cannot read exactly @a size bytes from the file.
|
---|
| 306 | *
|
---|
| 307 | * @param fd File to read from.
|
---|
| 308 | * @param start Position in file where to start reading.
|
---|
| 309 | * @param size Number of bytes to read.
|
---|
| 310 | * @param ptr Place to store pointer to newly allocated block.
|
---|
| 311 | *
|
---|
| 312 | * @return EOK on success or EIO on failure.
|
---|
| 313 | */
|
---|
| 314 | static int chunk_load(int fd, off_t start, off_t size, void **ptr)
|
---|
| 315 | {
|
---|
| 316 | int rc;
|
---|
| 317 |
|
---|
| 318 | rc = lseek(fd, start, SEEK_SET);
|
---|
| 319 | if (rc == (off_t) -1) {
|
---|
| 320 | printf("failed seeking chunk\n");
|
---|
| 321 | *ptr = NULL;
|
---|
| 322 | return EIO;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | *ptr = malloc(size);
|
---|
| 326 | if (*ptr == NULL) {
|
---|
| 327 | printf("failed allocating memory\n");
|
---|
| 328 | return ENOMEM;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | rc = read_all(fd, *ptr, size);
|
---|
| 332 | if (rc != EOK) {
|
---|
| 333 | printf("failed reading chunk\n");
|
---|
| 334 | free(*ptr);
|
---|
| 335 | *ptr = NULL;
|
---|
| 336 | return EIO;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | return EOK;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /** Read until the buffer is read in its entirety.
|
---|
| 343 | *
|
---|
| 344 | * This function fails if it cannot read exactly @a len bytes from the file.
|
---|
| 345 | *
|
---|
| 346 | * @param fd The file to read from.
|
---|
| 347 | * @param buf Buffer for storing data, @a len bytes long.
|
---|
| 348 | * @param len Number of bytes to read.
|
---|
| 349 | *
|
---|
| 350 | * @return EOK on error, EIO if file is short or return value
|
---|
| 351 | * from read() if reading failed.
|
---|
| 352 | */
|
---|
| 353 | static int read_all(int fd, void *buf, size_t len)
|
---|
| 354 | {
|
---|
| 355 | int cnt = 0;
|
---|
| 356 |
|
---|
| 357 | do {
|
---|
| 358 | buf += cnt;
|
---|
| 359 | len -= cnt;
|
---|
| 360 | cnt = read(fd, buf, len);
|
---|
| 361 | } while (cnt > 0 && (len - cnt) > 0);
|
---|
| 362 |
|
---|
| 363 | if (cnt < 0)
|
---|
| 364 | return cnt;
|
---|
| 365 |
|
---|
| 366 | if (len - cnt > 0)
|
---|
| 367 | return EIO;
|
---|
| 368 |
|
---|
| 369 | return EOK;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /** @}
|
---|
| 373 | */
|
---|