1 /* 2 * Copyright (c) 2021 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_IMAGE_H 16 #define DFX_ELF_IMAGE_H 17 #include <cstddef> 18 #include <elf.h> 19 #include <link.h> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 #include "iosfwd" 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 struct ElfLoadInfo { 28 uint64_t vaddr; 29 uint64_t offset; 30 }; 31 using ElfLoadInfo = struct ElfLoadInfo; 32 33 class DfxElf { 34 public: 35 DfxElf() = default; 36 ~DfxElf() = default; 37 38 static std::shared_ptr<DfxElf> Create(const std::string path); 39 bool ParseElfHeader(); 40 bool ParseElfProgramHeader(); 41 uint64_t FindRealLoadOffset(uint64_t offset) const; 42 void CreateLoadInfo(uint64_t vaddr, uint64_t offset); 43 44 std::string GetName() const; 45 void SetName(const std::string &name); 46 std::string GetPath() const; 47 void SetPath(const std::string &path); 48 int32_t GetFd() const; 49 void SetFd(int32_t fdValue); 50 size_t GetLoadBias() const; 51 void SetLoadBias(size_t loadBias); 52 uint64_t GetSize() const; 53 void SetSize(uint64_t size); 54 ElfW(Ehdr) GetHeader() const; 55 void SetHeader(ElfW(Ehdr) header); 56 std::vector<ElfLoadInfo> GetInfos() const; 57 void SetInfos(const std::vector<ElfLoadInfo> &infos); 58 59 private: 60 std::string name_; 61 std::string path_; 62 int32_t fd_; 63 size_t loadBias_; 64 uint64_t size_; 65 ElfW(Ehdr)header_; 66 std::vector<ElfLoadInfo> infos_; 67 }; 68 } // namespace HiviewDFX 69 } // namespace OHOS 70 #endif 71