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 "DwarfParser.hpp"
22 /// \brief EHHeaderParser does basic parsing of an ELF .eh_frame_hdr section.
24 /// See DWARF spec for details:
25 /// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
27 template <typename A> class EHHeaderParser {
29 typedef typename A::pint_t pint_t;
31 /// Information encoded in the EH frame header.
39 static bool decodeEHHdr(A &addressSpace, pint_t ehHdrStart, pint_t ehHdrEnd,
40 EHHeaderInfo &ehHdrInfo);
41 static bool findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart,
42 uint32_t sectionLength,
43 typename CFI_Parser<A>::FDE_Info *fdeInfo,
44 typename CFI_Parser<A>::CIE_Info *cieInfo);
47 static bool decodeTableEntry(A &addressSpace, pint_t &tableEntry,
48 pint_t ehHdrStart, pint_t ehHdrEnd,
50 typename CFI_Parser<A>::FDE_Info *fdeInfo,
51 typename CFI_Parser<A>::CIE_Info *cieInfo);
52 static size_t getTableEntrySize(uint8_t tableEnc);
56 bool EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart,
57 pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) {
58 pint_t p = ehHdrStart;
59 uint8_t version = addressSpace.get8(p++);
61 _LIBUNWIND_LOG0("Unsupported .eh_frame_hdr version");
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++);
69 ehHdrInfo.eh_frame_ptr =
70 addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart);
72 fde_count_enc == DW_EH_PE_omit
74 : addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart);
81 bool 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
87 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
89 addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);
91 CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo);
92 if (message != NULL) {
93 _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s",
101 template <typename A>
102 bool 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;
108 EHHeaderParser<A>::EHHeaderInfo hdrInfo;
109 if (!EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd,
113 size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc);
117 for (size_t len = hdrInfo.fde_count; len > 1;) {
118 size_t mid = low + (len / 2);
119 tableEntry = hdrInfo.table + mid * tableEntrySize;
120 pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd,
121 hdrInfo.table_enc, ehHdrStart);
126 } else if (start < pc) {
134 tableEntry = hdrInfo.table + low * tableEntrySize;
135 if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd,
136 hdrInfo.table_enc, fdeInfo, cieInfo)) {
137 if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd)
144 template <typename A>
145 size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) {
146 switch (tableEnc & 0x0f) {
147 case DW_EH_PE_sdata2:
148 case DW_EH_PE_udata2:
150 case DW_EH_PE_sdata4:
151 case DW_EH_PE_udata4:
153 case DW_EH_PE_sdata8:
154 case DW_EH_PE_udata8:
156 case DW_EH_PE_sleb128:
157 case DW_EH_PE_uleb128:
158 _LIBUNWIND_ABORT("Can't binary search on variable length encoded data.");
162 _LIBUNWIND_ABORT("Unknown DWARF encoding for search table.");