Gfind_unwind_table.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003-2004 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include <elf.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/mman.h>
  26. #include "libunwind_i.h"
  27. #include "dwarf-eh.h"
  28. #include "dwarf_i.h"
  29. int
  30. dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
  31. char *path, unw_word_t segbase, unw_word_t mapoff,
  32. unw_word_t ip)
  33. {
  34. Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
  35. unw_word_t addr, eh_frame_start, fde_count, load_base;
  36. unw_word_t max_load_addr = 0;
  37. unw_word_t start_ip = (unw_word_t) -1;
  38. unw_word_t end_ip = 0;
  39. struct dwarf_eh_frame_hdr *hdr;
  40. unw_proc_info_t pi;
  41. unw_accessors_t *a;
  42. Elf_W(Ehdr) *ehdr;
  43. #if UNW_TARGET_ARM
  44. const Elf_W(Phdr) *parm_exidx = NULL;
  45. #endif
  46. int i, ret, found = 0;
  47. /* XXX: Much of this code is Linux/LSB-specific. */
  48. if (!elf_w(valid_object) (&edi->ei))
  49. return -UNW_ENOINFO;
  50. ehdr = edi->ei.image;
  51. phdr = (Elf_W(Phdr) *) ((char *) edi->ei.image + ehdr->e_phoff);
  52. for (i = 0; i < ehdr->e_phnum; ++i)
  53. {
  54. switch (phdr[i].p_type)
  55. {
  56. case PT_LOAD:
  57. if (phdr[i].p_vaddr < start_ip)
  58. start_ip = phdr[i].p_vaddr;
  59. if (phdr[i].p_vaddr + phdr[i].p_memsz > end_ip)
  60. end_ip = phdr[i].p_vaddr + phdr[i].p_memsz;
  61. if (phdr[i].p_offset == mapoff)
  62. ptxt = phdr + i;
  63. if ((uintptr_t) edi->ei.image + phdr->p_filesz > max_load_addr)
  64. max_load_addr = (uintptr_t) edi->ei.image + phdr->p_filesz;
  65. break;
  66. case PT_GNU_EH_FRAME:
  67. peh_hdr = phdr + i;
  68. break;
  69. case PT_DYNAMIC:
  70. pdyn = phdr + i;
  71. break;
  72. #if UNW_TARGET_ARM
  73. case PT_ARM_EXIDX:
  74. parm_exidx = phdr + i;
  75. break;
  76. #endif
  77. default:
  78. break;
  79. }
  80. }
  81. if (!ptxt)
  82. return 0;
  83. load_base = segbase - ptxt->p_vaddr;
  84. start_ip += load_base;
  85. end_ip += load_base;
  86. if (peh_hdr)
  87. {
  88. if (pdyn)
  89. {
  90. /* For dynamicly linked executables and shared libraries,
  91. DT_PLTGOT is the value that data-relative addresses are
  92. relative to for that object. We call this the "gp". */
  93. Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(pdyn->p_offset
  94. + (char *) edi->ei.image);
  95. for (; dyn->d_tag != DT_NULL; ++dyn)
  96. if (dyn->d_tag == DT_PLTGOT)
  97. {
  98. /* Assume that _DYNAMIC is writable and GLIBC has
  99. relocated it (true for x86 at least). */
  100. edi->di_cache.gp = dyn->d_un.d_ptr;
  101. break;
  102. }
  103. }
  104. else
  105. /* Otherwise this is a static executable with no _DYNAMIC. Assume
  106. that data-relative addresses are relative to 0, i.e.,
  107. absolute. */
  108. edi->di_cache.gp = 0;
  109. hdr = (struct dwarf_eh_frame_hdr *) (peh_hdr->p_offset
  110. + (char *) edi->ei.image);
  111. if (hdr->version != DW_EH_VERSION)
  112. {
  113. Debug (1, "table `%s' has unexpected version %d\n",
  114. path, hdr->version);
  115. return -UNW_ENOINFO;
  116. }
  117. a = unw_get_accessors (unw_local_addr_space);
  118. addr = (unw_word_t) (hdr + 1);
  119. /* Fill in a dummy proc_info structure. We just need to fill in
  120. enough to ensure that dwarf_read_encoded_pointer() can do it's
  121. job. Since we don't have a procedure-context at this point, all
  122. we have to do is fill in the global-pointer. */
  123. memset (&pi, 0, sizeof (pi));
  124. pi.gp = edi->di_cache.gp;
  125. /* (Optionally) read eh_frame_ptr: */
  126. if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
  127. &addr, hdr->eh_frame_ptr_enc, &pi,
  128. &eh_frame_start, NULL)) < 0)
  129. return -UNW_ENOINFO;
  130. /* (Optionally) read fde_count: */
  131. if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
  132. &addr, hdr->fde_count_enc, &pi,
  133. &fde_count, NULL)) < 0)
  134. return -UNW_ENOINFO;
  135. if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
  136. {
  137. #if 1
  138. abort ();
  139. #else
  140. unw_word_t eh_frame_end;
  141. /* If there is no search table or it has an unsupported
  142. encoding, fall back on linear search. */
  143. if (hdr->table_enc == DW_EH_PE_omit)
  144. Debug (4, "EH lacks search table; doing linear search\n");
  145. else
  146. Debug (4, "EH table has encoding 0x%x; doing linear search\n",
  147. hdr->table_enc);
  148. eh_frame_end = max_load_addr; /* XXX can we do better? */
  149. if (hdr->fde_count_enc == DW_EH_PE_omit)
  150. fde_count = ~0UL;
  151. if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
  152. abort ();
  153. return linear_search (unw_local_addr_space, ip,
  154. eh_frame_start, eh_frame_end, fde_count,
  155. pi, need_unwind_info, NULL);
  156. #endif
  157. }
  158. edi->di_cache.start_ip = start_ip;
  159. edi->di_cache.end_ip = end_ip;
  160. edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
  161. edi->di_cache.u.rti.name_ptr = 0;
  162. /* two 32-bit values (ip_offset/fde_offset) per table-entry: */
  163. edi->di_cache.u.rti.table_len = (fde_count * 8) / sizeof (unw_word_t);
  164. edi->di_cache.u.rti.table_data = ((load_base + peh_hdr->p_vaddr)
  165. + (addr - (unw_word_t) edi->ei.image
  166. - peh_hdr->p_offset));
  167. /* For the binary-search table in the eh_frame_hdr, data-relative
  168. means relative to the start of that section... */
  169. edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
  170. + ((unw_word_t) hdr - (unw_word_t) edi->ei.image
  171. - peh_hdr->p_offset));
  172. found = 1;
  173. }
  174. #if UNW_TARGET_ARM
  175. if (parm_exidx)
  176. {
  177. edi->di_arm.format = UNW_INFO_FORMAT_ARM_EXIDX;
  178. edi->di_arm.start_ip = start_ip;
  179. edi->di_arm.end_ip = end_ip;
  180. edi->di_arm.u.rti.name_ptr = (unw_word_t) path;
  181. edi->di_arm.u.rti.table_data = load_base + parm_exidx->p_vaddr;
  182. edi->di_arm.u.rti.table_len = parm_exidx->p_memsz;
  183. found = 1;
  184. }
  185. #endif
  186. #ifdef CONFIG_DEBUG_FRAME
  187. /* Try .debug_frame. */
  188. found = dwarf_find_debug_frame (found, &edi->edi.di_debug, ip, segbase, path,
  189. start_ip, end_ip);
  190. #endif
  191. return found;
  192. }