mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2003 Hewlett-Packard Co |
| 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> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | |
mostang.com!davidm | d098282 | 2003-03-06 06:14:36 +0000 | [diff] [blame] | 32 | #include "tdep.h" |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 33 | |
| 34 | extern HIDDEN int |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 35 | elf_w (valid_object) (struct elf_image *ei) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 36 | { |
| 37 | if (ei->size <= EI_CLASS) |
| 38 | return 0; |
| 39 | |
| 40 | return (memcmp (ei->image, ELFMAG, SELFMAG) == 0 |
| 41 | && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | static int |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 46 | elf_w (lookup_symbol) (unw_word_t ip, struct elf_image *ei, |
| 47 | Elf_W (Addr) load_offset, |
| 48 | char *buf, size_t buf_len, unw_word_t *offp) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 49 | { |
hp.com!davidm | dee53d7 | 2003-11-27 06:52:54 +0000 | [diff] [blame] | 50 | size_t syment_size; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 51 | Elf_W (Ehdr) *ehdr = ei->image; |
| 52 | Elf_W (Sym) *sym, *symtab, *symtab_end; |
| 53 | Elf_W (Off) soff, str_soff; |
| 54 | Elf_W (Shdr) *shdr, *str_shdr; |
| 55 | Elf_W (Addr) val, min_dist = ~(Elf_W (Addr))0; |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 56 | int i, ret = 0; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 57 | char *strtab; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 58 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 59 | if (!elf_w (valid_object) (ei)) |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 60 | return -UNW_ENOINFO; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 61 | |
| 62 | soff = ehdr->e_shoff; |
| 63 | if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size) |
| 64 | { |
| 65 | debug (1, "%s: section table outside of image? (%lu > %lu)\n", |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 66 | __FUNCTION__, |
| 67 | (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize), |
| 68 | (unsigned long) ei->size); |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 69 | return -UNW_ENOINFO; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 72 | shdr = (Elf_W (Shdr) *) ((char *) ei->image + soff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 73 | |
| 74 | for (i = 0; i < ehdr->e_shnum; ++i) |
| 75 | { |
| 76 | switch (shdr->sh_type) |
| 77 | { |
| 78 | case SHT_SYMTAB: |
| 79 | case SHT_DYNSYM: |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 80 | symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset); |
| 81 | symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 82 | syment_size = shdr->sh_entsize; |
| 83 | |
| 84 | str_soff = soff + (shdr->sh_link * ehdr->e_shentsize); |
| 85 | if (str_soff + ehdr->e_shentsize >= ei->size) |
| 86 | { |
| 87 | debug (1, "%s: string table outside of image? (%lu >= %lu)\n", |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 88 | __FUNCTION__, |
| 89 | (unsigned long) (str_soff + ehdr->e_shentsize), |
| 90 | (unsigned long) ei->size); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 91 | break; |
| 92 | } |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 93 | str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 94 | strtab = (char *) ei->image + str_shdr->sh_offset; |
| 95 | |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 96 | debug (160, "symtab=0x%lx[%d], strtab=0x%lx\n", |
| 97 | (long) shdr->sh_offset, shdr->sh_type, |
| 98 | (long) str_shdr->sh_offset); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 99 | |
| 100 | for (sym = symtab; |
| 101 | sym < symtab_end; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 102 | sym = (Elf_W (Sym) *) ((char *) sym + syment_size)) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 103 | { |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 104 | if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 105 | && sym->st_shndx != SHN_UNDEF) |
| 106 | { |
| 107 | val = sym->st_value; |
| 108 | if (sym->st_shndx != SHN_ABS) |
| 109 | val += load_offset; |
mostang.com!davidm | 03950aa | 2003-02-27 09:58:57 +0000 | [diff] [blame] | 110 | debug (160, "0x%016lx info=0x%02x %s\n", |
hp.com!davidm | 7165025 | 2003-09-25 05:29:14 +0000 | [diff] [blame] | 111 | (long) val, sym->st_info, strtab + sym->st_name); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 112 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 113 | if ((Elf_W (Addr)) (ip - val) < min_dist) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 114 | { |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 115 | min_dist = (Elf_W (Addr)) (ip - val); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 116 | strncpy (buf, strtab + sym->st_name, buf_len); |
| 117 | buf[buf_len - 1] = '\0'; |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 118 | if (strlen (strtab + sym->st_name) >= buf_len) |
| 119 | ret = -UNW_ENOMEM; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | break; |
| 124 | |
| 125 | default: |
| 126 | break; |
| 127 | } |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 128 | shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 129 | } |
| 130 | if (min_dist >= ei->size) |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 131 | return -UNW_ENOINFO; /* not found */ |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 132 | if (offp) |
| 133 | *offp = min_dist; |
mostang.com!davidm | 7bfbbb6 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 134 | return ret; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Find the ELF image that contains IP and return the "closest" |
| 138 | procedure name, if there is one. With some caching, this could be |
| 139 | sped up greatly, but until an application materializes that's |
| 140 | sensitive to the performance of this routine, why bother... */ |
| 141 | |
| 142 | HIDDEN int |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 143 | elf_w (get_proc_name) (pid_t pid, unw_word_t ip, char *buf, size_t buf_len, |
| 144 | unw_word_t *offp) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 145 | { |
| 146 | unsigned long segbase, mapoff; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 147 | Elf_W (Addr) load_offset = 0; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 148 | struct elf_image ei; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 149 | Elf_W (Ehdr) *ehdr; |
| 150 | Elf_W (Phdr) *phdr; |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 151 | int i, ret; |
| 152 | |
mostang.com!davidm | 03950aa | 2003-02-27 09:58:57 +0000 | [diff] [blame] | 153 | ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | |
| 157 | ehdr = ei.image; |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 158 | phdr = (Elf_W (Phdr) *) ((char *) ei.image + ehdr->e_phoff); |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 159 | |
| 160 | for (i = 0; i < ehdr->e_phnum; ++i) |
| 161 | if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff) |
| 162 | { |
| 163 | load_offset = segbase - phdr[i].p_vaddr; |
| 164 | break; |
| 165 | } |
| 166 | |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame^] | 167 | 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] | 168 | |
| 169 | munmap (ei.image, ei.size); |
| 170 | ei.image = NULL; |
| 171 | |
| 172 | return ret; |
| 173 | } |