blob: 7b1350673d8ccfb56ae09a81fec10e92be61de24 [file] [log] [blame]
mostang.com!davidm824d6612003-02-08 10:10:59 +00001/* libunwind - a platform-independent unwind library
hp.com!davidm379fb062005-05-20 09:48:08 +00002 Copyright (C) 2003-2005 Hewlett-Packard Co
mostang.com!davidm824d6612003-02-08 10:10:59 +00003 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>
mostang.com!davidm824d6612003-02-08 10:10:59 +000027
hp.com!davidm379fb062005-05-20 09:48:08 +000028#include "libunwind_i.h"
mostang.com!davidm824d6612003-02-08 10:10:59 +000029
hp.com!davidm5d8b2e32005-05-03 09:13:17 +000030HIDDEN int
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000031elf_w (valid_object) (struct elf_image *ei)
mostang.com!davidm824d6612003-02-08 10:10:59 +000032{
33 if (ei->size <= EI_CLASS)
34 return 0;
35
36 return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
37 && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS);
38}
39
40
41static int
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000042elf_w (lookup_symbol) (unw_word_t ip, struct elf_image *ei,
43 Elf_W (Addr) load_offset,
44 char *buf, size_t buf_len, unw_word_t *offp)
mostang.com!davidm824d6612003-02-08 10:10:59 +000045{
hp.com!davidmdee53d72003-11-27 06:52:54 +000046 size_t syment_size;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000047 Elf_W (Ehdr) *ehdr = ei->image;
48 Elf_W (Sym) *sym, *symtab, *symtab_end;
49 Elf_W (Off) soff, str_soff;
50 Elf_W (Shdr) *shdr, *str_shdr;
51 Elf_W (Addr) val, min_dist = ~(Elf_W (Addr))0;
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +000052 int i, ret = 0;
mostang.com!davidm824d6612003-02-08 10:10:59 +000053 char *strtab;
mostang.com!davidm824d6612003-02-08 10:10:59 +000054
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000055 if (!elf_w (valid_object) (ei))
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +000056 return -UNW_ENOINFO;
mostang.com!davidm824d6612003-02-08 10:10:59 +000057
58 soff = ehdr->e_shoff;
59 if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size)
60 {
hp.com!davidmc67d3452004-01-30 00:01:24 +000061 Debug (1, "section table outside of image? (%lu > %lu)\n",
hp.com!davidm71650252003-09-25 05:29:14 +000062 (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize),
63 (unsigned long) ei->size);
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +000064 return -UNW_ENOINFO;
mostang.com!davidm824d6612003-02-08 10:10:59 +000065 }
66
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000067 shdr = (Elf_W (Shdr) *) ((char *) ei->image + soff);
mostang.com!davidm824d6612003-02-08 10:10:59 +000068
69 for (i = 0; i < ehdr->e_shnum; ++i)
70 {
71 switch (shdr->sh_type)
72 {
73 case SHT_SYMTAB:
74 case SHT_DYNSYM:
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000075 symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset);
76 symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size);
mostang.com!davidm824d6612003-02-08 10:10:59 +000077 syment_size = shdr->sh_entsize;
78
79 str_soff = soff + (shdr->sh_link * ehdr->e_shentsize);
80 if (str_soff + ehdr->e_shentsize >= ei->size)
81 {
hp.com!davidmc67d3452004-01-30 00:01:24 +000082 Debug (1, "string table outside of image? (%lu >= %lu)\n",
hp.com!davidm71650252003-09-25 05:29:14 +000083 (unsigned long) (str_soff + ehdr->e_shentsize),
84 (unsigned long) ei->size);
mostang.com!davidm824d6612003-02-08 10:10:59 +000085 break;
86 }
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000087 str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff);
mostang.com!davidm824d6612003-02-08 10:10:59 +000088 strtab = (char *) ei->image + str_shdr->sh_offset;
89
hp.com!davidmc67d3452004-01-30 00:01:24 +000090 Debug (16, "symtab=0x%lx[%d], strtab=0x%lx\n",
hp.com!davidm71650252003-09-25 05:29:14 +000091 (long) shdr->sh_offset, shdr->sh_type,
92 (long) str_shdr->sh_offset);
mostang.com!davidm824d6612003-02-08 10:10:59 +000093
94 for (sym = symtab;
95 sym < symtab_end;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000096 sym = (Elf_W (Sym) *) ((char *) sym + syment_size))
mostang.com!davidm824d6612003-02-08 10:10:59 +000097 {
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000098 if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC
mostang.com!davidm824d6612003-02-08 10:10:59 +000099 && sym->st_shndx != SHN_UNDEF)
100 {
101 val = sym->st_value;
102 if (sym->st_shndx != SHN_ABS)
103 val += load_offset;
hp.com!davidmc67d3452004-01-30 00:01:24 +0000104 Debug (16, "0x%016lx info=0x%02x %s\n",
hp.com!davidm71650252003-09-25 05:29:14 +0000105 (long) val, sym->st_info, strtab + sym->st_name);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000106
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000107 if ((Elf_W (Addr)) (ip - val) < min_dist)
mostang.com!davidm824d6612003-02-08 10:10:59 +0000108 {
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000109 min_dist = (Elf_W (Addr)) (ip - val);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000110 strncpy (buf, strtab + sym->st_name, buf_len);
111 buf[buf_len - 1] = '\0';
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000112 if (strlen (strtab + sym->st_name) >= buf_len)
113 ret = -UNW_ENOMEM;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000114 }
115 }
116 }
117 break;
118
119 default:
120 break;
121 }
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000122 shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000123 }
124 if (min_dist >= ei->size)
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000125 return -UNW_ENOINFO; /* not found */
mostang.com!davidm824d6612003-02-08 10:10:59 +0000126 if (offp)
127 *offp = min_dist;
mostang.com!davidm7bfbbb62003-03-19 19:25:18 +0000128 return ret;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000129}
130
131/* Find the ELF image that contains IP and return the "closest"
132 procedure name, if there is one. With some caching, this could be
133 sped up greatly, but until an application materializes that's
134 sensitive to the performance of this routine, why bother... */
135
hp.com!davidm5d8b2e32005-05-03 09:13:17 +0000136HIDDEN int
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000137elf_w (get_proc_name) (pid_t pid, unw_word_t ip, char *buf, size_t buf_len,
138 unw_word_t *offp)
mostang.com!davidm824d6612003-02-08 10:10:59 +0000139{
140 unsigned long segbase, mapoff;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000141 Elf_W (Addr) load_offset = 0;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000142 struct elf_image ei;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000143 Elf_W (Ehdr) *ehdr;
144 Elf_W (Phdr) *phdr;
mostang.com!davidm824d6612003-02-08 10:10:59 +0000145 int i, ret;
146
mostang.com!davidm03950aa2003-02-27 09:58:57 +0000147 ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000148 if (ret < 0)
149 return ret;
150
151 ehdr = ei.image;
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000152 phdr = (Elf_W (Phdr) *) ((char *) ei.image + ehdr->e_phoff);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000153
154 for (i = 0; i < ehdr->e_phnum; ++i)
155 if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff)
156 {
157 load_offset = segbase - phdr[i].p_vaddr;
158 break;
159 }
160
hp.com!davidm4fafd8c2003-12-20 11:23:44 +0000161 ret = elf_w (lookup_symbol) (ip, &ei, load_offset, buf, buf_len, offp);
mostang.com!davidm824d6612003-02-08 10:10:59 +0000162
163 munmap (ei.image, ei.size);
164 ei.image = NULL;
165
166 return ret;
167}