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