blob: f486e231488eae8a587a9426542e3a08ca70c9c4 [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();
82 } else {
83 // Free all of the memory associated with the gnu_debugdata section.
84 gnu_debugdata_memory_.reset(nullptr);
85 gnu_debugdata_interface_.reset(nullptr);
86 }
87}
88
Christopher Ferrisd226a512017-07-14 10:37:19 -070089bool Elf::GetSoname(std::string* name) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080090 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisd226a512017-07-14 10:37:19 -070091 return valid_ && interface_->GetSoname(name);
92}
93
94uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070095 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -070096}
97
98bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080099 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700100 return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) ||
101 (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName(
102 addr, load_bias_, name, func_offset)));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700103}
104
Christopher Ferrise69f4702017-10-19 16:08:58 -0700105// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800106bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
107 Memory* process_memory, bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700108 if (!valid_) {
109 return false;
110 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700111
112 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
113 if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700114 *finished = false;
115 return true;
116 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700117
118 // Adjust the load bias to get the real relative pc.
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800119 if (adjusted_rel_pc < load_bias_) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700120 return false;
121 }
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800122 adjusted_rel_pc -= load_bias_;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700123
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800124 // Lock during the step which can update information in the object.
125 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800126 return interface_->Step(adjusted_rel_pc, regs, process_memory, finished) ||
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700127 (gnu_debugdata_interface_ &&
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800128 gnu_debugdata_interface_->Step(adjusted_rel_pc, regs, process_memory, finished));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700129}
130
Christopher Ferris3958f802017-02-01 15:44:40 -0800131bool Elf::IsValidElf(Memory* memory) {
132 if (memory == nullptr) {
133 return false;
134 }
135
136 // Verify that this is a valid elf file.
137 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700138 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800139 return false;
140 }
141
142 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
143 return false;
144 }
145 return true;
146}
147
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700148void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
149 if (!IsValidElf(memory)) {
150 *valid = false;
151 return;
152 }
153 *size = 0;
154 *valid = true;
155
156 // Now read the section header information.
157 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700158 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700159 return;
160 }
161 if (class_type == ELFCLASS32) {
162 ElfInterface32::GetMaxSize(memory, size);
163 } else if (class_type == ELFCLASS64) {
164 ElfInterface64::GetMaxSize(memory, size);
165 } else {
166 *valid = false;
167 }
168}
169
Christopher Ferris3958f802017-02-01 15:44:40 -0800170ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
171 if (!IsValidElf(memory)) {
172 return nullptr;
173 }
174
175 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700176 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800177 return nullptr;
178 }
179 if (class_type_ == ELFCLASS32) {
180 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700181 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800182 return nullptr;
183 }
184
Christopher Ferris3958f802017-02-01 15:44:40 -0800185 machine_type_ = e_machine;
186 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800187 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800188 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700189 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800190 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800191 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100192 } else if (e_machine == EM_MIPS) {
193 arch_ = ARCH_MIPS;
194 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700195 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800196 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100197 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 -0700198 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800199 }
200 } else if (class_type_ == ELFCLASS64) {
201 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700202 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800203 return nullptr;
204 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800205
206 machine_type_ = e_machine;
207 if (e_machine == EM_AARCH64) {
208 arch_ = ARCH_ARM64;
209 } else if (e_machine == EM_X86_64) {
210 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100211 } else if (e_machine == EM_MIPS) {
212 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800213 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800214 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100215 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
216 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800217 return nullptr;
218 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800219 interface.reset(new ElfInterface64(memory));
220 }
221
222 return interface.release();
223}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700224
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800225uint64_t Elf::GetLoadBias(Memory* memory) {
226 if (!IsValidElf(memory)) {
227 return 0;
228 }
229
230 uint8_t class_type;
231 if (!memory->Read(EI_CLASS, &class_type, 1)) {
232 return 0;
233 }
234
235 if (class_type == ELFCLASS32) {
236 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
237 } else if (class_type == ELFCLASS64) {
238 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
239 }
240 return 0;
241}
242
Christopher Ferrisd226a512017-07-14 10:37:19 -0700243} // namespace unwindstack