blob: d5d158f8207be4472c4fb52d772bfb411779d9be [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
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 <elf.h>
18#include <stdint.h>
19
20#include <memory>
21#include <string>
22
Christopher Ferrisbae69f12017-06-28 14:51:54 -070023#include <7zCrc.h>
24#include <Xz.h>
25#include <XzCrc64.h>
26
Christopher Ferrisd226a512017-07-14 10:37:19 -070027#include <unwindstack/DwarfSection.h>
28#include <unwindstack/ElfInterface.h>
29#include <unwindstack/Log.h>
30#include <unwindstack/Memory.h>
31#include <unwindstack/Regs.h>
32
Christopher Ferris61d40972017-06-12 19:14:20 -070033#include "DwarfDebugFrame.h"
34#include "DwarfEhFrame.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070035#include "DwarfEhFrameWithHdr.h"
Christopher Ferris8098b1c2017-06-20 13:54:08 -070036#include "Symbols.h"
37
Christopher Ferrisd226a512017-07-14 10:37:19 -070038namespace unwindstack {
39
Christopher Ferris8098b1c2017-06-20 13:54:08 -070040ElfInterface::~ElfInterface() {
41 for (auto symbol : symbols_) {
42 delete symbol;
43 }
44}
Christopher Ferris3958f802017-02-01 15:44:40 -080045
Christopher Ferrisbae69f12017-06-28 14:51:54 -070046Memory* ElfInterface::CreateGnuDebugdataMemory() {
47 if (gnu_debugdata_offset_ == 0 || gnu_debugdata_size_ == 0) {
48 return nullptr;
49 }
50
51 // TODO: Only call these initialization functions once.
52 CrcGenerateTable();
53 Crc64GenerateTable();
54
55 std::vector<uint8_t> src(gnu_debugdata_size_);
56 if (!memory_->Read(gnu_debugdata_offset_, src.data(), gnu_debugdata_size_)) {
57 gnu_debugdata_offset_ = 0;
58 gnu_debugdata_size_ = static_cast<uint64_t>(-1);
59 return nullptr;
60 }
61
62 ISzAlloc alloc;
63 CXzUnpacker state;
64 alloc.Alloc = [](void*, size_t size) { return malloc(size); };
65 alloc.Free = [](void*, void* ptr) { return free(ptr); };
66
67 XzUnpacker_Construct(&state, &alloc);
68
69 std::unique_ptr<MemoryBuffer> dst(new MemoryBuffer);
70 int return_val;
71 size_t src_offset = 0;
72 size_t dst_offset = 0;
73 ECoderStatus status;
74 dst->Resize(5 * gnu_debugdata_size_);
75 do {
76 size_t src_remaining = src.size() - src_offset;
77 size_t dst_remaining = dst->Size() - dst_offset;
78 if (dst_remaining < 2 * gnu_debugdata_size_) {
79 dst->Resize(dst->Size() + 2 * gnu_debugdata_size_);
80 dst_remaining += 2 * gnu_debugdata_size_;
81 }
82 return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset],
83 &src_remaining, CODER_FINISH_ANY, &status);
84 src_offset += src_remaining;
85 dst_offset += dst_remaining;
86 } while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED);
87 XzUnpacker_Free(&state);
88 if (return_val != SZ_OK || !XzUnpacker_IsStreamWasFinished(&state)) {
89 gnu_debugdata_offset_ = 0;
90 gnu_debugdata_size_ = static_cast<uint64_t>(-1);
91 return nullptr;
92 }
93
94 // Shrink back down to the exact size.
95 dst->Resize(dst_offset);
96
97 return dst.release();
98}
99
Christopher Ferris61d40972017-06-12 19:14:20 -0700100template <typename AddressType>
101void ElfInterface::InitHeadersWithTemplate() {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700102 if (eh_frame_hdr_offset_ != 0) {
103 eh_frame_.reset(new DwarfEhFrameWithHdr<AddressType>(memory_));
104 if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_)) {
105 // Even if the eh_frame_offset_ is non-zero, do not bother
106 // trying to read that since something has gone wrong.
107 eh_frame_.reset(nullptr);
108 eh_frame_hdr_offset_ = 0;
109 eh_frame_hdr_size_ = static_cast<uint64_t>(-1);
110 }
111 } else if (eh_frame_offset_ != 0) {
112 // If there is a eh_frame section without a eh_frame_hdr section.
Christopher Ferris61d40972017-06-12 19:14:20 -0700113 eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
114 if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_)) {
115 eh_frame_.reset(nullptr);
116 eh_frame_offset_ = 0;
117 eh_frame_size_ = static_cast<uint64_t>(-1);
118 }
119 }
120
121 if (debug_frame_offset_ != 0) {
122 debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
123 if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_)) {
124 debug_frame_.reset(nullptr);
125 debug_frame_offset_ = 0;
126 debug_frame_size_ = static_cast<uint64_t>(-1);
127 }
128 }
129}
130
Christopher Ferris3958f802017-02-01 15:44:40 -0800131template <typename EhdrType, typename PhdrType, typename ShdrType>
Christopher Ferrise69f4702017-10-19 16:08:58 -0700132bool ElfInterface::ReadAllHeaders(uint64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800133 EhdrType ehdr;
134 if (!memory_->Read(0, &ehdr, sizeof(ehdr))) {
135 return false;
136 }
137
Christopher Ferrise69f4702017-10-19 16:08:58 -0700138 if (!ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800139 return false;
140 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700141
142 // We could still potentially unwind without the section header
143 // information, so ignore any errors.
144 if (!ReadSectionHeaders<EhdrType, ShdrType>(ehdr)) {
145 log(0, "Malformed section header found, ignoring...");
146 }
147 return true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800148}
149
150template <typename EhdrType, typename PhdrType>
Christopher Ferrise69f4702017-10-19 16:08:58 -0700151bool ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800152 uint64_t offset = ehdr.e_phoff;
153 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
154 PhdrType phdr;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700155 if (!memory_->ReadField(offset, &phdr, &phdr.p_type, sizeof(phdr.p_type))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800156 return false;
157 }
158
Christopher Ferrise69f4702017-10-19 16:08:58 -0700159 if (HandleType(offset, phdr.p_type, *load_bias)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800160 continue;
161 }
162
163 switch (phdr.p_type) {
164 case PT_LOAD:
165 {
166 // Get the flags first, if this isn't an executable header, ignore it.
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700167 if (!memory_->ReadField(offset, &phdr, &phdr.p_flags, sizeof(phdr.p_flags))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800168 return false;
169 }
170 if ((phdr.p_flags & PF_X) == 0) {
171 continue;
172 }
173
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700174 if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800175 return false;
176 }
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700177 if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800178 return false;
179 }
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700180 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800181 return false;
182 }
183 pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
184 static_cast<size_t>(phdr.p_memsz)};
185 if (phdr.p_offset == 0) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700186 *load_bias = phdr.p_vaddr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800187 }
188 break;
189 }
190
191 case PT_GNU_EH_FRAME:
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700192 if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800193 return false;
194 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700195 // This is really the pointer to the .eh_frame_hdr section.
196 eh_frame_hdr_offset_ = phdr.p_offset;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700197 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800198 return false;
199 }
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700200 eh_frame_hdr_size_ = phdr.p_memsz;
Christopher Ferris3958f802017-02-01 15:44:40 -0800201 break;
202
203 case PT_DYNAMIC:
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700204 if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800205 return false;
206 }
207 dynamic_offset_ = phdr.p_offset;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700208 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800209 return false;
210 }
211 dynamic_size_ = phdr.p_memsz;
212 break;
213 }
214 }
215 return true;
216}
217
218template <typename EhdrType, typename ShdrType>
219bool ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
220 uint64_t offset = ehdr.e_shoff;
221 uint64_t sec_offset = 0;
222 uint64_t sec_size = 0;
223
224 // Get the location of the section header names.
225 // If something is malformed in the header table data, we aren't going
226 // to terminate, we'll simply ignore this part.
227 ShdrType shdr;
228 if (ehdr.e_shstrndx < ehdr.e_shnum) {
229 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700230 if (memory_->ReadField(sh_offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) &&
231 memory_->ReadField(sh_offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800232 sec_offset = shdr.sh_offset;
233 sec_size = shdr.sh_size;
234 }
235 }
236
237 // Skip the first header, it's always going to be NULL.
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700238 offset += ehdr.e_shentsize;
Christopher Ferris3958f802017-02-01 15:44:40 -0800239 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700240 if (!memory_->ReadField(offset, &shdr, &shdr.sh_type, sizeof(shdr.sh_type))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800241 return false;
242 }
243
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700244 if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
245 if (!memory_->Read(offset, &shdr, sizeof(shdr))) {
246 return false;
247 }
248 // Need to go get the information about the section that contains
249 // the string terminated names.
250 ShdrType str_shdr;
251 if (shdr.sh_link >= ehdr.e_shnum) {
252 return false;
253 }
254 uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
255 if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_type, sizeof(str_shdr.sh_type))) {
256 return false;
257 }
258 if (str_shdr.sh_type != SHT_STRTAB) {
259 return false;
260 }
261 if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_offset,
262 sizeof(str_shdr.sh_offset))) {
263 return false;
264 }
265 if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_size, sizeof(str_shdr.sh_size))) {
266 return false;
267 }
268 symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
269 str_shdr.sh_offset, str_shdr.sh_size));
270 } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800271 // Look for the .debug_frame and .gnu_debugdata.
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700272 if (!memory_->ReadField(offset, &shdr, &shdr.sh_name, sizeof(shdr.sh_name))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800273 return false;
274 }
275 if (shdr.sh_name < sec_size) {
276 std::string name;
277 if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700278 uint64_t* offset_ptr = nullptr;
279 uint64_t* size_ptr = nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800280 if (name == ".debug_frame") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700281 offset_ptr = &debug_frame_offset_;
282 size_ptr = &debug_frame_size_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800283 } else if (name == ".gnu_debugdata") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700284 offset_ptr = &gnu_debugdata_offset_;
285 size_ptr = &gnu_debugdata_size_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700286 } else if (name == ".eh_frame") {
287 offset_ptr = &eh_frame_offset_;
288 size_ptr = &eh_frame_size_;
289 } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
290 offset_ptr = &eh_frame_hdr_offset_;
291 size_ptr = &eh_frame_hdr_size_;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700292 }
293 if (offset_ptr != nullptr &&
294 memory_->ReadField(offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) &&
295 memory_->ReadField(offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) {
296 *offset_ptr = shdr.sh_offset;
297 *size_ptr = shdr.sh_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800298 }
299 }
300 }
301 }
302 }
303 return true;
304}
305
306template <typename DynType>
307bool ElfInterface::GetSonameWithTemplate(std::string* soname) {
308 if (soname_type_ == SONAME_INVALID) {
309 return false;
310 }
311 if (soname_type_ == SONAME_VALID) {
312 *soname = soname_;
313 return true;
314 }
315
316 soname_type_ = SONAME_INVALID;
317
318 uint64_t soname_offset = 0;
319 uint64_t strtab_offset = 0;
320 uint64_t strtab_size = 0;
321
322 // Find the soname location from the dynamic headers section.
323 DynType dyn;
324 uint64_t offset = dynamic_offset_;
325 uint64_t max_offset = offset + dynamic_size_;
326 for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
327 if (!memory_->Read(offset, &dyn, sizeof(dyn))) {
328 return false;
329 }
330
331 if (dyn.d_tag == DT_STRTAB) {
332 strtab_offset = dyn.d_un.d_ptr;
333 } else if (dyn.d_tag == DT_STRSZ) {
334 strtab_size = dyn.d_un.d_val;
335 } else if (dyn.d_tag == DT_SONAME) {
336 soname_offset = dyn.d_un.d_val;
337 } else if (dyn.d_tag == DT_NULL) {
338 break;
339 }
340 }
341
342 soname_offset += strtab_offset;
343 if (soname_offset >= strtab_offset + strtab_size) {
344 return false;
345 }
346 if (!memory_->ReadString(soname_offset, &soname_)) {
347 return false;
348 }
349 soname_type_ = SONAME_VALID;
350 *soname = soname_;
351 return true;
352}
353
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700354template <typename SymType>
Christopher Ferrise69f4702017-10-19 16:08:58 -0700355bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, uint64_t load_bias, std::string* name,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700356 uint64_t* func_offset) {
357 if (symbols_.empty()) {
358 return false;
359 }
360
361 for (const auto symbol : symbols_) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700362 if (symbol->GetName<SymType>(addr, load_bias, memory_, name, func_offset)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700363 return true;
364 }
365 }
366 return false;
367}
368
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700369bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700370 // Try the eh_frame first.
371 DwarfSection* eh_frame = eh_frame_.get();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700372 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700373 return true;
374 }
375
376 // Try the debug_frame next.
377 DwarfSection* debug_frame = debug_frame_.get();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700378 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700379 return true;
380 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800381 return false;
382}
383
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700384// This is an estimation of the size of the elf file using the location
385// of the section headers and size. This assumes that the section headers
386// are at the end of the elf file. If the elf has a load bias, the size
387// will be too large, but this is acceptable.
388template <typename EhdrType>
389void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
390 EhdrType ehdr;
391 if (!memory->Read(0, &ehdr, sizeof(ehdr))) {
392 return;
393 }
394 if (ehdr.e_shnum == 0) {
395 return;
396 }
397 *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
398}
399
Christopher Ferris3958f802017-02-01 15:44:40 -0800400// Instantiate all of the needed template functions.
Christopher Ferris61d40972017-06-12 19:14:20 -0700401template void ElfInterface::InitHeadersWithTemplate<uint32_t>();
402template void ElfInterface::InitHeadersWithTemplate<uint64_t>();
403
Christopher Ferrise69f4702017-10-19 16:08:58 -0700404template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*);
405template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800406
Christopher Ferrise69f4702017-10-19 16:08:58 -0700407template bool ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, uint64_t*);
408template bool ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800409
410template bool ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
411template bool ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
412
413template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*);
414template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700415
Christopher Ferrise69f4702017-10-19 16:08:58 -0700416template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700417 uint64_t*);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700418template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700419 uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700420
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700421template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
422template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
423
Christopher Ferrisd226a512017-07-14 10:37:19 -0700424} // namespace unwindstack