/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef DFX_ELF_PARSER_H #define DFX_ELF_PARSER_H #include #if is_mingw #include "dfx_nonlinux_define.h" #else #include #include #endif #include #include #include #include #include #include #include #include #include #include "dfx_define.h" #include "dfx_elf_define.h" #include "dfx_mmap.h" #include "dfx_symbol.h" #include "unwind_context.h" namespace OHOS { namespace HiviewDFX { class ElfParser { public: ElfParser(const std::shared_ptr& mmap) : mmap_(mmap) {} virtual ~ElfParser() = default; virtual bool InitHeaders() = 0; virtual ArchType GetArchType() { return archType_; } virtual uint64_t GetElfSize(); virtual int64_t GetLoadBias() { return loadBias_; } virtual uint64_t GetStartVaddr() { return startVaddr_; } virtual uint64_t GetEndVaddr() { return endVaddr_; } virtual uint64_t GetStartOffset() { return startOffset_; } virtual std::string GetElfName() = 0; virtual uintptr_t GetGlobalPointer() = 0; virtual const std::vector& GetElfSymbols(bool isFunc) = 0; virtual bool GetSectionInfo(ShdrInfo& shdr, const uint32_t idx); virtual bool GetSectionInfo(ShdrInfo& shdr, const std::string& secName); virtual bool GetSectionData(unsigned char *buf, uint64_t size, std::string secName); virtual bool GetElfSymbolByAddr(uint64_t addr, ElfSymbol& elfSymbol) = 0; const std::unordered_map& GetPtLoads() {return ptLoads_;} bool Read(uintptr_t pos, void *buf, size_t size); std::shared_ptr GetMiniDebugInfo(); protected: size_t MmapSize(); template bool ParseAllHeaders(); template bool ParseElfHeaders(const EhdrType& ehdr); template bool ParseProgramHeaders(const EhdrType& ehdr); template bool ParseSectionHeaders(const EhdrType& ehdr); template bool IsFunc(const SymType sym); template bool ParseElfSymbols(bool isFunc); template bool ParseElfSymbols(ElfShdr shdr, bool isFunc); template bool ParseElfSymbolName(ShdrInfo linkShdr, SymType sym, std::string& nameStr); template bool ParseElfSymbolByAddr(uint64_t addr, ElfSymbol& elfSymbol); template bool ParseElfDynamic(); template bool ParseElfName(); bool ParseStrTab(std::string& nameStr, const uint64_t offset, const uint64_t size); bool GetSectionNameByIndex(std::string& nameStr, const uint32_t name); protected: std::vector elfSymbols_; uint64_t dynamicOffset_ = 0; uintptr_t dtPltGotAddr_ = 0; uintptr_t dtStrtabAddr_ = 0; uintptr_t dtStrtabSize_ = 0; uintptr_t dtSonameOffset_ = 0; std::string soname_ = ""; std::shared_ptr minidebugInfo_ = nullptr; private: std::shared_ptr mmap_; ArchType archType_ = ARCH_UNKNOWN; uint64_t elfSize_ = 0; int64_t loadBias_ = 0; uint64_t startVaddr_ = static_cast(-1); uint64_t startOffset_ = 0; uint64_t endVaddr_ = 0; std::vector symShdrs_; std::map, ShdrInfo> shdrInfoPairs_; std::unordered_map ptLoads_; std::string sectionNames_; }; class ElfParser32 : public ElfParser { public: ElfParser32(const std::shared_ptr& mmap) : ElfParser(mmap) {} virtual ~ElfParser32() = default; bool InitHeaders() override; std::string GetElfName() override; uintptr_t GetGlobalPointer() override; const std::vector& GetElfSymbols(bool isFunc) override; bool GetElfSymbolByAddr(uint64_t addr, ElfSymbol& elfSymbol) override; }; class ElfParser64 : public ElfParser { public: ElfParser64(const std::shared_ptr& mmap) : ElfParser(mmap) {} virtual ~ElfParser64() = default; bool InitHeaders() override; std::string GetElfName() override; uintptr_t GetGlobalPointer() override; const std::vector& GetElfSymbols(bool isFunc) override; bool GetElfSymbolByAddr(uint64_t addr, ElfSymbol& elfSymbol) override; }; } // namespace HiviewDFX } // namespace OHOS #endif