Path: blob/main/contrib/llvm-project/libunwind/src/EHHeaderParser.hpp
35148 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//7// Parses ELF .eh_frame_hdr sections.8//9//===----------------------------------------------------------------------===//1011#ifndef __EHHEADERPARSER_HPP__12#define __EHHEADERPARSER_HPP__1314#include "libunwind.h"1516#include "DwarfParser.hpp"1718namespace libunwind {1920/// \brief EHHeaderParser does basic parsing of an ELF .eh_frame_hdr section.21///22/// See DWARF spec for details:23/// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html24///25template <typename A> class EHHeaderParser {26public:27typedef typename A::pint_t pint_t;2829/// Information encoded in the EH frame header.30struct EHHeaderInfo {31pint_t eh_frame_ptr;32size_t fde_count;33pint_t table;34uint8_t table_enc;35};3637static bool decodeEHHdr(A &addressSpace, pint_t ehHdrStart, pint_t ehHdrEnd,38EHHeaderInfo &ehHdrInfo);39static bool findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart,40uint32_t sectionLength,41typename CFI_Parser<A>::FDE_Info *fdeInfo,42typename CFI_Parser<A>::CIE_Info *cieInfo);4344private:45static bool decodeTableEntry(A &addressSpace, pint_t &tableEntry,46pint_t ehHdrStart, pint_t ehHdrEnd,47uint8_t tableEnc,48typename CFI_Parser<A>::FDE_Info *fdeInfo,49typename CFI_Parser<A>::CIE_Info *cieInfo);50static size_t getTableEntrySize(uint8_t tableEnc);51};5253template <typename A>54bool EHHeaderParser<A>::decodeEHHdr(A &addressSpace, pint_t ehHdrStart,55pint_t ehHdrEnd, EHHeaderInfo &ehHdrInfo) {56pint_t p = ehHdrStart;5758// Ensure that we don't read data beyond the end of .eh_frame_hdr59if (ehHdrEnd - ehHdrStart < 4) {60// Don't print a message for an empty .eh_frame_hdr (this can happen if61// the linker script defines symbols for it even in the empty case).62if (ehHdrEnd == ehHdrStart)63return false;64_LIBUNWIND_LOG("unsupported .eh_frame_hdr at %" PRIx6465": need at least 4 bytes of data but only got %zd",66static_cast<uint64_t>(ehHdrStart),67static_cast<size_t>(ehHdrEnd - ehHdrStart));68return false;69}70uint8_t version = addressSpace.get8(p++);71if (version != 1) {72_LIBUNWIND_LOG("unsupported .eh_frame_hdr version: %" PRIu8 " at %" PRIx64,73version, static_cast<uint64_t>(ehHdrStart));74return false;75}7677uint8_t eh_frame_ptr_enc = addressSpace.get8(p++);78uint8_t fde_count_enc = addressSpace.get8(p++);79ehHdrInfo.table_enc = addressSpace.get8(p++);8081ehHdrInfo.eh_frame_ptr =82addressSpace.getEncodedP(p, ehHdrEnd, eh_frame_ptr_enc, ehHdrStart);83ehHdrInfo.fde_count =84fde_count_enc == DW_EH_PE_omit85? 086: addressSpace.getEncodedP(p, ehHdrEnd, fde_count_enc, ehHdrStart);87ehHdrInfo.table = p;8889return true;90}9192template <typename A>93bool EHHeaderParser<A>::decodeTableEntry(94A &addressSpace, pint_t &tableEntry, pint_t ehHdrStart, pint_t ehHdrEnd,95uint8_t tableEnc, typename CFI_Parser<A>::FDE_Info *fdeInfo,96typename CFI_Parser<A>::CIE_Info *cieInfo) {97// Have to decode the whole FDE for the PC range anyway, so just throw away98// the PC start.99addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);100pint_t fde =101addressSpace.getEncodedP(tableEntry, ehHdrEnd, tableEnc, ehHdrStart);102const char *message =103CFI_Parser<A>::decodeFDE(addressSpace, fde, fdeInfo, cieInfo);104if (message != NULL) {105_LIBUNWIND_DEBUG_LOG("EHHeaderParser::decodeTableEntry: bad fde: %s",106message);107return false;108}109110return true;111}112113template <typename A>114bool EHHeaderParser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehHdrStart,115uint32_t sectionLength,116typename CFI_Parser<A>::FDE_Info *fdeInfo,117typename CFI_Parser<A>::CIE_Info *cieInfo) {118pint_t ehHdrEnd = ehHdrStart + sectionLength;119120EHHeaderParser<A>::EHHeaderInfo hdrInfo;121if (!EHHeaderParser<A>::decodeEHHdr(addressSpace, ehHdrStart, ehHdrEnd,122hdrInfo))123return false;124125if (hdrInfo.fde_count == 0) return false;126127size_t tableEntrySize = getTableEntrySize(hdrInfo.table_enc);128pint_t tableEntry;129130size_t low = 0;131for (size_t len = hdrInfo.fde_count; len > 1;) {132size_t mid = low + (len / 2);133tableEntry = hdrInfo.table + mid * tableEntrySize;134pint_t start = addressSpace.getEncodedP(tableEntry, ehHdrEnd,135hdrInfo.table_enc, ehHdrStart);136137if (start == pc) {138low = mid;139break;140} else if (start < pc) {141low = mid;142len -= (len / 2);143} else {144len /= 2;145}146}147148tableEntry = hdrInfo.table + low * tableEntrySize;149if (decodeTableEntry(addressSpace, tableEntry, ehHdrStart, ehHdrEnd,150hdrInfo.table_enc, fdeInfo, cieInfo)) {151if (pc >= fdeInfo->pcStart && pc < fdeInfo->pcEnd)152return true;153}154155return false;156}157158template <typename A>159size_t EHHeaderParser<A>::getTableEntrySize(uint8_t tableEnc) {160switch (tableEnc & 0x0f) {161case DW_EH_PE_sdata2:162case DW_EH_PE_udata2:163return 4;164case DW_EH_PE_sdata4:165case DW_EH_PE_udata4:166return 8;167case DW_EH_PE_sdata8:168case DW_EH_PE_udata8:169return 16;170case DW_EH_PE_sleb128:171case DW_EH_PE_uleb128:172_LIBUNWIND_ABORT("Can't binary search on variable length encoded data.");173case DW_EH_PE_omit:174return 0;175default:176_LIBUNWIND_ABORT("Unknown DWARF encoding for search table.");177}178}179180}181182#endif183184185