| 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 | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 193 | AddressType prev_cfa = regs->sp(); | 
|  | 194 |  | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 195 | EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs, | 
|  | 196 | .cie = cie, | 
|  | 197 | .regular_memory = regular_memory, | 
|  | 198 | .regs_info = RegsInfo<AddressType>(cur_regs)}; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 199 | const DwarfLocation* loc = &cfa_entry->second; | 
|  | 200 | // Only a few location types are valid for the cfa. | 
|  | 201 | switch (loc->type) { | 
|  | 202 | case DWARF_LOCATION_REGISTER: | 
|  | 203 | if (loc->values[0] >= cur_regs->total_regs()) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 204 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 205 | return false; | 
|  | 206 | } | 
|  | 207 | // If the stack pointer register is the CFA, and the stack | 
|  | 208 | // pointer register does not have any associated location | 
|  | 209 | // information, use the current cfa value. | 
|  | 210 | if (regs->sp_reg() == loc->values[0] && loc_regs.count(regs->sp_reg()) == 0) { | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 211 | eval_info.cfa = prev_cfa; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 212 | } else { | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 213 | eval_info.cfa = (*cur_regs)[loc->values[0]]; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 214 | } | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 215 | eval_info.cfa += loc->values[1]; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 216 | break; | 
|  | 217 | case DWARF_LOCATION_EXPRESSION: | 
|  | 218 | case DWARF_LOCATION_VAL_EXPRESSION: { | 
|  | 219 | AddressType value; | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 220 | if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 221 | return false; | 
|  | 222 | } | 
|  | 223 | if (loc->type == DWARF_LOCATION_EXPRESSION) { | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 224 | if (!regular_memory->ReadFully(value, &eval_info.cfa, sizeof(AddressType))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 225 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 226 | last_error_.address = value; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 227 | return false; | 
|  | 228 | } | 
|  | 229 | } else { | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 230 | eval_info.cfa = value; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 231 | } | 
|  | 232 | break; | 
|  | 233 | } | 
|  | 234 | default: | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 235 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 236 | return false; | 
|  | 237 | } | 
|  | 238 |  | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 239 | for (const auto& entry : loc_regs) { | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 240 | uint32_t reg = entry.first; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 241 | // Already handled the CFA register. | 
|  | 242 | if (reg == CFA_REG) continue; | 
|  | 243 |  | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 244 | AddressType* reg_ptr; | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 245 | if (reg >= cur_regs->total_regs()) { | 
|  | 246 | // Skip this unknown register. | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 247 | continue; | 
|  | 248 | } | 
|  | 249 |  | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 250 | reg_ptr = eval_info.regs_info.Save(reg); | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 251 | if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) { | 
|  | 252 | return false; | 
|  | 253 | } | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 254 | } | 
|  | 255 |  | 
|  | 256 | // Find the return address location. | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 257 | if (eval_info.return_address_undefined) { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 258 | cur_regs->set_pc(0); | 
|  | 259 | } else { | 
|  | 260 | cur_regs->set_pc((*cur_regs)[cie->return_address_register]); | 
|  | 261 | } | 
| Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 262 |  | 
|  | 263 | // If the pc was set to zero, consider this the final frame. | 
|  | 264 | *finished = (cur_regs->pc() == 0) ? true : false; | 
|  | 265 |  | 
| Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 266 | cur_regs->set_sp(eval_info.cfa); | 
| Christopher Ferris | fda7edd | 2017-10-31 16:10:42 -0700 | [diff] [blame] | 267 |  | 
|  | 268 | return true; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 269 | } | 
|  | 270 |  | 
|  | 271 | template <typename AddressType> | 
|  | 272 | const DwarfCie* DwarfSectionImpl<AddressType>::GetCie(uint64_t offset) { | 
|  | 273 | auto cie_entry = cie_entries_.find(offset); | 
|  | 274 | if (cie_entry != cie_entries_.end()) { | 
|  | 275 | return &cie_entry->second; | 
|  | 276 | } | 
|  | 277 | DwarfCie* cie = &cie_entries_[offset]; | 
|  | 278 | memory_.set_cur_offset(offset); | 
|  | 279 | if (!FillInCie(cie)) { | 
|  | 280 | // Erase the cached entry. | 
|  | 281 | cie_entries_.erase(offset); | 
|  | 282 | return nullptr; | 
|  | 283 | } | 
|  | 284 | return cie; | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | template <typename AddressType> | 
|  | 288 | bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) { | 
|  | 289 | uint32_t length32; | 
|  | 290 | if (!memory_.ReadBytes(&length32, sizeof(length32))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 291 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 292 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 293 | return false; | 
|  | 294 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 295 | // Set the default for the lsda encoding. | 
|  | 296 | cie->lsda_encoding = DW_EH_PE_omit; | 
|  | 297 |  | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 298 | if (length32 == static_cast<uint32_t>(-1)) { | 
|  | 299 | // 64 bit Cie | 
|  | 300 | uint64_t length64; | 
|  | 301 | if (!memory_.ReadBytes(&length64, sizeof(length64))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 302 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 303 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 304 | return false; | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | cie->cfa_instructions_end = memory_.cur_offset() + length64; | 
|  | 308 | cie->fde_address_encoding = DW_EH_PE_sdata8; | 
|  | 309 |  | 
|  | 310 | uint64_t cie_id; | 
|  | 311 | if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 312 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 313 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 314 | return false; | 
|  | 315 | } | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 316 | if (cie_id != cie64_value_) { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 317 | // This is not a Cie, something has gone horribly wrong. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 318 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 319 | return false; | 
|  | 320 | } | 
|  | 321 | } else { | 
|  | 322 | // 32 bit Cie | 
|  | 323 | cie->cfa_instructions_end = memory_.cur_offset() + length32; | 
|  | 324 | cie->fde_address_encoding = DW_EH_PE_sdata4; | 
|  | 325 |  | 
|  | 326 | uint32_t cie_id; | 
|  | 327 | if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 328 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 329 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 330 | return false; | 
|  | 331 | } | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 332 | if (cie_id != cie32_value_) { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 333 | // This is not a Cie, something has gone horribly wrong. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 334 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 335 | return false; | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 340 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 341 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 342 | return false; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | if (cie->version != 1 && cie->version != 3 && cie->version != 4) { | 
|  | 346 | // Unrecognized version. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 347 | last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 348 | return false; | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | // Read the augmentation string. | 
|  | 352 | char aug_value; | 
|  | 353 | do { | 
|  | 354 | if (!memory_.ReadBytes(&aug_value, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 355 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 356 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 357 | return false; | 
|  | 358 | } | 
|  | 359 | cie->augmentation_string.push_back(aug_value); | 
|  | 360 | } while (aug_value != '\0'); | 
|  | 361 |  | 
|  | 362 | if (cie->version == 4) { | 
|  | 363 | // Skip the Address Size field since we only use it for validation. | 
|  | 364 | memory_.set_cur_offset(memory_.cur_offset() + 1); | 
|  | 365 |  | 
|  | 366 | // Segment Size | 
|  | 367 | if (!memory_.ReadBytes(&cie->segment_size, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 368 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 369 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 370 | return false; | 
|  | 371 | } | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | // Code Alignment Factor | 
|  | 375 | if (!memory_.ReadULEB128(&cie->code_alignment_factor)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 376 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 377 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 378 | return false; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | // Data Alignment Factor | 
|  | 382 | if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 383 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 384 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 385 | return false; | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | if (cie->version == 1) { | 
|  | 389 | // Return Address is a single byte. | 
|  | 390 | uint8_t return_address_register; | 
|  | 391 | if (!memory_.ReadBytes(&return_address_register, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 392 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 393 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 394 | return false; | 
|  | 395 | } | 
|  | 396 | cie->return_address_register = return_address_register; | 
|  | 397 | } else if (!memory_.ReadULEB128(&cie->return_address_register)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 398 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 399 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 400 | return false; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | if (cie->augmentation_string[0] != 'z') { | 
|  | 404 | cie->cfa_instructions_offset = memory_.cur_offset(); | 
|  | 405 | return true; | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | uint64_t aug_length; | 
|  | 409 | if (!memory_.ReadULEB128(&aug_length)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 410 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 411 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 412 | return false; | 
|  | 413 | } | 
|  | 414 | cie->cfa_instructions_offset = memory_.cur_offset() + aug_length; | 
|  | 415 |  | 
|  | 416 | for (size_t i = 1; i < cie->augmentation_string.size(); i++) { | 
|  | 417 | switch (cie->augmentation_string[i]) { | 
|  | 418 | case 'L': | 
|  | 419 | if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 420 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 421 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 422 | return false; | 
|  | 423 | } | 
|  | 424 | break; | 
|  | 425 | case 'P': { | 
|  | 426 | uint8_t encoding; | 
|  | 427 | if (!memory_.ReadBytes(&encoding, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 428 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 429 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 430 | return false; | 
|  | 431 | } | 
|  | 432 | if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 433 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 434 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 435 | return false; | 
|  | 436 | } | 
|  | 437 | } break; | 
|  | 438 | case 'R': | 
|  | 439 | if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 440 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 441 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 442 | return false; | 
|  | 443 | } | 
|  | 444 | break; | 
|  | 445 | } | 
|  | 446 | } | 
|  | 447 | return true; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | template <typename AddressType> | 
|  | 451 | const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) { | 
|  | 452 | auto fde_entry = fde_entries_.find(offset); | 
|  | 453 | if (fde_entry != fde_entries_.end()) { | 
|  | 454 | return &fde_entry->second; | 
|  | 455 | } | 
|  | 456 | DwarfFde* fde = &fde_entries_[offset]; | 
|  | 457 | memory_.set_cur_offset(offset); | 
|  | 458 | if (!FillInFde(fde)) { | 
|  | 459 | fde_entries_.erase(offset); | 
|  | 460 | return nullptr; | 
|  | 461 | } | 
|  | 462 | return fde; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | template <typename AddressType> | 
|  | 466 | bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) { | 
|  | 467 | uint32_t length32; | 
|  | 468 | if (!memory_.ReadBytes(&length32, sizeof(length32))) { | 
| 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 | } | 
|  | 473 |  | 
|  | 474 | if (length32 == static_cast<uint32_t>(-1)) { | 
|  | 475 | // 64 bit Fde. | 
|  | 476 | uint64_t length64; | 
|  | 477 | if (!memory_.ReadBytes(&length64, sizeof(length64))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 478 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 479 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 480 | return false; | 
|  | 481 | } | 
|  | 482 | fde->cfa_instructions_end = memory_.cur_offset() + length64; | 
|  | 483 |  | 
|  | 484 | uint64_t value64; | 
|  | 485 | if (!memory_.ReadBytes(&value64, sizeof(value64))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 486 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 487 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 488 | return false; | 
|  | 489 | } | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 490 | if (value64 == cie64_value_) { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 491 | // This is a Cie, this means something has gone wrong. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 492 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 493 | return false; | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | // Get the Cie pointer, which is necessary to properly read the rest of | 
|  | 497 | // of the Fde information. | 
|  | 498 | fde->cie_offset = GetCieOffsetFromFde64(value64); | 
|  | 499 | } else { | 
|  | 500 | // 32 bit Fde. | 
|  | 501 | fde->cfa_instructions_end = memory_.cur_offset() + length32; | 
|  | 502 |  | 
|  | 503 | uint32_t value32; | 
|  | 504 | if (!memory_.ReadBytes(&value32, sizeof(value32))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 505 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 506 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 507 | return false; | 
|  | 508 | } | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 509 | if (value32 == cie32_value_) { | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 510 | // This is a Cie, this means something has gone wrong. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 511 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 512 | return false; | 
|  | 513 | } | 
|  | 514 |  | 
|  | 515 | // Get the Cie pointer, which is necessary to properly read the rest of | 
|  | 516 | // of the Fde information. | 
|  | 517 | fde->cie_offset = GetCieOffsetFromFde32(value32); | 
|  | 518 | } | 
|  | 519 | uint64_t cur_offset = memory_.cur_offset(); | 
|  | 520 |  | 
|  | 521 | const DwarfCie* cie = GetCie(fde->cie_offset); | 
|  | 522 | if (cie == nullptr) { | 
|  | 523 | return false; | 
|  | 524 | } | 
|  | 525 | fde->cie = cie; | 
|  | 526 |  | 
|  | 527 | if (cie->segment_size != 0) { | 
|  | 528 | // Skip over the segment selector for now. | 
|  | 529 | cur_offset += cie->segment_size; | 
|  | 530 | } | 
|  | 531 | memory_.set_cur_offset(cur_offset); | 
|  | 532 |  | 
|  | 533 | if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_start)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 534 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 535 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 536 | return false; | 
|  | 537 | } | 
|  | 538 | fde->pc_start = AdjustPcFromFde(fde->pc_start); | 
|  | 539 |  | 
|  | 540 | if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_end)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 541 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 542 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 543 | return false; | 
|  | 544 | } | 
|  | 545 | fde->pc_end += fde->pc_start; | 
|  | 546 | if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') { | 
|  | 547 | // Augmentation Size | 
|  | 548 | uint64_t aug_length; | 
|  | 549 | if (!memory_.ReadULEB128(&aug_length)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 550 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 551 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 552 | return false; | 
|  | 553 | } | 
|  | 554 | uint64_t cur_offset = memory_.cur_offset(); | 
|  | 555 |  | 
|  | 556 | if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 557 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 558 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 559 | return false; | 
|  | 560 | } | 
|  | 561 |  | 
|  | 562 | // Set our position to after all of the augmentation data. | 
|  | 563 | memory_.set_cur_offset(cur_offset + aug_length); | 
|  | 564 | } | 
|  | 565 | fde->cfa_instructions_offset = memory_.cur_offset(); | 
|  | 566 |  | 
|  | 567 | return true; | 
|  | 568 | } | 
|  | 569 |  | 
|  | 570 | template <typename AddressType> | 
|  | 571 | bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde, | 
|  | 572 | dwarf_loc_regs_t* loc_regs) { | 
|  | 573 | DwarfCfa<AddressType> cfa(&memory_, fde); | 
|  | 574 |  | 
|  | 575 | // Look for the cached copy of the cie data. | 
|  | 576 | auto reg_entry = cie_loc_regs_.find(fde->cie_offset); | 
|  | 577 | if (reg_entry == cie_loc_regs_.end()) { | 
|  | 578 | if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end, | 
|  | 579 | loc_regs)) { | 
|  | 580 | last_error_ = cfa.last_error(); | 
|  | 581 | return false; | 
|  | 582 | } | 
|  | 583 | cie_loc_regs_[fde->cie_offset] = *loc_regs; | 
|  | 584 | } | 
|  | 585 | cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]); | 
|  | 586 | if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) { | 
|  | 587 | last_error_ = cfa.last_error(); | 
|  | 588 | return false; | 
|  | 589 | } | 
|  | 590 | return true; | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 | template <typename AddressType> | 
|  | 594 | bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, uint64_t load_bias, | 
|  | 595 | const DwarfFde* fde) { | 
|  | 596 | DwarfCfa<AddressType> cfa(&memory_, fde); | 
|  | 597 |  | 
|  | 598 | // Always print the cie information. | 
|  | 599 | const DwarfCie* cie = fde->cie; | 
|  | 600 | if (!cfa.Log(indent, pc, load_bias, cie->cfa_instructions_offset, cie->cfa_instructions_end)) { | 
|  | 601 | last_error_ = cfa.last_error(); | 
|  | 602 | return false; | 
|  | 603 | } | 
|  | 604 | if (!cfa.Log(indent, pc, load_bias, fde->cfa_instructions_offset, fde->cfa_instructions_end)) { | 
|  | 605 | last_error_ = cfa.last_error(); | 
|  | 606 | return false; | 
|  | 607 | } | 
|  | 608 | return true; | 
|  | 609 | } | 
|  | 610 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 611 | template <typename AddressType> | 
|  | 612 | bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size) { | 
|  | 613 | entries_offset_ = offset; | 
|  | 614 | entries_end_ = offset + size; | 
|  | 615 |  | 
|  | 616 | memory_.clear_func_offset(); | 
|  | 617 | memory_.clear_text_offset(); | 
|  | 618 | memory_.set_data_offset(offset); | 
|  | 619 | memory_.set_cur_offset(offset); | 
|  | 620 | memory_.set_pc_offset(offset); | 
|  | 621 |  | 
|  | 622 | return CreateSortedFdeList(); | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | template <typename AddressType> | 
|  | 626 | bool DwarfSectionImpl<AddressType>::GetCieInfo(uint8_t* segment_size, uint8_t* encoding) { | 
|  | 627 | uint8_t version; | 
|  | 628 | if (!memory_.ReadBytes(&version, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 629 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 630 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 631 | return false; | 
|  | 632 | } | 
|  | 633 | // Read the augmentation string. | 
|  | 634 | std::vector<char> aug_string; | 
|  | 635 | char aug_value; | 
|  | 636 | bool get_encoding = false; | 
|  | 637 | do { | 
|  | 638 | if (!memory_.ReadBytes(&aug_value, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 639 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 640 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 641 | return false; | 
|  | 642 | } | 
|  | 643 | if (aug_value == 'R') { | 
|  | 644 | get_encoding = true; | 
|  | 645 | } | 
|  | 646 | aug_string.push_back(aug_value); | 
|  | 647 | } while (aug_value != '\0'); | 
|  | 648 |  | 
|  | 649 | if (version == 4) { | 
|  | 650 | // Skip the Address Size field. | 
|  | 651 | memory_.set_cur_offset(memory_.cur_offset() + 1); | 
|  | 652 |  | 
|  | 653 | // Read the segment size. | 
|  | 654 | if (!memory_.ReadBytes(segment_size, 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 | } else { | 
|  | 660 | *segment_size = 0; | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | if (aug_string[0] != 'z' || !get_encoding) { | 
|  | 664 | // No encoding | 
|  | 665 | return true; | 
|  | 666 | } | 
|  | 667 |  | 
|  | 668 | // Skip code alignment factor | 
|  | 669 | uint8_t value; | 
|  | 670 | do { | 
|  | 671 | if (!memory_.ReadBytes(&value, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 672 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 673 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 674 | return false; | 
|  | 675 | } | 
|  | 676 | } while (value & 0x80); | 
|  | 677 |  | 
|  | 678 | // Skip data alignment factor | 
|  | 679 | do { | 
|  | 680 | if (!memory_.ReadBytes(&value, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 681 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 682 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 683 | return false; | 
|  | 684 | } | 
|  | 685 | } while (value & 0x80); | 
|  | 686 |  | 
|  | 687 | if (version == 1) { | 
|  | 688 | // Skip return address register. | 
|  | 689 | memory_.set_cur_offset(memory_.cur_offset() + 1); | 
|  | 690 | } else { | 
|  | 691 | // Skip return address register. | 
|  | 692 | do { | 
|  | 693 | if (!memory_.ReadBytes(&value, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 694 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 695 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 696 | return false; | 
|  | 697 | } | 
|  | 698 | } while (value & 0x80); | 
|  | 699 | } | 
|  | 700 |  | 
|  | 701 | // Skip the augmentation length. | 
|  | 702 | do { | 
|  | 703 | if (!memory_.ReadBytes(&value, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 704 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 705 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 706 | return false; | 
|  | 707 | } | 
|  | 708 | } while (value & 0x80); | 
|  | 709 |  | 
|  | 710 | for (size_t i = 1; i < aug_string.size(); i++) { | 
|  | 711 | if (aug_string[i] == 'R') { | 
|  | 712 | if (!memory_.ReadBytes(encoding, 1)) { | 
| 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 | // Got the encoding, that's all we are looking for. | 
|  | 718 | return true; | 
|  | 719 | } else if (aug_string[i] == 'L') { | 
|  | 720 | memory_.set_cur_offset(memory_.cur_offset() + 1); | 
|  | 721 | } else if (aug_string[i] == 'P') { | 
|  | 722 | uint8_t encoding; | 
|  | 723 | if (!memory_.ReadBytes(&encoding, 1)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 724 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 725 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 726 | return false; | 
|  | 727 | } | 
|  | 728 | uint64_t value; | 
|  | 729 | if (!memory_.template ReadEncodedValue<AddressType>(encoding, &value)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 730 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 731 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 732 | return false; | 
|  | 733 | } | 
|  | 734 | } | 
|  | 735 | } | 
|  | 736 |  | 
|  | 737 | // It should be impossible to get here. | 
|  | 738 | abort(); | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | template <typename AddressType> | 
|  | 742 | bool DwarfSectionImpl<AddressType>::AddFdeInfo(uint64_t entry_offset, uint8_t segment_size, | 
|  | 743 | uint8_t encoding) { | 
|  | 744 | if (segment_size != 0) { | 
|  | 745 | memory_.set_cur_offset(memory_.cur_offset() + 1); | 
|  | 746 | } | 
|  | 747 |  | 
|  | 748 | uint64_t start; | 
|  | 749 | if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &start)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 750 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 751 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 752 | return false; | 
|  | 753 | } | 
|  | 754 | start = AdjustPcFromFde(start); | 
|  | 755 |  | 
|  | 756 | uint64_t length; | 
|  | 757 | if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &length)) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 758 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 759 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 760 | return false; | 
|  | 761 | } | 
|  | 762 | if (length != 0) { | 
|  | 763 | fdes_.emplace_back(entry_offset, start, length); | 
|  | 764 | } | 
|  | 765 |  | 
|  | 766 | return true; | 
|  | 767 | } | 
|  | 768 |  | 
|  | 769 | template <typename AddressType> | 
|  | 770 | bool DwarfSectionImpl<AddressType>::CreateSortedFdeList() { | 
|  | 771 | memory_.set_cur_offset(entries_offset_); | 
|  | 772 |  | 
|  | 773 | // Loop through all of the entries and read just enough to create | 
|  | 774 | // a sorted list of pcs. | 
|  | 775 | // This code assumes that first comes the cie, then the fdes that | 
|  | 776 | // it applies to. | 
|  | 777 | uint64_t cie_offset = 0; | 
|  | 778 | uint8_t address_encoding; | 
|  | 779 | uint8_t segment_size; | 
|  | 780 | while (memory_.cur_offset() < entries_end_) { | 
|  | 781 | uint64_t cur_entry_offset = memory_.cur_offset(); | 
|  | 782 |  | 
|  | 783 | // Figure out the entry length and type. | 
|  | 784 | uint32_t value32; | 
|  | 785 | if (!memory_.ReadBytes(&value32, sizeof(value32))) { | 
| 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 | uint64_t next_entry_offset; | 
|  | 792 | if (value32 == static_cast<uint32_t>(-1)) { | 
|  | 793 | uint64_t value64; | 
|  | 794 | if (!memory_.ReadBytes(&value64, sizeof(value64))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 795 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 796 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 797 | return false; | 
|  | 798 | } | 
|  | 799 | next_entry_offset = memory_.cur_offset() + value64; | 
|  | 800 |  | 
|  | 801 | // Read the Cie Id of a Cie or the pointer of the Fde. | 
|  | 802 | if (!memory_.ReadBytes(&value64, sizeof(value64))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 803 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 804 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 805 | return false; | 
|  | 806 | } | 
|  | 807 |  | 
|  | 808 | if (value64 == cie64_value_) { | 
|  | 809 | // Cie 64 bit | 
|  | 810 | address_encoding = DW_EH_PE_sdata8; | 
|  | 811 | if (!GetCieInfo(&segment_size, &address_encoding)) { | 
|  | 812 | return false; | 
|  | 813 | } | 
|  | 814 | cie_offset = cur_entry_offset; | 
|  | 815 | } else { | 
|  | 816 | uint64_t last_cie_offset = GetCieOffsetFromFde64(value64); | 
|  | 817 | if (last_cie_offset != cie_offset) { | 
|  | 818 | // This means that this Fde is not following the Cie. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 819 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 820 | return false; | 
|  | 821 | } | 
|  | 822 |  | 
|  | 823 | // Fde 64 bit | 
|  | 824 | if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) { | 
|  | 825 | return false; | 
|  | 826 | } | 
|  | 827 | } | 
|  | 828 | } else { | 
|  | 829 | next_entry_offset = memory_.cur_offset() + value32; | 
|  | 830 |  | 
|  | 831 | // Read the Cie Id of a Cie or the pointer of the Fde. | 
|  | 832 | if (!memory_.ReadBytes(&value32, sizeof(value32))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 833 | last_error_.code = DWARF_ERROR_MEMORY_INVALID; | 
|  | 834 | last_error_.address = memory_.cur_offset(); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 835 | return false; | 
|  | 836 | } | 
|  | 837 |  | 
|  | 838 | if (value32 == cie32_value_) { | 
|  | 839 | // Cie 32 bit | 
|  | 840 | address_encoding = DW_EH_PE_sdata4; | 
|  | 841 | if (!GetCieInfo(&segment_size, &address_encoding)) { | 
|  | 842 | return false; | 
|  | 843 | } | 
|  | 844 | cie_offset = cur_entry_offset; | 
|  | 845 | } else { | 
|  | 846 | uint64_t last_cie_offset = GetCieOffsetFromFde32(value32); | 
|  | 847 | if (last_cie_offset != cie_offset) { | 
|  | 848 | // This means that this Fde is not following the Cie. | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 849 | last_error_.code = DWARF_ERROR_ILLEGAL_VALUE; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 850 | return false; | 
|  | 851 | } | 
|  | 852 |  | 
|  | 853 | // Fde 32 bit | 
|  | 854 | if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) { | 
|  | 855 | return false; | 
|  | 856 | } | 
|  | 857 | } | 
|  | 858 | } | 
|  | 859 |  | 
|  | 860 | if (next_entry_offset < memory_.cur_offset()) { | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 861 | // Simply consider the processing done in this case. | 
|  | 862 | break; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 863 | } | 
|  | 864 | memory_.set_cur_offset(next_entry_offset); | 
|  | 865 | } | 
|  | 866 |  | 
|  | 867 | // Sort the entries. | 
|  | 868 | std::sort(fdes_.begin(), fdes_.end(), [](const FdeInfo& a, const FdeInfo& b) { | 
|  | 869 | if (a.start == b.start) return a.end < b.end; | 
|  | 870 | return a.start < b.start; | 
|  | 871 | }); | 
|  | 872 |  | 
|  | 873 | fde_count_ = fdes_.size(); | 
|  | 874 |  | 
|  | 875 | return true; | 
|  | 876 | } | 
|  | 877 |  | 
|  | 878 | template <typename AddressType> | 
|  | 879 | bool DwarfSectionImpl<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) { | 
|  | 880 | if (fde_count_ == 0) { | 
|  | 881 | return false; | 
|  | 882 | } | 
|  | 883 |  | 
|  | 884 | size_t first = 0; | 
|  | 885 | size_t last = fde_count_; | 
|  | 886 | while (first < last) { | 
|  | 887 | size_t current = (first + last) / 2; | 
|  | 888 | const FdeInfo* info = &fdes_[current]; | 
|  | 889 | if (pc >= info->start && pc <= info->end) { | 
|  | 890 | *fde_offset = info->offset; | 
|  | 891 | return true; | 
|  | 892 | } | 
|  | 893 |  | 
|  | 894 | if (pc < info->start) { | 
|  | 895 | last = current; | 
|  | 896 | } else { | 
|  | 897 | first = current + 1; | 
|  | 898 | } | 
|  | 899 | } | 
|  | 900 | return false; | 
|  | 901 | } | 
|  | 902 |  | 
|  | 903 | template <typename AddressType> | 
|  | 904 | const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromIndex(size_t index) { | 
|  | 905 | if (index >= fdes_.size()) { | 
|  | 906 | return nullptr; | 
|  | 907 | } | 
|  | 908 | return this->GetFdeFromOffset(fdes_[index].offset); | 
|  | 909 | } | 
|  | 910 |  | 
| Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 911 | // Explicitly instantiate DwarfSectionImpl | 
|  | 912 | template class DwarfSectionImpl<uint32_t>; | 
|  | 913 | template class DwarfSectionImpl<uint64_t>; | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 914 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 915 | // Explicitly instantiate DwarfDebugFrame | 
|  | 916 | template class DwarfDebugFrame<uint32_t>; | 
|  | 917 | template class DwarfDebugFrame<uint64_t>; | 
|  | 918 |  | 
|  | 919 | // Explicitly instantiate DwarfEhFrame | 
|  | 920 | template class DwarfEhFrame<uint32_t>; | 
|  | 921 | template class DwarfEhFrame<uint64_t>; | 
|  | 922 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 923 | }  // namespace unwindstack |