blob: e3c15a29866007ef302d8c2c06857bc38ad2473b [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>
22
Christopher Ferrisd226a512017-07-14 10:37:19 -070023#include <unwindstack/Memory.h>
24
Christopher Ferris94167032017-06-28 18:56:52 -070025#include "Check.h"
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070026#include "Symbols.h"
27
Christopher Ferrisd226a512017-07-14 10:37:19 -070028namespace unwindstack {
29
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070030Symbols::Symbols(uint64_t offset, uint64_t size, uint64_t entry_size, uint64_t str_offset,
31 uint64_t str_size)
32 : cur_offset_(offset),
33 offset_(offset),
34 end_(offset + size),
35 entry_size_(entry_size),
36 str_offset_(str_offset),
37 str_end_(str_offset_ + str_size) {}
38
39const Symbols::Info* Symbols::GetInfoFromCache(uint64_t addr) {
40 // Binary search the table.
41 size_t first = 0;
42 size_t last = symbols_.size();
43 while (first < last) {
44 size_t current = first + (last - first) / 2;
45 const Info* info = &symbols_[current];
46 if (addr < info->start_offset) {
47 last = current;
48 } else if (addr < info->end_offset) {
49 return info;
50 } else {
51 first = current + 1;
52 }
53 }
54 return nullptr;
55}
56
57template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -070058bool Symbols::GetName(uint64_t addr, Memory* elf_memory, std::string* name, uint64_t* func_offset) {
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070059 if (symbols_.size() != 0) {
60 const Info* info = GetInfoFromCache(addr);
61 if (info) {
Christopher Ferris94167032017-06-28 18:56:52 -070062 CHECK(addr >= info->start_offset && addr <= info->end_offset);
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070063 *func_offset = addr - info->start_offset;
64 return elf_memory->ReadString(info->str_offset, name, str_end_ - info->str_offset);
65 }
66 }
67
68 bool symbol_added = false;
69 bool return_value = false;
70 while (cur_offset_ + entry_size_ <= end_) {
71 SymType entry;
Josh Gaoef35aa52017-10-18 11:44:51 -070072 if (!elf_memory->ReadFully(cur_offset_, &entry, sizeof(entry))) {
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070073 // Stop all processing, something looks like it is corrupted.
74 cur_offset_ = UINT64_MAX;
75 return false;
76 }
77 cur_offset_ += entry_size_;
78
79 if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_FUNC) {
80 // Treat st_value as virtual address.
81 uint64_t start_offset = entry.st_value;
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070082 uint64_t end_offset = start_offset + entry.st_size;
83
84 // Cache the value.
85 symbols_.emplace_back(start_offset, end_offset, str_offset_ + entry.st_name);
86 symbol_added = true;
87
88 if (addr >= start_offset && addr < end_offset) {
89 *func_offset = addr - start_offset;
90 uint64_t offset = str_offset_ + entry.st_name;
91 if (offset < str_end_) {
92 return_value = elf_memory->ReadString(offset, name, str_end_ - offset);
93 }
94 break;
95 }
96 }
97 }
98
99 if (symbol_added) {
100 std::sort(symbols_.begin(), symbols_.end(),
101 [](const Info& a, const Info& b) { return a.start_offset < b.start_offset; });
102 }
103 return return_value;
104}
105
Christopher Ferris150db122017-12-20 18:49:01 -0800106template <typename SymType>
107bool Symbols::GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address) {
108 uint64_t cur_offset = offset_;
109 while (cur_offset + entry_size_ <= end_) {
110 SymType entry;
111 if (!elf_memory->ReadFully(cur_offset, &entry, sizeof(entry))) {
112 return false;
113 }
114 cur_offset += entry_size_;
115
116 if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_OBJECT &&
117 ELF32_ST_BIND(entry.st_info) == STB_GLOBAL) {
118 uint64_t str_offset = str_offset_ + entry.st_name;
119 if (str_offset < str_end_) {
120 std::string symbol;
121 if (elf_memory->ReadString(str_offset, &symbol, str_end_ - str_offset) && symbol == name) {
122 *memory_address = entry.st_value;
123 return true;
124 }
125 }
126 }
127 }
128 return false;
129}
130
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700131// Instantiate all of the needed template functions.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700132template bool Symbols::GetName<Elf32_Sym>(uint64_t, Memory*, std::string*, uint64_t*);
133template bool Symbols::GetName<Elf64_Sym>(uint64_t, Memory*, std::string*, uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700134
Christopher Ferris150db122017-12-20 18:49:01 -0800135template bool Symbols::GetGlobal<Elf32_Sym>(Memory*, const std::string&, uint64_t*);
136template bool Symbols::GetGlobal<Elf64_Sym>(Memory*, const std::string&, uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700137} // namespace unwindstack