blob: d0d0d2839aca2110a5692070831408cc5f4bbc4e [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;
Christopher Ferris8098b1c2017-06-20 13:54:08 -070033class Symbols;
Christopher Ferris3958f802017-02-01 15:44:40 -080034
35struct LoadInfo {
36 uint64_t offset;
37 uint64_t table_offset;
38 size_t table_size;
39};
40
41enum : uint8_t {
42 SONAME_UNKNOWN = 0,
43 SONAME_VALID,
44 SONAME_INVALID,
45};
46
47class ElfInterface {
48 public:
49 ElfInterface(Memory* memory) : memory_(memory) {}
Christopher Ferris8098b1c2017-06-20 13:54:08 -070050 virtual ~ElfInterface();
Christopher Ferris3958f802017-02-01 15:44:40 -080051
52 virtual bool Init() = 0;
53
54 virtual void InitHeaders() = 0;
55
56 virtual bool GetSoname(std::string* name) = 0;
57
58 virtual bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* offset) = 0;
59
60 virtual bool Step(uint64_t rel_pc, Regs* regs, Memory* process_memory);
61
62 Memory* CreateGnuDebugdataMemory();
63
64 Memory* memory() { return memory_; }
65
66 const std::unordered_map<uint64_t, LoadInfo>& pt_loads() { return pt_loads_; }
67 uint64_t load_bias() { return load_bias_; }
68 void set_load_bias(uint64_t load_bias) { load_bias_ = load_bias; }
69
70 uint64_t dynamic_offset() { return dynamic_offset_; }
71 uint64_t dynamic_size() { return dynamic_size_; }
72 uint64_t eh_frame_offset() { return eh_frame_offset_; }
73 uint64_t eh_frame_size() { return eh_frame_size_; }
Christopher Ferris61d40972017-06-12 19:14:20 -070074 uint64_t debug_frame_offset() { return debug_frame_offset_; }
75 uint64_t debug_frame_size() { return debug_frame_size_; }
Christopher Ferris3958f802017-02-01 15:44:40 -080076 uint64_t gnu_debugdata_offset() { return gnu_debugdata_offset_; }
77 uint64_t gnu_debugdata_size() { return gnu_debugdata_size_; }
78
Christopher Ferris61d40972017-06-12 19:14:20 -070079 DwarfSection* eh_frame() { return eh_frame_.get(); }
80 DwarfSection* debug_frame() { return debug_frame_.get(); }
81
Christopher Ferris3958f802017-02-01 15:44:40 -080082 protected:
Christopher Ferris61d40972017-06-12 19:14:20 -070083 template <typename AddressType>
84 void InitHeadersWithTemplate();
85
Christopher Ferris3958f802017-02-01 15:44:40 -080086 template <typename EhdrType, typename PhdrType, typename ShdrType>
87 bool ReadAllHeaders();
88
89 template <typename EhdrType, typename PhdrType>
90 bool ReadProgramHeaders(const EhdrType& ehdr);
91
92 template <typename EhdrType, typename ShdrType>
93 bool ReadSectionHeaders(const EhdrType& ehdr);
94
95 template <typename DynType>
96 bool GetSonameWithTemplate(std::string* soname);
97
Christopher Ferris8098b1c2017-06-20 13:54:08 -070098 template <typename SymType>
99 bool GetFunctionNameWithTemplate(uint64_t addr, std::string* name, uint64_t* func_offset);
100
Christopher Ferris3958f802017-02-01 15:44:40 -0800101 virtual bool HandleType(uint64_t, uint32_t) { return false; }
102
103 Memory* memory_;
104 std::unordered_map<uint64_t, LoadInfo> pt_loads_;
105 uint64_t load_bias_ = 0;
106
107 // Stored elf data.
108 uint64_t dynamic_offset_ = 0;
109 uint64_t dynamic_size_ = 0;
110
111 uint64_t eh_frame_offset_ = 0;
112 uint64_t eh_frame_size_ = 0;
113
114 uint64_t debug_frame_offset_ = 0;
115 uint64_t debug_frame_size_ = 0;
116
117 uint64_t gnu_debugdata_offset_ = 0;
118 uint64_t gnu_debugdata_size_ = 0;
119
120 uint8_t soname_type_ = SONAME_UNKNOWN;
121 std::string soname_;
Christopher Ferris61d40972017-06-12 19:14:20 -0700122
123 std::unique_ptr<DwarfSection> eh_frame_;
124 std::unique_ptr<DwarfSection> debug_frame_;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700125
126 std::vector<Symbols*> symbols_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800127};
128
129class ElfInterface32 : public ElfInterface {
130 public:
131 ElfInterface32(Memory* memory) : ElfInterface(memory) {}
132 virtual ~ElfInterface32() = default;
133
134 bool Init() override {
135 return ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>();
136 }
137
Christopher Ferris61d40972017-06-12 19:14:20 -0700138 void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint32_t>(); }
Christopher Ferris3958f802017-02-01 15:44:40 -0800139
140 bool GetSoname(std::string* soname) override {
141 return ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(soname);
142 }
143
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700144 bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) override {
145 return ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(addr, name, func_offset);
Christopher Ferris3958f802017-02-01 15:44:40 -0800146 }
147};
148
149class ElfInterface64 : public ElfInterface {
150 public:
151 ElfInterface64(Memory* memory) : ElfInterface(memory) {}
152 virtual ~ElfInterface64() = default;
153
154 bool Init() override {
155 return ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>();
156 }
157
Christopher Ferris61d40972017-06-12 19:14:20 -0700158 void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint64_t>(); }
Christopher Ferris3958f802017-02-01 15:44:40 -0800159
160 bool GetSoname(std::string* soname) override {
161 return ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(soname);
162 }
163
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700164 bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) override {
165 return ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(addr, name, func_offset);
Christopher Ferris3958f802017-02-01 15:44:40 -0800166 }
167};
168
169#endif // _LIBUNWINDSTACK_ELF_INTERFACE_H