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