Gfde.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
  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 "dwarf_i.h"
  22. static inline int
  23. is_cie_id (unw_word_t val, int is_debug_frame)
  24. {
  25. /* The CIE ID is normally 0xffffffff (for 32-bit ELF) or
  26. 0xffffffffffffffff (for 64-bit ELF). However, .eh_frame
  27. uses 0. */
  28. if (is_debug_frame)
  29. return (val == - (uint32_t) 1 || val == - (uint64_t) 1);
  30. else
  31. return (val == 0);
  32. }
  33. /* Note: we don't need to keep track of more than the first four
  34. characters of the augmentation string, because we (a) ignore any
  35. augmentation string contents once we find an unrecognized character
  36. and (b) those characters that we do recognize, can't be
  37. repeated. */
  38. static inline int
  39. parse_cie (unw_addr_space_t as, unw_accessors_t *a, unw_word_t addr,
  40. const unw_proc_info_t *pi, struct dwarf_cie_info *dci,
  41. unw_word_t base, void *arg)
  42. {
  43. uint8_t version, ch, augstr[5], fde_encoding, handler_encoding;
  44. unw_word_t len, cie_end_addr, aug_size;
  45. uint32_t u32val;
  46. uint64_t u64val;
  47. size_t i;
  48. int ret;
  49. # define STR2(x) #x
  50. # define STR(x) STR2(x)
  51. /* Pick appropriate default for FDE-encoding. DWARF spec says
  52. start-IP (initial_location) and the code-size (address_range) are
  53. "address-unit sized constants". The `R' augmentation can be used
  54. to override this, but by default, we pick an address-sized unit
  55. for fde_encoding. */
  56. switch (dwarf_addr_size (as))
  57. {
  58. case 4: fde_encoding = DW_EH_PE_udata4; break;
  59. case 8: fde_encoding = DW_EH_PE_udata8; break;
  60. default: fde_encoding = DW_EH_PE_omit; break;
  61. }
  62. dci->lsda_encoding = DW_EH_PE_omit;
  63. dci->handler = 0;
  64. if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
  65. return ret;
  66. if (u32val != 0xffffffff)
  67. {
  68. /* the CIE is in the 32-bit DWARF format */
  69. uint32_t cie_id;
  70. /* DWARF says CIE id should be 0xffffffff, but in .eh_frame, it's 0 */
  71. const uint32_t expected_id = (base) ? 0xffffffff : 0;
  72. len = u32val;
  73. cie_end_addr = addr + len;
  74. if ((ret = dwarf_readu32 (as, a, &addr, &cie_id, arg)) < 0)
  75. return ret;
  76. if (cie_id != expected_id)
  77. {
  78. Debug (1, "Unexpected CIE id %x\n", cie_id);
  79. return -UNW_EINVAL;
  80. }
  81. }
  82. else
  83. {
  84. /* the CIE is in the 64-bit DWARF format */
  85. uint64_t cie_id;
  86. /* DWARF says CIE id should be 0xffffffffffffffff, but in
  87. .eh_frame, it's 0 */
  88. const uint64_t expected_id = (base) ? 0xffffffffffffffffull : 0;
  89. if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
  90. return ret;
  91. len = u64val;
  92. cie_end_addr = addr + len;
  93. if ((ret = dwarf_readu64 (as, a, &addr, &cie_id, arg)) < 0)
  94. return ret;
  95. if (cie_id != expected_id)
  96. {
  97. Debug (1, "Unexpected CIE id %llx\n", (long long) cie_id);
  98. return -UNW_EINVAL;
  99. }
  100. }
  101. dci->cie_instr_end = cie_end_addr;
  102. if ((ret = dwarf_readu8 (as, a, &addr, &version, arg)) < 0)
  103. return ret;
  104. if (version != 1 && version != DWARF_CIE_VERSION)
  105. {
  106. Debug (1, "Got CIE version %u, expected version 1 or "
  107. STR (DWARF_CIE_VERSION) "\n", version);
  108. return -UNW_EBADVERSION;
  109. }
  110. /* read and parse the augmentation string: */
  111. memset (augstr, 0, sizeof (augstr));
  112. for (i = 0;;)
  113. {
  114. if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
  115. return ret;
  116. if (!ch)
  117. break; /* end of augmentation string */
  118. if (i < sizeof (augstr) - 1)
  119. augstr[i++] = ch;
  120. }
  121. if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->code_align, arg)) < 0
  122. || (ret = dwarf_read_sleb128 (as, a, &addr, &dci->data_align, arg)) < 0)
  123. return ret;
  124. /* Read the return-address column either as a u8 or as a uleb128. */
  125. if (version == 1)
  126. {
  127. if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
  128. return ret;
  129. dci->ret_addr_column = ch;
  130. }
  131. else if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->ret_addr_column,
  132. arg)) < 0)
  133. return ret;
  134. i = 0;
  135. if (augstr[0] == 'z')
  136. {
  137. dci->sized_augmentation = 1;
  138. if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
  139. return ret;
  140. i++;
  141. }
  142. for (; i < sizeof (augstr) && augstr[i]; ++i)
  143. switch (augstr[i])
  144. {
  145. case 'L':
  146. /* read the LSDA pointer-encoding format. */
  147. if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
  148. return ret;
  149. dci->lsda_encoding = ch;
  150. break;
  151. case 'R':
  152. /* read the FDE pointer-encoding format. */
  153. if ((ret = dwarf_readu8 (as, a, &addr, &fde_encoding, arg)) < 0)
  154. return ret;
  155. break;
  156. case 'P':
  157. /* read the personality-routine pointer-encoding format. */
  158. if ((ret = dwarf_readu8 (as, a, &addr, &handler_encoding, arg)) < 0)
  159. return ret;
  160. if ((ret = dwarf_read_encoded_pointer (as, a, &addr, handler_encoding,
  161. pi, &dci->handler, arg)) < 0)
  162. return ret;
  163. break;
  164. case 'S':
  165. /* This is a signal frame. */
  166. dci->signal_frame = 1;
  167. /* Temporarily set it to one so dwarf_parse_fde() knows that
  168. it should fetch the actual ABI/TAG pair from the FDE. */
  169. dci->have_abi_marker = 1;
  170. break;
  171. default:
  172. Debug (1, "Unexpected augmentation string `%s'\n", augstr);
  173. if (dci->sized_augmentation)
  174. /* If we have the size of the augmentation body, we can skip
  175. over the parts that we don't understand, so we're OK. */
  176. goto done;
  177. else
  178. return -UNW_EINVAL;
  179. }
  180. done:
  181. dci->fde_encoding = fde_encoding;
  182. dci->cie_instr_start = addr;
  183. Debug (15, "CIE parsed OK, augmentation = \"%s\", handler=0x%lx\n",
  184. augstr, (long) dci->handler);
  185. return 0;
  186. }
  187. /* Extract proc-info from the FDE starting at adress ADDR.
  188. Pass BASE as zero for eh_frame behaviour, or a pointer to
  189. debug_frame base for debug_frame behaviour. */
  190. HIDDEN int
  191. dwarf_extract_proc_info_from_fde (unw_addr_space_t as, unw_accessors_t *a,
  192. unw_word_t *addrp, unw_proc_info_t *pi,
  193. int need_unwind_info, unw_word_t base,
  194. void *arg)
  195. {
  196. unw_word_t fde_end_addr, cie_addr, cie_offset_addr, aug_end_addr = 0;
  197. unw_word_t start_ip, ip_range, aug_size, addr = *addrp;
  198. int ret, ip_range_encoding;
  199. struct dwarf_cie_info dci;
  200. uint64_t u64val;
  201. uint32_t u32val;
  202. Debug (12, "FDE @ 0x%lx\n", (long) addr);
  203. memset (&dci, 0, sizeof (dci));
  204. if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
  205. return ret;
  206. if (u32val != 0xffffffff)
  207. {
  208. int32_t cie_offset;
  209. /* In some configurations, an FDE with a 0 length indicates the
  210. end of the FDE-table. */
  211. if (u32val == 0)
  212. return -UNW_ENOINFO;
  213. /* the FDE is in the 32-bit DWARF format */
  214. *addrp = fde_end_addr = addr + u32val;
  215. cie_offset_addr = addr;
  216. if ((ret = dwarf_reads32 (as, a, &addr, &cie_offset, arg)) < 0)
  217. return ret;
  218. if (is_cie_id (cie_offset, base != 0))
  219. /* ignore CIEs (happens during linear searches) */
  220. return 0;
  221. if (base != 0)
  222. cie_addr = base + cie_offset;
  223. else
  224. /* DWARF says that the CIE_pointer in the FDE is a
  225. .debug_frame-relative offset, but the GCC-generated .eh_frame
  226. sections instead store a "pcrelative" offset, which is just
  227. as fine as it's self-contained. */
  228. cie_addr = cie_offset_addr - cie_offset;
  229. }
  230. else
  231. {
  232. int64_t cie_offset;
  233. /* the FDE is in the 64-bit DWARF format */
  234. if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
  235. return ret;
  236. *addrp = fde_end_addr = addr + u64val;
  237. cie_offset_addr = addr;
  238. if ((ret = dwarf_reads64 (as, a, &addr, &cie_offset, arg)) < 0)
  239. return ret;
  240. if (is_cie_id (cie_offset, base != 0))
  241. /* ignore CIEs (happens during linear searches) */
  242. return 0;
  243. if (base != 0)
  244. cie_addr = base + cie_offset;
  245. else
  246. /* DWARF says that the CIE_pointer in the FDE is a
  247. .debug_frame-relative offset, but the GCC-generated .eh_frame
  248. sections instead store a "pcrelative" offset, which is just
  249. as fine as it's self-contained. */
  250. cie_addr = (unw_word_t) ((uint64_t) cie_offset_addr - cie_offset);
  251. }
  252. Debug (15, "looking for CIE at address %lx\n", (long) cie_addr);
  253. if ((ret = parse_cie (as, a, cie_addr, pi, &dci, base, arg)) < 0)
  254. return ret;
  255. /* IP-range has same encoding as FDE pointers, except that it's
  256. always an absolute value: */
  257. ip_range_encoding = dci.fde_encoding & DW_EH_PE_FORMAT_MASK;
  258. if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.fde_encoding,
  259. pi, &start_ip, arg)) < 0
  260. || (ret = dwarf_read_encoded_pointer (as, a, &addr, ip_range_encoding,
  261. pi, &ip_range, arg)) < 0)
  262. return ret;
  263. pi->start_ip = start_ip;
  264. pi->end_ip = start_ip + ip_range;
  265. pi->handler = dci.handler;
  266. if (dci.sized_augmentation)
  267. {
  268. if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
  269. return ret;
  270. aug_end_addr = addr + aug_size;
  271. }
  272. if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.lsda_encoding,
  273. pi, &pi->lsda, arg)) < 0)
  274. return ret;
  275. Debug (15, "FDE covers IP 0x%lx-0x%lx, LSDA=0x%lx\n",
  276. (long) pi->start_ip, (long) pi->end_ip, (long) pi->lsda);
  277. if (need_unwind_info)
  278. {
  279. pi->format = UNW_INFO_FORMAT_TABLE;
  280. pi->unwind_info_size = sizeof (dci);
  281. pi->unwind_info = mempool_alloc (&dwarf_cie_info_pool);
  282. if (!pi->unwind_info)
  283. return -UNW_ENOMEM;
  284. if (dci.have_abi_marker)
  285. {
  286. if ((ret = dwarf_readu16 (as, a, &addr, &dci.abi, arg)) < 0
  287. || (ret = dwarf_readu16 (as, a, &addr, &dci.tag, arg)) < 0)
  288. return ret;
  289. Debug (13, "Found ABI marker = (abi=%u, tag=%u)\n",
  290. dci.abi, dci.tag);
  291. }
  292. if (dci.sized_augmentation)
  293. dci.fde_instr_start = aug_end_addr;
  294. else
  295. dci.fde_instr_start = addr;
  296. dci.fde_instr_end = fde_end_addr;
  297. memcpy (pi->unwind_info, &dci, sizeof (dci));
  298. }
  299. return 0;
  300. }