blob: 341275dbbce776cc339c187d84221f3c89b6f689 [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
Christopher Ferris8726d3a2019-12-19 16:10:53 -080081 // Verify the request is not larger than the max size_t value.
82 if (gnu_debugdata_size_ > SIZE_MAX) {
83 return nullptr;
84 }
85 size_t initial_buffer_size;
86 if (__builtin_mul_overflow(5, gnu_debugdata_size_, &initial_buffer_size)) {
87 return nullptr;
88 }
89
90 size_t buffer_increment;
91 if (__builtin_mul_overflow(2, gnu_debugdata_size_, &buffer_increment)) {
92 return nullptr;
93 }
94
95 std::unique_ptr<uint8_t[]> src(new (std::nothrow) uint8_t[gnu_debugdata_size_]);
96 if (src.get() == nullptr) {
97 return nullptr;
98 }
99
100 std::unique_ptr<MemoryBuffer> dst(new MemoryBuffer);
101 if (!dst->Resize(initial_buffer_size)) {
102 return nullptr;
103 }
104
105 if (!memory_->ReadFully(gnu_debugdata_offset_, src.get(), gnu_debugdata_size_)) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700106 return nullptr;
107 }
108
109 ISzAlloc alloc;
110 CXzUnpacker state;
Sen Jiang2ef03e02018-05-04 12:56:04 -0700111 alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); };
112 alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); };
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700113 XzUnpacker_Construct(&state, &alloc);
114
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700115 int return_val;
116 size_t src_offset = 0;
117 size_t dst_offset = 0;
118 ECoderStatus status;
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700119 do {
Christopher Ferris8726d3a2019-12-19 16:10:53 -0800120 size_t src_remaining = gnu_debugdata_size_ - src_offset;
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700121 size_t dst_remaining = dst->Size() - dst_offset;
Christopher Ferris8726d3a2019-12-19 16:10:53 -0800122 if (dst_remaining < buffer_increment) {
123 size_t new_size;
124 if (__builtin_add_overflow(dst->Size(), buffer_increment, &new_size) ||
125 !dst->Resize(new_size)) {
126 XzUnpacker_Free(&state);
127 return nullptr;
128 }
129 dst_remaining += buffer_increment;
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700130 }
131 return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset],
Sen Jiang2ef03e02018-05-04 12:56:04 -0700132 &src_remaining, true, CODER_FINISH_ANY, &status);
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700133 src_offset += src_remaining;
134 dst_offset += dst_remaining;
135 } while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED);
136 XzUnpacker_Free(&state);
137 if (return_val != SZ_OK || !XzUnpacker_IsStreamWasFinished(&state)) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700138 return nullptr;
139 }
140
141 // Shrink back down to the exact size.
Christopher Ferris8726d3a2019-12-19 16:10:53 -0800142 if (!dst->Resize(dst_offset)) {
143 return nullptr;
144 }
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700145
146 return dst.release();
147}
148
Christopher Ferris61d40972017-06-12 19:14:20 -0700149template <typename AddressType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700150void ElfInterface::InitHeadersWithTemplate() {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700151 if (eh_frame_hdr_offset_ != 0) {
Christopher Ferris4ca98e12019-10-29 10:21:11 -0700152 DwarfEhFrameWithHdr<AddressType>* eh_frame_hdr = new DwarfEhFrameWithHdr<AddressType>(memory_);
153 eh_frame_.reset(eh_frame_hdr);
154 if (!eh_frame_hdr->EhFrameInit(eh_frame_offset_, eh_frame_size_, eh_frame_section_bias_) ||
155 !eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_, eh_frame_hdr_section_bias_)) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700156 eh_frame_.reset(nullptr);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700157 }
Christopher Ferris1a141a02018-01-24 08:52:47 -0800158 }
159
160 if (eh_frame_.get() == nullptr && eh_frame_offset_ != 0) {
161 // If there is an eh_frame section without an eh_frame_hdr section,
162 // or using the frame hdr object failed to init.
Christopher Ferris61d40972017-06-12 19:14:20 -0700163 eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
Christopher Ferris819f1312019-10-03 13:35:48 -0700164 if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, eh_frame_section_bias_)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700165 eh_frame_.reset(nullptr);
Christopher Ferris61d40972017-06-12 19:14:20 -0700166 }
167 }
168
Christopher Ferris1a141a02018-01-24 08:52:47 -0800169 if (eh_frame_.get() == nullptr) {
170 eh_frame_hdr_offset_ = 0;
Christopher Ferris819f1312019-10-03 13:35:48 -0700171 eh_frame_hdr_section_bias_ = 0;
Christopher Ferris1a141a02018-01-24 08:52:47 -0800172 eh_frame_hdr_size_ = static_cast<uint64_t>(-1);
173 eh_frame_offset_ = 0;
Christopher Ferris819f1312019-10-03 13:35:48 -0700174 eh_frame_section_bias_ = 0;
Christopher Ferris1a141a02018-01-24 08:52:47 -0800175 eh_frame_size_ = static_cast<uint64_t>(-1);
176 }
177
Christopher Ferris61d40972017-06-12 19:14:20 -0700178 if (debug_frame_offset_ != 0) {
179 debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
Christopher Ferris819f1312019-10-03 13:35:48 -0700180 if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, debug_frame_section_bias_)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700181 debug_frame_.reset(nullptr);
182 debug_frame_offset_ = 0;
183 debug_frame_size_ = static_cast<uint64_t>(-1);
184 }
185 }
186}
187
Christopher Ferris3958f802017-02-01 15:44:40 -0800188template <typename EhdrType, typename PhdrType, typename ShdrType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700189bool ElfInterface::ReadAllHeaders(int64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800190 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700191 if (!memory_->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800192 last_error_.code = ERROR_MEMORY_INVALID;
193 last_error_.address = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800194 return false;
195 }
196
Christopher Ferris5acf0692018-08-01 13:10:46 -0700197 // If we have enough information that this is an elf file, then allow
198 // malformed program and section headers.
199 ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias);
200 ReadSectionHeaders<EhdrType, ShdrType>(ehdr);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700201 return true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800202}
203
204template <typename EhdrType, typename PhdrType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700205int64_t ElfInterface::GetLoadBias(Memory* memory) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800206 EhdrType ehdr;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800207 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800208 return false;
209 }
210
211 uint64_t offset = ehdr.e_phoff;
212 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
213 PhdrType phdr;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800214 if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800215 return 0;
216 }
Christopher Ferris6c8ac562019-10-02 17:41:16 -0700217
218 // Find the first executable load when looking for the load bias.
219 if (phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
Christopher Ferris819f1312019-10-03 13:35:48 -0700220 return static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800221 }
222 }
223 return 0;
224}
225
226template <typename EhdrType, typename PhdrType>
Christopher Ferris819f1312019-10-03 13:35:48 -0700227void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, int64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800228 uint64_t offset = ehdr.e_phoff;
Florian Mayer249c90f2019-07-05 16:48:04 +0100229 bool first_exec_load_header = true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800230 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
231 PhdrType phdr;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700232 if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700233 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800234 }
235
Christopher Ferris3958f802017-02-01 15:44:40 -0800236 switch (phdr.p_type) {
237 case PT_LOAD:
238 {
Christopher Ferris3958f802017-02-01 15:44:40 -0800239 if ((phdr.p_flags & PF_X) == 0) {
240 continue;
241 }
242
Christopher Ferris3958f802017-02-01 15:44:40 -0800243 pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
244 static_cast<size_t>(phdr.p_memsz)};
Florian Mayer249c90f2019-07-05 16:48:04 +0100245 // Only set the load bias from the first executable load header.
Christopher Ferris819f1312019-10-03 13:35:48 -0700246 if (first_exec_load_header) {
247 *load_bias = static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800248 }
Florian Mayer249c90f2019-07-05 16:48:04 +0100249 first_exec_load_header = false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800250 break;
251 }
252
253 case PT_GNU_EH_FRAME:
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700254 // This is really the pointer to the .eh_frame_hdr section.
255 eh_frame_hdr_offset_ = phdr.p_offset;
Christopher Ferris5838e532019-10-21 18:59:42 -0700256 eh_frame_hdr_section_bias_ = static_cast<uint64_t>(phdr.p_vaddr) - phdr.p_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700257 eh_frame_hdr_size_ = phdr.p_memsz;
Christopher Ferris3958f802017-02-01 15:44:40 -0800258 break;
259
260 case PT_DYNAMIC:
Christopher Ferris3958f802017-02-01 15:44:40 -0800261 dynamic_offset_ = phdr.p_offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800262 dynamic_vaddr_start_ = phdr.p_vaddr;
263 if (__builtin_add_overflow(dynamic_vaddr_start_, phdr.p_memsz, &dynamic_vaddr_end_)) {
264 dynamic_offset_ = 0;
265 dynamic_vaddr_start_ = 0;
266 dynamic_vaddr_end_ = 0;
267 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800268 break;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700269
270 default:
271 HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
272 break;
Christopher Ferris3958f802017-02-01 15:44:40 -0800273 }
274 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800275}
276
Florian Mayerda459e52018-11-23 16:56:17 +0000277template <typename NhdrType>
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800278std::string ElfInterface::ReadBuildID() {
Florian Mayerda459e52018-11-23 16:56:17 +0000279 // Ensure there is no overflow in any of the calulations below.
280 uint64_t tmp;
281 if (__builtin_add_overflow(gnu_build_id_offset_, gnu_build_id_size_, &tmp)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800282 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000283 }
284
285 uint64_t offset = 0;
286 while (offset < gnu_build_id_size_) {
287 if (gnu_build_id_size_ - offset < sizeof(NhdrType)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800288 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000289 }
290 NhdrType hdr;
291 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &hdr, sizeof(hdr))) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800292 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000293 }
294 offset += sizeof(hdr);
295
296 if (gnu_build_id_size_ - offset < hdr.n_namesz) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800297 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000298 }
299 if (hdr.n_namesz > 0) {
300 std::string name(hdr.n_namesz, '\0');
301 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &(name[0]), hdr.n_namesz)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800302 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000303 }
304
305 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
306 if (name.back() == '\0')
307 name.resize(name.size() - 1);
308
309 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
310 offset += (hdr.n_namesz + 3) & ~3;
311
312 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800313 if (gnu_build_id_size_ - offset < hdr.n_descsz || hdr.n_descsz == 0) {
314 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000315 }
Christopher Ferris1760b452019-04-03 14:14:30 -0700316 std::string build_id(hdr.n_descsz, '\0');
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800317 if (memory_->ReadFully(gnu_build_id_offset_ + offset, &build_id[0], hdr.n_descsz)) {
318 return build_id;
319 }
320 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000321 }
322 }
323 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
324 offset += (hdr.n_descsz + 3) & ~3;
325 }
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800326 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000327}
328
Christopher Ferris3958f802017-02-01 15:44:40 -0800329template <typename EhdrType, typename ShdrType>
Christopher Ferris5acf0692018-08-01 13:10:46 -0700330void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800331 uint64_t offset = ehdr.e_shoff;
332 uint64_t sec_offset = 0;
333 uint64_t sec_size = 0;
334
335 // Get the location of the section header names.
336 // If something is malformed in the header table data, we aren't going
337 // to terminate, we'll simply ignore this part.
338 ShdrType shdr;
339 if (ehdr.e_shstrndx < ehdr.e_shnum) {
340 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700341 if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800342 sec_offset = shdr.sh_offset;
343 sec_size = shdr.sh_size;
344 }
345 }
346
347 // Skip the first header, it's always going to be NULL.
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700348 offset += ehdr.e_shentsize;
Christopher Ferris3958f802017-02-01 15:44:40 -0800349 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800350 if (!memory_->ReadFully(offset, &shdr, sizeof(shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700351 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800352 }
353
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700354 if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700355 // Need to go get the information about the section that contains
356 // the string terminated names.
357 ShdrType str_shdr;
358 if (shdr.sh_link >= ehdr.e_shnum) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700359 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700360 }
361 uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800362 if (!memory_->ReadFully(str_offset, &str_shdr, sizeof(str_shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700363 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700364 }
365 if (str_shdr.sh_type != SHT_STRTAB) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700366 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700367 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700368 symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
369 str_shdr.sh_offset, str_shdr.sh_size));
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000370 } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800371 // Look for the .debug_frame and .gnu_debugdata.
Christopher Ferris3958f802017-02-01 15:44:40 -0800372 if (shdr.sh_name < sec_size) {
373 std::string name;
374 if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
375 if (name == ".debug_frame") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700376 debug_frame_offset_ = shdr.sh_offset;
377 debug_frame_size_ = shdr.sh_size;
378 debug_frame_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800379 } else if (name == ".gnu_debugdata") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700380 gnu_debugdata_offset_ = shdr.sh_offset;
381 gnu_debugdata_size_ = shdr.sh_size;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700382 } else if (name == ".eh_frame") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700383 eh_frame_offset_ = shdr.sh_offset;
384 eh_frame_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset;
385 eh_frame_size_ = shdr.sh_size;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700386 } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
Christopher Ferris819f1312019-10-03 13:35:48 -0700387 eh_frame_hdr_offset_ = shdr.sh_offset;
388 eh_frame_hdr_section_bias_ = static_cast<uint64_t>(shdr.sh_addr) - shdr.sh_offset;
389 eh_frame_hdr_size_ = shdr.sh_size;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800390 } else if (name == ".data") {
391 data_offset_ = shdr.sh_offset;
392 data_vaddr_start_ = shdr.sh_addr;
393 if (__builtin_add_overflow(data_vaddr_start_, shdr.sh_size, &data_vaddr_end_)) {
394 data_offset_ = 0;
395 data_vaddr_start_ = 0;
396 data_vaddr_end_ = 0;
397 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800398 }
399 }
400 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800401 } else if (shdr.sh_type == SHT_STRTAB) {
402 // In order to read soname, keep track of address to offset mapping.
403 strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr),
404 static_cast<uint64_t>(shdr.sh_offset)));
Florian Mayerda459e52018-11-23 16:56:17 +0000405 } else if (shdr.sh_type == SHT_NOTE) {
406 if (shdr.sh_name < sec_size) {
407 std::string name;
408 if (memory_->ReadString(sec_offset + shdr.sh_name, &name) &&
409 name == ".note.gnu.build-id") {
410 gnu_build_id_offset_ = shdr.sh_offset;
411 gnu_build_id_size_ = shdr.sh_size;
412 }
413 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800414 }
415 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800416}
417
418template <typename DynType>
Christopher Ferris02a6c442019-03-11 14:43:33 -0700419std::string ElfInterface::GetSonameWithTemplate() {
Christopher Ferris3958f802017-02-01 15:44:40 -0800420 if (soname_type_ == SONAME_INVALID) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700421 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800422 }
423 if (soname_type_ == SONAME_VALID) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700424 return soname_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800425 }
426
427 soname_type_ = SONAME_INVALID;
428
429 uint64_t soname_offset = 0;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800430 uint64_t strtab_addr = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800431 uint64_t strtab_size = 0;
432
433 // Find the soname location from the dynamic headers section.
434 DynType dyn;
435 uint64_t offset = dynamic_offset_;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800436 uint64_t max_offset = offset + dynamic_vaddr_end_ - dynamic_vaddr_start_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800437 for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
Josh Gaoef35aa52017-10-18 11:44:51 -0700438 if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800439 last_error_.code = ERROR_MEMORY_INVALID;
440 last_error_.address = offset;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700441 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800442 }
443
444 if (dyn.d_tag == DT_STRTAB) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800445 strtab_addr = dyn.d_un.d_ptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800446 } else if (dyn.d_tag == DT_STRSZ) {
447 strtab_size = dyn.d_un.d_val;
448 } else if (dyn.d_tag == DT_SONAME) {
449 soname_offset = dyn.d_un.d_val;
450 } else if (dyn.d_tag == DT_NULL) {
451 break;
452 }
453 }
454
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800455 // Need to map the strtab address to the real offset.
456 for (const auto& entry : strtabs_) {
457 if (entry.first == strtab_addr) {
458 soname_offset = entry.second + soname_offset;
459 if (soname_offset >= entry.second + strtab_size) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700460 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800461 }
462 if (!memory_->ReadString(soname_offset, &soname_)) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700463 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800464 }
465 soname_type_ = SONAME_VALID;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700466 return soname_;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800467 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800468 }
Christopher Ferris02a6c442019-03-11 14:43:33 -0700469 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800470}
471
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700472template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700473bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700474 uint64_t* func_offset) {
475 if (symbols_.empty()) {
476 return false;
477 }
478
479 for (const auto symbol : symbols_) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700480 if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700481 return true;
482 }
483 }
484 return false;
485}
486
Christopher Ferris150db122017-12-20 18:49:01 -0800487template <typename SymType>
488bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
489 if (symbols_.empty()) {
490 return false;
491 }
492
493 for (const auto symbol : symbols_) {
494 if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
495 return true;
496 }
497 }
498 return false;
499}
500
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700501bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800502 last_error_.code = ERROR_NONE;
503 last_error_.address = 0;
504
Christopher Ferris1a141a02018-01-24 08:52:47 -0800505 // Try the debug_frame first since it contains the most specific unwind
506 // information.
507 DwarfSection* debug_frame = debug_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700508 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700509 return true;
510 }
511
Christopher Ferris1a141a02018-01-24 08:52:47 -0800512 // Try the eh_frame next.
513 DwarfSection* eh_frame = eh_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700514 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800515 return true;
516 }
517
Christopher Ferrise7b66242017-12-15 11:17:45 -0800518 if (gnu_debugdata_interface_ != nullptr &&
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700519 gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700520 return true;
521 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800522
523 // Set the error code based on the first error encountered.
524 DwarfSection* section = nullptr;
525 if (debug_frame_ != nullptr) {
526 section = debug_frame_.get();
527 } else if (eh_frame_ != nullptr) {
528 section = eh_frame_.get();
529 } else if (gnu_debugdata_interface_ != nullptr) {
530 last_error_ = gnu_debugdata_interface_->last_error();
531 return false;
532 } else {
533 return false;
534 }
535
536 // Convert the DWARF ERROR to an external error.
537 DwarfErrorCode code = section->LastErrorCode();
538 switch (code) {
539 case DWARF_ERROR_NONE:
540 last_error_.code = ERROR_NONE;
541 break;
542
543 case DWARF_ERROR_MEMORY_INVALID:
544 last_error_.code = ERROR_MEMORY_INVALID;
545 last_error_.address = section->LastErrorAddress();
546 break;
547
548 case DWARF_ERROR_ILLEGAL_VALUE:
549 case DWARF_ERROR_ILLEGAL_STATE:
550 case DWARF_ERROR_STACK_INDEX_NOT_VALID:
551 case DWARF_ERROR_TOO_MANY_ITERATIONS:
552 case DWARF_ERROR_CFA_NOT_DEFINED:
553 case DWARF_ERROR_NO_FDES:
554 last_error_.code = ERROR_UNWIND_INFO;
555 break;
556
557 case DWARF_ERROR_NOT_IMPLEMENTED:
558 case DWARF_ERROR_UNSUPPORTED_VERSION:
559 last_error_.code = ERROR_UNSUPPORTED;
560 break;
561 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800562 return false;
563}
564
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700565// This is an estimation of the size of the elf file using the location
566// of the section headers and size. This assumes that the section headers
567// are at the end of the elf file. If the elf has a load bias, the size
568// will be too large, but this is acceptable.
569template <typename EhdrType>
570void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
571 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700572 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700573 return;
574 }
575 if (ehdr.e_shnum == 0) {
576 return;
577 }
578 *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
579}
580
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800581template <typename EhdrType, typename ShdrType>
582bool GetBuildIDInfo(Memory* memory, uint64_t* build_id_offset, uint64_t* build_id_size) {
583 EhdrType ehdr;
584 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
585 return false;
586 }
587
588 uint64_t offset = ehdr.e_shoff;
589 uint64_t sec_offset;
590 uint64_t sec_size;
591 ShdrType shdr;
592 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
593 return false;
594 }
595
596 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
597 if (!memory->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
598 return false;
599 }
600 sec_offset = shdr.sh_offset;
601 sec_size = shdr.sh_size;
602
603 // Skip the first header, it's always going to be NULL.
604 offset += ehdr.e_shentsize;
605 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
606 if (!memory->ReadFully(offset, &shdr, sizeof(shdr))) {
607 return false;
608 }
609 std::string name;
610 if (shdr.sh_type == SHT_NOTE && shdr.sh_name < sec_size &&
611 memory->ReadString(sec_offset + shdr.sh_name, &name) && name == ".note.gnu.build-id") {
612 *build_id_offset = shdr.sh_offset;
613 *build_id_size = shdr.sh_size;
614 return true;
615 }
616 }
617
618 return false;
619}
620
621template <typename EhdrType, typename ShdrType, typename NhdrType>
622std::string ElfInterface::ReadBuildIDFromMemory(Memory* memory) {
623 uint64_t note_offset;
624 uint64_t note_size;
625 if (!GetBuildIDInfo<EhdrType, ShdrType>(memory, &note_offset, &note_size)) {
626 return "";
627 }
628
629 // Ensure there is no overflow in any of the calculations below.
630 uint64_t tmp;
631 if (__builtin_add_overflow(note_offset, note_size, &tmp)) {
632 return "";
633 }
634
635 uint64_t offset = 0;
636 while (offset < note_size) {
637 if (note_size - offset < sizeof(NhdrType)) {
638 return "";
639 }
640 NhdrType hdr;
641 if (!memory->ReadFully(note_offset + offset, &hdr, sizeof(hdr))) {
642 return "";
643 }
644 offset += sizeof(hdr);
645
646 if (note_size - offset < hdr.n_namesz) {
647 return "";
648 }
649 if (hdr.n_namesz > 0) {
650 std::string name(hdr.n_namesz, '\0');
651 if (!memory->ReadFully(note_offset + offset, &(name[0]), hdr.n_namesz)) {
652 return "";
653 }
654
655 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
656 if (name.back() == '\0') name.resize(name.size() - 1);
657
658 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
659 offset += (hdr.n_namesz + 3) & ~3;
660
661 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
662 if (note_size - offset < hdr.n_descsz || hdr.n_descsz == 0) {
663 return "";
664 }
665 std::string build_id(hdr.n_descsz - 1, '\0');
666 if (memory->ReadFully(note_offset + offset, &build_id[0], hdr.n_descsz)) {
667 return build_id;
668 }
669 return "";
670 }
671 }
672 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
673 offset += (hdr.n_descsz + 3) & ~3;
674 }
675 return "";
676}
677
Christopher Ferris3958f802017-02-01 15:44:40 -0800678// Instantiate all of the needed template functions.
Christopher Ferris819f1312019-10-03 13:35:48 -0700679template void ElfInterface::InitHeadersWithTemplate<uint32_t>();
680template void ElfInterface::InitHeadersWithTemplate<uint64_t>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700681
Christopher Ferris819f1312019-10-03 13:35:48 -0700682template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(int64_t*);
683template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(int64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800684
Christopher Ferris819f1312019-10-03 13:35:48 -0700685template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, int64_t*);
686template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, int64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800687
Christopher Ferris5acf0692018-08-01 13:10:46 -0700688template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
689template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
Christopher Ferris3958f802017-02-01 15:44:40 -0800690
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800691template std::string ElfInterface::ReadBuildID<Elf32_Nhdr>();
692template std::string ElfInterface::ReadBuildID<Elf64_Nhdr>();
Florian Mayerda459e52018-11-23 16:56:17 +0000693
Christopher Ferris02a6c442019-03-11 14:43:33 -0700694template std::string ElfInterface::GetSonameWithTemplate<Elf32_Dyn>();
695template std::string ElfInterface::GetSonameWithTemplate<Elf64_Dyn>();
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700696
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700697template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700698 uint64_t*);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700699template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700700 uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700701
Christopher Ferris150db122017-12-20 18:49:01 -0800702template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
703template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
704
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700705template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
706template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
707
Christopher Ferris819f1312019-10-03 13:35:48 -0700708template int64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
709template int64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800710
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800711template std::string ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(
712 Memory*);
713template std::string ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(
714 Memory*);
715
Christopher Ferrisd226a512017-07-14 10:37:19 -0700716} // namespace unwindstack