#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>
using namespace llvm;
using namespace dwarf;
DwarfFormat DWARFDebugMacro::MacroHeader::getDwarfFormat() const {
return Flags & MACRO_OFFSET_SIZE ? DWARF64 : DWARF32;
}
uint8_t DWARFDebugMacro::MacroHeader::getOffsetByteSize() const {
return getDwarfOffsetByteSize(getDwarfFormat());
}
void DWARFDebugMacro::MacroHeader::dumpMacroHeader(raw_ostream &OS) const {
OS << format("macro header: version = 0x%04" PRIx16, Version)
<< format(", flags = 0x%02" PRIx8, Flags)
<< ", format = " << FormatString(getDwarfFormat());
if (Flags & MACRO_DEBUG_LINE_OFFSET)
OS << format(", debug_line_offset = 0x%0*" PRIx64, 2 * getOffsetByteSize(),
DebugLineOffset);
OS << "\n";
}
void DWARFDebugMacro::dump(raw_ostream &OS) const {
unsigned IndLevel = 0;
for (const auto &Macros : MacroLists) {
OS << format("0x%08" PRIx64 ":\n", Macros.Offset);
if (Macros.IsDebugMacro)
Macros.Header.dumpMacroHeader(OS);
for (const Entry &E : Macros.Macros) {
if (IndLevel > 0)
IndLevel -= (E.Type == DW_MACINFO_end_file);
for (unsigned I = 0; I < IndLevel; I++)
OS << " ";
IndLevel += (E.Type == DW_MACINFO_start_file);
if (Macros.IsDebugMacro)
WithColor(OS, HighlightColor::Macro).get()
<< (Macros.Header.Version < 5 ? GnuMacroString(E.Type)
: MacroString(E.Type));
else
WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type);
switch (E.Type) {
default:
break;
case DW_MACRO_define:
case DW_MACRO_undef:
case DW_MACRO_define_strp:
case DW_MACRO_undef_strp:
case DW_MACRO_define_strx:
case DW_MACRO_undef_strx:
OS << " - lineno: " << E.Line;
OS << " macro: " << E.MacroStr;
break;
case DW_MACRO_start_file:
OS << " - lineno: " << E.Line;
OS << " filenum: " << E.File;
break;
case DW_MACRO_import:
OS << format(" - import offset: 0x%0*" PRIx64,
2 * Macros.Header.getOffsetByteSize(), E.ImportOffset);
break;
case DW_MACRO_end_file:
break;
case DW_MACINFO_vendor_ext:
OS << " - constant: " << E.ExtConstant;
OS << " string: " << E.ExtStr;
break;
}
OS << "\n";
}
}
}
Error DWARFDebugMacro::parseImpl(
Optional<DWARFUnitVector::compile_unit_range> Units,
Optional<DataExtractor> StringExtractor, DWARFDataExtractor Data,
bool IsMacro) {
uint64_t Offset = 0;
MacroList *M = nullptr;
using MacroToUnitsMap = DenseMap<uint64_t, DWARFUnit *>;
MacroToUnitsMap MacroToUnits;
if (IsMacro && Data.isValidOffset(Offset)) {
for (const auto &U : *Units)
if (auto CUDIE = U->getUnitDIE())
if (auto MacroOffset = toSectionOffset(CUDIE.find(DW_AT_macros)))
MacroToUnits.try_emplace(*MacroOffset, U.get());
}
while (Data.isValidOffset(Offset)) {
if (!M) {
MacroLists.emplace_back();
M = &MacroLists.back();
M->Offset = Offset;
M->IsDebugMacro = IsMacro;
if (IsMacro) {
auto Err = M->Header.parseMacroHeader(Data, &Offset);
if (Err)
return Err;
}
}
M->Macros.emplace_back();
Entry &E = M->Macros.back();
E.Type = Data.getULEB128(&Offset);
if (E.Type == 0) {
M = nullptr;
continue;
}
switch (E.Type) {
default:
E.Type = DW_MACINFO_invalid;
return Error::success();
case DW_MACRO_define:
case DW_MACRO_undef:
E.Line = Data.getULEB128(&Offset);
E.MacroStr = Data.getCStr(&Offset);
break;
case DW_MACRO_define_strp:
case DW_MACRO_undef_strp: {
if (!IsMacro) {
E.Type = DW_MACINFO_invalid;
return Error::success();
}
uint64_t StrOffset = 0;
E.Line = Data.getULEB128(&Offset);
StrOffset =
Data.getRelocatedValue(M->Header.getOffsetByteSize(), &Offset);
assert(StringExtractor && "String Extractor not found");
E.MacroStr = StringExtractor->getCStr(&StrOffset);
break;
}
case DW_MACRO_define_strx:
case DW_MACRO_undef_strx: {
if (!IsMacro) {
E.Type = DW_MACINFO_invalid;
return Error::success();
}
E.Line = Data.getULEB128(&Offset);
auto MacroContributionOffset = MacroToUnits.find(M->Offset);
if (MacroContributionOffset == MacroToUnits.end())
return createStringError(errc::invalid_argument,
"Macro contribution of the unit not found");
Expected<uint64_t> StrOffset =
MacroContributionOffset->second->getStringOffsetSectionItem(
Data.getULEB128(&Offset));
if (!StrOffset)
return StrOffset.takeError();
E.MacroStr =
MacroContributionOffset->second->getStringExtractor().getCStr(
&*StrOffset);
break;
}
case DW_MACRO_start_file:
E.Line = Data.getULEB128(&Offset);
E.File = Data.getULEB128(&Offset);
break;
case DW_MACRO_end_file:
break;
case DW_MACRO_import:
E.ImportOffset =
Data.getRelocatedValue(M->Header.getOffsetByteSize(), &Offset);
break;
case DW_MACINFO_vendor_ext:
E.ExtConstant = Data.getULEB128(&Offset);
E.ExtStr = Data.getCStr(&Offset);
break;
}
}
return Error::success();
}
Error DWARFDebugMacro::MacroHeader::parseMacroHeader(DWARFDataExtractor Data,
uint64_t *Offset) {
Version = Data.getU16(Offset);
uint8_t FlagData = Data.getU8(Offset);
if (FlagData & MACRO_OPCODE_OPERANDS_TABLE)
return createStringError(errc::not_supported,
"opcode_operands_table is not supported");
Flags = FlagData;
if (Flags & MACRO_DEBUG_LINE_OFFSET)
DebugLineOffset = Data.getUnsigned(Offset, getOffsetByteSize());
return Error::success();
}