1 //===- EhFrameReader.h ----------------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef MCLD_LD_EHFRAMEREADER_H 10 #define MCLD_LD_EHFRAMEREADER_H 11 #include <mcld/LD/EhFrame.h> 12 #include <llvm/ADT/StringRef.h> 13 #include <llvm/Support/DataTypes.h> 14 15 namespace mcld { 16 17 class Input; 18 class LDSection; 19 20 /** \class EhFrameReader 21 * \brief EhFrameReader reads .eh_frame section 22 * 23 * EhFrameReader is responsible to parse the input eh_frame sections and create 24 * the corresponding CIE and FDE entries. 25 */ 26 class EhFrameReader 27 { 28 public: 29 typedef const char* ConstAddress; 30 typedef char* Address; 31 32 public: 33 /// read - read an .eh_frame section and create the corresponding 34 /// CIEs and FDEs 35 /// @param pInput [in] the Input contains this eh_frame 36 /// @param pEhFrame [inout] the input eh_frame 37 /// @return if we read all CIEs and FDEs successfully, return true. Otherwise, 38 /// return false; 39 template<size_t BITCLASS, bool SAME_ENDIAN> 40 bool read(Input& pInput, EhFrame& pEhFrame); 41 42 private: 43 enum TokenKind { 44 CIE, 45 FDE, 46 Terminator, 47 Unknown, 48 NumOfTokenKinds 49 }; 50 51 enum State { 52 Q0, 53 Q1, 54 Accept, 55 NumOfStates = 2, 56 Reject = -1 57 }; 58 59 struct Token { 60 TokenKind kind; 61 size_t file_off; 62 size_t data_off; 63 uint64_t size; 64 }; 65 66 /// Action - the transition function of autometa. 67 /// @param pEhFrame - the output .eh_frame section 68 /// @param pSection - the input .eh_frame section 69 /// @param pRegion - the memory region that needs to handle with. 70 typedef bool (*Action)(EhFrame& pEhFrame, 71 llvm::StringRef pRegion, 72 const Token& pToken); 73 private: 74 /// scan - scan pData from pHandler for a token. 75 template<bool SAME_ENDIAN> Token scan(ConstAddress pHandler, 76 uint64_t pOffset, 77 llvm::StringRef pData) const; 78 79 static bool addCIE(EhFrame& pEhFrame, 80 llvm::StringRef pRegion, 81 const Token& pToken); 82 83 static bool addFDE(EhFrame& pEhFrame, 84 llvm::StringRef pRegion, 85 const Token& pToken); 86 87 static bool addTerm(EhFrame& pEhFrame, 88 llvm::StringRef pRegion, 89 const Token& pToken); 90 91 static bool reject(EhFrame& pEhFrame, 92 llvm::StringRef pRegion, 93 const Token& pToken); 94 }; 95 96 template<> bool 97 EhFrameReader::read<32, true>(Input& pInput, EhFrame& pEhFrame); 98 99 template<> EhFrameReader::Token 100 EhFrameReader::scan<true>(ConstAddress pHandler, 101 uint64_t pOffset, 102 llvm::StringRef pData) const; 103 104 } // namespace of mcld 105 106 #endif 107 108