blob: f120da2b28aaadeea8ddb891a26b1703a951dca2 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 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 <string.h>
19
20#include <memory>
Christopher Ferrisbe788d82017-11-27 14:50:38 -080021#include <mutex>
Christopher Ferris3958f802017-02-01 15:44:40 -080022#include <string>
23
24#define LOG_TAG "unwind"
25#include <log/log.h>
26
Christopher Ferrisd226a512017-07-14 10:37:19 -070027#include <unwindstack/Elf.h>
28#include <unwindstack/ElfInterface.h>
29#include <unwindstack/MapInfo.h>
30#include <unwindstack/Memory.h>
31#include <unwindstack/Regs.h>
32
Christopher Ferris3958f802017-02-01 15:44:40 -080033#include "ElfInterfaceArm.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070034#include "Symbols.h"
35
36namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080037
Christopher Ferrise69f4702017-10-19 16:08:58 -070038bool Elf::Init(bool init_gnu_debugdata) {
39 load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -080040 if (!memory_) {
41 return false;
42 }
43
44 interface_.reset(CreateInterfaceFromMemory(memory_.get()));
45 if (!interface_) {
46 return false;
47 }
48
Christopher Ferrise69f4702017-10-19 16:08:58 -070049 valid_ = interface_->Init(&load_bias_);
Christopher Ferris3958f802017-02-01 15:44:40 -080050 if (valid_) {
51 interface_->InitHeaders();
Christopher Ferrise69f4702017-10-19 16:08:58 -070052 if (init_gnu_debugdata) {
53 InitGnuDebugdata();
54 } else {
55 gnu_debugdata_interface_.reset(nullptr);
56 }
Christopher Ferris3958f802017-02-01 15:44:40 -080057 } else {
58 interface_.reset(nullptr);
59 }
60 return valid_;
61}
62
Christopher Ferrisbae69f12017-06-28 14:51:54 -070063// It is expensive to initialize the .gnu_debugdata section. Provide a method
64// to initialize this data separately.
65void Elf::InitGnuDebugdata() {
66 if (!valid_ || interface_->gnu_debugdata_offset() == 0) {
67 return;
68 }
69
70 gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory());
71 gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get()));
72 ElfInterface* gnu = gnu_debugdata_interface_.get();
73 if (gnu == nullptr) {
74 return;
75 }
Christopher Ferrise69f4702017-10-19 16:08:58 -070076
77 // Ignore the load_bias from the compressed section, the correct load bias
78 // is in the uncompressed data.
79 uint64_t load_bias;
80 if (gnu->Init(&load_bias)) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070081 gnu->InitHeaders();
Christopher Ferrise7b66242017-12-15 11:17:45 -080082 interface_->SetGnuDebugdataInterface(gnu);
Christopher Ferrisbae69f12017-06-28 14:51:54 -070083 } else {
84 // Free all of the memory associated with the gnu_debugdata section.
85 gnu_debugdata_memory_.reset(nullptr);
86 gnu_debugdata_interface_.reset(nullptr);
87 }
88}
89
Christopher Ferrisd226a512017-07-14 10:37:19 -070090bool Elf::GetSoname(std::string* name) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080091 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisd226a512017-07-14 10:37:19 -070092 return valid_ && interface_->GetSoname(name);
93}
94
95uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070096 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -070097}
98
99bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800100 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700101 return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) ||
102 (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName(
103 addr, load_bias_, name, func_offset)));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700104}
105
Christopher Ferris150db122017-12-20 18:49:01 -0800106bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) {
107 if (!valid_) {
108 return false;
109 }
110
111 if (!interface_->GetGlobalVariable(name, memory_address) &&
112 (gnu_debugdata_interface_ == nullptr ||
113 !gnu_debugdata_interface_->GetGlobalVariable(name, memory_address))) {
114 return false;
115 }
116
117 // Adjust by the load bias.
118 if (*memory_address < load_bias_) {
119 return false;
120 }
121
122 *memory_address -= load_bias_;
123
124 // If this winds up in the dynamic section, then we might need to adjust
125 // the address.
126 uint64_t dynamic_end = interface_->dynamic_vaddr() + interface_->dynamic_size();
127 if (*memory_address >= interface_->dynamic_vaddr() && *memory_address < dynamic_end) {
128 if (interface_->dynamic_vaddr() > interface_->dynamic_offset()) {
129 *memory_address -= interface_->dynamic_vaddr() - interface_->dynamic_offset();
130 } else {
131 *memory_address += interface_->dynamic_offset() - interface_->dynamic_vaddr();
132 }
133 }
134 return true;
135}
136
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800137void Elf::GetLastError(ErrorData* data) {
138 if (valid_) {
139 *data = interface_->last_error();
140 }
141}
142
143ErrorCode Elf::GetLastErrorCode() {
144 if (valid_) {
145 return interface_->LastErrorCode();
146 }
147 return ERROR_NONE;
148}
149
150uint64_t Elf::GetLastErrorAddress() {
151 if (valid_) {
152 return interface_->LastErrorAddress();
153 }
154 return 0;
155}
156
Christopher Ferrise69f4702017-10-19 16:08:58 -0700157// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800158bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
159 Memory* process_memory, bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700160 if (!valid_) {
161 return false;
162 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700163
164 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
165 if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700166 *finished = false;
167 return true;
168 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700169
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800170 // Lock during the step which can update information in the object.
171 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise7b66242017-12-15 11:17:45 -0800172 return interface_->Step(adjusted_rel_pc, load_bias_, regs, process_memory, finished);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700173}
174
Christopher Ferris3958f802017-02-01 15:44:40 -0800175bool Elf::IsValidElf(Memory* memory) {
176 if (memory == nullptr) {
177 return false;
178 }
179
180 // Verify that this is a valid elf file.
181 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700182 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800183 return false;
184 }
185
186 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
187 return false;
188 }
189 return true;
190}
191
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700192void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
193 if (!IsValidElf(memory)) {
194 *valid = false;
195 return;
196 }
197 *size = 0;
198 *valid = true;
199
200 // Now read the section header information.
201 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700202 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700203 return;
204 }
205 if (class_type == ELFCLASS32) {
206 ElfInterface32::GetMaxSize(memory, size);
207 } else if (class_type == ELFCLASS64) {
208 ElfInterface64::GetMaxSize(memory, size);
209 } else {
210 *valid = false;
211 }
212}
213
Christopher Ferris150db122017-12-20 18:49:01 -0800214bool Elf::IsValidPc(uint64_t pc) {
215 if (!valid_ || pc < load_bias_) {
216 return false;
217 }
218 pc -= load_bias_;
219
220 if (interface_->IsValidPc(pc)) {
221 return true;
222 }
223
224 if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) {
225 return true;
226 }
227
228 return false;
229}
230
Christopher Ferris3958f802017-02-01 15:44:40 -0800231ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
232 if (!IsValidElf(memory)) {
233 return nullptr;
234 }
235
236 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700237 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800238 return nullptr;
239 }
240 if (class_type_ == ELFCLASS32) {
241 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700242 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800243 return nullptr;
244 }
245
Christopher Ferris3958f802017-02-01 15:44:40 -0800246 machine_type_ = e_machine;
247 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800248 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800249 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700250 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800251 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800252 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100253 } else if (e_machine == EM_MIPS) {
254 arch_ = ARCH_MIPS;
255 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700256 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800257 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100258 ALOGI("32 bit elf that is neither arm nor x86 nor mips: e_machine = %d\n", e_machine);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700259 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800260 }
261 } else if (class_type_ == ELFCLASS64) {
262 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700263 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800264 return nullptr;
265 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800266
267 machine_type_ = e_machine;
268 if (e_machine == EM_AARCH64) {
269 arch_ = ARCH_ARM64;
270 } else if (e_machine == EM_X86_64) {
271 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100272 } else if (e_machine == EM_MIPS) {
273 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800274 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800275 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100276 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
277 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800278 return nullptr;
279 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800280 interface.reset(new ElfInterface64(memory));
281 }
282
283 return interface.release();
284}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700285
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800286uint64_t Elf::GetLoadBias(Memory* memory) {
287 if (!IsValidElf(memory)) {
288 return 0;
289 }
290
291 uint8_t class_type;
292 if (!memory->Read(EI_CLASS, &class_type, 1)) {
293 return 0;
294 }
295
296 if (class_type == ELFCLASS32) {
297 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
298 } else if (class_type == ELFCLASS64) {
299 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
300 }
301 return 0;
302}
303
Christopher Ferrisd226a512017-07-14 10:37:19 -0700304} // namespace unwindstack