213 lines · cpp
1//===- FormatUtil.cpp ----------------------------------------- *- C++ --*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/DebugInfo/PDB/Native/FormatUtil.h"10 11#include "llvm/ADT/STLForwardCompat.h"12#include "llvm/ADT/StringExtras.h"13#include "llvm/BinaryFormat/COFF.h"14#include "llvm/DebugInfo/CodeView/CodeView.h"15#include "llvm/Support/FormatAdapters.h"16#include "llvm/Support/FormatVariadic.h"17 18using namespace llvm;19using namespace llvm::codeview;20using namespace llvm::pdb;21 22std::string llvm::pdb::typesetItemList(ArrayRef<std::string> Opts,23 uint32_t IndentLevel, uint32_t GroupSize,24 StringRef Sep) {25 std::string Result;26 while (!Opts.empty()) {27 ArrayRef<std::string> ThisGroup;28 ThisGroup = Opts.take_front(GroupSize);29 Opts = Opts.drop_front(ThisGroup.size());30 Result += join(ThisGroup, Sep);31 if (!Opts.empty()) {32 Result += Sep;33 Result += "\n";34 Result += std::string(formatv("{0}", fmt_repeat(' ', IndentLevel)));35 }36 }37 return Result;38}39 40std::string llvm::pdb::typesetStringList(uint32_t IndentLevel,41 ArrayRef<StringRef> Strings) {42 std::string Result = "[";43 for (const auto &S : Strings) {44 Result += std::string(formatv("\n{0}{1}", fmt_repeat(' ', IndentLevel), S));45 }46 Result += "]";47 return Result;48}49 50std::string llvm::pdb::formatChunkKind(DebugSubsectionKind Kind,51 bool Friendly) {52 if (Friendly) {53 switch (Kind) {54 RETURN_CASE(DebugSubsectionKind, None, "none");55 RETURN_CASE(DebugSubsectionKind, Symbols, "symbols");56 RETURN_CASE(DebugSubsectionKind, Lines, "lines");57 RETURN_CASE(DebugSubsectionKind, StringTable, "strings");58 RETURN_CASE(DebugSubsectionKind, FileChecksums, "checksums");59 RETURN_CASE(DebugSubsectionKind, FrameData, "frames");60 RETURN_CASE(DebugSubsectionKind, InlineeLines, "inlinee lines");61 RETURN_CASE(DebugSubsectionKind, CrossScopeImports, "xmi");62 RETURN_CASE(DebugSubsectionKind, CrossScopeExports, "xme");63 RETURN_CASE(DebugSubsectionKind, ILLines, "il lines");64 RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap, "func md token map");65 RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap, "type md token map");66 RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,67 "merged assembly input");68 RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA, "coff symbol rva");69 RETURN_CASE(DebugSubsectionKind, XfgHashType, "xfg hash type");70 RETURN_CASE(DebugSubsectionKind, XfgHashVirtual, "xfg hash virtual");71 }72 } else {73 switch (Kind) {74 RETURN_CASE(DebugSubsectionKind, None, "none");75 RETURN_CASE(DebugSubsectionKind, Symbols, "DEBUG_S_SYMBOLS");76 RETURN_CASE(DebugSubsectionKind, Lines, "DEBUG_S_LINES");77 RETURN_CASE(DebugSubsectionKind, StringTable, "DEBUG_S_STRINGTABLE");78 RETURN_CASE(DebugSubsectionKind, FileChecksums, "DEBUG_S_FILECHKSMS");79 RETURN_CASE(DebugSubsectionKind, FrameData, "DEBUG_S_FRAMEDATA");80 RETURN_CASE(DebugSubsectionKind, InlineeLines, "DEBUG_S_INLINEELINES");81 RETURN_CASE(DebugSubsectionKind, CrossScopeImports,82 "DEBUG_S_CROSSSCOPEIMPORTS");83 RETURN_CASE(DebugSubsectionKind, CrossScopeExports,84 "DEBUG_S_CROSSSCOPEEXPORTS");85 RETURN_CASE(DebugSubsectionKind, ILLines, "DEBUG_S_IL_LINES");86 RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap,87 "DEBUG_S_FUNC_MDTOKEN_MAP");88 RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap,89 "DEBUG_S_TYPE_MDTOKEN_MAP");90 RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,91 "DEBUG_S_MERGED_ASSEMBLYINPUT");92 RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA,93 "DEBUG_S_COFF_SYMBOL_RVA");94 RETURN_CASE(DebugSubsectionKind, XfgHashType,95 "DEBUG_S_XFGHASH_TYPE");96 RETURN_CASE(DebugSubsectionKind, XfgHashVirtual,97 "DEBUG_S_XFGHASH_VIRTUAL");98 99 }100 }101 return formatUnknownEnum(Kind);102}103 104std::string llvm::pdb::formatSymbolKind(SymbolKind K) {105 switch (uint32_t(K)) {106#define SYMBOL_RECORD(EnumName, value, name) \107 case EnumName: \108 return #EnumName;109#define CV_SYMBOL(EnumName, value) SYMBOL_RECORD(EnumName, value, EnumName)110#include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"111 }112 return formatUnknownEnum(K);113}114 115std::string llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {116 switch (K) {117#define TYPE_RECORD(EnumName, value, name) \118 case EnumName: \119 return #EnumName;120#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"121 default:122 return formatv("UNKNOWN RECORD ({0:X})", llvm::to_underlying(K)).str();123 }124}125 126std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) {127 return std::string(formatv("{0:4}:{1:4}", Segment, Offset));128}129 130#define PUSH_CHARACTERISTIC_FLAG(Enum, TheOpt, Value, Style, Descriptive) \131 PUSH_FLAG(Enum, TheOpt, Value, \132 ((Style == CharacteristicStyle::HeaderDefinition) ? #TheOpt \133 : Descriptive))134 135#define PUSH_MASKED_CHARACTERISTIC_FLAG(Enum, Mask, TheOpt, Value, Style, \136 Descriptive) \137 PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, \138 ((Style == CharacteristicStyle::HeaderDefinition) \139 ? #TheOpt \140 : Descriptive))141 142std::string llvm::pdb::formatSectionCharacteristics(uint32_t IndentLevel,143 uint32_t C,144 uint32_t FlagsPerLine,145 StringRef Separator,146 CharacteristicStyle Style) {147 using SC = COFF::SectionCharacteristics;148 std::vector<std::string> Opts;149 if (C == COFF::SC_Invalid)150 return "invalid";151 if (C == 0)152 return "none";153 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NOLOAD, C, Style, "noload");154 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NO_PAD, C, Style, "no padding");155 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_CODE, C, Style, "code");156 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_INITIALIZED_DATA, C, Style,157 "initialized data");158 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_UNINITIALIZED_DATA, C, Style,159 "uninitialized data");160 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_OTHER, C, Style, "other");161 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_INFO, C, Style, "info");162 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_REMOVE, C, Style, "remove");163 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_COMDAT, C, Style, "comdat");164 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_GPREL, C, Style, "gp rel");165 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PURGEABLE, C, Style, "purgeable");166 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_16BIT, C, Style, "16-bit");167 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_LOCKED, C, Style, "locked");168 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PRELOAD, C, Style, "preload");169 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1BYTES, C,170 Style, "1 byte align");171 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2BYTES, C,172 Style, "2 byte align");173 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4BYTES, C,174 Style, "4 byte align");175 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8BYTES, C,176 Style, "8 byte align");177 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_16BYTES, C,178 Style, "16 byte align");179 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_32BYTES, C,180 Style, "32 byte align");181 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_64BYTES, C,182 Style, "64 byte align");183 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_128BYTES, C,184 Style, "128 byte align");185 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_256BYTES, C,186 Style, "256 byte align");187 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_512BYTES, C,188 Style, "512 byte align");189 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1024BYTES, C,190 Style, "1024 byte align");191 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2048BYTES, C,192 Style, "2048 byte align");193 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4096BYTES, C,194 Style, "4096 byte align");195 PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8192BYTES, C,196 Style, "8192 byte align");197 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_NRELOC_OVFL, C, Style,198 "noreloc overflow");199 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_DISCARDABLE, C, Style,200 "discardable");201 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_CACHED, C, Style,202 "not cached");203 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_PAGED, C, Style, "not paged");204 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_SHARED, C, Style, "shared");205 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_EXECUTE, C, Style,206 "execute permissions");207 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_READ, C, Style,208 "read permissions");209 PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_WRITE, C, Style,210 "write permissions");211 return typesetItemList(Opts, IndentLevel, FlagsPerLine, Separator);212}213