unwind-dw2.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /* DWARF2 exception handling and frame unwind runtime interface routines.
  2. Copyright (C) 1997-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. #include "tconfig.h"
  20. #include "tsystem.h"
  21. #include "coretypes.h"
  22. #include "tm.h"
  23. #include "libgcc_tm.h"
  24. #include "dwarf2.h"
  25. #include "unwind.h"
  26. #ifdef __USING_SJLJ_EXCEPTIONS__
  27. # define NO_SIZE_OF_ENCODED_VALUE
  28. #endif
  29. #include "unwind-pe.h"
  30. #include "unwind-dw2-fde.h"
  31. #include "gthr.h"
  32. #include "unwind-dw2.h"
  33. #ifdef HAVE_SYS_SDT_H
  34. #include <sys/sdt.h>
  35. #endif
  36. #ifndef __USING_SJLJ_EXCEPTIONS__
  37. #ifndef __LIBGCC_STACK_GROWS_DOWNWARD__
  38. #define __LIBGCC_STACK_GROWS_DOWNWARD__ 0
  39. #else
  40. #undef __LIBGCC_STACK_GROWS_DOWNWARD__
  41. #define __LIBGCC_STACK_GROWS_DOWNWARD__ 1
  42. #endif
  43. /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
  44. #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
  45. #define PRE_GCC3_DWARF_FRAME_REGISTERS __LIBGCC_DWARF_FRAME_REGISTERS__
  46. #endif
  47. /* ??? For the public function interfaces, we tend to gcc_assert that the
  48. column numbers are in range. For the dwarf2 unwind info this does happen,
  49. although so far in a case that doesn't actually matter.
  50. See PR49146, in which a call from x86_64 ms abi to x86_64 unix abi stores
  51. the call-saved xmm registers and annotates them. We havn't bothered
  52. providing support for the xmm registers for the x86_64 port primarily
  53. because the 64-bit windows targets don't use dwarf2 unwind, using sjlj or
  54. SEH instead. Adding the support for unix targets would generally be a
  55. waste. However, some runtime libraries supplied with ICC do contain such
  56. an unorthodox transition, as well as the unwind info to match. This loss
  57. of register restoration doesn't matter in practice, because the exception
  58. is caught in the native unix abi, where all of the xmm registers are
  59. call clobbered.
  60. Ideally, we'd record some bit to notice when we're failing to restore some
  61. register recorded in the unwind info, but to do that we need annotation on
  62. the unix->ms abi edge, so that we know when the register data may be
  63. discarded. And since this edge is also within the ICC library, we're
  64. unlikely to be able to get the new annotation.
  65. Barring a magic solution to restore the ms abi defined 128-bit xmm registers
  66. (as distictly opposed to the full runtime width) without causing extra
  67. overhead for normal unix abis, the best solution seems to be to simply
  68. ignore unwind data for unknown columns. */
  69. #define UNWIND_COLUMN_IN_RANGE(x) \
  70. __builtin_expect((x) <= __LIBGCC_DWARF_FRAME_REGISTERS__, 1)
  71. #ifdef REG_VALUE_IN_UNWIND_CONTEXT
  72. typedef _Unwind_Word _Unwind_Context_Reg_Val;
  73. #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
  74. #define ASSUME_EXTENDED_UNWIND_CONTEXT 1
  75. #endif
  76. static inline _Unwind_Word
  77. _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
  78. {
  79. return val;
  80. }
  81. static inline _Unwind_Context_Reg_Val
  82. _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
  83. {
  84. return val;
  85. }
  86. #else
  87. typedef void *_Unwind_Context_Reg_Val;
  88. static inline _Unwind_Word
  89. _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
  90. {
  91. return (_Unwind_Word) (_Unwind_Internal_Ptr) val;
  92. }
  93. static inline _Unwind_Context_Reg_Val
  94. _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
  95. {
  96. return (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) val;
  97. }
  98. #endif
  99. #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
  100. #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
  101. #endif
  102. /* This is the register and unwind state for a particular frame. This
  103. provides the information necessary to unwind up past a frame and return
  104. to its caller. */
  105. struct _Unwind_Context
  106. {
  107. _Unwind_Context_Reg_Val reg[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
  108. void *cfa;
  109. void *ra;
  110. void *lsda;
  111. struct dwarf_eh_bases bases;
  112. /* Signal frame context. */
  113. #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
  114. /* Context which has version/args_size/by_value fields. */
  115. #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
  116. /* Bit reserved on AArch64, return address has been signed with A or B
  117. key. */
  118. #define RA_SIGNED_BIT ((~(_Unwind_Word) 0 >> 3) + 1)
  119. _Unwind_Word flags;
  120. /* 0 for now, can be increased when further fields are added to
  121. struct _Unwind_Context. */
  122. _Unwind_Word version;
  123. _Unwind_Word args_size;
  124. char by_value[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
  125. };
  126. /* Byte size of every register managed by these routines. */
  127. static unsigned char dwarf_reg_size_table[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
  128. /* Read unaligned data from the instruction buffer. */
  129. union unaligned
  130. {
  131. void *p;
  132. unsigned u2 __attribute__ ((mode (HI)));
  133. unsigned u4 __attribute__ ((mode (SI)));
  134. unsigned u8 __attribute__ ((mode (DI)));
  135. signed s2 __attribute__ ((mode (HI)));
  136. signed s4 __attribute__ ((mode (SI)));
  137. signed s8 __attribute__ ((mode (DI)));
  138. } __attribute__ ((packed));
  139. static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
  140. static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
  141. _Unwind_FrameState *);
  142. static inline void *
  143. read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
  144. static inline int
  145. read_1u (const void *p) { return *(const unsigned char *) p; }
  146. static inline int
  147. read_1s (const void *p) { return *(const signed char *) p; }
  148. static inline int
  149. read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
  150. static inline int
  151. read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
  152. static inline unsigned int
  153. read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
  154. static inline int
  155. read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
  156. static inline unsigned long
  157. read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
  158. static inline unsigned long
  159. read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
  160. static inline _Unwind_Word
  161. _Unwind_IsSignalFrame (struct _Unwind_Context *context)
  162. {
  163. return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
  164. }
  165. static inline void
  166. _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
  167. {
  168. if (val)
  169. context->flags |= SIGNAL_FRAME_BIT;
  170. else
  171. context->flags &= ~SIGNAL_FRAME_BIT;
  172. }
  173. static inline _Unwind_Word
  174. _Unwind_IsExtendedContext (struct _Unwind_Context *context)
  175. {
  176. return (ASSUME_EXTENDED_UNWIND_CONTEXT
  177. || (context->flags & EXTENDED_CONTEXT_BIT));
  178. }
  179. /* Get the value of register REGNO as saved in CONTEXT. */
  180. inline _Unwind_Word
  181. _Unwind_GetGR (struct _Unwind_Context *context, int regno)
  182. {
  183. int size, index;
  184. _Unwind_Context_Reg_Val val;
  185. #ifdef DWARF_ZERO_REG
  186. if (regno == DWARF_ZERO_REG)
  187. return 0;
  188. #endif
  189. index = DWARF_REG_TO_UNWIND_COLUMN (regno);
  190. gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
  191. size = dwarf_reg_size_table[index];
  192. val = context->reg[index];
  193. if (_Unwind_IsExtendedContext (context) && context->by_value[index])
  194. return _Unwind_Get_Unwind_Word (val);
  195. #ifdef DWARF_LAZY_REGISTER_VALUE
  196. {
  197. _Unwind_Word value;
  198. if (DWARF_LAZY_REGISTER_VALUE (regno, &value))
  199. return value;
  200. }
  201. #endif
  202. /* This will segfault if the register hasn't been saved. */
  203. if (size == sizeof(_Unwind_Ptr))
  204. return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
  205. else
  206. {
  207. gcc_assert (size == sizeof(_Unwind_Word));
  208. return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
  209. }
  210. }
  211. static inline void *
  212. _Unwind_GetPtr (struct _Unwind_Context *context, int index)
  213. {
  214. return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
  215. }
  216. /* Get the value of the CFA as saved in CONTEXT. */
  217. _Unwind_Word
  218. _Unwind_GetCFA (struct _Unwind_Context *context)
  219. {
  220. return (_Unwind_Ptr) context->cfa;
  221. }
  222. /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
  223. inline void
  224. _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
  225. {
  226. int size;
  227. void *ptr;
  228. index = DWARF_REG_TO_UNWIND_COLUMN (index);
  229. gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
  230. size = dwarf_reg_size_table[index];
  231. if (_Unwind_IsExtendedContext (context) && context->by_value[index])
  232. {
  233. context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
  234. return;
  235. }
  236. ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
  237. if (size == sizeof(_Unwind_Ptr))
  238. * (_Unwind_Ptr *) ptr = val;
  239. else
  240. {
  241. gcc_assert (size == sizeof(_Unwind_Word));
  242. * (_Unwind_Word *) ptr = val;
  243. }
  244. }
  245. /* Get the pointer to a register INDEX as saved in CONTEXT. */
  246. static inline void *
  247. _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
  248. {
  249. index = DWARF_REG_TO_UNWIND_COLUMN (index);
  250. if (_Unwind_IsExtendedContext (context) && context->by_value[index])
  251. return &context->reg[index];
  252. return (void *) (_Unwind_Internal_Ptr) context->reg[index];
  253. }
  254. /* Set the pointer to a register INDEX as saved in CONTEXT. */
  255. static inline void
  256. _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
  257. {
  258. index = DWARF_REG_TO_UNWIND_COLUMN (index);
  259. if (_Unwind_IsExtendedContext (context))
  260. context->by_value[index] = 0;
  261. context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
  262. }
  263. /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
  264. static inline void
  265. _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
  266. _Unwind_Word val)
  267. {
  268. index = DWARF_REG_TO_UNWIND_COLUMN (index);
  269. gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
  270. /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
  271. gcc_assert (dwarf_reg_size_table[index] <= sizeof (_Unwind_Context_Reg_Val));
  272. context->by_value[index] = 1;
  273. context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
  274. }
  275. /* Return nonzero if register INDEX is stored by value rather than
  276. by reference. */
  277. static inline int
  278. _Unwind_GRByValue (struct _Unwind_Context *context, int index)
  279. {
  280. index = DWARF_REG_TO_UNWIND_COLUMN (index);
  281. return context->by_value[index];
  282. }
  283. /* Retrieve the return address for CONTEXT. */
  284. inline _Unwind_Ptr
  285. _Unwind_GetIP (struct _Unwind_Context *context)
  286. {
  287. return (_Unwind_Ptr) context->ra;
  288. }
  289. /* Retrieve the return address and flag whether that IP is before
  290. or after first not yet fully executed instruction. */
  291. inline _Unwind_Ptr
  292. _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
  293. {
  294. *ip_before_insn = _Unwind_IsSignalFrame (context);
  295. return (_Unwind_Ptr) context->ra;
  296. }
  297. /* Overwrite the return address for CONTEXT with VAL. */
  298. inline void
  299. _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
  300. {
  301. context->ra = (void *) val;
  302. }
  303. void *
  304. _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
  305. {
  306. return context->lsda;
  307. }
  308. _Unwind_Ptr
  309. _Unwind_GetRegionStart (struct _Unwind_Context *context)
  310. {
  311. return (_Unwind_Ptr) context->bases.func;
  312. }
  313. void *
  314. _Unwind_FindEnclosingFunction (void *pc)
  315. {
  316. struct dwarf_eh_bases bases;
  317. const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
  318. if (fde)
  319. return bases.func;
  320. else
  321. return NULL;
  322. }
  323. #ifndef __ia64__
  324. _Unwind_Ptr
  325. _Unwind_GetDataRelBase (struct _Unwind_Context *context)
  326. {
  327. return (_Unwind_Ptr) context->bases.dbase;
  328. }
  329. _Unwind_Ptr
  330. _Unwind_GetTextRelBase (struct _Unwind_Context *context)
  331. {
  332. return (_Unwind_Ptr) context->bases.tbase;
  333. }
  334. #endif
  335. #include "md-unwind-support.h"
  336. /* Extract any interesting information from the CIE for the translation
  337. unit F belongs to. Return a pointer to the byte after the augmentation,
  338. or NULL if we encountered an undecipherable augmentation. */
  339. static const unsigned char *
  340. extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
  341. _Unwind_FrameState *fs)
  342. {
  343. const unsigned char *aug = cie->augmentation;
  344. const unsigned char *p = aug + strlen ((const char *)aug) + 1;
  345. const unsigned char *ret = NULL;
  346. _uleb128_t utmp;
  347. _sleb128_t stmp;
  348. /* g++ v2 "eh" has pointer immediately following augmentation string,
  349. so it must be handled first. */
  350. if (aug[0] == 'e' && aug[1] == 'h')
  351. {
  352. fs->eh_ptr = read_pointer (p);
  353. p += sizeof (void *);
  354. aug += 2;
  355. }
  356. /* After the augmentation resp. pointer for "eh" augmentation
  357. follows for CIE version >= 4 address size byte and
  358. segment size byte. */
  359. if (__builtin_expect (cie->version >= 4, 0))
  360. {
  361. if (p[0] != sizeof (void *) || p[1] != 0)
  362. return NULL;
  363. p += 2;
  364. }
  365. /* Immediately following this are the code and
  366. data alignment and return address column. */
  367. p = read_uleb128 (p, &utmp);
  368. fs->code_align = (_Unwind_Word)utmp;
  369. p = read_sleb128 (p, &stmp);
  370. fs->data_align = (_Unwind_Sword)stmp;
  371. if (cie->version == 1)
  372. fs->retaddr_column = *p++;
  373. else
  374. {
  375. p = read_uleb128 (p, &utmp);
  376. fs->retaddr_column = (_Unwind_Word)utmp;
  377. }
  378. fs->lsda_encoding = DW_EH_PE_omit;
  379. /* If the augmentation starts with 'z', then a uleb128 immediately
  380. follows containing the length of the augmentation field following
  381. the size. */
  382. if (*aug == 'z')
  383. {
  384. p = read_uleb128 (p, &utmp);
  385. ret = p + utmp;
  386. fs->saw_z = 1;
  387. ++aug;
  388. }
  389. /* Iterate over recognized augmentation subsequences. */
  390. while (*aug != '\0')
  391. {
  392. /* "L" indicates a byte showing how the LSDA pointer is encoded. */
  393. if (aug[0] == 'L')
  394. {
  395. fs->lsda_encoding = *p++;
  396. aug += 1;
  397. }
  398. /* "R" indicates a byte indicating how FDE addresses are encoded. */
  399. else if (aug[0] == 'R')
  400. {
  401. fs->fde_encoding = *p++;
  402. aug += 1;
  403. }
  404. /* "P" indicates a personality routine in the CIE augmentation. */
  405. else if (aug[0] == 'P')
  406. {
  407. _Unwind_Ptr personality;
  408. p = read_encoded_value (context, *p, p + 1, &personality);
  409. fs->personality = (_Unwind_Personality_Fn) personality;
  410. aug += 1;
  411. }
  412. /* "S" indicates a signal frame. */
  413. else if (aug[0] == 'S')
  414. {
  415. fs->signal_frame = 1;
  416. aug += 1;
  417. }
  418. /* aarch64 B-key pointer authentication. */
  419. else if (aug[0] == 'B')
  420. {
  421. aug += 1;
  422. }
  423. /* Otherwise we have an unknown augmentation string.
  424. Bail unless we saw a 'z' prefix. */
  425. else
  426. return ret;
  427. }
  428. return ret ? ret : p;
  429. }
  430. /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
  431. onto the stack to start. */
  432. static _Unwind_Word
  433. execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
  434. struct _Unwind_Context *context, _Unwind_Word initial)
  435. {
  436. _Unwind_Word stack[64]; /* ??? Assume this is enough. */
  437. int stack_elt;
  438. stack[0] = initial;
  439. stack_elt = 1;
  440. while (op_ptr < op_end)
  441. {
  442. enum dwarf_location_atom op = *op_ptr++;
  443. _Unwind_Word result;
  444. _uleb128_t reg, utmp;
  445. _sleb128_t offset, stmp;
  446. switch (op)
  447. {
  448. case DW_OP_lit0:
  449. case DW_OP_lit1:
  450. case DW_OP_lit2:
  451. case DW_OP_lit3:
  452. case DW_OP_lit4:
  453. case DW_OP_lit5:
  454. case DW_OP_lit6:
  455. case DW_OP_lit7:
  456. case DW_OP_lit8:
  457. case DW_OP_lit9:
  458. case DW_OP_lit10:
  459. case DW_OP_lit11:
  460. case DW_OP_lit12:
  461. case DW_OP_lit13:
  462. case DW_OP_lit14:
  463. case DW_OP_lit15:
  464. case DW_OP_lit16:
  465. case DW_OP_lit17:
  466. case DW_OP_lit18:
  467. case DW_OP_lit19:
  468. case DW_OP_lit20:
  469. case DW_OP_lit21:
  470. case DW_OP_lit22:
  471. case DW_OP_lit23:
  472. case DW_OP_lit24:
  473. case DW_OP_lit25:
  474. case DW_OP_lit26:
  475. case DW_OP_lit27:
  476. case DW_OP_lit28:
  477. case DW_OP_lit29:
  478. case DW_OP_lit30:
  479. case DW_OP_lit31:
  480. result = op - DW_OP_lit0;
  481. break;
  482. case DW_OP_addr:
  483. result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
  484. op_ptr += sizeof (void *);
  485. break;
  486. case DW_OP_GNU_encoded_addr:
  487. {
  488. _Unwind_Ptr presult;
  489. op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
  490. result = presult;
  491. }
  492. break;
  493. case DW_OP_const1u:
  494. result = read_1u (op_ptr);
  495. op_ptr += 1;
  496. break;
  497. case DW_OP_const1s:
  498. result = read_1s (op_ptr);
  499. op_ptr += 1;
  500. break;
  501. case DW_OP_const2u:
  502. result = read_2u (op_ptr);
  503. op_ptr += 2;
  504. break;
  505. case DW_OP_const2s:
  506. result = read_2s (op_ptr);
  507. op_ptr += 2;
  508. break;
  509. case DW_OP_const4u:
  510. result = read_4u (op_ptr);
  511. op_ptr += 4;
  512. break;
  513. case DW_OP_const4s:
  514. result = read_4s (op_ptr);
  515. op_ptr += 4;
  516. break;
  517. case DW_OP_const8u:
  518. result = read_8u (op_ptr);
  519. op_ptr += 8;
  520. break;
  521. case DW_OP_const8s:
  522. result = read_8s (op_ptr);
  523. op_ptr += 8;
  524. break;
  525. case DW_OP_constu:
  526. op_ptr = read_uleb128 (op_ptr, &utmp);
  527. result = (_Unwind_Word)utmp;
  528. break;
  529. case DW_OP_consts:
  530. op_ptr = read_sleb128 (op_ptr, &stmp);
  531. result = (_Unwind_Sword)stmp;
  532. break;
  533. case DW_OP_reg0:
  534. case DW_OP_reg1:
  535. case DW_OP_reg2:
  536. case DW_OP_reg3:
  537. case DW_OP_reg4:
  538. case DW_OP_reg5:
  539. case DW_OP_reg6:
  540. case DW_OP_reg7:
  541. case DW_OP_reg8:
  542. case DW_OP_reg9:
  543. case DW_OP_reg10:
  544. case DW_OP_reg11:
  545. case DW_OP_reg12:
  546. case DW_OP_reg13:
  547. case DW_OP_reg14:
  548. case DW_OP_reg15:
  549. case DW_OP_reg16:
  550. case DW_OP_reg17:
  551. case DW_OP_reg18:
  552. case DW_OP_reg19:
  553. case DW_OP_reg20:
  554. case DW_OP_reg21:
  555. case DW_OP_reg22:
  556. case DW_OP_reg23:
  557. case DW_OP_reg24:
  558. case DW_OP_reg25:
  559. case DW_OP_reg26:
  560. case DW_OP_reg27:
  561. case DW_OP_reg28:
  562. case DW_OP_reg29:
  563. case DW_OP_reg30:
  564. case DW_OP_reg31:
  565. result = _Unwind_GetGR (context, op - DW_OP_reg0);
  566. break;
  567. case DW_OP_regx:
  568. op_ptr = read_uleb128 (op_ptr, &reg);
  569. result = _Unwind_GetGR (context, reg);
  570. break;
  571. case DW_OP_breg0:
  572. case DW_OP_breg1:
  573. case DW_OP_breg2:
  574. case DW_OP_breg3:
  575. case DW_OP_breg4:
  576. case DW_OP_breg5:
  577. case DW_OP_breg6:
  578. case DW_OP_breg7:
  579. case DW_OP_breg8:
  580. case DW_OP_breg9:
  581. case DW_OP_breg10:
  582. case DW_OP_breg11:
  583. case DW_OP_breg12:
  584. case DW_OP_breg13:
  585. case DW_OP_breg14:
  586. case DW_OP_breg15:
  587. case DW_OP_breg16:
  588. case DW_OP_breg17:
  589. case DW_OP_breg18:
  590. case DW_OP_breg19:
  591. case DW_OP_breg20:
  592. case DW_OP_breg21:
  593. case DW_OP_breg22:
  594. case DW_OP_breg23:
  595. case DW_OP_breg24:
  596. case DW_OP_breg25:
  597. case DW_OP_breg26:
  598. case DW_OP_breg27:
  599. case DW_OP_breg28:
  600. case DW_OP_breg29:
  601. case DW_OP_breg30:
  602. case DW_OP_breg31:
  603. op_ptr = read_sleb128 (op_ptr, &offset);
  604. result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
  605. break;
  606. case DW_OP_bregx:
  607. op_ptr = read_uleb128 (op_ptr, &reg);
  608. op_ptr = read_sleb128 (op_ptr, &offset);
  609. result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
  610. break;
  611. case DW_OP_dup:
  612. gcc_assert (stack_elt);
  613. result = stack[stack_elt - 1];
  614. break;
  615. case DW_OP_drop:
  616. gcc_assert (stack_elt);
  617. stack_elt -= 1;
  618. goto no_push;
  619. case DW_OP_pick:
  620. offset = *op_ptr++;
  621. gcc_assert (offset < stack_elt - 1);
  622. result = stack[stack_elt - 1 - offset];
  623. break;
  624. case DW_OP_over:
  625. gcc_assert (stack_elt >= 2);
  626. result = stack[stack_elt - 2];
  627. break;
  628. case DW_OP_swap:
  629. {
  630. _Unwind_Word t;
  631. gcc_assert (stack_elt >= 2);
  632. t = stack[stack_elt - 1];
  633. stack[stack_elt - 1] = stack[stack_elt - 2];
  634. stack[stack_elt - 2] = t;
  635. goto no_push;
  636. }
  637. case DW_OP_rot:
  638. {
  639. _Unwind_Word t1, t2, t3;
  640. gcc_assert (stack_elt >= 3);
  641. t1 = stack[stack_elt - 1];
  642. t2 = stack[stack_elt - 2];
  643. t3 = stack[stack_elt - 3];
  644. stack[stack_elt - 1] = t2;
  645. stack[stack_elt - 2] = t3;
  646. stack[stack_elt - 3] = t1;
  647. goto no_push;
  648. }
  649. case DW_OP_deref:
  650. case DW_OP_deref_size:
  651. case DW_OP_abs:
  652. case DW_OP_neg:
  653. case DW_OP_not:
  654. case DW_OP_plus_uconst:
  655. /* Unary operations. */
  656. gcc_assert (stack_elt);
  657. stack_elt -= 1;
  658. result = stack[stack_elt];
  659. switch (op)
  660. {
  661. case DW_OP_deref:
  662. {
  663. void *ptr = (void *) (_Unwind_Ptr) result;
  664. result = (_Unwind_Ptr) read_pointer (ptr);
  665. }
  666. break;
  667. case DW_OP_deref_size:
  668. {
  669. void *ptr = (void *) (_Unwind_Ptr) result;
  670. switch (*op_ptr++)
  671. {
  672. case 1:
  673. result = read_1u (ptr);
  674. break;
  675. case 2:
  676. result = read_2u (ptr);
  677. break;
  678. case 4:
  679. result = read_4u (ptr);
  680. break;
  681. case 8:
  682. result = read_8u (ptr);
  683. break;
  684. default:
  685. gcc_unreachable ();
  686. }
  687. }
  688. break;
  689. case DW_OP_abs:
  690. if ((_Unwind_Sword) result < 0)
  691. result = -result;
  692. break;
  693. case DW_OP_neg:
  694. result = -result;
  695. break;
  696. case DW_OP_not:
  697. result = ~result;
  698. break;
  699. case DW_OP_plus_uconst:
  700. op_ptr = read_uleb128 (op_ptr, &utmp);
  701. result += (_Unwind_Word)utmp;
  702. break;
  703. default:
  704. gcc_unreachable ();
  705. }
  706. break;
  707. case DW_OP_and:
  708. case DW_OP_div:
  709. case DW_OP_minus:
  710. case DW_OP_mod:
  711. case DW_OP_mul:
  712. case DW_OP_or:
  713. case DW_OP_plus:
  714. case DW_OP_shl:
  715. case DW_OP_shr:
  716. case DW_OP_shra:
  717. case DW_OP_xor:
  718. case DW_OP_le:
  719. case DW_OP_ge:
  720. case DW_OP_eq:
  721. case DW_OP_lt:
  722. case DW_OP_gt:
  723. case DW_OP_ne:
  724. {
  725. /* Binary operations. */
  726. _Unwind_Word first, second;
  727. gcc_assert (stack_elt >= 2);
  728. stack_elt -= 2;
  729. second = stack[stack_elt];
  730. first = stack[stack_elt + 1];
  731. switch (op)
  732. {
  733. case DW_OP_and:
  734. result = second & first;
  735. break;
  736. case DW_OP_div:
  737. result = (_Unwind_Sword) second / (_Unwind_Sword) first;
  738. break;
  739. case DW_OP_minus:
  740. result = second - first;
  741. break;
  742. case DW_OP_mod:
  743. result = second % first;
  744. break;
  745. case DW_OP_mul:
  746. result = second * first;
  747. break;
  748. case DW_OP_or:
  749. result = second | first;
  750. break;
  751. case DW_OP_plus:
  752. result = second + first;
  753. break;
  754. case DW_OP_shl:
  755. result = second << first;
  756. break;
  757. case DW_OP_shr:
  758. result = second >> first;
  759. break;
  760. case DW_OP_shra:
  761. result = (_Unwind_Sword) second >> first;
  762. break;
  763. case DW_OP_xor:
  764. result = second ^ first;
  765. break;
  766. case DW_OP_le:
  767. result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
  768. break;
  769. case DW_OP_ge:
  770. result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
  771. break;
  772. case DW_OP_eq:
  773. result = (_Unwind_Sword) second == (_Unwind_Sword) first;
  774. break;
  775. case DW_OP_lt:
  776. result = (_Unwind_Sword) second < (_Unwind_Sword) first;
  777. break;
  778. case DW_OP_gt:
  779. result = (_Unwind_Sword) second > (_Unwind_Sword) first;
  780. break;
  781. case DW_OP_ne:
  782. result = (_Unwind_Sword) second != (_Unwind_Sword) first;
  783. break;
  784. default:
  785. gcc_unreachable ();
  786. }
  787. }
  788. break;
  789. case DW_OP_skip:
  790. offset = read_2s (op_ptr);
  791. op_ptr += 2;
  792. op_ptr += offset;
  793. goto no_push;
  794. case DW_OP_bra:
  795. gcc_assert (stack_elt);
  796. stack_elt -= 1;
  797. offset = read_2s (op_ptr);
  798. op_ptr += 2;
  799. if (stack[stack_elt] != 0)
  800. op_ptr += offset;
  801. goto no_push;
  802. case DW_OP_nop:
  803. goto no_push;
  804. default:
  805. gcc_unreachable ();
  806. }
  807. /* Most things push a result value. */
  808. gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
  809. stack[stack_elt++] = result;
  810. no_push:;
  811. }
  812. /* We were executing this program to get a value. It should be
  813. at top of stack. */
  814. gcc_assert (stack_elt);
  815. stack_elt -= 1;
  816. return stack[stack_elt];
  817. }
  818. /* Decode DWARF 2 call frame information. Takes pointers the
  819. instruction sequence to decode, current register information and
  820. CIE info, and the PC range to evaluate. */
  821. static void
  822. execute_cfa_program (const unsigned char *insn_ptr,
  823. const unsigned char *insn_end,
  824. struct _Unwind_Context *context,
  825. _Unwind_FrameState *fs)
  826. {
  827. struct frame_state_reg_info *unused_rs = NULL;
  828. /* Don't allow remember/restore between CIE and FDE programs. */
  829. fs->regs.prev = NULL;
  830. /* The comparison with the return address uses < rather than <= because
  831. we are only interested in the effects of code before the call; for a
  832. noreturn function, the return address may point to unrelated code with
  833. a different stack configuration that we are not interested in. We
  834. assume that the call itself is unwind info-neutral; if not, or if
  835. there are delay instructions that adjust the stack, these must be
  836. reflected at the point immediately before the call insn.
  837. In signal frames, return address is after last completed instruction,
  838. so we add 1 to return address to make the comparison <=. */
  839. while (insn_ptr < insn_end
  840. && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
  841. {
  842. unsigned char insn = *insn_ptr++;
  843. _uleb128_t reg, utmp;
  844. _sleb128_t offset, stmp;
  845. if ((insn & 0xc0) == DW_CFA_advance_loc)
  846. fs->pc += (insn & 0x3f) * fs->code_align;
  847. else if ((insn & 0xc0) == DW_CFA_offset)
  848. {
  849. reg = insn & 0x3f;
  850. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  851. offset = (_Unwind_Sword) utmp * fs->data_align;
  852. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  853. if (UNWIND_COLUMN_IN_RANGE (reg))
  854. {
  855. fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  856. fs->regs.reg[reg].loc.offset = offset;
  857. }
  858. }
  859. else if ((insn & 0xc0) == DW_CFA_restore)
  860. {
  861. reg = insn & 0x3f;
  862. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  863. if (UNWIND_COLUMN_IN_RANGE (reg))
  864. fs->regs.reg[reg].how = REG_UNSAVED;
  865. }
  866. else switch (insn)
  867. {
  868. case DW_CFA_set_loc:
  869. {
  870. _Unwind_Ptr pc;
  871. insn_ptr = read_encoded_value (context, fs->fde_encoding,
  872. insn_ptr, &pc);
  873. fs->pc = (void *) pc;
  874. }
  875. break;
  876. case DW_CFA_advance_loc1:
  877. fs->pc += read_1u (insn_ptr) * fs->code_align;
  878. insn_ptr += 1;
  879. break;
  880. case DW_CFA_advance_loc2:
  881. fs->pc += read_2u (insn_ptr) * fs->code_align;
  882. insn_ptr += 2;
  883. break;
  884. case DW_CFA_advance_loc4:
  885. fs->pc += read_4u (insn_ptr) * fs->code_align;
  886. insn_ptr += 4;
  887. break;
  888. case DW_CFA_offset_extended:
  889. insn_ptr = read_uleb128 (insn_ptr, &reg);
  890. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  891. offset = (_Unwind_Sword) utmp * fs->data_align;
  892. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  893. if (UNWIND_COLUMN_IN_RANGE (reg))
  894. {
  895. fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  896. fs->regs.reg[reg].loc.offset = offset;
  897. }
  898. break;
  899. case DW_CFA_restore_extended:
  900. insn_ptr = read_uleb128 (insn_ptr, &reg);
  901. /* FIXME, this is wrong; the CIE might have said that the
  902. register was saved somewhere. */
  903. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  904. if (UNWIND_COLUMN_IN_RANGE (reg))
  905. fs->regs.reg[reg].how = REG_UNSAVED;
  906. break;
  907. case DW_CFA_same_value:
  908. insn_ptr = read_uleb128 (insn_ptr, &reg);
  909. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  910. if (UNWIND_COLUMN_IN_RANGE (reg))
  911. fs->regs.reg[reg].how = REG_UNSAVED;
  912. break;
  913. case DW_CFA_undefined:
  914. insn_ptr = read_uleb128 (insn_ptr, &reg);
  915. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  916. if (UNWIND_COLUMN_IN_RANGE (reg))
  917. fs->regs.reg[reg].how = REG_UNDEFINED;
  918. break;
  919. case DW_CFA_nop:
  920. break;
  921. case DW_CFA_register:
  922. {
  923. _uleb128_t reg2;
  924. insn_ptr = read_uleb128 (insn_ptr, &reg);
  925. insn_ptr = read_uleb128 (insn_ptr, &reg2);
  926. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  927. if (UNWIND_COLUMN_IN_RANGE (reg))
  928. {
  929. fs->regs.reg[reg].how = REG_SAVED_REG;
  930. fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2;
  931. }
  932. }
  933. break;
  934. case DW_CFA_remember_state:
  935. {
  936. struct frame_state_reg_info *new_rs;
  937. if (unused_rs)
  938. {
  939. new_rs = unused_rs;
  940. unused_rs = unused_rs->prev;
  941. }
  942. else
  943. new_rs = alloca (sizeof (struct frame_state_reg_info));
  944. *new_rs = fs->regs;
  945. fs->regs.prev = new_rs;
  946. }
  947. break;
  948. case DW_CFA_restore_state:
  949. {
  950. struct frame_state_reg_info *old_rs = fs->regs.prev;
  951. fs->regs = *old_rs;
  952. old_rs->prev = unused_rs;
  953. unused_rs = old_rs;
  954. }
  955. break;
  956. case DW_CFA_def_cfa:
  957. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  958. fs->regs.cfa_reg = (_Unwind_Word)utmp;
  959. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  960. fs->regs.cfa_offset = (_Unwind_Word)utmp;
  961. fs->regs.cfa_how = CFA_REG_OFFSET;
  962. break;
  963. case DW_CFA_def_cfa_register:
  964. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  965. fs->regs.cfa_reg = (_Unwind_Word)utmp;
  966. fs->regs.cfa_how = CFA_REG_OFFSET;
  967. break;
  968. case DW_CFA_def_cfa_offset:
  969. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  970. fs->regs.cfa_offset = utmp;
  971. /* cfa_how deliberately not set. */
  972. break;
  973. case DW_CFA_def_cfa_expression:
  974. fs->regs.cfa_exp = insn_ptr;
  975. fs->regs.cfa_how = CFA_EXP;
  976. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  977. insn_ptr += utmp;
  978. break;
  979. case DW_CFA_expression:
  980. insn_ptr = read_uleb128 (insn_ptr, &reg);
  981. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  982. if (UNWIND_COLUMN_IN_RANGE (reg))
  983. {
  984. fs->regs.reg[reg].how = REG_SAVED_EXP;
  985. fs->regs.reg[reg].loc.exp = insn_ptr;
  986. }
  987. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  988. insn_ptr += utmp;
  989. break;
  990. /* Dwarf3. */
  991. case DW_CFA_offset_extended_sf:
  992. insn_ptr = read_uleb128 (insn_ptr, &reg);
  993. insn_ptr = read_sleb128 (insn_ptr, &stmp);
  994. offset = stmp * fs->data_align;
  995. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  996. if (UNWIND_COLUMN_IN_RANGE (reg))
  997. {
  998. fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  999. fs->regs.reg[reg].loc.offset = offset;
  1000. }
  1001. break;
  1002. case DW_CFA_def_cfa_sf:
  1003. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1004. fs->regs.cfa_reg = (_Unwind_Word)utmp;
  1005. insn_ptr = read_sleb128 (insn_ptr, &stmp);
  1006. fs->regs.cfa_offset = (_Unwind_Sword)stmp;
  1007. fs->regs.cfa_how = CFA_REG_OFFSET;
  1008. fs->regs.cfa_offset *= fs->data_align;
  1009. break;
  1010. case DW_CFA_def_cfa_offset_sf:
  1011. insn_ptr = read_sleb128 (insn_ptr, &stmp);
  1012. fs->regs.cfa_offset = (_Unwind_Sword)stmp;
  1013. fs->regs.cfa_offset *= fs->data_align;
  1014. /* cfa_how deliberately not set. */
  1015. break;
  1016. case DW_CFA_val_offset:
  1017. insn_ptr = read_uleb128 (insn_ptr, &reg);
  1018. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1019. offset = (_Unwind_Sword) utmp * fs->data_align;
  1020. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1021. if (UNWIND_COLUMN_IN_RANGE (reg))
  1022. {
  1023. fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
  1024. fs->regs.reg[reg].loc.offset = offset;
  1025. }
  1026. break;
  1027. case DW_CFA_val_offset_sf:
  1028. insn_ptr = read_uleb128 (insn_ptr, &reg);
  1029. insn_ptr = read_sleb128 (insn_ptr, &stmp);
  1030. offset = stmp * fs->data_align;
  1031. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1032. if (UNWIND_COLUMN_IN_RANGE (reg))
  1033. {
  1034. fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
  1035. fs->regs.reg[reg].loc.offset = offset;
  1036. }
  1037. break;
  1038. case DW_CFA_val_expression:
  1039. insn_ptr = read_uleb128 (insn_ptr, &reg);
  1040. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1041. if (UNWIND_COLUMN_IN_RANGE (reg))
  1042. {
  1043. fs->regs.reg[reg].how = REG_SAVED_VAL_EXP;
  1044. fs->regs.reg[reg].loc.exp = insn_ptr;
  1045. }
  1046. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1047. insn_ptr += utmp;
  1048. break;
  1049. case DW_CFA_GNU_window_save:
  1050. #if defined (__aarch64__) && !defined (__ILP32__)
  1051. /* This CFA is multiplexed with Sparc. On AArch64 it's used to toggle
  1052. return address signing status. */
  1053. fs->regs.reg[DWARF_REGNUM_AARCH64_RA_STATE].loc.offset ^= 1;
  1054. #else
  1055. /* ??? Hardcoded for SPARC register window configuration. */
  1056. if (__LIBGCC_DWARF_FRAME_REGISTERS__ >= 32)
  1057. for (reg = 16; reg < 32; ++reg)
  1058. {
  1059. fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  1060. fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
  1061. }
  1062. #endif
  1063. break;
  1064. case DW_CFA_GNU_args_size:
  1065. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1066. context->args_size = (_Unwind_Word)utmp;
  1067. break;
  1068. case DW_CFA_GNU_negative_offset_extended:
  1069. /* Obsoleted by DW_CFA_offset_extended_sf, but used by
  1070. older PowerPC code. */
  1071. insn_ptr = read_uleb128 (insn_ptr, &reg);
  1072. insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1073. offset = (_Unwind_Word) utmp * fs->data_align;
  1074. reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1075. if (UNWIND_COLUMN_IN_RANGE (reg))
  1076. {
  1077. fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  1078. fs->regs.reg[reg].loc.offset = -offset;
  1079. }
  1080. break;
  1081. default:
  1082. gcc_unreachable ();
  1083. }
  1084. }
  1085. }
  1086. /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
  1087. its caller and decode it into FS. This function also sets the
  1088. args_size and lsda members of CONTEXT, as they are really information
  1089. about the caller's frame. */
  1090. static _Unwind_Reason_Code
  1091. uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1092. {
  1093. const struct dwarf_fde *fde;
  1094. const struct dwarf_cie *cie;
  1095. const unsigned char *aug, *insn, *end;
  1096. memset (fs, 0, sizeof (*fs));
  1097. context->args_size = 0;
  1098. context->lsda = 0;
  1099. if (context->ra == 0)
  1100. return _URC_END_OF_STACK;
  1101. fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
  1102. &context->bases);
  1103. if (fde == NULL)
  1104. {
  1105. #ifdef MD_FALLBACK_FRAME_STATE_FOR
  1106. /* Couldn't find frame unwind info for this function. Try a
  1107. target-specific fallback mechanism. This will necessarily
  1108. not provide a personality routine or LSDA. */
  1109. return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
  1110. #else
  1111. return _URC_END_OF_STACK;
  1112. #endif
  1113. }
  1114. fs->pc = context->bases.func;
  1115. cie = get_cie (fde);
  1116. insn = extract_cie_info (cie, context, fs);
  1117. if (insn == NULL)
  1118. /* CIE contained unknown augmentation. */
  1119. return _URC_FATAL_PHASE1_ERROR;
  1120. /* First decode all the insns in the CIE. */
  1121. end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
  1122. execute_cfa_program (insn, end, context, fs);
  1123. /* Locate augmentation for the fde. */
  1124. aug = (const unsigned char *) fde + sizeof (*fde);
  1125. aug += 2 * size_of_encoded_value (fs->fde_encoding);
  1126. insn = NULL;
  1127. if (fs->saw_z)
  1128. {
  1129. _uleb128_t i;
  1130. aug = read_uleb128 (aug, &i);
  1131. insn = aug + i;
  1132. }
  1133. if (fs->lsda_encoding != DW_EH_PE_omit)
  1134. {
  1135. _Unwind_Ptr lsda;
  1136. aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
  1137. context->lsda = (void *) lsda;
  1138. }
  1139. /* Then the insns in the FDE up to our target PC. */
  1140. if (insn == NULL)
  1141. insn = aug;
  1142. end = (const unsigned char *) next_fde (fde);
  1143. execute_cfa_program (insn, end, context, fs);
  1144. return _URC_NO_REASON;
  1145. }
  1146. typedef struct frame_state
  1147. {
  1148. void *cfa;
  1149. void *eh_ptr;
  1150. long cfa_offset;
  1151. long args_size;
  1152. long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
  1153. unsigned short cfa_reg;
  1154. unsigned short retaddr_column;
  1155. char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
  1156. } frame_state;
  1157. struct frame_state * __frame_state_for (void *, struct frame_state *);
  1158. /* Called from pre-G++ 3.0 __throw to find the registers to restore for
  1159. a given PC_TARGET. The caller should allocate a local variable of
  1160. `struct frame_state' and pass its address to STATE_IN. */
  1161. struct frame_state *
  1162. __frame_state_for (void *pc_target, struct frame_state *state_in)
  1163. {
  1164. struct _Unwind_Context context;
  1165. _Unwind_FrameState fs;
  1166. int reg;
  1167. memset (&context, 0, sizeof (struct _Unwind_Context));
  1168. if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
  1169. context.flags = EXTENDED_CONTEXT_BIT;
  1170. context.ra = pc_target + 1;
  1171. if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
  1172. return 0;
  1173. /* We have no way to pass a location expression for the CFA to our
  1174. caller. It wouldn't understand it anyway. */
  1175. if (fs.regs.cfa_how == CFA_EXP)
  1176. return 0;
  1177. for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
  1178. {
  1179. state_in->saved[reg] = fs.regs.reg[reg].how;
  1180. switch (state_in->saved[reg])
  1181. {
  1182. case REG_SAVED_REG:
  1183. state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
  1184. break;
  1185. case REG_SAVED_OFFSET:
  1186. state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
  1187. break;
  1188. default:
  1189. state_in->reg_or_offset[reg] = 0;
  1190. break;
  1191. }
  1192. }
  1193. state_in->cfa_offset = fs.regs.cfa_offset;
  1194. state_in->cfa_reg = fs.regs.cfa_reg;
  1195. state_in->retaddr_column = fs.retaddr_column;
  1196. state_in->args_size = context.args_size;
  1197. state_in->eh_ptr = fs.eh_ptr;
  1198. return state_in;
  1199. }
  1200. typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
  1201. static inline void
  1202. _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
  1203. _Unwind_SpTmp *tmp_sp)
  1204. {
  1205. int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
  1206. if (size == sizeof(_Unwind_Ptr))
  1207. tmp_sp->ptr = (_Unwind_Ptr) cfa;
  1208. else
  1209. {
  1210. gcc_assert (size == sizeof(_Unwind_Word));
  1211. tmp_sp->word = (_Unwind_Ptr) cfa;
  1212. }
  1213. _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
  1214. }
  1215. static void
  1216. uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1217. {
  1218. struct _Unwind_Context orig_context = *context;
  1219. void *cfa;
  1220. long i;
  1221. #ifdef __LIBGCC_EH_RETURN_STACKADJ_RTX__
  1222. /* Special handling here: Many machines do not use a frame pointer,
  1223. and track the CFA only through offsets from the stack pointer from
  1224. one frame to the next. In this case, the stack pointer is never
  1225. stored, so it has no saved address in the context. What we do
  1226. have is the CFA from the previous stack frame.
  1227. In very special situations (such as unwind info for signal return),
  1228. there may be location expressions that use the stack pointer as well.
  1229. Do this conditionally for one frame. This allows the unwind info
  1230. for one frame to save a copy of the stack pointer from the previous
  1231. frame, and be able to use much easier CFA mechanisms to do it.
  1232. Always zap the saved stack pointer value for the next frame; carrying
  1233. the value over from one frame to another doesn't make sense. */
  1234. _Unwind_SpTmp tmp_sp;
  1235. if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
  1236. _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
  1237. _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
  1238. #endif
  1239. /* Compute this frame's CFA. */
  1240. switch (fs->regs.cfa_how)
  1241. {
  1242. case CFA_REG_OFFSET:
  1243. cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
  1244. cfa += fs->regs.cfa_offset;
  1245. break;
  1246. case CFA_EXP:
  1247. {
  1248. const unsigned char *exp = fs->regs.cfa_exp;
  1249. _uleb128_t len;
  1250. exp = read_uleb128 (exp, &len);
  1251. cfa = (void *) (_Unwind_Ptr)
  1252. execute_stack_op (exp, exp + len, &orig_context, 0);
  1253. break;
  1254. }
  1255. default:
  1256. gcc_unreachable ();
  1257. }
  1258. context->cfa = cfa;
  1259. /* Compute the addresses of all registers saved in this frame. */
  1260. for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__ + 1; ++i)
  1261. switch (fs->regs.reg[i].how)
  1262. {
  1263. case REG_UNSAVED:
  1264. case REG_UNDEFINED:
  1265. break;
  1266. case REG_SAVED_OFFSET:
  1267. _Unwind_SetGRPtr (context, i,
  1268. (void *) (cfa + fs->regs.reg[i].loc.offset));
  1269. break;
  1270. case REG_SAVED_REG:
  1271. if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
  1272. _Unwind_SetGRValue (context, i,
  1273. _Unwind_GetGR (&orig_context,
  1274. fs->regs.reg[i].loc.reg));
  1275. else
  1276. _Unwind_SetGRPtr (context, i,
  1277. _Unwind_GetGRPtr (&orig_context,
  1278. fs->regs.reg[i].loc.reg));
  1279. break;
  1280. case REG_SAVED_EXP:
  1281. {
  1282. const unsigned char *exp = fs->regs.reg[i].loc.exp;
  1283. _uleb128_t len;
  1284. _Unwind_Ptr val;
  1285. exp = read_uleb128 (exp, &len);
  1286. val = execute_stack_op (exp, exp + len, &orig_context,
  1287. (_Unwind_Ptr) cfa);
  1288. _Unwind_SetGRPtr (context, i, (void *) val);
  1289. }
  1290. break;
  1291. case REG_SAVED_VAL_OFFSET:
  1292. _Unwind_SetGRValue (context, i,
  1293. (_Unwind_Internal_Ptr)
  1294. (cfa + fs->regs.reg[i].loc.offset));
  1295. break;
  1296. case REG_SAVED_VAL_EXP:
  1297. {
  1298. const unsigned char *exp = fs->regs.reg[i].loc.exp;
  1299. _uleb128_t len;
  1300. _Unwind_Ptr val;
  1301. exp = read_uleb128 (exp, &len);
  1302. val = execute_stack_op (exp, exp + len, &orig_context,
  1303. (_Unwind_Ptr) cfa);
  1304. _Unwind_SetGRValue (context, i, val);
  1305. }
  1306. break;
  1307. }
  1308. _Unwind_SetSignalFrame (context, fs->signal_frame);
  1309. #ifdef MD_FROB_UPDATE_CONTEXT
  1310. MD_FROB_UPDATE_CONTEXT (context, fs);
  1311. #endif
  1312. }
  1313. /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
  1314. of its caller. Update CONTEXT to refer to the caller as well. Note
  1315. that the args_size and lsda members are not updated here, but later in
  1316. uw_frame_state_for. */
  1317. static void
  1318. uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1319. {
  1320. uw_update_context_1 (context, fs);
  1321. /* In general this unwinder doesn't make any distinction between
  1322. undefined and same_value rule. Call-saved registers are assumed
  1323. to have same_value rule by default and explicit undefined
  1324. rule is handled like same_value. The only exception is
  1325. DW_CFA_undefined on retaddr_column which is supposed to
  1326. mark outermost frame in DWARF 3. */
  1327. if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
  1328. == REG_UNDEFINED)
  1329. /* uw_frame_state_for uses context->ra == 0 check to find outermost
  1330. stack frame. */
  1331. context->ra = 0;
  1332. else
  1333. {
  1334. /* Compute the return address now, since the return address column
  1335. can change from frame to frame. */
  1336. void *ret_addr;
  1337. #ifdef MD_DEMANGLE_RETURN_ADDR
  1338. _Unwind_Word ra = _Unwind_GetGR (context, fs->retaddr_column);
  1339. ret_addr = MD_DEMANGLE_RETURN_ADDR (context, fs, ra);
  1340. #else
  1341. ret_addr = _Unwind_GetPtr (context, fs->retaddr_column);
  1342. #endif
  1343. context->ra = __builtin_extract_return_addr (ret_addr);
  1344. }
  1345. }
  1346. static void
  1347. uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1348. {
  1349. uw_update_context (context, fs);
  1350. }
  1351. /* Fill in CONTEXT for top-of-stack. The only valid registers at this
  1352. level will be the return address and the CFA. */
  1353. #define uw_init_context(CONTEXT) \
  1354. do \
  1355. { \
  1356. /* Do any necessary initialization to access arbitrary stack frames. \
  1357. On the SPARC, this means flushing the register windows. */ \
  1358. __builtin_unwind_init (); \
  1359. uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
  1360. __builtin_return_address (0)); \
  1361. } \
  1362. while (0)
  1363. static inline void
  1364. init_dwarf_reg_size_table (void)
  1365. {
  1366. __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
  1367. }
  1368. static void __attribute__((noinline))
  1369. uw_init_context_1 (struct _Unwind_Context *context,
  1370. void *outer_cfa, void *outer_ra)
  1371. {
  1372. void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
  1373. _Unwind_FrameState fs;
  1374. _Unwind_SpTmp sp_slot;
  1375. _Unwind_Reason_Code code;
  1376. memset (context, 0, sizeof (struct _Unwind_Context));
  1377. context->ra = ra;
  1378. if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
  1379. context->flags = EXTENDED_CONTEXT_BIT;
  1380. code = uw_frame_state_for (context, &fs);
  1381. gcc_assert (code == _URC_NO_REASON);
  1382. #if __GTHREADS
  1383. {
  1384. static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
  1385. if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
  1386. && dwarf_reg_size_table[0] == 0)
  1387. init_dwarf_reg_size_table ();
  1388. }
  1389. #else
  1390. if (dwarf_reg_size_table[0] == 0)
  1391. init_dwarf_reg_size_table ();
  1392. #endif
  1393. /* Force the frame state to use the known cfa value. */
  1394. _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
  1395. fs.regs.cfa_how = CFA_REG_OFFSET;
  1396. fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
  1397. fs.regs.cfa_offset = 0;
  1398. uw_update_context_1 (context, &fs);
  1399. /* If the return address column was saved in a register in the
  1400. initialization context, then we can't see it in the given
  1401. call frame data. So have the initialization context tell us. */
  1402. context->ra = __builtin_extract_return_addr (outer_ra);
  1403. }
  1404. static void _Unwind_DebugHook (void *, void *)
  1405. __attribute__ ((__noinline__, __used__, __noclone__));
  1406. /* This function is called during unwinding. It is intended as a hook
  1407. for a debugger to intercept exceptions. CFA is the CFA of the
  1408. target frame. HANDLER is the PC to which control will be
  1409. transferred. */
  1410. static void
  1411. _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
  1412. void *handler __attribute__ ((__unused__)))
  1413. {
  1414. /* We only want to use stap probes starting with v3. Earlier
  1415. versions added too much startup cost. */
  1416. #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
  1417. STAP_PROBE2 (libgcc, unwind, cfa, handler);
  1418. #else
  1419. asm ("");
  1420. #endif
  1421. }
  1422. /* Install TARGET into CURRENT so that we can return to it. This is a
  1423. macro because __builtin_eh_return must be invoked in the context of
  1424. our caller. FRAMES is a number of frames to be unwind.
  1425. _Unwind_Frames_Extra is a macro to do additional work during unwinding
  1426. if needed, for example shadow stack pointer adjustment for Intel CET
  1427. technology. */
  1428. #define uw_install_context(CURRENT, TARGET, FRAMES) \
  1429. do \
  1430. { \
  1431. long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
  1432. void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
  1433. _Unwind_DebugHook ((TARGET)->cfa, handler); \
  1434. _Unwind_Frames_Extra (FRAMES); \
  1435. __builtin_eh_return (offset, handler); \
  1436. } \
  1437. while (0)
  1438. static long
  1439. uw_install_context_1 (struct _Unwind_Context *current,
  1440. struct _Unwind_Context *target)
  1441. {
  1442. long i;
  1443. _Unwind_SpTmp sp_slot;
  1444. /* If the target frame does not have a saved stack pointer,
  1445. then set up the target's CFA. */
  1446. if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
  1447. _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
  1448. for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__; ++i)
  1449. {
  1450. void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
  1451. void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
  1452. gcc_assert (current->by_value[i] == 0);
  1453. if (target->by_value[i] && c)
  1454. {
  1455. _Unwind_Word w;
  1456. _Unwind_Ptr p;
  1457. if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
  1458. {
  1459. w = (_Unwind_Internal_Ptr) t;
  1460. memcpy (c, &w, sizeof (_Unwind_Word));
  1461. }
  1462. else
  1463. {
  1464. gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
  1465. p = (_Unwind_Internal_Ptr) t;
  1466. memcpy (c, &p, sizeof (_Unwind_Ptr));
  1467. }
  1468. }
  1469. else if (t && c && t != c)
  1470. memcpy (c, t, dwarf_reg_size_table[i]);
  1471. }
  1472. /* If the current frame doesn't have a saved stack pointer, then we
  1473. need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
  1474. pointer value reloaded. */
  1475. if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
  1476. {
  1477. void *target_cfa;
  1478. target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
  1479. /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
  1480. if (__LIBGCC_STACK_GROWS_DOWNWARD__)
  1481. return target_cfa - current->cfa + target->args_size;
  1482. else
  1483. return current->cfa - target_cfa - target->args_size;
  1484. }
  1485. return 0;
  1486. }
  1487. static inline _Unwind_Ptr
  1488. uw_identify_context (struct _Unwind_Context *context)
  1489. {
  1490. /* The CFA is not sufficient to disambiguate the context of a function
  1491. interrupted by a signal before establishing its frame and the context
  1492. of the signal itself. */
  1493. if (__LIBGCC_STACK_GROWS_DOWNWARD__)
  1494. return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
  1495. else
  1496. return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
  1497. }
  1498. #include "unwind.inc"
  1499. #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
  1500. alias (_Unwind_Backtrace);
  1501. alias (_Unwind_DeleteException);
  1502. alias (_Unwind_FindEnclosingFunction);
  1503. alias (_Unwind_ForcedUnwind);
  1504. alias (_Unwind_GetDataRelBase);
  1505. alias (_Unwind_GetTextRelBase);
  1506. alias (_Unwind_GetCFA);
  1507. alias (_Unwind_GetGR);
  1508. alias (_Unwind_GetIP);
  1509. alias (_Unwind_GetLanguageSpecificData);
  1510. alias (_Unwind_GetRegionStart);
  1511. alias (_Unwind_RaiseException);
  1512. alias (_Unwind_Resume);
  1513. alias (_Unwind_Resume_or_Rethrow);
  1514. alias (_Unwind_SetGR);
  1515. alias (_Unwind_SetIP);
  1516. #endif
  1517. #endif /* !USING_SJLJ_EXCEPTIONS */