blob: 25def40c486e85fb9822a81c784461b1efe5d3a3 [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>
57bool Symbols::GetName(uint64_t addr, uint64_t load_bias, Memory* elf_memory, std::string* name,
58 uint64_t* func_offset) {
59 addr += load_bias;
60
61 if (symbols_.size() != 0) {
62 const Info* info = GetInfoFromCache(addr);
63 if (info) {
Christopher Ferris94167032017-06-28 18:56:52 -070064 CHECK(addr >= info->start_offset && addr <= info->end_offset);
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070065 *func_offset = addr - info->start_offset;
66 return elf_memory->ReadString(info->str_offset, name, str_end_ - info->str_offset);
67 }
68 }
69
70 bool symbol_added = false;
71 bool return_value = false;
72 while (cur_offset_ + entry_size_ <= end_) {
73 SymType entry;
Josh Gaoef35aa52017-10-18 11:44:51 -070074 if (!elf_memory->ReadFully(cur_offset_, &entry, sizeof(entry))) {
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070075 // Stop all processing, something looks like it is corrupted.
76 cur_offset_ = UINT64_MAX;
77 return false;
78 }
79 cur_offset_ += entry_size_;
80
81 if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_FUNC) {
82 // Treat st_value as virtual address.
83 uint64_t start_offset = entry.st_value;
84 if (entry.st_shndx != SHN_ABS) {
85 start_offset += load_bias;
86 }
87 uint64_t end_offset = start_offset + entry.st_size;
88
89 // Cache the value.
90 symbols_.emplace_back(start_offset, end_offset, str_offset_ + entry.st_name);
91 symbol_added = true;
92
93 if (addr >= start_offset && addr < end_offset) {
94 *func_offset = addr - start_offset;
95 uint64_t offset = str_offset_ + entry.st_name;
96 if (offset < str_end_) {
97 return_value = elf_memory->ReadString(offset, name, str_end_ - offset);
98 }
99 break;
100 }
101 }
102 }
103
104 if (symbol_added) {
105 std::sort(symbols_.begin(), symbols_.end(),
106 [](const Info& a, const Info& b) { return a.start_offset < b.start_offset; });
107 }
108 return return_value;
109}
110
Christopher Ferris150db122017-12-20 18:49:01 -0800111template <typename SymType>
112bool Symbols::GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address) {
113 uint64_t cur_offset = offset_;
114 while (cur_offset + entry_size_ <= end_) {
115 SymType entry;
116 if (!elf_memory->ReadFully(cur_offset, &entry, sizeof(entry))) {
117 return false;
118 }
119 cur_offset += entry_size_;
120
121 if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_OBJECT &&
122 ELF32_ST_BIND(entry.st_info) == STB_GLOBAL) {
123 uint64_t str_offset = str_offset_ + entry.st_name;
124 if (str_offset < str_end_) {
125 std::string symbol;
126 if (elf_memory->ReadString(str_offset, &symbol, str_end_ - str_offset) && symbol == name) {
127 *memory_address = entry.st_value;
128 return true;
129 }
130 }
131 }
132 }
133 return false;
134}
135
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -0700136// Instantiate all of the needed template functions.
137template bool Symbols::GetName<Elf32_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);
138template bool Symbols::GetName<Elf64_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700139
Christopher Ferris150db122017-12-20 18:49:01 -0800140template bool Symbols::GetGlobal<Elf32_Sym>(Memory*, const std::string&, uint64_t*);
141template bool Symbols::GetGlobal<Elf64_Sym>(Memory*, const std::string&, uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700142} // namespace unwindstack