blob: be1f092607aa9916767f45652f13e87187c715dd [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 Ferris4cc36d22018-06-06 14:47:31 -0700127void ElfInterface::InitHeadersWithTemplate(uint64_t load_bias) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700128 if (eh_frame_hdr_offset_ != 0) {
129 eh_frame_.reset(new DwarfEhFrameWithHdr<AddressType>(memory_));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700130 if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_, load_bias)) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700131 eh_frame_.reset(nullptr);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700132 }
Christopher Ferris1a141a02018-01-24 08:52:47 -0800133 }
134
135 if (eh_frame_.get() == nullptr && eh_frame_offset_ != 0) {
136 // If there is an eh_frame section without an eh_frame_hdr section,
137 // or using the frame hdr object failed to init.
Christopher Ferris61d40972017-06-12 19:14:20 -0700138 eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700139 if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, load_bias)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700140 eh_frame_.reset(nullptr);
Christopher Ferris61d40972017-06-12 19:14:20 -0700141 }
142 }
143
Christopher Ferris1a141a02018-01-24 08:52:47 -0800144 if (eh_frame_.get() == nullptr) {
145 eh_frame_hdr_offset_ = 0;
146 eh_frame_hdr_size_ = static_cast<uint64_t>(-1);
147 eh_frame_offset_ = 0;
148 eh_frame_size_ = static_cast<uint64_t>(-1);
149 }
150
Christopher Ferris61d40972017-06-12 19:14:20 -0700151 if (debug_frame_offset_ != 0) {
152 debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700153 if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, load_bias)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700154 debug_frame_.reset(nullptr);
155 debug_frame_offset_ = 0;
156 debug_frame_size_ = static_cast<uint64_t>(-1);
157 }
158 }
159}
160
Christopher Ferris3958f802017-02-01 15:44:40 -0800161template <typename EhdrType, typename PhdrType, typename ShdrType>
Christopher Ferrise69f4702017-10-19 16:08:58 -0700162bool ElfInterface::ReadAllHeaders(uint64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800163 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700164 if (!memory_->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800165 last_error_.code = ERROR_MEMORY_INVALID;
166 last_error_.address = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800167 return false;
168 }
169
Christopher Ferris5acf0692018-08-01 13:10:46 -0700170 // If we have enough information that this is an elf file, then allow
171 // malformed program and section headers.
172 ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias);
173 ReadSectionHeaders<EhdrType, ShdrType>(ehdr);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700174 return true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800175}
176
177template <typename EhdrType, typename PhdrType>
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800178uint64_t ElfInterface::GetLoadBias(Memory* memory) {
179 EhdrType ehdr;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800180 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800181 return false;
182 }
183
184 uint64_t offset = ehdr.e_phoff;
185 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
186 PhdrType phdr;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800187 if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800188 return 0;
189 }
Christopher Ferris6c8ac562019-10-02 17:41:16 -0700190
191 // Find the first executable load when looking for the load bias.
192 if (phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
193 if (phdr.p_vaddr > phdr.p_offset) {
194 return phdr.p_vaddr - phdr.p_offset;
195 }
196 break;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800197 }
198 }
199 return 0;
200}
201
202template <typename EhdrType, typename PhdrType>
Christopher Ferris5acf0692018-08-01 13:10:46 -0700203void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800204 uint64_t offset = ehdr.e_phoff;
Florian Mayer249c90f2019-07-05 16:48:04 +0100205 bool first_exec_load_header = true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800206 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
207 PhdrType phdr;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700208 if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700209 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800210 }
211
Christopher Ferris3958f802017-02-01 15:44:40 -0800212 switch (phdr.p_type) {
213 case PT_LOAD:
214 {
Christopher Ferris3958f802017-02-01 15:44:40 -0800215 if ((phdr.p_flags & PF_X) == 0) {
216 continue;
217 }
218
Christopher Ferris3958f802017-02-01 15:44:40 -0800219 pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
220 static_cast<size_t>(phdr.p_memsz)};
Florian Mayer249c90f2019-07-05 16:48:04 +0100221 // Only set the load bias from the first executable load header.
222 if (first_exec_load_header && phdr.p_vaddr > phdr.p_offset) {
223 *load_bias = phdr.p_vaddr - phdr.p_offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800224 }
Florian Mayer249c90f2019-07-05 16:48:04 +0100225 first_exec_load_header = false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800226 break;
227 }
228
229 case PT_GNU_EH_FRAME:
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700230 // This is really the pointer to the .eh_frame_hdr section.
231 eh_frame_hdr_offset_ = phdr.p_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700232 eh_frame_hdr_size_ = phdr.p_memsz;
Christopher Ferris3958f802017-02-01 15:44:40 -0800233 break;
234
235 case PT_DYNAMIC:
Christopher Ferris3958f802017-02-01 15:44:40 -0800236 dynamic_offset_ = phdr.p_offset;
Christopher Ferris150db122017-12-20 18:49:01 -0800237 dynamic_vaddr_ = phdr.p_vaddr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800238 dynamic_size_ = phdr.p_memsz;
239 break;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700240
241 default:
242 HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
243 break;
Christopher Ferris3958f802017-02-01 15:44:40 -0800244 }
245 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800246}
247
Florian Mayerda459e52018-11-23 16:56:17 +0000248template <typename NhdrType>
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800249std::string ElfInterface::ReadBuildID() {
Florian Mayerda459e52018-11-23 16:56:17 +0000250 // Ensure there is no overflow in any of the calulations below.
251 uint64_t tmp;
252 if (__builtin_add_overflow(gnu_build_id_offset_, gnu_build_id_size_, &tmp)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800253 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000254 }
255
256 uint64_t offset = 0;
257 while (offset < gnu_build_id_size_) {
258 if (gnu_build_id_size_ - offset < sizeof(NhdrType)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800259 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000260 }
261 NhdrType hdr;
262 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &hdr, sizeof(hdr))) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800263 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000264 }
265 offset += sizeof(hdr);
266
267 if (gnu_build_id_size_ - offset < hdr.n_namesz) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800268 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000269 }
270 if (hdr.n_namesz > 0) {
271 std::string name(hdr.n_namesz, '\0');
272 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &(name[0]), hdr.n_namesz)) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800273 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000274 }
275
276 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
277 if (name.back() == '\0')
278 name.resize(name.size() - 1);
279
280 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
281 offset += (hdr.n_namesz + 3) & ~3;
282
283 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800284 if (gnu_build_id_size_ - offset < hdr.n_descsz || hdr.n_descsz == 0) {
285 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000286 }
Christopher Ferris1760b452019-04-03 14:14:30 -0700287 std::string build_id(hdr.n_descsz, '\0');
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800288 if (memory_->ReadFully(gnu_build_id_offset_ + offset, &build_id[0], hdr.n_descsz)) {
289 return build_id;
290 }
291 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000292 }
293 }
294 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
295 offset += (hdr.n_descsz + 3) & ~3;
296 }
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800297 return "";
Florian Mayerda459e52018-11-23 16:56:17 +0000298}
299
Christopher Ferris3958f802017-02-01 15:44:40 -0800300template <typename EhdrType, typename ShdrType>
Christopher Ferris5acf0692018-08-01 13:10:46 -0700301void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800302 uint64_t offset = ehdr.e_shoff;
303 uint64_t sec_offset = 0;
304 uint64_t sec_size = 0;
305
306 // Get the location of the section header names.
307 // If something is malformed in the header table data, we aren't going
308 // to terminate, we'll simply ignore this part.
309 ShdrType shdr;
310 if (ehdr.e_shstrndx < ehdr.e_shnum) {
311 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700312 if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800313 sec_offset = shdr.sh_offset;
314 sec_size = shdr.sh_size;
315 }
316 }
317
318 // Skip the first header, it's always going to be NULL.
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700319 offset += ehdr.e_shentsize;
Christopher Ferris3958f802017-02-01 15:44:40 -0800320 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800321 if (!memory_->ReadFully(offset, &shdr, sizeof(shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700322 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800323 }
324
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700325 if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700326 // Need to go get the information about the section that contains
327 // the string terminated names.
328 ShdrType str_shdr;
329 if (shdr.sh_link >= ehdr.e_shnum) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700330 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700331 }
332 uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
Christopher Ferriscadacdf2019-01-15 19:10:49 -0800333 if (!memory_->ReadFully(str_offset, &str_shdr, sizeof(str_shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700334 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700335 }
336 if (str_shdr.sh_type != SHT_STRTAB) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700337 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700338 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700339 symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
340 str_shdr.sh_offset, str_shdr.sh_size));
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000341 } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800342 // Look for the .debug_frame and .gnu_debugdata.
Christopher Ferris3958f802017-02-01 15:44:40 -0800343 if (shdr.sh_name < sec_size) {
344 std::string name;
345 if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000346 uint64_t* offset_ptr = nullptr;
347 uint64_t* size_ptr = nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800348 if (name == ".debug_frame") {
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000349 offset_ptr = &debug_frame_offset_;
350 size_ptr = &debug_frame_size_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800351 } else if (name == ".gnu_debugdata") {
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000352 offset_ptr = &gnu_debugdata_offset_;
353 size_ptr = &gnu_debugdata_size_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700354 } else if (name == ".eh_frame") {
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000355 offset_ptr = &eh_frame_offset_;
356 size_ptr = &eh_frame_size_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700357 } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000358 offset_ptr = &eh_frame_hdr_offset_;
359 size_ptr = &eh_frame_hdr_size_;
360 }
361 if (offset_ptr != nullptr) {
362 *offset_ptr = shdr.sh_offset;
363 *size_ptr = shdr.sh_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800364 }
365 }
366 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800367 } else if (shdr.sh_type == SHT_STRTAB) {
368 // In order to read soname, keep track of address to offset mapping.
369 strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr),
370 static_cast<uint64_t>(shdr.sh_offset)));
Florian Mayerda459e52018-11-23 16:56:17 +0000371 } else if (shdr.sh_type == SHT_NOTE) {
372 if (shdr.sh_name < sec_size) {
373 std::string name;
374 if (memory_->ReadString(sec_offset + shdr.sh_name, &name) &&
375 name == ".note.gnu.build-id") {
376 gnu_build_id_offset_ = shdr.sh_offset;
377 gnu_build_id_size_ = shdr.sh_size;
378 }
379 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800380 }
381 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800382}
383
384template <typename DynType>
Christopher Ferris02a6c442019-03-11 14:43:33 -0700385std::string ElfInterface::GetSonameWithTemplate() {
Christopher Ferris3958f802017-02-01 15:44:40 -0800386 if (soname_type_ == SONAME_INVALID) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700387 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800388 }
389 if (soname_type_ == SONAME_VALID) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700390 return soname_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800391 }
392
393 soname_type_ = SONAME_INVALID;
394
395 uint64_t soname_offset = 0;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800396 uint64_t strtab_addr = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800397 uint64_t strtab_size = 0;
398
399 // Find the soname location from the dynamic headers section.
400 DynType dyn;
401 uint64_t offset = dynamic_offset_;
402 uint64_t max_offset = offset + dynamic_size_;
403 for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
Josh Gaoef35aa52017-10-18 11:44:51 -0700404 if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800405 last_error_.code = ERROR_MEMORY_INVALID;
406 last_error_.address = offset;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700407 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800408 }
409
410 if (dyn.d_tag == DT_STRTAB) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800411 strtab_addr = dyn.d_un.d_ptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800412 } else if (dyn.d_tag == DT_STRSZ) {
413 strtab_size = dyn.d_un.d_val;
414 } else if (dyn.d_tag == DT_SONAME) {
415 soname_offset = dyn.d_un.d_val;
416 } else if (dyn.d_tag == DT_NULL) {
417 break;
418 }
419 }
420
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800421 // Need to map the strtab address to the real offset.
422 for (const auto& entry : strtabs_) {
423 if (entry.first == strtab_addr) {
424 soname_offset = entry.second + soname_offset;
425 if (soname_offset >= entry.second + strtab_size) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700426 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800427 }
428 if (!memory_->ReadString(soname_offset, &soname_)) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700429 return "";
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800430 }
431 soname_type_ = SONAME_VALID;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700432 return soname_;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800433 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800434 }
Christopher Ferris02a6c442019-03-11 14:43:33 -0700435 return "";
Christopher Ferris3958f802017-02-01 15:44:40 -0800436}
437
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700438template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700439bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700440 uint64_t* func_offset) {
441 if (symbols_.empty()) {
442 return false;
443 }
444
445 for (const auto symbol : symbols_) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700446 if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700447 return true;
448 }
449 }
450 return false;
451}
452
Christopher Ferris150db122017-12-20 18:49:01 -0800453template <typename SymType>
454bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
455 if (symbols_.empty()) {
456 return false;
457 }
458
459 for (const auto symbol : symbols_) {
460 if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
461 return true;
462 }
463 }
464 return false;
465}
466
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700467bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800468 last_error_.code = ERROR_NONE;
469 last_error_.address = 0;
470
Christopher Ferris1a141a02018-01-24 08:52:47 -0800471 // Try the debug_frame first since it contains the most specific unwind
472 // information.
473 DwarfSection* debug_frame = debug_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700474 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700475 return true;
476 }
477
Christopher Ferris1a141a02018-01-24 08:52:47 -0800478 // Try the eh_frame next.
479 DwarfSection* eh_frame = eh_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700480 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800481 return true;
482 }
483
Christopher Ferrise7b66242017-12-15 11:17:45 -0800484 if (gnu_debugdata_interface_ != nullptr &&
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700485 gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700486 return true;
487 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800488
489 // Set the error code based on the first error encountered.
490 DwarfSection* section = nullptr;
491 if (debug_frame_ != nullptr) {
492 section = debug_frame_.get();
493 } else if (eh_frame_ != nullptr) {
494 section = eh_frame_.get();
495 } else if (gnu_debugdata_interface_ != nullptr) {
496 last_error_ = gnu_debugdata_interface_->last_error();
497 return false;
498 } else {
499 return false;
500 }
501
502 // Convert the DWARF ERROR to an external error.
503 DwarfErrorCode code = section->LastErrorCode();
504 switch (code) {
505 case DWARF_ERROR_NONE:
506 last_error_.code = ERROR_NONE;
507 break;
508
509 case DWARF_ERROR_MEMORY_INVALID:
510 last_error_.code = ERROR_MEMORY_INVALID;
511 last_error_.address = section->LastErrorAddress();
512 break;
513
514 case DWARF_ERROR_ILLEGAL_VALUE:
515 case DWARF_ERROR_ILLEGAL_STATE:
516 case DWARF_ERROR_STACK_INDEX_NOT_VALID:
517 case DWARF_ERROR_TOO_MANY_ITERATIONS:
518 case DWARF_ERROR_CFA_NOT_DEFINED:
519 case DWARF_ERROR_NO_FDES:
520 last_error_.code = ERROR_UNWIND_INFO;
521 break;
522
523 case DWARF_ERROR_NOT_IMPLEMENTED:
524 case DWARF_ERROR_UNSUPPORTED_VERSION:
525 last_error_.code = ERROR_UNSUPPORTED;
526 break;
527 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800528 return false;
529}
530
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700531// This is an estimation of the size of the elf file using the location
532// of the section headers and size. This assumes that the section headers
533// are at the end of the elf file. If the elf has a load bias, the size
534// will be too large, but this is acceptable.
535template <typename EhdrType>
536void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
537 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700538 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700539 return;
540 }
541 if (ehdr.e_shnum == 0) {
542 return;
543 }
544 *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
545}
546
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800547template <typename EhdrType, typename ShdrType>
548bool GetBuildIDInfo(Memory* memory, uint64_t* build_id_offset, uint64_t* build_id_size) {
549 EhdrType ehdr;
550 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
551 return false;
552 }
553
554 uint64_t offset = ehdr.e_shoff;
555 uint64_t sec_offset;
556 uint64_t sec_size;
557 ShdrType shdr;
558 if (ehdr.e_shstrndx >= ehdr.e_shnum) {
559 return false;
560 }
561
562 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
563 if (!memory->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
564 return false;
565 }
566 sec_offset = shdr.sh_offset;
567 sec_size = shdr.sh_size;
568
569 // Skip the first header, it's always going to be NULL.
570 offset += ehdr.e_shentsize;
571 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
572 if (!memory->ReadFully(offset, &shdr, sizeof(shdr))) {
573 return false;
574 }
575 std::string name;
576 if (shdr.sh_type == SHT_NOTE && shdr.sh_name < sec_size &&
577 memory->ReadString(sec_offset + shdr.sh_name, &name) && name == ".note.gnu.build-id") {
578 *build_id_offset = shdr.sh_offset;
579 *build_id_size = shdr.sh_size;
580 return true;
581 }
582 }
583
584 return false;
585}
586
587template <typename EhdrType, typename ShdrType, typename NhdrType>
588std::string ElfInterface::ReadBuildIDFromMemory(Memory* memory) {
589 uint64_t note_offset;
590 uint64_t note_size;
591 if (!GetBuildIDInfo<EhdrType, ShdrType>(memory, &note_offset, &note_size)) {
592 return "";
593 }
594
595 // Ensure there is no overflow in any of the calculations below.
596 uint64_t tmp;
597 if (__builtin_add_overflow(note_offset, note_size, &tmp)) {
598 return "";
599 }
600
601 uint64_t offset = 0;
602 while (offset < note_size) {
603 if (note_size - offset < sizeof(NhdrType)) {
604 return "";
605 }
606 NhdrType hdr;
607 if (!memory->ReadFully(note_offset + offset, &hdr, sizeof(hdr))) {
608 return "";
609 }
610 offset += sizeof(hdr);
611
612 if (note_size - offset < hdr.n_namesz) {
613 return "";
614 }
615 if (hdr.n_namesz > 0) {
616 std::string name(hdr.n_namesz, '\0');
617 if (!memory->ReadFully(note_offset + offset, &(name[0]), hdr.n_namesz)) {
618 return "";
619 }
620
621 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
622 if (name.back() == '\0') name.resize(name.size() - 1);
623
624 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
625 offset += (hdr.n_namesz + 3) & ~3;
626
627 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
628 if (note_size - offset < hdr.n_descsz || hdr.n_descsz == 0) {
629 return "";
630 }
631 std::string build_id(hdr.n_descsz - 1, '\0');
632 if (memory->ReadFully(note_offset + offset, &build_id[0], hdr.n_descsz)) {
633 return build_id;
634 }
635 return "";
636 }
637 }
638 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
639 offset += (hdr.n_descsz + 3) & ~3;
640 }
641 return "";
642}
643
Christopher Ferris3958f802017-02-01 15:44:40 -0800644// Instantiate all of the needed template functions.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700645template void ElfInterface::InitHeadersWithTemplate<uint32_t>(uint64_t);
646template void ElfInterface::InitHeadersWithTemplate<uint64_t>(uint64_t);
Christopher Ferris61d40972017-06-12 19:14:20 -0700647
Christopher Ferrise69f4702017-10-19 16:08:58 -0700648template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*);
649template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800650
Christopher Ferris5acf0692018-08-01 13:10:46 -0700651template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&,
652 uint64_t*);
653template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&,
654 uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800655
Christopher Ferris5acf0692018-08-01 13:10:46 -0700656template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
657template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
Christopher Ferris3958f802017-02-01 15:44:40 -0800658
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800659template std::string ElfInterface::ReadBuildID<Elf32_Nhdr>();
660template std::string ElfInterface::ReadBuildID<Elf64_Nhdr>();
Florian Mayerda459e52018-11-23 16:56:17 +0000661
Christopher Ferris02a6c442019-03-11 14:43:33 -0700662template std::string ElfInterface::GetSonameWithTemplate<Elf32_Dyn>();
663template std::string ElfInterface::GetSonameWithTemplate<Elf64_Dyn>();
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700664
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700665template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700666 uint64_t*);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700667template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700668 uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700669
Christopher Ferris150db122017-12-20 18:49:01 -0800670template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
671template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
672
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700673template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
674template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
675
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800676template uint64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
677template uint64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
678
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800679template std::string ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(
680 Memory*);
681template std::string ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(
682 Memory*);
683
Christopher Ferrisd226a512017-07-14 10:37:19 -0700684} // namespace unwindstack