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 #pragma once 18 19 #include <stdint.h> 20 21 #include <stack> 22 #include <string> 23 #include <type_traits> 24 #include <vector> 25 26 #include <unwindstack/DwarfError.h> 27 #include <unwindstack/DwarfLocation.h> 28 #include <unwindstack/DwarfMemory.h> 29 #include <unwindstack/DwarfStructs.h> 30 31 namespace unwindstack { 32 33 // Forward declarations. 34 enum ArchEnum : uint8_t; 35 36 // DWARF Standard home: http://dwarfstd.org/ 37 // This code is based on DWARF 4: http://http://dwarfstd.org/doc/DWARF4.pdf 38 // See section 6.4.2.1 for a description of the DW_CFA_xxx values. 39 40 class DwarfCfaInfo { 41 public: 42 enum DisplayType : uint8_t { 43 DWARF_DISPLAY_NONE = 0, 44 DWARF_DISPLAY_REGISTER, 45 DWARF_DISPLAY_NUMBER, 46 DWARF_DISPLAY_SIGNED_NUMBER, 47 DWARF_DISPLAY_EVAL_BLOCK, 48 DWARF_DISPLAY_ADDRESS, 49 DWARF_DISPLAY_SET_LOC, 50 DWARF_DISPLAY_ADVANCE_LOC, 51 }; 52 53 struct Info { 54 // It may seem cleaner to just change the type of 'name' to 'const char *'. 55 // However, having a pointer here would require relocation at runtime, 56 // causing 'kTable' to be placed in data.rel.ro section instead of rodata 57 // section, adding memory pressure to the system. Note that this is only 58 // safe because this is only used in C++ code. C++ standard, unlike C 59 // standard, mandates the array size to be large enough to hold the NULL 60 // terminator when initialized with a string literal. 61 const char name[36]; 62 uint8_t supported_version; 63 uint8_t num_operands; 64 uint8_t operands[2]; 65 uint8_t display_operands[2]; 66 }; 67 68 const static Info kTable[64]; 69 }; 70 71 template <typename AddressType> 72 class DwarfCfa { 73 // Signed version of AddressType 74 typedef typename std::make_signed<AddressType>::type SignedType; 75 76 public: DwarfCfa(DwarfMemory * memory,const DwarfFde * fde,ArchEnum arch)77 DwarfCfa(DwarfMemory* memory, const DwarfFde* fde, ArchEnum arch) 78 : memory_(memory), fde_(fde), arch_(arch) {} 79 virtual ~DwarfCfa() = default; 80 81 bool GetLocationInfo(uint64_t pc, uint64_t start_offset, uint64_t end_offset, 82 DwarfLocations* loc_regs); 83 84 bool Log(uint32_t indent, uint64_t pc, uint64_t start_offset, uint64_t end_offset); 85 last_error()86 const DwarfErrorData& last_error() { return last_error_; } LastErrorCode()87 DwarfErrorCode LastErrorCode() { return last_error_.code; } LastErrorAddress()88 uint64_t LastErrorAddress() { return last_error_.address; } 89 cur_pc()90 AddressType cur_pc() { return cur_pc_; } 91 set_cie_loc_regs(const DwarfLocations * cie_loc_regs)92 void set_cie_loc_regs(const DwarfLocations* cie_loc_regs) { cie_loc_regs_ = cie_loc_regs; } 93 94 protected: 95 std::string GetOperandString(uint8_t operand, uint64_t value, uint64_t* cur_pc); 96 97 bool LogOffsetRegisterString(uint32_t indent, uint64_t cfa_offset, uint8_t reg); 98 99 bool LogInstruction(uint32_t indent, uint64_t cfa_offset, uint8_t op, uint64_t* cur_pc); 100 101 private: 102 DwarfErrorData last_error_; 103 DwarfMemory* memory_; 104 const DwarfFde* fde_; 105 ArchEnum arch_; 106 107 AddressType cur_pc_; 108 const DwarfLocations* cie_loc_regs_ = nullptr; 109 std::vector<AddressType> operands_; 110 std::stack<DwarfLocations> loc_reg_state_; 111 112 // CFA processing functions. 113 bool cfa_nop(DwarfLocations*); 114 bool cfa_set_loc(DwarfLocations*); 115 bool cfa_advance_loc(DwarfLocations*); 116 bool cfa_offset(DwarfLocations*); 117 bool cfa_restore(DwarfLocations*); 118 bool cfa_undefined(DwarfLocations*); 119 bool cfa_same_value(DwarfLocations*); 120 bool cfa_register(DwarfLocations*); 121 bool cfa_remember_state(DwarfLocations*); 122 bool cfa_restore_state(DwarfLocations*); 123 bool cfa_def_cfa(DwarfLocations*); 124 bool cfa_def_cfa_register(DwarfLocations*); 125 bool cfa_def_cfa_offset(DwarfLocations*); 126 bool cfa_def_cfa_expression(DwarfLocations*); 127 bool cfa_expression(DwarfLocations*); 128 bool cfa_offset_extended_sf(DwarfLocations*); 129 bool cfa_def_cfa_sf(DwarfLocations*); 130 bool cfa_def_cfa_offset_sf(DwarfLocations*); 131 bool cfa_val_offset(DwarfLocations*); 132 bool cfa_val_offset_sf(DwarfLocations*); 133 bool cfa_val_expression(DwarfLocations*); 134 bool cfa_gnu_negative_offset_extended(DwarfLocations*); 135 bool cfa_aarch64_negate_ra_state(DwarfLocations*); 136 137 using process_func = bool (DwarfCfa::*)(DwarfLocations*); 138 constexpr static process_func kCallbackTable[64] = { 139 // 0x00 DW_CFA_nop 140 &DwarfCfa::cfa_nop, 141 // 0x01 DW_CFA_set_loc 142 &DwarfCfa::cfa_set_loc, 143 // 0x02 DW_CFA_advance_loc1 144 &DwarfCfa::cfa_advance_loc, 145 // 0x03 DW_CFA_advance_loc2 146 &DwarfCfa::cfa_advance_loc, 147 // 0x04 DW_CFA_advance_loc4 148 &DwarfCfa::cfa_advance_loc, 149 // 0x05 DW_CFA_offset_extended 150 &DwarfCfa::cfa_offset, 151 // 0x06 DW_CFA_restore_extended 152 &DwarfCfa::cfa_restore, 153 // 0x07 DW_CFA_undefined 154 &DwarfCfa::cfa_undefined, 155 // 0x08 DW_CFA_same_value 156 &DwarfCfa::cfa_same_value, 157 // 0x09 DW_CFA_register 158 &DwarfCfa::cfa_register, 159 // 0x0a DW_CFA_remember_state 160 &DwarfCfa::cfa_remember_state, 161 // 0x0b DW_CFA_restore_state 162 &DwarfCfa::cfa_restore_state, 163 // 0x0c DW_CFA_def_cfa 164 &DwarfCfa::cfa_def_cfa, 165 // 0x0d DW_CFA_def_cfa_register 166 &DwarfCfa::cfa_def_cfa_register, 167 // 0x0e DW_CFA_def_cfa_offset 168 &DwarfCfa::cfa_def_cfa_offset, 169 // 0x0f DW_CFA_def_cfa_expression 170 &DwarfCfa::cfa_def_cfa_expression, 171 // 0x10 DW_CFA_expression 172 &DwarfCfa::cfa_expression, 173 // 0x11 DW_CFA_offset_extended_sf 174 &DwarfCfa::cfa_offset_extended_sf, 175 // 0x12 DW_CFA_def_cfa_sf 176 &DwarfCfa::cfa_def_cfa_sf, 177 // 0x13 DW_CFA_def_cfa_offset_sf 178 &DwarfCfa::cfa_def_cfa_offset_sf, 179 // 0x14 DW_CFA_val_offset 180 &DwarfCfa::cfa_val_offset, 181 // 0x15 DW_CFA_val_offset_sf 182 &DwarfCfa::cfa_val_offset_sf, 183 // 0x16 DW_CFA_val_expression 184 &DwarfCfa::cfa_val_expression, 185 // 0x17 illegal cfa 186 nullptr, 187 // 0x18 illegal cfa 188 nullptr, 189 // 0x19 illegal cfa 190 nullptr, 191 // 0x1a illegal cfa 192 nullptr, 193 // 0x1b illegal cfa 194 nullptr, 195 // 0x1c DW_CFA_lo_user (Treat this as illegal) 196 nullptr, 197 // 0x1d illegal cfa 198 nullptr, 199 // 0x1e illegal cfa 200 nullptr, 201 // 0x1f illegal cfa 202 nullptr, 203 // 0x20 illegal cfa 204 nullptr, 205 // 0x21 illegal cfa 206 nullptr, 207 // 0x22 illegal cfa 208 nullptr, 209 // 0x23 illegal cfa 210 nullptr, 211 // 0x24 illegal cfa 212 nullptr, 213 // 0x25 illegal cfa 214 nullptr, 215 // 0x26 illegal cfa 216 nullptr, 217 // 0x27 illegal cfa 218 nullptr, 219 // 0x28 illegal cfa 220 nullptr, 221 // 0x29 illegal cfa 222 nullptr, 223 // 0x2a illegal cfa 224 nullptr, 225 // 0x2b illegal cfa 226 nullptr, 227 // 0x2c illegal cfa 228 nullptr, 229 // 0x2d DW_CFA_AARCH64_negate_ra_state (aarch64 only) 230 // DW_CFA_GNU_window_save on other architectures. 231 &DwarfCfa::cfa_aarch64_negate_ra_state, 232 // 0x2e DW_CFA_GNU_args_size 233 &DwarfCfa::cfa_nop, 234 // 0x2f DW_CFA_GNU_negative_offset_extended 235 &DwarfCfa::cfa_gnu_negative_offset_extended, 236 // 0x30 illegal cfa 237 nullptr, 238 // 0x31 illegal cfa 239 nullptr, 240 // 0x32 illegal cfa 241 nullptr, 242 // 0x33 illegal cfa 243 nullptr, 244 // 0x34 illegal cfa 245 nullptr, 246 // 0x35 illegal cfa 247 nullptr, 248 // 0x36 illegal cfa 249 nullptr, 250 // 0x37 illegal cfa 251 nullptr, 252 // 0x38 illegal cfa 253 nullptr, 254 // 0x39 illegal cfa 255 nullptr, 256 // 0x3a illegal cfa 257 nullptr, 258 // 0x3b illegal cfa 259 nullptr, 260 // 0x3c illegal cfa 261 nullptr, 262 // 0x3d illegal cfa 263 nullptr, 264 // 0x3e illegal cfa 265 nullptr, 266 // 0x3f DW_CFA_hi_user (Treat this as illegal) 267 nullptr, 268 }; 269 }; 270 271 } // namespace unwindstack 272