1349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric // 80b57cec5SDimitry Andric // Parses ELF .eh_frame_hdr sections. 90b57cec5SDimitry Andric // 100b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #ifndef __EHHEADERPARSER_HPP__ 130b57cec5SDimitry Andric #define __EHHEADERPARSER_HPP__ 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "libunwind.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "DwarfParser.hpp" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace libunwind { 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric /// \brief EHHeaderParser does basic parsing of an ELF .eh_frame_hdr section. 220b57cec5SDimitry Andric /// 230b57cec5SDimitry Andric /// See DWARF spec for details: 240b57cec5SDimitry Andric /// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html 250b57cec5SDimitry Andric /// 260b57cec5SDimitry Andric template <typename A> class EHHeaderParser { 270b57cec5SDimitry Andric public: 280b57cec5SDimitry Andric typedef typename A::pint_t pint_t; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric /// Information encoded in the EH frame header. 310b57cec5SDimitry Andric struct EHHeaderInfo { 320b57cec5SDimitry Andric pint_t eh_frame_ptr; 330b57cec5SDimitry Andric size_t fde_count; 340b57cec5SDimitry Andric pint_t table; 350b57cec5SDimitry Andric uint8_t table_enc; 360b57cec5SDimitry Andric }; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric static bool decodeEHHdr(A &addressSpace, pint_t ehHdrStart, pint_t ehHdrEnd, 390b57cec5SDimitry Andric EHHeaderInfo &ehHdrInfo); 400b57cec5SDimitry Andric static bool findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart, 410b57cec5SDimitry Andric uint32_t sectionLength, 420b57cec5SDimitry Andric typename CFI_Parser<A>::FDE_Info *fdeInfo, 430b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info *cieInfo); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric private: 460b57cec5SDimitry Andric static bool decodeTableEntry(A &addressSpace, pint_t &tableEntry, 470b57cec5SDimitry Andric pint_t ehHdrStart, pint_t ehHdrEnd, 480b57cec5SDimitry Andric uint8_t tableEnc, 490b57cec5SDimitry Andric typename CFI_Parser<A>::FDE_Info *fdeInfo, 500b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info *cieInfo); 510b57cec5SDimitry Andric static size_t getTableEntrySize(uint8_t tableEnc); 520b57cec5SDimitry Andric }; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric template <typename A> 550b57cec5SDimitry Andric bool EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart, 560b57cec5SDimitry Andric pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) { 570b57cec5SDimitry Andric pint_t p = ehHdrStart; 58*5f757f3fSDimitry Andric 59*5f757f3fSDimitry Andric // Ensure that we don't read data beyond the end of .eh_frame_hdr 60*5f757f3fSDimitry Andric if (ehHdrEnd - ehHdrStart < 4) { 61*5f757f3fSDimitry Andric // Don't print a message for an empty .eh_frame_hdr (this can happen if 62*5f757f3fSDimitry Andric // the linker script defines symbols for it even in the empty case). 63*5f757f3fSDimitry Andric if (ehHdrEnd == ehHdrStart) 64*5f757f3fSDimitry Andric return false; 65*5f757f3fSDimitry Andric _LIBUNWIND_LOG("unsupported .eh_frame_hdr at %" PRIx64 66*5f757f3fSDimitry Andric ": need at least 4 bytes of data but only got %zd", 67*5f757f3fSDimitry Andric static_cast<uint64_t>(ehHdrStart), 68*5f757f3fSDimitry Andric static_cast<size_t>(ehHdrEnd - ehHdrStart)); 69*5f757f3fSDimitry Andric return false; 70*5f757f3fSDimitry Andric } 710b57cec5SDimitry Andric uint8_t version = addressSpace.get8(p++); 720b57cec5SDimitry Andric if (version != 1) { 7381ad6265SDimitry Andric _LIBUNWIND_LOG("unsupported .eh_frame_hdr version: %" PRIu8 " at %" PRIx64, 7481ad6265SDimitry Andric version, static_cast<uint64_t>(ehHdrStart)); 750b57cec5SDimitry Andric return false; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric uint8_t eh_frame_ptr_enc = addressSpace.get8(p++); 790b57cec5SDimitry Andric uint8_t fde_count_enc = addressSpace.get8(p++); 800b57cec5SDimitry Andric ehHdrInfo.table_enc = addressSpace.get8(p++); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric ehHdrInfo.eh_frame_ptr = 830b57cec5SDimitry Andric addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart); 840b57cec5SDimitry Andric ehHdrInfo.fde_count = 850b57cec5SDimitry Andric fde_count_enc == DW_EH_PE_omit 860b57cec5SDimitry Andric ? 0 870b57cec5SDimitry Andric : addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart); 880b57cec5SDimitry Andric ehHdrInfo.table = p; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric return true; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric template <typename A> 940b57cec5SDimitry Andric bool EHHeaderParser<A>::decodeTableEntry( 950b57cec5SDimitry Andric A &addressSpace, pint_t &tableEntry, pint_t ehHdrStart, pint_t ehHdrEnd, 960b57cec5SDimitry Andric uint8_t tableEnc, typename CFI_Parser<A>::FDE_Info *fdeInfo, 970b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info *cieInfo) { 980b57cec5SDimitry Andric // Have to decode the whole FDE for the PC range anyway, so just throw away 990b57cec5SDimitry Andric // the PC start. 1000b57cec5SDimitry Andric addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart); 1010b57cec5SDimitry Andric pint_t fde = 1020b57cec5SDimitry Andric addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart); 1030b57cec5SDimitry Andric const char *message = 1040b57cec5SDimitry Andric CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo); 1050b57cec5SDimitry Andric if (message != NULL) { 1060b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s", 1070b57cec5SDimitry Andric message); 1080b57cec5SDimitry Andric return false; 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric return true; 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric template <typename A> 1150b57cec5SDimitry Andric bool EHHeaderParser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart, 1160b57cec5SDimitry Andric uint32_t sectionLength, 1170b57cec5SDimitry Andric typename CFI_Parser<A>::FDE_Info *fdeInfo, 1180b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info *cieInfo) { 1190b57cec5SDimitry Andric pint_t ehHdrEnd = ehHdrStart + sectionLength; 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric EHHeaderParser<A>::EHHeaderInfo hdrInfo; 1220b57cec5SDimitry Andric if (!EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd, 1230b57cec5SDimitry Andric hdrInfo)) 1240b57cec5SDimitry Andric return false; 1250b57cec5SDimitry Andric 1265ffd83dbSDimitry Andric if (hdrInfo.fde_count == 0) return false; 1275ffd83dbSDimitry Andric 1280b57cec5SDimitry Andric size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc); 1290b57cec5SDimitry Andric pint_t tableEntry; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric size_t low = 0; 1320b57cec5SDimitry Andric for (size_t len = hdrInfo.fde_count; len > 1;) { 1330b57cec5SDimitry Andric size_t mid = low + (len / 2); 1340b57cec5SDimitry Andric tableEntry = hdrInfo.table + mid * tableEntrySize; 1350b57cec5SDimitry Andric pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd, 1360b57cec5SDimitry Andric hdrInfo.table_enc, ehHdrStart); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric if (start == pc) { 1390b57cec5SDimitry Andric low = mid; 1400b57cec5SDimitry Andric break; 1410b57cec5SDimitry Andric } else if (start < pc) { 1420b57cec5SDimitry Andric low = mid; 1430b57cec5SDimitry Andric len -= (len / 2); 1440b57cec5SDimitry Andric } else { 1450b57cec5SDimitry Andric len /= 2; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric tableEntry = hdrInfo.table + low * tableEntrySize; 1500b57cec5SDimitry Andric if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd, 1510b57cec5SDimitry Andric hdrInfo.table_enc, fdeInfo, cieInfo)) { 1520b57cec5SDimitry Andric if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd) 1530b57cec5SDimitry Andric return true; 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric return false; 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric template <typename A> 1600b57cec5SDimitry Andric size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) { 1610b57cec5SDimitry Andric switch (tableEnc & 0x0f) { 1620b57cec5SDimitry Andric case DW_EH_PE_sdata2: 1630b57cec5SDimitry Andric case DW_EH_PE_udata2: 1640b57cec5SDimitry Andric return 4; 1650b57cec5SDimitry Andric case DW_EH_PE_sdata4: 1660b57cec5SDimitry Andric case DW_EH_PE_udata4: 1670b57cec5SDimitry Andric return 8; 1680b57cec5SDimitry Andric case DW_EH_PE_sdata8: 1690b57cec5SDimitry Andric case DW_EH_PE_udata8: 1700b57cec5SDimitry Andric return 16; 1710b57cec5SDimitry Andric case DW_EH_PE_sleb128: 1720b57cec5SDimitry Andric case DW_EH_PE_uleb128: 1730b57cec5SDimitry Andric _LIBUNWIND_ABORT("Can't binary search on variable length encoded data."); 1740b57cec5SDimitry Andric case DW_EH_PE_omit: 1750b57cec5SDimitry Andric return 0; 1760b57cec5SDimitry Andric default: 1770b57cec5SDimitry Andric _LIBUNWIND_ABORT("Unknown DWARF encoding for search table."); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric #endif 184