175112133SCarlos Alberto Enciso //===- FormatUtil.cpp ----------------------------------------- *- C++ --*-===//
275112133SCarlos Alberto Enciso //
375112133SCarlos Alberto Enciso // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
475112133SCarlos Alberto Enciso // See https://llvm.org/LICENSE.txt for license information.
575112133SCarlos Alberto Enciso // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
675112133SCarlos Alberto Enciso //
775112133SCarlos Alberto Enciso //===----------------------------------------------------------------------===//
875112133SCarlos Alberto Enciso
975112133SCarlos Alberto Enciso #include "llvm/DebugInfo/PDB/Native/FormatUtil.h"
1075112133SCarlos Alberto Enciso
1175112133SCarlos Alberto Enciso #include "llvm/ADT/STLExtras.h"
1275112133SCarlos Alberto Enciso #include "llvm/ADT/StringExtras.h"
1375112133SCarlos Alberto Enciso #include "llvm/BinaryFormat/COFF.h"
1475112133SCarlos Alberto Enciso #include "llvm/DebugInfo/CodeView/CodeView.h"
1575112133SCarlos Alberto Enciso #include "llvm/Support/FormatAdapters.h"
1675112133SCarlos Alberto Enciso #include "llvm/Support/FormatVariadic.h"
1775112133SCarlos Alberto Enciso
1875112133SCarlos Alberto Enciso using namespace llvm;
1975112133SCarlos Alberto Enciso using namespace llvm::codeview;
2075112133SCarlos Alberto Enciso using namespace llvm::pdb;
2175112133SCarlos Alberto Enciso
typesetItemList(ArrayRef<std::string> Opts,uint32_t IndentLevel,uint32_t GroupSize,StringRef Sep)2275112133SCarlos Alberto Enciso std::string llvm::pdb::typesetItemList(ArrayRef<std::string> Opts,
2375112133SCarlos Alberto Enciso uint32_t IndentLevel, uint32_t GroupSize,
2475112133SCarlos Alberto Enciso StringRef Sep) {
2575112133SCarlos Alberto Enciso std::string Result;
2675112133SCarlos Alberto Enciso while (!Opts.empty()) {
2775112133SCarlos Alberto Enciso ArrayRef<std::string> ThisGroup;
2875112133SCarlos Alberto Enciso ThisGroup = Opts.take_front(GroupSize);
2975112133SCarlos Alberto Enciso Opts = Opts.drop_front(ThisGroup.size());
3075112133SCarlos Alberto Enciso Result += join(ThisGroup, Sep);
3175112133SCarlos Alberto Enciso if (!Opts.empty()) {
3275112133SCarlos Alberto Enciso Result += Sep;
3375112133SCarlos Alberto Enciso Result += "\n";
3475112133SCarlos Alberto Enciso Result += std::string(formatv("{0}", fmt_repeat(' ', IndentLevel)));
3575112133SCarlos Alberto Enciso }
3675112133SCarlos Alberto Enciso }
3775112133SCarlos Alberto Enciso return Result;
3875112133SCarlos Alberto Enciso }
3975112133SCarlos Alberto Enciso
typesetStringList(uint32_t IndentLevel,ArrayRef<StringRef> Strings)4075112133SCarlos Alberto Enciso std::string llvm::pdb::typesetStringList(uint32_t IndentLevel,
4175112133SCarlos Alberto Enciso ArrayRef<StringRef> Strings) {
4275112133SCarlos Alberto Enciso std::string Result = "[";
4375112133SCarlos Alberto Enciso for (const auto &S : Strings) {
4475112133SCarlos Alberto Enciso Result += std::string(formatv("\n{0}{1}", fmt_repeat(' ', IndentLevel), S));
4575112133SCarlos Alberto Enciso }
4675112133SCarlos Alberto Enciso Result += "]";
4775112133SCarlos Alberto Enciso return Result;
4875112133SCarlos Alberto Enciso }
4975112133SCarlos Alberto Enciso
formatChunkKind(DebugSubsectionKind Kind,bool Friendly)5075112133SCarlos Alberto Enciso std::string llvm::pdb::formatChunkKind(DebugSubsectionKind Kind,
5175112133SCarlos Alberto Enciso bool Friendly) {
5275112133SCarlos Alberto Enciso if (Friendly) {
5375112133SCarlos Alberto Enciso switch (Kind) {
5475112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, None, "none");
5575112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, Symbols, "symbols");
5675112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, Lines, "lines");
5775112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, StringTable, "strings");
5875112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, FileChecksums, "checksums");
5975112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, FrameData, "frames");
6075112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, InlineeLines, "inlinee lines");
6175112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, CrossScopeImports, "xmi");
6275112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, CrossScopeExports, "xme");
6375112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, ILLines, "il lines");
6475112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap, "func md token map");
6575112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap, "type md token map");
6675112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,
6775112133SCarlos Alberto Enciso "merged assembly input");
6875112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA, "coff symbol rva");
69*1f956109STobias Hieta RETURN_CASE(DebugSubsectionKind, XfgHashType, "xfg hash type");
70*1f956109STobias Hieta RETURN_CASE(DebugSubsectionKind, XfgHashVirtual, "xfg hash virtual");
7175112133SCarlos Alberto Enciso }
7275112133SCarlos Alberto Enciso } else {
7375112133SCarlos Alberto Enciso switch (Kind) {
7475112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, None, "none");
7575112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, Symbols, "DEBUG_S_SYMBOLS");
7675112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, Lines, "DEBUG_S_LINES");
7775112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, StringTable, "DEBUG_S_STRINGTABLE");
7875112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, FileChecksums, "DEBUG_S_FILECHKSMS");
7975112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, FrameData, "DEBUG_S_FRAMEDATA");
8075112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, InlineeLines, "DEBUG_S_INLINEELINES");
8175112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, CrossScopeImports,
8275112133SCarlos Alberto Enciso "DEBUG_S_CROSSSCOPEIMPORTS");
8375112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, CrossScopeExports,
8475112133SCarlos Alberto Enciso "DEBUG_S_CROSSSCOPEEXPORTS");
8575112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, ILLines, "DEBUG_S_IL_LINES");
8675112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap,
8775112133SCarlos Alberto Enciso "DEBUG_S_FUNC_MDTOKEN_MAP");
8875112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap,
8975112133SCarlos Alberto Enciso "DEBUG_S_TYPE_MDTOKEN_MAP");
9075112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,
9175112133SCarlos Alberto Enciso "DEBUG_S_MERGED_ASSEMBLYINPUT");
9275112133SCarlos Alberto Enciso RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA,
9375112133SCarlos Alberto Enciso "DEBUG_S_COFF_SYMBOL_RVA");
94*1f956109STobias Hieta RETURN_CASE(DebugSubsectionKind, XfgHashType,
95*1f956109STobias Hieta "DEBUG_S_XFGHASH_TYPE");
96*1f956109STobias Hieta RETURN_CASE(DebugSubsectionKind, XfgHashVirtual,
97*1f956109STobias Hieta "DEBUG_S_XFGHASH_VIRTUAL");
98*1f956109STobias Hieta
9975112133SCarlos Alberto Enciso }
10075112133SCarlos Alberto Enciso }
10175112133SCarlos Alberto Enciso return formatUnknownEnum(Kind);
10275112133SCarlos Alberto Enciso }
10375112133SCarlos Alberto Enciso
formatSymbolKind(SymbolKind K)10475112133SCarlos Alberto Enciso std::string llvm::pdb::formatSymbolKind(SymbolKind K) {
10575112133SCarlos Alberto Enciso switch (uint32_t(K)) {
10675112133SCarlos Alberto Enciso #define SYMBOL_RECORD(EnumName, value, name) \
10775112133SCarlos Alberto Enciso case EnumName: \
10875112133SCarlos Alberto Enciso return #EnumName;
10975112133SCarlos Alberto Enciso #define CV_SYMBOL(EnumName, value) SYMBOL_RECORD(EnumName, value, EnumName)
11075112133SCarlos Alberto Enciso #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
11175112133SCarlos Alberto Enciso }
11275112133SCarlos Alberto Enciso return formatUnknownEnum(K);
11375112133SCarlos Alberto Enciso }
11475112133SCarlos Alberto Enciso
formatTypeLeafKind(TypeLeafKind K)11575112133SCarlos Alberto Enciso std::string llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {
11675112133SCarlos Alberto Enciso switch (K) {
11775112133SCarlos Alberto Enciso #define TYPE_RECORD(EnumName, value, name) \
11875112133SCarlos Alberto Enciso case EnumName: \
11975112133SCarlos Alberto Enciso return #EnumName;
12075112133SCarlos Alberto Enciso #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
12175112133SCarlos Alberto Enciso default:
12275112133SCarlos Alberto Enciso return formatv("UNKNOWN RECORD ({0:X})",
12375112133SCarlos Alberto Enciso static_cast<std::underlying_type_t<TypeLeafKind>>(K))
12475112133SCarlos Alberto Enciso .str();
12575112133SCarlos Alberto Enciso }
12675112133SCarlos Alberto Enciso }
12775112133SCarlos Alberto Enciso
formatSegmentOffset(uint16_t Segment,uint32_t Offset)12875112133SCarlos Alberto Enciso std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) {
12975112133SCarlos Alberto Enciso return std::string(formatv("{0:4}:{1:4}", Segment, Offset));
13075112133SCarlos Alberto Enciso }
13175112133SCarlos Alberto Enciso
13275112133SCarlos Alberto Enciso #define PUSH_CHARACTERISTIC_FLAG(Enum, TheOpt, Value, Style, Descriptive) \
13375112133SCarlos Alberto Enciso PUSH_FLAG(Enum, TheOpt, Value, \
13475112133SCarlos Alberto Enciso ((Style == CharacteristicStyle::HeaderDefinition) ? #TheOpt \
13575112133SCarlos Alberto Enciso : Descriptive))
13675112133SCarlos Alberto Enciso
13775112133SCarlos Alberto Enciso #define PUSH_MASKED_CHARACTERISTIC_FLAG(Enum, Mask, TheOpt, Value, Style, \
13875112133SCarlos Alberto Enciso Descriptive) \
13975112133SCarlos Alberto Enciso PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, \
14075112133SCarlos Alberto Enciso ((Style == CharacteristicStyle::HeaderDefinition) \
14175112133SCarlos Alberto Enciso ? #TheOpt \
14275112133SCarlos Alberto Enciso : Descriptive))
14375112133SCarlos Alberto Enciso
formatSectionCharacteristics(uint32_t IndentLevel,uint32_t C,uint32_t FlagsPerLine,StringRef Separator,CharacteristicStyle Style)14475112133SCarlos Alberto Enciso std::string llvm::pdb::formatSectionCharacteristics(uint32_t IndentLevel,
14575112133SCarlos Alberto Enciso uint32_t C,
14675112133SCarlos Alberto Enciso uint32_t FlagsPerLine,
14775112133SCarlos Alberto Enciso StringRef Separator,
14875112133SCarlos Alberto Enciso CharacteristicStyle Style) {
14975112133SCarlos Alberto Enciso using SC = COFF::SectionCharacteristics;
15075112133SCarlos Alberto Enciso std::vector<std::string> Opts;
15175112133SCarlos Alberto Enciso if (C == COFF::SC_Invalid)
15275112133SCarlos Alberto Enciso return "invalid";
15375112133SCarlos Alberto Enciso if (C == 0)
15475112133SCarlos Alberto Enciso return "none";
15575112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NOLOAD, C, Style, "noload");
15675112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NO_PAD, C, Style, "no padding");
15775112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_CODE, C, Style, "code");
15875112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_INITIALIZED_DATA, C, Style,
15975112133SCarlos Alberto Enciso "initialized data");
16075112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_UNINITIALIZED_DATA, C, Style,
16175112133SCarlos Alberto Enciso "uninitialized data");
16275112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_OTHER, C, Style, "other");
16375112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_INFO, C, Style, "info");
16475112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_REMOVE, C, Style, "remove");
16575112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_COMDAT, C, Style, "comdat");
16675112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_GPREL, C, Style, "gp rel");
16775112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PURGEABLE, C, Style, "purgeable");
16875112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_16BIT, C, Style, "16-bit");
16975112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_LOCKED, C, Style, "locked");
17075112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PRELOAD, C, Style, "preload");
17175112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1BYTES, C,
17275112133SCarlos Alberto Enciso Style, "1 byte align");
17375112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2BYTES, C,
17475112133SCarlos Alberto Enciso Style, "2 byte align");
17575112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4BYTES, C,
17675112133SCarlos Alberto Enciso Style, "4 byte align");
17775112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8BYTES, C,
17875112133SCarlos Alberto Enciso Style, "8 byte align");
17975112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_16BYTES, C,
18075112133SCarlos Alberto Enciso Style, "16 byte align");
18175112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_32BYTES, C,
18275112133SCarlos Alberto Enciso Style, "32 byte align");
18375112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_64BYTES, C,
18475112133SCarlos Alberto Enciso Style, "64 byte align");
18575112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_128BYTES, C,
18675112133SCarlos Alberto Enciso Style, "128 byte align");
18775112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_256BYTES, C,
18875112133SCarlos Alberto Enciso Style, "256 byte align");
18975112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_512BYTES, C,
19075112133SCarlos Alberto Enciso Style, "512 byte align");
19175112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1024BYTES, C,
19275112133SCarlos Alberto Enciso Style, "1024 byte align");
19375112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2048BYTES, C,
19475112133SCarlos Alberto Enciso Style, "2048 byte align");
19575112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4096BYTES, C,
19675112133SCarlos Alberto Enciso Style, "4096 byte align");
19775112133SCarlos Alberto Enciso PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8192BYTES, C,
19875112133SCarlos Alberto Enciso Style, "8192 byte align");
19975112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_NRELOC_OVFL, C, Style,
20075112133SCarlos Alberto Enciso "noreloc overflow");
20175112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_DISCARDABLE, C, Style,
20275112133SCarlos Alberto Enciso "discardable");
20375112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_CACHED, C, Style,
20475112133SCarlos Alberto Enciso "not cached");
20575112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_PAGED, C, Style, "not paged");
20675112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_SHARED, C, Style, "shared");
20775112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_EXECUTE, C, Style,
20875112133SCarlos Alberto Enciso "execute permissions");
20975112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_READ, C, Style,
21075112133SCarlos Alberto Enciso "read permissions");
21175112133SCarlos Alberto Enciso PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_WRITE, C, Style,
21275112133SCarlos Alberto Enciso "write permissions");
21375112133SCarlos Alberto Enciso return typesetItemList(Opts, IndentLevel, FlagsPerLine, Separator);
21475112133SCarlos Alberto Enciso }
215