blob: 1cc8aa0ee247a5f6038d4b2ccec6238158a1fafc [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#ifndef _LIBUNWINDSTACK_ELF_INTERFACE_H
18#define _LIBUNWINDSTACK_ELF_INTERFACE_H
19
20#include <elf.h>
21#include <stdint.h>
22
23#include <memory>
24#include <string>
25#include <unordered_map>
26#include <vector>
27
Christopher Ferris61d40972017-06-12 19:14:20 -070028#include "DwarfSection.h"
29
Christopher Ferris3958f802017-02-01 15:44:40 -080030// Forward declarations.
31class Memory;
32class Regs;
33
34struct LoadInfo {
35 uint64_t offset;
36 uint64_t table_offset;
37 size_t table_size;
38};
39
40enum : uint8_t {
41 SONAME_UNKNOWN = 0,
42 SONAME_VALID,
43 SONAME_INVALID,
44};
45
46class ElfInterface {
47 public:
48 ElfInterface(Memory* memory) : memory_(memory) {}
49 virtual ~ElfInterface() = default;
50
51 virtual bool Init() = 0;
52
53 virtual void InitHeaders() = 0;
54
55 virtual bool GetSoname(std::string* name) = 0;
56
57 virtual bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* offset) = 0;
58
59 virtual bool Step(uint64_t rel_pc, Regs* regs, Memory* process_memory);
60
61 Memory* CreateGnuDebugdataMemory();
62
63 Memory* memory() { return memory_; }
64
65 const std::unordered_map<uint64_t, LoadInfo>& pt_loads() { return pt_loads_; }
66 uint64_t load_bias() { return load_bias_; }
67 void set_load_bias(uint64_t load_bias) { load_bias_ = load_bias; }
68
69 uint64_t dynamic_offset() { return dynamic_offset_; }
70 uint64_t dynamic_size() { return dynamic_size_; }
71 uint64_t eh_frame_offset() { return eh_frame_offset_; }
72 uint64_t eh_frame_size() { return eh_frame_size_; }
Christopher Ferris61d40972017-06-12 19:14:20 -070073 uint64_t debug_frame_offset() { return debug_frame_offset_; }
74 uint64_t debug_frame_size() { return debug_frame_size_; }
Christopher Ferris3958f802017-02-01 15:44:40 -080075 uint64_t gnu_debugdata_offset() { return gnu_debugdata_offset_; }
76 uint64_t gnu_debugdata_size() { return gnu_debugdata_size_; }
77
Christopher Ferris61d40972017-06-12 19:14:20 -070078 DwarfSection* eh_frame() { return eh_frame_.get(); }
79 DwarfSection* debug_frame() { return debug_frame_.get(); }
80
Christopher Ferris3958f802017-02-01 15:44:40 -080081 protected:
Christopher Ferris61d40972017-06-12 19:14:20 -070082 template <typename AddressType>
83 void InitHeadersWithTemplate();
84
Christopher Ferris3958f802017-02-01 15:44:40 -080085 template <typename EhdrType, typename PhdrType, typename ShdrType>
86 bool ReadAllHeaders();
87
88 template <typename EhdrType, typename PhdrType>
89 bool ReadProgramHeaders(const EhdrType& ehdr);
90
91 template <typename EhdrType, typename ShdrType>
92 bool ReadSectionHeaders(const EhdrType& ehdr);
93
94 template <typename DynType>
95 bool GetSonameWithTemplate(std::string* soname);
96
97 virtual bool HandleType(uint64_t, uint32_t) { return false; }
98
99 Memory* memory_;
100 std::unordered_map<uint64_t, LoadInfo> pt_loads_;
101 uint64_t load_bias_ = 0;
102
103 // Stored elf data.
104 uint64_t dynamic_offset_ = 0;
105 uint64_t dynamic_size_ = 0;
106
107 uint64_t eh_frame_offset_ = 0;
108 uint64_t eh_frame_size_ = 0;
109
110 uint64_t debug_frame_offset_ = 0;
111 uint64_t debug_frame_size_ = 0;
112
113 uint64_t gnu_debugdata_offset_ = 0;
114 uint64_t gnu_debugdata_size_ = 0;
115
116 uint8_t soname_type_ = SONAME_UNKNOWN;
117 std::string soname_;
Christopher Ferris61d40972017-06-12 19:14:20 -0700118
119 std::unique_ptr<DwarfSection> eh_frame_;
120 std::unique_ptr<DwarfSection> debug_frame_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800121};
122
123class ElfInterface32 : public ElfInterface {
124 public:
125 ElfInterface32(Memory* memory) : ElfInterface(memory) {}
126 virtual ~ElfInterface32() = default;
127
128 bool Init() override {
129 return ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>();
130 }
131
Christopher Ferris61d40972017-06-12 19:14:20 -0700132 void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint32_t>(); }
Christopher Ferris3958f802017-02-01 15:44:40 -0800133
134 bool GetSoname(std::string* soname) override {
135 return ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(soname);
136 }
137
138 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override {
139 return false;
140 }
141};
142
143class ElfInterface64 : public ElfInterface {
144 public:
145 ElfInterface64(Memory* memory) : ElfInterface(memory) {}
146 virtual ~ElfInterface64() = default;
147
148 bool Init() override {
149 return ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>();
150 }
151
Christopher Ferris61d40972017-06-12 19:14:20 -0700152 void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint64_t>(); }
Christopher Ferris3958f802017-02-01 15:44:40 -0800153
154 bool GetSoname(std::string* soname) override {
155 return ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(soname);
156 }
157
158 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override {
159 return false;
160 }
161};
162
163#endif // _LIBUNWINDSTACK_ELF_INTERFACE_H