blob: 17470fd3e5ddf5d5938167df889daff0fb0e508b [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;
David Srbeckya17c2b62020-04-14 16:59:27 +0100374 if (memory_->ReadString(sec_offset + shdr.sh_name, &name, sec_size - shdr.sh_name)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800375 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;
David Srbeckya17c2b62020-04-14 16:59:27 +0100408 if (memory_->ReadString(sec_offset + shdr.sh_name, &name, sec_size - shdr.sh_name) &&
Florian Mayerda459e52018-11-23 16:56:17 +0000409 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;
David Srbeckya17c2b62020-04-14 16:59:27 +0100459 uint64_t soname_max = entry.second + strtab_size;
460 if (soname_offset >= soname_max) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700461 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800462 }
David Srbeckya17c2b62020-04-14 16:59:27 +0100463 if (!memory_->ReadString(soname_offset, &soname_, soname_max - soname_offset)) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700464 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800465 }
466 soname_type_ = SONAME_VALID;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700467 return soname_;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800468 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800469 }
Christopher Ferris02a6c442019-03-11 14:43:33 -0700470 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800471}
472
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700473template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700474bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700475 uint64_t* func_offset) {
476 if (symbols_.empty()) {
477 return false;
478 }
479
480 for (const auto symbol : symbols_) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700481 if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700482 return true;
483 }
484 }
485 return false;
486}
487
Christopher Ferris150db122017-12-20 18:49:01 -0800488template <typename SymType>
489bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
490 if (symbols_.empty()) {
491 return false;
492 }
493
494 for (const auto symbol : symbols_) {
495 if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
496 return true;
497 }
498 }
499 return false;
500}
501
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700502bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800503 last_error_.code = ERROR_NONE;
504 last_error_.address = 0;
505
Christopher Ferris1a141a02018-01-24 08:52:47 -0800506 // Try the debug_frame first since it contains the most specific unwind
507 // information.
508 DwarfSection* debug_frame = debug_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700509 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700510 return true;
511 }
512
Christopher Ferris1a141a02018-01-24 08:52:47 -0800513 // Try the eh_frame next.
514 DwarfSection* eh_frame = eh_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700515 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800516 return true;
517 }
518
Christopher Ferrise7b66242017-12-15 11:17:45 -0800519 if (gnu_debugdata_interface_ != nullptr &&
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700520 gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700521 return true;
522 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800523
524 // Set the error code based on the first error encountered.
525 DwarfSection* section = nullptr;
526 if (debug_frame_ != nullptr) {
527 section = debug_frame_.get();
528 } else if (eh_frame_ != nullptr) {
529 section = eh_frame_.get();
530 } else if (gnu_debugdata_interface_ != nullptr) {
531 last_error_ = gnu_debugdata_interface_->last_error();
532 return false;
533 } else {
534 return false;
535 }
536
537 // Convert the DWARF ERROR to an external error.
538 DwarfErrorCode code = section->LastErrorCode();
539 switch (code) {
540 case DWARF_ERROR_NONE:
541 last_error_.code = ERROR_NONE;
542 break;
543
544 case DWARF_ERROR_MEMORY_INVALID:
545 last_error_.code = ERROR_MEMORY_INVALID;
546 last_error_.address = section->LastErrorAddress();
547 break;
548
549 case DWARF_ERROR_ILLEGAL_VALUE:
550 case DWARF_ERROR_ILLEGAL_STATE:
551 case DWARF_ERROR_STACK_INDEX_NOT_VALID:
552 case DWARF_ERROR_TOO_MANY_ITERATIONS:
553 case DWARF_ERROR_CFA_NOT_DEFINED:
554 case DWARF_ERROR_NO_FDES:
555 last_error_.code = ERROR_UNWIND_INFO;
556 break;
557
558 case DWARF_ERROR_NOT_IMPLEMENTED:
559 case DWARF_ERROR_UNSUPPORTED_VERSION:
560 last_error_.code = ERROR_UNSUPPORTED;
561 break;
562 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800563 return false;
564}
565
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700566// This is an estimation of the size of the elf file using the location
567// of the section headers and size. This assumes that the section headers
568// are at the end of the elf file. If the elf has a load bias, the size
569// will be too large, but this is acceptable.
570template <typename EhdrType>
571void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
572 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700573 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700574 return;
575 }
576 if (ehdr.e_shnum == 0) {
577 return;
578 }
579 *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
580}
581
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800582template <typename EhdrType, typename ShdrType>
583bool GetBuildIDInfo(Memory* memory, uint64_t* build_id_offset, uint64_t* build_id_size) {
584 EhdrType ehdr;
585 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
586 return false;
587 }
588
589 uint64_t offset = ehdr.e_shoff;
590 uint64_t sec_offset;
591 uint64_t sec_size;
592 ShdrType shdr;
593 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
594 return false;
595 }
596
597 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
598 if (!memory->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
599 return false;
600 }
601 sec_offset = shdr.sh_offset;
602 sec_size = shdr.sh_size;
603
604 // Skip the first header, it's always going to be NULL.
605 offset += ehdr.e_shentsize;
606 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
607 if (!memory->ReadFully(offset, &shdr, sizeof(shdr))) {
608 return false;
609 }
610 std::string name;
611 if (shdr.sh_type == SHT_NOTE && shdr.sh_name < sec_size &&
David Srbeckya17c2b62020-04-14 16:59:27 +0100612 memory->ReadString(sec_offset + shdr.sh_name, &name, sec_size - shdr.sh_name) &&
613 name == ".note.gnu.build-id") {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800614 *build_id_offset = shdr.sh_offset;
615 *build_id_size = shdr.sh_size;
616 return true;
617 }
618 }
619
620 return false;
621}
622
623template <typename EhdrType, typename ShdrType, typename NhdrType>
624std::string ElfInterface::ReadBuildIDFromMemory(Memory* memory) {
625 uint64_t note_offset;
626 uint64_t note_size;
627 if (!GetBuildIDInfo<EhdrType, ShdrType>(memory, &note_offset, &note_size)) {
628 return "";
629 }
630
631 // Ensure there is no overflow in any of the calculations below.
632 uint64_t tmp;
633 if (__builtin_add_overflow(note_offset, note_size, &tmp)) {
634 return "";
635 }
636
637 uint64_t offset = 0;
638 while (offset < note_size) {
639 if (note_size - offset < sizeof(NhdrType)) {
640 return "";
641 }
642 NhdrType hdr;
643 if (!memory->ReadFully(note_offset + offset, &hdr, sizeof(hdr))) {
644 return "";
645 }
646 offset += sizeof(hdr);
647
648 if (note_size - offset < hdr.n_namesz) {
649 return "";
650 }
651 if (hdr.n_namesz > 0) {
652 std::string name(hdr.n_namesz, '\0');
653 if (!memory->ReadFully(note_offset + offset, &(name[0]), hdr.n_namesz)) {
654 return "";
655 }
656
657 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
658 if (name.back() == '\0') name.resize(name.size() - 1);
659
660 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
661 offset += (hdr.n_namesz + 3) & ~3;
662
663 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
664 if (note_size - offset < hdr.n_descsz || hdr.n_descsz == 0) {
665 return "";
666 }
Peter Collingbournea7b4c5d2020-03-30 18:38:21 -0700667 std::string build_id(hdr.n_descsz, '\0');
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800668 if (memory->ReadFully(note_offset + offset, &build_id[0], hdr.n_descsz)) {
669 return build_id;
670 }
671 return "";
672 }
673 }
674 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
675 offset += (hdr.n_descsz + 3) & ~3;
676 }
677 return "";
678}
679
Christopher Ferris3958f802017-02-01 15:44:40 -0800680// Instantiate all of the needed template functions.
Christopher Ferris819f1312019-10-03 13:35:48 -0700681template void ElfInterface::InitHeadersWithTemplate<uint32_t>();
682template void ElfInterface::InitHeadersWithTemplate<uint64_t>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700683
Christopher Ferris819f1312019-10-03 13:35:48 -0700684template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(int64_t*);
685template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(int64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800686
Christopher Ferris819f1312019-10-03 13:35:48 -0700687template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, int64_t*);
688template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, int64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800689
Christopher Ferris5acf0692018-08-01 13:10:46 -0700690template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
691template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
Christopher Ferris3958f802017-02-01 15:44:40 -0800692
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800693template std::string ElfInterface::ReadBuildID<Elf32_Nhdr>();
694template std::string ElfInterface::ReadBuildID<Elf64_Nhdr>();
Florian Mayerda459e52018-11-23 16:56:17 +0000695
Christopher Ferris02a6c442019-03-11 14:43:33 -0700696template std::string ElfInterface::GetSonameWithTemplate<Elf32_Dyn>();
697template std::string ElfInterface::GetSonameWithTemplate<Elf64_Dyn>();
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700698
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700699template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700700 uint64_t*);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700701template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700702 uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700703
Christopher Ferris150db122017-12-20 18:49:01 -0800704template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
705template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
706
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700707template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
708template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
709
Christopher Ferris819f1312019-10-03 13:35:48 -0700710template int64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
711template int64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800712
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800713template std::string ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(
714 Memory*);
715template std::string ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(
716 Memory*);
717
Christopher Ferrisd226a512017-07-14 10:37:19 -0700718} // namespace unwindstack