blob: ad1447a02b62389d3784ea3c2262a55f5650c9bc [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
26#include "Elf.h"
27#include "ElfInterface.h"
28#include "ElfInterfaceArm.h"
29#include "Machine.h"
30#include "Memory.h"
31#include "Regs.h"
32
33bool Elf::Init() {
34 if (!memory_) {
35 return false;
36 }
37
38 interface_.reset(CreateInterfaceFromMemory(memory_.get()));
39 if (!interface_) {
40 return false;
41 }
42
43 valid_ = interface_->Init();
44 if (valid_) {
45 interface_->InitHeaders();
46 } else {
47 interface_.reset(nullptr);
48 }
49 return valid_;
50}
51
Christopher Ferrisbae69f12017-06-28 14:51:54 -070052// It is expensive to initialize the .gnu_debugdata section. Provide a method
53// to initialize this data separately.
54void Elf::InitGnuDebugdata() {
55 if (!valid_ || interface_->gnu_debugdata_offset() == 0) {
56 return;
57 }
58
59 gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory());
60 gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get()));
61 ElfInterface* gnu = gnu_debugdata_interface_.get();
62 if (gnu == nullptr) {
63 return;
64 }
65 if (gnu->Init()) {
66 gnu->InitHeaders();
67 } else {
68 // Free all of the memory associated with the gnu_debugdata section.
69 gnu_debugdata_memory_.reset(nullptr);
70 gnu_debugdata_interface_.reset(nullptr);
71 }
72}
73
Christopher Ferris3958f802017-02-01 15:44:40 -080074bool Elf::IsValidElf(Memory* memory) {
75 if (memory == nullptr) {
76 return false;
77 }
78
79 // Verify that this is a valid elf file.
80 uint8_t e_ident[SELFMAG + 1];
81 if (!memory->Read(0, e_ident, SELFMAG)) {
82 return false;
83 }
84
85 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
86 return false;
87 }
88 return true;
89}
90
91ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
92 if (!IsValidElf(memory)) {
93 return nullptr;
94 }
95
96 std::unique_ptr<ElfInterface> interface;
97 if (!memory->Read(EI_CLASS, &class_type_, 1)) {
98 return nullptr;
99 }
100 if (class_type_ == ELFCLASS32) {
101 Elf32_Half e_machine;
102 if (!memory->Read(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
103 return nullptr;
104 }
105
106 if (e_machine != EM_ARM && e_machine != EM_386) {
107 // Unsupported.
108 ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine);
109 return nullptr;
110 }
111
112 machine_type_ = e_machine;
113 if (e_machine == EM_ARM) {
114 interface.reset(new ElfInterfaceArm(memory));
115 } else {
116 interface.reset(new ElfInterface32(memory));
117 }
118 } else if (class_type_ == ELFCLASS64) {
119 Elf64_Half e_machine;
120 if (!memory->Read(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
121 return nullptr;
122 }
123
124 if (e_machine != EM_AARCH64 && e_machine != EM_X86_64) {
125 // Unsupported.
126 ALOGI("64 bit elf that is neither aarch64 nor x86_64: e_machine = %d\n", e_machine);
127 return nullptr;
128 }
129
130 machine_type_ = e_machine;
131 interface.reset(new ElfInterface64(memory));
132 }
133
134 return interface.release();
135}