]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/projects/libunwind/src/EHHeaderParser.hpp
Fix incorrect exception handling.
[FreeBSD/FreeBSD.git] / contrib / llvm / projects / libunwind / src / EHHeaderParser.hpp
1 //===------------------------- EHHeaderParser.hpp -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //
9 //  Parses ELF .eh_frame_hdr sections.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef __EHHEADERPARSER_HPP__
14 #define __EHHEADERPARSER_HPP__
15
16 #include "libunwind.h"
17
18 #include "AddressSpace.hpp"
19 #include "DwarfParser.hpp"
20
21 namespace libunwind {
22
23 /// \brief EHHeaderParser does basic parsing of an ELF .eh_frame_hdr section.
24 ///
25 /// See DWARF spec for details:
26 ///    http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
27 ///
28 template <typename A> class EHHeaderParser {
29 public:
30   typedef typename A::pint_t pint_t;
31
32   /// Information encoded in the EH frame header.
33   struct EHHeaderInfo {
34     pint_t eh_frame_ptr;
35     size_t fde_count;
36     pint_t table;
37     uint8_t table_enc;
38   };
39
40   static void decodeEHHdr(A &addressSpace, pint_t ehHdrStart, pint_t ehHdrEnd,
41                           EHHeaderInfo &ehHdrInfo);
42   static bool findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart,
43                       uint32_t sectionLength,
44                       typename CFI_Parser<A>::FDE_Info *fdeInfo,
45                       typename CFI_Parser<A>::CIE_Info *cieInfo);
46
47 private:
48   static bool decodeTableEntry(A &addressSpace, pint_t &tableEntry,
49                                pint_t ehHdrStart, pint_t ehHdrEnd,
50                                uint8_t tableEnc,
51                                typename CFI_Parser<A>::FDE_Info *fdeInfo,
52                                typename CFI_Parser<A>::CIE_Info *cieInfo);
53   static size_t getTableEntrySize(uint8_t tableEnc);
54 };
55
56 template <typename A>
57 void EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart,
58                                     pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) {
59   pint_t p = ehHdrStart;
60   uint8_t version = addressSpace.get8(p++);
61   if (version != 1)
62     _LIBUNWIND_ABORT("Unsupported .eh_frame_hdr version");
63
64   uint8_t eh_frame_ptr_enc = addressSpace.get8(p++);
65   uint8_t fde_count_enc = addressSpace.get8(p++);
66   ehHdrInfo.table_enc = addressSpace.get8(p++);
67
68   ehHdrInfo.eh_frame_ptr =
69       addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart);
70   ehHdrInfo.fde_count =
71       fde_count_enc == DW_EH_PE_omit
72           ? 0
73           : addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart);
74   ehHdrInfo.table = p;
75 }
76
77 template <typename A>
78 bool EHHeaderParser<A>::decodeTableEntry(
79     A &addressSpace, pint_t &tableEntry, pint_t ehHdrStart, pint_t ehHdrEnd,
80     uint8_t tableEnc, typename CFI_Parser<A>::FDE_Info *fdeInfo,
81     typename CFI_Parser<A>::CIE_Info *cieInfo) {
82   // Have to decode the whole FDE for the PC range anyway, so just throw away
83   // the PC start.
84   addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
85   pint_t fde =
86       addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
87   const char *message =
88       CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo);
89   if (message != NULL) {
90     _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s",
91                          message);
92     return false;
93   }
94
95   return true;
96 }
97
98 template <typename A>
99 bool EHHeaderParser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart,
100                                 uint32_t sectionLength,
101                                 typename CFI_Parser<A>::FDE_Info *fdeInfo,
102                                 typename CFI_Parser<A>::CIE_Info *cieInfo) {
103   pint_t ehHdrEnd = ehHdrStart + sectionLength;
104
105   EHHeaderParser<A>::EHHeaderInfo hdrInfo;
106   EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd, hdrInfo);
107
108   size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc);
109   pint_t tableEntry;
110
111   size_t low = 0;
112   for (size_t len = hdrInfo.fde_count; len > 1;) {
113     size_t mid = low + (len / 2);
114     tableEntry = hdrInfo.table + mid * tableEntrySize;
115     pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd,
116                                             hdrInfo.table_enc, ehHdrStart);
117
118     if (start == pc) {
119       low = mid;
120       break;
121     } else if (start < pc) {
122       low = mid;
123       len -= (len / 2);
124     } else {
125       len /= 2;
126     }
127   }
128
129   tableEntry = hdrInfo.table + low * tableEntrySize;
130   if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd,
131                        hdrInfo.table_enc, fdeInfo, cieInfo)) {
132     if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd)
133       return true;
134   }
135
136   return false;
137 }
138
139 template <typename A>
140 size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) {
141   switch (tableEnc & 0x0f) {
142   case DW_EH_PE_sdata2:
143   case DW_EH_PE_udata2:
144     return 4;
145   case DW_EH_PE_sdata4:
146   case DW_EH_PE_udata4:
147     return 8;
148   case DW_EH_PE_sdata8:
149   case DW_EH_PE_udata8:
150     return 16;
151   case DW_EH_PE_sleb128:
152   case DW_EH_PE_uleb128:
153     _LIBUNWIND_ABORT("Can't binary search on variable length encoded data.");
154   case DW_EH_PE_omit:
155     return 0;
156   default:
157     _LIBUNWIND_ABORT("Unknown DWARF encoding for search table.");
158   }
159 }
160
161 }
162
163 #endif