On 03/24/2012 11:31 PM, Arun Sharma wrote:
On Fri, Mar 16, 2012 at 5:53 AM, Denys Vlasenko<[email protected]>  wrote:

I want this code to end up in libunwind.so instead,
so we can use it equally from both libs.

Our predicament seems to be that we have no good
place to put its source in the source tree, right?

drwxr-xr-x. 2 root root  4096 Feb 13 18:43 arm
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 hppa
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 ia64
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 mi
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 mips
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 ppc
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 ppc32
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 ppc64
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 x86
drwxr-xr-x. 2 root root  4096 Feb 13 18:43 x86_64

The above dirs are arch specific - not suitable.

The code is also ia64 specific. I don't have any objections to putting
it under ia64.

It isn't.

Please take a look at src/ptrace/_UPT_find_proc_info.c
(before patching. Current git, for example).

It has the following structure:


#if UNW_TARGET_IA64

static unw_word_t
find_gp (struct UPT_info *ui, Elf64_Phdr *pdyn, Elf64_Addr load_base) ...
HIDDEN int
_UPTi_find_unwind_table (struct UPT_info *ui, unw_addr_space_t as,
                         char *path, unw_word_t segbase, unw_word_t mapoff,
                         unw_word_t ip) ...

#elif UNW_TARGET_X86 || UNW_TARGET_X86_64 || UNW_TARGET_HPPA \
|| UNW_TARGET_PPC32 || UNW_TARGET_PPC64 || UNW_TARGET_ARM

int
dwarf_read_encoded_pointer (unw_addr_space_t as, unw_accessors_t *a,
                            unw_word_t *addr, unsigned char encoding,
                            const unw_proc_info_t *pi,
                            unw_word_t *valp, void *arg) ...
HIDDEN int
_UPTi_find_unwind_table (struct UPT_info *ui, unw_addr_space_t as,
                         char *path, unw_word_t segbase, unw_word_t mapoff,
                         unw_word_t ip) ...

#endif /* UNW_TARGET_X86 || UNW_TARGET_X86_64 || UNW_TARGET_HPPA*/

static int
get_unwind_info (struct UPT_info *ui, unw_addr_space_t as, unw_word_t ip) ...
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) ...

Thus, find_unwind_table exists for all architectures, not only for IA64.
It just has two different implementations: one for IA64, and another for e


Re: common

There is also a dir called "mi" (machine independent). But this is
arch specific code, so we shouldn't move it there.

Actually it might be a good place...

Please find attached patch which moves find_proc_info() to mi/*.

--
vda
commit 77e89016c23b7fc54a87407405321da6f0142a97
Author: Denys Vlasenko <[email protected]>
Date:   Thu Mar 29 18:34:03 2012 +0200

    Move find_unwind_table() from dwarf/ to mi/
    
    Signed-off-by: Denys Vlasenko <[email protected]>

diff --git a/include/dwarf.h b/include/dwarf.h
index 44d30de..20e2e4d 100644
--- a/include/dwarf.h
+++ b/include/dwarf.h
@@ -369,7 +369,6 @@ struct dwarf_callback_data
 #define dwarf_find_proc_info		UNW_OBJ (dwarf_find_proc_info)
 #define dwarf_find_debug_frame		UNW_OBJ (dwarf_find_debug_frame)
 #define dwarf_search_unwind_table	UNW_OBJ (dwarf_search_unwind_table)
-#define dwarf_find_unwind_table		UNW_OBJ (dwarf_find_unwind_table)
 #define dwarf_put_unwind_info		UNW_OBJ (dwarf_put_unwind_info)
 #define dwarf_put_unwind_info		UNW_OBJ (dwarf_put_unwind_info)
 #define dwarf_eval_expr			UNW_OBJ (dwarf_eval_expr)
@@ -397,9 +396,6 @@ extern int dwarf_search_unwind_table (unw_addr_space_t as,
 				      unw_dyn_info_t *di,
 				      unw_proc_info_t *pi,
 				      int need_unwind_info, void *arg);
-extern int dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
-				    char *path, unw_word_t segbase, unw_word_t mapoff,
-				    unw_word_t ip);
 extern void dwarf_put_unwind_info (unw_addr_space_t as,
 				   unw_proc_info_t *pi, void *arg);
 extern int dwarf_eval_expr (struct dwarf_cursor *c, unw_word_t *addr,
diff --git a/include/libunwind_i.h b/include/libunwind_i.h
index 2ece7f0..ca22a81 100644
--- a/include/libunwind_i.h
+++ b/include/libunwind_i.h
@@ -258,6 +258,11 @@ extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
 extern unw_dyn_info_list_t _U_dyn_info_list;
 extern pthread_mutex_t _U_dyn_info_list_lock;
 
+struct elf_dyn_info;
+extern int unwi_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
+				    char *path, unw_word_t segbase, unw_word_t mapoff,
+				    unw_word_t ip);
+
 #if UNW_DEBUG
 #define unwi_debug_level		UNWI_ARCH_OBJ(debug_level)
 extern long unwi_debug_level;
diff --git a/src/Makefile.am b/src/Makefile.am
index 11c7b49..b514178 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -81,7 +81,8 @@ libunwind_la_SOURCES_generic =						\
 	mi/Gput_dynamic_unwind_info.c mi/Gdestroy_addr_space.c		\
 	mi/Gget_reg.c mi/Gset_reg.c					\
 	mi/Gget_fpreg.c mi/Gset_fpreg.c					\
-	mi/Gset_caching_policy.c
+	mi/Gset_caching_policy.c					\
+	mi/Gfind_unwind_table.c
 
 if SUPPORT_CXX_EXCEPTIONS
 libunwind_la_SOURCES_local_unwind =					\
@@ -109,7 +110,9 @@ libunwind_la_SOURCES_local_nounwind =					\
 	mi/Lput_dynamic_unwind_info.c mi/Ldestroy_addr_space.c		\
 	mi/Lget_reg.c   mi/Lset_reg.c					\
 	mi/Lget_fpreg.c mi/Lset_fpreg.c					\
-	mi/Lset_caching_policy.c
+	mi/Lset_caching_policy.c					\
+	mi/Lfind_unwind_table.c
+
 
 libunwind_la_SOURCES_local =						\
 	$(libunwind_la_SOURCES_local_nounwind)				\
@@ -126,14 +129,12 @@ libunwind_dwarf_common_la_SOURCES = dwarf/global.c
 
 libunwind_dwarf_local_la_SOURCES = \
 	dwarf/Lexpr.c dwarf/Lfde.c dwarf/Lparser.c dwarf/Lpe.c dwarf/Lstep.c \
-	dwarf/Lfind_proc_info-lsb.c \
-	dwarf/Lfind_unwind_table.c
+	dwarf/Lfind_proc_info-lsb.c
 libunwind_dwarf_local_la_LIBADD = libunwind-dwarf-common.la
 
 libunwind_dwarf_generic_la_SOURCES = \
 	dwarf/Gexpr.c dwarf/Gfde.c dwarf/Gparser.c dwarf/Gpe.c dwarf/Gstep.c \
-	dwarf/Gfind_proc_info-lsb.c \
-	dwarf/Gfind_unwind_table.c
+	dwarf/Gfind_proc_info-lsb.c
 libunwind_dwarf_generic_la_LIBADD = libunwind-dwarf-common.la
 
 if USE_DWARF
diff --git a/src/coredump/_UCD_find_proc_info.c b/src/coredump/_UCD_find_proc_info.c
index b727ea9..584b18c 100644
--- a/src/coredump/_UCD_find_proc_info.c
+++ b/src/coredump/_UCD_find_proc_info.c
@@ -68,9 +68,9 @@ get_unwind_info(struct UCD_info *ui, unw_addr_space_t as, unw_word_t ip)
 
   /* Here, SEGBASE is the starting-address of the (mmap'ped) segment
      which covers the IP we're looking for.  */
-  if (dwarf_find_unwind_table(&ui->edi, as, phdr->backing_filename, segbase, mapoff, ip) < 0)
+  if (unwi_find_unwind_table(&ui->edi, as, phdr->backing_filename, segbase, mapoff, ip) < 0)
     {
-      Debug(1, "%s returns error: dwarf_find_unwind_table failed\n", __func__);
+      Debug(1, "%s returns error: unwi_find_unwind_table failed\n", __func__);
       return -UNW_ENOINFO;
     }
 
diff --git a/src/coredump/_UPT_get_dyn_info_list_addr.c b/src/coredump/_UPT_get_dyn_info_list_addr.c
index f1e6568..e68994a 100644
--- a/src/coredump/_UPT_get_dyn_info_list_addr.c
+++ b/src/coredump/_UPT_get_dyn_info_list_addr.c
@@ -56,7 +56,7 @@ get_list_addr (unw_addr_space_t as, unw_word_t *dil_addr, void *arg,
 
       Debug (16, "checking object %s\n", path);
 
-      di = dwarf_find_unwind_table (&ui->edi, as, path, lo, off);
+      di = unwi_find_unwind_table (&ui->edi, as, path, lo, off);
       if (di)
 	{
 	  res = _Uia64_find_dyn_list (as, di, arg);
diff --git a/src/dwarf/Gfind_unwind_table.c b/src/dwarf/Gfind_unwind_table.c
deleted file mode 100644
index 753e1c6..0000000
--- a/src/dwarf/Gfind_unwind_table.c
+++ /dev/null
@@ -1,348 +0,0 @@
-/* libunwind - a platform-independent unwind library
-   Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <[email protected]>
-
-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 "libunwind_i.h"
-
-#if UNW_TARGET_IA64
-
-#include "elf64.h"
-
-static unw_word_t
-find_gp (struct elf_dyn_info *edi, Elf64_Phdr *pdyn, Elf64_Addr load_base)
-{
-  Elf64_Off soff, str_soff;
-  Elf64_Ehdr *ehdr = edi->ei.image;
-  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.  */
-      Elf64_Dyn *dyn = (Elf64_Dyn *) (pdyn->p_offset + (char *) edi->ei.image);
-      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);
-
-  if (soff + ehdr->e_shnum * ehdr->e_shentsize > edi->ei.size)
-    {
-      Debug (1, "section table outside of image? (%lu > %lu)",
-	     soff + ehdr->e_shnum * ehdr->e_shentsize,
-	     edi->ei.size);
-      goto done;
-    }
-
-  shdr = (Elf64_Shdr *) ((char *) edi->ei.image + soff);
-  str_shdr = (Elf64_Shdr *) ((char *) edi->ei.image + str_soff);
-  strtab = (char *) edi->ei.image + str_shdr->sh_offset;
-  for (i = 0; i < ehdr->e_shnum; ++i)
-    {
-      if (strcmp (strtab + shdr->sh_name, ".opd") == 0
-	  && shdr->sh_size >= 16)
-	{
-	  gp = ((Elf64_Addr *) ((char *) edi->ei.image + shdr->sh_offset))[1];
-	  goto done;
-	}
-      shdr = (Elf64_Shdr *) (((char *) shdr) + ehdr->e_shentsize);
-    }
-
- done:
-  Debug (16, "image at %p, gp = %lx\n", edi->ei.image, gp);
-  return gp;
-}
-
-int
-dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
-			 char *path, unw_word_t segbase, unw_word_t mapoff,
-			 unw_word_t ip)
-{
-  Elf64_Phdr *phdr, *ptxt = NULL, *punw = NULL, *pdyn = NULL;
-  Elf64_Ehdr *ehdr;
-  int i;
-
-  if (!_Uelf64_valid_object (&edi->ei))
-    return -UNW_ENOINFO;
-
-  ehdr = edi->ei.image;
-  phdr = (Elf64_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_offset == mapoff)
-	    ptxt = phdr + i;
-	  break;
-
-	case PT_IA_64_UNWIND:
-	  punw = phdr + i;
-	  break;
-
-	case PT_DYNAMIC:
-	  pdyn = phdr + i;
-	  break;
-
-	default:
-	  break;
-	}
-    }
-  if (!ptxt || !punw)
-    return 0;
-
-  edi->di_cache.start_ip = segbase;
-  edi->di_cache.end_ip = edi->di_cache.start_ip + ptxt->p_memsz;
-  edi->di_cache.gp = find_gp (edi, pdyn, segbase - ptxt->p_vaddr);
-  edi->di_cache.format = UNW_INFO_FORMAT_TABLE;
-  edi->di_cache.u.ti.name_ptr = 0;
-  edi->di_cache.u.ti.segbase = segbase;
-  edi->di_cache.u.ti.table_len = punw->p_memsz / sizeof (unw_word_t);
-  edi->di_cache.u.ti.table_data = (unw_word_t *)
-    ((char *) edi->ei.image + (punw->p_vaddr - ptxt->p_vaddr));
-  return 1;
-}
-
-#elif UNW_TARGET_X86 || UNW_TARGET_X86_64 || UNW_TARGET_HPPA \
-|| UNW_TARGET_PPC32 || UNW_TARGET_PPC64 || UNW_TARGET_ARM
-
-#include "dwarf-eh.h"
-#include "dwarf_i.h"
-
-int
-dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
-			 char *path, unw_word_t segbase, unw_word_t mapoff,
-			 unw_word_t ip)
-{
-  Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
-  unw_word_t addr, eh_frame_start, fde_count, load_base;
-  unw_word_t max_load_addr = 0;
-  unw_word_t start_ip = (unw_word_t) -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) *parm_exidx = NULL;
-#endif
-  int i, ret, found = 0;
-
-  /* XXX: Much of this code is Linux/LSB-specific.  */
-
-  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_offset == mapoff)
-	    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:
-	  peh_hdr = phdr + i;
-	  break;
-
-	case PT_DYNAMIC:
-	  pdyn = phdr + i;
-	  break;
-
-#if UNW_TARGET_ARM
-	case PT_ARM_EXIDX:
-	  parm_exidx = phdr + i;
-	  break;
-#endif
-
-	default:
-	  break;
-	}
-    }
-
-  if (!ptxt)
-    return 0;
-
-  load_base = segbase - ptxt->p_vaddr;
-  start_ip += load_base;
-  end_ip += load_base;
-
-  if (peh_hdr)
-    {
-      if (pdyn)
-	{
-	  /* For dynamicly linked executables and shared libraries,
-	     DT_PLTGOT is the value that data-relative addresses are
-	     relative to for that object.  We call this the "gp".  */
-		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)
-	      {
-		/* Assume that _DYNAMIC is writable and GLIBC has
-		   relocated it (true for x86 at least).  */
-		edi->di_cache.gp = dyn->d_un.d_ptr;
-		break;
-	      }
-	}
-      else
-	/* Otherwise this is a static executable with no _DYNAMIC.  Assume
-	   that data-relative addresses are relative to 0, i.e.,
-	   absolute.  */
-	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 (unw_local_addr_space);
-      addr = (unw_word_t) (hdr + 1);
-
-      /* Fill in a dummy proc_info structure.  We just need to fill in
-	 enough to ensure that dwarf_read_encoded_pointer() can do it's
-	 job.  Since we don't have a procedure-context at this point, all
-	 we have to do is fill in the global-pointer.  */
-      memset (&pi, 0, sizeof (pi));
-      pi.gp = edi->di_cache.gp;
-
-      /* (Optionally) read eh_frame_ptr: */
-      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;
-
-      /* (Optionally) read fde_count: */
-      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
-	  unw_word_t eh_frame_end;
-
-	  /* If there is no search table or it has an unsupported
-	     encoding, fall back on linear search.  */
-	  if (hdr->table_enc == DW_EH_PE_omit)
-	    Debug (4, "EH lacks search table; doing linear search\n");
-	  else
-	    Debug (4, "EH table has encoding 0x%x; doing linear search\n",
-		   hdr->table_enc);
-
-	  eh_frame_end = max_load_addr;	/* XXX can we do better? */
-
-	  if (hdr->fde_count_enc == DW_EH_PE_omit)
-	    fde_count = ~0UL;
-	  if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
-	    abort ();
-
-	  return linear_search (unw_local_addr_space, ip,
-				eh_frame_start, eh_frame_end, fde_count,
-				pi, need_unwind_info, NULL);
-    #endif
-	}
-
-      edi->di_cache.start_ip = start_ip;
-      edi->di_cache.end_ip = end_ip;
-      edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
-      edi->di_cache.u.rti.name_ptr = 0;
-      /* two 32-bit values (ip_offset/fde_offset) per table-entry: */
-      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 - (unw_word_t) edi->ei.image
-					  - peh_hdr->p_offset));
-
-      /* For the binary-search table in the eh_frame_hdr, data-relative
-	 means relative to the start of that section... */
-      edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
-				    + ((unw_word_t) hdr - (unw_word_t) edi->ei.image
-				       - peh_hdr->p_offset));
-      found = 1;
-    }
-
-#if UNW_TARGET_ARM
-  if (parm_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 = (unw_word_t) path;
-      edi->di_arm.u.rti.table_data = load_base + parm_exidx->p_vaddr;
-      edi->di_arm.u.rti.table_len = parm_exidx->p_memsz;
-      found = 1;
-    }
-#endif
-
-#ifdef CONFIG_DEBUG_FRAME
-  /* Try .debug_frame. */
-  found = dwarf_find_debug_frame (found, &edi->edi.di_debug, ip, segbase, path,
-				  start_ip, end_ip);
-#endif
-
-  return found;
-}
-
-#endif /* UNW_TARGET_X86 || UNW_TARGET_X86_64 || UNW_TARGET_HPPA*/
diff --git a/src/dwarf/Lfind_unwind_table.c b/src/dwarf/Lfind_unwind_table.c
deleted file mode 100644
index 68e269f..0000000
--- a/src/dwarf/Lfind_unwind_table.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#define UNW_LOCAL_ONLY
-#include <libunwind.h>
-#if defined(UNW_LOCAL_ONLY) && !defined(UNW_REMOTE_ONLY)
-#include "Gfind_unwind_table.c"
-#endif
diff --git a/src/mi/Gfind_unwind_table.c b/src/mi/Gfind_unwind_table.c
new file mode 100644
index 0000000..bf4edd1
--- /dev/null
+++ b/src/mi/Gfind_unwind_table.c
@@ -0,0 +1,348 @@
+/* libunwind - a platform-independent unwind library
+   Copyright (C) 2003-2004 Hewlett-Packard Co
+	Contributed by David Mosberger-Tang <[email protected]>
+
+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 "libunwind_i.h"
+
+#if UNW_TARGET_IA64
+
+#include "elf64.h"
+
+static unw_word_t
+find_gp (struct elf_dyn_info *edi, Elf64_Phdr *pdyn, Elf64_Addr load_base)
+{
+  Elf64_Off soff, str_soff;
+  Elf64_Ehdr *ehdr = edi->ei.image;
+  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.  */
+      Elf64_Dyn *dyn = (Elf64_Dyn *) (pdyn->p_offset + (char *) edi->ei.image);
+      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);
+
+  if (soff + ehdr->e_shnum * ehdr->e_shentsize > edi->ei.size)
+    {
+      Debug (1, "section table outside of image? (%lu > %lu)",
+	     soff + ehdr->e_shnum * ehdr->e_shentsize,
+	     edi->ei.size);
+      goto done;
+    }
+
+  shdr = (Elf64_Shdr *) ((char *) edi->ei.image + soff);
+  str_shdr = (Elf64_Shdr *) ((char *) edi->ei.image + str_soff);
+  strtab = (char *) edi->ei.image + str_shdr->sh_offset;
+  for (i = 0; i < ehdr->e_shnum; ++i)
+    {
+      if (strcmp (strtab + shdr->sh_name, ".opd") == 0
+	  && shdr->sh_size >= 16)
+	{
+	  gp = ((Elf64_Addr *) ((char *) edi->ei.image + shdr->sh_offset))[1];
+	  goto done;
+	}
+      shdr = (Elf64_Shdr *) (((char *) shdr) + ehdr->e_shentsize);
+    }
+
+ done:
+  Debug (16, "image at %p, gp = %lx\n", edi->ei.image, gp);
+  return gp;
+}
+
+int
+unwi_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
+			 char *path, unw_word_t segbase, unw_word_t mapoff,
+			 unw_word_t ip)
+{
+  Elf64_Phdr *phdr, *ptxt = NULL, *punw = NULL, *pdyn = NULL;
+  Elf64_Ehdr *ehdr;
+  int i;
+
+  if (!_Uelf64_valid_object (&edi->ei))
+    return -UNW_ENOINFO;
+
+  ehdr = edi->ei.image;
+  phdr = (Elf64_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_offset == mapoff)
+	    ptxt = phdr + i;
+	  break;
+
+	case PT_IA_64_UNWIND:
+	  punw = phdr + i;
+	  break;
+
+	case PT_DYNAMIC:
+	  pdyn = phdr + i;
+	  break;
+
+	default:
+	  break;
+	}
+    }
+  if (!ptxt || !punw)
+    return 0;
+
+  edi->di_cache.start_ip = segbase;
+  edi->di_cache.end_ip = edi->di_cache.start_ip + ptxt->p_memsz;
+  edi->di_cache.gp = find_gp (edi, pdyn, segbase - ptxt->p_vaddr);
+  edi->di_cache.format = UNW_INFO_FORMAT_TABLE;
+  edi->di_cache.u.ti.name_ptr = 0;
+  edi->di_cache.u.ti.segbase = segbase;
+  edi->di_cache.u.ti.table_len = punw->p_memsz / sizeof (unw_word_t);
+  edi->di_cache.u.ti.table_data = (unw_word_t *)
+    ((char *) edi->ei.image + (punw->p_vaddr - ptxt->p_vaddr));
+  return 1;
+}
+
+#elif UNW_TARGET_X86 || UNW_TARGET_X86_64 || UNW_TARGET_HPPA \
+|| UNW_TARGET_PPC32 || UNW_TARGET_PPC64 || UNW_TARGET_ARM
+
+#include "dwarf-eh.h"
+#include "dwarf_i.h"
+
+int
+unwi_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
+			 char *path, unw_word_t segbase, unw_word_t mapoff,
+			 unw_word_t ip)
+{
+  Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
+  unw_word_t addr, eh_frame_start, fde_count, load_base;
+  unw_word_t max_load_addr = 0;
+  unw_word_t start_ip = (unw_word_t) -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) *parm_exidx = NULL;
+#endif
+  int i, ret, found = 0;
+
+  /* XXX: Much of this code is Linux/LSB-specific.  */
+
+  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_offset == mapoff)
+	    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:
+	  peh_hdr = phdr + i;
+	  break;
+
+	case PT_DYNAMIC:
+	  pdyn = phdr + i;
+	  break;
+
+#if UNW_TARGET_ARM
+	case PT_ARM_EXIDX:
+	  parm_exidx = phdr + i;
+	  break;
+#endif
+
+	default:
+	  break;
+	}
+    }
+
+  if (!ptxt)
+    return 0;
+
+  load_base = segbase - ptxt->p_vaddr;
+  start_ip += load_base;
+  end_ip += load_base;
+
+  if (peh_hdr)
+    {
+      if (pdyn)
+	{
+	  /* For dynamicly linked executables and shared libraries,
+	     DT_PLTGOT is the value that data-relative addresses are
+	     relative to for that object.  We call this the "gp".  */
+		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)
+	      {
+		/* Assume that _DYNAMIC is writable and GLIBC has
+		   relocated it (true for x86 at least).  */
+		edi->di_cache.gp = dyn->d_un.d_ptr;
+		break;
+	      }
+	}
+      else
+	/* Otherwise this is a static executable with no _DYNAMIC.  Assume
+	   that data-relative addresses are relative to 0, i.e.,
+	   absolute.  */
+	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 (unw_local_addr_space);
+      addr = (unw_word_t) (hdr + 1);
+
+      /* Fill in a dummy proc_info structure.  We just need to fill in
+	 enough to ensure that dwarf_read_encoded_pointer() can do it's
+	 job.  Since we don't have a procedure-context at this point, all
+	 we have to do is fill in the global-pointer.  */
+      memset (&pi, 0, sizeof (pi));
+      pi.gp = edi->di_cache.gp;
+
+      /* (Optionally) read eh_frame_ptr: */
+      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;
+
+      /* (Optionally) read fde_count: */
+      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
+	  unw_word_t eh_frame_end;
+
+	  /* If there is no search table or it has an unsupported
+	     encoding, fall back on linear search.  */
+	  if (hdr->table_enc == DW_EH_PE_omit)
+	    Debug (4, "EH lacks search table; doing linear search\n");
+	  else
+	    Debug (4, "EH table has encoding 0x%x; doing linear search\n",
+		   hdr->table_enc);
+
+	  eh_frame_end = max_load_addr;	/* XXX can we do better? */
+
+	  if (hdr->fde_count_enc == DW_EH_PE_omit)
+	    fde_count = ~0UL;
+	  if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
+	    abort ();
+
+	  return linear_search (unw_local_addr_space, ip,
+				eh_frame_start, eh_frame_end, fde_count,
+				pi, need_unwind_info, NULL);
+    #endif
+	}
+
+      edi->di_cache.start_ip = start_ip;
+      edi->di_cache.end_ip = end_ip;
+      edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
+      edi->di_cache.u.rti.name_ptr = 0;
+      /* two 32-bit values (ip_offset/fde_offset) per table-entry: */
+      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 - (unw_word_t) edi->ei.image
+					  - peh_hdr->p_offset));
+
+      /* For the binary-search table in the eh_frame_hdr, data-relative
+	 means relative to the start of that section... */
+      edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
+				    + ((unw_word_t) hdr - (unw_word_t) edi->ei.image
+				       - peh_hdr->p_offset));
+      found = 1;
+    }
+
+#if UNW_TARGET_ARM
+  if (parm_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 = (unw_word_t) path;
+      edi->di_arm.u.rti.table_data = load_base + parm_exidx->p_vaddr;
+      edi->di_arm.u.rti.table_len = parm_exidx->p_memsz;
+      found = 1;
+    }
+#endif
+
+#ifdef CONFIG_DEBUG_FRAME
+  /* Try .debug_frame. */
+  found = dwarf_find_debug_frame (found, &edi->edi.di_debug, ip, segbase, path,
+				  start_ip, end_ip);
+#endif
+
+  return found;
+}
+
+#endif /* UNW_TARGET_X86 || UNW_TARGET_X86_64 || UNW_TARGET_HPPA*/
diff --git a/src/mi/Lfind_unwind_table.c b/src/mi/Lfind_unwind_table.c
new file mode 100644
index 0000000..68e269f
--- /dev/null
+++ b/src/mi/Lfind_unwind_table.c
@@ -0,0 +1,5 @@
+#define UNW_LOCAL_ONLY
+#include <libunwind.h>
+#if defined(UNW_LOCAL_ONLY) && !defined(UNW_REMOTE_ONLY)
+#include "Gfind_unwind_table.c"
+#endif
diff --git a/src/ptrace/_UPT_find_proc_info.c b/src/ptrace/_UPT_find_proc_info.c
index 93b3f22..a999766 100644
--- a/src/ptrace/_UPT_find_proc_info.c
+++ b/src/ptrace/_UPT_find_proc_info.c
@@ -65,7 +65,7 @@ get_unwind_info (struct elf_dyn_info *edi, pid_t pid, unw_addr_space_t as, unw_w
 
   /* Here, SEGBASE is the starting-address of the (mmap'ped) segment
      which covers the IP we're looking for.  */
-  if (dwarf_find_unwind_table (edi, as, path, segbase, mapoff, ip) < 0)
+  if (unwi_find_unwind_table (edi, as, path, segbase, mapoff, ip) < 0)
     return -UNW_ENOINFO;
 
   /* This can happen in corner cases where dynamically generated
diff --git a/src/ptrace/_UPT_get_dyn_info_list_addr.c b/src/ptrace/_UPT_get_dyn_info_list_addr.c
index 60286ac..9ceb5ce 100644
--- a/src/ptrace/_UPT_get_dyn_info_list_addr.c
+++ b/src/ptrace/_UPT_get_dyn_info_list_addr.c
@@ -54,7 +54,7 @@ get_list_addr (unw_addr_space_t as, unw_word_t *dil_addr, void *arg,
 
       Debug (16, "checking object %s\n", path);
 
-      if (dwarf_find_unwind_table (&ui->edi, as, path, lo, off, 0) > 0)
+      if (unwi_find_unwind_table (&ui->edi, as, path, lo, off, 0) > 0)
 	{
 	  res = _Uia64_find_dyn_list (as, &ui->edi.di_cache, arg);
 	  if (res && count++ == 0)
_______________________________________________
Libunwind-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/libunwind-devel

Reply via email to