DwarfEhFrameWithHdr.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/DwarfStructs.h>
  19. #include <unwindstack/Memory.h>
  20. #include "Check.h"
  21. #include "DwarfEhFrameWithHdr.h"
  22. #include "DwarfEncoding.h"
  23. namespace unwindstack {
  24. static inline bool IsEncodingRelative(uint8_t encoding) {
  25. encoding >>= 4;
  26. return encoding > 0 && encoding <= DW_EH_PE_funcrel;
  27. }
  28. template <typename AddressType>
  29. bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) {
  30. load_bias_ = load_bias;
  31. memory_.clear_func_offset();
  32. memory_.clear_text_offset();
  33. memory_.set_data_offset(offset);
  34. memory_.set_cur_offset(offset);
  35. pc_offset_ = offset;
  36. // Read the first four bytes all at once.
  37. uint8_t data[4];
  38. if (!memory_.ReadBytes(data, 4)) {
  39. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  40. last_error_.address = memory_.cur_offset();
  41. return false;
  42. }
  43. version_ = data[0];
  44. if (version_ != 1) {
  45. // Unknown version.
  46. last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
  47. return false;
  48. }
  49. ptr_encoding_ = data[1];
  50. uint8_t fde_count_encoding = data[2];
  51. table_encoding_ = data[3];
  52. table_entry_size_ = memory_.template GetEncodedSize<AddressType>(table_encoding_);
  53. // If we can't perform a binary search on the entries, it's not worth
  54. // using this object. The calling code will fall back to the DwarfEhFrame
  55. // object in this case.
  56. if (table_entry_size_ == 0) {
  57. last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
  58. return false;
  59. }
  60. memory_.set_pc_offset(memory_.cur_offset());
  61. if (!memory_.template ReadEncodedValue<AddressType>(ptr_encoding_, &ptr_offset_)) {
  62. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  63. last_error_.address = memory_.cur_offset();
  64. return false;
  65. }
  66. memory_.set_pc_offset(memory_.cur_offset());
  67. if (!memory_.template ReadEncodedValue<AddressType>(fde_count_encoding, &fde_count_)) {
  68. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  69. last_error_.address = memory_.cur_offset();
  70. return false;
  71. }
  72. if (fde_count_ == 0) {
  73. last_error_.code = DWARF_ERROR_NO_FDES;
  74. return false;
  75. }
  76. entries_offset_ = memory_.cur_offset();
  77. entries_end_ = offset + size;
  78. entries_data_offset_ = offset;
  79. cur_entries_offset_ = entries_offset_;
  80. return true;
  81. }
  82. template <typename AddressType>
  83. const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
  84. uint64_t fde_offset;
  85. if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
  86. return nullptr;
  87. }
  88. const DwarfFde* fde = this->GetFdeFromOffset(fde_offset);
  89. if (fde == nullptr) {
  90. return nullptr;
  91. }
  92. // Guaranteed pc >= pc_start, need to check pc in the fde range.
  93. if (pc < fde->pc_end) {
  94. return fde;
  95. }
  96. last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
  97. return nullptr;
  98. }
  99. template <typename AddressType>
  100. const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo*
  101. DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) {
  102. auto entry = fde_info_.find(index);
  103. if (entry != fde_info_.end()) {
  104. return &fde_info_[index];
  105. }
  106. FdeInfo* info = &fde_info_[index];
  107. memory_.set_data_offset(entries_data_offset_);
  108. memory_.set_cur_offset(entries_offset_ + 2 * index * table_entry_size_);
  109. memory_.set_pc_offset(0);
  110. uint64_t value;
  111. if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) ||
  112. !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
  113. last_error_.code = DWARF_ERROR_MEMORY_INVALID;
  114. last_error_.address = memory_.cur_offset();
  115. fde_info_.erase(index);
  116. return nullptr;
  117. }
  118. // Relative encodings require adding in the load bias.
  119. if (IsEncodingRelative(table_encoding_)) {
  120. value += load_bias_;
  121. }
  122. info->pc = value;
  123. return info;
  124. }
  125. template <typename AddressType>
  126. bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
  127. if (fde_count_ == 0) {
  128. return false;
  129. }
  130. size_t first = 0;
  131. size_t last = fde_count_;
  132. while (first < last) {
  133. size_t current = (first + last) / 2;
  134. const FdeInfo* info = GetFdeInfoFromIndex(current);
  135. if (info == nullptr) {
  136. return false;
  137. }
  138. if (pc == info->pc) {
  139. *fde_offset = info->offset;
  140. return true;
  141. }
  142. if (pc < info->pc) {
  143. last = current;
  144. } else {
  145. first = current + 1;
  146. }
  147. }
  148. if (last != 0) {
  149. const FdeInfo* info = GetFdeInfoFromIndex(last - 1);
  150. if (info == nullptr) {
  151. return false;
  152. }
  153. *fde_offset = info->offset;
  154. return true;
  155. }
  156. return false;
  157. }
  158. template <typename AddressType>
  159. void DwarfEhFrameWithHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
  160. for (size_t i = 0; i < fde_count_; i++) {
  161. const FdeInfo* info = GetFdeInfoFromIndex(i);
  162. if (info == nullptr) {
  163. break;
  164. }
  165. const DwarfFde* fde = this->GetFdeFromOffset(info->offset);
  166. if (fde == nullptr) {
  167. break;
  168. }
  169. fdes->push_back(fde);
  170. }
  171. }
  172. // Explicitly instantiate DwarfEhFrameWithHdr
  173. template class DwarfEhFrameWithHdr<uint32_t>;
  174. template class DwarfEhFrameWithHdr<uint64_t>;
  175. } // namespace unwindstack