blob: 965275b6052eccc029c7c0a52e9eda6056ed86d5 [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;
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +000056 int i, ret = 0;
mostang.com!davidm824d6612003-02-08 10:10:59 +000057 char *strtab;
mostang.com!davidm824d6612003-02-08 10:10:59 +000058
59 if (!elfW (valid_object) (ei))
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +000060 return -UNW_ENOINFO;
mostang.com!davidm824d6612003-02-08 10:10:59 +000061
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);
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +000068 return -UNW_ENOINFO;
mostang.com!davidm824d6612003-02-08 10:10:59 +000069 }
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);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000113 strncpy (buf, strtab + sym->st_name, buf_len);
114 buf[buf_len - 1] = '\0';
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000115 if (strlen (strtab + sym->st_name) >= buf_len)
116 ret = -UNW_ENOMEM;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000117 }
118 }
119 }
120 break;
121
122 default:
123 break;
124 }
125 shdr = (Elf64_Shdr *) (((char *) shdr) + ehdr->e_shentsize);
126 }
127 if (min_dist >= ei->size)
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000128 return -UNW_ENOINFO; /* not found */
mostang.com!davidm824d6612003-02-08 10:10:59 +0000129 if (offp)
130 *offp = min_dist;
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000131 return ret;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000132}
133
134/* Find the ELF image that contains IP and return the "closest"
135 procedure name, if there is one. With some caching, this could be
136 sped up greatly, but until an application materializes that's
137 sensitive to the performance of this routine, why bother... */
138
139HIDDEN int
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000140elfW (get_proc_name) (pid_t pid, unw_word_t ip, char *buf, size_t buf_len,
mostang.com!davidm824d6612003-02-08 10:10:59 +0000141 unw_word_t *offp)
142{
143 unsigned long segbase, mapoff;
144 ElfW (Addr) load_offset = 0;
145 struct elf_image ei;
146 ElfW (Ehdr) *ehdr;
147 ElfW (Phdr) *phdr;
148 int i, ret;
149
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000150 ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000151 if (ret < 0)
152 return ret;
153
154 ehdr = ei.image;
155 phdr = (Elf64_Phdr *) ((char *) ei.image + ehdr->e_phoff);
156
157 for (i = 0; i < ehdr->e_phnum; ++i)
158 if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff)
159 {
160 load_offset = segbase - phdr[i].p_vaddr;
161 break;
162 }
163
164 ret = elfW (lookup_symbol) (ip, &ei, load_offset, buf, buf_len, offp);
165
166 munmap (ei.image, ei.size);
167 ei.image = NULL;
168
169 return ret;
170}