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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/PDB/Native/FormatUtil.h"
10 
11 #include "llvm/ADT/STLExtras.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 
18 using namespace llvm;
19 using namespace llvm::codeview;
20 using namespace llvm::pdb;
21 
typesetItemList(ArrayRef<std::string> Opts,uint32_t IndentLevel,uint32_t GroupSize,StringRef Sep)22 std::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 
typesetStringList(uint32_t IndentLevel,ArrayRef<StringRef> Strings)40 std::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 
formatChunkKind(DebugSubsectionKind Kind,bool Friendly)50 std::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 
formatSymbolKind(SymbolKind K)104 std::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 
formatTypeLeafKind(TypeLeafKind K)115 std::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})",
123                    static_cast<std::underlying_type_t<TypeLeafKind>>(K))
124         .str();
125   }
126 }
127 
formatSegmentOffset(uint16_t Segment,uint32_t Offset)128 std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) {
129   return std::string(formatv("{0:4}:{1:4}", Segment, Offset));
130 }
131 
132 #define PUSH_CHARACTERISTIC_FLAG(Enum, TheOpt, Value, Style, Descriptive)      \
133   PUSH_FLAG(Enum, TheOpt, Value,                                               \
134             ((Style == CharacteristicStyle::HeaderDefinition) ? #TheOpt        \
135                                                               : Descriptive))
136 
137 #define PUSH_MASKED_CHARACTERISTIC_FLAG(Enum, Mask, TheOpt, Value, Style,      \
138                                         Descriptive)                           \
139   PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value,                                  \
140                    ((Style == CharacteristicStyle::HeaderDefinition)           \
141                         ? #TheOpt                                              \
142                         : Descriptive))
143 
formatSectionCharacteristics(uint32_t IndentLevel,uint32_t C,uint32_t FlagsPerLine,StringRef Separator,CharacteristicStyle Style)144 std::string llvm::pdb::formatSectionCharacteristics(uint32_t IndentLevel,
145                                                     uint32_t C,
146                                                     uint32_t FlagsPerLine,
147                                                     StringRef Separator,
148                                                     CharacteristicStyle Style) {
149   using SC = COFF::SectionCharacteristics;
150   std::vector<std::string> Opts;
151   if (C == COFF::SC_Invalid)
152     return "invalid";
153   if (C == 0)
154     return "none";
155   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NOLOAD, C, Style, "noload");
156   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NO_PAD, C, Style, "no padding");
157   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_CODE, C, Style, "code");
158   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_INITIALIZED_DATA, C, Style,
159                            "initialized data");
160   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_UNINITIALIZED_DATA, C, Style,
161                            "uninitialized data");
162   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_OTHER, C, Style, "other");
163   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_INFO, C, Style, "info");
164   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_REMOVE, C, Style, "remove");
165   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_COMDAT, C, Style, "comdat");
166   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_GPREL, C, Style, "gp rel");
167   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PURGEABLE, C, Style, "purgeable");
168   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_16BIT, C, Style, "16-bit");
169   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_LOCKED, C, Style, "locked");
170   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PRELOAD, C, Style, "preload");
171   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1BYTES, C,
172                                   Style, "1 byte align");
173   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2BYTES, C,
174                                   Style, "2 byte align");
175   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4BYTES, C,
176                                   Style, "4 byte align");
177   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8BYTES, C,
178                                   Style, "8 byte align");
179   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_16BYTES, C,
180                                   Style, "16 byte align");
181   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_32BYTES, C,
182                                   Style, "32 byte align");
183   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_64BYTES, C,
184                                   Style, "64 byte align");
185   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_128BYTES, C,
186                                   Style, "128 byte align");
187   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_256BYTES, C,
188                                   Style, "256 byte align");
189   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_512BYTES, C,
190                                   Style, "512 byte align");
191   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1024BYTES, C,
192                                   Style, "1024 byte align");
193   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2048BYTES, C,
194                                   Style, "2048 byte align");
195   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4096BYTES, C,
196                                   Style, "4096 byte align");
197   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8192BYTES, C,
198                                   Style, "8192 byte align");
199   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_NRELOC_OVFL, C, Style,
200                            "noreloc overflow");
201   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_DISCARDABLE, C, Style,
202                            "discardable");
203   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_CACHED, C, Style,
204                            "not cached");
205   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_PAGED, C, Style, "not paged");
206   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_SHARED, C, Style, "shared");
207   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_EXECUTE, C, Style,
208                            "execute permissions");
209   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_READ, C, Style,
210                            "read permissions");
211   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_WRITE, C, Style,
212                            "write permissions");
213   return typesetItemList(Opts, IndentLevel, FlagsPerLine, Separator);
214 }
215