| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 17 | #include <elf.h> | 
|  | 18 | #include <stdint.h> | 
|  | 19 |  | 
| Florian Mayer | 3f1f2e0 | 2018-10-23 15:56:28 +0100 | [diff] [blame] | 20 | #include <algorithm> | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 21 | #include <string> | 
|  | 22 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 23 | #include <unwindstack/Memory.h> | 
|  | 24 |  | 
| Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 25 | #include "Check.h" | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 26 | #include "Symbols.h" | 
|  | 27 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 28 | namespace unwindstack { | 
|  | 29 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 30 | Symbols::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 |  | 
|  | 39 | const 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 |  | 
|  | 57 | template <typename SymType> | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 58 | bool Symbols::GetName(uint64_t addr, Memory* elf_memory, std::string* name, uint64_t* func_offset) { | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 59 | if (symbols_.size() != 0) { | 
|  | 60 | const Info* info = GetInfoFromCache(addr); | 
|  | 61 | if (info) { | 
| Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 62 | CHECK(addr >= info->start_offset && addr <= info->end_offset); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 63 | *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 Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 72 | if (!elf_memory->ReadFully(cur_offset_, &entry, sizeof(entry))) { | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 73 | // 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 Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 82 | 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 Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 106 | template <typename SymType> | 
|  | 107 | bool 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 Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 131 | // Instantiate all of the needed template functions. | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 132 | template bool Symbols::GetName<Elf32_Sym>(uint64_t, Memory*, std::string*, uint64_t*); | 
|  | 133 | template bool Symbols::GetName<Elf64_Sym>(uint64_t, Memory*, std::string*, uint64_t*); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 134 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 135 | template bool Symbols::GetGlobal<Elf32_Sym>(Memory*, const std::string&, uint64_t*); | 
|  | 136 | template bool Symbols::GetGlobal<Elf64_Sym>(Memory*, const std::string&, uint64_t*); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 137 | }  // namespace unwindstack |