DwarfSection.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. #include <stdint.h>
  17. #include <unwindstack/DwarfError.h>
  18. #include <unwindstack/DwarfLocation.h>
  19. #include <unwindstack/DwarfMemory.h>
  20. #include <unwindstack/DwarfSection.h>
  21. #include <unwindstack/DwarfStructs.h>
  22. #include <unwindstack/Log.h>
  23. #include <unwindstack/Memory.h>
  24. #include <unwindstack/Regs.h>
  25. #include "DwarfCfa.h"
  26. #include "DwarfDebugFrame.h"
  27. #include "DwarfEhFrame.h"
  28. #include "DwarfEncoding.h"
  29. #include "DwarfOp.h"
  30. #include "RegsInfo.h"
  31. namespace unwindstack {
  32. DwarfSection::DwarfSection(Memory* memory) : memory_(memory) {}
  33. bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
  34. // Lookup the pc in the cache.
  35. auto it = loc_regs_.upper_bound(pc);
  36. if (it == loc_regs_.end() || pc < it->second.pc_start) {
  37. last_error_.code = DWARF_ERROR_NONE;
  38. const DwarfFde* fde = GetFdeFromPc(pc);
  39. if (fde == nullptr || fde->cie == nullptr) {
  40. last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
  41. return false;
  42. }
  43. // Now get the location information for this pc.
  44. dwarf_loc_regs_t loc_regs;
  45. if (!GetCfaLocationInfo(pc, fde, &loc_regs)) {
  46. return false;
  47. }
  48. loc_regs.cie = fde->cie;
  49. // Store it in the cache.
  50. it = loc_regs_.emplace(loc_regs.pc_end, std::move(loc_regs)).first;
  51. }
  52. // Now eval the actual registers.
  53. return Eval(it->second.cie, process_memory, it->second, regs, finished);
  54. }
  55. template <typename AddressType>
  56. const DwarfCie* DwarfSectionImpl<AddressType>::GetCieFromOffset(uint64_t offset) {
  57. auto cie_entry = cie_entries_.find(offset);
  58. if (cie_entry != cie_entries_.end()) {
  59. return &cie_entry->second;
  60. }
  61. DwarfCie* cie = &cie_entries_[offset];
  62. memory_.set_cur_offset(offset);
  63. if (!FillInCieHeader(cie) || !FillInCie(cie)) {
  64. // Erase the cached entry.
  65. cie_entries_.erase(offset);
  66. return nullptr;
  67. }
  68. return cie;
  69. }
  70. template <typename AddressType>
  71. bool DwarfSectionImpl<AddressType>::FillInCieHeader(DwarfCie* cie) {
  72. cie->lsda_encoding = DW_EH_PE_omit;
  73. uint32_t length32;
  74. if (!memory_.ReadBytes(&length32, sizeof(length32))) {
  75. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  76. last_error_.address = memory_.cur_offset();
  77. return false;
  78. }
  79. if (length32 == static_cast<uint32_t>(-1)) {
  80. // 64 bit Cie
  81. uint64_t length64;
  82. if (!memory_.ReadBytes(&length64, sizeof(length64))) {
  83. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  84. last_error_.address = memory_.cur_offset();
  85. return false;
  86. }
  87. cie->cfa_instructions_end = memory_.cur_offset() + length64;
  88. cie->fde_address_encoding = DW_EH_PE_sdata8;
  89. uint64_t cie_id;
  90. if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
  91. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  92. last_error_.address = memory_.cur_offset();
  93. return false;
  94. }
  95. if (cie_id != cie64_value_) {
  96. // This is not a Cie, something has gone horribly wrong.
  97. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  98. return false;
  99. }
  100. } else {
  101. // 32 bit Cie
  102. cie->cfa_instructions_end = memory_.cur_offset() + length32;
  103. cie->fde_address_encoding = DW_EH_PE_sdata4;
  104. uint32_t cie_id;
  105. if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
  106. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  107. last_error_.address = memory_.cur_offset();
  108. return false;
  109. }
  110. if (cie_id != cie32_value_) {
  111. // This is not a Cie, something has gone horribly wrong.
  112. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. template <typename AddressType>
  119. bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
  120. if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
  121. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  122. last_error_.address = memory_.cur_offset();
  123. return false;
  124. }
  125. if (cie->version != 1 && cie->version != 3 && cie->version != 4 && cie->version != 5) {
  126. // Unrecognized version.
  127. last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
  128. return false;
  129. }
  130. // Read the augmentation string.
  131. char aug_value;
  132. do {
  133. if (!memory_.ReadBytes(&aug_value, 1)) {
  134. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  135. last_error_.address = memory_.cur_offset();
  136. return false;
  137. }
  138. cie->augmentation_string.push_back(aug_value);
  139. } while (aug_value != '\0');
  140. if (cie->version == 4 || cie->version == 5) {
  141. // Skip the Address Size field since we only use it for validation.
  142. memory_.set_cur_offset(memory_.cur_offset() + 1);
  143. // Segment Size
  144. if (!memory_.ReadBytes(&cie->segment_size, 1)) {
  145. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  146. last_error_.address = memory_.cur_offset();
  147. return false;
  148. }
  149. }
  150. // Code Alignment Factor
  151. if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
  152. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  153. last_error_.address = memory_.cur_offset();
  154. return false;
  155. }
  156. // Data Alignment Factor
  157. if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
  158. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  159. last_error_.address = memory_.cur_offset();
  160. return false;
  161. }
  162. if (cie->version == 1) {
  163. // Return Address is a single byte.
  164. uint8_t return_address_register;
  165. if (!memory_.ReadBytes(&return_address_register, 1)) {
  166. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  167. last_error_.address = memory_.cur_offset();
  168. return false;
  169. }
  170. cie->return_address_register = return_address_register;
  171. } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
  172. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  173. last_error_.address = memory_.cur_offset();
  174. return false;
  175. }
  176. if (cie->augmentation_string[0] != 'z') {
  177. cie->cfa_instructions_offset = memory_.cur_offset();
  178. return true;
  179. }
  180. uint64_t aug_length;
  181. if (!memory_.ReadULEB128(&aug_length)) {
  182. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  183. last_error_.address = memory_.cur_offset();
  184. return false;
  185. }
  186. cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
  187. for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
  188. switch (cie->augmentation_string[i]) {
  189. case 'L':
  190. if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
  191. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  192. last_error_.address = memory_.cur_offset();
  193. return false;
  194. }
  195. break;
  196. case 'P': {
  197. uint8_t encoding;
  198. if (!memory_.ReadBytes(&encoding, 1)) {
  199. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  200. last_error_.address = memory_.cur_offset();
  201. return false;
  202. }
  203. memory_.set_pc_offset(pc_offset_);
  204. if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
  205. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  206. last_error_.address = memory_.cur_offset();
  207. return false;
  208. }
  209. } break;
  210. case 'R':
  211. if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
  212. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  213. last_error_.address = memory_.cur_offset();
  214. return false;
  215. }
  216. break;
  217. }
  218. }
  219. return true;
  220. }
  221. template <typename AddressType>
  222. const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
  223. auto fde_entry = fde_entries_.find(offset);
  224. if (fde_entry != fde_entries_.end()) {
  225. return &fde_entry->second;
  226. }
  227. DwarfFde* fde = &fde_entries_[offset];
  228. memory_.set_cur_offset(offset);
  229. if (!FillInFdeHeader(fde) || !FillInFde(fde)) {
  230. fde_entries_.erase(offset);
  231. return nullptr;
  232. }
  233. return fde;
  234. }
  235. template <typename AddressType>
  236. bool DwarfSectionImpl<AddressType>::FillInFdeHeader(DwarfFde* fde) {
  237. uint32_t length32;
  238. if (!memory_.ReadBytes(&length32, sizeof(length32))) {
  239. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  240. last_error_.address = memory_.cur_offset();
  241. return false;
  242. }
  243. if (length32 == static_cast<uint32_t>(-1)) {
  244. // 64 bit Fde.
  245. uint64_t length64;
  246. if (!memory_.ReadBytes(&length64, sizeof(length64))) {
  247. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  248. last_error_.address = memory_.cur_offset();
  249. return false;
  250. }
  251. fde->cfa_instructions_end = memory_.cur_offset() + length64;
  252. uint64_t value64;
  253. if (!memory_.ReadBytes(&value64, sizeof(value64))) {
  254. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  255. last_error_.address = memory_.cur_offset();
  256. return false;
  257. }
  258. if (value64 == cie64_value_) {
  259. // This is a Cie, this means something has gone wrong.
  260. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  261. return false;
  262. }
  263. // Get the Cie pointer, which is necessary to properly read the rest of
  264. // of the Fde information.
  265. fde->cie_offset = GetCieOffsetFromFde64(value64);
  266. } else {
  267. // 32 bit Fde.
  268. fde->cfa_instructions_end = memory_.cur_offset() + length32;
  269. uint32_t value32;
  270. if (!memory_.ReadBytes(&value32, sizeof(value32))) {
  271. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  272. last_error_.address = memory_.cur_offset();
  273. return false;
  274. }
  275. if (value32 == cie32_value_) {
  276. // This is a Cie, this means something has gone wrong.
  277. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  278. return false;
  279. }
  280. // Get the Cie pointer, which is necessary to properly read the rest of
  281. // of the Fde information.
  282. fde->cie_offset = GetCieOffsetFromFde32(value32);
  283. }
  284. return true;
  285. }
  286. template <typename AddressType>
  287. bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
  288. uint64_t cur_offset = memory_.cur_offset();
  289. const DwarfCie* cie = GetCieFromOffset(fde->cie_offset);
  290. if (cie == nullptr) {
  291. return false;
  292. }
  293. fde->cie = cie;
  294. if (cie->segment_size != 0) {
  295. // Skip over the segment selector for now.
  296. cur_offset += cie->segment_size;
  297. }
  298. memory_.set_cur_offset(cur_offset);
  299. // The load bias only applies to the start.
  300. memory_.set_pc_offset(load_bias_);
  301. bool valid = memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_start);
  302. fde->pc_start = AdjustPcFromFde(fde->pc_start);
  303. memory_.set_pc_offset(0);
  304. if (!valid || !memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_end)) {
  305. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  306. last_error_.address = memory_.cur_offset();
  307. return false;
  308. }
  309. fde->pc_end += fde->pc_start;
  310. if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
  311. // Augmentation Size
  312. uint64_t aug_length;
  313. if (!memory_.ReadULEB128(&aug_length)) {
  314. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  315. last_error_.address = memory_.cur_offset();
  316. return false;
  317. }
  318. uint64_t cur_offset = memory_.cur_offset();
  319. memory_.set_pc_offset(pc_offset_);
  320. if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
  321. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  322. last_error_.address = memory_.cur_offset();
  323. return false;
  324. }
  325. // Set our position to after all of the augmentation data.
  326. memory_.set_cur_offset(cur_offset + aug_length);
  327. }
  328. fde->cfa_instructions_offset = memory_.cur_offset();
  329. return true;
  330. }
  331. template <typename AddressType>
  332. bool DwarfSectionImpl<AddressType>::EvalExpression(const DwarfLocation& loc, Memory* regular_memory,
  333. AddressType* value,
  334. RegsInfo<AddressType>* regs_info,
  335. bool* is_dex_pc) {
  336. DwarfOp<AddressType> op(&memory_, regular_memory);
  337. op.set_regs_info(regs_info);
  338. // Need to evaluate the op data.
  339. uint64_t end = loc.values[1];
  340. uint64_t start = end - loc.values[0];
  341. if (!op.Eval(start, end)) {
  342. last_error_ = op.last_error();
  343. return false;
  344. }
  345. if (op.StackSize() == 0) {
  346. last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
  347. return false;
  348. }
  349. // We don't support an expression that evaluates to a register number.
  350. if (op.is_register()) {
  351. last_error_.code = DWARF_ERROR_NOT_IMPLEMENTED;
  352. return false;
  353. }
  354. *value = op.StackAt(0);
  355. if (is_dex_pc != nullptr && op.dex_pc_set()) {
  356. *is_dex_pc = true;
  357. }
  358. return true;
  359. }
  360. template <typename AddressType>
  361. struct EvalInfo {
  362. const dwarf_loc_regs_t* loc_regs;
  363. const DwarfCie* cie;
  364. Memory* regular_memory;
  365. AddressType cfa;
  366. bool return_address_undefined = false;
  367. RegsInfo<AddressType> regs_info;
  368. };
  369. template <typename AddressType>
  370. bool DwarfSectionImpl<AddressType>::EvalRegister(const DwarfLocation* loc, uint32_t reg,
  371. AddressType* reg_ptr, void* info) {
  372. EvalInfo<AddressType>* eval_info = reinterpret_cast<EvalInfo<AddressType>*>(info);
  373. Memory* regular_memory = eval_info->regular_memory;
  374. switch (loc->type) {
  375. case DWARF_LOCATION_OFFSET:
  376. if (!regular_memory->ReadFully(eval_info->cfa + loc->values[0], reg_ptr, sizeof(AddressType))) {
  377. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  378. last_error_.address = eval_info->cfa + loc->values[0];
  379. return false;
  380. }
  381. break;
  382. case DWARF_LOCATION_VAL_OFFSET:
  383. *reg_ptr = eval_info->cfa + loc->values[0];
  384. break;
  385. case DWARF_LOCATION_REGISTER: {
  386. uint32_t cur_reg = loc->values[0];
  387. if (cur_reg >= eval_info->regs_info.Total()) {
  388. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  389. return false;
  390. }
  391. *reg_ptr = eval_info->regs_info.Get(cur_reg) + loc->values[1];
  392. break;
  393. }
  394. case DWARF_LOCATION_EXPRESSION:
  395. case DWARF_LOCATION_VAL_EXPRESSION: {
  396. AddressType value;
  397. bool is_dex_pc = false;
  398. if (!EvalExpression(*loc, regular_memory, &value, &eval_info->regs_info, &is_dex_pc)) {
  399. return false;
  400. }
  401. if (loc->type == DWARF_LOCATION_EXPRESSION) {
  402. if (!regular_memory->ReadFully(value, reg_ptr, sizeof(AddressType))) {
  403. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  404. last_error_.address = value;
  405. return false;
  406. }
  407. } else {
  408. *reg_ptr = value;
  409. if (is_dex_pc) {
  410. eval_info->regs_info.regs->set_dex_pc(value);
  411. }
  412. }
  413. break;
  414. }
  415. case DWARF_LOCATION_UNDEFINED:
  416. if (reg == eval_info->cie->return_address_register) {
  417. eval_info->return_address_undefined = true;
  418. }
  419. break;
  420. default:
  421. break;
  422. }
  423. return true;
  424. }
  425. template <typename AddressType>
  426. bool DwarfSectionImpl<AddressType>::Eval(const DwarfCie* cie, Memory* regular_memory,
  427. const dwarf_loc_regs_t& loc_regs, Regs* regs,
  428. bool* finished) {
  429. RegsImpl<AddressType>* cur_regs = reinterpret_cast<RegsImpl<AddressType>*>(regs);
  430. if (cie->return_address_register >= cur_regs->total_regs()) {
  431. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  432. return false;
  433. }
  434. // Get the cfa value;
  435. auto cfa_entry = loc_regs.find(CFA_REG);
  436. if (cfa_entry == loc_regs.end()) {
  437. last_error_.code = DWARF_ERROR_CFA_NOT_DEFINED;
  438. return false;
  439. }
  440. // Always set the dex pc to zero when evaluating.
  441. cur_regs->set_dex_pc(0);
  442. EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
  443. .cie = cie,
  444. .regular_memory = regular_memory,
  445. .regs_info = RegsInfo<AddressType>(cur_regs)};
  446. const DwarfLocation* loc = &cfa_entry->second;
  447. // Only a few location types are valid for the cfa.
  448. switch (loc->type) {
  449. case DWARF_LOCATION_REGISTER:
  450. if (loc->values[0] >= cur_regs->total_regs()) {
  451. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  452. return false;
  453. }
  454. eval_info.cfa = (*cur_regs)[loc->values[0]];
  455. eval_info.cfa += loc->values[1];
  456. break;
  457. case DWARF_LOCATION_VAL_EXPRESSION: {
  458. AddressType value;
  459. if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
  460. return false;
  461. }
  462. // There is only one type of valid expression for CFA evaluation.
  463. eval_info.cfa = value;
  464. break;
  465. }
  466. default:
  467. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  468. return false;
  469. }
  470. for (const auto& entry : loc_regs) {
  471. uint32_t reg = entry.first;
  472. // Already handled the CFA register.
  473. if (reg == CFA_REG) continue;
  474. AddressType* reg_ptr;
  475. if (reg >= cur_regs->total_regs()) {
  476. // Skip this unknown register.
  477. continue;
  478. }
  479. reg_ptr = eval_info.regs_info.Save(reg);
  480. if (!EvalRegister(&entry.second, reg, reg_ptr, &eval_info)) {
  481. return false;
  482. }
  483. }
  484. // Find the return address location.
  485. if (eval_info.return_address_undefined) {
  486. cur_regs->set_pc(0);
  487. } else {
  488. cur_regs->set_pc((*cur_regs)[cie->return_address_register]);
  489. }
  490. // If the pc was set to zero, consider this the final frame.
  491. *finished = (cur_regs->pc() == 0) ? true : false;
  492. cur_regs->set_sp(eval_info.cfa);
  493. return true;
  494. }
  495. template <typename AddressType>
  496. bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
  497. dwarf_loc_regs_t* loc_regs) {
  498. DwarfCfa<AddressType> cfa(&memory_, fde);
  499. // Look for the cached copy of the cie data.
  500. auto reg_entry = cie_loc_regs_.find(fde->cie_offset);
  501. if (reg_entry == cie_loc_regs_.end()) {
  502. if (!cfa.GetLocationInfo(pc, fde->cie->cfa_instructions_offset, fde->cie->cfa_instructions_end,
  503. loc_regs)) {
  504. last_error_ = cfa.last_error();
  505. return false;
  506. }
  507. cie_loc_regs_[fde->cie_offset] = *loc_regs;
  508. }
  509. cfa.set_cie_loc_regs(&cie_loc_regs_[fde->cie_offset]);
  510. if (!cfa.GetLocationInfo(pc, fde->cfa_instructions_offset, fde->cfa_instructions_end, loc_regs)) {
  511. last_error_ = cfa.last_error();
  512. return false;
  513. }
  514. return true;
  515. }
  516. template <typename AddressType>
  517. bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, const DwarfFde* fde) {
  518. DwarfCfa<AddressType> cfa(&memory_, fde);
  519. // Always print the cie information.
  520. const DwarfCie* cie = fde->cie;
  521. if (!cfa.Log(indent, pc, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
  522. last_error_ = cfa.last_error();
  523. return false;
  524. }
  525. if (!cfa.Log(indent, pc, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
  526. last_error_ = cfa.last_error();
  527. return false;
  528. }
  529. return true;
  530. }
  531. template <typename AddressType>
  532. bool DwarfSectionImplNoHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) {
  533. load_bias_ = load_bias;
  534. entries_offset_ = offset;
  535. next_entries_offset_ = offset;
  536. entries_end_ = offset + size;
  537. memory_.clear_func_offset();
  538. memory_.clear_text_offset();
  539. memory_.set_cur_offset(offset);
  540. memory_.set_data_offset(offset);
  541. pc_offset_ = offset;
  542. return true;
  543. }
  544. // Create a cached version of the fde information such that it is a std::map
  545. // that is indexed by end pc and contains a pair that represents the start pc
  546. // followed by the fde object. The fde pointers are owned by fde_entries_
  547. // and not by the map object.
  548. // It is possible for an fde to be represented by multiple entries in
  549. // the map. This can happen if the the start pc and end pc overlap already
  550. // existing entries. For example, if there is already an entry of 0x400, 0x200,
  551. // and an fde has a start pc of 0x100 and end pc of 0x500, two new entries
  552. // will be added: 0x200, 0x100 and 0x500, 0x400.
  553. template <typename AddressType>
  554. void DwarfSectionImplNoHdr<AddressType>::InsertFde(const DwarfFde* fde) {
  555. uint64_t start = fde->pc_start;
  556. uint64_t end = fde->pc_end;
  557. auto it = fdes_.upper_bound(start);
  558. bool add_element = false;
  559. while (it != fdes_.end() && start < end) {
  560. if (add_element) {
  561. add_element = false;
  562. if (end < it->second.first) {
  563. if (it->first == end) {
  564. return;
  565. }
  566. fdes_[end] = std::make_pair(start, fde);
  567. return;
  568. }
  569. if (start != it->second.first) {
  570. fdes_[it->second.first] = std::make_pair(start, fde);
  571. }
  572. }
  573. if (start < it->first) {
  574. if (end < it->second.first) {
  575. if (it->first != end) {
  576. fdes_[end] = std::make_pair(start, fde);
  577. }
  578. return;
  579. }
  580. add_element = true;
  581. }
  582. start = it->first;
  583. ++it;
  584. }
  585. if (start < end) {
  586. fdes_[end] = std::make_pair(start, fde);
  587. }
  588. }
  589. template <typename AddressType>
  590. bool DwarfSectionImplNoHdr<AddressType>::GetNextCieOrFde(DwarfFde** fde_entry) {
  591. uint64_t start_offset = next_entries_offset_;
  592. memory_.set_cur_offset(next_entries_offset_);
  593. uint32_t value32;
  594. if (!memory_.ReadBytes(&value32, sizeof(value32))) {
  595. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  596. last_error_.address = memory_.cur_offset();
  597. return false;
  598. }
  599. uint64_t cie_offset;
  600. uint8_t cie_fde_encoding;
  601. bool entry_is_cie = false;
  602. if (value32 == static_cast<uint32_t>(-1)) {
  603. // 64 bit entry.
  604. uint64_t value64;
  605. if (!memory_.ReadBytes(&value64, sizeof(value64))) {
  606. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  607. last_error_.address = memory_.cur_offset();
  608. return false;
  609. }
  610. next_entries_offset_ = memory_.cur_offset() + value64;
  611. // Read the Cie Id of a Cie or the pointer of the Fde.
  612. if (!memory_.ReadBytes(&value64, sizeof(value64))) {
  613. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  614. last_error_.address = memory_.cur_offset();
  615. return false;
  616. }
  617. if (value64 == cie64_value_) {
  618. entry_is_cie = true;
  619. cie_fde_encoding = DW_EH_PE_sdata8;
  620. } else {
  621. cie_offset = this->GetCieOffsetFromFde64(value64);
  622. }
  623. } else {
  624. next_entries_offset_ = memory_.cur_offset() + value32;
  625. // 32 bit Cie
  626. if (!memory_.ReadBytes(&value32, sizeof(value32))) {
  627. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  628. last_error_.address = memory_.cur_offset();
  629. return false;
  630. }
  631. if (value32 == cie32_value_) {
  632. entry_is_cie = true;
  633. cie_fde_encoding = DW_EH_PE_sdata4;
  634. } else {
  635. cie_offset = this->GetCieOffsetFromFde32(value32);
  636. }
  637. }
  638. if (entry_is_cie) {
  639. DwarfCie* cie = &cie_entries_[start_offset];
  640. cie->lsda_encoding = DW_EH_PE_omit;
  641. cie->cfa_instructions_end = next_entries_offset_;
  642. cie->fde_address_encoding = cie_fde_encoding;
  643. if (!this->FillInCie(cie)) {
  644. cie_entries_.erase(start_offset);
  645. return false;
  646. }
  647. *fde_entry = nullptr;
  648. } else {
  649. DwarfFde* fde = &fde_entries_[start_offset];
  650. fde->cfa_instructions_end = next_entries_offset_;
  651. fde->cie_offset = cie_offset;
  652. if (!this->FillInFde(fde)) {
  653. fde_entries_.erase(start_offset);
  654. return false;
  655. }
  656. *fde_entry = fde;
  657. }
  658. return true;
  659. }
  660. template <typename AddressType>
  661. void DwarfSectionImplNoHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
  662. // Loop through the already cached entries.
  663. uint64_t entry_offset = entries_offset_;
  664. while (entry_offset < next_entries_offset_) {
  665. auto cie_it = cie_entries_.find(entry_offset);
  666. if (cie_it != cie_entries_.end()) {
  667. entry_offset = cie_it->second.cfa_instructions_end;
  668. } else {
  669. auto fde_it = fde_entries_.find(entry_offset);
  670. if (fde_it == fde_entries_.end()) {
  671. // No fde or cie at this entry, should not be possible.
  672. return;
  673. }
  674. entry_offset = fde_it->second.cfa_instructions_end;
  675. fdes->push_back(&fde_it->second);
  676. }
  677. }
  678. while (next_entries_offset_ < entries_end_) {
  679. DwarfFde* fde;
  680. if (!GetNextCieOrFde(&fde)) {
  681. break;
  682. }
  683. if (fde != nullptr) {
  684. InsertFde(fde);
  685. fdes->push_back(fde);
  686. }
  687. if (next_entries_offset_ < memory_.cur_offset()) {
  688. // Simply consider the processing done in this case.
  689. break;
  690. }
  691. }
  692. }
  693. template <typename AddressType>
  694. const DwarfFde* DwarfSectionImplNoHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
  695. // Search in the list of fdes we already have.
  696. auto it = fdes_.upper_bound(pc);
  697. if (it != fdes_.end()) {
  698. if (pc >= it->second.first) {
  699. return it->second.second;
  700. }
  701. }
  702. // The section might have overlapping pcs in fdes, so it is necessary
  703. // to do a linear search of the fdes by pc. As fdes are read, a cached
  704. // search map is created.
  705. while (next_entries_offset_ < entries_end_) {
  706. DwarfFde* fde;
  707. if (!GetNextCieOrFde(&fde)) {
  708. return nullptr;
  709. }
  710. if (fde != nullptr) {
  711. InsertFde(fde);
  712. if (pc >= fde->pc_start && pc < fde->pc_end) {
  713. return fde;
  714. }
  715. }
  716. if (next_entries_offset_ < memory_.cur_offset()) {
  717. // Simply consider the processing done in this case.
  718. break;
  719. }
  720. }
  721. return nullptr;
  722. }
  723. // Explicitly instantiate DwarfSectionImpl
  724. template class DwarfSectionImpl<uint32_t>;
  725. template class DwarfSectionImpl<uint64_t>;
  726. // Explicitly instantiate DwarfSectionImplNoHdr
  727. template class DwarfSectionImplNoHdr<uint32_t>;
  728. template class DwarfSectionImplNoHdr<uint64_t>;
  729. // Explicitly instantiate DwarfDebugFrame
  730. template class DwarfDebugFrame<uint32_t>;
  731. template class DwarfDebugFrame<uint64_t>;
  732. // Explicitly instantiate DwarfEhFrame
  733. template class DwarfEhFrame<uint32_t>;
  734. template class DwarfEhFrame<uint64_t>;
  735. } // namespace unwindstack