blob: 3b3f20b1cf18b5c4abeaaff19a23bf80ddc85570 [file] [log] [blame]
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -07001/*
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 Srbeckyaf419602019-03-26 14:38:28 +000022#include <optional>
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070023#include <string>
David Srbeckyaf419602019-03-26 14:38:28 +000024#include <unordered_map>
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070025
Christopher Ferrisd226a512017-07-14 10:37:19 -070026namespace unwindstack {
27
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070028// Forward declaration.
29class Memory;
30
31class Symbols {
32 struct Info {
David Srbeckyaf419602019-03-26 14:38:28 +000033 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 Ferrise7ba4cc2017-04-04 14:06:58 -070036 };
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 Ferrise7ba4cc2017-04-04 14:06:58 -070043 template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -070044 bool GetName(uint64_t addr, Memory* elf_memory, std::string* name, uint64_t* func_offset);
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070045
Christopher Ferris150db122017-12-20 18:49:01 -080046 template <typename SymType>
47 bool GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address);
48
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070049 void ClearCache() {
50 symbols_.clear();
David Srbeckyaf419602019-03-26 14:38:28 +000051 remap_.reset();
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070052 }
53
54 private:
David Srbeckyaf419602019-03-26 14:38:28 +000055 template <typename SymType>
56 const Info* ReadFuncInfo(uint32_t symbol_index, Memory* elf_memory);
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070057
David Srbeckyaf419602019-03-26 14:38:28 +000058 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 Ferrise7ba4cc2017-04-04 14:06:58 -070072};
73
Christopher Ferrisd226a512017-07-14 10:37:19 -070074} // namespace unwindstack
75
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070076#endif // _LIBUNWINDSTACK_SYMBOLS_H