| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2016 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 |  | 
|  | 17 | #ifndef _LIBUNWINDSTACK_SYMBOLS_H | 
|  | 18 | #define _LIBUNWINDSTACK_SYMBOLS_H | 
|  | 19 |  | 
|  | 20 | #include <stdint.h> | 
|  | 21 |  | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 22 | #include <optional> | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 23 | #include <string> | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 24 | #include <unordered_map> | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 25 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 26 | namespace unwindstack { | 
|  | 27 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 28 | // Forward declaration. | 
|  | 29 | class Memory; | 
|  | 30 |  | 
|  | 31 | class Symbols { | 
|  | 32 | struct Info { | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 33 | uint64_t addr;  // Symbol address. | 
|  | 34 | uint32_t size;  // Symbol size in bytes. Zero if not a function. | 
|  | 35 | uint32_t name;  // Offset in .strtab. | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 36 | }; | 
|  | 37 |  | 
|  | 38 | public: | 
|  | 39 | Symbols(uint64_t offset, uint64_t size, uint64_t entry_size, uint64_t str_offset, | 
|  | 40 | uint64_t str_size); | 
|  | 41 | virtual ~Symbols() = default; | 
|  | 42 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 43 | template <typename SymType> | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 44 | bool 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] | 45 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 46 | template <typename SymType> | 
|  | 47 | bool GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address); | 
|  | 48 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 49 | void ClearCache() { | 
|  | 50 | symbols_.clear(); | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 51 | remap_.reset(); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
|  | 54 | private: | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 55 | template <typename SymType> | 
|  | 56 | const Info* ReadFuncInfo(uint32_t symbol_index, Memory* elf_memory); | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 57 |  | 
| David Srbecky | af41960 | 2019-03-26 14:38:28 +0000 | [diff] [blame] | 58 | template <typename SymType, bool RemapIndices> | 
|  | 59 | const Info* BinarySearch(uint64_t addr, Memory* elf_memory); | 
|  | 60 |  | 
|  | 61 | template <typename SymType> | 
|  | 62 | void BuildRemapTable(Memory* elf_memory); | 
|  | 63 |  | 
|  | 64 | const uint64_t offset_; | 
|  | 65 | const uint64_t count_; | 
|  | 66 | const uint64_t entry_size_; | 
|  | 67 | const uint64_t str_offset_; | 
|  | 68 | const uint64_t str_end_; | 
|  | 69 |  | 
|  | 70 | std::unordered_map<uint32_t, Info> symbols_;  // Cache of read symbols (keyed by symbol index). | 
|  | 71 | std::optional<std::vector<uint32_t>> remap_;  // Indices of function symbols sorted by address. | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 72 | }; | 
|  | 73 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 74 | }  // namespace unwindstack | 
|  | 75 |  | 
| Christopher Ferris | e7ba4cc | 2017-04-04 14:06:58 -0700 | [diff] [blame] | 76 | #endif  // _LIBUNWINDSTACK_SYMBOLS_H |