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