blob: 4f7476d92f8889c688068e11f446ab36b003ea4d [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
38bool Elf::Init() {
39 if (!memory_) {
40 return false;
41 }
42
43 interface_.reset(CreateInterfaceFromMemory(memory_.get()));
44 if (!interface_) {
45 return false;
46 }
47
48 valid_ = interface_->Init();
49 if (valid_) {
50 interface_->InitHeaders();
51 } else {
52 interface_.reset(nullptr);
53 }
54 return valid_;
55}
56
Christopher Ferrisbae69f12017-06-28 14:51:54 -070057// It is expensive to initialize the .gnu_debugdata section. Provide a method
58// to initialize this data separately.
59void Elf::InitGnuDebugdata() {
60 if (!valid_ || interface_->gnu_debugdata_offset() == 0) {
61 return;
62 }
63
64 gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory());
65 gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get()));
66 ElfInterface* gnu = gnu_debugdata_interface_.get();
67 if (gnu == nullptr) {
68 return;
69 }
70 if (gnu->Init()) {
71 gnu->InitHeaders();
72 } else {
73 // Free all of the memory associated with the gnu_debugdata section.
74 gnu_debugdata_memory_.reset(nullptr);
75 gnu_debugdata_interface_.reset(nullptr);
76 }
77}
78
Christopher Ferrisd226a512017-07-14 10:37:19 -070079bool Elf::GetSoname(std::string* name) {
80 return valid_ && interface_->GetSoname(name);
81}
82
83uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
84 uint64_t load_bias = 0;
85 if (valid()) {
86 load_bias = interface_->load_bias();
87 }
88
89 return pc - map_info->start + load_bias + map_info->elf_offset;
90}
91
92bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
93 return valid_ && (interface_->GetFunctionName(addr, name, func_offset) ||
94 (gnu_debugdata_interface_ &&
95 gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset)));
96}
97
98bool Elf::Step(uint64_t rel_pc, Regs* regs, Memory* process_memory) {
Christopher Ferriseb4a6db2017-07-19 12:37:45 -070099 return valid_ && (regs->StepIfSignalHandler(rel_pc, this, process_memory) ||
Christopher Ferrisa0196652017-07-18 16:09:20 -0700100 interface_->Step(rel_pc, regs, process_memory) ||
Christopher Ferrisd226a512017-07-14 10:37:19 -0700101 (gnu_debugdata_interface_ &&
102 gnu_debugdata_interface_->Step(rel_pc, regs, process_memory)));
103}
104
105uint64_t Elf::GetLoadBias() {
106 if (!valid_) return 0;
107 return interface_->load_bias();
108}
109
Christopher Ferris3958f802017-02-01 15:44:40 -0800110bool Elf::IsValidElf(Memory* memory) {
111 if (memory == nullptr) {
112 return false;
113 }
114
115 // Verify that this is a valid elf file.
116 uint8_t e_ident[SELFMAG + 1];
117 if (!memory->Read(0, e_ident, SELFMAG)) {
118 return false;
119 }
120
121 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
122 return false;
123 }
124 return true;
125}
126
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700127void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
128 if (!IsValidElf(memory)) {
129 *valid = false;
130 return;
131 }
132 *size = 0;
133 *valid = true;
134
135 // Now read the section header information.
136 uint8_t class_type;
137 if (!memory->Read(EI_CLASS, &class_type, 1)) {
138 return;
139 }
140 if (class_type == ELFCLASS32) {
141 ElfInterface32::GetMaxSize(memory, size);
142 } else if (class_type == ELFCLASS64) {
143 ElfInterface64::GetMaxSize(memory, size);
144 } else {
145 *valid = false;
146 }
147}
148
Christopher Ferris3958f802017-02-01 15:44:40 -0800149ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
150 if (!IsValidElf(memory)) {
151 return nullptr;
152 }
153
154 std::unique_ptr<ElfInterface> interface;
155 if (!memory->Read(EI_CLASS, &class_type_, 1)) {
156 return nullptr;
157 }
158 if (class_type_ == ELFCLASS32) {
159 Elf32_Half e_machine;
160 if (!memory->Read(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
161 return nullptr;
162 }
163
164 if (e_machine != EM_ARM && e_machine != EM_386) {
165 // Unsupported.
166 ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine);
167 return nullptr;
168 }
169
170 machine_type_ = e_machine;
171 if (e_machine == EM_ARM) {
172 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700173 } else if (e_machine == EM_386) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800174 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700175 } else {
176 ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine);
177 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800178 }
179 } else if (class_type_ == ELFCLASS64) {
180 Elf64_Half e_machine;
181 if (!memory->Read(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
182 return nullptr;
183 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800184 if (e_machine != EM_AARCH64 && e_machine != EM_X86_64) {
185 // Unsupported.
186 ALOGI("64 bit elf that is neither aarch64 nor x86_64: e_machine = %d\n", e_machine);
187 return nullptr;
188 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800189 machine_type_ = e_machine;
190 interface.reset(new ElfInterface64(memory));
191 }
192
193 return interface.release();
194}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700195
196} // namespace unwindstack