EhFrameReader.h revision 533eae20118036f425f27bf0536ef0ccbb090b65
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
15namespace mcld {
16
17class Input;
18class 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 */
26class EhFrameReader
27{
28public:
29  typedef const char* ConstAddress;
30  typedef       char* Address;
31
32public:
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
42private:
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);
73private:
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
96template<> bool
97EhFrameReader::read<32, true>(Input& pInput, EhFrame& pEhFrame);
98
99template<> EhFrameReader::Token
100EhFrameReader::scan<true>(ConstAddress pHandler,
101                          uint64_t pOffset,
102                          llvm::StringRef pData) const;
103
104} // namespace of mcld
105
106#endif
107
108