unwind-pe.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Exception handling and frame unwind runtime interface routines.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* @@@ Really this should be out of line, but this also causes link
  20. compatibility problems with the base ABI. This is slightly better
  21. than duplicating code, however. */
  22. #ifndef GCC_UNWIND_PE_H
  23. #define GCC_UNWIND_PE_H
  24. /* If using C++, references to abort have to be qualified with std::. */
  25. #if __cplusplus
  26. #define __gxx_abort std::abort
  27. #else
  28. #define __gxx_abort abort
  29. #endif
  30. /* Pointer encodings, from dwarf2.h. */
  31. #define DW_EH_PE_absptr 0x00
  32. #define DW_EH_PE_omit 0xff
  33. #define DW_EH_PE_uleb128 0x01
  34. #define DW_EH_PE_udata2 0x02
  35. #define DW_EH_PE_udata4 0x03
  36. #define DW_EH_PE_udata8 0x04
  37. #define DW_EH_PE_sleb128 0x09
  38. #define DW_EH_PE_sdata2 0x0A
  39. #define DW_EH_PE_sdata4 0x0B
  40. #define DW_EH_PE_sdata8 0x0C
  41. #define DW_EH_PE_signed 0x08
  42. #define DW_EH_PE_pcrel 0x10
  43. #define DW_EH_PE_textrel 0x20
  44. #define DW_EH_PE_datarel 0x30
  45. #define DW_EH_PE_funcrel 0x40
  46. #define DW_EH_PE_aligned 0x50
  47. #define DW_EH_PE_indirect 0x80
  48. #ifndef NO_SIZE_OF_ENCODED_VALUE
  49. /* Given an encoding, return the number of bytes the format occupies.
  50. This is only defined for fixed-size encodings, and so does not
  51. include leb128. */
  52. static unsigned int
  53. size_of_encoded_value (unsigned char encoding) __attribute__ ((unused));
  54. static unsigned int
  55. size_of_encoded_value (unsigned char encoding)
  56. {
  57. if (encoding == DW_EH_PE_omit)
  58. return 0;
  59. switch (encoding & 0x07)
  60. {
  61. case DW_EH_PE_absptr:
  62. return sizeof (void *);
  63. case DW_EH_PE_udata2:
  64. return 2;
  65. case DW_EH_PE_udata4:
  66. return 4;
  67. case DW_EH_PE_udata8:
  68. return 8;
  69. }
  70. __gxx_abort ();
  71. }
  72. #endif
  73. #ifndef NO_BASE_OF_ENCODED_VALUE
  74. /* Given an encoding and an _Unwind_Context, return the base to which
  75. the encoding is relative. This base may then be passed to
  76. read_encoded_value_with_base for use when the _Unwind_Context is
  77. not available. */
  78. static _Unwind_Ptr
  79. base_of_encoded_value (unsigned char encoding, struct _Unwind_Context *context)
  80. {
  81. if (encoding == DW_EH_PE_omit)
  82. return 0;
  83. switch (encoding & 0x70)
  84. {
  85. case DW_EH_PE_absptr:
  86. case DW_EH_PE_pcrel:
  87. case DW_EH_PE_aligned:
  88. return 0;
  89. case DW_EH_PE_textrel:
  90. return _Unwind_GetTextRelBase (context);
  91. case DW_EH_PE_datarel:
  92. return _Unwind_GetDataRelBase (context);
  93. case DW_EH_PE_funcrel:
  94. return _Unwind_GetRegionStart (context);
  95. }
  96. __gxx_abort ();
  97. }
  98. #endif
  99. /* Read an unsigned leb128 value from P, store the value in VAL, return
  100. P incremented past the value. We assume that a word is large enough to
  101. hold any value so encoded; if it is smaller than a pointer on some target,
  102. pointers should not be leb128 encoded on that target. */
  103. static const unsigned char *
  104. read_uleb128 (const unsigned char *p, _uleb128_t *val)
  105. {
  106. unsigned int shift = 0;
  107. unsigned char byte;
  108. _uleb128_t result;
  109. result = 0;
  110. do
  111. {
  112. byte = *p++;
  113. result |= ((_uleb128_t)byte & 0x7f) << shift;
  114. shift += 7;
  115. }
  116. while (byte & 0x80);
  117. *val = result;
  118. return p;
  119. }
  120. /* Similar, but read a signed leb128 value. */
  121. static const unsigned char *
  122. read_sleb128 (const unsigned char *p, _sleb128_t *val)
  123. {
  124. unsigned int shift = 0;
  125. unsigned char byte;
  126. _uleb128_t result;
  127. result = 0;
  128. do
  129. {
  130. byte = *p++;
  131. result |= ((_uleb128_t)byte & 0x7f) << shift;
  132. shift += 7;
  133. }
  134. while (byte & 0x80);
  135. /* Sign-extend a negative value. */
  136. if (shift < 8 * sizeof(result) && (byte & 0x40) != 0)
  137. result |= -(((_uleb128_t)1L) << shift);
  138. *val = (_sleb128_t) result;
  139. return p;
  140. }
  141. /* Load an encoded value from memory at P. The value is returned in VAL;
  142. The function returns P incremented past the value. BASE is as given
  143. by base_of_encoded_value for this encoding in the appropriate context. */
  144. #pragma GCC diagnostic push
  145. #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
  146. static const unsigned char *
  147. read_encoded_value_with_base (unsigned char encoding, _Unwind_Ptr base,
  148. const unsigned char *p, _Unwind_Ptr *val)
  149. {
  150. union unaligned
  151. {
  152. void *ptr;
  153. unsigned u2 __attribute__ ((mode (HI)));
  154. unsigned u4 __attribute__ ((mode (SI)));
  155. unsigned u8 __attribute__ ((mode (DI)));
  156. signed s2 __attribute__ ((mode (HI)));
  157. signed s4 __attribute__ ((mode (SI)));
  158. signed s8 __attribute__ ((mode (DI)));
  159. } __attribute__((__packed__));
  160. const union unaligned *u = (const union unaligned *) p;
  161. _Unwind_Internal_Ptr result;
  162. if (encoding == DW_EH_PE_aligned)
  163. {
  164. _Unwind_Internal_Ptr a = (_Unwind_Internal_Ptr) p;
  165. a = (a + sizeof (void *) - 1) & - sizeof(void *);
  166. result = *(_Unwind_Internal_Ptr *) a;
  167. p = (const unsigned char *) (_Unwind_Internal_Ptr) (a + sizeof (void *));
  168. }
  169. else
  170. {
  171. switch (encoding & 0x0f)
  172. {
  173. case DW_EH_PE_absptr:
  174. result = (_Unwind_Internal_Ptr) u->ptr;
  175. p += sizeof (void *);
  176. break;
  177. case DW_EH_PE_uleb128:
  178. {
  179. _uleb128_t tmp;
  180. p = read_uleb128 (p, &tmp);
  181. result = (_Unwind_Internal_Ptr) tmp;
  182. }
  183. break;
  184. case DW_EH_PE_sleb128:
  185. {
  186. _sleb128_t tmp;
  187. p = read_sleb128 (p, &tmp);
  188. result = (_Unwind_Internal_Ptr) tmp;
  189. }
  190. break;
  191. case DW_EH_PE_udata2:
  192. result = u->u2;
  193. p += 2;
  194. break;
  195. case DW_EH_PE_udata4:
  196. result = u->u4;
  197. p += 4;
  198. break;
  199. case DW_EH_PE_udata8:
  200. result = u->u8;
  201. p += 8;
  202. break;
  203. case DW_EH_PE_sdata2:
  204. result = u->s2;
  205. p += 2;
  206. break;
  207. case DW_EH_PE_sdata4:
  208. result = u->s4;
  209. p += 4;
  210. break;
  211. case DW_EH_PE_sdata8:
  212. result = u->s8;
  213. p += 8;
  214. break;
  215. default:
  216. __gxx_abort ();
  217. }
  218. if (result != 0)
  219. {
  220. #if __FDPIC__
  221. /* FDPIC relative addresses imply taking the GOT address
  222. into account. */
  223. if ((encoding & DW_EH_PE_pcrel) && (encoding & DW_EH_PE_indirect))
  224. {
  225. result += _Unwind_gnu_Find_got ((_Unwind_Ptr) u);
  226. result = *(_Unwind_Internal_Ptr *) result;
  227. }
  228. else
  229. {
  230. result += ((encoding & 0x70) == DW_EH_PE_pcrel
  231. ? (_Unwind_Internal_Ptr) u : base);
  232. if (encoding & DW_EH_PE_indirect)
  233. result = *(_Unwind_Internal_Ptr *) result;
  234. }
  235. #else
  236. result += ((encoding & 0x70) == DW_EH_PE_pcrel
  237. ? (_Unwind_Internal_Ptr) u : base);
  238. if (encoding & DW_EH_PE_indirect)
  239. result = *(_Unwind_Internal_Ptr *) result;
  240. #endif
  241. }
  242. }
  243. *val = result;
  244. return p;
  245. }
  246. #pragma GCC diagnostic pop
  247. #ifndef NO_BASE_OF_ENCODED_VALUE
  248. /* Like read_encoded_value_with_base, but get the base from the context
  249. rather than providing it directly. */
  250. static inline const unsigned char *
  251. read_encoded_value (struct _Unwind_Context *context, unsigned char encoding,
  252. const unsigned char *p, _Unwind_Ptr *val)
  253. {
  254. return read_encoded_value_with_base (encoding,
  255. base_of_encoded_value (encoding, context),
  256. p, val);
  257. }
  258. #endif
  259. #endif /* unwind-pe.h */