blob: 5ec4a3d2e7569ee3488e5c3d3486de15fbbd005e [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 Ferrise69f4702017-10-19 16:08:58 -0700106// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800107bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
108 Memory* process_memory, bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700109 if (!valid_) {
110 return false;
111 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700112
113 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
114 if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700115 *finished = false;
116 return true;
117 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700118
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800119 // Lock during the step which can update information in the object.
120 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise7b66242017-12-15 11:17:45 -0800121 return interface_->Step(adjusted_rel_pc, load_bias_, regs, process_memory, finished);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700122}
123
Christopher Ferris3958f802017-02-01 15:44:40 -0800124bool Elf::IsValidElf(Memory* memory) {
125 if (memory == nullptr) {
126 return false;
127 }
128
129 // Verify that this is a valid elf file.
130 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700131 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800132 return false;
133 }
134
135 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
136 return false;
137 }
138 return true;
139}
140
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700141void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
142 if (!IsValidElf(memory)) {
143 *valid = false;
144 return;
145 }
146 *size = 0;
147 *valid = true;
148
149 // Now read the section header information.
150 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700151 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700152 return;
153 }
154 if (class_type == ELFCLASS32) {
155 ElfInterface32::GetMaxSize(memory, size);
156 } else if (class_type == ELFCLASS64) {
157 ElfInterface64::GetMaxSize(memory, size);
158 } else {
159 *valid = false;
160 }
161}
162
Christopher Ferris3958f802017-02-01 15:44:40 -0800163ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
164 if (!IsValidElf(memory)) {
165 return nullptr;
166 }
167
168 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700169 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800170 return nullptr;
171 }
172 if (class_type_ == ELFCLASS32) {
173 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700174 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800175 return nullptr;
176 }
177
Christopher Ferris3958f802017-02-01 15:44:40 -0800178 machine_type_ = e_machine;
179 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800180 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800181 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700182 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800183 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800184 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100185 } else if (e_machine == EM_MIPS) {
186 arch_ = ARCH_MIPS;
187 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700188 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800189 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100190 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 -0700191 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800192 }
193 } else if (class_type_ == ELFCLASS64) {
194 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700195 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800196 return nullptr;
197 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800198
199 machine_type_ = e_machine;
200 if (e_machine == EM_AARCH64) {
201 arch_ = ARCH_ARM64;
202 } else if (e_machine == EM_X86_64) {
203 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100204 } else if (e_machine == EM_MIPS) {
205 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800206 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800207 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100208 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
209 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800210 return nullptr;
211 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800212 interface.reset(new ElfInterface64(memory));
213 }
214
215 return interface.release();
216}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700217
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800218uint64_t Elf::GetLoadBias(Memory* memory) {
219 if (!IsValidElf(memory)) {
220 return 0;
221 }
222
223 uint8_t class_type;
224 if (!memory->Read(EI_CLASS, &class_type, 1)) {
225 return 0;
226 }
227
228 if (class_type == ELFCLASS32) {
229 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
230 } else if (class_type == ELFCLASS64) {
231 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
232 }
233 return 0;
234}
235
Christopher Ferrisd226a512017-07-14 10:37:19 -0700236} // namespace unwindstack