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