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_ELF_H
18 #define _LIBUNWINDSTACK_ELF_H
19 
20 #include <stddef.h>
21 
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <unordered_map>
26 #include <utility>
27 
28 #include <unwindstack/Arch.h>
29 #include <unwindstack/ElfInterface.h>
30 #include <unwindstack/Memory.h>
31 #include <unwindstack/SharedString.h>
32 
33 #if !defined(EM_AARCH64)
34 #define EM_AARCH64 183
35 #endif
36 
37 namespace unwindstack {
38 
39 // Forward declaration.
40 class MapInfo;
41 class Regs;
42 
43 class Elf {
44  public:
Elf(Memory * memory)45   Elf(Memory* memory) : memory_(memory) {}
46   virtual ~Elf() = default;
47 
48   bool Init();
49 
50   void InitGnuDebugdata();
51 
52   void Invalidate();
53 
54   std::string GetSoname();
55 
56   bool GetFunctionName(uint64_t addr, SharedString* name, uint64_t* func_offset);
57 
58   bool GetGlobalVariableOffset(const std::string& name, uint64_t* memory_offset);
59 
60   uint64_t GetRelPc(uint64_t pc, MapInfo* map_info);
61 
62   bool StepIfSignalHandler(uint64_t rel_pc, Regs* regs, Memory* process_memory);
63 
64   bool Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished,
65             bool* is_signal_frame);
66 
67   ElfInterface* CreateInterfaceFromMemory(Memory* memory);
68 
69   std::string GetBuildID();
70 
GetLoadBias()71   int64_t GetLoadBias() { return load_bias_; }
72 
73   bool IsValidPc(uint64_t pc);
74 
75   bool GetTextRange(uint64_t* addr, uint64_t* size);
76 
77   void GetLastError(ErrorData* data);
78   ErrorCode GetLastErrorCode();
79   uint64_t GetLastErrorAddress();
80 
valid()81   bool valid() { return valid_; }
82 
machine_type()83   uint32_t machine_type() { return machine_type_; }
84 
class_type()85   uint8_t class_type() { return class_type_; }
86 
arch()87   ArchEnum arch() { return arch_; }
88 
memory()89   Memory* memory() { return memory_.get(); }
90 
interface()91   ElfInterface* interface() { return interface_.get(); }
92 
gnu_debugdata_interface()93   ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); }
94 
95   static bool IsValidElf(Memory* memory);
96 
97   static bool GetInfo(Memory* memory, uint64_t* size);
98 
99   static int64_t GetLoadBias(Memory* memory);
100 
101   static std::string GetBuildID(Memory* memory);
102 
103   static void SetCachingEnabled(bool enable);
CachingEnabled()104   static bool CachingEnabled() { return cache_enabled_; }
105 
106   static void CacheLock();
107   static void CacheUnlock();
108   static void CacheAdd(MapInfo* info);
109   static bool CacheGet(MapInfo* info);
110   static bool CacheAfterCreateMemory(MapInfo* info);
111 
112  protected:
113   bool valid_ = false;
114   int64_t load_bias_ = 0;
115   std::unique_ptr<ElfInterface> interface_;
116   std::unique_ptr<Memory> memory_;
117   uint32_t machine_type_;
118   uint8_t class_type_;
119   ArchEnum arch_;
120   // Protect calls that can modify internal state of the interface object.
121   std::mutex lock_;
122 
123   std::unique_ptr<Memory> gnu_debugdata_memory_;
124   std::unique_ptr<ElfInterface> gnu_debugdata_interface_;
125 
126   static bool cache_enabled_;
127   static std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>* cache_;
128   static std::mutex* cache_lock_;
129 };
130 
131 }  // namespace unwindstack
132 
133 #endif  // _LIBUNWINDSTACK_ELF_H
134