mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
hp.com!davidm | 379fb06 | 2005-05-20 09:48:08 +0000 | [diff] [blame^] | 2 | Copyright (C) 2003-2005 Hewlett-Packard Co |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
| 5 | This file is part of libunwind. |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining |
| 8 | a copy of this software and associated documentation files (the |
| 9 | "Software"), to deal in the Software without restriction, including |
| 10 | without limitation the rights to use, copy, modify, merge, publish, |
| 11 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | permit persons to whom the Software is furnished to do so, subject to |
| 13 | the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be |
| 16 | included in all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 25 | |
| 26 | #include <stdio.h> |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 27 | |
hp.com!davidm | 379fb06 | 2005-05-20 09:48:08 +0000 | [diff] [blame^] | 28 | #include "libunwind_i.h" |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 29 | |
hp.com!davidm | 5d8b2e3 | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 30 | HIDDEN int |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 31 | elf_w (valid_object) (struct elf_image *ei) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 32 | { |
| 33 | if (ei->size <= EI_CLASS) |
| 34 | return 0; |
| 35 | |
| 36 | return (memcmp (ei->image, ELFMAG, SELFMAG) == 0 |
| 37 | && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | static int |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 42 | elf_w (lookup_symbol) (unw_word_t ip, struct elf_image *ei, |
| 43 | Elf_W (Addr) load_offset, |
| 44 | char *buf, size_t buf_len, unw_word_t *offp) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 45 | { |
hp.com!davidm | dee53d7 | 2003-11-27 06:52:54 +0000 | [diff] [blame] | 46 | size_t syment_size; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 47 | Elf_W (Ehdr) *ehdr = ei->image; |
| 48 | Elf_W (Sym) *sym, *symtab, *symtab_end; |
| 49 | Elf_W (Off) soff, str_soff; |
| 50 | Elf_W (Shdr) *shdr, *str_shdr; |
| 51 | Elf_W (Addr) val, min_dist = ~(Elf_W (Addr))0; |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 52 | int i, ret = 0; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 53 | char *strtab; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 54 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 55 | if (!elf_w (valid_object) (ei)) |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 56 | return -UNW_ENOINFO; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 57 | |
| 58 | soff = ehdr->e_shoff; |
| 59 | if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size) |
| 60 | { |
hp.com!davidm | c67d345 | 2004-01-30 00:01:24 +0000 | [diff] [blame] | 61 | Debug (1, "section table outside of image? (%lu > %lu)\n", |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 62 | (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize), |
| 63 | (unsigned long) ei->size); |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 64 | return -UNW_ENOINFO; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 65 | } |
| 66 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 67 | shdr = (Elf_W (Shdr) *) ((char *) ei->image + soff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 68 | |
| 69 | for (i = 0; i < ehdr->e_shnum; ++i) |
| 70 | { |
| 71 | switch (shdr->sh_type) |
| 72 | { |
| 73 | case SHT_SYMTAB: |
| 74 | case SHT_DYNSYM: |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 75 | symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset); |
| 76 | symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 77 | syment_size = shdr->sh_entsize; |
| 78 | |
| 79 | str_soff = soff + (shdr->sh_link * ehdr->e_shentsize); |
| 80 | if (str_soff + ehdr->e_shentsize >= ei->size) |
| 81 | { |
hp.com!davidm | c67d345 | 2004-01-30 00:01:24 +0000 | [diff] [blame] | 82 | Debug (1, "string table outside of image? (%lu >= %lu)\n", |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 83 | (unsigned long) (str_soff + ehdr->e_shentsize), |
| 84 | (unsigned long) ei->size); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 85 | break; |
| 86 | } |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 87 | str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 88 | strtab = (char *) ei->image + str_shdr->sh_offset; |
| 89 | |
hp.com!davidm | c67d345 | 2004-01-30 00:01:24 +0000 | [diff] [blame] | 90 | Debug (16, "symtab=0x%lx[%d], strtab=0x%lx\n", |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 91 | (long) shdr->sh_offset, shdr->sh_type, |
| 92 | (long) str_shdr->sh_offset); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 93 | |
| 94 | for (sym = symtab; |
| 95 | sym < symtab_end; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 96 | sym = (Elf_W (Sym) *) ((char *) sym + syment_size)) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 97 | { |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 98 | if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 99 | && sym->st_shndx != SHN_UNDEF) |
| 100 | { |
| 101 | val = sym->st_value; |
| 102 | if (sym->st_shndx != SHN_ABS) |
| 103 | val += load_offset; |
hp.com!davidm | c67d345 | 2004-01-30 00:01:24 +0000 | [diff] [blame] | 104 | Debug (16, "0x%016lx info=0x%02x %s\n", |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 105 | (long) val, sym->st_info, strtab + sym->st_name); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 106 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 107 | if ((Elf_W (Addr)) (ip - val) < min_dist) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 108 | { |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 109 | min_dist = (Elf_W (Addr)) (ip - val); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 110 | strncpy (buf, strtab + sym->st_name, buf_len); |
| 111 | buf[buf_len - 1] = '\0'; |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 112 | if (strlen (strtab + sym->st_name) >= buf_len) |
| 113 | ret = -UNW_ENOMEM; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | break; |
| 118 | |
| 119 | default: |
| 120 | break; |
| 121 | } |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 122 | shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 123 | } |
| 124 | if (min_dist >= ei->size) |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 125 | return -UNW_ENOINFO; /* not found */ |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 126 | if (offp) |
| 127 | *offp = min_dist; |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 128 | return ret; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Find the ELF image that contains IP and return the "closest" |
| 132 | procedure name, if there is one. With some caching, this could be |
| 133 | sped up greatly, but until an application materializes that's |
| 134 | sensitive to the performance of this routine, why bother... */ |
| 135 | |
hp.com!davidm | 5d8b2e3 | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 136 | HIDDEN int |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 137 | elf_w (get_proc_name) (pid_t pid, unw_word_t ip, char *buf, size_t buf_len, |
| 138 | unw_word_t *offp) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 139 | { |
| 140 | unsigned long segbase, mapoff; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 141 | Elf_W (Addr) load_offset = 0; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 142 | struct elf_image ei; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 143 | Elf_W (Ehdr) *ehdr; |
| 144 | Elf_W (Phdr) *phdr; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 145 | int i, ret; |
| 146 | |
mostang.com!davidm | 03950aa | 2003-02-27 09:58:57 +0000 | [diff] [blame] | 147 | ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 148 | if (ret < 0) |
| 149 | return ret; |
| 150 | |
| 151 | ehdr = ei.image; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 152 | phdr = (Elf_W (Phdr) *) ((char *) ei.image + ehdr->e_phoff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 153 | |
| 154 | for (i = 0; i < ehdr->e_phnum; ++i) |
| 155 | if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff) |
| 156 | { |
| 157 | load_offset = segbase - phdr[i].p_vaddr; |
| 158 | break; |
| 159 | } |
| 160 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 161 | ret = elf_w (lookup_symbol) (ip, &ei, load_offset, buf, buf_len, offp); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 162 | |
| 163 | munmap (ei.image, ei.size); |
| 164 | ei.image = NULL; |
| 165 | |
| 166 | return ret; |
| 167 | } |