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