blob: ed4317c89055c9e57482fc6460a4b738adc20049 [file] [log] [blame]
Louis Dionne7f068e52021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002//
Chandler Carruth61860a52019-01-19 10:56:40 +00003// 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 Abdulrasool17552662015-04-24 19:39:17 +00006//
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 Abdulrasool17552662015-04-24 19:39:17 +000017#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 Hosek06be3282019-01-24 03:04:42 +000038 static bool decodeEHHdr(A &addressSpace, pint_t ehHdrStart, pint_t ehHdrEnd,
Saleem Abdulrasool17552662015-04-24 19:39:17 +000039 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 Hosek06be3282019-01-24 03:04:42 +000055bool EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart,
Saleem Abdulrasool17552662015-04-24 19:39:17 +000056 pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) {
57 pint_t p = ehHdrStart;
58 uint8_t version = addressSpace.get8(p++);
Petr Hosek06be3282019-01-24 03:04:42 +000059 if (version != 1) {
Florian Mayer86ab9dd2022-06-01 16:43:59 -070060 _LIBUNWIND_LOG("unsupported .eh_frame_hdr version: %" PRIu8 " at %" PRIx64,
61 version, static_cast<uint64_t>(ehHdrStart));
Petr Hosek06be3282019-01-24 03:04:42 +000062 return false;
63 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +000064
65 uint8_t eh_frame_ptr_enc = addressSpace.get8(p++);
66 uint8_t fde_count_enc = addressSpace.get8(p++);
67 ehHdrInfo.table_enc = addressSpace.get8(p++);
68
69 ehHdrInfo.eh_frame_ptr =
70 addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart);
71 ehHdrInfo.fde_count =
Saleem Abdulrasoolc973ac92017-10-20 18:47:35 +000072 fde_count_enc == DW_EH_PE_omit
73 ? 0
74 : addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000075 ehHdrInfo.table = p;
Petr Hosek06be3282019-01-24 03:04:42 +000076
77 return true;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000078}
79
80template <typename A>
81bool EHHeaderParser<A>::decodeTableEntry(
82 A &addressSpace, pint_t &tableEntry, pint_t ehHdrStart, pint_t ehHdrEnd,
83 uint8_t tableEnc, typename CFI_Parser<A>::FDE_Info *fdeInfo,
84 typename CFI_Parser<A>::CIE_Info *cieInfo) {
85 // Have to decode the whole FDE for the PC range anyway, so just throw away
86 // the PC start.
87 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
88 pint_t fde =
89 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
90 const char *message =
91 CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo);
92 if (message != NULL) {
Ed Maste41bc5a72016-08-30 15:38:10 +000093 _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000094 message);
95 return false;
96 }
97
98 return true;
99}
100
101template <typename A>
102bool EHHeaderParser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart,
103 uint32_t sectionLength,
104 typename CFI_Parser<A>::FDE_Info *fdeInfo,
105 typename CFI_Parser<A>::CIE_Info *cieInfo) {
106 pint_t ehHdrEnd = ehHdrStart + sectionLength;
107
108 EHHeaderParser<A>::EHHeaderInfo hdrInfo;
Petr Hosek06be3282019-01-24 03:04:42 +0000109 if (!EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd,
110 hdrInfo))
111 return false;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000112
Jorge Gorbe Moya05c95982020-04-07 14:44:42 -0700113 if (hdrInfo.fde_count == 0) return false;
114
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000115 size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc);
116 pint_t tableEntry;
117
118 size_t low = 0;
119 for (size_t len = hdrInfo.fde_count; len > 1;) {
120 size_t mid = low + (len / 2);
121 tableEntry = hdrInfo.table + mid * tableEntrySize;
122 pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd,
123 hdrInfo.table_enc, ehHdrStart);
124
125 if (start == pc) {
126 low = mid;
127 break;
128 } else if (start < pc) {
129 low = mid;
130 len -= (len / 2);
131 } else {
132 len /= 2;
133 }
134 }
135
136 tableEntry = hdrInfo.table + low * tableEntrySize;
137 if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd,
138 hdrInfo.table_enc, fdeInfo, cieInfo)) {
139 if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd)
140 return true;
141 }
142
143 return false;
144}
145
146template <typename A>
147size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) {
148 switch (tableEnc & 0x0f) {
149 case DW_EH_PE_sdata2:
150 case DW_EH_PE_udata2:
151 return 4;
152 case DW_EH_PE_sdata4:
153 case DW_EH_PE_udata4:
154 return 8;
155 case DW_EH_PE_sdata8:
156 case DW_EH_PE_udata8:
157 return 16;
158 case DW_EH_PE_sleb128:
159 case DW_EH_PE_uleb128:
160 _LIBUNWIND_ABORT("Can't binary search on variable length encoded data.");
161 case DW_EH_PE_omit:
162 return 0;
163 default:
164 _LIBUNWIND_ABORT("Unknown DWARF encoding for search table.");
165 }
166}
167
168}
169
170#endif