blob: f4cdbdaf43f6592d5b11d9a6a9c29f74c8ef3a34 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
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_ELF_H
18#define _LIBUNWINDSTACK_ELF_H
19
20#include <stddef.h>
21
22#include <memory>
Christopher Ferrisbe788d82017-11-27 14:50:38 -080023#include <mutex>
Christopher Ferris3958f802017-02-01 15:44:40 -080024#include <string>
Christopher Ferris0b79ae12018-01-25 12:15:56 -080025#include <unordered_map>
Christopher Ferrisd9575b62018-02-16 13:48:19 -080026#include <utility>
Christopher Ferris3958f802017-02-01 15:44:40 -080027
Christopher Ferrisd226a512017-07-14 10:37:19 -070028#include <unwindstack/ElfInterface.h>
29#include <unwindstack/Memory.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080030
31#if !defined(EM_AARCH64)
32#define EM_AARCH64 183
33#endif
34
Christopher Ferrisd226a512017-07-14 10:37:19 -070035namespace unwindstack {
36
Christopher Ferris3958f802017-02-01 15:44:40 -080037// Forward declaration.
Christopher Ferrisd226a512017-07-14 10:37:19 -070038struct MapInfo;
Christopher Ferris3958f802017-02-01 15:44:40 -080039class Regs;
40
Christopher Ferrisd06001d2017-11-30 18:56:01 -080041enum ArchEnum : uint8_t {
42 ARCH_UNKNOWN = 0,
43 ARCH_ARM,
44 ARCH_ARM64,
45 ARCH_X86,
46 ARCH_X86_64,
Douglas Leung61b1a1a2017-11-08 10:53:53 +010047 ARCH_MIPS,
48 ARCH_MIPS64,
Christopher Ferrisd06001d2017-11-30 18:56:01 -080049};
50
Christopher Ferris3958f802017-02-01 15:44:40 -080051class Elf {
52 public:
53 Elf(Memory* memory) : memory_(memory) {}
54 virtual ~Elf() = default;
55
Christopher Ferrise69f4702017-10-19 16:08:58 -070056 bool Init(bool init_gnu_debugdata);
Christopher Ferris3958f802017-02-01 15:44:40 -080057
58 void InitGnuDebugdata();
59
Christopher Ferrisd226a512017-07-14 10:37:19 -070060 bool GetSoname(std::string* name);
Christopher Ferris3958f802017-02-01 15:44:40 -080061
Christopher Ferrisd226a512017-07-14 10:37:19 -070062 bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset);
Christopher Ferris3958f802017-02-01 15:44:40 -080063
Christopher Ferris150db122017-12-20 18:49:01 -080064 bool GetGlobalVariable(const std::string& name, uint64_t* memory_address);
65
Christopher Ferrisd226a512017-07-14 10:37:19 -070066 uint64_t GetRelPc(uint64_t pc, const MapInfo* map_info);
67
Christopher Ferrisd299a7a2018-05-17 18:37:38 -070068 bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, Regs* regs, Memory* process_memory,
69 bool* finished);
Christopher Ferris3958f802017-02-01 15:44:40 -080070
71 ElfInterface* CreateInterfaceFromMemory(Memory* memory);
72
Christopher Ferrise69f4702017-10-19 16:08:58 -070073 uint64_t GetLoadBias() { return load_bias_; }
Christopher Ferrisd226a512017-07-14 10:37:19 -070074
Christopher Ferris150db122017-12-20 18:49:01 -080075 bool IsValidPc(uint64_t pc);
76
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080077 void GetLastError(ErrorData* data);
78 ErrorCode GetLastErrorCode();
79 uint64_t GetLastErrorAddress();
80
Christopher Ferris3958f802017-02-01 15:44:40 -080081 bool valid() { return valid_; }
82
83 uint32_t machine_type() { return machine_type_; }
84
85 uint8_t class_type() { return class_type_; }
86
Christopher Ferrisd06001d2017-11-30 18:56:01 -080087 ArchEnum arch() { return arch_; }
88
Christopher Ferris3958f802017-02-01 15:44:40 -080089 Memory* memory() { return memory_.get(); }
90
91 ElfInterface* interface() { return interface_.get(); }
92
Christopher Ferrisbae69f12017-06-28 14:51:54 -070093 ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); }
94
Christopher Ferris3958f802017-02-01 15:44:40 -080095 static bool IsValidElf(Memory* memory);
96
Christopher Ferris3f805ac2017-08-30 13:15:19 -070097 static void GetInfo(Memory* memory, bool* valid, uint64_t* size);
98
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080099 static uint64_t GetLoadBias(Memory* memory);
100
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800101 static void SetCachingEnabled(bool enable);
102 static bool CachingEnabled() { return cache_enabled_; }
103
104 static void CacheLock();
105 static void CacheUnlock();
106 static void CacheAdd(MapInfo* info);
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800107 static bool CacheGet(MapInfo* info);
108 static bool CacheAfterCreateMemory(MapInfo* info);
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800109
Christopher Ferris3958f802017-02-01 15:44:40 -0800110 protected:
111 bool valid_ = false;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700112 uint64_t load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800113 std::unique_ptr<ElfInterface> interface_;
114 std::unique_ptr<Memory> memory_;
115 uint32_t machine_type_;
116 uint8_t class_type_;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800117 ArchEnum arch_;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800118 // Protect calls that can modify internal state of the interface object.
119 std::mutex lock_;
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700120
121 std::unique_ptr<Memory> gnu_debugdata_memory_;
122 std::unique_ptr<ElfInterface> gnu_debugdata_interface_;
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800123
124 static bool cache_enabled_;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800125 static std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>* cache_;
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800126 static std::mutex* cache_lock_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800127};
128
Christopher Ferrisd226a512017-07-14 10:37:19 -0700129} // namespace unwindstack
130
Christopher Ferris3958f802017-02-01 15:44:40 -0800131#endif // _LIBUNWINDSTACK_ELF_H