Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include <stdint.h> |
| 18 | |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 19 | #include <unwindstack/DwarfError.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 20 | #include <unwindstack/DwarfLocation.h> |
| 21 | #include <unwindstack/DwarfMemory.h> |
| 22 | #include <unwindstack/DwarfSection.h> |
| 23 | #include <unwindstack/DwarfStructs.h> |
| 24 | #include <unwindstack/Log.h> |
| 25 | #include <unwindstack/Memory.h> |
| 26 | #include <unwindstack/Regs.h> |
| 27 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 28 | #include "DwarfCfa.h" |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 29 | #include "DwarfDebugFrame.h" |
| 30 | #include "DwarfEhFrame.h" |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 31 | #include "DwarfEncoding.h" |
| 32 | #include "DwarfOp.h" |
| 33 | #include "RegsInfo.h" |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 34 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 35 | namespace unwindstack { |
| 36 | |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 37 | DwarfSection::DwarfSection(Memory* memory) : memory_(memory) {} |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 38 | |
| 39 | const DwarfFde* DwarfSection::GetFdeFromPc(uint64_t pc) { |
| 40 | uint64_t fde_offset; |
| 41 | if (!GetFdeOffsetFromPc(pc, &fde_offset)) { |
| 42 | return nullptr; |
| 43 | } |
| 44 | const DwarfFde* fde = GetFdeFromOffset(fde_offset); |
Christopher Ferris | 13b8665 | 2017-11-05 14:01:43 -0800 | [diff] [blame] | 45 | if (fde == nullptr) { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 49 | // Guaranteed pc >= pc_start, need to check pc in the fde range. |
| 50 | if (pc < fde->pc_end) { |
| 51 | return fde; |
| 52 | } |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 53 | last_error_.code = DWARF_ERROR_ILLEGAL_STATE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 54 | return nullptr; |
| 55 | } |
| 56 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 57 | bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 58 | last_error_.code = DWARF_ERROR_NONE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 59 | const DwarfFde* fde = GetFdeFromPc(pc); |
| 60 | if (fde == nullptr || fde->cie == nullptr) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 61 | last_error_.code = DWARF_ERROR_ILLEGAL_STATE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // Now get the location information for this pc. |
| 66 | dwarf_loc_regs_t loc_regs; |
| 67 | if (!GetCfaLocationInfo(pc, fde, &loc_regs)) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // Now eval the actual registers. |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 72 | return Eval(fde->cie, process_memory, loc_regs, regs, finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | template <typename AddressType> |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 76 | bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, Memory* regular_memory, |
| 77 | AddressType* value, |
| 78 | RegsInfo<AddressType>* regs_info, |
| 79 | bool* is_dex_pc) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 80 | DwarfOp<AddressType> op(&memory_, regular_memory); |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 81 | op.set_regs_info(regs_info); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 82 | |
| 83 | // Need to evaluate the op data. |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 84 | uint64_t end = loc.values[1]; |
| 85 | uint64_t start = end - loc.values[0]; |
| 86 | if (!op.Eval(start, end)) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 87 | last_error_ = op.last_error(); |
| 88 | return false; |
| 89 | } |
| 90 | if (op.StackSize() == 0) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 91 | last_error_.code = DWARF_ERROR_ILLEGAL_STATE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | // We don't support an expression that evaluates to a register number. |
| 95 | if (op.is_register()) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 96 | last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | *value = op.StackAt(0); |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 100 | if (is_dex_pc != nullptr && op.dex_pc_set()) { |
| 101 | *is_dex_pc = true; |
| 102 | } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | template <typename AddressType> |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 107 | struct EvalInfo { |
| 108 | const dwarf_loc_regs_t* loc_regs; |
| 109 | const DwarfCie* cie; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 110 | Memory* regular_memory; |
| 111 | AddressType cfa; |
| 112 | bool return_address_undefined = false; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 113 | RegsInfo<AddressType> regs_info; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | template <typename AddressType> |
| 117 | bool DwarfSectionImpl<AddressType>::EvalRegister(const DwarfLocation* loc, uint32_t reg, |
| 118 | AddressType* reg_ptr, void* info) { |
| 119 | EvalInfo<AddressType>* eval_info = reinterpret_cast<EvalInfo<AddressType>*>(info); |
| 120 | Memory* regular_memory = eval_info->regular_memory; |
| 121 | switch (loc->type) { |
| 122 | case DWARF_LOCATION_OFFSET: |
| 123 | if (!regular_memory->ReadFully(eval_info->cfa + loc->values[0], reg_ptr, sizeof(AddressType))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 124 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 125 | last_error_.address = eval_info->cfa + loc->values[0]; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | break; |
| 129 | case DWARF_LOCATION_VAL_OFFSET: |
| 130 | *reg_ptr = eval_info->cfa + loc->values[0]; |
| 131 | break; |
| 132 | case DWARF_LOCATION_REGISTER: { |
| 133 | uint32_t cur_reg = loc->values[0]; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 134 | if (cur_reg >= eval_info->regs_info.Total()) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 135 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 136 | return false; |
| 137 | } |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 138 | *reg_ptr = eval_info->regs_info.Get(cur_reg) + loc->values[1]; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | case DWARF_LOCATION_EXPRESSION: |
| 142 | case DWARF_LOCATION_VAL_EXPRESSION: { |
| 143 | AddressType value; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 144 | bool is_dex_pc = false; |
| 145 | if (!EvalExpression(*loc, regular_memory, &value, &eval_info->regs_info, &is_dex_pc)) { |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | if (loc->type == DWARF_LOCATION_EXPRESSION) { |
| 149 | if (!regular_memory->ReadFully(value, reg_ptr, sizeof(AddressType))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 150 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 151 | last_error_.address = value; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | } else { |
| 155 | *reg_ptr = value; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 156 | if (is_dex_pc) { |
| 157 | eval_info->regs_info.regs->set_dex_pc(value); |
| 158 | } |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 159 | } |
| 160 | break; |
| 161 | } |
| 162 | case DWARF_LOCATION_UNDEFINED: |
| 163 | if (reg == eval_info->cie->return_address_register) { |
| 164 | eval_info->return_address_undefined = true; |
| 165 | } |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | template <typename AddressType> |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 174 | bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory, |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 175 | const dwarf_loc_regs_t& loc_regs, Regs* regs, |
| 176 | bool* finished) { |
Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 177 | RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 178 | if (cie->return_address_register >= cur_regs->total_regs()) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 179 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // Get the cfa value; |
| 184 | auto cfa_entry = loc_regs.find(CFA_REG); |
| 185 | if (cfa_entry == loc_regs.end()) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 186 | last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 190 | // Always set the dex pc to zero when evaluating. |
| 191 | cur_regs->set_dex_pc(0); |
| 192 | |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 193 | EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs, |
| 194 | .cie = cie, |
| 195 | .regular_memory = regular_memory, |
| 196 | .regs_info = RegsInfo<AddressType>(cur_regs)}; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 197 | const DwarfLocation* loc = &cfa_entry->second; |
| 198 | // Only a few location types are valid for the cfa. |
| 199 | switch (loc->type) { |
| 200 | case DWARF_LOCATION_REGISTER: |
| 201 | if (loc->values[0] >= cur_regs->total_regs()) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 202 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 203 | return false; |
| 204 | } |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 205 | eval_info.cfa = (*cur_regs)[loc->values[0]]; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 206 | eval_info.cfa += loc->values[1]; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 207 | break; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 208 | case DWARF_LOCATION_VAL_EXPRESSION: { |
| 209 | AddressType value; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 210 | if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
David Srbecky | 3692f25 | 2018-03-08 16:57:19 +0000 | [diff] [blame] | 213 | // There is only one type of valid expression for CFA evaluation. |
| 214 | eval_info.cfa = value; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | default: |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 218 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 222 | for (const auto& entry : loc_regs) { |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 223 | uint32_t reg = entry.first; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 224 | // Already handled the CFA register. |
| 225 | if (reg == CFA_REG) continue; |
| 226 | |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 227 | AddressType* reg_ptr; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 228 | if (reg >= cur_regs->total_regs()) { |
| 229 | // Skip this unknown register. |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 230 | continue; |
| 231 | } |
| 232 | |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 233 | reg_ptr = eval_info.regs_info.Save(reg); |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 234 | if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) { |
| 235 | return false; |
| 236 | } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Find the return address location. |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 240 | if (eval_info.return_address_undefined) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 241 | cur_regs->set_pc(0); |
| 242 | } else { |
| 243 | cur_regs->set_pc((*cur_regs)[cie->return_address_register]); |
| 244 | } |
Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 245 | |
| 246 | // If the pc was set to zero, consider this the final frame. |
| 247 | *finished = (cur_regs->pc() == 0) ? true : false; |
| 248 | |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 249 | cur_regs->set_sp(eval_info.cfa); |
Christopher Ferris | fda7edd | 2017-10-31 16:10:42 -0700 | [diff] [blame] | 250 | |
| 251 | return true; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | template <typename AddressType> |
| 255 | const DwarfCie* DwarfSectionImpl<AddressType>::GetCie(uint64_t offset) { |
| 256 | auto cie_entry = cie_entries_.find(offset); |
| 257 | if (cie_entry != cie_entries_.end()) { |
| 258 | return &cie_entry->second; |
| 259 | } |
| 260 | DwarfCie* cie = &cie_entries_[offset]; |
| 261 | memory_.set_cur_offset(offset); |
| 262 | if (!FillInCie(cie)) { |
| 263 | // Erase the cached entry. |
| 264 | cie_entries_.erase(offset); |
| 265 | return nullptr; |
| 266 | } |
| 267 | return cie; |
| 268 | } |
| 269 | |
| 270 | template <typename AddressType> |
| 271 | bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) { |
| 272 | uint32_t length32; |
| 273 | if (!memory_.ReadBytes(&length32, sizeof(length32))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 274 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 275 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 278 | // Set the default for the lsda encoding. |
| 279 | cie->lsda_encoding = DW_EH_PE_omit; |
| 280 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 281 | if (length32 == static_cast<uint32_t>(-1)) { |
| 282 | // 64 bit Cie |
| 283 | uint64_t length64; |
| 284 | if (!memory_.ReadBytes(&length64, sizeof(length64))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 285 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 286 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
| 290 | cie->cfa_instructions_end = memory_.cur_offset() + length64; |
| 291 | cie->fde_address_encoding = DW_EH_PE_sdata8; |
| 292 | |
| 293 | uint64_t cie_id; |
| 294 | if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 295 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 296 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 297 | return false; |
| 298 | } |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 299 | if (cie_id != cie64_value_) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 300 | // This is not a Cie, something has gone horribly wrong. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 301 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | } else { |
| 305 | // 32 bit Cie |
| 306 | cie->cfa_instructions_end = memory_.cur_offset() + length32; |
| 307 | cie->fde_address_encoding = DW_EH_PE_sdata4; |
| 308 | |
| 309 | uint32_t cie_id; |
| 310 | if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 311 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 312 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 313 | return false; |
| 314 | } |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 315 | if (cie_id != cie32_value_) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 316 | // This is not a Cie, something has gone horribly wrong. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 317 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 323 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 324 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
| 328 | if (cie->version != 1 && cie->version != 3 && cie->version != 4) { |
| 329 | // Unrecognized version. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 330 | last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
| 334 | // Read the augmentation string. |
| 335 | char aug_value; |
| 336 | do { |
| 337 | if (!memory_.ReadBytes(&aug_value, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 338 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 339 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | cie->augmentation_string.push_back(aug_value); |
| 343 | } while (aug_value != '\0'); |
| 344 | |
| 345 | if (cie->version == 4) { |
| 346 | // Skip the Address Size field since we only use it for validation. |
| 347 | memory_.set_cur_offset(memory_.cur_offset() + 1); |
| 348 | |
| 349 | // Segment Size |
| 350 | if (!memory_.ReadBytes(&cie->segment_size, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 351 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 352 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Code Alignment Factor |
| 358 | if (!memory_.ReadULEB128(&cie->code_alignment_factor)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 359 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 360 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | |
| 364 | // Data Alignment Factor |
| 365 | if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 366 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 367 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | |
| 371 | if (cie->version == 1) { |
| 372 | // Return Address is a single byte. |
| 373 | uint8_t return_address_register; |
| 374 | if (!memory_.ReadBytes(&return_address_register, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 375 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 376 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | cie->return_address_register = return_address_register; |
| 380 | } else if (!memory_.ReadULEB128(&cie->return_address_register)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 381 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 382 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | |
| 386 | if (cie->augmentation_string[0] != 'z') { |
| 387 | cie->cfa_instructions_offset = memory_.cur_offset(); |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | uint64_t aug_length; |
| 392 | if (!memory_.ReadULEB128(&aug_length)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 393 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 394 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | cie->cfa_instructions_offset = memory_.cur_offset() + aug_length; |
| 398 | |
| 399 | for (size_t i = 1; i < cie->augmentation_string.size(); i++) { |
| 400 | switch (cie->augmentation_string[i]) { |
| 401 | case 'L': |
| 402 | if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 403 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 404 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | break; |
| 408 | case 'P': { |
| 409 | uint8_t encoding; |
| 410 | if (!memory_.ReadBytes(&encoding, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 411 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 412 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 416 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 417 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | } break; |
| 421 | case 'R': |
| 422 | if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 423 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 424 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | template <typename AddressType> |
| 434 | const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) { |
| 435 | auto fde_entry = fde_entries_.find(offset); |
| 436 | if (fde_entry != fde_entries_.end()) { |
| 437 | return &fde_entry->second; |
| 438 | } |
| 439 | DwarfFde* fde = &fde_entries_[offset]; |
| 440 | memory_.set_cur_offset(offset); |
| 441 | if (!FillInFde(fde)) { |
| 442 | fde_entries_.erase(offset); |
| 443 | return nullptr; |
| 444 | } |
| 445 | return fde; |
| 446 | } |
| 447 | |
| 448 | template <typename AddressType> |
| 449 | bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) { |
| 450 | uint32_t length32; |
| 451 | if (!memory_.ReadBytes(&length32, sizeof(length32))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 452 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 453 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 454 | return false; |
| 455 | } |
| 456 | |
| 457 | if (length32 == static_cast<uint32_t>(-1)) { |
| 458 | // 64 bit Fde. |
| 459 | uint64_t length64; |
| 460 | if (!memory_.ReadBytes(&length64, sizeof(length64))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 461 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 462 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 463 | return false; |
| 464 | } |
| 465 | fde->cfa_instructions_end = memory_.cur_offset() + length64; |
| 466 | |
| 467 | uint64_t value64; |
| 468 | if (!memory_.ReadBytes(&value64, sizeof(value64))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 469 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 470 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 471 | return false; |
| 472 | } |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 473 | if (value64 == cie64_value_) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 474 | // This is a Cie, this means something has gone wrong. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 475 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
| 479 | // Get the Cie pointer, which is necessary to properly read the rest of |
| 480 | // of the Fde information. |
| 481 | fde->cie_offset = GetCieOffsetFromFde64(value64); |
| 482 | } else { |
| 483 | // 32 bit Fde. |
| 484 | fde->cfa_instructions_end = memory_.cur_offset() + length32; |
| 485 | |
| 486 | uint32_t value32; |
| 487 | if (!memory_.ReadBytes(&value32, sizeof(value32))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 488 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 489 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 490 | return false; |
| 491 | } |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 492 | if (value32 == cie32_value_) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 493 | // This is a Cie, this means something has gone wrong. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 494 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 495 | return false; |
| 496 | } |
| 497 | |
| 498 | // Get the Cie pointer, which is necessary to properly read the rest of |
| 499 | // of the Fde information. |
| 500 | fde->cie_offset = GetCieOffsetFromFde32(value32); |
| 501 | } |
| 502 | uint64_t cur_offset = memory_.cur_offset(); |
| 503 | |
| 504 | const DwarfCie* cie = GetCie(fde->cie_offset); |
| 505 | if (cie == nullptr) { |
| 506 | return false; |
| 507 | } |
| 508 | fde->cie = cie; |
| 509 | |
| 510 | if (cie->segment_size != 0) { |
| 511 | // Skip over the segment selector for now. |
| 512 | cur_offset += cie->segment_size; |
| 513 | } |
| 514 | memory_.set_cur_offset(cur_offset); |
| 515 | |
| 516 | if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_start)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 517 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 518 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | fde->pc_start = AdjustPcFromFde(fde->pc_start); |
| 522 | |
| 523 | if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_end)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 524 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 525 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | fde->pc_end += fde->pc_start; |
| 529 | if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') { |
| 530 | // Augmentation Size |
| 531 | uint64_t aug_length; |
| 532 | if (!memory_.ReadULEB128(&aug_length)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 533 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 534 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 535 | return false; |
| 536 | } |
| 537 | uint64_t cur_offset = memory_.cur_offset(); |
| 538 | |
| 539 | if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 540 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 541 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | |
| 545 | // Set our position to after all of the augmentation data. |
| 546 | memory_.set_cur_offset(cur_offset + aug_length); |
| 547 | } |
| 548 | fde->cfa_instructions_offset = memory_.cur_offset(); |
| 549 | |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | template <typename AddressType> |
| 554 | bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde, |
| 555 | dwarf_loc_regs_t* loc_regs) { |
| 556 | DwarfCfa<AddressType> cfa(&memory_, fde); |
| 557 | |
| 558 | // Look for the cached copy of the cie data. |
| 559 | auto reg_entry = cie_loc_regs_.find(fde->cie_offset); |
| 560 | if (reg_entry == cie_loc_regs_.end()) { |
| 561 | if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end, |
| 562 | loc_regs)) { |
| 563 | last_error_ = cfa.last_error(); |
| 564 | return false; |
| 565 | } |
| 566 | cie_loc_regs_[fde->cie_offset] = *loc_regs; |
| 567 | } |
| 568 | cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]); |
| 569 | if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) { |
| 570 | last_error_ = cfa.last_error(); |
| 571 | return false; |
| 572 | } |
| 573 | return true; |
| 574 | } |
| 575 | |
| 576 | template <typename AddressType> |
| 577 | bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, uint64_t load_bias, |
| 578 | const DwarfFde* fde) { |
| 579 | DwarfCfa<AddressType> cfa(&memory_, fde); |
| 580 | |
| 581 | // Always print the cie information. |
| 582 | const DwarfCie* cie = fde->cie; |
| 583 | if (!cfa.Log(indent, pc, load_bias, cie->cfa_instructions_offset, cie->cfa_instructions_end)) { |
| 584 | last_error_ = cfa.last_error(); |
| 585 | return false; |
| 586 | } |
| 587 | if (!cfa.Log(indent, pc, load_bias, fde->cfa_instructions_offset, fde->cfa_instructions_end)) { |
| 588 | last_error_ = cfa.last_error(); |
| 589 | return false; |
| 590 | } |
| 591 | return true; |
| 592 | } |
| 593 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 594 | template <typename AddressType> |
| 595 | bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size) { |
| 596 | entries_offset_ = offset; |
| 597 | entries_end_ = offset + size; |
| 598 | |
| 599 | memory_.clear_func_offset(); |
| 600 | memory_.clear_text_offset(); |
| 601 | memory_.set_data_offset(offset); |
| 602 | memory_.set_cur_offset(offset); |
| 603 | memory_.set_pc_offset(offset); |
| 604 | |
| 605 | return CreateSortedFdeList(); |
| 606 | } |
| 607 | |
| 608 | template <typename AddressType> |
| 609 | bool DwarfSectionImpl<AddressType>::GetCieInfo(uint8_t* segment_size, uint8_t* encoding) { |
| 610 | uint8_t version; |
| 611 | if (!memory_.ReadBytes(&version, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 612 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 613 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 614 | return false; |
| 615 | } |
| 616 | // Read the augmentation string. |
| 617 | std::vector<char> aug_string; |
| 618 | char aug_value; |
| 619 | bool get_encoding = false; |
| 620 | do { |
| 621 | if (!memory_.ReadBytes(&aug_value, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 622 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 623 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 624 | return false; |
| 625 | } |
| 626 | if (aug_value == 'R') { |
| 627 | get_encoding = true; |
| 628 | } |
| 629 | aug_string.push_back(aug_value); |
| 630 | } while (aug_value != '\0'); |
| 631 | |
| 632 | if (version == 4) { |
| 633 | // Skip the Address Size field. |
| 634 | memory_.set_cur_offset(memory_.cur_offset() + 1); |
| 635 | |
| 636 | // Read the segment size. |
| 637 | if (!memory_.ReadBytes(segment_size, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 638 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 639 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 640 | return false; |
| 641 | } |
| 642 | } else { |
| 643 | *segment_size = 0; |
| 644 | } |
| 645 | |
| 646 | if (aug_string[0] != 'z' || !get_encoding) { |
| 647 | // No encoding |
| 648 | return true; |
| 649 | } |
| 650 | |
| 651 | // Skip code alignment factor |
| 652 | uint8_t value; |
| 653 | do { |
| 654 | if (!memory_.ReadBytes(&value, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 655 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 656 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 657 | return false; |
| 658 | } |
| 659 | } while (value & 0x80); |
| 660 | |
| 661 | // Skip data alignment factor |
| 662 | do { |
| 663 | if (!memory_.ReadBytes(&value, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 664 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 665 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | } while (value & 0x80); |
| 669 | |
| 670 | if (version == 1) { |
| 671 | // Skip return address register. |
| 672 | memory_.set_cur_offset(memory_.cur_offset() + 1); |
| 673 | } else { |
| 674 | // Skip return address register. |
| 675 | do { |
| 676 | if (!memory_.ReadBytes(&value, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 677 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 678 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | } while (value & 0x80); |
| 682 | } |
| 683 | |
| 684 | // Skip the augmentation length. |
| 685 | do { |
| 686 | if (!memory_.ReadBytes(&value, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 687 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 688 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 689 | return false; |
| 690 | } |
| 691 | } while (value & 0x80); |
| 692 | |
| 693 | for (size_t i = 1; i < aug_string.size(); i++) { |
| 694 | if (aug_string[i] == 'R') { |
| 695 | if (!memory_.ReadBytes(encoding, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 696 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 697 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 698 | return false; |
| 699 | } |
| 700 | // Got the encoding, that's all we are looking for. |
| 701 | return true; |
| 702 | } else if (aug_string[i] == 'L') { |
| 703 | memory_.set_cur_offset(memory_.cur_offset() + 1); |
| 704 | } else if (aug_string[i] == 'P') { |
| 705 | uint8_t encoding; |
| 706 | if (!memory_.ReadBytes(&encoding, 1)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 707 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 708 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 709 | return false; |
| 710 | } |
| 711 | uint64_t value; |
| 712 | if (!memory_.template ReadEncodedValue<AddressType>(encoding, &value)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 713 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 714 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 715 | return false; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // It should be impossible to get here. |
| 721 | abort(); |
| 722 | } |
| 723 | |
| 724 | template <typename AddressType> |
| 725 | bool DwarfSectionImpl<AddressType>::AddFdeInfo(uint64_t entry_offset, uint8_t segment_size, |
| 726 | uint8_t encoding) { |
| 727 | if (segment_size != 0) { |
| 728 | memory_.set_cur_offset(memory_.cur_offset() + 1); |
| 729 | } |
| 730 | |
| 731 | uint64_t start; |
| 732 | if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &start)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 733 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 734 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 735 | return false; |
| 736 | } |
| 737 | start = AdjustPcFromFde(start); |
| 738 | |
| 739 | uint64_t length; |
| 740 | if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &length)) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 741 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 742 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 743 | return false; |
| 744 | } |
| 745 | if (length != 0) { |
| 746 | fdes_.emplace_back(entry_offset, start, length); |
| 747 | } |
| 748 | |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | template <typename AddressType> |
| 753 | bool DwarfSectionImpl<AddressType>::CreateSortedFdeList() { |
| 754 | memory_.set_cur_offset(entries_offset_); |
| 755 | |
| 756 | // Loop through all of the entries and read just enough to create |
| 757 | // a sorted list of pcs. |
| 758 | // This code assumes that first comes the cie, then the fdes that |
| 759 | // it applies to. |
| 760 | uint64_t cie_offset = 0; |
| 761 | uint8_t address_encoding; |
| 762 | uint8_t segment_size; |
| 763 | while (memory_.cur_offset() < entries_end_) { |
| 764 | uint64_t cur_entry_offset = memory_.cur_offset(); |
| 765 | |
| 766 | // Figure out the entry length and type. |
| 767 | uint32_t value32; |
| 768 | if (!memory_.ReadBytes(&value32, sizeof(value32))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 769 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 770 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 771 | return false; |
| 772 | } |
| 773 | |
| 774 | uint64_t next_entry_offset; |
| 775 | if (value32 == static_cast<uint32_t>(-1)) { |
| 776 | uint64_t value64; |
| 777 | if (!memory_.ReadBytes(&value64, sizeof(value64))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 778 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 779 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 780 | return false; |
| 781 | } |
| 782 | next_entry_offset = memory_.cur_offset() + value64; |
| 783 | |
| 784 | // Read the Cie Id of a Cie or the pointer of the Fde. |
| 785 | if (!memory_.ReadBytes(&value64, sizeof(value64))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 786 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 787 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 788 | return false; |
| 789 | } |
| 790 | |
| 791 | if (value64 == cie64_value_) { |
| 792 | // Cie 64 bit |
| 793 | address_encoding = DW_EH_PE_sdata8; |
| 794 | if (!GetCieInfo(&segment_size, &address_encoding)) { |
| 795 | return false; |
| 796 | } |
| 797 | cie_offset = cur_entry_offset; |
| 798 | } else { |
| 799 | uint64_t last_cie_offset = GetCieOffsetFromFde64(value64); |
| 800 | if (last_cie_offset != cie_offset) { |
| 801 | // This means that this Fde is not following the Cie. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 802 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 803 | return false; |
| 804 | } |
| 805 | |
| 806 | // Fde 64 bit |
| 807 | if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) { |
| 808 | return false; |
| 809 | } |
| 810 | } |
| 811 | } else { |
| 812 | next_entry_offset = memory_.cur_offset() + value32; |
| 813 | |
| 814 | // Read the Cie Id of a Cie or the pointer of the Fde. |
| 815 | if (!memory_.ReadBytes(&value32, sizeof(value32))) { |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 816 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; |
| 817 | last_error_.address = memory_.cur_offset(); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 818 | return false; |
| 819 | } |
| 820 | |
| 821 | if (value32 == cie32_value_) { |
| 822 | // Cie 32 bit |
| 823 | address_encoding = DW_EH_PE_sdata4; |
| 824 | if (!GetCieInfo(&segment_size, &address_encoding)) { |
| 825 | return false; |
| 826 | } |
| 827 | cie_offset = cur_entry_offset; |
| 828 | } else { |
| 829 | uint64_t last_cie_offset = GetCieOffsetFromFde32(value32); |
| 830 | if (last_cie_offset != cie_offset) { |
| 831 | // This means that this Fde is not following the Cie. |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 832 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 833 | return false; |
| 834 | } |
| 835 | |
| 836 | // Fde 32 bit |
| 837 | if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) { |
| 838 | return false; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (next_entry_offset < memory_.cur_offset()) { |
Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 844 | // Simply consider the processing done in this case. |
| 845 | break; |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 846 | } |
| 847 | memory_.set_cur_offset(next_entry_offset); |
| 848 | } |
| 849 | |
| 850 | // Sort the entries. |
| 851 | std::sort(fdes_.begin(), fdes_.end(), [](const FdeInfo& a, const FdeInfo& b) { |
| 852 | if (a.start == b.start) return a.end < b.end; |
| 853 | return a.start < b.start; |
| 854 | }); |
| 855 | |
| 856 | fde_count_ = fdes_.size(); |
| 857 | |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | template <typename AddressType> |
| 862 | bool DwarfSectionImpl<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) { |
| 863 | if (fde_count_ == 0) { |
| 864 | return false; |
| 865 | } |
| 866 | |
| 867 | size_t first = 0; |
| 868 | size_t last = fde_count_; |
| 869 | while (first < last) { |
| 870 | size_t current = (first + last) / 2; |
| 871 | const FdeInfo* info = &fdes_[current]; |
| 872 | if (pc >= info->start && pc <= info->end) { |
| 873 | *fde_offset = info->offset; |
| 874 | return true; |
| 875 | } |
| 876 | |
| 877 | if (pc < info->start) { |
| 878 | last = current; |
| 879 | } else { |
| 880 | first = current + 1; |
| 881 | } |
| 882 | } |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | template <typename AddressType> |
| 887 | const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromIndex(size_t index) { |
| 888 | if (index >= fdes_.size()) { |
| 889 | return nullptr; |
| 890 | } |
| 891 | return this->GetFdeFromOffset(fdes_[index].offset); |
| 892 | } |
| 893 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 894 | // Explicitly instantiate DwarfSectionImpl |
| 895 | template class DwarfSectionImpl<uint32_t>; |
| 896 | template class DwarfSectionImpl<uint64_t>; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 897 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 898 | // Explicitly instantiate DwarfDebugFrame |
| 899 | template class DwarfDebugFrame<uint32_t>; |
| 900 | template class DwarfDebugFrame<uint64_t>; |
| 901 | |
| 902 | // Explicitly instantiate DwarfEhFrame |
| 903 | template class DwarfEhFrame<uint32_t>; |
| 904 | template class DwarfEhFrame<uint64_t>; |
| 905 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 906 | } // namespace unwindstack |