2003-02-08 11:10:59 +01:00
|
|
|
/* libunwind - a platform-independent unwind library
|
2005-05-20 11:48:08 +02:00
|
|
|
Copyright (C) 2003-2005 Hewlett-Packard Co
|
Introduce a tdep_get_func_addr_hook() in the ELF lookup_symbol()
routine and add address-space argument. This is needed because on
PPC64, a the function-name symbol refers to a function descriptor
(unlike, for example, on ia64, where the @fptr() operator is needed to
refer to a function descriptor). Thus, in order to look up the name
of a function, we need to dereference the function descriptor. To
make matters more "interesting", the function descriptors are normally
resolved by the dynamic linker, so we can't get their values from the
ELF file. Instead, we have to read them from the running image, hence
the need for the address-space argument.
2007-08-22 21:02:09 +02:00
|
|
|
Copyright (C) 2007 David Mosberger-Tang
|
|
|
|
Contributed by David Mosberger-Tang <dmosberger@gmail.com>
|
2003-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
This file is part of libunwind.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-05-15 21:14:09 +02:00
|
|
|
#include <sys/param.h>
|
2003-02-08 11:10:59 +01:00
|
|
|
|
2005-05-20 11:48:08 +02:00
|
|
|
#include "libunwind_i.h"
|
2003-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
static int
|
Introduce a tdep_get_func_addr_hook() in the ELF lookup_symbol()
routine and add address-space argument. This is needed because on
PPC64, a the function-name symbol refers to a function descriptor
(unlike, for example, on ia64, where the @fptr() operator is needed to
refer to a function descriptor). Thus, in order to look up the name
of a function, we need to dereference the function descriptor. To
make matters more "interesting", the function descriptors are normally
resolved by the dynamic linker, so we can't get their values from the
ELF file. Instead, we have to read them from the running image, hence
the need for the address-space argument.
2007-08-22 21:02:09 +02:00
|
|
|
elf_w (lookup_symbol) (unw_addr_space_t as,
|
|
|
|
unw_word_t ip, struct elf_image *ei,
|
2003-12-20 12:23:44 +01:00
|
|
|
Elf_W (Addr) load_offset,
|
|
|
|
char *buf, size_t buf_len, unw_word_t *offp)
|
2003-02-08 11:10:59 +01:00
|
|
|
{
|
2003-11-27 07:52:54 +01:00
|
|
|
size_t syment_size;
|
2003-12-20 12:23:44 +01:00
|
|
|
Elf_W (Ehdr) *ehdr = ei->image;
|
|
|
|
Elf_W (Sym) *sym, *symtab, *symtab_end;
|
|
|
|
Elf_W (Off) soff, str_soff;
|
|
|
|
Elf_W (Shdr) *shdr, *str_shdr;
|
|
|
|
Elf_W (Addr) val, min_dist = ~(Elf_W (Addr))0;
|
2003-03-19 20:25:18 +01:00
|
|
|
int i, ret = 0;
|
2003-02-08 11:10:59 +01:00
|
|
|
char *strtab;
|
|
|
|
|
2003-12-20 12:23:44 +01:00
|
|
|
if (!elf_w (valid_object) (ei))
|
2003-03-19 20:25:18 +01:00
|
|
|
return -UNW_ENOINFO;
|
2003-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
soff = ehdr->e_shoff;
|
|
|
|
if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size)
|
|
|
|
{
|
2004-01-30 01:01:24 +01:00
|
|
|
Debug (1, "section table outside of image? (%lu > %lu)\n",
|
2003-09-25 07:29:14 +02:00
|
|
|
(unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize),
|
|
|
|
(unsigned long) ei->size);
|
2003-03-19 20:25:18 +01:00
|
|
|
return -UNW_ENOINFO;
|
2003-02-08 11:10:59 +01:00
|
|
|
}
|
|
|
|
|
2003-12-20 12:23:44 +01:00
|
|
|
shdr = (Elf_W (Shdr) *) ((char *) ei->image + soff);
|
2003-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ehdr->e_shnum; ++i)
|
|
|
|
{
|
|
|
|
switch (shdr->sh_type)
|
|
|
|
{
|
|
|
|
case SHT_SYMTAB:
|
|
|
|
case SHT_DYNSYM:
|
2003-12-20 12:23:44 +01:00
|
|
|
symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset);
|
|
|
|
symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size);
|
2003-02-08 11:10:59 +01:00
|
|
|
syment_size = shdr->sh_entsize;
|
|
|
|
|
|
|
|
str_soff = soff + (shdr->sh_link * ehdr->e_shentsize);
|
|
|
|
if (str_soff + ehdr->e_shentsize >= ei->size)
|
|
|
|
{
|
2004-01-30 01:01:24 +01:00
|
|
|
Debug (1, "string table outside of image? (%lu >= %lu)\n",
|
2003-09-25 07:29:14 +02:00
|
|
|
(unsigned long) (str_soff + ehdr->e_shentsize),
|
|
|
|
(unsigned long) ei->size);
|
2003-02-08 11:10:59 +01:00
|
|
|
break;
|
|
|
|
}
|
2003-12-20 12:23:44 +01:00
|
|
|
str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff);
|
2003-02-08 11:10:59 +01:00
|
|
|
strtab = (char *) ei->image + str_shdr->sh_offset;
|
|
|
|
|
2004-01-30 01:01:24 +01:00
|
|
|
Debug (16, "symtab=0x%lx[%d], strtab=0x%lx\n",
|
2003-09-25 07:29:14 +02:00
|
|
|
(long) shdr->sh_offset, shdr->sh_type,
|
|
|
|
(long) str_shdr->sh_offset);
|
2003-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
for (sym = symtab;
|
|
|
|
sym < symtab_end;
|
2003-12-20 12:23:44 +01:00
|
|
|
sym = (Elf_W (Sym) *) ((char *) sym + syment_size))
|
2003-02-08 11:10:59 +01:00
|
|
|
{
|
2003-12-20 12:23:44 +01:00
|
|
|
if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC
|
2003-02-08 11:10:59 +01:00
|
|
|
&& sym->st_shndx != SHN_UNDEF)
|
|
|
|
{
|
Introduce a tdep_get_func_addr_hook() in the ELF lookup_symbol()
routine and add address-space argument. This is needed because on
PPC64, a the function-name symbol refers to a function descriptor
(unlike, for example, on ia64, where the @fptr() operator is needed to
refer to a function descriptor). Thus, in order to look up the name
of a function, we need to dereference the function descriptor. To
make matters more "interesting", the function descriptors are normally
resolved by the dynamic linker, so we can't get their values from the
ELF file. Instead, we have to read them from the running image, hence
the need for the address-space argument.
2007-08-22 21:02:09 +02:00
|
|
|
if (tdep_get_func_addr (as, sym->st_value, &val) < 0)
|
|
|
|
continue;
|
2003-02-08 11:10:59 +01:00
|
|
|
if (sym->st_shndx != SHN_ABS)
|
|
|
|
val += load_offset;
|
2004-01-30 01:01:24 +01:00
|
|
|
Debug (16, "0x%016lx info=0x%02x %s\n",
|
2003-09-25 07:29:14 +02:00
|
|
|
(long) val, sym->st_info, strtab + sym->st_name);
|
2003-02-08 11:10:59 +01:00
|
|
|
|
2003-12-20 12:23:44 +01:00
|
|
|
if ((Elf_W (Addr)) (ip - val) < min_dist)
|
2003-02-08 11:10:59 +01:00
|
|
|
{
|
2003-12-20 12:23:44 +01:00
|
|
|
min_dist = (Elf_W (Addr)) (ip - val);
|
2003-02-08 11:10:59 +01:00
|
|
|
strncpy (buf, strtab + sym->st_name, buf_len);
|
|
|
|
buf[buf_len - 1] = '\0';
|
2003-03-19 20:25:18 +01:00
|
|
|
if (strlen (strtab + sym->st_name) >= buf_len)
|
|
|
|
ret = -UNW_ENOMEM;
|
2003-02-08 11:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2003-12-20 12:23:44 +01:00
|
|
|
shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
|
2003-02-08 11:10:59 +01:00
|
|
|
}
|
|
|
|
if (min_dist >= ei->size)
|
2003-03-19 20:25:18 +01:00
|
|
|
return -UNW_ENOINFO; /* not found */
|
2003-02-08 11:10:59 +01:00
|
|
|
if (offp)
|
|
|
|
*offp = min_dist;
|
2003-03-19 20:25:18 +01:00
|
|
|
return ret;
|
2003-02-08 11:10:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the ELF image that contains IP and return the "closest"
|
|
|
|
procedure name, if there is one. With some caching, this could be
|
|
|
|
sped up greatly, but until an application materializes that's
|
|
|
|
sensitive to the performance of this routine, why bother... */
|
|
|
|
|
2005-05-03 11:13:17 +02:00
|
|
|
HIDDEN int
|
2012-03-13 02:45:50 +01:00
|
|
|
elf_w (get_proc_name_in_image) (unw_addr_space_t as, struct elf_image *ei,
|
|
|
|
unsigned long segbase,
|
|
|
|
unsigned long mapoff,
|
|
|
|
unw_word_t ip,
|
Introduce a tdep_get_func_addr_hook() in the ELF lookup_symbol()
routine and add address-space argument. This is needed because on
PPC64, a the function-name symbol refers to a function descriptor
(unlike, for example, on ia64, where the @fptr() operator is needed to
refer to a function descriptor). Thus, in order to look up the name
of a function, we need to dereference the function descriptor. To
make matters more "interesting", the function descriptors are normally
resolved by the dynamic linker, so we can't get their values from the
ELF file. Instead, we have to read them from the running image, hence
the need for the address-space argument.
2007-08-22 21:02:09 +02:00
|
|
|
char *buf, size_t buf_len, unw_word_t *offp)
|
2003-02-08 11:10:59 +01:00
|
|
|
{
|
2003-12-20 12:23:44 +01:00
|
|
|
Elf_W (Addr) load_offset = 0;
|
|
|
|
Elf_W (Ehdr) *ehdr;
|
|
|
|
Elf_W (Phdr) *phdr;
|
2003-02-08 11:10:59 +01:00
|
|
|
int i, ret;
|
|
|
|
|
2012-03-13 02:45:50 +01:00
|
|
|
ehdr = ei->image;
|
|
|
|
phdr = (Elf_W (Phdr) *) ((char *) ei->image + ehdr->e_phoff);
|
2003-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ehdr->e_phnum; ++i)
|
|
|
|
if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff)
|
|
|
|
{
|
|
|
|
load_offset = segbase - phdr[i].p_vaddr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-13 02:45:50 +01:00
|
|
|
ret = elf_w (lookup_symbol) (as, ip, ei, load_offset, buf, buf_len, offp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
HIDDEN int
|
|
|
|
elf_w (get_proc_name) (unw_addr_space_t as, pid_t pid, unw_word_t ip,
|
|
|
|
char *buf, size_t buf_len, unw_word_t *offp)
|
|
|
|
{
|
|
|
|
unsigned long segbase, mapoff;
|
|
|
|
struct elf_image ei;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff, NULL, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = elf_w (get_proc_name_in_image) (as, &ei, segbase, mapoff, ip, buf, buf_len, offp);
|
2003-02-08 11:10:59 +01:00
|
|
|
|
|
|
|
munmap (ei.image, ei.size);
|
|
|
|
ei.image = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|