EhFrame.h revision 67e37f1be98c926645219cfb47fab9e90d8c725c
1//===- EhFrame.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_EXCEPTION_HANDLING_FRAME_H
10#define MCLD_EXCEPTION_HANDLING_FRAME_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14#include <vector>
15
16#include <mcld/ADT/TypeTraits.h>
17#include <mcld/LD/CIE.h>
18#include <mcld/LD/FDE.h>
19#include <mcld/MC/MCRegionFragment.h>
20#include <mcld/Support/GCFactory.h>
21
22namespace mcld
23{
24/** \class EhFrame
25 *  \brief EhFrame represents .eh_frame section
26 *  EhFrame is responsible to parse the input eh_frame sections and create
27 *  the corresponding CIE and FDE entries.
28 */
29
30class TargetLDBackend;
31class Input;
32
33class EhFrame
34{
35public:
36  typedef ConstTraits<unsigned char>::pointer ConstAddress;
37  typedef std::vector<CIE*> CIEListType;
38  typedef std::vector<FDE*> FDEListType;
39  typedef CIEListType::iterator cie_iterator;
40  typedef CIEListType::const_iterator const_cie_iterator;
41  typedef FDEListType::iterator fde_iterator;
42  typedef FDEListType::const_iterator const_fde_iterator;
43
44public:
45  EhFrame();
46  ~EhFrame();
47
48  /// readEhFrame - read an .eh_frame section and create the corresponding
49  /// CIEs and FDEs
50  /// @param pSD - the MCSectionData of this input eh_frame
51  /// @param pSection - the input eh_frame
52  /// @param pArea - the memory area which pSection is within.
53  /// @ return - size of this eh_frame section, 0 if we do not recognize
54  /// this eh_frame or this is an empty section
55  uint64_t readEhFrame(Layout& pLayout,
56                       const TargetLDBackend& pBackend,
57                       llvm::MCSectionData& pSD,
58                       const Input& pInput,
59                       LDSection& pSection,
60                       MemoryArea& pArea);
61
62  // ----- observers ----- //
63  cie_iterator cie_begin()
64  { return m_CIEs.begin(); }
65
66  const_cie_iterator cie_begin() const
67  { return m_CIEs.begin(); }
68
69  cie_iterator cie_end()
70  { return m_CIEs.end(); }
71
72  const_cie_iterator cie_end() const
73  { return m_CIEs.end(); }
74
75  fde_iterator fde_begin()
76  { return m_FDEs.begin(); }
77
78  const_fde_iterator fde_begin() const
79  { return m_FDEs.begin(); }
80
81  fde_iterator fde_end()
82  { return m_FDEs.end(); }
83
84  const_fde_iterator fde_end() const
85  { return m_FDEs.end(); }
86
87  /// getFDECount - the number of FDE entries
88  size_t getFDECount()
89  { return m_FDEs.size(); }
90
91  size_t getFDECount() const
92  { return m_FDEs.size(); }
93
94  /// canRecognizeAllEhFrame - return if we are able to parse all input
95  /// eh_frame sections
96  /// @return false - if there is any input .eh_frame section that
97  /// we are not able to recognize
98  bool canRecognizeAllEhFrame()
99  { return m_fCanRecognizeAll; }
100
101  bool canRecognizeAllEhFrame() const
102  { return m_fCanRecognizeAll; }
103
104private:
105  typedef std::vector<llvm::MCFragment*> FragListType;
106
107private:
108  /// addCIE - parse and create a CIE entry
109  /// @return false - cannot recognize this CIE
110  bool addCIE(MemoryRegion& pFrag,
111              const TargetLDBackend& pBackend,
112              FragListType& pFragList);
113
114  /// addFDE - parse and create an FDE entry
115  /// @return false - cannot recognize this FDE
116  bool addFDE(MemoryRegion& pFrag,
117              const TargetLDBackend& pBackend,
118              FragListType& pFragList);
119
120  /// readVal - read a 32 bit data from pAddr, swap it if needed
121  uint32_t readVal(ConstAddress pAddr, bool pIsTargetLittleEndian);
122
123  /// skipLEB128 - skip the first LEB128 encoded value from *pp, update *pp
124  /// to the next character.
125  /// @return - false if we ran off the end of the string.
126  /// @ref - GNU gold 1.11, ehframe.h, Eh_frame::skip_leb128.
127  bool skipLEB128(ConstAddress* pp, ConstAddress pend);
128
129  /// deleteFragments - release the MemoryRegion and delete MCFragments in pList
130  void deleteFragments(FragListType& pList, MemoryArea& pArea);
131
132private:
133  CIEListType m_CIEs;
134  FDEListType m_FDEs;
135
136  bool m_fCanRecognizeAll;
137};
138
139} // namespace of mcld
140
141#endif
142
143