blob: 1f8b0d43a7e7dad8e6e693e7cfdada5b0c6ee99b [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
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000035elf_w (valid_object) (struct elf_image *ei)
mostang.com!davidm824d6612003-02-08 10:10:59 +000036{
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
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000046elf_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!davidm824d6612003-02-08 10:10:59 +000049{
hp.com!davidmdee53d72003-11-27 06:52:54 +000050 size_t syment_size;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000051 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!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
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000059 if (!elf_w (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",
hp.com!davidm71650252003-09-25 05:29:14 +000066 __FUNCTION__,
67 (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize),
68 (unsigned long) ei->size);
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +000069 return -UNW_ENOINFO;
mostang.com!davidm824d6612003-02-08 10:10:59 +000070 }
71
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000072 shdr = (Elf_W (Shdr) *) ((char *) ei->image + soff);
mostang.com!davidm824d6612003-02-08 10:10:59 +000073
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!davidm4fafd8c2003-12-20 11:23:44 +000080 symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset);
81 symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size);
mostang.com!davidm824d6612003-02-08 10:10:59 +000082 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!davidm71650252003-09-25 05:29:14 +000088 __FUNCTION__,
89 (unsigned long) (str_soff + ehdr->e_shentsize),
90 (unsigned long) ei->size);
mostang.com!davidm824d6612003-02-08 10:10:59 +000091 break;
92 }
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000093 str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff);
mostang.com!davidm824d6612003-02-08 10:10:59 +000094 strtab = (char *) ei->image + str_shdr->sh_offset;
95
hp.com!davidm71650252003-09-25 05:29:14 +000096 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!davidm824d6612003-02-08 10:10:59 +000099
100 for (sym = symtab;
101 sym < symtab_end;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000102 sym = (Elf_W (Sym) *) ((char *) sym + syment_size))
mostang.com!davidm824d6612003-02-08 10:10:59 +0000103 {
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000104 if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC
mostang.com!davidm824d6612003-02-08 10:10:59 +0000105 && sym->st_shndx != SHN_UNDEF)
106 {
107 val = sym->st_value;
108 if (sym->st_shndx != SHN_ABS)
109 val += load_offset;
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000110 debug (160, "0x%016lx info=0x%02x %s\n",
hp.com!davidm71650252003-09-25 05:29:14 +0000111 (long) val, sym->st_info, strtab + sym->st_name);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000112
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000113 if ((Elf_W (Addr)) (ip - val) < min_dist)
mostang.com!davidm824d6612003-02-08 10:10:59 +0000114 {
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000115 min_dist = (Elf_W (Addr)) (ip - val);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000116 strncpy (buf, strtab + sym->st_name, buf_len);
117 buf[buf_len - 1] = '\0';
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000118 if (strlen (strtab + sym->st_name) >= buf_len)
119 ret = -UNW_ENOMEM;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000120 }
121 }
122 }
123 break;
124
125 default:
126 break;
127 }
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000128 shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000129 }
130 if (min_dist >= ei->size)
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000131 return -UNW_ENOINFO; /* not found */
mostang.com!davidm824d6612003-02-08 10:10:59 +0000132 if (offp)
133 *offp = min_dist;
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000134 return ret;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000135}
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
142HIDDEN int
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000143elf_w (get_proc_name) (pid_t pid, unw_word_t ip, char *buf, size_t buf_len,
144 unw_word_t *offp)
mostang.com!davidm824d6612003-02-08 10:10:59 +0000145{
146 unsigned long segbase, mapoff;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000147 Elf_W (Addr) load_offset = 0;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000148 struct elf_image ei;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000149 Elf_W (Ehdr) *ehdr;
150 Elf_W (Phdr) *phdr;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000151 int i, ret;
152
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000153 ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000154 if (ret < 0)
155 return ret;
156
157 ehdr = ei.image;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000158 phdr = (Elf_W (Phdr) *) ((char *) ei.image + ehdr->e_phoff);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000159
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!davidm4fafd8c2003-12-20 11:23:44 +0000167 ret = elf_w (lookup_symbol) (ip, &ei, load_offset, buf, buf_len, offp);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000168
169 munmap (ei.image, ei.size);
170 ei.image = NULL;
171
172 return ret;
173}