blob: edf7ac2d761d0d9f905e989d57e74246a5c9fcaf [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>
21#include <string>
22
23#define LOG_TAG "unwind"
24#include <log/log.h>
25
Christopher Ferrisd226a512017-07-14 10:37:19 -070026#include <unwindstack/Elf.h>
27#include <unwindstack/ElfInterface.h>
28#include <unwindstack/MapInfo.h>
29#include <unwindstack/Memory.h>
30#include <unwindstack/Regs.h>
31
Christopher Ferris3958f802017-02-01 15:44:40 -080032#include "ElfInterfaceArm.h"
33#include "Machine.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) {
90 return valid_ && interface_->GetSoname(name);
91}
92
93uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070094 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -070095}
96
97bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070098 return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) ||
99 (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName(
100 addr, load_bias_, name, func_offset)));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700101}
102
Christopher Ferrise69f4702017-10-19 16:08:58 -0700103// The relative pc is always relative to the start of the map from which it comes.
104bool Elf::Step(uint64_t rel_pc, uint64_t elf_offset, Regs* regs, Memory* process_memory,
105 bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700106 if (!valid_) {
107 return false;
108 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700109
110 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
111 if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700112 *finished = false;
113 return true;
114 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700115
116 // Adjust the load bias to get the real relative pc.
117 if (rel_pc < load_bias_) {
118 return false;
119 }
120 rel_pc -= load_bias_;
121
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700122 return interface_->Step(rel_pc, regs, process_memory, finished) ||
123 (gnu_debugdata_interface_ &&
124 gnu_debugdata_interface_->Step(rel_pc, regs, process_memory, finished));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700125}
126
Christopher Ferris3958f802017-02-01 15:44:40 -0800127bool Elf::IsValidElf(Memory* memory) {
128 if (memory == nullptr) {
129 return false;
130 }
131
132 // Verify that this is a valid elf file.
133 uint8_t e_ident[SELFMAG + 1];
134 if (!memory->Read(0, e_ident, SELFMAG)) {
135 return false;
136 }
137
138 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
139 return false;
140 }
141 return true;
142}
143
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700144void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
145 if (!IsValidElf(memory)) {
146 *valid = false;
147 return;
148 }
149 *size = 0;
150 *valid = true;
151
152 // Now read the section header information.
153 uint8_t class_type;
154 if (!memory->Read(EI_CLASS, &class_type, 1)) {
155 return;
156 }
157 if (class_type == ELFCLASS32) {
158 ElfInterface32::GetMaxSize(memory, size);
159 } else if (class_type == ELFCLASS64) {
160 ElfInterface64::GetMaxSize(memory, size);
161 } else {
162 *valid = false;
163 }
164}
165
Christopher Ferris3958f802017-02-01 15:44:40 -0800166ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
167 if (!IsValidElf(memory)) {
168 return nullptr;
169 }
170
171 std::unique_ptr<ElfInterface> interface;
172 if (!memory->Read(EI_CLASS, &class_type_, 1)) {
173 return nullptr;
174 }
175 if (class_type_ == ELFCLASS32) {
176 Elf32_Half e_machine;
177 if (!memory->Read(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
178 return nullptr;
179 }
180
181 if (e_machine != EM_ARM && e_machine != EM_386) {
182 // Unsupported.
183 ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine);
184 return nullptr;
185 }
186
187 machine_type_ = e_machine;
188 if (e_machine == EM_ARM) {
189 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700190 } else if (e_machine == EM_386) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800191 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700192 } else {
193 ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine);
194 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800195 }
196 } else if (class_type_ == ELFCLASS64) {
197 Elf64_Half e_machine;
198 if (!memory->Read(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
199 return nullptr;
200 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800201 if (e_machine != EM_AARCH64 && e_machine != EM_X86_64) {
202 // Unsupported.
203 ALOGI("64 bit elf that is neither aarch64 nor x86_64: e_machine = %d\n", e_machine);
204 return nullptr;
205 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800206 machine_type_ = e_machine;
207 interface.reset(new ElfInterface64(memory));
208 }
209
210 return interface.release();
211}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700212
213} // namespace unwindstack