| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 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 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 28 | #include "DwarfSection.h" | 
|  | 29 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 30 | // Forward declarations. | 
|  | 31 | class Memory; | 
|  | 32 | class Regs; | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 33 | class Symbols; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 34 |  | 
|  | 35 | struct LoadInfo { | 
|  | 36 | uint64_t offset; | 
|  | 37 | uint64_t table_offset; | 
|  | 38 | size_t table_size; | 
|  | 39 | }; | 
|  | 40 |  | 
|  | 41 | enum : uint8_t { | 
|  | 42 | SONAME_UNKNOWN = 0, | 
|  | 43 | SONAME_VALID, | 
|  | 44 | SONAME_INVALID, | 
|  | 45 | }; | 
|  | 46 |  | 
|  | 47 | class ElfInterface { | 
|  | 48 | public: | 
|  | 49 | ElfInterface(Memory* memory) : memory_(memory) {} | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 50 | virtual ~ElfInterface(); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 51 |  | 
|  | 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 74 | uint64_t debug_frame_offset() { return debug_frame_offset_; } | 
|  | 75 | uint64_t debug_frame_size() { return debug_frame_size_; } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 76 | uint64_t gnu_debugdata_offset() { return gnu_debugdata_offset_; } | 
|  | 77 | uint64_t gnu_debugdata_size() { return gnu_debugdata_size_; } | 
|  | 78 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 79 | DwarfSection* eh_frame() { return eh_frame_.get(); } | 
|  | 80 | DwarfSection* debug_frame() { return debug_frame_.get(); } | 
|  | 81 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 82 | protected: | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 83 | template <typename AddressType> | 
|  | 84 | void InitHeadersWithTemplate(); | 
|  | 85 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 86 | 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 Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 98 | template <typename SymType> | 
|  | 99 | bool GetFunctionNameWithTemplate(uint64_t addr, std::string* name, uint64_t* func_offset); | 
|  | 100 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 101 | 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 122 |  | 
|  | 123 | std::unique_ptr<DwarfSection> eh_frame_; | 
|  | 124 | std::unique_ptr<DwarfSection> debug_frame_; | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 125 |  | 
|  | 126 | std::vector<Symbols*> symbols_; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 127 | }; | 
|  | 128 |  | 
|  | 129 | class 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 138 | void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint32_t>(); } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 139 |  | 
|  | 140 | bool GetSoname(std::string* soname) override { | 
|  | 141 | return ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(soname); | 
|  | 142 | } | 
|  | 143 |  | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 144 | bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) override { | 
|  | 145 | return ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(addr, name, func_offset); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 146 | } | 
|  | 147 | }; | 
|  | 148 |  | 
|  | 149 | class 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 158 | void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint64_t>(); } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 159 |  | 
|  | 160 | bool GetSoname(std::string* soname) override { | 
|  | 161 | return ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(soname); | 
|  | 162 | } | 
|  | 163 |  | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 164 | bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) override { | 
|  | 165 | return ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(addr, name, func_offset); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 166 | } | 
|  | 167 | }; | 
|  | 168 |  | 
|  | 169 | #endif  // _LIBUNWINDSTACK_ELF_INTERFACE_H |