unwind-cr16.c 45 KB

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