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 | |
| 20 | #include <string> |
| 21 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 22 | #include <unwindstack/Memory.h> |
| 23 | |
Christopher Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 24 | #include "Check.h" |
Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 25 | #include "Symbols.h" |
| 26 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 27 | namespace unwindstack { |
| 28 | |
Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 29 | Symbols::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 | |
| 38 | const 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 | |
| 56 | template <typename SymType> |
| 57 | bool 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 Ferris | 9416703 | 2017-06-28 18:56:52 -0700 | [diff] [blame] | 64 | CHECK(addr >= info->start_offset && addr <= info->end_offset); |
Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 65 | *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; |
| 74 | if (!elf_memory->Read(cur_offset_, &entry, sizeof(entry))) { |
| 75 | // 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 | |
| 111 | // Instantiate all of the needed template functions. |
| 112 | template bool Symbols::GetName<Elf32_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*); |
| 113 | template bool Symbols::GetName<Elf64_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 114 | |
| 115 | } // namespace unwindstack |