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