• 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 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <map>
22 #include <optional>
23 #include <string>
24 #include <unordered_map>
25 
26 #include <unwindstack/SharedString.h>
27 
28 namespace unwindstack {
29 
30 // Forward declaration.
31 class Memory;
32 
33 class Symbols {
34   struct Info {
35     uint32_t size;   // Symbol size in bytes.
36     uint32_t index;  // Index into *sorted* symbol table.
37     SharedString name;
38   };
39 
40  public:
41   Symbols(uint64_t offset, uint64_t size, uint64_t entry_size, uint64_t str_offset,
42           uint64_t str_size);
43   virtual ~Symbols() = default;
44 
45   template <typename SymType>
46   bool GetName(uint64_t addr, Memory* elf_memory, SharedString* name, uint64_t* func_offset);
47 
48   template <typename SymType>
49   bool GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address);
50 
ClearCache()51   void ClearCache() {
52     symbols_.clear();
53     remap_.reset();
54   }
55 
56  private:
57   template <typename SymType, bool RemapIndices>
58   Info* BinarySearch(uint64_t addr, Memory* elf_memory, uint64_t* func_offset);
59 
60   template <typename SymType>
61   void BuildRemapTable(Memory* elf_memory);
62 
63   const uint64_t offset_;
64   const uint64_t count_;
65   const uint64_t entry_size_;
66   const uint64_t str_offset_;
67   uint64_t str_end_;
68 
69   std::map<uint64_t, Info> symbols_;  // Cache of read symbols (keyed by function *end* address).
70   std::optional<std::vector<uint32_t>> remap_;  // Indices of function symbols sorted by address.
71 
72   // Cache of global data (non-function) symbols.
73   std::unordered_map<std::string, std::optional<uint64_t>> global_variables_;
74 
75   // Do not allow the total number of symbols to go above this.
76   constexpr static size_t kMaxSymbols = 1000000;
77 };
78 
79 }  // namespace unwindstack
80