• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef ELF_PARSER_IMITATE_H
17 #define ELF_PARSER_IMITATE_H
18 
19 #include <cstddef>
20 #include <elf.h>
21 #include <link.h>
22 #include <stdint.h>
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <unordered_map>
30 #include <vector>
31 #include "dfx_define.h"
32 #include "dfx_mmap.h"
33 #include "dfx_symbol.h"
34 #include "dfx_symbol.h"
35 #include "dfx_symbols.h"
36 #include "dfx_elf_define.h"
37 #include "unwind_context.h"
38 
39 namespace OHOS {
40 namespace HiviewDFX {
41 class ElfImitate {
42 public:
43     enum class ElfFileType {
44         ELF32,
45         ELF64,
46     };
47     enum Index {
48         INDEX_I0 = 0,
49         INDEX_I1,
50         INDEX_I2,
51         INDEX_I3,
52         INDEX_I4,
53         INDEX_I5,
54         INDEX_I6,
55         INDEX_I7,
56     };
ElfImitate()57     ElfImitate()  {}
58     ~ElfImitate();
59 
60     bool ParseAllHeaders(ElfFileType elfFileType);
GetArchType()61     ArchType GetArchType() { return archType_; }
GetElfSize()62     uint64_t GetElfSize() {return elfSize_; }
GetLoadBias()63     int64_t GetLoadBias() { return loadBias_; }
GetStartVaddr()64     uint64_t GetStartVaddr() { return startVaddr_; }
GetEndVaddr()65     uint64_t GetEndVaddr() { return endVaddr_; }
66     const std::vector<ElfSymbol>& GetElfSymbols();
67     bool GetSectionInfo(ShdrInfo& shdr, const std::string secName);
GetPtLoads()68     const std::unordered_map<uint64_t, ElfLoadInfo>& GetPtLoads() {return ptLoads_;}
GetClassType()69     uint8_t GetClassType() { return classType_;}
70     uint64_t GetLoadBase(uint64_t mapStart, uint64_t mapOffset);
71     uint64_t GetStartPc();
72     uint64_t GetEndPc();
73     uint64_t GetRelPc(uint64_t pc, uint64_t mapStart, uint64_t mapOffset);
74     bool ParseSymbols(std::vector<DfxSymbol>& symbols, const std::string& filePath);
75     bool AddSymbolsByPlt(std::vector<DfxSymbol>& symbols, const std::string& filePath);
76     bool GetFuncNameAndOffset(uint64_t pc, std::string& funcName, uint64_t& start, uint64_t& end);
77 protected:
78     bool ParseElfHeaders();
79     bool ParseProgramHeaders(ElfFileType fileType);
80     bool ParseSectionHeaders(ElfFileType fileType);
81     bool ParseElfSymbols();
82 
83 protected:
84     std::vector<ElfSymbol> elfSymbols_;
85 
86 private:
87     std::vector<std::string> StringSplit(std::string src, const std::string split);
88     bool GetMagic(FILE * const fp);
89     bool GetClass(FILE * const fp);
90     bool GetMachine(FILE * const fp);
91     bool GetEntryAddr(FILE * const fp);
92     bool GetPrgOffset(FILE * const fp);
93     bool GetSecOffset(FILE * const fp);
94     bool GetFlag(FILE * const fp);
95     bool GetEhdrSize(FILE * const fp);
96     bool GetPhdrSize(FILE * const fp);
97     bool GetNumPhdrs(FILE * const fp);
98     bool GetShdrSize(FILE * const fp);
99     bool GetNumShdrs(FILE * const fp);
100     bool GetShdrStrTabIdx(FILE * const fp);
101     int64_t GetSecIndex(const std::string &line);
102     const std::string GetNextShdrLine();
103     const std::string GetNextPhdrLine();
104     const std::string GetNextSymLine();
105 
106     static bool IsFunc(const ElfSymbol symbol);
107     std::shared_ptr<DfxMmap> mmap_;
108     ArchType archType_ = ARCH_UNKNOWN;
109     uint64_t elfSize_ = 0;
110     int64_t loadBias_ = 0;
111     uint64_t startVaddr_ = static_cast<uint64_t>(-1);
112     uint64_t endVaddr_ = 0;
113     std::unordered_map<std::string, ElfShdr> symShdrs_;
114     std::map<std::pair<uint32_t, const std::string>, ShdrInfo> shdrInfoPairs_;
115     std::unordered_map<uint64_t, ElfLoadInfo> ptLoads_;
116     FILE *ehdrFP_ {nullptr};
117     FILE *shdrFP_ {nullptr};
118     FILE *phdrFP_ {nullptr};
119     FILE *symTabFP_ {nullptr};
120     unsigned char ehdrIdent_[EI_NIDENT];
121     std::string machine_;
122     uint8_t classType_;
123     uint16_t ehdrSize_;
124     uint16_t phdrEntSize_;
125     uint16_t phdrNumEnts_;
126     uint16_t shdrEntSize_;
127     uint16_t shdrNumEnts_;
128     uint16_t shdrStrTabIdx_;
129     uint32_t ehdrFlags_;
130     uint64_t prgEntryVaddr_;
131     uint64_t phdrOffset_;
132     uint64_t shdrOffset_;
133     uint64_t loadBase_ = static_cast<uint64_t>(-1);
134     uint64_t startPc_ = static_cast<uint64_t>(-1);
135     uint64_t endPc_ = static_cast<uint64_t>(-1);
136 };
137 }
138 }
139 #endif //ELF_PARSER_IMITATE_H