Ryan Prichard | fd8d687 | 2020-04-17 19:46:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | // Define assembler macros for generating DWARF CFI instructions that use DWARF expressions. |
| 30 | // Assemblers don't natively support DWARF expressions, so use the C preprocessor and assembler |
| 31 | // macros to lower them to .cfi_escape directives. |
| 32 | // |
| 33 | // Signal trampolines need to use DWARF expressions to record the locations of saved registers, |
| 34 | // because the offsets from the restored SP to the saved registers is variable. e.g. A signal frame |
| 35 | // can have optional FP/SIMD extensions, and there may be extra padding if the interrupted SP wasn't |
| 36 | // aligned. |
| 37 | |
| 38 | // DWARF constants. |
| 39 | #define DW_CFA_def_cfa_expression 0x0f |
| 40 | #define DW_CFA_expression 0x10 |
| 41 | #define DW_OP_breg0 0x70 |
| 42 | #define DW_OP_deref 0x06 |
| 43 | |
| 44 | // Return the size of a small uleb128 value: either 1 or 2 bytes |
| 45 | #define ULEB128_14BIT_SIZE(val) \ |
| 46 | (1 + (((val) > 0x7f) & 1)) |
| 47 | |
| 48 | // Return the size of a small sleb128 value: either 1 or 2 bytes |
| 49 | #define SLEB128_14BIT_SIZE(val) \ |
| 50 | (1 + (((val) < -0x40) & 1) + \ |
| 51 | (((val) > 0x3f) & 1) ) |
| 52 | |
| 53 | // Output a 1 or 2-byte CFI uleb128 absolute value. |
| 54 | .macro m_cfi_uleb128 val |
| 55 | .if (\val) < 0 || (\val) > 0x3fff |
| 56 | .error "m_cfi_uleb128 value is out of range (\val)" |
| 57 | .elseif (\val) > 0x7f |
| 58 | .cfi_escape ((\val) & 0x7f) | 0x80 |
| 59 | .cfi_escape (\val) >> 7 |
| 60 | .else |
| 61 | .cfi_escape (\val) |
| 62 | .endif |
| 63 | .endm |
| 64 | |
| 65 | // Output a 1 or 2-byte CFI sleb128 absolute value. |
| 66 | .macro m_cfi_sleb128 val |
| 67 | .if (\val) < -0x2000 || (\val) > 0x1fff |
| 68 | .error "m_cfi_sleb128 value is out of range (\val)" |
| 69 | .elseif (\val) < -0x40 || (\val) > 0x3f |
| 70 | .cfi_escape ((\val) & 0x7f) | 0x80 |
| 71 | .cfi_escape ((\val) >> 7) & 0x7f |
| 72 | .else |
| 73 | .cfi_escape (\val) & 0x7f |
| 74 | .endif |
| 75 | .endm |
| 76 | |
| 77 | .macro check_base_reg reg_no |
| 78 | .if (\reg_no) < 0 || (\reg_no) > 31 |
| 79 | .error "base register is out of range for DW_OP_breg0..DW_OP_breg31 (\reg_no)" |
| 80 | .endif |
| 81 | .endm |
| 82 | |
| 83 | // Set CFA to the expression, *(base_reg + offset) |
| 84 | .macro m_cfi_def_cfa_deref base_reg, offset |
| 85 | check_base_reg (\base_reg) |
| 86 | .cfi_escape DW_CFA_def_cfa_expression |
| 87 | m_cfi_uleb128 (1 + SLEB128_14BIT_SIZE(\offset) + 1) // size of DWARF expression in bytes |
| 88 | .cfi_escape DW_OP_breg0 + (\base_reg) // expr: 1 byte |
| 89 | m_cfi_sleb128 (\offset) // expr: 1 or 2 bytes |
| 90 | .cfi_escape DW_OP_deref // expr: 1 byte |
| 91 | .endm |
| 92 | |
| 93 | // Set the address of the register's previous value to the expression, (base_reg + offset) |
| 94 | .macro m_cfi_breg_offset dest_reg, base_reg, offset |
| 95 | check_base_reg (\base_reg) |
| 96 | .cfi_escape DW_CFA_expression |
| 97 | m_cfi_uleb128 (\dest_reg) |
| 98 | m_cfi_uleb128 (1 + SLEB128_14BIT_SIZE(\offset)) // size of DWARF expression in bytes |
| 99 | .cfi_escape DW_OP_breg0 + (\base_reg) // expr: 1 byte |
| 100 | m_cfi_sleb128 (\offset) // expr: 1 or 2 bytes |
| 101 | .endm |