blob: 241d63d6475d97dadebdad12b99f16a7d9af9514 [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
32
33extern HIDDEN int
34elfW (valid_object) (struct elf_image *ei)
35{
36 if (ei->size <= EI_CLASS)
37 return 0;
38
39 return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
40 && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS);
41}
42
43
44static int
45elfW (lookup_symbol) (unw_word_t ip, struct elf_image *ei,
46 ElfW (Addr) load_offset,
47 char *buf, size_t buf_len, unw_word_t *offp)
48{
49 size_t syment_size, str_size;
50 ElfW (Ehdr) *ehdr = ei->image;
51 ElfW (Sym) *sym, *symtab, *symtab_end;
52 ElfW (Off) soff, str_soff;
53 ElfW (Shdr) *shdr, *str_shdr;
54 ElfW (Addr) val, min_dist = ~(ElfW (Addr))0;
55 char *strtab;
56 int i;
57
58 if (!elfW (valid_object) (ei))
59 return -1;
60
61 soff = ehdr->e_shoff;
62 if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size)
63 {
64 debug (1, "%s: section table outside of image? (%lu > %lu)\n",
65 __FUNCTION__, soff + ehdr->e_shnum * ehdr->e_shentsize,
66 ei->size);
67 return -1;
68 }
69
70 shdr = (ElfW (Shdr) *) ((char *) ei->image + soff);
71
72 for (i = 0; i < ehdr->e_shnum; ++i)
73 {
74 switch (shdr->sh_type)
75 {
76 case SHT_SYMTAB:
77 case SHT_DYNSYM:
78 symtab = (ElfW (Sym) *) ((char *) ei->image + shdr->sh_offset);
79 symtab_end = (ElfW (Sym) *) ((char *) symtab + shdr->sh_size);
80 syment_size = shdr->sh_entsize;
81
82 str_soff = soff + (shdr->sh_link * ehdr->e_shentsize);
83 if (str_soff + ehdr->e_shentsize >= ei->size)
84 {
85 debug (1, "%s: string table outside of image? (%lu >= %lu)\n",
86 __FUNCTION__, str_soff + ehdr->e_shentsize, ei->size);
87 break;
88 }
89 str_shdr = (ElfW (Shdr) *) ((char *) ei->image + str_soff);
90 str_size = str_shdr->sh_size;
91 strtab = (char *) ei->image + str_shdr->sh_offset;
92
mostang.com!davidm03950aa2003-02-27 09:58:57 +000093 debug (160, "symtab=0x%lx[%d], strtab=0x%lx\n", shdr->sh_offset,
mostang.com!davidm824d6612003-02-08 10:10:59 +000094 shdr->sh_type, str_shdr->sh_offset);
95
96 for (sym = symtab;
97 sym < symtab_end;
98 sym = (ElfW (Sym) *) ((char *) sym + syment_size))
99 {
100 if (ELFW (ST_TYPE) (sym->st_info) == STT_FUNC
101 && sym->st_shndx != SHN_UNDEF)
102 {
103 val = sym->st_value;
104 if (sym->st_shndx != SHN_ABS)
105 val += load_offset;
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000106 debug (160, "0x%016lx info=0x%02x %s\n",
mostang.com!davidm824d6612003-02-08 10:10:59 +0000107 val, sym->st_info, strtab + sym->st_name);
108
109 if ((ElfW (Addr)) (ip - val) < min_dist)
110 {
111 min_dist = (ElfW (Addr)) (ip - val);
112 buf[buf_len - 1] = 'x';
113 strncpy (buf, strtab + sym->st_name, buf_len);
114 buf[buf_len - 1] = '\0';
115 }
116 }
117 }
118 break;
119
120 default:
121 break;
122 }
123 shdr = (Elf64_Shdr *) (((char *) shdr) + ehdr->e_shentsize);
124 }
125 if (min_dist >= ei->size)
126 return -1; /* not found */
127 if (offp)
128 *offp = min_dist;
129 return 0;
130}
131
132/* Find the ELF image that contains IP and return the "closest"
133 procedure name, if there is one. With some caching, this could be
134 sped up greatly, but until an application materializes that's
135 sensitive to the performance of this routine, why bother... */
136
137HIDDEN int
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000138elfW (get_proc_name) (pid_t pid, unw_word_t ip, char *buf, size_t buf_len,
mostang.com!davidm824d6612003-02-08 10:10:59 +0000139 unw_word_t *offp)
140{
141 unsigned long segbase, mapoff;
142 ElfW (Addr) load_offset = 0;
143 struct elf_image ei;
144 ElfW (Ehdr) *ehdr;
145 ElfW (Phdr) *phdr;
146 int i, ret;
147
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000148 ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000149 if (ret < 0)
150 return ret;
151
152 ehdr = ei.image;
153 phdr = (Elf64_Phdr *) ((char *) ei.image + ehdr->e_phoff);
154
155 for (i = 0; i < ehdr->e_phnum; ++i)
156 if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff)
157 {
158 load_offset = segbase - phdr[i].p_vaddr;
159 break;
160 }
161
162 ret = elfW (lookup_symbol) (ip, &ei, load_offset, buf, buf_len, offp);
163
164 munmap (ei.image, ei.size);
165 ei.image = NULL;
166
167 return ret;
168}