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