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