blob: 5f95fa81b693e37d3d06e07ba374bcd6bdcbe75c [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
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 Ferrisbeae42b2018-02-15 17:36:33 -080022#include <utility>
Christopher Ferris3958f802017-02-01 15:44:40 -080023
Christopher Ferrisbae69f12017-06-28 14:51:54 -070024#include <7zCrc.h>
25#include <Xz.h>
26#include <XzCrc64.h>
27
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080028#include <unwindstack/DwarfError.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070029#include <unwindstack/DwarfSection.h>
30#include <unwindstack/ElfInterface.h>
31#include <unwindstack/Log.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070032#include <unwindstack/Regs.h>
33
Christopher Ferris61d40972017-06-12 19:14:20 -070034#include "DwarfDebugFrame.h"
35#include "DwarfEhFrame.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070036#include "DwarfEhFrameWithHdr.h"
Casey Dahlin6b95a0e2019-03-12 17:50:52 -070037#include "MemoryBuffer.h"
Christopher Ferris8098b1c2017-06-20 13:54:08 -070038#include "Symbols.h"
39
Christopher Ferrisd226a512017-07-14 10:37:19 -070040namespace unwindstack {
41
Christopher Ferris8098b1c2017-06-20 13:54:08 -070042ElfInterface::~ElfInterface() {
43 for (auto symbol : symbols_) {
44 delete symbol;
45 }
46}
Christopher Ferris3958f802017-02-01 15:44:40 -080047
Christopher Ferris150db122017-12-20 18:49:01 -080048bool 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 Ferrisbae69f12017-06-28 14:51:54 -070072Memory* 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 Gaoef35aa52017-10-18 11:44:51 -070082 if (!memory_->ReadFully(gnu_debugdata_offset_, src.data(), gnu_debugdata_size_)) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070083 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 Jiang2ef03e02018-05-04 12:56:04 -070090 alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); };
91 alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); };
Christopher Ferrisbae69f12017-06-28 14:51:54 -070092
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 Jiang2ef03e02018-05-04 12:56:04 -0700109 &src_remaining, true, CODER_FINISH_ANY, &status);
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700110 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 Ferris61d40972017-06-12 19:14:20 -0700126template <typename AddressType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700127void ElfInterface::InitHeadersWithTemplate() {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700128 if (eh_frame_hdr_offset_ != 0) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700129 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 Ferrisc9dee842017-11-03 14:50:27 -0700133 eh_frame_.reset(nullptr);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700134 }
Christopher Ferris1a141a02018-01-24 08:52:47 -0800135 }
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 Ferris61d40972017-06-12 19:14:20 -0700140 eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
Christopher Ferris819f1312019-10-03 13:35:48 -0700141 if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, eh_frame_section_bias_)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700142 eh_frame_.reset(nullptr);
Christopher Ferris61d40972017-06-12 19:14:20 -0700143 }
144 }
145
Christopher Ferris1a141a02018-01-24 08:52:47 -0800146 if (eh_frame_.get() == nullptr) {
147 eh_frame_hdr_offset_ = 0;
Christopher Ferris819f1312019-10-03 13:35:48 -0700148 eh_frame_hdr_section_bias_ = 0;
Christopher Ferris1a141a02018-01-24 08:52:47 -0800149 eh_frame_hdr_size_ = static_cast<uint64_t>(-1);
150 eh_frame_offset_ = 0;
Christopher Ferris819f1312019-10-03 13:35:48 -0700151 eh_frame_section_bias_ = 0;
Christopher Ferris1a141a02018-01-24 08:52:47 -0800152 eh_frame_size_ = static_cast<uint64_t>(-1);
153 }
154
Christopher Ferris61d40972017-06-12 19:14:20 -0700155 if (debug_frame_offset_ != 0) {
156 debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
Christopher Ferris819f1312019-10-03 13:35:48 -0700157 if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, debug_frame_section_bias_)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700158 debug_frame_.reset(nullptr);
159 debug_frame_offset_ = 0;
160 debug_frame_size_ = static_cast<uint64_t>(-1);
161 }
162 }
163}
164
Christopher Ferris3958f802017-02-01 15:44:40 -0800165template <typename EhdrType, typename PhdrType, typename ShdrType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700166bool ElfInterface::ReadAllHeaders(int64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800167 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700168 if (!memory_->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800169 last_error_.code = ERROR_MEMORY_INVALID;
170 last_error_.address = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800171 return false;
172 }
173
Christopher Ferris5acf0692018-08-01 13:10:46 -0700174 // 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 Ferris8098b1c2017-06-20 13:54:08 -0700178 return true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800179}
180
181template <typename EhdrType, typename PhdrType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700182int64_t ElfInterface::GetLoadBias(Memory* memory) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800183 EhdrType ehdr;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800184 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800185 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 Ferriscadacdf2019-01-15 19:10:49 -0800191 if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800192 return 0;
193 }
Christopher Ferris6c8ac562019-10-02 17:41:16 -0700194
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 Ferris819f1312019-10-03 13:35:48 -0700197 return static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800198 }
199 }
200 return 0;
201}
202
203template <typename EhdrType, typename PhdrType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700204void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, int64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800205 uint64_t offset = ehdr.e_phoff;
Florian Mayer249c90f2019-07-05 16:48:04 +0100206 bool first_exec_load_header = true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800207 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
208 PhdrType phdr;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700209 if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700210 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800211 }
212
Christopher Ferris3958f802017-02-01 15:44:40 -0800213 switch (phdr.p_type) {
214 case PT_LOAD:
215 {
Christopher Ferris3958f802017-02-01 15:44:40 -0800216 if ((phdr.p_flags & PF_X) == 0) {
217 continue;
218 }
219
Christopher Ferris3958f802017-02-01 15:44:40 -0800220 pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
221 static_cast<size_t>(phdr.p_memsz)};
Florian Mayer249c90f2019-07-05 16:48:04 +0100222 // Only set the load bias from the first executable load header.
Christopher Ferris819f1312019-10-03 13:35:48 -0700223 if (first_exec_load_header) {
224 *load_bias = static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800225 }
Florian Mayer249c90f2019-07-05 16:48:04 +0100226 first_exec_load_header = false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800227 break;
228 }
229
230 case PT_GNU_EH_FRAME:
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700231 // This is really the pointer to the .eh_frame_hdr section.
232 eh_frame_hdr_offset_ = phdr.p_offset;
Christopher Ferris5838e532019-10-21 18:59:42 -0700233 eh_frame_hdr_section_bias_ = static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700234 eh_frame_hdr_size_ = phdr.p_memsz;
Christopher Ferris3958f802017-02-01 15:44:40 -0800235 break;
236
237 case PT_DYNAMIC:
Christopher Ferris3958f802017-02-01 15:44:40 -0800238 dynamic_offset_ = phdr.p_offset;
Christopher Ferris150db122017-12-20 18:49:01 -0800239 dynamic_vaddr_ = phdr.p_vaddr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800240 dynamic_size_ = phdr.p_memsz;
241 break;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700242
243 default:
244 HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
245 break;
Christopher Ferris3958f802017-02-01 15:44:40 -0800246 }
247 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800248}
249
Florian Mayerda459e52018-11-23 16:56:17 +0000250template <typename NhdrType>
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800251std::string ElfInterface::ReadBuildID() {
Florian Mayerda459e52018-11-23 16:56:17 +0000252 // Ensure there is no overflow in any of the calulations below.
253 uint64_t tmp;
254 if (__builtin_add_overflow(gnu_build_id_offset_, gnu_build_id_size_, &tmp)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800255 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000256 }
257
258 uint64_t offset = 0;
259 while (offset < gnu_build_id_size_) {
260 if (gnu_build_id_size_ - offset < sizeof(NhdrType)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800261 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000262 }
263 NhdrType hdr;
264 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &hdr, sizeof(hdr))) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800265 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000266 }
267 offset += sizeof(hdr);
268
269 if (gnu_build_id_size_ - offset < hdr.n_namesz) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800270 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000271 }
272 if (hdr.n_namesz > 0) {
273 std::string name(hdr.n_namesz, '\0');
274 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &(name[0]), hdr.n_namesz)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800275 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000276 }
277
278 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
279 if (name.back() == '\0')
280 name.resize(name.size() - 1);
281
282 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
283 offset += (hdr.n_namesz + 3) & ~3;
284
285 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800286 if (gnu_build_id_size_ - offset < hdr.n_descsz || hdr.n_descsz == 0) {
287 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000288 }
Christopher Ferris1760b452019-04-03 14:14:30 -0700289 std::string build_id(hdr.n_descsz, '\0');
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800290 if (memory_->ReadFully(gnu_build_id_offset_ + offset, &build_id[0], hdr.n_descsz)) {
291 return build_id;
292 }
293 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000294 }
295 }
296 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
297 offset += (hdr.n_descsz + 3) & ~3;
298 }
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800299 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000300}
301
Christopher Ferris3958f802017-02-01 15:44:40 -0800302template <typename EhdrType, typename ShdrType>
Christopher Ferris5acf0692018-08-01 13:10:46 -0700303void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800304 uint64_t offset = ehdr.e_shoff;
305 uint64_t sec_offset = 0;
306 uint64_t sec_size = 0;
307
308 // Get the location of the section header names.
309 // If something is malformed in the header table data, we aren't going
310 // to terminate, we'll simply ignore this part.
311 ShdrType shdr;
312 if (ehdr.e_shstrndx < ehdr.e_shnum) {
313 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700314 if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800315 sec_offset = shdr.sh_offset;
316 sec_size = shdr.sh_size;
317 }
318 }
319
320 // Skip the first header, it's always going to be NULL.
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700321 offset += ehdr.e_shentsize;
Christopher Ferris3958f802017-02-01 15:44:40 -0800322 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800323 if (!memory_->ReadFully(offset, &shdr, sizeof(shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700324 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800325 }
326
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700327 if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700328 // Need to go get the information about the section that contains
329 // the string terminated names.
330 ShdrType str_shdr;
331 if (shdr.sh_link >= ehdr.e_shnum) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700332 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700333 }
334 uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800335 if (!memory_->ReadFully(str_offset, &str_shdr, sizeof(str_shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700336 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700337 }
338 if (str_shdr.sh_type != SHT_STRTAB) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700339 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700340 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700341 symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
342 str_shdr.sh_offset, str_shdr.sh_size));
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000343 } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800344 // Look for the .debug_frame and .gnu_debugdata.
Christopher Ferris3958f802017-02-01 15:44:40 -0800345 if (shdr.sh_name < sec_size) {
346 std::string name;
347 if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
348 if (name == ".debug_frame") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700349 debug_frame_offset_ = shdr.sh_offset;
350 debug_frame_size_ = shdr.sh_size;
351 debug_frame_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800352 } else if (name == ".gnu_debugdata") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700353 gnu_debugdata_offset_ = shdr.sh_offset;
354 gnu_debugdata_size_ = shdr.sh_size;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700355 } else if (name == ".eh_frame") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700356 eh_frame_offset_ = shdr.sh_offset;
357 eh_frame_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset;
358 eh_frame_size_ = shdr.sh_size;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700359 } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700360 eh_frame_hdr_offset_ = shdr.sh_offset;
361 eh_frame_hdr_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset;
362 eh_frame_hdr_size_ = shdr.sh_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800363 }
364 }
365 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800366 } else if (shdr.sh_type == SHT_STRTAB) {
367 // In order to read soname, keep track of address to offset mapping.
368 strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr),
369 static_cast<uint64_t>(shdr.sh_offset)));
Florian Mayerda459e52018-11-23 16:56:17 +0000370 } else if (shdr.sh_type == SHT_NOTE) {
371 if (shdr.sh_name < sec_size) {
372 std::string name;
373 if (memory_->ReadString(sec_offset + shdr.sh_name, &name) &&
374 name == ".note.gnu.build-id") {
375 gnu_build_id_offset_ = shdr.sh_offset;
376 gnu_build_id_size_ = shdr.sh_size;
377 }
378 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800379 }
380 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800381}
382
383template <typename DynType>
Christopher Ferris02a6c442019-03-11 14:43:33 -0700384std::string ElfInterface::GetSonameWithTemplate() {
Christopher Ferris3958f802017-02-01 15:44:40 -0800385 if (soname_type_ == SONAME_INVALID) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700386 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800387 }
388 if (soname_type_ == SONAME_VALID) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700389 return soname_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800390 }
391
392 soname_type_ = SONAME_INVALID;
393
394 uint64_t soname_offset = 0;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800395 uint64_t strtab_addr = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800396 uint64_t strtab_size = 0;
397
398 // Find the soname location from the dynamic headers section.
399 DynType dyn;
400 uint64_t offset = dynamic_offset_;
401 uint64_t max_offset = offset + dynamic_size_;
402 for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
Josh Gaoef35aa52017-10-18 11:44:51 -0700403 if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800404 last_error_.code = ERROR_MEMORY_INVALID;
405 last_error_.address = offset;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700406 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800407 }
408
409 if (dyn.d_tag == DT_STRTAB) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800410 strtab_addr = dyn.d_un.d_ptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800411 } else if (dyn.d_tag == DT_STRSZ) {
412 strtab_size = dyn.d_un.d_val;
413 } else if (dyn.d_tag == DT_SONAME) {
414 soname_offset = dyn.d_un.d_val;
415 } else if (dyn.d_tag == DT_NULL) {
416 break;
417 }
418 }
419
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800420 // Need to map the strtab address to the real offset.
421 for (const auto& entry : strtabs_) {
422 if (entry.first == strtab_addr) {
423 soname_offset = entry.second + soname_offset;
424 if (soname_offset >= entry.second + strtab_size) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700425 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800426 }
427 if (!memory_->ReadString(soname_offset, &soname_)) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700428 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800429 }
430 soname_type_ = SONAME_VALID;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700431 return soname_;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800432 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800433 }
Christopher Ferris02a6c442019-03-11 14:43:33 -0700434 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800435}
436
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700437template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700438bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700439 uint64_t* func_offset) {
440 if (symbols_.empty()) {
441 return false;
442 }
443
444 for (const auto symbol : symbols_) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700445 if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700446 return true;
447 }
448 }
449 return false;
450}
451
Christopher Ferris150db122017-12-20 18:49:01 -0800452template <typename SymType>
453bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
454 if (symbols_.empty()) {
455 return false;
456 }
457
458 for (const auto symbol : symbols_) {
459 if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
460 return true;
461 }
462 }
463 return false;
464}
465
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700466bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800467 last_error_.code = ERROR_NONE;
468 last_error_.address = 0;
469
Christopher Ferris1a141a02018-01-24 08:52:47 -0800470 // Try the debug_frame first since it contains the most specific unwind
471 // information.
472 DwarfSection* debug_frame = debug_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700473 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700474 return true;
475 }
476
Christopher Ferris1a141a02018-01-24 08:52:47 -0800477 // Try the eh_frame next.
478 DwarfSection* eh_frame = eh_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700479 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800480 return true;
481 }
482
Christopher Ferrise7b66242017-12-15 11:17:45 -0800483 if (gnu_debugdata_interface_ != nullptr &&
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700484 gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700485 return true;
486 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800487
488 // Set the error code based on the first error encountered.
489 DwarfSection* section = nullptr;
490 if (debug_frame_ != nullptr) {
491 section = debug_frame_.get();
492 } else if (eh_frame_ != nullptr) {
493 section = eh_frame_.get();
494 } else if (gnu_debugdata_interface_ != nullptr) {
495 last_error_ = gnu_debugdata_interface_->last_error();
496 return false;
497 } else {
498 return false;
499 }
500
501 // Convert the DWARF ERROR to an external error.
502 DwarfErrorCode code = section->LastErrorCode();
503 switch (code) {
504 case DWARF_ERROR_NONE:
505 last_error_.code = ERROR_NONE;
506 break;
507
508 case DWARF_ERROR_MEMORY_INVALID:
509 last_error_.code = ERROR_MEMORY_INVALID;
510 last_error_.address = section->LastErrorAddress();
511 break;
512
513 case DWARF_ERROR_ILLEGAL_VALUE:
514 case DWARF_ERROR_ILLEGAL_STATE:
515 case DWARF_ERROR_STACK_INDEX_NOT_VALID:
516 case DWARF_ERROR_TOO_MANY_ITERATIONS:
517 case DWARF_ERROR_CFA_NOT_DEFINED:
518 case DWARF_ERROR_NO_FDES:
519 last_error_.code = ERROR_UNWIND_INFO;
520 break;
521
522 case DWARF_ERROR_NOT_IMPLEMENTED:
523 case DWARF_ERROR_UNSUPPORTED_VERSION:
524 last_error_.code = ERROR_UNSUPPORTED;
525 break;
526 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800527 return false;
528}
529
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700530// This is an estimation of the size of the elf file using the location
531// of the section headers and size. This assumes that the section headers
532// are at the end of the elf file. If the elf has a load bias, the size
533// will be too large, but this is acceptable.
534template <typename EhdrType>
535void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
536 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700537 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700538 return;
539 }
540 if (ehdr.e_shnum == 0) {
541 return;
542 }
543 *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
544}
545
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800546template <typename EhdrType, typename ShdrType>
547bool GetBuildIDInfo(Memory* memory, uint64_t* build_id_offset, uint64_t* build_id_size) {
548 EhdrType ehdr;
549 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
550 return false;
551 }
552
553 uint64_t offset = ehdr.e_shoff;
554 uint64_t sec_offset;
555 uint64_t sec_size;
556 ShdrType shdr;
557 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
558 return false;
559 }
560
561 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
562 if (!memory->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
563 return false;
564 }
565 sec_offset = shdr.sh_offset;
566 sec_size = shdr.sh_size;
567
568 // Skip the first header, it's always going to be NULL.
569 offset += ehdr.e_shentsize;
570 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
571 if (!memory->ReadFully(offset, &shdr, sizeof(shdr))) {
572 return false;
573 }
574 std::string name;
575 if (shdr.sh_type == SHT_NOTE && shdr.sh_name < sec_size &&
576 memory->ReadString(sec_offset + shdr.sh_name, &name) && name == ".note.gnu.build-id") {
577 *build_id_offset = shdr.sh_offset;
578 *build_id_size = shdr.sh_size;
579 return true;
580 }
581 }
582
583 return false;
584}
585
586template <typename EhdrType, typename ShdrType, typename NhdrType>
587std::string ElfInterface::ReadBuildIDFromMemory(Memory* memory) {
588 uint64_t note_offset;
589 uint64_t note_size;
590 if (!GetBuildIDInfo<EhdrType, ShdrType>(memory, &note_offset, &note_size)) {
591 return "";
592 }
593
594 // Ensure there is no overflow in any of the calculations below.
595 uint64_t tmp;
596 if (__builtin_add_overflow(note_offset, note_size, &tmp)) {
597 return "";
598 }
599
600 uint64_t offset = 0;
601 while (offset < note_size) {
602 if (note_size - offset < sizeof(NhdrType)) {
603 return "";
604 }
605 NhdrType hdr;
606 if (!memory->ReadFully(note_offset + offset, &hdr, sizeof(hdr))) {
607 return "";
608 }
609 offset += sizeof(hdr);
610
611 if (note_size - offset < hdr.n_namesz) {
612 return "";
613 }
614 if (hdr.n_namesz > 0) {
615 std::string name(hdr.n_namesz, '\0');
616 if (!memory->ReadFully(note_offset + offset, &(name[0]), hdr.n_namesz)) {
617 return "";
618 }
619
620 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
621 if (name.back() == '\0') name.resize(name.size() - 1);
622
623 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
624 offset += (hdr.n_namesz + 3) & ~3;
625
626 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
627 if (note_size - offset < hdr.n_descsz || hdr.n_descsz == 0) {
628 return "";
629 }
630 std::string build_id(hdr.n_descsz - 1, '\0');
631 if (memory->ReadFully(note_offset + offset, &build_id[0], hdr.n_descsz)) {
632 return build_id;
633 }
634 return "";
635 }
636 }
637 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
638 offset += (hdr.n_descsz + 3) & ~3;
639 }
640 return "";
641}
642
Christopher Ferris3958f802017-02-01 15:44:40 -0800643// Instantiate all of the needed template functions.
Christopher Ferris819f1312019-10-03 13:35:48 -0700644template void ElfInterface::InitHeadersWithTemplate<uint32_t>();
645template void ElfInterface::InitHeadersWithTemplate<uint64_t>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700646
Christopher Ferris819f1312019-10-03 13:35:48 -0700647template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(int64_t*);
648template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(int64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800649
Christopher Ferris819f1312019-10-03 13:35:48 -0700650template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, int64_t*);
651template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, int64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800652
Christopher Ferris5acf0692018-08-01 13:10:46 -0700653template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
654template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
Christopher Ferris3958f802017-02-01 15:44:40 -0800655
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800656template std::string ElfInterface::ReadBuildID<Elf32_Nhdr>();
657template std::string ElfInterface::ReadBuildID<Elf64_Nhdr>();
Florian Mayerda459e52018-11-23 16:56:17 +0000658
Christopher Ferris02a6c442019-03-11 14:43:33 -0700659template std::string ElfInterface::GetSonameWithTemplate<Elf32_Dyn>();
660template std::string ElfInterface::GetSonameWithTemplate<Elf64_Dyn>();
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700661
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700662template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700663 uint64_t*);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700664template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700665 uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700666
Christopher Ferris150db122017-12-20 18:49:01 -0800667template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
668template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
669
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700670template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
671template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
672
Christopher Ferris819f1312019-10-03 13:35:48 -0700673template int64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
674template int64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800675
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800676template std::string ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(
677 Memory*);
678template std::string ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(
679 Memory*);
680
Christopher Ferrisd226a512017-07-14 10:37:19 -0700681} // namespace unwindstack