blob: a800c31246be8cd42a9c99aef6fc59890ab5867e [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) {
99 return valid_ && (interface_->Step(rel_pc, regs, process_memory) ||
100 (gnu_debugdata_interface_ &&
101 gnu_debugdata_interface_->Step(rel_pc, regs, process_memory)));
102}
103
104uint64_t Elf::GetLoadBias() {
105 if (!valid_) return 0;
106 return interface_->load_bias();
107}
108
Christopher Ferris3958f802017-02-01 15:44:40 -0800109bool Elf::IsValidElf(Memory* memory) {
110 if (memory == nullptr) {
111 return false;
112 }
113
114 // Verify that this is a valid elf file.
115 uint8_t e_ident[SELFMAG + 1];
116 if (!memory->Read(0, e_ident, SELFMAG)) {
117 return false;
118 }
119
120 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
121 return false;
122 }
123 return true;
124}
125
126ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
127 if (!IsValidElf(memory)) {
128 return nullptr;
129 }
130
131 std::unique_ptr<ElfInterface> interface;
132 if (!memory->Read(EI_CLASS, &class_type_, 1)) {
133 return nullptr;
134 }
135 if (class_type_ == ELFCLASS32) {
136 Elf32_Half e_machine;
137 if (!memory->Read(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
138 return nullptr;
139 }
140
141 if (e_machine != EM_ARM && e_machine != EM_386) {
142 // Unsupported.
143 ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine);
144 return nullptr;
145 }
146
147 machine_type_ = e_machine;
148 if (e_machine == EM_ARM) {
149 interface.reset(new ElfInterfaceArm(memory));
150 } else {
151 interface.reset(new ElfInterface32(memory));
152 }
153 } else if (class_type_ == ELFCLASS64) {
154 Elf64_Half e_machine;
155 if (!memory->Read(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
156 return nullptr;
157 }
158
159 if (e_machine != EM_AARCH64 && e_machine != EM_X86_64) {
160 // Unsupported.
161 ALOGI("64 bit elf that is neither aarch64 nor x86_64: e_machine = %d\n", e_machine);
162 return nullptr;
163 }
164
165 machine_type_ = e_machine;
166 interface.reset(new ElfInterface64(memory));
167 }
168
169 return interface.release();
170}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700171
172} // namespace unwindstack