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