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