1 //===------------------------- EHHeaderParser.hpp -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Parses ELF .eh_frame_hdr sections.
11 //===----------------------------------------------------------------------===//
13 #ifndef __EHHEADERPARSER_HPP__
14 #define __EHHEADERPARSER_HPP__
16 #include "libunwind.h"
18 #include "AddressSpace.hpp"
19 #include "DwarfParser.hpp"
23 /// \brief EHHeaderParser does basic parsing of an ELF .eh_frame_hdr section.
25 /// See DWARF spec for details:
26 /// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
28 template <typename A> class EHHeaderParser {
30 typedef typename A::pint_t pint_t;
32 /// Information encoded in the EH frame header.
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);
48 static bool decodeTableEntry(A &addressSpace, pint_t &tableEntry,
49 pint_t ehHdrStart, pint_t ehHdrEnd,
51 typename CFI_Parser<A>::FDE_Info *fdeInfo,
52 typename CFI_Parser<A>::CIE_Info *cieInfo);
53 static size_t getTableEntrySize(uint8_t tableEnc);
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++);
62 _LIBUNWIND_ABORT("Unsupported .eh_frame_hdr version");
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++);
68 ehHdrInfo.eh_frame_ptr =
69 addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart);
71 addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart);
76 bool EHHeaderParser<A>::decodeTableEntry(
77 A &addressSpace, pint_t &tableEntry, pint_t ehHdrStart, pint_t ehHdrEnd,
78 uint8_t tableEnc, typename CFI_Parser<A>::FDE_Info *fdeInfo,
79 typename CFI_Parser<A>::CIE_Info *cieInfo) {
80 // Have to decode the whole FDE for the PC range anyway, so just throw away
82 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
84 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
86 CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo);
87 if (message != NULL) {
88 _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s\n",
97 bool EHHeaderParser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart,
98 uint32_t sectionLength,
99 typename CFI_Parser<A>::FDE_Info *fdeInfo,
100 typename CFI_Parser<A>::CIE_Info *cieInfo) {
101 pint_t ehHdrEnd = ehHdrStart + sectionLength;
103 EHHeaderParser<A>::EHHeaderInfo hdrInfo;
104 EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd, hdrInfo);
106 size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc);
110 for (size_t len = hdrInfo.fde_count; len > 1;) {
111 size_t mid = low + (len / 2);
112 tableEntry = hdrInfo.table + mid * tableEntrySize;
113 pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd,
114 hdrInfo.table_enc, ehHdrStart);
119 } else if (start < pc) {
127 tableEntry = hdrInfo.table + low * tableEntrySize;
128 if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd,
129 hdrInfo.table_enc, fdeInfo, cieInfo)) {
130 if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd)
137 template <typename A>
138 size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) {
139 switch (tableEnc & 0x0f) {
140 case DW_EH_PE_sdata2:
141 case DW_EH_PE_udata2:
143 case DW_EH_PE_sdata4:
144 case DW_EH_PE_udata4:
146 case DW_EH_PE_sdata8:
147 case DW_EH_PE_udata8:
149 case DW_EH_PE_sleb128:
150 case DW_EH_PE_uleb128:
151 _LIBUNWIND_ABORT("Can't binary search on variable length encoded data.");
155 _LIBUNWIND_ABORT("Unknown DWARF encoding for search table.");