• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include <map>
23 #include <optional>
24 #include <string>
25 #include <unordered_map>
26 
27 #include <unwindstack/SharedString.h>
28 
29 namespace unwindstack {
30 
31 // Forward declaration.
32 class Memory;
33 
34 class Symbols {
35   struct Info {
36     uint32_t size;   // Symbol size in bytes.
37     uint32_t index;  // Index into *sorted* symbol table.
38     SharedString name;
39   };
40 
41  public:
42   Symbols(uint64_t offset, uint64_t size, uint64_t entry_size, uint64_t str_offset,
43           uint64_t str_size);
44   virtual ~Symbols() = default;
45 
46   template <typename SymType>
47   bool GetName(uint64_t addr, Memory* elf_memory, SharedString* name, uint64_t* func_offset);
48 
49   template <typename SymType>
50   bool GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address);
51 
ClearCache()52   void ClearCache() {
53     symbols_.clear();
54     remap_.reset();
55   }
56 
57  private:
58   template <typename SymType, bool RemapIndices>
59   Info* BinarySearch(uint64_t addr, Memory* elf_memory, uint64_t* func_offset);
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::map<uint64_t, Info> symbols_;  // Cache of read symbols (keyed by function *end* address).
71   std::optional<std::vector<uint32_t>> remap_;  // Indices of function symbols sorted by address.
72 
73   // Cache of global data (non-function) symbols.
74   std::unordered_map<std::string, std::optional<uint64_t>> global_variables_;
75 };
76 
77 }  // namespace unwindstack
78 
79 #endif  // _LIBUNWINDSTACK_SYMBOLS_H
80