1//===----------------------------------------------------------------------===//
2//
3// 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
6//
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
17#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
38 static bool decodeEHHdr(A &addressSpace, pint_t ehHdrStart, pint_t ehHdrEnd,
39 EHHeaderInfo &ehHdrInfo);
40 template <typename R>
41 static bool findFDE(A &addressSpace, typename R::link_hardened_reg_arg_t pc,
42 pint_t ehHdrStart, uint32_t sectionLength,
43 typename CFI_Parser<A>::FDE_Info *fdeInfo,
44 typename CFI_Parser<A>::CIE_Info *cieInfo);
45
46private:
47 static bool decodeTableEntry(A &addressSpace, pint_t &tableEntry,
48 pint_t ehHdrStart, pint_t ehHdrEnd,
49 uint8_t tableEnc,
50 typename CFI_Parser<A>::FDE_Info *fdeInfo,
51 typename CFI_Parser<A>::CIE_Info *cieInfo);
52 static size_t getTableEntrySize(uint8_t tableEnc);
53};
54
55template <typename A>
56bool EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart,
57 pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) {
58 pint_t p = ehHdrStart;
59
60 // Ensure that we don't read data beyond the end of .eh_frame_hdr
61 if (ehHdrEnd - ehHdrStart < 4) {
62 // Don't print a message for an empty .eh_frame_hdr (this can happen if
63 // the linker script defines symbols for it even in the empty case).
64 if (ehHdrEnd == ehHdrStart)
65 return false;
66 _LIBUNWIND_LOG("unsupported .eh_frame_hdr at %" PRIx64
67 ": need at least 4 bytes of data but only got %zd",
68 static_cast<uint64_t>(ehHdrStart),
69 static_cast<size_t>(ehHdrEnd - ehHdrStart));
70 return false;
71 }
72 uint8_t version = addressSpace.get8(p++);
73 if (version != 1) {
74 _LIBUNWIND_LOG("unsupported .eh_frame_hdr version: %" PRIu8 " at %" PRIx64,
75 version, static_cast<uint64_t>(ehHdrStart));
76 return false;
77 }
78
79 uint8_t eh_frame_ptr_enc = addressSpace.get8(p++);
80 uint8_t fde_count_enc = addressSpace.get8(p++);
81 ehHdrInfo.table_enc = addressSpace.get8(p++);
82
83 ehHdrInfo.eh_frame_ptr =
84 addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart);
85 ehHdrInfo.fde_count =
86 fde_count_enc == DW_EH_PE_omit
87 ? 0
88 : addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart);
89 ehHdrInfo.table = p;
90
91 return true;
92}
93
94template <typename A>
95bool EHHeaderParser<A>::decodeTableEntry(
96 A &addressSpace, pint_t &tableEntry, pint_t ehHdrStart, pint_t ehHdrEnd,
97 uint8_t tableEnc, typename CFI_Parser<A>::FDE_Info *fdeInfo,
98 typename CFI_Parser<A>::CIE_Info *cieInfo) {
99 // Have to decode the whole FDE for the PC range anyway, so just throw away
100 // the PC start.
101 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
102 pint_t fde =
103 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
104 const char *message =
105 CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo);
106 if (message != NULL) {
107 _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s",
108 message);
109 return false;
110 }
111
112 return true;
113}
114
115template <typename A>
116template <typename R>
117bool EHHeaderParser<A>::findFDE(A &addressSpace,
118 typename R::link_hardened_reg_arg_t pc,
119 pint_t ehHdrStart, uint32_t sectionLength,
120 typename CFI_Parser<A>::FDE_Info *fdeInfo,
121 typename CFI_Parser<A>::CIE_Info *cieInfo) {
122 pint_t ehHdrEnd = ehHdrStart + sectionLength;
123
124 EHHeaderParser<A>::EHHeaderInfo hdrInfo;
125 if (!EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd,
126 ehHdrInfo&: hdrInfo))
127 return false;
128
129 if (hdrInfo.fde_count == 0) return false;
130
131 size_t tableEntrySize = getTableEntrySize(tableEnc: hdrInfo.table_enc);
132 pint_t tableEntry;
133
134 size_t low = 0;
135 for (size_t len = hdrInfo.fde_count; len > 1;) {
136 size_t mid = low + (len / 2);
137 tableEntry = hdrInfo.table + mid * tableEntrySize;
138 pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd,
139 hdrInfo.table_enc, ehHdrStart);
140
141 if (start == pc) {
142 low = mid;
143 break;
144 } else if (start < pc) {
145 low = mid;
146 len -= (len / 2);
147 } else {
148 len /= 2;
149 }
150 }
151
152 tableEntry = hdrInfo.table + low * tableEntrySize;
153 if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd,
154 tableEnc: hdrInfo.table_enc, fdeInfo, cieInfo)) {
155 if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd)
156 return true;
157 }
158
159 return false;
160}
161
162template <typename A>
163size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) {
164 switch (tableEnc & 0x0f) {
165 case DW_EH_PE_sdata2:
166 case DW_EH_PE_udata2:
167 return 4;
168 case DW_EH_PE_sdata4:
169 case DW_EH_PE_udata4:
170 return 8;
171 case DW_EH_PE_sdata8:
172 case DW_EH_PE_udata8:
173 return 16;
174 case DW_EH_PE_sleb128:
175 case DW_EH_PE_uleb128:
176 _LIBUNWIND_ABORT("Can't binary search on variable length encoded data.");
177 case DW_EH_PE_omit:
178 return 0;
179 default:
180 _LIBUNWIND_ABORT("Unknown DWARF encoding for search table.");
181 }
182}
183
184}
185
186#endif
187