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