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; 580b57cec5SDimitry Andric uint8_t version = addressSpace.get8(p++); 590b57cec5SDimitry Andric if (version != 1) { 60*81ad6265SDimitry Andric _LIBUNWIND_LOG("unsupported .eh_frame_hdr version: %" PRIu8 " at %" PRIx64, 61*81ad6265SDimitry Andric version, static_cast<uint64_t>(ehHdrStart)); 620b57cec5SDimitry Andric return false; 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric uint8_t eh_frame_ptr_enc = addressSpace.get8(p++); 660b57cec5SDimitry Andric uint8_t fde_count_enc = addressSpace.get8(p++); 670b57cec5SDimitry Andric ehHdrInfo.table_enc = addressSpace.get8(p++); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric ehHdrInfo.eh_frame_ptr = 700b57cec5SDimitry Andric addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart); 710b57cec5SDimitry Andric ehHdrInfo.fde_count = 720b57cec5SDimitry Andric fde_count_enc == DW_EH_PE_omit 730b57cec5SDimitry Andric ? 0 740b57cec5SDimitry Andric : addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart); 750b57cec5SDimitry Andric ehHdrInfo.table = p; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric return true; 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric template <typename A> 810b57cec5SDimitry Andric bool EHHeaderParser<A>::decodeTableEntry( 820b57cec5SDimitry Andric A &addressSpace, pint_t &tableEntry, pint_t ehHdrStart, pint_t ehHdrEnd, 830b57cec5SDimitry Andric uint8_t tableEnc, typename CFI_Parser<A>::FDE_Info *fdeInfo, 840b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info *cieInfo) { 850b57cec5SDimitry Andric // Have to decode the whole FDE for the PC range anyway, so just throw away 860b57cec5SDimitry Andric // the PC start. 870b57cec5SDimitry Andric addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart); 880b57cec5SDimitry Andric pint_t fde = 890b57cec5SDimitry Andric addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart); 900b57cec5SDimitry Andric const char *message = 910b57cec5SDimitry Andric CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo); 920b57cec5SDimitry Andric if (message != NULL) { 930b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s", 940b57cec5SDimitry Andric message); 950b57cec5SDimitry Andric return false; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric return true; 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric template <typename A> 1020b57cec5SDimitry Andric bool EHHeaderParser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart, 1030b57cec5SDimitry Andric uint32_t sectionLength, 1040b57cec5SDimitry Andric typename CFI_Parser<A>::FDE_Info *fdeInfo, 1050b57cec5SDimitry Andric typename CFI_Parser<A>::CIE_Info *cieInfo) { 1060b57cec5SDimitry Andric pint_t ehHdrEnd = ehHdrStart + sectionLength; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric EHHeaderParser<A>::EHHeaderInfo hdrInfo; 1090b57cec5SDimitry Andric if (!EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd, 1100b57cec5SDimitry Andric hdrInfo)) 1110b57cec5SDimitry Andric return false; 1120b57cec5SDimitry Andric 1135ffd83dbSDimitry Andric if (hdrInfo.fde_count == 0) return false; 1145ffd83dbSDimitry Andric 1150b57cec5SDimitry Andric size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc); 1160b57cec5SDimitry Andric pint_t tableEntry; 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric size_t low = 0; 1190b57cec5SDimitry Andric for (size_t len = hdrInfo.fde_count; len > 1;) { 1200b57cec5SDimitry Andric size_t mid = low + (len / 2); 1210b57cec5SDimitry Andric tableEntry = hdrInfo.table + mid * tableEntrySize; 1220b57cec5SDimitry Andric pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd, 1230b57cec5SDimitry Andric hdrInfo.table_enc, ehHdrStart); 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric if (start == pc) { 1260b57cec5SDimitry Andric low = mid; 1270b57cec5SDimitry Andric break; 1280b57cec5SDimitry Andric } else if (start < pc) { 1290b57cec5SDimitry Andric low = mid; 1300b57cec5SDimitry Andric len -= (len / 2); 1310b57cec5SDimitry Andric } else { 1320b57cec5SDimitry Andric len /= 2; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric tableEntry = hdrInfo.table + low * tableEntrySize; 1370b57cec5SDimitry Andric if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd, 1380b57cec5SDimitry Andric hdrInfo.table_enc, fdeInfo, cieInfo)) { 1390b57cec5SDimitry Andric if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd) 1400b57cec5SDimitry Andric return true; 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric return false; 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric template <typename A> 1470b57cec5SDimitry Andric size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) { 1480b57cec5SDimitry Andric switch (tableEnc & 0x0f) { 1490b57cec5SDimitry Andric case DW_EH_PE_sdata2: 1500b57cec5SDimitry Andric case DW_EH_PE_udata2: 1510b57cec5SDimitry Andric return 4; 1520b57cec5SDimitry Andric case DW_EH_PE_sdata4: 1530b57cec5SDimitry Andric case DW_EH_PE_udata4: 1540b57cec5SDimitry Andric return 8; 1550b57cec5SDimitry Andric case DW_EH_PE_sdata8: 1560b57cec5SDimitry Andric case DW_EH_PE_udata8: 1570b57cec5SDimitry Andric return 16; 1580b57cec5SDimitry Andric case DW_EH_PE_sleb128: 1590b57cec5SDimitry Andric case DW_EH_PE_uleb128: 1600b57cec5SDimitry Andric _LIBUNWIND_ABORT("Can't binary search on variable length encoded data."); 1610b57cec5SDimitry Andric case DW_EH_PE_omit: 1620b57cec5SDimitry Andric return 0; 1630b57cec5SDimitry Andric default: 1640b57cec5SDimitry Andric _LIBUNWIND_ABORT("Unknown DWARF encoding for search table."); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric #endif 171