Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 1 | //===------------------------- EHHeaderParser.hpp -------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame^] | 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 |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 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 | |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 17 | #include "DwarfParser.hpp" |
| 18 | |
| 19 | namespace 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 | /// |
| 26 | template <typename A> class EHHeaderParser { |
| 27 | public: |
| 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 void decodeEHHdr(A &addressSpace, pint_t ehHdrStart, pint_t ehHdrEnd, |
| 39 | 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 | |
| 45 | private: |
| 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 | |
| 54 | template <typename A> |
| 55 | void EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart, |
| 56 | pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) { |
| 57 | pint_t p = ehHdrStart; |
| 58 | uint8_t version = addressSpace.get8(p++); |
| 59 | if (version != 1) |
| 60 | _LIBUNWIND_ABORT("Unsupported .eh_frame_hdr version"); |
| 61 | |
| 62 | uint8_t eh_frame_ptr_enc = addressSpace.get8(p++); |
| 63 | uint8_t fde_count_enc = addressSpace.get8(p++); |
| 64 | ehHdrInfo.table_enc = addressSpace.get8(p++); |
| 65 | |
| 66 | ehHdrInfo.eh_frame_ptr = |
| 67 | addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart); |
| 68 | ehHdrInfo.fde_count = |
Saleem Abdulrasool | f7596ec | 2017-10-20 18:47:35 | [diff] [blame] | 69 | fde_count_enc == DW_EH_PE_omit |
| 70 | ? 0 |
| 71 | : addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart); |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 72 | ehHdrInfo.table = p; |
| 73 | } |
| 74 | |
| 75 | template <typename A> |
| 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 |
| 81 | // the PC start. |
| 82 | addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart); |
| 83 | pint_t fde = |
| 84 | addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart); |
| 85 | const char *message = |
| 86 | CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo); |
| 87 | if (message != NULL) { |
Ed Maste | 92c9489 | 2016-08-30 15:38:10 | [diff] [blame] | 88 | _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s", |
Saleem Abdulrasool | b1b1911 | 2015-04-24 19:39:17 | [diff] [blame] | 89 | message); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | template <typename A> |
| 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; |
| 102 | |
| 103 | EHHeaderParser<A>::EHHeaderInfo hdrInfo; |
| 104 | EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd, hdrInfo); |
| 105 | |
| 106 | size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc); |
| 107 | pint_t tableEntry; |
| 108 | |
| 109 | size_t low = 0; |
| 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); |
| 115 | |
| 116 | if (start == pc) { |
| 117 | low = mid; |
| 118 | break; |
| 119 | } else if (start < pc) { |
| 120 | low = mid; |
| 121 | len -= (len / 2); |
| 122 | } else { |
| 123 | len /= 2; |
| 124 | } |
| 125 | } |
| 126 | |
| 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) |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | |
| 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: |
| 142 | return 4; |
| 143 | case DW_EH_PE_sdata4: |
| 144 | case DW_EH_PE_udata4: |
| 145 | return 8; |
| 146 | case DW_EH_PE_sdata8: |
| 147 | case DW_EH_PE_udata8: |
| 148 | return 16; |
| 149 | case DW_EH_PE_sleb128: |
| 150 | case DW_EH_PE_uleb128: |
| 151 | _LIBUNWIND_ABORT("Can't binary search on variable length encoded data."); |
| 152 | case DW_EH_PE_omit: |
| 153 | return 0; |
| 154 | default: |
| 155 | _LIBUNWIND_ABORT("Unknown DWARF encoding for search table."); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | #endif |