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