Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp
35269 views
//===- DWARFDebugMacro.cpp ------------------------------------------------===//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//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"9#include "llvm/ADT/DenseMap.h"10#include "llvm/BinaryFormat/Dwarf.h"11#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"12#include "llvm/DebugInfo/DWARF/DWARFDie.h"13#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"14#include "llvm/Support/Errc.h"15#include "llvm/Support/WithColor.h"16#include "llvm/Support/raw_ostream.h"17#include <cstdint>1819using namespace llvm;20using namespace dwarf;2122DwarfFormat DWARFDebugMacro::MacroHeader::getDwarfFormat() const {23return Flags & MACRO_OFFSET_SIZE ? DWARF64 : DWARF32;24}2526uint8_t DWARFDebugMacro::MacroHeader::getOffsetByteSize() const {27return getDwarfOffsetByteSize(getDwarfFormat());28}2930void DWARFDebugMacro::MacroHeader::dumpMacroHeader(raw_ostream &OS) const {31// FIXME: Add support for dumping opcode_operands_table32OS << format("macro header: version = 0x%04" PRIx16, Version)33<< format(", flags = 0x%02" PRIx8, Flags)34<< ", format = " << FormatString(getDwarfFormat());35if (Flags & MACRO_DEBUG_LINE_OFFSET)36OS << format(", debug_line_offset = 0x%0*" PRIx64, 2 * getOffsetByteSize(),37DebugLineOffset);38OS << "\n";39}4041void DWARFDebugMacro::dump(raw_ostream &OS) const {42unsigned IndLevel = 0;43for (const auto &Macros : MacroLists) {44OS << format("0x%08" PRIx64 ":\n", Macros.Offset);45if (Macros.IsDebugMacro)46Macros.Header.dumpMacroHeader(OS);47for (const Entry &E : Macros.Macros) {48// There should not be DW_MACINFO_end_file when IndLevel is Zero. However,49// this check handles the case of corrupted ".debug_macinfo" section.50if (IndLevel > 0)51IndLevel -= (E.Type == DW_MACINFO_end_file);52// Print indentation.53for (unsigned I = 0; I < IndLevel; I++)54OS << " ";55IndLevel += (E.Type == DW_MACINFO_start_file);56// Based on which version we are handling choose appropriate macro forms.57if (Macros.IsDebugMacro)58WithColor(OS, HighlightColor::Macro).get()59<< (Macros.Header.Version < 5 ? GnuMacroString(E.Type)60: MacroString(E.Type));61else62WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type);63switch (E.Type) {64default:65// Got a corrupted ".debug_macinfo/.debug_macro" section (invalid66// macinfo type).67break;68// debug_macro and debug_macinfo share some common encodings.69// DW_MACRO_define == DW_MACINFO_define70// DW_MACRO_undef == DW_MACINFO_undef71// DW_MACRO_start_file == DW_MACINFO_start_file72// DW_MACRO_end_file == DW_MACINFO_end_file73// For readability/uniformity we are using DW_MACRO_*.74//75// The GNU .debug_macro extension's entries have the same encoding76// as DWARF 5's DW_MACRO_* entries, so we only use the latter here.77case DW_MACRO_define:78case DW_MACRO_undef:79case DW_MACRO_define_strp:80case DW_MACRO_undef_strp:81case DW_MACRO_define_strx:82case DW_MACRO_undef_strx:83OS << " - lineno: " << E.Line;84OS << " macro: " << E.MacroStr;85break;86case DW_MACRO_start_file:87OS << " - lineno: " << E.Line;88OS << " filenum: " << E.File;89break;90case DW_MACRO_import:91OS << format(" - import offset: 0x%0*" PRIx64,922 * Macros.Header.getOffsetByteSize(), E.ImportOffset);93break;94case DW_MACRO_end_file:95break;96case DW_MACINFO_vendor_ext:97OS << " - constant: " << E.ExtConstant;98OS << " string: " << E.ExtStr;99break;100}101OS << "\n";102}103}104}105106Error DWARFDebugMacro::parseImpl(107std::optional<DWARFUnitVector::compile_unit_range> Units,108std::optional<DataExtractor> StringExtractor, DWARFDataExtractor Data,109bool IsMacro) {110uint64_t Offset = 0;111MacroList *M = nullptr;112using MacroToUnitsMap = DenseMap<uint64_t, DWARFUnit *>;113MacroToUnitsMap MacroToUnits;114if (IsMacro && Data.isValidOffset(Offset)) {115// Keep a mapping from Macro contribution to CUs, this will116// be needed while retrieving macro from DW_MACRO_define_strx form.117for (const auto &U : *Units)118if (auto CUDIE = U->getUnitDIE())119// Skip units which does not contibutes to macro section.120if (auto MacroOffset = toSectionOffset(CUDIE.find(DW_AT_macros)))121MacroToUnits.try_emplace(*MacroOffset, U.get());122}123while (Data.isValidOffset(Offset)) {124if (!M) {125MacroLists.emplace_back();126M = &MacroLists.back();127M->Offset = Offset;128M->IsDebugMacro = IsMacro;129if (IsMacro) {130auto Err = M->Header.parseMacroHeader(Data, &Offset);131if (Err)132return Err;133}134}135// A macro list entry consists of:136M->Macros.emplace_back();137Entry &E = M->Macros.back();138// 1. Macinfo type139E.Type = Data.getULEB128(&Offset);140141if (E.Type == 0) {142// Reached end of a ".debug_macinfo/debug_macro" section contribution.143M = nullptr;144continue;145}146147switch (E.Type) {148default:149// Got a corrupted ".debug_macinfo" section (invalid macinfo type).150// Push the corrupted entry to the list and halt parsing.151E.Type = DW_MACINFO_invalid;152return Error::success();153// debug_macro and debug_macinfo share some common encodings.154// DW_MACRO_define == DW_MACINFO_define155// DW_MACRO_undef == DW_MACINFO_undef156// DW_MACRO_start_file == DW_MACINFO_start_file157// DW_MACRO_end_file == DW_MACINFO_end_file158// For readibility/uniformity we are using DW_MACRO_*.159case DW_MACRO_define:160case DW_MACRO_undef:161// 2. Source line162E.Line = Data.getULEB128(&Offset);163// 3. Macro string164E.MacroStr = Data.getCStr(&Offset);165break;166case DW_MACRO_define_strp:167case DW_MACRO_undef_strp: {168if (!IsMacro) {169// DW_MACRO_define_strp is a new form introduced in DWARFv5, it is170// not supported in debug_macinfo[.dwo] sections. Assume it as an171// invalid entry, push it and halt parsing.172E.Type = DW_MACINFO_invalid;173return Error::success();174}175uint64_t StrOffset = 0;176// 2. Source line177E.Line = Data.getULEB128(&Offset);178// 3. Macro string179StrOffset =180Data.getRelocatedValue(M->Header.getOffsetByteSize(), &Offset);181assert(StringExtractor && "String Extractor not found");182E.MacroStr = StringExtractor->getCStr(&StrOffset);183break;184}185case DW_MACRO_define_strx:186case DW_MACRO_undef_strx: {187if (!IsMacro) {188// DW_MACRO_define_strx is a new form introduced in DWARFv5, it is189// not supported in debug_macinfo[.dwo] sections. Assume it as an190// invalid entry, push it and halt parsing.191E.Type = DW_MACINFO_invalid;192return Error::success();193}194E.Line = Data.getULEB128(&Offset);195auto MacroContributionOffset = MacroToUnits.find(M->Offset);196if (MacroContributionOffset == MacroToUnits.end())197return createStringError(errc::invalid_argument,198"Macro contribution of the unit not found");199Expected<uint64_t> StrOffset =200MacroContributionOffset->second->getStringOffsetSectionItem(201Data.getULEB128(&Offset));202if (!StrOffset)203return StrOffset.takeError();204E.MacroStr =205MacroContributionOffset->second->getStringExtractor().getCStr(206&*StrOffset);207break;208}209case DW_MACRO_start_file:210// 2. Source line211E.Line = Data.getULEB128(&Offset);212// 3. Source file id213E.File = Data.getULEB128(&Offset);214break;215case DW_MACRO_end_file:216break;217case DW_MACRO_import:218E.ImportOffset =219Data.getRelocatedValue(M->Header.getOffsetByteSize(), &Offset);220break;221case DW_MACINFO_vendor_ext:222// 2. Vendor extension constant223E.ExtConstant = Data.getULEB128(&Offset);224// 3. Vendor extension string225E.ExtStr = Data.getCStr(&Offset);226break;227}228}229return Error::success();230}231232Error DWARFDebugMacro::MacroHeader::parseMacroHeader(DWARFDataExtractor Data,233uint64_t *Offset) {234Version = Data.getU16(Offset);235uint8_t FlagData = Data.getU8(Offset);236237// FIXME: Add support for parsing opcode_operands_table238if (FlagData & MACRO_OPCODE_OPERANDS_TABLE)239return createStringError(errc::not_supported,240"opcode_operands_table is not supported");241Flags = FlagData;242if (Flags & MACRO_DEBUG_LINE_OFFSET)243DebugLineOffset = Data.getUnsigned(Offset, getOffsetByteSize());244return Error::success();245}246247248