blob: 2117ebd3eb9c63e545a9a17e6fbb78acb189004a [file] [log] [blame]
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -07001/*
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
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070017#include <elf.h>
18#include <stdint.h>
19
Florian Mayer3f1f2e02018-10-23 15:56:28 +010020#include <algorithm>
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070021#include <string>
David Srbeckyaf419602019-03-26 14:38:28 +000022#include <vector>
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070023
Christopher Ferrisd226a512017-07-14 10:37:19 -070024#include <unwindstack/Memory.h>
25
Christopher Ferris94167032017-06-28 18:56:52 -070026#include "Check.h"
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070027#include "Symbols.h"
28
Christopher Ferrisd226a512017-07-14 10:37:19 -070029namespace unwindstack {
30
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070031Symbols::Symbols(uint64_t offset, uint64_t size, uint64_t entry_size, uint64_t str_offset,
32 uint64_t str_size)
David Srbeckyaf419602019-03-26 14:38:28 +000033 : offset_(offset),
34 count_(entry_size != 0 ? size / entry_size : 0),
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070035 entry_size_(entry_size),
36 str_offset_(str_offset),
37 str_end_(str_offset_ + str_size) {}
38
David Srbeckyaf419602019-03-26 14:38:28 +000039template <typename SymType>
40static bool IsFunc(const SymType* entry) {
41 return entry->st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry->st_info) == STT_FUNC;
42}
43
44// Read symbol entry from memory and cache it so we don't have to read it again.
45template <typename SymType>
46inline __attribute__((__always_inline__)) const Symbols::Info* Symbols::ReadFuncInfo(
47 uint32_t symbol_index, Memory* elf_memory) {
48 auto it = symbols_.find(symbol_index);
49 if (it != symbols_.end()) {
50 return &it->second;
51 }
52 SymType sym;
53 if (!elf_memory->ReadFully(offset_ + symbol_index * entry_size_, &sym, sizeof(sym))) {
54 return nullptr;
55 }
56 if (!IsFunc(&sym)) {
57 // We need the address for binary search, but we don't want it to be matched.
58 sym.st_size = 0;
59 }
60 Info info{.addr = sym.st_value, .size = static_cast<uint32_t>(sym.st_size), .name = sym.st_name};
61 return &symbols_.emplace(symbol_index, info).first->second;
62}
63
64// Binary search the symbol table to find function containing the given address.
65// Without remap, the symbol table is assumed to be sorted and accessed directly.
66// If the symbol table is not sorted this method might fail but should not crash.
67// When the indices are remapped, they are guaranteed to be sorted by address.
68template <typename SymType, bool RemapIndices>
69const Symbols::Info* Symbols::BinarySearch(uint64_t addr, Memory* elf_memory) {
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070070 size_t first = 0;
David Srbeckyaf419602019-03-26 14:38:28 +000071 size_t last = RemapIndices ? remap_->size() : count_;
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070072 while (first < last) {
73 size_t current = first + (last - first) / 2;
David Srbeckyaf419602019-03-26 14:38:28 +000074 size_t symbol_index = RemapIndices ? remap_.value()[current] : current;
75 const Info* info = ReadFuncInfo<SymType>(symbol_index, elf_memory);
76 if (info == nullptr) {
77 return nullptr;
78 }
79 if (addr < info->addr) {
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070080 last = current;
David Srbeckyaf419602019-03-26 14:38:28 +000081 } else if (addr < info->addr + info->size) {
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070082 return info;
83 } else {
84 first = current + 1;
85 }
86 }
87 return nullptr;
88}
89
David Srbeckyaf419602019-03-26 14:38:28 +000090// Create remapping table which allows us to access symbols as if they were sorted by address.
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070091template <typename SymType>
David Srbeckyaf419602019-03-26 14:38:28 +000092void Symbols::BuildRemapTable(Memory* elf_memory) {
93 std::vector<uint64_t> addrs; // Addresses of all symbols (addrs[i] == symbols[i].st_value).
94 addrs.reserve(count_);
95 remap_.emplace(); // Construct the optional remap table.
96 remap_->reserve(count_);
97 for (size_t symbol_idx = 0; symbol_idx < count_;) {
98 // Read symbols from memory. We intentionally bypass the cache to save memory.
99 // Do the reads in batches so that we minimize the number of memory read calls.
100 uint8_t buffer[1024];
101 size_t read = std::min<size_t>(sizeof(buffer), (count_ - symbol_idx) * entry_size_);
102 size_t size = elf_memory->Read(offset_ + symbol_idx * entry_size_, buffer, read);
103 if (size < sizeof(SymType)) {
104 break; // Stop processing, something looks like it is corrupted.
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700105 }
David Srbeckyaf419602019-03-26 14:38:28 +0000106 for (size_t offset = 0; offset + sizeof(SymType) <= size; offset += entry_size_, symbol_idx++) {
107 SymType sym;
108 memcpy(&sym, &buffer[offset], sizeof(SymType)); // Copy to ensure alignment.
109 addrs.push_back(sym.st_value); // Always insert so it is indexable by symbol index.
110 if (IsFunc(&sym)) {
111 remap_->push_back(symbol_idx); // Indices of function symbols only.
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700112 }
113 }
114 }
David Srbeckyaf419602019-03-26 14:38:28 +0000115 // Sort by address to make the remap list binary searchable (stable due to the a<b tie break).
116 auto comp = [&addrs](auto a, auto b) { return std::tie(addrs[a], a) < std::tie(addrs[b], b); };
117 std::sort(remap_->begin(), remap_->end(), comp);
118 // Remove duplicate entries (methods de-duplicated by the linker).
119 auto pred = [&addrs](auto a, auto b) { return addrs[a] == addrs[b]; };
120 remap_->erase(std::unique(remap_->begin(), remap_->end(), pred), remap_->end());
121 remap_->shrink_to_fit();
122}
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700123
David Srbeckyaf419602019-03-26 14:38:28 +0000124template <typename SymType>
125bool Symbols::GetName(uint64_t addr, Memory* elf_memory, std::string* name, uint64_t* func_offset) {
126 const Info* info;
127 if (!remap_.has_value()) {
128 // Assume the symbol table is sorted. If it is not, this will gracefully fail.
129 info = BinarySearch<SymType, false>(addr, elf_memory);
130 if (info == nullptr) {
131 // Create the remapping table and retry the search.
132 BuildRemapTable<SymType>(elf_memory);
133 symbols_.clear(); // Remove cached symbols since the access pattern will be different.
134 info = BinarySearch<SymType, true>(addr, elf_memory);
135 }
136 } else {
137 // Fast search using the previously created remap table.
138 info = BinarySearch<SymType, true>(addr, elf_memory);
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700139 }
David Srbeckyaf419602019-03-26 14:38:28 +0000140 if (info == nullptr) {
141 return false;
142 }
143 // Read the function name from the string table.
144 *func_offset = addr - info->addr;
145 uint64_t str = str_offset_ + info->name;
146 return str < str_end_ && elf_memory->ReadString(str, name, str_end_ - str);
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700147}
148
Christopher Ferris150db122017-12-20 18:49:01 -0800149template <typename SymType>
150bool Symbols::GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address) {
David Srbeckyaf419602019-03-26 14:38:28 +0000151 for (uint32_t i = 0; i < count_; i++) {
Christopher Ferris150db122017-12-20 18:49:01 -0800152 SymType entry;
David Srbeckyaf419602019-03-26 14:38:28 +0000153 if (!elf_memory->ReadFully(offset_ + i * entry_size_, &entry, sizeof(entry))) {
Christopher Ferris150db122017-12-20 18:49:01 -0800154 return false;
155 }
Christopher Ferris150db122017-12-20 18:49:01 -0800156
157 if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_OBJECT &&
158 ELF32_ST_BIND(entry.st_info) == STB_GLOBAL) {
159 uint64_t str_offset = str_offset_ + entry.st_name;
160 if (str_offset < str_end_) {
161 std::string symbol;
162 if (elf_memory->ReadString(str_offset, &symbol, str_end_ - str_offset) && symbol == name) {
163 *memory_address = entry.st_value;
164 return true;
165 }
166 }
167 }
168 }
169 return false;
170}
171
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700172// Instantiate all of the needed template functions.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700173template bool Symbols::GetName<Elf32_Sym>(uint64_t, Memory*, std::string*, uint64_t*);
174template bool Symbols::GetName<Elf64_Sym>(uint64_t, Memory*, std::string*, uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700175
Christopher Ferris150db122017-12-20 18:49:01 -0800176template bool Symbols::GetGlobal<Elf32_Sym>(Memory*, const std::string&, uint64_t*);
177template bool Symbols::GetGlobal<Elf64_Sym>(Memory*, const std::string&, uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700178} // namespace unwindstack