Christopher Ferris | 8642fcb | 2017-04-24 11:14:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _LIBUNWINDSTACK_DWARF_CFA_H |
| 18 | #define _LIBUNWINDSTACK_DWARF_CFA_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <stack> |
| 23 | #include <string> |
| 24 | #include <type_traits> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "DwarfError.h" |
| 28 | #include "DwarfLocation.h" |
| 29 | #include "DwarfMemory.h" |
| 30 | #include "DwarfStructs.h" |
| 31 | |
| 32 | // DWARF Standard home: http://dwarfstd.org/ |
| 33 | // This code is based on DWARF 4: http://http://dwarfstd.org/doc/DWARF4.pdf |
| 34 | // See section 6.4.2.1 for a description of the DW_CFA_xxx values. |
| 35 | |
| 36 | class DwarfCfaInfo { |
| 37 | public: |
| 38 | enum DisplayType : uint8_t { |
| 39 | DWARF_DISPLAY_NONE = 0, |
| 40 | DWARF_DISPLAY_REGISTER, |
| 41 | DWARF_DISPLAY_NUMBER, |
| 42 | DWARF_DISPLAY_SIGNED_NUMBER, |
| 43 | DWARF_DISPLAY_EVAL_BLOCK, |
| 44 | DWARF_DISPLAY_ADDRESS, |
| 45 | DWARF_DISPLAY_SET_LOC, |
| 46 | DWARF_DISPLAY_ADVANCE_LOC, |
| 47 | }; |
| 48 | |
| 49 | struct Info { |
| 50 | const char* name; |
| 51 | uint8_t supported_version; |
| 52 | uint8_t num_operands; |
| 53 | uint8_t operands[2]; |
| 54 | uint8_t display_operands[2]; |
| 55 | }; |
| 56 | |
| 57 | const static Info kTable[64]; |
| 58 | }; |
| 59 | |
| 60 | template <typename AddressType> |
| 61 | class DwarfCfa { |
| 62 | // Signed version of AddressType |
| 63 | typedef typename std::make_signed<AddressType>::type SignedType; |
| 64 | |
| 65 | public: |
| 66 | DwarfCfa(DwarfMemory* memory, const DwarfFDE* fde) : memory_(memory), fde_(fde) {} |
| 67 | virtual ~DwarfCfa() = default; |
| 68 | |
| 69 | bool GetLocationInfo(uint64_t pc, uint64_t start_offset, uint64_t end_offset, |
| 70 | dwarf_loc_regs_t* loc_regs); |
| 71 | |
| 72 | bool Log(uint32_t indent, uint64_t pc, uint64_t load_bias, uint64_t start_offset, |
| 73 | uint64_t end_offset); |
| 74 | |
| 75 | DwarfError last_error() { return last_error_; } |
| 76 | |
| 77 | AddressType cur_pc() { return cur_pc_; } |
| 78 | |
| 79 | void set_cie_loc_regs(const dwarf_loc_regs_t* cie_loc_regs) { cie_loc_regs_ = cie_loc_regs; } |
| 80 | |
| 81 | protected: |
| 82 | std::string GetOperandString(uint8_t operand, uint64_t value, uint64_t* cur_pc); |
| 83 | |
| 84 | bool LogOffsetRegisterString(uint32_t indent, uint64_t cfa_offset, uint8_t reg); |
| 85 | |
| 86 | bool LogInstruction(uint32_t indent, uint64_t cfa_offset, uint8_t op, uint64_t* cur_pc); |
| 87 | |
| 88 | private: |
| 89 | DwarfError last_error_; |
| 90 | DwarfMemory* memory_; |
| 91 | const DwarfFDE* fde_; |
| 92 | |
| 93 | AddressType cur_pc_; |
| 94 | const dwarf_loc_regs_t* cie_loc_regs_ = nullptr; |
| 95 | std::vector<AddressType> operands_; |
| 96 | std::stack<dwarf_loc_regs_t> loc_reg_state_; |
| 97 | |
| 98 | // CFA processing functions. |
| 99 | bool cfa_nop(dwarf_loc_regs_t*); |
| 100 | bool cfa_set_loc(dwarf_loc_regs_t*); |
| 101 | bool cfa_advance_loc(dwarf_loc_regs_t*); |
| 102 | bool cfa_offset(dwarf_loc_regs_t*); |
| 103 | bool cfa_restore(dwarf_loc_regs_t*); |
| 104 | bool cfa_undefined(dwarf_loc_regs_t*); |
| 105 | bool cfa_same_value(dwarf_loc_regs_t*); |
| 106 | bool cfa_register(dwarf_loc_regs_t*); |
| 107 | bool cfa_remember_state(dwarf_loc_regs_t*); |
| 108 | bool cfa_restore_state(dwarf_loc_regs_t*); |
| 109 | bool cfa_def_cfa(dwarf_loc_regs_t*); |
| 110 | bool cfa_def_cfa_register(dwarf_loc_regs_t*); |
| 111 | bool cfa_def_cfa_offset(dwarf_loc_regs_t*); |
| 112 | bool cfa_def_cfa_expression(dwarf_loc_regs_t*); |
| 113 | bool cfa_expression(dwarf_loc_regs_t*); |
| 114 | bool cfa_offset_extended_sf(dwarf_loc_regs_t*); |
| 115 | bool cfa_def_cfa_sf(dwarf_loc_regs_t*); |
| 116 | bool cfa_def_cfa_offset_sf(dwarf_loc_regs_t*); |
| 117 | bool cfa_val_offset(dwarf_loc_regs_t*); |
| 118 | bool cfa_val_offset_sf(dwarf_loc_regs_t*); |
| 119 | bool cfa_val_expression(dwarf_loc_regs_t*); |
| 120 | bool cfa_gnu_negative_offset_extended(dwarf_loc_regs_t*); |
| 121 | |
| 122 | using process_func = bool (DwarfCfa::*)(dwarf_loc_regs_t*); |
| 123 | constexpr static process_func kCallbackTable[64] = { |
| 124 | // 0x00 DW_CFA_nop |
| 125 | &DwarfCfa::cfa_nop, |
| 126 | // 0x01 DW_CFA_set_loc |
| 127 | &DwarfCfa::cfa_set_loc, |
| 128 | // 0x02 DW_CFA_advance_loc1 |
| 129 | &DwarfCfa::cfa_advance_loc, |
| 130 | // 0x03 DW_CFA_advance_loc2 |
| 131 | &DwarfCfa::cfa_advance_loc, |
| 132 | // 0x04 DW_CFA_advance_loc4 |
| 133 | &DwarfCfa::cfa_advance_loc, |
| 134 | // 0x05 DW_CFA_offset_extended |
| 135 | &DwarfCfa::cfa_offset, |
| 136 | // 0x06 DW_CFA_restore_extended |
| 137 | &DwarfCfa::cfa_restore, |
| 138 | // 0x07 DW_CFA_undefined |
| 139 | &DwarfCfa::cfa_undefined, |
| 140 | // 0x08 DW_CFA_same_value |
| 141 | &DwarfCfa::cfa_same_value, |
| 142 | // 0x09 DW_CFA_register |
| 143 | &DwarfCfa::cfa_register, |
| 144 | // 0x0a DW_CFA_remember_state |
| 145 | &DwarfCfa::cfa_remember_state, |
| 146 | // 0x0b DW_CFA_restore_state |
| 147 | &DwarfCfa::cfa_restore_state, |
| 148 | // 0x0c DW_CFA_def_cfa |
| 149 | &DwarfCfa::cfa_def_cfa, |
| 150 | // 0x0d DW_CFA_def_cfa_register |
| 151 | &DwarfCfa::cfa_def_cfa_register, |
| 152 | // 0x0e DW_CFA_def_cfa_offset |
| 153 | &DwarfCfa::cfa_def_cfa_offset, |
| 154 | // 0x0f DW_CFA_def_cfa_expression |
| 155 | &DwarfCfa::cfa_def_cfa_expression, |
| 156 | // 0x10 DW_CFA_expression |
| 157 | &DwarfCfa::cfa_expression, |
| 158 | // 0x11 DW_CFA_offset_extended_sf |
| 159 | &DwarfCfa::cfa_offset_extended_sf, |
| 160 | // 0x12 DW_CFA_def_cfa_sf |
| 161 | &DwarfCfa::cfa_def_cfa_sf, |
| 162 | // 0x13 DW_CFA_def_cfa_offset_sf |
| 163 | &DwarfCfa::cfa_def_cfa_offset_sf, |
| 164 | // 0x14 DW_CFA_val_offset |
| 165 | &DwarfCfa::cfa_val_offset, |
| 166 | // 0x15 DW_CFA_val_offset_sf |
| 167 | &DwarfCfa::cfa_val_offset_sf, |
| 168 | // 0x16 DW_CFA_val_expression |
| 169 | &DwarfCfa::cfa_val_expression, |
| 170 | // 0x17 illegal cfa |
| 171 | nullptr, |
| 172 | // 0x18 illegal cfa |
| 173 | nullptr, |
| 174 | // 0x19 illegal cfa |
| 175 | nullptr, |
| 176 | // 0x1a illegal cfa |
| 177 | nullptr, |
| 178 | // 0x1b illegal cfa |
| 179 | nullptr, |
| 180 | // 0x1c DW_CFA_lo_user (Treat this as illegal) |
| 181 | nullptr, |
| 182 | // 0x1d illegal cfa |
| 183 | nullptr, |
| 184 | // 0x1e illegal cfa |
| 185 | nullptr, |
| 186 | // 0x1f illegal cfa |
| 187 | nullptr, |
| 188 | // 0x20 illegal cfa |
| 189 | nullptr, |
| 190 | // 0x21 illegal cfa |
| 191 | nullptr, |
| 192 | // 0x22 illegal cfa |
| 193 | nullptr, |
| 194 | // 0x23 illegal cfa |
| 195 | nullptr, |
| 196 | // 0x24 illegal cfa |
| 197 | nullptr, |
| 198 | // 0x25 illegal cfa |
| 199 | nullptr, |
| 200 | // 0x26 illegal cfa |
| 201 | nullptr, |
| 202 | // 0x27 illegal cfa |
| 203 | nullptr, |
| 204 | // 0x28 illegal cfa |
| 205 | nullptr, |
| 206 | // 0x29 illegal cfa |
| 207 | nullptr, |
| 208 | // 0x2a illegal cfa |
| 209 | nullptr, |
| 210 | // 0x2b illegal cfa |
| 211 | nullptr, |
| 212 | // 0x2c illegal cfa |
| 213 | nullptr, |
| 214 | // 0x2d DW_CFA_GNU_window_save (Treat this as illegal) |
| 215 | nullptr, |
| 216 | // 0x2e DW_CFA_GNU_args_size |
| 217 | &DwarfCfa::cfa_nop, |
| 218 | // 0x2f DW_CFA_GNU_negative_offset_extended |
| 219 | &DwarfCfa::cfa_gnu_negative_offset_extended, |
| 220 | // 0x30 illegal cfa |
| 221 | nullptr, |
| 222 | // 0x31 illegal cfa |
| 223 | nullptr, |
| 224 | // 0x32 illegal cfa |
| 225 | nullptr, |
| 226 | // 0x33 illegal cfa |
| 227 | nullptr, |
| 228 | // 0x34 illegal cfa |
| 229 | nullptr, |
| 230 | // 0x35 illegal cfa |
| 231 | nullptr, |
| 232 | // 0x36 illegal cfa |
| 233 | nullptr, |
| 234 | // 0x37 illegal cfa |
| 235 | nullptr, |
| 236 | // 0x38 illegal cfa |
| 237 | nullptr, |
| 238 | // 0x39 illegal cfa |
| 239 | nullptr, |
| 240 | // 0x3a illegal cfa |
| 241 | nullptr, |
| 242 | // 0x3b illegal cfa |
| 243 | nullptr, |
| 244 | // 0x3c illegal cfa |
| 245 | nullptr, |
| 246 | // 0x3d illegal cfa |
| 247 | nullptr, |
| 248 | // 0x3e illegal cfa |
| 249 | nullptr, |
| 250 | // 0x3f DW_CFA_hi_user (Treat this as illegal) |
| 251 | nullptr, |
| 252 | }; |
| 253 | }; |
| 254 | |
| 255 | #endif // _LIBUNWINDSTACK_DWARF_CFA_H |