#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include "libunwind_i.h"
#include "dwarf-eh.h"
#include "dwarf_i.h"
#define to_unw_word(p) ((unw_word_t) (uintptr_t) (p))
int
dwarf_find_unwind_table (struct elf_dyn_info *edi,
unw_addr_space_t as UNUSED,
const char *path UNUSED,
unw_word_t segbase,
unw_word_t mapoff,
unw_word_t ip UNUSED)
{
Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
unw_word_t addr, eh_frame_start, fde_count, loadoff, load_base;
unw_word_t max_load_addr = 0;
unw_word_t start_ip = to_unw_word (-1);
unw_word_t end_ip = 0;
struct dwarf_eh_frame_hdr *hdr;
unw_proc_info_t pi;
unw_accessors_t *a;
Elf_W(Ehdr) *ehdr;
#if UNW_TARGET_ARM
const Elf_W(Phdr) *param_exidx = NULL;
#endif
int i, ret, found = 0;
if (!elf_w(valid_object) (&edi->ei))
return -UNW_ENOINFO;
ehdr = edi->ei.image;
phdr = (Elf_W(Phdr) *) ((char *) edi->ei.image + ehdr->e_phoff);
for (i = 0; i < ehdr->e_phnum; ++i)
{
switch (phdr[i].p_type)
{
case PT_LOAD:
if (phdr[i].p_vaddr < start_ip)
start_ip = phdr[i].p_vaddr;
if (phdr[i].p_vaddr + phdr[i].p_memsz > end_ip)
end_ip = phdr[i].p_vaddr + phdr[i].p_memsz;
if ((phdr[i].p_flags & PF_X) == PF_X)
ptxt = phdr + i;
if ((uintptr_t) edi->ei.image + phdr->p_filesz > max_load_addr)
max_load_addr = (uintptr_t) edi->ei.image + phdr->p_filesz;
break;
case PT_GNU_EH_FRAME:
#if defined __sun
case PT_SUNW_UNWIND:
#endif
peh_hdr = phdr + i;
break;
case PT_DYNAMIC:
pdyn = phdr + i;
break;
#if UNW_TARGET_ARM
case PT_ARM_EXIDX:
param_exidx = phdr + i;
break;
#endif
default:
break;
}
}
if (!ptxt)
return 0;
loadoff = mapoff + (ptxt->p_vaddr - ptxt->p_offset);
load_base = segbase - loadoff;
start_ip += load_base;
end_ip += load_base;
if (peh_hdr)
{
if (pdyn)
{
Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(pdyn->p_offset
+ (char *) edi->ei.image);
for (; dyn->d_tag != DT_NULL; ++dyn)
if (dyn->d_tag == DT_PLTGOT)
{
edi->di_cache.gp = dyn->d_un.d_ptr;
break;
}
}
else
edi->di_cache.gp = 0;
hdr = (struct dwarf_eh_frame_hdr *) (peh_hdr->p_offset
+ (char *) edi->ei.image);
if (hdr->version != DW_EH_VERSION)
{
Debug (1, "table `%s' has unexpected version %d\n",
path, hdr->version);
return -UNW_ENOINFO;
}
a = unw_get_accessors_int (unw_local_addr_space);
addr = to_unw_word (&hdr->eh_frame);
memset (&pi, 0, sizeof (pi));
pi.gp = edi->di_cache.gp;
if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
&addr, hdr->eh_frame_ptr_enc, &pi,
&eh_frame_start, NULL)) < 0)
return -UNW_ENOINFO;
if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
&addr, hdr->fde_count_enc, &pi,
&fde_count, NULL)) < 0)
return -UNW_ENOINFO;
if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
{
#if 1
abort ();
#else #endif
}
edi->di_cache.start_ip = start_ip;
edi->di_cache.end_ip = end_ip;
edi->di_cache.load_offset = 0;
edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
edi->di_cache.u.rti.name_ptr = 0;
edi->di_cache.u.rti.table_len = (fde_count * 8) / sizeof (unw_word_t);
edi->di_cache.u.rti.table_data = ((load_base + peh_hdr->p_vaddr)
+ (addr - to_unw_word (edi->ei.image)
- peh_hdr->p_offset));
edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
+ (to_unw_word (hdr) -
to_unw_word (edi->ei.image)
- peh_hdr->p_offset));
found = 1;
}
#if UNW_TARGET_ARM
if (param_exidx)
{
edi->di_arm.format = UNW_INFO_FORMAT_ARM_EXIDX;
edi->di_arm.start_ip = start_ip;
edi->di_arm.end_ip = end_ip;
edi->di_arm.u.rti.name_ptr = to_unw_word (path);
edi->di_arm.u.rti.table_data = load_base + param_exidx->p_vaddr;
edi->di_arm.u.rti.table_len = param_exidx->p_memsz;
found = 1;
}
#endif
#ifdef CONFIG_DEBUG_FRAME
found = dwarf_find_debug_frame (found, &edi->di_debug, ip, load_base, path,
start_ip, end_ip);
#endif
return found;
}