ElfInterface.h revision e69f470933f24977825bb34ee39d32650b75107f
1/*
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
28#include <unwindstack/DwarfSection.h>
29
30namespace unwindstack {
31
32// Forward declarations.
33class Memory;
34class Regs;
35class Symbols;
36
37struct LoadInfo {
38  uint64_t offset;
39  uint64_t table_offset;
40  size_t table_size;
41};
42
43enum : uint8_t {
44  SONAME_UNKNOWN = 0,
45  SONAME_VALID,
46  SONAME_INVALID,
47};
48
49class ElfInterface {
50 public:
51  ElfInterface(Memory* memory) : memory_(memory) {}
52  virtual ~ElfInterface();
53
54  virtual bool Init(uint64_t* load_bias) = 0;
55
56  virtual void InitHeaders() = 0;
57
58  virtual bool GetSoname(std::string* name) = 0;
59
60  virtual bool GetFunctionName(uint64_t addr, uint64_t load_bias, std::string* name,
61                               uint64_t* offset) = 0;
62
63  virtual bool Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished);
64
65  Memory* CreateGnuDebugdataMemory();
66
67  Memory* memory() { return memory_; }
68
69  const std::unordered_map<uint64_t, LoadInfo>& pt_loads() { return pt_loads_; }
70
71  uint64_t dynamic_offset() { return dynamic_offset_; }
72  uint64_t dynamic_size() { return dynamic_size_; }
73  uint64_t eh_frame_offset() { return eh_frame_offset_; }
74  uint64_t eh_frame_size() { return eh_frame_size_; }
75  uint64_t debug_frame_offset() { return debug_frame_offset_; }
76  uint64_t debug_frame_size() { return debug_frame_size_; }
77  uint64_t gnu_debugdata_offset() { return gnu_debugdata_offset_; }
78  uint64_t gnu_debugdata_size() { return gnu_debugdata_size_; }
79
80  DwarfSection* eh_frame() { return eh_frame_.get(); }
81  DwarfSection* debug_frame() { return debug_frame_.get(); }
82
83 protected:
84  template <typename AddressType>
85  void InitHeadersWithTemplate();
86
87  template <typename EhdrType, typename PhdrType, typename ShdrType>
88  bool ReadAllHeaders(uint64_t* load_bias);
89
90  template <typename EhdrType, typename PhdrType>
91  bool ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias);
92
93  template <typename EhdrType, typename ShdrType>
94  bool ReadSectionHeaders(const EhdrType& ehdr);
95
96  template <typename DynType>
97  bool GetSonameWithTemplate(std::string* soname);
98
99  template <typename SymType>
100  bool GetFunctionNameWithTemplate(uint64_t addr, uint64_t load_bias, std::string* name,
101                                   uint64_t* func_offset);
102
103  virtual bool HandleType(uint64_t, uint32_t, uint64_t) { return false; }
104
105  template <typename EhdrType>
106  static void GetMaxSizeWithTemplate(Memory* memory, uint64_t* size);
107
108  Memory* memory_;
109  std::unordered_map<uint64_t, LoadInfo> pt_loads_;
110
111  // Stored elf data.
112  uint64_t dynamic_offset_ = 0;
113  uint64_t dynamic_size_ = 0;
114
115  uint64_t eh_frame_offset_ = 0;
116  uint64_t eh_frame_size_ = 0;
117
118  uint64_t debug_frame_offset_ = 0;
119  uint64_t debug_frame_size_ = 0;
120
121  uint64_t gnu_debugdata_offset_ = 0;
122  uint64_t gnu_debugdata_size_ = 0;
123
124  uint8_t soname_type_ = SONAME_UNKNOWN;
125  std::string soname_;
126
127  std::unique_ptr<DwarfSection> eh_frame_;
128  std::unique_ptr<DwarfSection> debug_frame_;
129
130  std::vector<Symbols*> symbols_;
131};
132
133class ElfInterface32 : public ElfInterface {
134 public:
135  ElfInterface32(Memory* memory) : ElfInterface(memory) {}
136  virtual ~ElfInterface32() = default;
137
138  bool Init(uint64_t* load_bias) override {
139    return ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(load_bias);
140  }
141
142  void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint32_t>(); }
143
144  bool GetSoname(std::string* soname) override {
145    return ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(soname);
146  }
147
148  bool GetFunctionName(uint64_t addr, uint64_t load_bias, std::string* name,
149                       uint64_t* func_offset) override {
150    return ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(addr, load_bias, name, func_offset);
151  }
152
153  static void GetMaxSize(Memory* memory, uint64_t* size) {
154    GetMaxSizeWithTemplate<Elf32_Ehdr>(memory, size);
155  }
156};
157
158class ElfInterface64 : public ElfInterface {
159 public:
160  ElfInterface64(Memory* memory) : ElfInterface(memory) {}
161  virtual ~ElfInterface64() = default;
162
163  bool Init(uint64_t* load_bias) override {
164    return ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(load_bias);
165  }
166
167  void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint64_t>(); }
168
169  bool GetSoname(std::string* soname) override {
170    return ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(soname);
171  }
172
173  bool GetFunctionName(uint64_t addr, uint64_t load_bias, std::string* name,
174                       uint64_t* func_offset) override {
175    return ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(addr, load_bias, name, func_offset);
176  }
177
178  static void GetMaxSize(Memory* memory, uint64_t* size) {
179    GetMaxSizeWithTemplate<Elf64_Ehdr>(memory, size);
180  }
181};
182
183}  // namespace unwindstack
184
185#endif  // _LIBUNWINDSTACK_ELF_INTERFACE_H
186