2003-01-28 04:40:06 +01:00
|
|
|
/* libunwind - a platform-independent unwind library
|
|
|
|
Copyright (C) 2003 Hewlett-Packard Co
|
|
|
|
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
|
|
|
|
|
|
|
|
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 <elf.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include "_UPT_internal.h"
|
|
|
|
|
2003-02-08 11:10:59 +01:00
|
|
|
#if UNW_TARGET_IA64
|
|
|
|
|
|
|
|
#include "elf64.h"
|
|
|
|
|
2003-01-28 04:40:06 +01:00
|
|
|
static unw_word_t
|
|
|
|
find_gp (struct UPT_info *ui, Elf64_Phdr *pdyn, Elf64_Addr load_base)
|
|
|
|
{
|
|
|
|
Elf64_Off soff, str_soff;
|
2003-02-08 11:10:59 +01:00
|
|
|
Elf64_Ehdr *ehdr = ui->ei.image;
|
2003-01-28 04:40:06 +01:00
|
|
|
Elf64_Shdr *shdr;
|
|
|
|
Elf64_Shdr *str_shdr;
|
|
|
|
Elf64_Addr gp = 0;
|
|
|
|
char *strtab;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pdyn)
|
|
|
|
{
|
|
|
|
/* If we have a PT_DYNAMIC program header, fetch the gp-value
|
|
|
|
from the DT_PLTGOT entry. */
|
2003-03-28 08:43:22 +01:00
|
|
|
Elf64_Dyn *dyn = (Elf64_Dyn *) (pdyn->p_offset + (char *) ui->ei.image);
|
2003-01-28 04:40:06 +01:00
|
|
|
for (; dyn->d_tag != DT_NULL; ++dyn)
|
|
|
|
if (dyn->d_tag == DT_PLTGOT)
|
|
|
|
{
|
|
|
|
gp = (Elf64_Addr) dyn->d_un.d_ptr + load_base;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Without a PT_DYAMIC header, lets try to look for a non-empty .opd
|
|
|
|
section. If there is such a section, we know it's full of
|
|
|
|
function descriptors, and we can simply pick up the gp from the
|
|
|
|
second word of the first entry in this table. */
|
|
|
|
|
|
|
|
soff = ehdr->e_shoff;
|
|
|
|
str_soff = soff + (ehdr->e_shstrndx * ehdr->e_shentsize);
|
|
|
|
|
2003-02-08 11:10:59 +01:00
|
|
|
if (soff + ehdr->e_shnum * ehdr->e_shentsize > ui->ei.size)
|
2003-01-28 04:40:06 +01:00
|
|
|
{
|
2003-02-08 11:10:59 +01:00
|
|
|
debug (1, "%s: section table outside of image? (%lu > %lu)",
|
|
|
|
__FUNCTION__, soff + ehdr->e_shnum * ehdr->e_shentsize,
|
|
|
|
ui->ei.size);
|
2003-01-28 04:40:06 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2003-02-08 11:10:59 +01:00
|
|
|
shdr = (Elf64_Shdr *) ((char *) ui->ei.image + soff);
|
|
|
|
str_shdr = (Elf64_Shdr *) ((char *) ui->ei.image + str_soff);
|
|
|
|
strtab = (char *) ui->ei.image + str_shdr->sh_offset;
|
2003-01-28 04:40:06 +01:00
|
|
|
for (i = 0; i < ehdr->e_shnum; ++i)
|
|
|
|
{
|
|
|
|
if (strcmp (strtab + shdr->sh_name, ".opd") == 0
|
|
|
|
&& shdr->sh_size >= 16)
|
|
|
|
{
|
2003-03-28 08:43:22 +01:00
|
|
|
gp = ((Elf64_Addr *) ((char *) ui->ei.image + shdr->sh_offset))[1];
|
2003-01-28 04:40:06 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
shdr = (Elf64_Shdr *) (((char *) shdr) + ehdr->e_shentsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2003-02-08 11:10:59 +01:00
|
|
|
debug (100, "%s: image at %p, gp = %lx\n", __FUNCTION__, ui->ei.image, gp);
|
2003-01-28 04:40:06 +01:00
|
|
|
return gp;
|
|
|
|
}
|
|
|
|
|
|
|
|
HIDDEN unw_dyn_info_t *
|
|
|
|
_UPTi_find_unwind_table (struct UPT_info *ui, unw_addr_space_t as,
|
|
|
|
char *path, unw_word_t segbase, unw_word_t mapoff)
|
|
|
|
{
|
|
|
|
Elf64_Phdr *phdr, *ptxt = NULL, *punw = NULL, *pdyn = NULL;
|
|
|
|
Elf64_Ehdr *ehdr;
|
2003-02-08 11:10:59 +01:00
|
|
|
int i;
|
2003-01-28 04:40:06 +01:00
|
|
|
|
2003-02-08 11:10:59 +01:00
|
|
|
if (!_Uelf64_valid_object (&ui->ei))
|
2003-01-28 04:40:06 +01:00
|
|
|
return NULL;
|
|
|
|
|
2003-02-08 11:10:59 +01:00
|
|
|
ehdr = ui->ei.image;
|
|
|
|
phdr = (Elf64_Phdr *) ((char *) ui->ei.image + ehdr->e_phoff);
|
2003-01-28 04:40:06 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ehdr->e_phnum; ++i)
|
|
|
|
{
|
|
|
|
switch (phdr[i].p_type)
|
|
|
|
{
|
|
|
|
case PT_LOAD:
|
|
|
|
if (phdr[i].p_offset == mapoff)
|
|
|
|
ptxt = phdr + i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PT_IA_64_UNWIND:
|
|
|
|
punw = phdr + i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PT_DYNAMIC:
|
|
|
|
pdyn = phdr + i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-02-27 10:58:57 +01:00
|
|
|
if (!ptxt || !punw
|
|
|
|
/* Verify that the segment that contains the IP also contains
|
|
|
|
the static unwind table. If not, we are dealing with
|
|
|
|
runtime-generated code, for which we have no info here. */
|
|
|
|
|| (punw->p_vaddr - ptxt->p_vaddr) >= ptxt->p_memsz)
|
2003-01-28 04:40:06 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ui->di_cache.start_ip = segbase;
|
|
|
|
ui->di_cache.end_ip = ui->di_cache.start_ip + ptxt->p_memsz;
|
|
|
|
ui->di_cache.gp = find_gp (ui, pdyn, segbase - ptxt->p_vaddr);
|
|
|
|
ui->di_cache.format = UNW_INFO_FORMAT_TABLE;
|
|
|
|
ui->di_cache.u.ti.name_ptr = 0;
|
|
|
|
ui->di_cache.u.ti.segbase = segbase;
|
|
|
|
ui->di_cache.u.ti.table_len = punw->p_memsz / sizeof (unw_word_t);
|
|
|
|
ui->di_cache.u.ti.table_data = (unw_word_t *)
|
2003-02-08 11:10:59 +01:00
|
|
|
((char *) ui->ei.image + (punw->p_vaddr - ptxt->p_vaddr));
|
2003-01-28 04:40:06 +01:00
|
|
|
return &ui->di_cache;
|
|
|
|
}
|
|
|
|
|
2003-03-29 08:32:50 +01:00
|
|
|
#elif UNW_TARGET_X86
|
|
|
|
|
|
|
|
HIDDEN unw_dyn_info_t *
|
|
|
|
_UPTi_find_unwind_table (struct UPT_info *ui, unw_addr_space_t as,
|
|
|
|
char *path, unw_word_t segbase, unw_word_t mapoff)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* UNW_TARGET_X86 */
|
2003-02-08 11:10:59 +01:00
|
|
|
|
2003-01-28 04:40:06 +01:00
|
|
|
static unw_dyn_info_t *
|
2003-02-14 04:09:27 +01:00
|
|
|
get_unwind_info (struct UPT_info *ui, unw_addr_space_t as, unw_word_t ip)
|
2003-01-28 04:40:06 +01:00
|
|
|
{
|
2003-02-08 11:10:59 +01:00
|
|
|
unsigned long segbase, mapoff;
|
2003-01-28 04:40:06 +01:00
|
|
|
char path[PATH_MAX];
|
2003-02-27 10:58:57 +01:00
|
|
|
unw_dyn_info_t *di;
|
2003-01-28 04:40:06 +01:00
|
|
|
|
2003-04-03 09:59:15 +02:00
|
|
|
#if UNW_TARGET_IA64 && defined(__linux)
|
2003-02-08 11:10:59 +01:00
|
|
|
if (!ui->ktab.start_ip && _Uia64_get_kernel_table (&ui->ktab) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2003-01-28 04:40:06 +01:00
|
|
|
if (ip >= ui->ktab.start_ip && ip < ui->ktab.end_ip)
|
|
|
|
return &ui->ktab;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ip >= ui->di_cache.start_ip && ip < ui->di_cache.end_ip)
|
|
|
|
return &ui->di_cache;
|
|
|
|
|
2003-02-08 11:10:59 +01:00
|
|
|
if (ui->ei.image)
|
2003-01-28 04:40:06 +01:00
|
|
|
{
|
2003-02-08 11:10:59 +01:00
|
|
|
munmap (ui->ei.image, ui->ei.size);
|
|
|
|
ui->ei.image = NULL;
|
|
|
|
ui->ei.size = 0;
|
|
|
|
|
|
|
|
/* invalidate the cache: */
|
|
|
|
ui->di_cache.start_ip = ui->di_cache.end_ip = 0;
|
2003-01-28 04:40:06 +01:00
|
|
|
}
|
|
|
|
|
2003-02-08 11:10:59 +01:00
|
|
|
if (tdep_get_elf_image (&ui->ei, ui->pid, ip, &segbase, &mapoff) < 0)
|
2003-01-28 04:40:06 +01:00
|
|
|
return NULL;
|
|
|
|
|
2003-02-27 10:58:57 +01:00
|
|
|
di = _UPTi_find_unwind_table (ui, as, path, segbase, mapoff);
|
|
|
|
if (!di
|
|
|
|
/* This can happen in corner cases where dynamically generated
|
|
|
|
code falls into the same page that contains the data-segment
|
|
|
|
and the page-offset of the code is within the first page of
|
|
|
|
the executable. */
|
|
|
|
|| ip < di->start_ip || ip >= di->end_ip)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return di;
|
2003-01-28 04:40:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_UPT_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
|
|
|
|
int need_unwind_info, void *arg)
|
|
|
|
{
|
|
|
|
struct UPT_info *ui = arg;
|
|
|
|
unw_dyn_info_t *di;
|
|
|
|
|
2003-02-14 04:09:27 +01:00
|
|
|
di = get_unwind_info (ui, as, ip);
|
2003-01-28 04:40:06 +01:00
|
|
|
if (!di)
|
|
|
|
return -UNW_ENOINFO;
|
|
|
|
|
2003-03-29 08:32:50 +01:00
|
|
|
#if UNW_TARGET_IA64
|
|
|
|
if (di == &ui->ktab)
|
|
|
|
{
|
|
|
|
/* The kernel unwind table resides in local memory, so we have
|
|
|
|
to use the local address space to search it. Since
|
|
|
|
_UPT_put_unwind_info() has no easy way of detecting this
|
|
|
|
case, we simply make a copy of the unwind-info, so
|
|
|
|
_UPT_put_unwind_info() can always free() the unwind-info
|
|
|
|
without ill effects. */
|
|
|
|
int ret = tdep_search_unwind_table (unw_local_addr_space, ip, di, pi,
|
|
|
|
need_unwind_info, arg);
|
|
|
|
if (ret >= 0 && need_unwind_info)
|
|
|
|
{
|
|
|
|
void *mem = malloc (pi->unwind_info_size);
|
|
|
|
|
|
|
|
if (!mem)
|
|
|
|
return -UNW_ENOMEM;
|
|
|
|
memcpy (mem, pi->unwind_info, pi->unwind_info_size);
|
|
|
|
pi->unwind_info = mem;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return tdep_search_unwind_table (as, ip, di, pi, need_unwind_info, arg);
|
2003-01-28 04:40:06 +01:00
|
|
|
}
|