| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 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 | #include <elf.h> | 
|  | 18 | #include <stdint.h> | 
|  | 19 |  | 
|  | 20 | #include <memory> | 
|  | 21 | #include <string> | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 22 | #include <utility> | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 23 |  | 
| Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 24 | #include <7zCrc.h> | 
|  | 25 | #include <Xz.h> | 
|  | 26 | #include <XzCrc64.h> | 
|  | 27 |  | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 28 | #include <unwindstack/DwarfError.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 29 | #include <unwindstack/DwarfSection.h> | 
|  | 30 | #include <unwindstack/ElfInterface.h> | 
|  | 31 | #include <unwindstack/Log.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 32 | #include <unwindstack/Regs.h> | 
|  | 33 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 34 | #include "DwarfDebugFrame.h" | 
|  | 35 | #include "DwarfEhFrame.h" | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 36 | #include "DwarfEhFrameWithHdr.h" | 
| Casey Dahlin | 6b95a0e | 2019-03-12 17:50:52 -0700 | [diff] [blame] | 37 | #include "MemoryBuffer.h" | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 38 | #include "Symbols.h" | 
|  | 39 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 40 | namespace unwindstack { | 
|  | 41 |  | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 42 | ElfInterface::~ElfInterface() { | 
|  | 43 | for (auto symbol : symbols_) { | 
|  | 44 | delete symbol; | 
|  | 45 | } | 
|  | 46 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 47 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 48 | bool ElfInterface::IsValidPc(uint64_t pc) { | 
|  | 49 | if (!pt_loads_.empty()) { | 
|  | 50 | for (auto& entry : pt_loads_) { | 
|  | 51 | uint64_t start = entry.second.table_offset; | 
|  | 52 | uint64_t end = start + entry.second.table_size; | 
|  | 53 | if (pc >= start && pc < end) { | 
|  | 54 | return true; | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 | return false; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | // No PT_LOAD data, look for a fde for this pc in the section data. | 
|  | 61 | if (debug_frame_ != nullptr && debug_frame_->GetFdeFromPc(pc) != nullptr) { | 
|  | 62 | return true; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | if (eh_frame_ != nullptr && eh_frame_->GetFdeFromPc(pc) != nullptr) { | 
|  | 66 | return true; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | return false; | 
|  | 70 | } | 
|  | 71 |  | 
| Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 72 | Memory* ElfInterface::CreateGnuDebugdataMemory() { | 
|  | 73 | if (gnu_debugdata_offset_ == 0 || gnu_debugdata_size_ == 0) { | 
|  | 74 | return nullptr; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | // TODO: Only call these initialization functions once. | 
|  | 78 | CrcGenerateTable(); | 
|  | 79 | Crc64GenerateTable(); | 
|  | 80 |  | 
|  | 81 | std::vector<uint8_t> src(gnu_debugdata_size_); | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 82 | if (!memory_->ReadFully(gnu_debugdata_offset_, src.data(), gnu_debugdata_size_)) { | 
| Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 83 | gnu_debugdata_offset_ = 0; | 
|  | 84 | gnu_debugdata_size_ = static_cast<uint64_t>(-1); | 
|  | 85 | return nullptr; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | ISzAlloc alloc; | 
|  | 89 | CXzUnpacker state; | 
| Sen Jiang | 2ef03e0 | 2018-05-04 12:56:04 -0700 | [diff] [blame] | 90 | alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); }; | 
|  | 91 | alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); }; | 
| Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | XzUnpacker_Construct(&state, &alloc); | 
|  | 94 |  | 
|  | 95 | std::unique_ptr<MemoryBuffer> dst(new MemoryBuffer); | 
|  | 96 | int return_val; | 
|  | 97 | size_t src_offset = 0; | 
|  | 98 | size_t dst_offset = 0; | 
|  | 99 | ECoderStatus status; | 
|  | 100 | dst->Resize(5 * gnu_debugdata_size_); | 
|  | 101 | do { | 
|  | 102 | size_t src_remaining = src.size() - src_offset; | 
|  | 103 | size_t dst_remaining = dst->Size() - dst_offset; | 
|  | 104 | if (dst_remaining < 2 * gnu_debugdata_size_) { | 
|  | 105 | dst->Resize(dst->Size() + 2 * gnu_debugdata_size_); | 
|  | 106 | dst_remaining += 2 * gnu_debugdata_size_; | 
|  | 107 | } | 
|  | 108 | return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset], | 
| Sen Jiang | 2ef03e0 | 2018-05-04 12:56:04 -0700 | [diff] [blame] | 109 | &src_remaining, true, CODER_FINISH_ANY, &status); | 
| Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 110 | src_offset += src_remaining; | 
|  | 111 | dst_offset += dst_remaining; | 
|  | 112 | } while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED); | 
|  | 113 | XzUnpacker_Free(&state); | 
|  | 114 | if (return_val != SZ_OK || !XzUnpacker_IsStreamWasFinished(&state)) { | 
|  | 115 | gnu_debugdata_offset_ = 0; | 
|  | 116 | gnu_debugdata_size_ = static_cast<uint64_t>(-1); | 
|  | 117 | return nullptr; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | // Shrink back down to the exact size. | 
|  | 121 | dst->Resize(dst_offset); | 
|  | 122 |  | 
|  | 123 | return dst.release(); | 
|  | 124 | } | 
|  | 125 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 126 | template <typename AddressType> | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 127 | void ElfInterface::InitHeadersWithTemplate() { | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 128 | if (eh_frame_hdr_offset_ != 0) { | 
| Christopher Ferris | 4ca98e1 | 2019-10-29 10:21:11 -0700 | [diff] [blame] | 129 | DwarfEhFrameWithHdr<AddressType>* eh_frame_hdr = new DwarfEhFrameWithHdr<AddressType>(memory_); | 
|  | 130 | eh_frame_.reset(eh_frame_hdr); | 
|  | 131 | if (!eh_frame_hdr->EhFrameInit(eh_frame_offset_, eh_frame_size_, eh_frame_section_bias_) || | 
|  | 132 | !eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_, eh_frame_hdr_section_bias_)) { | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 133 | eh_frame_.reset(nullptr); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 134 | } | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
|  | 137 | if (eh_frame_.get() == nullptr && eh_frame_offset_ != 0) { | 
|  | 138 | // If there is an eh_frame section without an eh_frame_hdr section, | 
|  | 139 | // or using the frame hdr object failed to init. | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 140 | eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_)); | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 141 | if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, eh_frame_section_bias_)) { | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 142 | eh_frame_.reset(nullptr); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 146 | if (eh_frame_.get() == nullptr) { | 
|  | 147 | eh_frame_hdr_offset_ = 0; | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 148 | eh_frame_hdr_section_bias_ = 0; | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 149 | eh_frame_hdr_size_ = static_cast<uint64_t>(-1); | 
|  | 150 | eh_frame_offset_ = 0; | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 151 | eh_frame_section_bias_ = 0; | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 152 | eh_frame_size_ = static_cast<uint64_t>(-1); | 
|  | 153 | } | 
|  | 154 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 155 | if (debug_frame_offset_ != 0) { | 
|  | 156 | debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_)); | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 157 | if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, debug_frame_section_bias_)) { | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 158 | debug_frame_.reset(nullptr); | 
|  | 159 | debug_frame_offset_ = 0; | 
|  | 160 | debug_frame_size_ = static_cast<uint64_t>(-1); | 
|  | 161 | } | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 165 | template <typename EhdrType, typename PhdrType, typename ShdrType> | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 166 | bool ElfInterface::ReadAllHeaders(int64_t* load_bias) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 167 | EhdrType ehdr; | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 168 | if (!memory_->ReadFully(0, &ehdr, sizeof(ehdr))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 169 | last_error_.code = ERROR_MEMORY_INVALID; | 
|  | 170 | last_error_.address = 0; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 171 | return false; | 
|  | 172 | } | 
|  | 173 |  | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 174 | // If we have enough information that this is an elf file, then allow | 
|  | 175 | // malformed program and section headers. | 
|  | 176 | ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias); | 
|  | 177 | ReadSectionHeaders<EhdrType, ShdrType>(ehdr); | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 178 | return true; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
|  | 181 | template <typename EhdrType, typename PhdrType> | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 182 | int64_t ElfInterface::GetLoadBias(Memory* memory) { | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 183 | EhdrType ehdr; | 
| Christopher Ferris | cadacdf | 2019-01-15 19:10:49 -0800 | [diff] [blame] | 184 | if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) { | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 185 | return false; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | uint64_t offset = ehdr.e_phoff; | 
|  | 189 | for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) { | 
|  | 190 | PhdrType phdr; | 
| Christopher Ferris | cadacdf | 2019-01-15 19:10:49 -0800 | [diff] [blame] | 191 | if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) { | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 192 | return 0; | 
|  | 193 | } | 
| Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 194 |  | 
|  | 195 | // Find the first executable load when looking for the load bias. | 
|  | 196 | if (phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) { | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 197 | return static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 198 | } | 
|  | 199 | } | 
|  | 200 | return 0; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | template <typename EhdrType, typename PhdrType> | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 204 | void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, int64_t* load_bias) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 205 | uint64_t offset = ehdr.e_phoff; | 
| Florian Mayer | 249c90f | 2019-07-05 16:48:04 +0100 | [diff] [blame] | 206 | bool first_exec_load_header = true; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 207 | for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) { | 
|  | 208 | PhdrType phdr; | 
| Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 209 | if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) { | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 210 | return; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 213 | switch (phdr.p_type) { | 
|  | 214 | case PT_LOAD: | 
|  | 215 | { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 216 | if ((phdr.p_flags & PF_X) == 0) { | 
|  | 217 | continue; | 
|  | 218 | } | 
|  | 219 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 220 | pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr, | 
|  | 221 | static_cast<size_t>(phdr.p_memsz)}; | 
| Florian Mayer | 249c90f | 2019-07-05 16:48:04 +0100 | [diff] [blame] | 222 | // Only set the load bias from the first executable load header. | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 223 | if (first_exec_load_header) { | 
|  | 224 | *load_bias = static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 225 | } | 
| Florian Mayer | 249c90f | 2019-07-05 16:48:04 +0100 | [diff] [blame] | 226 | first_exec_load_header = false; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 227 | break; | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | case PT_GNU_EH_FRAME: | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 231 | // This is really the pointer to the .eh_frame_hdr section. | 
|  | 232 | eh_frame_hdr_offset_ = phdr.p_offset; | 
| Christopher Ferris | 5838e53 | 2019-10-21 18:59:42 -0700 | [diff] [blame] | 233 | eh_frame_hdr_section_bias_ = static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 234 | eh_frame_hdr_size_ = phdr.p_memsz; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 235 | break; | 
|  | 236 |  | 
|  | 237 | case PT_DYNAMIC: | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 238 | dynamic_offset_ = phdr.p_offset; | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 239 | dynamic_vaddr_start_ = phdr.p_vaddr; | 
|  | 240 | if (__builtin_add_overflow(dynamic_vaddr_start_, phdr.p_memsz, &dynamic_vaddr_end_)) { | 
|  | 241 | dynamic_offset_ = 0; | 
|  | 242 | dynamic_vaddr_start_ = 0; | 
|  | 243 | dynamic_vaddr_end_ = 0; | 
|  | 244 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 245 | break; | 
| Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 246 |  | 
|  | 247 | default: | 
|  | 248 | HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz); | 
|  | 249 | break; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 250 | } | 
|  | 251 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 254 | template <typename NhdrType> | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 255 | std::string ElfInterface::ReadBuildID() { | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 256 | // Ensure there is no overflow in any of the calulations below. | 
|  | 257 | uint64_t tmp; | 
|  | 258 | if (__builtin_add_overflow(gnu_build_id_offset_, gnu_build_id_size_, &tmp)) { | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 259 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 260 | } | 
|  | 261 |  | 
|  | 262 | uint64_t offset = 0; | 
|  | 263 | while (offset < gnu_build_id_size_) { | 
|  | 264 | if (gnu_build_id_size_ - offset < sizeof(NhdrType)) { | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 265 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 266 | } | 
|  | 267 | NhdrType hdr; | 
|  | 268 | if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &hdr, sizeof(hdr))) { | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 269 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 270 | } | 
|  | 271 | offset += sizeof(hdr); | 
|  | 272 |  | 
|  | 273 | if (gnu_build_id_size_ - offset < hdr.n_namesz) { | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 274 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 275 | } | 
|  | 276 | if (hdr.n_namesz > 0) { | 
|  | 277 | std::string name(hdr.n_namesz, '\0'); | 
|  | 278 | if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &(name[0]), hdr.n_namesz)) { | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 279 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
|  | 282 | // Trim trailing \0 as GNU is stored as a C string in the ELF file. | 
|  | 283 | if (name.back() == '\0') | 
|  | 284 | name.resize(name.size() - 1); | 
|  | 285 |  | 
|  | 286 | // Align hdr.n_namesz to next power multiple of 4. See man 5 elf. | 
|  | 287 | offset += (hdr.n_namesz + 3) & ~3; | 
|  | 288 |  | 
|  | 289 | if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) { | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 290 | if (gnu_build_id_size_ - offset < hdr.n_descsz || hdr.n_descsz == 0) { | 
|  | 291 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 292 | } | 
| Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 293 | std::string build_id(hdr.n_descsz, '\0'); | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 294 | if (memory_->ReadFully(gnu_build_id_offset_ + offset, &build_id[0], hdr.n_descsz)) { | 
|  | 295 | return build_id; | 
|  | 296 | } | 
|  | 297 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 298 | } | 
|  | 299 | } | 
|  | 300 | // Align hdr.n_descsz to next power multiple of 4. See man 5 elf. | 
|  | 301 | offset += (hdr.n_descsz + 3) & ~3; | 
|  | 302 | } | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 303 | return ""; | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 306 | template <typename EhdrType, typename ShdrType> | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 307 | void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 308 | uint64_t offset = ehdr.e_shoff; | 
|  | 309 | uint64_t sec_offset = 0; | 
|  | 310 | uint64_t sec_size = 0; | 
|  | 311 |  | 
|  | 312 | // Get the location of the section header names. | 
|  | 313 | // If something is malformed in the header table data, we aren't going | 
|  | 314 | // to terminate, we'll simply ignore this part. | 
|  | 315 | ShdrType shdr; | 
|  | 316 | if (ehdr.e_shstrndx < ehdr.e_shnum) { | 
|  | 317 | uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize; | 
| Christopher Ferris | 5afddb0 | 2018-06-29 16:30:55 -0700 | [diff] [blame] | 318 | if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 319 | sec_offset = shdr.sh_offset; | 
|  | 320 | sec_size = shdr.sh_size; | 
|  | 321 | } | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | // Skip the first header, it's always going to be NULL. | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 325 | offset += ehdr.e_shentsize; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 326 | for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) { | 
| Christopher Ferris | cadacdf | 2019-01-15 19:10:49 -0800 | [diff] [blame] | 327 | if (!memory_->ReadFully(offset, &shdr, sizeof(shdr))) { | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 328 | return; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 331 | if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) { | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 332 | // Need to go get the information about the section that contains | 
|  | 333 | // the string terminated names. | 
|  | 334 | ShdrType str_shdr; | 
|  | 335 | if (shdr.sh_link >= ehdr.e_shnum) { | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 336 | continue; | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 337 | } | 
|  | 338 | uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize; | 
| Christopher Ferris | cadacdf | 2019-01-15 19:10:49 -0800 | [diff] [blame] | 339 | if (!memory_->ReadFully(str_offset, &str_shdr, sizeof(str_shdr))) { | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 340 | continue; | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 341 | } | 
|  | 342 | if (str_shdr.sh_type != SHT_STRTAB) { | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 343 | continue; | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 344 | } | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 345 | symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize, | 
|  | 346 | str_shdr.sh_offset, str_shdr.sh_size)); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 347 | } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 348 | // Look for the .debug_frame and .gnu_debugdata. | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 349 | if (shdr.sh_name < sec_size) { | 
|  | 350 | std::string name; | 
|  | 351 | if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) { | 
|  | 352 | if (name == ".debug_frame") { | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 353 | debug_frame_offset_ = shdr.sh_offset; | 
|  | 354 | debug_frame_size_ = shdr.sh_size; | 
|  | 355 | debug_frame_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 356 | } else if (name == ".gnu_debugdata") { | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 357 | gnu_debugdata_offset_ = shdr.sh_offset; | 
|  | 358 | gnu_debugdata_size_ = shdr.sh_size; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 359 | } else if (name == ".eh_frame") { | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 360 | eh_frame_offset_ = shdr.sh_offset; | 
|  | 361 | eh_frame_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset; | 
|  | 362 | eh_frame_size_ = shdr.sh_size; | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 363 | } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") { | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 364 | eh_frame_hdr_offset_ = shdr.sh_offset; | 
|  | 365 | eh_frame_hdr_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset; | 
|  | 366 | eh_frame_hdr_size_ = shdr.sh_size; | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 367 | } else if (name == ".data") { | 
|  | 368 | data_offset_ = shdr.sh_offset; | 
|  | 369 | data_vaddr_start_ = shdr.sh_addr; | 
|  | 370 | if (__builtin_add_overflow(data_vaddr_start_, shdr.sh_size, &data_vaddr_end_)) { | 
|  | 371 | data_offset_ = 0; | 
|  | 372 | data_vaddr_start_ = 0; | 
|  | 373 | data_vaddr_end_ = 0; | 
|  | 374 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 375 | } | 
|  | 376 | } | 
|  | 377 | } | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 378 | } else if (shdr.sh_type == SHT_STRTAB) { | 
|  | 379 | // In order to read soname, keep track of address to offset mapping. | 
|  | 380 | strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr), | 
|  | 381 | static_cast<uint64_t>(shdr.sh_offset))); | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 382 | } else if (shdr.sh_type == SHT_NOTE) { | 
|  | 383 | if (shdr.sh_name < sec_size) { | 
|  | 384 | std::string name; | 
|  | 385 | if (memory_->ReadString(sec_offset + shdr.sh_name, &name) && | 
|  | 386 | name == ".note.gnu.build-id") { | 
|  | 387 | gnu_build_id_offset_ = shdr.sh_offset; | 
|  | 388 | gnu_build_id_size_ = shdr.sh_size; | 
|  | 389 | } | 
|  | 390 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 391 | } | 
|  | 392 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 393 | } | 
|  | 394 |  | 
|  | 395 | template <typename DynType> | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 396 | std::string ElfInterface::GetSonameWithTemplate() { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 397 | if (soname_type_ == SONAME_INVALID) { | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 398 | return ""; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 399 | } | 
|  | 400 | if (soname_type_ == SONAME_VALID) { | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 401 | return soname_; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 402 | } | 
|  | 403 |  | 
|  | 404 | soname_type_ = SONAME_INVALID; | 
|  | 405 |  | 
|  | 406 | uint64_t soname_offset = 0; | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 407 | uint64_t strtab_addr = 0; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 408 | uint64_t strtab_size = 0; | 
|  | 409 |  | 
|  | 410 | // Find the soname location from the dynamic headers section. | 
|  | 411 | DynType dyn; | 
|  | 412 | uint64_t offset = dynamic_offset_; | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 413 | uint64_t max_offset = offset + dynamic_vaddr_end_ - dynamic_vaddr_start_; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 414 | for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) { | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 415 | if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 416 | last_error_.code = ERROR_MEMORY_INVALID; | 
|  | 417 | last_error_.address = offset; | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 418 | return ""; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 419 | } | 
|  | 420 |  | 
|  | 421 | if (dyn.d_tag == DT_STRTAB) { | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 422 | strtab_addr = dyn.d_un.d_ptr; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 423 | } else if (dyn.d_tag == DT_STRSZ) { | 
|  | 424 | strtab_size = dyn.d_un.d_val; | 
|  | 425 | } else if (dyn.d_tag == DT_SONAME) { | 
|  | 426 | soname_offset = dyn.d_un.d_val; | 
|  | 427 | } else if (dyn.d_tag == DT_NULL) { | 
|  | 428 | break; | 
|  | 429 | } | 
|  | 430 | } | 
|  | 431 |  | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 432 | // Need to map the strtab address to the real offset. | 
|  | 433 | for (const auto& entry : strtabs_) { | 
|  | 434 | if (entry.first == strtab_addr) { | 
|  | 435 | soname_offset = entry.second + soname_offset; | 
|  | 436 | if (soname_offset >= entry.second + strtab_size) { | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 437 | return ""; | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 438 | } | 
|  | 439 | if (!memory_->ReadString(soname_offset, &soname_)) { | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 440 | return ""; | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 441 | } | 
|  | 442 | soname_type_ = SONAME_VALID; | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 443 | return soname_; | 
| Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 444 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 445 | } | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 446 | return ""; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 447 | } | 
|  | 448 |  | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 449 | template <typename SymType> | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 450 | bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name, | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 451 | uint64_t* func_offset) { | 
|  | 452 | if (symbols_.empty()) { | 
|  | 453 | return false; | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | for (const auto symbol : symbols_) { | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 457 | if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) { | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 458 | return true; | 
|  | 459 | } | 
|  | 460 | } | 
|  | 461 | return false; | 
|  | 462 | } | 
|  | 463 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 464 | template <typename SymType> | 
|  | 465 | bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) { | 
|  | 466 | if (symbols_.empty()) { | 
|  | 467 | return false; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | for (const auto symbol : symbols_) { | 
|  | 471 | if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) { | 
|  | 472 | return true; | 
|  | 473 | } | 
|  | 474 | } | 
|  | 475 | return false; | 
|  | 476 | } | 
|  | 477 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 478 | bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) { | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 479 | last_error_.code = ERROR_NONE; | 
|  | 480 | last_error_.address = 0; | 
|  | 481 |  | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 482 | // Try the debug_frame first since it contains the most specific unwind | 
|  | 483 | // information. | 
|  | 484 | DwarfSection* debug_frame = debug_frame_.get(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 485 | if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) { | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 486 | return true; | 
|  | 487 | } | 
|  | 488 |  | 
| Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 489 | // Try the eh_frame next. | 
|  | 490 | DwarfSection* eh_frame = eh_frame_.get(); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 491 | if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 492 | return true; | 
|  | 493 | } | 
|  | 494 |  | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 495 | if (gnu_debugdata_interface_ != nullptr && | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 496 | gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) { | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 497 | return true; | 
|  | 498 | } | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 499 |  | 
|  | 500 | // Set the error code based on the first error encountered. | 
|  | 501 | DwarfSection* section = nullptr; | 
|  | 502 | if (debug_frame_ != nullptr) { | 
|  | 503 | section = debug_frame_.get(); | 
|  | 504 | } else if (eh_frame_ != nullptr) { | 
|  | 505 | section = eh_frame_.get(); | 
|  | 506 | } else if (gnu_debugdata_interface_ != nullptr) { | 
|  | 507 | last_error_ = gnu_debugdata_interface_->last_error(); | 
|  | 508 | return false; | 
|  | 509 | } else { | 
|  | 510 | return false; | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 | // Convert the DWARF ERROR to an external error. | 
|  | 514 | DwarfErrorCode code = section->LastErrorCode(); | 
|  | 515 | switch (code) { | 
|  | 516 | case DWARF_ERROR_NONE: | 
|  | 517 | last_error_.code = ERROR_NONE; | 
|  | 518 | break; | 
|  | 519 |  | 
|  | 520 | case DWARF_ERROR_MEMORY_INVALID: | 
|  | 521 | last_error_.code = ERROR_MEMORY_INVALID; | 
|  | 522 | last_error_.address = section->LastErrorAddress(); | 
|  | 523 | break; | 
|  | 524 |  | 
|  | 525 | case DWARF_ERROR_ILLEGAL_VALUE: | 
|  | 526 | case DWARF_ERROR_ILLEGAL_STATE: | 
|  | 527 | case DWARF_ERROR_STACK_INDEX_NOT_VALID: | 
|  | 528 | case DWARF_ERROR_TOO_MANY_ITERATIONS: | 
|  | 529 | case DWARF_ERROR_CFA_NOT_DEFINED: | 
|  | 530 | case DWARF_ERROR_NO_FDES: | 
|  | 531 | last_error_.code = ERROR_UNWIND_INFO; | 
|  | 532 | break; | 
|  | 533 |  | 
|  | 534 | case DWARF_ERROR_NOT_IMPLEMENTED: | 
|  | 535 | case DWARF_ERROR_UNSUPPORTED_VERSION: | 
|  | 536 | last_error_.code = ERROR_UNSUPPORTED; | 
|  | 537 | break; | 
|  | 538 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 539 | return false; | 
|  | 540 | } | 
|  | 541 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 542 | // This is an estimation of the size of the elf file using the location | 
|  | 543 | // of the section headers and size. This assumes that the section headers | 
|  | 544 | // are at the end of the elf file. If the elf has a load bias, the size | 
|  | 545 | // will be too large, but this is acceptable. | 
|  | 546 | template <typename EhdrType> | 
|  | 547 | void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) { | 
|  | 548 | EhdrType ehdr; | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 549 | if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) { | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 550 | return; | 
|  | 551 | } | 
|  | 552 | if (ehdr.e_shnum == 0) { | 
|  | 553 | return; | 
|  | 554 | } | 
|  | 555 | *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum; | 
|  | 556 | } | 
|  | 557 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 558 | template <typename EhdrType, typename ShdrType> | 
|  | 559 | bool GetBuildIDInfo(Memory* memory, uint64_t* build_id_offset, uint64_t* build_id_size) { | 
|  | 560 | EhdrType ehdr; | 
|  | 561 | if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) { | 
|  | 562 | return false; | 
|  | 563 | } | 
|  | 564 |  | 
|  | 565 | uint64_t offset = ehdr.e_shoff; | 
|  | 566 | uint64_t sec_offset; | 
|  | 567 | uint64_t sec_size; | 
|  | 568 | ShdrType shdr; | 
|  | 569 | if (ehdr.e_shstrndx >= ehdr.e_shnum) { | 
|  | 570 | return false; | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize; | 
|  | 574 | if (!memory->ReadFully(sh_offset, &shdr, sizeof(shdr))) { | 
|  | 575 | return false; | 
|  | 576 | } | 
|  | 577 | sec_offset = shdr.sh_offset; | 
|  | 578 | sec_size = shdr.sh_size; | 
|  | 579 |  | 
|  | 580 | // Skip the first header, it's always going to be NULL. | 
|  | 581 | offset += ehdr.e_shentsize; | 
|  | 582 | for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) { | 
|  | 583 | if (!memory->ReadFully(offset, &shdr, sizeof(shdr))) { | 
|  | 584 | return false; | 
|  | 585 | } | 
|  | 586 | std::string name; | 
|  | 587 | if (shdr.sh_type == SHT_NOTE && shdr.sh_name < sec_size && | 
|  | 588 | memory->ReadString(sec_offset + shdr.sh_name, &name) && name == ".note.gnu.build-id") { | 
|  | 589 | *build_id_offset = shdr.sh_offset; | 
|  | 590 | *build_id_size = shdr.sh_size; | 
|  | 591 | return true; | 
|  | 592 | } | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | return false; | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | template <typename EhdrType, typename ShdrType, typename NhdrType> | 
|  | 599 | std::string ElfInterface::ReadBuildIDFromMemory(Memory* memory) { | 
|  | 600 | uint64_t note_offset; | 
|  | 601 | uint64_t note_size; | 
|  | 602 | if (!GetBuildIDInfo<EhdrType, ShdrType>(memory, ¬e_offset, ¬e_size)) { | 
|  | 603 | return ""; | 
|  | 604 | } | 
|  | 605 |  | 
|  | 606 | // Ensure there is no overflow in any of the calculations below. | 
|  | 607 | uint64_t tmp; | 
|  | 608 | if (__builtin_add_overflow(note_offset, note_size, &tmp)) { | 
|  | 609 | return ""; | 
|  | 610 | } | 
|  | 611 |  | 
|  | 612 | uint64_t offset = 0; | 
|  | 613 | while (offset < note_size) { | 
|  | 614 | if (note_size - offset < sizeof(NhdrType)) { | 
|  | 615 | return ""; | 
|  | 616 | } | 
|  | 617 | NhdrType hdr; | 
|  | 618 | if (!memory->ReadFully(note_offset + offset, &hdr, sizeof(hdr))) { | 
|  | 619 | return ""; | 
|  | 620 | } | 
|  | 621 | offset += sizeof(hdr); | 
|  | 622 |  | 
|  | 623 | if (note_size - offset < hdr.n_namesz) { | 
|  | 624 | return ""; | 
|  | 625 | } | 
|  | 626 | if (hdr.n_namesz > 0) { | 
|  | 627 | std::string name(hdr.n_namesz, '\0'); | 
|  | 628 | if (!memory->ReadFully(note_offset + offset, &(name[0]), hdr.n_namesz)) { | 
|  | 629 | return ""; | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 | // Trim trailing \0 as GNU is stored as a C string in the ELF file. | 
|  | 633 | if (name.back() == '\0') name.resize(name.size() - 1); | 
|  | 634 |  | 
|  | 635 | // Align hdr.n_namesz to next power multiple of 4. See man 5 elf. | 
|  | 636 | offset += (hdr.n_namesz + 3) & ~3; | 
|  | 637 |  | 
|  | 638 | if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) { | 
|  | 639 | if (note_size - offset < hdr.n_descsz || hdr.n_descsz == 0) { | 
|  | 640 | return ""; | 
|  | 641 | } | 
|  | 642 | std::string build_id(hdr.n_descsz - 1, '\0'); | 
|  | 643 | if (memory->ReadFully(note_offset + offset, &build_id[0], hdr.n_descsz)) { | 
|  | 644 | return build_id; | 
|  | 645 | } | 
|  | 646 | return ""; | 
|  | 647 | } | 
|  | 648 | } | 
|  | 649 | // Align hdr.n_descsz to next power multiple of 4. See man 5 elf. | 
|  | 650 | offset += (hdr.n_descsz + 3) & ~3; | 
|  | 651 | } | 
|  | 652 | return ""; | 
|  | 653 | } | 
|  | 654 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 655 | // Instantiate all of the needed template functions. | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 656 | template void ElfInterface::InitHeadersWithTemplate<uint32_t>(); | 
|  | 657 | template void ElfInterface::InitHeadersWithTemplate<uint64_t>(); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 658 |  | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 659 | template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(int64_t*); | 
|  | 660 | template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(int64_t*); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 661 |  | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 662 | template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, int64_t*); | 
|  | 663 | template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, int64_t*); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 664 |  | 
| Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 665 | template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&); | 
|  | 666 | template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 667 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 668 | template std::string ElfInterface::ReadBuildID<Elf32_Nhdr>(); | 
|  | 669 | template std::string ElfInterface::ReadBuildID<Elf64_Nhdr>(); | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 670 |  | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 671 | template std::string ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(); | 
|  | 672 | template std::string ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(); | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 673 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 674 | template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*, | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 675 | uint64_t*); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 676 | template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*, | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 677 | uint64_t*); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 678 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 679 | template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*); | 
|  | 680 | template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*); | 
|  | 681 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 682 | template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*); | 
|  | 683 | template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*); | 
|  | 684 |  | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 685 | template int64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*); | 
|  | 686 | template int64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*); | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 687 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 688 | template std::string ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>( | 
|  | 689 | Memory*); | 
|  | 690 | template std::string ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>( | 
|  | 691 | Memory*); | 
|  | 692 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 693 | }  // namespace unwindstack |