blob: 3a7f7cb7ba499210061ec58dfdc64d06bae7824d [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 Ferris61d40972017-06-12 19:14:20 -070023#include "DwarfDebugFrame.h"
24#include "DwarfEhFrame.h"
Christopher Ferris8098b1c2017-06-20 13:54:08 -070025#include "DwarfSection.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080026#include "ElfInterface.h"
Christopher Ferris8098b1c2017-06-20 13:54:08 -070027#include "Log.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080028#include "Memory.h"
29#include "Regs.h"
Christopher Ferris8098b1c2017-06-20 13:54:08 -070030#include "Symbols.h"
31
32ElfInterface::~ElfInterface() {
33 for (auto symbol : symbols_) {
34 delete symbol;
35 }
36}
Christopher Ferris3958f802017-02-01 15:44:40 -080037
Christopher Ferris61d40972017-06-12 19:14:20 -070038template <typename AddressType>
39void ElfInterface::InitHeadersWithTemplate() {
40 if (eh_frame_offset_ != 0) {
41 eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
42 if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_)) {
43 eh_frame_.reset(nullptr);
44 eh_frame_offset_ = 0;
45 eh_frame_size_ = static_cast<uint64_t>(-1);
46 }
47 }
48
49 if (debug_frame_offset_ != 0) {
50 debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
51 if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_)) {
52 debug_frame_.reset(nullptr);
53 debug_frame_offset_ = 0;
54 debug_frame_size_ = static_cast<uint64_t>(-1);
55 }
56 }
57}
58
Christopher Ferris3958f802017-02-01 15:44:40 -080059template <typename EhdrType, typename PhdrType, typename ShdrType>
60bool ElfInterface::ReadAllHeaders() {
61 EhdrType ehdr;
62 if (!memory_->Read(0, &ehdr, sizeof(ehdr))) {
63 return false;
64 }
65
66 if (!ReadProgramHeaders<EhdrType, PhdrType>(ehdr)) {
67 return false;
68 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -070069
70 // We could still potentially unwind without the section header
71 // information, so ignore any errors.
72 if (!ReadSectionHeaders<EhdrType, ShdrType>(ehdr)) {
73 log(0, "Malformed section header found, ignoring...");
74 }
75 return true;
Christopher Ferris3958f802017-02-01 15:44:40 -080076}
77
78template <typename EhdrType, typename PhdrType>
79bool ElfInterface::ReadProgramHeaders(const EhdrType& ehdr) {
80 uint64_t offset = ehdr.e_phoff;
81 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
82 PhdrType phdr;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070083 if (!memory_->ReadField(offset, &phdr, &phdr.p_type, sizeof(phdr.p_type))) {
Christopher Ferris3958f802017-02-01 15:44:40 -080084 return false;
85 }
86
87 if (HandleType(offset, phdr.p_type)) {
88 continue;
89 }
90
91 switch (phdr.p_type) {
92 case PT_LOAD:
93 {
94 // Get the flags first, if this isn't an executable header, ignore it.
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070095 if (!memory_->ReadField(offset, &phdr, &phdr.p_flags, sizeof(phdr.p_flags))) {
Christopher Ferris3958f802017-02-01 15:44:40 -080096 return false;
97 }
98 if ((phdr.p_flags & PF_X) == 0) {
99 continue;
100 }
101
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700102 if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800103 return false;
104 }
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700105 if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800106 return false;
107 }
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700108 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800109 return false;
110 }
111 pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
112 static_cast<size_t>(phdr.p_memsz)};
113 if (phdr.p_offset == 0) {
114 load_bias_ = phdr.p_vaddr;
115 }
116 break;
117 }
118
119 case PT_GNU_EH_FRAME:
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700120 if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800121 return false;
122 }
123 eh_frame_offset_ = phdr.p_offset;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700124 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800125 return false;
126 }
127 eh_frame_size_ = phdr.p_memsz;
128 break;
129
130 case PT_DYNAMIC:
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700131 if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800132 return false;
133 }
134 dynamic_offset_ = phdr.p_offset;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700135 if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800136 return false;
137 }
138 dynamic_size_ = phdr.p_memsz;
139 break;
140 }
141 }
142 return true;
143}
144
145template <typename EhdrType, typename ShdrType>
146bool ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
147 uint64_t offset = ehdr.e_shoff;
148 uint64_t sec_offset = 0;
149 uint64_t sec_size = 0;
150
151 // Get the location of the section header names.
152 // If something is malformed in the header table data, we aren't going
153 // to terminate, we'll simply ignore this part.
154 ShdrType shdr;
155 if (ehdr.e_shstrndx < ehdr.e_shnum) {
156 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700157 if (memory_->ReadField(sh_offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) &&
158 memory_->ReadField(sh_offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800159 sec_offset = shdr.sh_offset;
160 sec_size = shdr.sh_size;
161 }
162 }
163
164 // Skip the first header, it's always going to be NULL.
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700165 offset += ehdr.e_shentsize;
Christopher Ferris3958f802017-02-01 15:44:40 -0800166 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700167 if (!memory_->ReadField(offset, &shdr, &shdr.sh_type, sizeof(shdr.sh_type))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800168 return false;
169 }
170
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700171 if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
172 if (!memory_->Read(offset, &shdr, sizeof(shdr))) {
173 return false;
174 }
175 // Need to go get the information about the section that contains
176 // the string terminated names.
177 ShdrType str_shdr;
178 if (shdr.sh_link >= ehdr.e_shnum) {
179 return false;
180 }
181 uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
182 if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_type, sizeof(str_shdr.sh_type))) {
183 return false;
184 }
185 if (str_shdr.sh_type != SHT_STRTAB) {
186 return false;
187 }
188 if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_offset,
189 sizeof(str_shdr.sh_offset))) {
190 return false;
191 }
192 if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_size, sizeof(str_shdr.sh_size))) {
193 return false;
194 }
195 symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
196 str_shdr.sh_offset, str_shdr.sh_size));
197 } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800198 // Look for the .debug_frame and .gnu_debugdata.
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700199 if (!memory_->ReadField(offset, &shdr, &shdr.sh_name, sizeof(shdr.sh_name))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800200 return false;
201 }
202 if (shdr.sh_name < sec_size) {
203 std::string name;
204 if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700205 uint64_t* offset_ptr = nullptr;
206 uint64_t* size_ptr = nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800207 if (name == ".debug_frame") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700208 offset_ptr = &debug_frame_offset_;
209 size_ptr = &debug_frame_size_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800210 } else if (name == ".gnu_debugdata") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700211 offset_ptr = &gnu_debugdata_offset_;
212 size_ptr = &gnu_debugdata_size_;
213 }
214 if (offset_ptr != nullptr &&
215 memory_->ReadField(offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) &&
216 memory_->ReadField(offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) {
217 *offset_ptr = shdr.sh_offset;
218 *size_ptr = shdr.sh_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800219 }
220 }
221 }
222 }
223 }
224 return true;
225}
226
227template <typename DynType>
228bool ElfInterface::GetSonameWithTemplate(std::string* soname) {
229 if (soname_type_ == SONAME_INVALID) {
230 return false;
231 }
232 if (soname_type_ == SONAME_VALID) {
233 *soname = soname_;
234 return true;
235 }
236
237 soname_type_ = SONAME_INVALID;
238
239 uint64_t soname_offset = 0;
240 uint64_t strtab_offset = 0;
241 uint64_t strtab_size = 0;
242
243 // Find the soname location from the dynamic headers section.
244 DynType dyn;
245 uint64_t offset = dynamic_offset_;
246 uint64_t max_offset = offset + dynamic_size_;
247 for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
248 if (!memory_->Read(offset, &dyn, sizeof(dyn))) {
249 return false;
250 }
251
252 if (dyn.d_tag == DT_STRTAB) {
253 strtab_offset = dyn.d_un.d_ptr;
254 } else if (dyn.d_tag == DT_STRSZ) {
255 strtab_size = dyn.d_un.d_val;
256 } else if (dyn.d_tag == DT_SONAME) {
257 soname_offset = dyn.d_un.d_val;
258 } else if (dyn.d_tag == DT_NULL) {
259 break;
260 }
261 }
262
263 soname_offset += strtab_offset;
264 if (soname_offset >= strtab_offset + strtab_size) {
265 return false;
266 }
267 if (!memory_->ReadString(soname_offset, &soname_)) {
268 return false;
269 }
270 soname_type_ = SONAME_VALID;
271 *soname = soname_;
272 return true;
273}
274
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700275template <typename SymType>
276bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
277 uint64_t* func_offset) {
278 if (symbols_.empty()) {
279 return false;
280 }
281
282 for (const auto symbol : symbols_) {
283 if (symbol->GetName<SymType>(addr, load_bias_, memory_, name, func_offset)) {
284 return true;
285 }
286 }
287 return false;
288}
289
290bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory) {
291 // Need to subtract off the load_bias to get the correct pc.
292 if (pc < load_bias_) {
293 return false;
294 }
295 pc -= load_bias_;
296
297 // Try the eh_frame first.
298 DwarfSection* eh_frame = eh_frame_.get();
299 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory)) {
300 return true;
301 }
302
303 // Try the debug_frame next.
304 DwarfSection* debug_frame = debug_frame_.get();
305 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory)) {
306 return true;
307 }
308
Christopher Ferris3958f802017-02-01 15:44:40 -0800309 return false;
310}
311
312// Instantiate all of the needed template functions.
Christopher Ferris61d40972017-06-12 19:14:20 -0700313template void ElfInterface::InitHeadersWithTemplate<uint32_t>();
314template void ElfInterface::InitHeadersWithTemplate<uint64_t>();
315
Christopher Ferris3958f802017-02-01 15:44:40 -0800316template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>();
317template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>();
318
319template bool ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&);
320template bool ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&);
321
322template bool ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
323template bool ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
324
325template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*);
326template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700327
328template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
329 uint64_t*);
330template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
331 uint64_t*);