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>
EhFrameInit(uint64_t offset,uint64_t size,int64_t section_bias)35 bool DwarfEhFrameWithHdr<AddressType>::EhFrameInit(uint64_t offset, uint64_t size,
36                                                    int64_t section_bias) {
37   return DwarfSectionImpl<AddressType>::Init(offset, size, section_bias);
38 }
39 
40 template <typename AddressType>
Init(uint64_t offset,uint64_t,int64_t section_bias)41 bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t, int64_t section_bias) {
42   memory_.clear_func_offset();
43   memory_.clear_text_offset();
44   memory_.set_data_offset(offset);
45   memory_.set_cur_offset(offset);
46 
47   hdr_section_bias_ = section_bias;
48 
49   // Read the first four bytes all at once.
50   uint8_t data[4];
51   if (!memory_.ReadBytes(data, 4)) {
52     last_error_.code = DWARF_ERROR_MEMORY_INVALID;
53     last_error_.address = memory_.cur_offset();
54     return false;
55   }
56 
57   version_ = data[0];
58   if (version_ != 1) {
59     // Unknown version.
60     last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
61     return false;
62   }
63 
64   uint8_t ptr_encoding = data[1];
65   uint8_t fde_count_encoding = data[2];
66   table_encoding_ = data[3];
67   table_entry_size_ = memory_.template GetEncodedSize<AddressType>(table_encoding_);
68 
69   // If we can't perform a binary search on the entries, it's not worth
70   // using this object. The calling code will fall back to the DwarfEhFrame
71   // object in this case.
72   if (table_entry_size_ == 0) {
73     last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
74     return false;
75   }
76 
77   memory_.set_pc_offset(memory_.cur_offset());
78   uint64_t ptr_offset;
79   if (!memory_.template ReadEncodedValue<AddressType>(ptr_encoding, &ptr_offset)) {
80     last_error_.code = DWARF_ERROR_MEMORY_INVALID;
81     last_error_.address = memory_.cur_offset();
82     return false;
83   }
84 
85   memory_.set_pc_offset(memory_.cur_offset());
86   if (!memory_.template ReadEncodedValue<AddressType>(fde_count_encoding, &fde_count_)) {
87     last_error_.code = DWARF_ERROR_MEMORY_INVALID;
88     last_error_.address = memory_.cur_offset();
89     return false;
90   }
91 
92   if (fde_count_ == 0) {
93     last_error_.code = DWARF_ERROR_NO_FDES;
94     return false;
95   }
96 
97   hdr_entries_offset_ = memory_.cur_offset();
98   hdr_entries_data_offset_ = offset;
99 
100   return true;
101 }
102 
103 template <typename AddressType>
GetFdeFromPc(uint64_t pc)104 const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
105   uint64_t fde_offset;
106   if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
107     return nullptr;
108   }
109   const DwarfFde* fde = this->GetFdeFromOffset(fde_offset);
110   if (fde == nullptr) {
111     return nullptr;
112   }
113 
114   // There is a possibility that this entry points to a zero length FDE
115   // due to a bug. If this happens, try and find the non-zero length FDE
116   // from eh_frame directly. See b/142483624.
117   if (fde->pc_start == fde->pc_end) {
118     fde = DwarfSectionImpl<AddressType>::GetFdeFromPc(pc);
119     if (fde == nullptr) {
120       return nullptr;
121     }
122   }
123 
124   // Guaranteed pc >= pc_start, need to check pc in the fde range.
125   if (pc < fde->pc_end) {
126     return fde;
127   }
128   last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
129   return nullptr;
130 }
131 
132 template <typename AddressType>
133 const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo*
GetFdeInfoFromIndex(size_t index)134 DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) {
135   auto entry = fde_info_.find(index);
136   if (entry != fde_info_.end()) {
137     return &fde_info_[index];
138   }
139   FdeInfo* info = &fde_info_[index];
140 
141   memory_.set_data_offset(hdr_entries_data_offset_);
142   memory_.set_cur_offset(hdr_entries_offset_ + 2 * index * table_entry_size_);
143   memory_.set_pc_offset(0);
144   uint64_t value;
145   if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) ||
146       !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
147     last_error_.code = DWARF_ERROR_MEMORY_INVALID;
148     last_error_.address = memory_.cur_offset();
149     fde_info_.erase(index);
150     return nullptr;
151   }
152 
153   // Relative encodings require adding in the load bias.
154   if (IsEncodingRelative(table_encoding_)) {
155     value += hdr_section_bias_;
156   }
157   info->pc = value;
158   return info;
159 }
160 
161 template <typename AddressType>
GetFdeOffsetFromPc(uint64_t pc,uint64_t * fde_offset)162 bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
163   if (fde_count_ == 0) {
164     return false;
165   }
166 
167   size_t first = 0;
168   size_t last = fde_count_;
169   while (first < last) {
170     size_t current = (first + last) / 2;
171     const FdeInfo* info = GetFdeInfoFromIndex(current);
172     if (info == nullptr) {
173       return false;
174     }
175     if (pc == info->pc) {
176       *fde_offset = info->offset;
177       return true;
178     }
179     if (pc < info->pc) {
180       last = current;
181     } else {
182       first = current + 1;
183     }
184   }
185   if (last != 0) {
186     const FdeInfo* info = GetFdeInfoFromIndex(last - 1);
187     if (info == nullptr) {
188       return false;
189     }
190     *fde_offset = info->offset;
191     return true;
192   }
193   return false;
194 }
195 
196 template <typename AddressType>
GetFdes(std::vector<const DwarfFde * > * fdes)197 void DwarfEhFrameWithHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
198   for (size_t i = 0; i < fde_count_; i++) {
199     const FdeInfo* info = GetFdeInfoFromIndex(i);
200     if (info == nullptr) {
201       break;
202     }
203     const DwarfFde* fde = this->GetFdeFromOffset(info->offset);
204     if (fde == nullptr) {
205       break;
206     }
207 
208     // There is a possibility that this entry points to a zero length FDE
209     // due to a bug. If this happens, try and find the non-zero length FDE
210     // from eh_frame directly. See b/142483624.
211     if (fde->pc_start == fde->pc_end) {
212       const DwarfFde* fde_real = DwarfSectionImpl<AddressType>::GetFdeFromPc(fde->pc_start);
213       if (fde_real != nullptr) {
214         fde = fde_real;
215       }
216     }
217     fdes->push_back(fde);
218   }
219 }
220 
221 // Explicitly instantiate DwarfEhFrameWithHdr
222 template class DwarfEhFrameWithHdr<uint32_t>;
223 template class DwarfEhFrameWithHdr<uint64_t>;
224 
225 }  // namespace unwindstack
226