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