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