1 //===--------------------------- Unwind-EHABI.cpp -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //
8 // Implements ARM zero-cost C++ exceptions
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "Unwind-EHABI.h"
13
14 #if defined(_LIBUNWIND_ARM_EHABI)
15
16 #include <inttypes.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "config.h"
24 #include "libunwind.h"
25 #include "libunwind_ext.h"
26 #include "unwind.h"
27
28 namespace {
29
30 // Strange order: take words in order, but inside word, take from most to least
31 // signinficant byte.
getByte(const uint32_t * data,size_t offset)32 uint8_t getByte(const uint32_t* data, size_t offset) {
33 const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data);
34 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
35 return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))];
36 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37 return byteData[offset];
38 #else
39 #error "Unable to determine endianess"
40 #endif
41 }
42
getNextWord(const char * data,uint32_t * out)43 const char* getNextWord(const char* data, uint32_t* out) {
44 *out = *reinterpret_cast<const uint32_t*>(data);
45 return data + 4;
46 }
47
getNextNibble(const char * data,uint32_t * out)48 const char* getNextNibble(const char* data, uint32_t* out) {
49 *out = *reinterpret_cast<const uint16_t*>(data);
50 return data + 2;
51 }
52
53 struct Descriptor {
54 // See # 9.2
55 typedef enum {
56 SU16 = 0, // Short descriptor, 16-bit entries
57 LU16 = 1, // Long descriptor, 16-bit entries
58 LU32 = 3, // Long descriptor, 32-bit entries
59 RESERVED0 = 4, RESERVED1 = 5, RESERVED2 = 6, RESERVED3 = 7,
60 RESERVED4 = 8, RESERVED5 = 9, RESERVED6 = 10, RESERVED7 = 11,
61 RESERVED8 = 12, RESERVED9 = 13, RESERVED10 = 14, RESERVED11 = 15
62 } Format;
63
64 // See # 9.2
65 typedef enum {
66 CLEANUP = 0x0,
67 FUNC = 0x1,
68 CATCH = 0x2,
69 INVALID = 0x4
70 } Kind;
71 };
72
ProcessDescriptors(_Unwind_State state,_Unwind_Control_Block * ucbp,struct _Unwind_Context * context,Descriptor::Format format,const char * descriptorStart,uint32_t flags)73 _Unwind_Reason_Code ProcessDescriptors(
74 _Unwind_State state,
75 _Unwind_Control_Block* ucbp,
76 struct _Unwind_Context* context,
77 Descriptor::Format format,
78 const char* descriptorStart,
79 uint32_t flags) {
80
81 // EHT is inlined in the index using compact form. No descriptors. #5
82 if (flags & 0x1)
83 return _URC_CONTINUE_UNWIND;
84
85 // TODO: We should check the state here, and determine whether we need to
86 // perform phase1 or phase2 unwinding.
87 (void)state;
88
89 const char* descriptor = descriptorStart;
90 uint32_t descriptorWord;
91 getNextWord(descriptor, &descriptorWord);
92 while (descriptorWord) {
93 // Read descriptor based on # 9.2.
94 uint32_t length;
95 uint32_t offset;
96 switch (format) {
97 case Descriptor::LU32:
98 descriptor = getNextWord(descriptor, &length);
99 descriptor = getNextWord(descriptor, &offset);
100 break;
101 case Descriptor::LU16:
102 descriptor = getNextNibble(descriptor, &length);
103 descriptor = getNextNibble(descriptor, &offset);
104 break;
105 default:
106 assert(false);
107 return _URC_FAILURE;
108 }
109
110 // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value.
111 Descriptor::Kind kind =
112 static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1));
113
114 // Clear off flag from last bit.
115 length &= ~1u;
116 offset &= ~1u;
117 uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset;
118 uintptr_t scopeEnd = scopeStart + length;
119 uintptr_t pc = _Unwind_GetIP(context);
120 bool isInScope = (scopeStart <= pc) && (pc < scopeEnd);
121
122 switch (kind) {
123 case Descriptor::CLEANUP: {
124 // TODO(ajwong): Handle cleanup descriptors.
125 break;
126 }
127 case Descriptor::FUNC: {
128 // TODO(ajwong): Handle function descriptors.
129 break;
130 }
131 case Descriptor::CATCH: {
132 // Catch descriptors require gobbling one more word.
133 uint32_t landing_pad;
134 descriptor = getNextWord(descriptor, &landing_pad);
135
136 if (isInScope) {
137 // TODO(ajwong): This is only phase1 compatible logic. Implement
138 // phase2.
139 landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
140 if (landing_pad == 0xffffffff) {
141 return _URC_HANDLER_FOUND;
142 } else if (landing_pad == 0xfffffffe) {
143 return _URC_FAILURE;
144 } else {
145 /*
146 bool is_reference_type = landing_pad & 0x80000000;
147 void* matched_object;
148 if (__cxxabiv1::__cxa_type_match(
149 ucbp, reinterpret_cast<const std::type_info *>(landing_pad),
150 is_reference_type,
151 &matched_object) != __cxxabiv1::ctm_failed)
152 return _URC_HANDLER_FOUND;
153 */
154 _LIBUNWIND_ABORT("Type matching not implemented");
155 }
156 }
157 break;
158 }
159 default:
160 _LIBUNWIND_ABORT("Invalid descriptor kind found.");
161 }
162
163 getNextWord(descriptor, &descriptorWord);
164 }
165
166 return _URC_CONTINUE_UNWIND;
167 }
168
unwindOneFrame(_Unwind_State state,_Unwind_Control_Block * ucbp,struct _Unwind_Context * context)169 static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state,
170 _Unwind_Control_Block* ucbp,
171 struct _Unwind_Context* context) {
172 // Read the compact model EHT entry's header # 6.3
173 const uint32_t* unwindingData = ucbp->pr_cache.ehtp;
174 assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry");
175 Descriptor::Format format =
176 static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24);
177
178 const char *lsda =
179 reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
180
181 // Handle descriptors before unwinding so they are processed in the context
182 // of the correct stack frame.
183 _Unwind_Reason_Code result =
184 ProcessDescriptors(state, ucbp, context, format, lsda,
185 ucbp->pr_cache.additional);
186
187 if (result != _URC_CONTINUE_UNWIND)
188 return result;
189
190 if (__unw_step(reinterpret_cast<unw_cursor_t *>(context)) != UNW_STEP_SUCCESS)
191 return _URC_FAILURE;
192 return _URC_CONTINUE_UNWIND;
193 }
194
195 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
196 // _UVRSD_UINT32.
RegisterMask(uint8_t start,uint8_t count_minus_one)197 uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
198 return ((1U << (count_minus_one + 1)) - 1) << start;
199 }
200
201 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
202 // _UVRSD_DOUBLE.
RegisterRange(uint8_t start,uint8_t count_minus_one)203 uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
204 return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
205 }
206
207 } // end anonymous namespace
208
209 /**
210 * Decodes an EHT entry.
211 *
212 * @param data Pointer to EHT.
213 * @param[out] off Offset from return value (in bytes) to begin interpretation.
214 * @param[out] len Number of bytes in unwind code.
215 * @return Pointer to beginning of unwind code.
216 */
217 extern "C" const uint32_t*
decode_eht_entry(const uint32_t * data,size_t * off,size_t * len)218 decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) {
219 if ((*data & 0x80000000) == 0) {
220 // 6.2: Generic Model
221 //
222 // EHT entry is a prel31 pointing to the PR, followed by data understood
223 // only by the personality routine. Fortunately, all existing assembler
224 // implementations, including GNU assembler, LLVM integrated assembler,
225 // and ARM assembler, assume that the unwind opcodes come after the
226 // personality rountine address.
227 *off = 1; // First byte is size data.
228 *len = (((data[1] >> 24) & 0xff) + 1) * 4;
229 data++; // Skip the first word, which is the prel31 offset.
230 } else {
231 // 6.3: ARM Compact Model
232 //
233 // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded
234 // by format:
235 Descriptor::Format format =
236 static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
237 switch (format) {
238 case Descriptor::SU16:
239 *len = 4;
240 *off = 1;
241 break;
242 case Descriptor::LU16:
243 case Descriptor::LU32:
244 *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
245 *off = 2;
246 break;
247 default:
248 return nullptr;
249 }
250 }
251 return data;
252 }
253
254 _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_VRS_Interpret(_Unwind_Context * context,const uint32_t * data,size_t offset,size_t len)255 _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data,
256 size_t offset, size_t len) {
257 bool wrotePC = false;
258 bool finish = false;
259 while (offset < len && !finish) {
260 uint8_t byte = getByte(data, offset++);
261 if ((byte & 0x80) == 0) {
262 uint32_t sp;
263 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
264 if (byte & 0x40)
265 sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
266 else
267 sp += ((uint32_t)byte << 2) + 4;
268 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
269 } else {
270 switch (byte & 0xf0) {
271 case 0x80: {
272 if (offset >= len)
273 return _URC_FAILURE;
274 uint32_t registers =
275 (((uint32_t)byte & 0x0f) << 12) |
276 (((uint32_t)getByte(data, offset++)) << 4);
277 if (!registers)
278 return _URC_FAILURE;
279 if (registers & (1 << 15))
280 wrotePC = true;
281 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
282 break;
283 }
284 case 0x90: {
285 uint8_t reg = byte & 0x0f;
286 if (reg == 13 || reg == 15)
287 return _URC_FAILURE;
288 uint32_t sp;
289 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
290 _UVRSD_UINT32, &sp);
291 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
292 &sp);
293 break;
294 }
295 case 0xa0: {
296 uint32_t registers = RegisterMask(4, byte & 0x07);
297 if (byte & 0x08)
298 registers |= 1 << 14;
299 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
300 break;
301 }
302 case 0xb0: {
303 switch (byte) {
304 case 0xb0:
305 finish = true;
306 break;
307 case 0xb1: {
308 if (offset >= len)
309 return _URC_FAILURE;
310 uint8_t registers = getByte(data, offset++);
311 if (registers & 0xf0 || !registers)
312 return _URC_FAILURE;
313 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
314 break;
315 }
316 case 0xb2: {
317 uint32_t addend = 0;
318 uint32_t shift = 0;
319 // This decodes a uleb128 value.
320 while (true) {
321 if (offset >= len)
322 return _URC_FAILURE;
323 uint32_t v = getByte(data, offset++);
324 addend |= (v & 0x7f) << shift;
325 if ((v & 0x80) == 0)
326 break;
327 shift += 7;
328 }
329 uint32_t sp;
330 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
331 &sp);
332 sp += 0x204 + (addend << 2);
333 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
334 &sp);
335 break;
336 }
337 case 0xb3: {
338 uint8_t v = getByte(data, offset++);
339 _Unwind_VRS_Pop(context, _UVRSC_VFP,
340 RegisterRange(static_cast<uint8_t>(v >> 4),
341 v & 0x0f), _UVRSD_VFPX);
342 break;
343 }
344 case 0xb4:
345 case 0xb5:
346 case 0xb6:
347 case 0xb7:
348 return _URC_FAILURE;
349 default:
350 _Unwind_VRS_Pop(context, _UVRSC_VFP,
351 RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
352 break;
353 }
354 break;
355 }
356 case 0xc0: {
357 switch (byte) {
358 #if defined(__ARM_WMMX)
359 case 0xc0:
360 case 0xc1:
361 case 0xc2:
362 case 0xc3:
363 case 0xc4:
364 case 0xc5:
365 _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
366 RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
367 break;
368 case 0xc6: {
369 uint8_t v = getByte(data, offset++);
370 uint8_t start = static_cast<uint8_t>(v >> 4);
371 uint8_t count_minus_one = v & 0xf;
372 if (start + count_minus_one >= 16)
373 return _URC_FAILURE;
374 _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
375 RegisterRange(start, count_minus_one),
376 _UVRSD_DOUBLE);
377 break;
378 }
379 case 0xc7: {
380 uint8_t v = getByte(data, offset++);
381 if (!v || v & 0xf0)
382 return _URC_FAILURE;
383 _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
384 break;
385 }
386 #endif
387 case 0xc8:
388 case 0xc9: {
389 uint8_t v = getByte(data, offset++);
390 uint8_t start =
391 static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
392 uint8_t count_minus_one = v & 0xf;
393 if (start + count_minus_one >= 32)
394 return _URC_FAILURE;
395 _Unwind_VRS_Pop(context, _UVRSC_VFP,
396 RegisterRange(start, count_minus_one),
397 _UVRSD_DOUBLE);
398 break;
399 }
400 default:
401 return _URC_FAILURE;
402 }
403 break;
404 }
405 case 0xd0: {
406 if (byte & 0x08)
407 return _URC_FAILURE;
408 _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
409 _UVRSD_DOUBLE);
410 break;
411 }
412 default:
413 return _URC_FAILURE;
414 }
415 }
416 }
417 if (!wrotePC) {
418 uint32_t lr;
419 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
420 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
421 }
422 return _URC_CONTINUE_UNWIND;
423 }
424
425 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr0(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)426 __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
427 _Unwind_Context *context) {
428 return unwindOneFrame(state, ucbp, context);
429 }
430
431 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr1(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)432 __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
433 _Unwind_Context *context) {
434 return unwindOneFrame(state, ucbp, context);
435 }
436
437 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr2(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)438 __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
439 _Unwind_Context *context) {
440 return unwindOneFrame(state, ucbp, context);
441 }
442
443 static _Unwind_Reason_Code
unwind_phase1(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object)444 unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
445 // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
446 // phase 1 and then restoring it to the "primary VRS" for phase 2. The
447 // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
448 // In this implementation, the phases don't share the VRS backing store.
449 // Instead, they are passed the original |uc| and they create a new VRS
450 // from scratch thus achieving the same effect.
451 __unw_init_local(cursor, uc);
452
453 // Walk each frame looking for a place to stop.
454 for (bool handlerNotFound = true; handlerNotFound;) {
455
456 // See if frame has code to run (has personality routine).
457 unw_proc_info_t frameInfo;
458 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
459 _LIBUNWIND_TRACE_UNWINDING(
460 "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
461 "failed => _URC_FATAL_PHASE1_ERROR",
462 static_cast<void *>(exception_object));
463 return _URC_FATAL_PHASE1_ERROR;
464 }
465
466 // When tracing, print state information.
467 if (_LIBUNWIND_TRACING_UNWINDING) {
468 char functionBuf[512];
469 const char *functionName = functionBuf;
470 unw_word_t offset;
471 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
472 &offset) != UNW_ESUCCESS) ||
473 (frameInfo.start_ip + offset > frameInfo.end_ip))
474 functionName = ".anonymous.";
475 unw_word_t pc;
476 __unw_get_reg(cursor, UNW_REG_IP, &pc);
477 _LIBUNWIND_TRACE_UNWINDING(
478 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
479 "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
480 static_cast<void *>(exception_object), pc,
481 frameInfo.start_ip, functionName,
482 frameInfo.lsda, frameInfo.handler);
483 }
484
485 // If there is a personality routine, ask it if it will want to stop at
486 // this frame.
487 if (frameInfo.handler != 0) {
488 _Unwind_Personality_Fn p =
489 (_Unwind_Personality_Fn)(long)(frameInfo.handler);
490 _LIBUNWIND_TRACE_UNWINDING(
491 "unwind_phase1(ex_ojb=%p): calling personality function %p",
492 static_cast<void *>(exception_object),
493 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
494 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
495 exception_object->pr_cache.fnstart = frameInfo.start_ip;
496 exception_object->pr_cache.ehtp =
497 (_Unwind_EHT_Header *)frameInfo.unwind_info;
498 exception_object->pr_cache.additional = frameInfo.flags;
499 _Unwind_Reason_Code personalityResult =
500 (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
501 _LIBUNWIND_TRACE_UNWINDING(
502 "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
503 "additional %x",
504 static_cast<void *>(exception_object), personalityResult,
505 exception_object->pr_cache.fnstart,
506 static_cast<void *>(exception_object->pr_cache.ehtp),
507 exception_object->pr_cache.additional);
508 switch (personalityResult) {
509 case _URC_HANDLER_FOUND:
510 // found a catch clause or locals that need destructing in this frame
511 // stop search and remember stack pointer at the frame
512 handlerNotFound = false;
513 // p should have initialized barrier_cache. EHABI #7.3.5
514 _LIBUNWIND_TRACE_UNWINDING(
515 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
516 static_cast<void *>(exception_object));
517 return _URC_NO_REASON;
518
519 case _URC_CONTINUE_UNWIND:
520 _LIBUNWIND_TRACE_UNWINDING(
521 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
522 static_cast<void *>(exception_object));
523 // continue unwinding
524 break;
525
526 // EHABI #7.3.3
527 case _URC_FAILURE:
528 return _URC_FAILURE;
529
530 default:
531 // something went wrong
532 _LIBUNWIND_TRACE_UNWINDING(
533 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
534 static_cast<void *>(exception_object));
535 return _URC_FATAL_PHASE1_ERROR;
536 }
537 }
538 }
539 return _URC_NO_REASON;
540 }
541
unwind_phase2(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object,bool resume)542 static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
543 _Unwind_Exception *exception_object,
544 bool resume) {
545 // See comment at the start of unwind_phase1 regarding VRS integrity.
546 __unw_init_local(cursor, uc);
547
548 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
549 static_cast<void *>(exception_object));
550 int frame_count = 0;
551
552 // Walk each frame until we reach where search phase said to stop.
553 while (true) {
554 // Ask libunwind to get next frame (skip over first which is
555 // _Unwind_RaiseException or _Unwind_Resume).
556 //
557 // Resume only ever makes sense for 1 frame.
558 _Unwind_State state =
559 resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
560 if (resume && frame_count == 1) {
561 // On a resume, first unwind the _Unwind_Resume() frame. The next frame
562 // is now the landing pad for the cleanup from a previous execution of
563 // phase2. To continue unwindingly correctly, replace VRS[15] with the
564 // IP of the frame that the previous run of phase2 installed the context
565 // for. After this, continue unwinding as if normal.
566 //
567 // See #7.4.6 for details.
568 __unw_set_reg(cursor, UNW_REG_IP,
569 exception_object->unwinder_cache.reserved2);
570 resume = false;
571 }
572
573 // Get info about this frame.
574 unw_word_t sp;
575 unw_proc_info_t frameInfo;
576 __unw_get_reg(cursor, UNW_REG_SP, &sp);
577 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
578 _LIBUNWIND_TRACE_UNWINDING(
579 "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
580 "failed => _URC_FATAL_PHASE2_ERROR",
581 static_cast<void *>(exception_object));
582 return _URC_FATAL_PHASE2_ERROR;
583 }
584
585 // When tracing, print state information.
586 if (_LIBUNWIND_TRACING_UNWINDING) {
587 char functionBuf[512];
588 const char *functionName = functionBuf;
589 unw_word_t offset;
590 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
591 &offset) != UNW_ESUCCESS) ||
592 (frameInfo.start_ip + offset > frameInfo.end_ip))
593 functionName = ".anonymous.";
594 _LIBUNWIND_TRACE_UNWINDING(
595 "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
596 "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
597 static_cast<void *>(exception_object), frameInfo.start_ip,
598 functionName, sp, frameInfo.lsda,
599 frameInfo.handler);
600 }
601
602 // If there is a personality routine, tell it we are unwinding.
603 if (frameInfo.handler != 0) {
604 _Unwind_Personality_Fn p =
605 (_Unwind_Personality_Fn)(long)(frameInfo.handler);
606 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
607 // EHABI #7.2
608 exception_object->pr_cache.fnstart = frameInfo.start_ip;
609 exception_object->pr_cache.ehtp =
610 (_Unwind_EHT_Header *)frameInfo.unwind_info;
611 exception_object->pr_cache.additional = frameInfo.flags;
612 _Unwind_Reason_Code personalityResult =
613 (*p)(state, exception_object, context);
614 switch (personalityResult) {
615 case _URC_CONTINUE_UNWIND:
616 // Continue unwinding
617 _LIBUNWIND_TRACE_UNWINDING(
618 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
619 static_cast<void *>(exception_object));
620 // EHABI #7.2
621 if (sp == exception_object->barrier_cache.sp) {
622 // Phase 1 said we would stop at this frame, but we did not...
623 _LIBUNWIND_ABORT("during phase1 personality function said it would "
624 "stop here, but now in phase2 it did not stop here");
625 }
626 break;
627 case _URC_INSTALL_CONTEXT:
628 _LIBUNWIND_TRACE_UNWINDING(
629 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
630 static_cast<void *>(exception_object));
631 // Personality routine says to transfer control to landing pad.
632 // We may get control back if landing pad calls _Unwind_Resume().
633 if (_LIBUNWIND_TRACING_UNWINDING) {
634 unw_word_t pc;
635 __unw_get_reg(cursor, UNW_REG_IP, &pc);
636 __unw_get_reg(cursor, UNW_REG_SP, &sp);
637 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
638 "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
639 static_cast<void *>(exception_object),
640 pc, sp);
641 }
642
643 {
644 // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
645 // is called back, to find this same frame.
646 unw_word_t pc;
647 __unw_get_reg(cursor, UNW_REG_IP, &pc);
648 exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
649 }
650 __unw_resume(cursor);
651 // __unw_resume() only returns if there was an error.
652 return _URC_FATAL_PHASE2_ERROR;
653
654 // # EHABI #7.4.3
655 case _URC_FAILURE:
656 abort();
657
658 default:
659 // Personality routine returned an unknown result code.
660 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
661 personalityResult);
662 return _URC_FATAL_PHASE2_ERROR;
663 }
664 }
665 frame_count++;
666 }
667
668 // Clean up phase did not resume at the frame that the search phase
669 // said it would...
670 return _URC_FATAL_PHASE2_ERROR;
671 }
672
673 /// Called by __cxa_throw. Only returns if there is a fatal error.
674 _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_RaiseException(_Unwind_Exception * exception_object)675 _Unwind_RaiseException(_Unwind_Exception *exception_object) {
676 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
677 static_cast<void *>(exception_object));
678 unw_context_t uc;
679 unw_cursor_t cursor;
680 __unw_getcontext(&uc);
681
682 // This field for is for compatibility with GCC to say this isn't a forced
683 // unwind. EHABI #7.2
684 exception_object->unwinder_cache.reserved1 = 0;
685
686 // phase 1: the search phase
687 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
688 if (phase1 != _URC_NO_REASON)
689 return phase1;
690
691 // phase 2: the clean up phase
692 return unwind_phase2(&uc, &cursor, exception_object, false);
693 }
694
_Unwind_Complete(_Unwind_Exception * exception_object)695 _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
696 // This is to be called when exception handling completes to give us a chance
697 // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
698 (void)exception_object;
699 }
700
701 /// When _Unwind_RaiseException() is in phase2, it hands control
702 /// to the personality function at each frame. The personality
703 /// may force a jump to a landing pad in that function, the landing
704 /// pad code may then call _Unwind_Resume() to continue with the
705 /// unwinding. Note: the call to _Unwind_Resume() is from compiler
706 /// geneated user code. All other _Unwind_* routines are called
707 /// by the C++ runtime __cxa_* routines.
708 ///
709 /// Note: re-throwing an exception (as opposed to continuing the unwind)
710 /// is implemented by having the code call __cxa_rethrow() which
711 /// in turn calls _Unwind_Resume_or_Rethrow().
712 _LIBUNWIND_EXPORT void
_Unwind_Resume(_Unwind_Exception * exception_object)713 _Unwind_Resume(_Unwind_Exception *exception_object) {
714 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
715 static_cast<void *>(exception_object));
716 unw_context_t uc;
717 unw_cursor_t cursor;
718 __unw_getcontext(&uc);
719
720 // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
721 // which is in the same position as private_1 below.
722 // TODO(ajwong): Who wronte the above? Why is it true?
723 unwind_phase2(&uc, &cursor, exception_object, true);
724
725 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
726 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
727 }
728
729 /// Called by personality handler during phase 2 to get LSDA for current frame.
730 _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetLanguageSpecificData(struct _Unwind_Context * context)731 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
732 unw_cursor_t *cursor = (unw_cursor_t *)context;
733 unw_proc_info_t frameInfo;
734 uintptr_t result = 0;
735 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
736 result = (uintptr_t)frameInfo.lsda;
737 _LIBUNWIND_TRACE_API(
738 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
739 static_cast<void *>(context), (long long)result);
740 return result;
741 }
742
ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,void * valuep)743 static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
744 void* valuep) {
745 uint64_t value = 0;
746 switch (representation) {
747 case _UVRSD_UINT32:
748 case _UVRSD_FLOAT:
749 memcpy(&value, valuep, sizeof(uint32_t));
750 break;
751
752 case _UVRSD_VFPX:
753 case _UVRSD_UINT64:
754 case _UVRSD_DOUBLE:
755 memcpy(&value, valuep, sizeof(uint64_t));
756 break;
757 }
758 return value;
759 }
760
761 _LIBUNWIND_EXPORT _Unwind_VRS_Result
_Unwind_VRS_Set(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)762 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
763 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
764 void *valuep) {
765 _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
766 "rep=%d, value=0x%llX)",
767 static_cast<void *>(context), regclass, regno,
768 representation,
769 ValueAsBitPattern(representation, valuep));
770 unw_cursor_t *cursor = (unw_cursor_t *)context;
771 switch (regclass) {
772 case _UVRSC_CORE:
773 if (representation != _UVRSD_UINT32 || regno > 15)
774 return _UVRSR_FAILED;
775 return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
776 *(unw_word_t *)valuep) == UNW_ESUCCESS
777 ? _UVRSR_OK
778 : _UVRSR_FAILED;
779 case _UVRSC_VFP:
780 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
781 return _UVRSR_FAILED;
782 if (representation == _UVRSD_VFPX) {
783 // Can only touch d0-15 with FSTMFDX.
784 if (regno > 15)
785 return _UVRSR_FAILED;
786 __unw_save_vfp_as_X(cursor);
787 } else {
788 if (regno > 31)
789 return _UVRSR_FAILED;
790 }
791 return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
792 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
793 ? _UVRSR_OK
794 : _UVRSR_FAILED;
795 #if defined(__ARM_WMMX)
796 case _UVRSC_WMMXC:
797 if (representation != _UVRSD_UINT32 || regno > 3)
798 return _UVRSR_FAILED;
799 return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
800 *(unw_word_t *)valuep) == UNW_ESUCCESS
801 ? _UVRSR_OK
802 : _UVRSR_FAILED;
803 case _UVRSC_WMMXD:
804 if (representation != _UVRSD_DOUBLE || regno > 31)
805 return _UVRSR_FAILED;
806 return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
807 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
808 ? _UVRSR_OK
809 : _UVRSR_FAILED;
810 #else
811 case _UVRSC_WMMXC:
812 case _UVRSC_WMMXD:
813 break;
814 #endif
815 }
816 _LIBUNWIND_ABORT("unsupported register class");
817 }
818
819 static _Unwind_VRS_Result
_Unwind_VRS_Get_Internal(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)820 _Unwind_VRS_Get_Internal(_Unwind_Context *context,
821 _Unwind_VRS_RegClass regclass, uint32_t regno,
822 _Unwind_VRS_DataRepresentation representation,
823 void *valuep) {
824 unw_cursor_t *cursor = (unw_cursor_t *)context;
825 switch (regclass) {
826 case _UVRSC_CORE:
827 if (representation != _UVRSD_UINT32 || regno > 15)
828 return _UVRSR_FAILED;
829 return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
830 (unw_word_t *)valuep) == UNW_ESUCCESS
831 ? _UVRSR_OK
832 : _UVRSR_FAILED;
833 case _UVRSC_VFP:
834 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
835 return _UVRSR_FAILED;
836 if (representation == _UVRSD_VFPX) {
837 // Can only touch d0-15 with FSTMFDX.
838 if (regno > 15)
839 return _UVRSR_FAILED;
840 __unw_save_vfp_as_X(cursor);
841 } else {
842 if (regno > 31)
843 return _UVRSR_FAILED;
844 }
845 return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
846 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
847 ? _UVRSR_OK
848 : _UVRSR_FAILED;
849 #if defined(__ARM_WMMX)
850 case _UVRSC_WMMXC:
851 if (representation != _UVRSD_UINT32 || regno > 3)
852 return _UVRSR_FAILED;
853 return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
854 (unw_word_t *)valuep) == UNW_ESUCCESS
855 ? _UVRSR_OK
856 : _UVRSR_FAILED;
857 case _UVRSC_WMMXD:
858 if (representation != _UVRSD_DOUBLE || regno > 31)
859 return _UVRSR_FAILED;
860 return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
861 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
862 ? _UVRSR_OK
863 : _UVRSR_FAILED;
864 #else
865 case _UVRSC_WMMXC:
866 case _UVRSC_WMMXD:
867 break;
868 #endif
869 }
870 _LIBUNWIND_ABORT("unsupported register class");
871 }
872
873 _LIBUNWIND_EXPORT _Unwind_VRS_Result
_Unwind_VRS_Get(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)874 _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
875 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
876 void *valuep) {
877 _Unwind_VRS_Result result =
878 _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
879 valuep);
880 _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
881 "rep=%d, value=0x%llX, result = %d)",
882 static_cast<void *>(context), regclass, regno,
883 representation,
884 ValueAsBitPattern(representation, valuep), result);
885 return result;
886 }
887
888 _Unwind_VRS_Result
_Unwind_VRS_Pop(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t discriminator,_Unwind_VRS_DataRepresentation representation)889 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
890 uint32_t discriminator,
891 _Unwind_VRS_DataRepresentation representation) {
892 _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
893 "discriminator=%d, representation=%d)",
894 static_cast<void *>(context), regclass, discriminator,
895 representation);
896 switch (regclass) {
897 case _UVRSC_WMMXC:
898 #if !defined(__ARM_WMMX)
899 break;
900 #endif
901 case _UVRSC_CORE: {
902 if (representation != _UVRSD_UINT32)
903 return _UVRSR_FAILED;
904 // When popping SP from the stack, we don't want to override it from the
905 // computed new stack location. See EHABI #7.5.4 table 3.
906 bool poppedSP = false;
907 uint32_t* sp;
908 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
909 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
910 return _UVRSR_FAILED;
911 }
912 for (uint32_t i = 0; i < 16; ++i) {
913 if (!(discriminator & static_cast<uint32_t>(1 << i)))
914 continue;
915 uint32_t value = *sp++;
916 if (regclass == _UVRSC_CORE && i == 13)
917 poppedSP = true;
918 if (_Unwind_VRS_Set(context, regclass, i,
919 _UVRSD_UINT32, &value) != _UVRSR_OK) {
920 return _UVRSR_FAILED;
921 }
922 }
923 if (!poppedSP) {
924 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
925 _UVRSD_UINT32, &sp);
926 }
927 return _UVRSR_OK;
928 }
929 case _UVRSC_WMMXD:
930 #if !defined(__ARM_WMMX)
931 break;
932 #endif
933 case _UVRSC_VFP: {
934 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
935 return _UVRSR_FAILED;
936 uint32_t first = discriminator >> 16;
937 uint32_t count = discriminator & 0xffff;
938 uint32_t end = first+count;
939 uint32_t* sp;
940 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
941 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
942 return _UVRSR_FAILED;
943 }
944 // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
945 // format 1", which is equivalent to FSTMD + a padding word.
946 for (uint32_t i = first; i < end; ++i) {
947 // SP is only 32-bit aligned so don't copy 64-bit at a time.
948 uint64_t w0 = *sp++;
949 uint64_t w1 = *sp++;
950 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
951 uint64_t value = (w1 << 32) | w0;
952 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
953 uint64_t value = (w0 << 32) | w1;
954 #else
955 #error "Unable to determine endianess"
956 #endif
957 if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
958 _UVRSR_OK)
959 return _UVRSR_FAILED;
960 }
961 if (representation == _UVRSD_VFPX)
962 ++sp;
963 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
964 &sp);
965 }
966 }
967 _LIBUNWIND_ABORT("unsupported register class");
968 }
969
970 /// Called by personality handler during phase 2 to find the start of the
971 /// function.
972 _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetRegionStart(struct _Unwind_Context * context)973 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
974 unw_cursor_t *cursor = (unw_cursor_t *)context;
975 unw_proc_info_t frameInfo;
976 uintptr_t result = 0;
977 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
978 result = (uintptr_t)frameInfo.start_ip;
979 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
980 static_cast<void *>(context), (long long)result);
981 return result;
982 }
983
984
985 /// Called by personality handler during phase 2 if a foreign exception
986 // is caught.
987 _LIBUNWIND_EXPORT void
_Unwind_DeleteException(_Unwind_Exception * exception_object)988 _Unwind_DeleteException(_Unwind_Exception *exception_object) {
989 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
990 static_cast<void *>(exception_object));
991 if (exception_object->exception_cleanup != NULL)
992 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
993 exception_object);
994 }
995
996 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__gnu_unwind_frame(_Unwind_Exception * exception_object,struct _Unwind_Context * context)997 __gnu_unwind_frame(_Unwind_Exception *exception_object,
998 struct _Unwind_Context *context) {
999 unw_cursor_t *cursor = (unw_cursor_t *)context;
1000 if (__unw_step(cursor) != UNW_STEP_SUCCESS)
1001 return _URC_FAILURE;
1002 return _URC_OK;
1003 }
1004
1005 #endif // defined(_LIBUNWIND_ARM_EHABI)
1006