| 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; | 
|  | 33 |  | 
|  | 34 | struct LoadInfo { | 
|  | 35 | uint64_t offset; | 
|  | 36 | uint64_t table_offset; | 
|  | 37 | size_t table_size; | 
|  | 38 | }; | 
|  | 39 |  | 
|  | 40 | enum : uint8_t { | 
|  | 41 | SONAME_UNKNOWN = 0, | 
|  | 42 | SONAME_VALID, | 
|  | 43 | SONAME_INVALID, | 
|  | 44 | }; | 
|  | 45 |  | 
|  | 46 | class 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 73 | uint64_t debug_frame_offset() { return debug_frame_offset_; } | 
|  | 74 | uint64_t debug_frame_size() { return debug_frame_size_; } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 75 | uint64_t gnu_debugdata_offset() { return gnu_debugdata_offset_; } | 
|  | 76 | uint64_t gnu_debugdata_size() { return gnu_debugdata_size_; } | 
|  | 77 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 78 | DwarfSection* eh_frame() { return eh_frame_.get(); } | 
|  | 79 | DwarfSection* debug_frame() { return debug_frame_.get(); } | 
|  | 80 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 81 | protected: | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 82 | template <typename AddressType> | 
|  | 83 | void InitHeadersWithTemplate(); | 
|  | 84 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 85 | 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | std::unique_ptr<DwarfSection> eh_frame_; | 
|  | 120 | std::unique_ptr<DwarfSection> debug_frame_; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 121 | }; | 
|  | 122 |  | 
|  | 123 | class 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 132 | void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint32_t>(); } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 133 |  | 
|  | 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 |  | 
|  | 143 | class 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 Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 152 | void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint64_t>(); } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 153 |  | 
|  | 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 |