1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef DFX_ELF_H 16 #define DFX_ELF_H 17 18 #include <cstddef> 19 #include <elf.h> 20 #include <link.h> 21 #include <stdint.h> 22 #include <memory> 23 #include <mutex> 24 #include <memory> 25 #include <string> 26 #include <unordered_map> 27 #include <vector> 28 #include "unwind_define.h" 29 #include "dfx_memory.h" 30 #include "dfx_symbols.h" 31 32 namespace OHOS { 33 namespace HiviewDFX { 34 struct ElfFileInfo { 35 std::string name = ""; 36 std::string path = ""; 37 std::string buildId = ""; 38 std::string hash = ""; 39 40 static std::string GetNameFromPath(const std::string &path); 41 }; 42 43 struct ElfLoadInfo { 44 uint64_t offset = 0; 45 uint64_t tableVaddr = 0; 46 size_t tableSize = 0; 47 }; 48 49 class DfxElfImpl { 50 public: DfxElfImpl(std::shared_ptr<DfxMemory> memory)51 DfxElfImpl(std::shared_ptr<DfxMemory> memory) : loadBias_(0), buildIdOffset_(0), buildIdSize_(0), memory_(memory) {} 52 virtual ~DfxElfImpl() = default; 53 54 virtual bool Init(); 55 56 virtual bool IsValidPc(uint64_t pc); 57 virtual void GetMaxSize(uint64_t* size); 58 virtual uint64_t GetRealLoadOffset(uint64_t offset) const; 59 60 virtual int64_t GetLoadBias(); 61 virtual bool GetFuncNameAndOffset(uint64_t addr, std::string* funcName, uint64_t* start, uint64_t* end); 62 virtual bool GetFuncNameAndOffset(uint64_t addr, std::string* funcName, uint64_t* funcOffset); 63 virtual bool GetGlobalVariableOffset(const std::string& name, uint64_t* offset); 64 virtual std::string GetBuildID(); 65 66 protected: 67 bool ReadAllHeaders(); 68 bool ReadElfHeaders(ElfW(Ehdr)& ehdr); 69 void ReadProgramHeaders(const ElfW(Ehdr)& ehdr); 70 void ReadSectionHeaders(const ElfW(Ehdr)& ehdr); 71 72 private: 73 int64_t loadBias_; 74 uint64_t buildIdOffset_; 75 uint64_t buildIdSize_; 76 std::unordered_map<uint64_t, ElfLoadInfo> ptLoads_; 77 std::vector<SymbolInfo> symbols_; 78 std::shared_ptr<DfxMemory> memory_; 79 }; 80 81 class DfxElf { 82 public: DfxElf(std::shared_ptr<DfxMemory> memory)83 DfxElf(std::shared_ptr<DfxMemory> memory) : machine_(0), class_(0), arch_(ARCH_UNKNOWN), memory_(memory) {} 84 virtual ~DfxElf() = default; 85 86 bool Init(); IsValid()87 bool IsValid() { return valid_; } 88 89 bool IsValidPc(uint64_t pc); 90 uint64_t GetRealLoadOffset(uint64_t offset) const; 91 92 bool GetFuncNameAndOffset(uint64_t addr, std::string* funcName, uint64_t* start, uint64_t* end); 93 bool GetFuncNameAndOffset(uint64_t addr, std::string* funcName, uint64_t* funcOffset); 94 bool GetGlobalVariableOffset(const std::string& name, uint64_t* offset); 95 std::string GetBuildID(); 96 int64_t GetLoadBias(); GetMachine()97 uint16_t GetMachine() { return machine_; } GetClass()98 uint8_t GetClass() { return class_; } GetArch()99 ArchType GetArch() { return arch_; } 100 101 static bool IsValidElf(std::shared_ptr<DfxMemory> memory); 102 static uint64_t GetMaxSize(std::shared_ptr<DfxMemory> memory); 103 static std::string GetReadableBuildID(const std::string &buildIdHex); 104 private: 105 bool ReadElfInfo(); 106 private: 107 bool valid_ = false; 108 uint16_t machine_; 109 uint8_t class_; 110 ArchType arch_; 111 std::mutex lock_; 112 std::unique_ptr<DfxElfImpl> elfImpl_; 113 std::shared_ptr<DfxMemory> memory_; 114 }; 115 } // namespace HiviewDFX 116 } // namespace OHOS 117 #endif 118