1 | //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===// |
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 | // This file contains support for writing Microsoft CodeView debug info. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "CodeViewDebug.h" |
14 | #include "llvm/ADT/APSInt.h" |
15 | #include "llvm/ADT/STLExtras.h" |
16 | #include "llvm/ADT/SmallBitVector.h" |
17 | #include "llvm/ADT/SmallString.h" |
18 | #include "llvm/ADT/StringRef.h" |
19 | #include "llvm/ADT/TinyPtrVector.h" |
20 | #include "llvm/ADT/Twine.h" |
21 | #include "llvm/BinaryFormat/COFF.h" |
22 | #include "llvm/BinaryFormat/Dwarf.h" |
23 | #include "llvm/CodeGen/AsmPrinter.h" |
24 | #include "llvm/CodeGen/LexicalScopes.h" |
25 | #include "llvm/CodeGen/MachineFrameInfo.h" |
26 | #include "llvm/CodeGen/MachineFunction.h" |
27 | #include "llvm/CodeGen/MachineInstr.h" |
28 | #include "llvm/CodeGen/MachineModuleInfo.h" |
29 | #include "llvm/CodeGen/TargetFrameLowering.h" |
30 | #include "llvm/CodeGen/TargetLowering.h" |
31 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
32 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
33 | #include "llvm/Config/llvm-config.h" |
34 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
35 | #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h" |
36 | #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h" |
37 | #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" |
38 | #include "llvm/DebugInfo/CodeView/EnumTables.h" |
39 | #include "llvm/DebugInfo/CodeView/Line.h" |
40 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
41 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
42 | #include "llvm/DebugInfo/CodeView/TypeTableCollection.h" |
43 | #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" |
44 | #include "llvm/IR/Constants.h" |
45 | #include "llvm/IR/DataLayout.h" |
46 | #include "llvm/IR/DebugInfoMetadata.h" |
47 | #include "llvm/IR/Function.h" |
48 | #include "llvm/IR/GlobalValue.h" |
49 | #include "llvm/IR/GlobalVariable.h" |
50 | #include "llvm/IR/Metadata.h" |
51 | #include "llvm/IR/Module.h" |
52 | #include "llvm/MC/MCAsmInfo.h" |
53 | #include "llvm/MC/MCContext.h" |
54 | #include "llvm/MC/MCSectionCOFF.h" |
55 | #include "llvm/MC/MCStreamer.h" |
56 | #include "llvm/MC/MCSymbol.h" |
57 | #include "llvm/Support/BinaryStreamWriter.h" |
58 | #include "llvm/Support/Casting.h" |
59 | #include "llvm/Support/Error.h" |
60 | #include "llvm/Support/ErrorHandling.h" |
61 | #include "llvm/Support/FormatVariadic.h" |
62 | #include "llvm/Support/Path.h" |
63 | #include "llvm/Support/SMLoc.h" |
64 | #include "llvm/Support/ScopedPrinter.h" |
65 | #include "llvm/Target/TargetLoweringObjectFile.h" |
66 | #include "llvm/Target/TargetMachine.h" |
67 | #include "llvm/TargetParser/Triple.h" |
68 | #include <algorithm> |
69 | #include <cassert> |
70 | #include <cctype> |
71 | #include <cstddef> |
72 | #include <limits> |
73 | |
74 | using namespace llvm; |
75 | using namespace llvm::codeview; |
76 | |
77 | namespace { |
78 | class CVMCAdapter : public CodeViewRecordStreamer { |
79 | public: |
80 | CVMCAdapter(MCStreamer &OS, TypeCollection &TypeTable) |
81 | : OS(&OS), TypeTable(TypeTable) {} |
82 | |
83 | void emitBytes(StringRef Data) override { OS->emitBytes(Data); } |
84 | |
85 | void emitIntValue(uint64_t Value, unsigned Size) override { |
86 | OS->emitIntValueInHex(Value, Size); |
87 | } |
88 | |
89 | void emitBinaryData(StringRef Data) override { OS->emitBinaryData(Data); } |
90 | |
91 | void (const Twine &T) override { OS->AddComment(T); } |
92 | |
93 | void (const Twine &T) override { OS->emitRawComment(T); } |
94 | |
95 | bool isVerboseAsm() override { return OS->isVerboseAsm(); } |
96 | |
97 | std::string getTypeName(TypeIndex TI) override { |
98 | std::string TypeName; |
99 | if (!TI.isNoneType()) { |
100 | if (TI.isSimple()) |
101 | TypeName = std::string(TypeIndex::simpleTypeName(TI)); |
102 | else |
103 | TypeName = std::string(TypeTable.getTypeName(Index: TI)); |
104 | } |
105 | return TypeName; |
106 | } |
107 | |
108 | private: |
109 | MCStreamer *OS = nullptr; |
110 | TypeCollection &TypeTable; |
111 | }; |
112 | } // namespace |
113 | |
114 | static CPUType mapArchToCVCPUType(Triple::ArchType Type) { |
115 | switch (Type) { |
116 | case Triple::ArchType::x86: |
117 | return CPUType::Pentium3; |
118 | case Triple::ArchType::x86_64: |
119 | return CPUType::X64; |
120 | case Triple::ArchType::thumb: |
121 | // LLVM currently doesn't support Windows CE and so thumb |
122 | // here is indiscriminately mapped to ARMNT specifically. |
123 | return CPUType::ARMNT; |
124 | case Triple::ArchType::aarch64: |
125 | return CPUType::ARM64; |
126 | case Triple::ArchType::mipsel: |
127 | return CPUType::MIPS; |
128 | case Triple::ArchType::UnknownArch: |
129 | return CPUType::Unknown; |
130 | default: |
131 | report_fatal_error(reason: "target architecture doesn't map to a CodeView CPUType" ); |
132 | } |
133 | } |
134 | |
135 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
136 | : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {} |
137 | |
138 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
139 | std::string &Filepath = FileToFilepathMap[File]; |
140 | if (!Filepath.empty()) |
141 | return Filepath; |
142 | |
143 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
144 | |
145 | // If this is a Unix-style path, just use it as is. Don't try to canonicalize |
146 | // it textually because one of the path components could be a symlink. |
147 | if (Dir.starts_with(Prefix: "/" ) || Filename.starts_with(Prefix: "/" )) { |
148 | if (llvm::sys::path::is_absolute(path: Filename, style: llvm::sys::path::Style::posix)) |
149 | return Filename; |
150 | Filepath = std::string(Dir); |
151 | if (Dir.back() != '/') |
152 | Filepath += '/'; |
153 | Filepath += Filename; |
154 | return Filepath; |
155 | } |
156 | |
157 | // Clang emits directory and relative filename info into the IR, but CodeView |
158 | // operates on full paths. We could change Clang to emit full paths too, but |
159 | // that would increase the IR size and probably not needed for other users. |
160 | // For now, just concatenate and canonicalize the path here. |
161 | if (Filename.find(C: ':') == 1) |
162 | Filepath = std::string(Filename); |
163 | else |
164 | Filepath = (Dir + "\\" + Filename).str(); |
165 | |
166 | // Canonicalize the path. We have to do it textually because we may no longer |
167 | // have access the file in the filesystem. |
168 | // First, replace all slashes with backslashes. |
169 | llvm::replace(Range&: Filepath, OldValue: '/', NewValue: '\\'); |
170 | |
171 | // Remove all "\.\" with "\". |
172 | size_t Cursor = 0; |
173 | while ((Cursor = Filepath.find(s: "\\.\\" , pos: Cursor)) != std::string::npos) |
174 | Filepath.erase(pos: Cursor, n: 2); |
175 | |
176 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
177 | // path should be well-formatted, e.g. start with a drive letter, etc. |
178 | Cursor = 0; |
179 | while ((Cursor = Filepath.find(s: "\\..\\" , pos: Cursor)) != std::string::npos) { |
180 | // Something's wrong if the path starts with "\..\", abort. |
181 | if (Cursor == 0) |
182 | break; |
183 | |
184 | size_t PrevSlash = Filepath.rfind(c: '\\', pos: Cursor - 1); |
185 | if (PrevSlash == std::string::npos) |
186 | // Something's wrong, abort. |
187 | break; |
188 | |
189 | Filepath.erase(pos: PrevSlash, n: Cursor + 3 - PrevSlash); |
190 | // The next ".." might be following the one we've just erased. |
191 | Cursor = PrevSlash; |
192 | } |
193 | |
194 | // Remove all duplicate backslashes. |
195 | Cursor = 0; |
196 | while ((Cursor = Filepath.find(s: "\\\\" , pos: Cursor)) != std::string::npos) |
197 | Filepath.erase(pos: Cursor, n: 1); |
198 | |
199 | return Filepath; |
200 | } |
201 | |
202 | unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { |
203 | StringRef FullPath = getFullFilepath(File: F); |
204 | unsigned NextId = FileIdMap.size() + 1; |
205 | auto Insertion = FileIdMap.insert(KV: std::make_pair(x&: FullPath, y&: NextId)); |
206 | if (Insertion.second) { |
207 | // We have to compute the full filepath and emit a .cv_file directive. |
208 | ArrayRef<uint8_t> ChecksumAsBytes; |
209 | FileChecksumKind CSKind = FileChecksumKind::None; |
210 | if (F->getChecksum()) { |
211 | std::string Checksum = fromHex(Input: F->getChecksum()->Value); |
212 | void *CKMem = OS.getContext().allocate(Size: Checksum.size(), Align: 1); |
213 | memcpy(dest: CKMem, src: Checksum.data(), n: Checksum.size()); |
214 | ChecksumAsBytes = ArrayRef<uint8_t>( |
215 | reinterpret_cast<const uint8_t *>(CKMem), Checksum.size()); |
216 | switch (F->getChecksum()->Kind) { |
217 | case DIFile::CSK_MD5: |
218 | CSKind = FileChecksumKind::MD5; |
219 | break; |
220 | case DIFile::CSK_SHA1: |
221 | CSKind = FileChecksumKind::SHA1; |
222 | break; |
223 | case DIFile::CSK_SHA256: |
224 | CSKind = FileChecksumKind::SHA256; |
225 | break; |
226 | } |
227 | } |
228 | bool Success = OS.emitCVFileDirective(FileNo: NextId, Filename: FullPath, Checksum: ChecksumAsBytes, |
229 | ChecksumKind: static_cast<unsigned>(CSKind)); |
230 | (void)Success; |
231 | assert(Success && ".cv_file directive failed" ); |
232 | } |
233 | return Insertion.first->second; |
234 | } |
235 | |
236 | CodeViewDebug::InlineSite & |
237 | CodeViewDebug::getInlineSite(const DILocation *InlinedAt, |
238 | const DISubprogram *Inlinee) { |
239 | auto SiteInsertion = CurFn->InlineSites.try_emplace(k: InlinedAt); |
240 | InlineSite *Site = &SiteInsertion.first->second; |
241 | if (SiteInsertion.second) { |
242 | unsigned ParentFuncId = CurFn->FuncId; |
243 | if (const DILocation *OuterIA = InlinedAt->getInlinedAt()) |
244 | ParentFuncId = |
245 | getInlineSite(InlinedAt: OuterIA, Inlinee: InlinedAt->getScope()->getSubprogram()) |
246 | .SiteFuncId; |
247 | |
248 | Site->SiteFuncId = NextFuncId++; |
249 | OS.emitCVInlineSiteIdDirective( |
250 | FunctionId: Site->SiteFuncId, IAFunc: ParentFuncId, IAFile: maybeRecordFile(F: InlinedAt->getFile()), |
251 | IALine: InlinedAt->getLine(), IACol: InlinedAt->getColumn(), Loc: SMLoc()); |
252 | Site->Inlinee = Inlinee; |
253 | InlinedSubprograms.insert(X: Inlinee); |
254 | auto InlineeIdx = getFuncIdForSubprogram(SP: Inlinee); |
255 | |
256 | if (InlinedAt->getInlinedAt() == nullptr) |
257 | CurFn->Inlinees.insert(V: InlineeIdx); |
258 | } |
259 | return *Site; |
260 | } |
261 | |
262 | static StringRef getPrettyScopeName(const DIScope *Scope) { |
263 | StringRef ScopeName = Scope->getName(); |
264 | if (!ScopeName.empty()) |
265 | return ScopeName; |
266 | |
267 | switch (Scope->getTag()) { |
268 | case dwarf::DW_TAG_enumeration_type: |
269 | case dwarf::DW_TAG_class_type: |
270 | case dwarf::DW_TAG_structure_type: |
271 | case dwarf::DW_TAG_union_type: |
272 | return "<unnamed-tag>" ; |
273 | case dwarf::DW_TAG_namespace: |
274 | return "`anonymous namespace'" ; |
275 | default: |
276 | return StringRef(); |
277 | } |
278 | } |
279 | |
280 | const DISubprogram *CodeViewDebug::collectParentScopeNames( |
281 | const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) { |
282 | const DISubprogram *ClosestSubprogram = nullptr; |
283 | while (Scope != nullptr) { |
284 | if (ClosestSubprogram == nullptr) |
285 | ClosestSubprogram = dyn_cast<DISubprogram>(Val: Scope); |
286 | |
287 | // If a type appears in a scope chain, make sure it gets emitted. The |
288 | // frontend will be responsible for deciding if this should be a forward |
289 | // declaration or a complete type. |
290 | if (const auto *Ty = dyn_cast<DICompositeType>(Val: Scope)) |
291 | DeferredCompleteTypes.push_back(Elt: Ty); |
292 | |
293 | StringRef ScopeName = getPrettyScopeName(Scope); |
294 | if (!ScopeName.empty()) |
295 | QualifiedNameComponents.push_back(Elt: ScopeName); |
296 | Scope = Scope->getScope(); |
297 | } |
298 | return ClosestSubprogram; |
299 | } |
300 | |
301 | static std::string formatNestedName(ArrayRef<StringRef> QualifiedNameComponents, |
302 | StringRef TypeName) { |
303 | std::string FullyQualifiedName; |
304 | for (StringRef QualifiedNameComponent : |
305 | llvm::reverse(C&: QualifiedNameComponents)) { |
306 | FullyQualifiedName.append(str: std::string(QualifiedNameComponent)); |
307 | FullyQualifiedName.append(s: "::" ); |
308 | } |
309 | FullyQualifiedName.append(str: std::string(TypeName)); |
310 | return FullyQualifiedName; |
311 | } |
312 | |
313 | struct CodeViewDebug::TypeLoweringScope { |
314 | TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; } |
315 | ~TypeLoweringScope() { |
316 | // Don't decrement TypeEmissionLevel until after emitting deferred types, so |
317 | // inner TypeLoweringScopes don't attempt to emit deferred types. |
318 | if (CVD.TypeEmissionLevel == 1) |
319 | CVD.emitDeferredCompleteTypes(); |
320 | --CVD.TypeEmissionLevel; |
321 | } |
322 | CodeViewDebug &CVD; |
323 | }; |
324 | |
325 | std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Scope, |
326 | StringRef Name) { |
327 | // Ensure types in the scope chain are emitted as soon as possible. |
328 | // This can create otherwise a situation where S_UDTs are emitted while |
329 | // looping in emitDebugInfoForUDTs. |
330 | TypeLoweringScope S(*this); |
331 | SmallVector<StringRef, 5> QualifiedNameComponents; |
332 | collectParentScopeNames(Scope, QualifiedNameComponents); |
333 | return formatNestedName(QualifiedNameComponents, TypeName: Name); |
334 | } |
335 | |
336 | std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Ty) { |
337 | const DIScope *Scope = Ty->getScope(); |
338 | return getFullyQualifiedName(Scope, Name: getPrettyScopeName(Scope: Ty)); |
339 | } |
340 | |
341 | TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) { |
342 | // No scope means global scope and that uses the zero index. |
343 | // |
344 | // We also use zero index when the scope is a DISubprogram |
345 | // to suppress the emission of LF_STRING_ID for the function, |
346 | // which can trigger a link-time error with the linker in |
347 | // VS2019 version 16.11.2 or newer. |
348 | // Note, however, skipping the debug info emission for the DISubprogram |
349 | // is a temporary fix. The root issue here is that we need to figure out |
350 | // the proper way to encode a function nested in another function |
351 | // (as introduced by the Fortran 'contains' keyword) in CodeView. |
352 | if (!Scope || isa<DIFile>(Val: Scope) || isa<DISubprogram>(Val: Scope)) |
353 | return TypeIndex(); |
354 | |
355 | assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type" ); |
356 | |
357 | // Check if we've already translated this scope. |
358 | auto I = TypeIndices.find(Val: {Scope, nullptr}); |
359 | if (I != TypeIndices.end()) |
360 | return I->second; |
361 | |
362 | // Build the fully qualified name of the scope. |
363 | std::string ScopeName = getFullyQualifiedName(Ty: Scope); |
364 | StringIdRecord SID(TypeIndex(), ScopeName); |
365 | auto TI = TypeTable.writeLeafType(Record&: SID); |
366 | return recordTypeIndexForDINode(Node: Scope, TI); |
367 | } |
368 | |
369 | static StringRef removeTemplateArgs(StringRef Name) { |
370 | // Remove template args from the display name. Assume that the template args |
371 | // are the last thing in the name. |
372 | if (Name.empty() || Name.back() != '>') |
373 | return Name; |
374 | |
375 | int OpenBrackets = 0; |
376 | for (int i = Name.size() - 1; i >= 0; --i) { |
377 | if (Name[i] == '>') |
378 | ++OpenBrackets; |
379 | else if (Name[i] == '<') { |
380 | --OpenBrackets; |
381 | if (OpenBrackets == 0) |
382 | return Name.substr(Start: 0, N: i); |
383 | } |
384 | } |
385 | return Name; |
386 | } |
387 | |
388 | TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) { |
389 | assert(SP); |
390 | |
391 | // Check if we've already translated this subprogram. |
392 | auto I = TypeIndices.find(Val: {SP, nullptr}); |
393 | if (I != TypeIndices.end()) |
394 | return I->second; |
395 | |
396 | // The display name includes function template arguments. Drop them to match |
397 | // MSVC. We need to have the template arguments in the DISubprogram name |
398 | // because they are used in other symbol records, such as S_GPROC32_IDs. |
399 | StringRef DisplayName = removeTemplateArgs(Name: SP->getName()); |
400 | |
401 | const DIScope *Scope = SP->getScope(); |
402 | TypeIndex TI; |
403 | if (const auto *Class = dyn_cast_or_null<DICompositeType>(Val: Scope)) { |
404 | // If the scope is a DICompositeType, then this must be a method. Member |
405 | // function types take some special handling, and require access to the |
406 | // subprogram. |
407 | TypeIndex ClassType = getTypeIndex(Ty: Class); |
408 | MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class), |
409 | DisplayName); |
410 | TI = TypeTable.writeLeafType(Record&: MFuncId); |
411 | } else { |
412 | // Otherwise, this must be a free function. |
413 | TypeIndex ParentScope = getScopeIndex(Scope); |
414 | FuncIdRecord FuncId(ParentScope, getTypeIndex(Ty: SP->getType()), DisplayName); |
415 | TI = TypeTable.writeLeafType(Record&: FuncId); |
416 | } |
417 | |
418 | return recordTypeIndexForDINode(Node: SP, TI); |
419 | } |
420 | |
421 | static bool isNonTrivial(const DICompositeType *DCTy) { |
422 | return ((DCTy->getFlags() & DINode::FlagNonTrivial) == DINode::FlagNonTrivial); |
423 | } |
424 | |
425 | static FunctionOptions |
426 | getFunctionOptions(const DISubroutineType *Ty, |
427 | const DICompositeType *ClassTy = nullptr, |
428 | StringRef SPName = StringRef("" )) { |
429 | FunctionOptions FO = FunctionOptions::None; |
430 | const DIType *ReturnTy = nullptr; |
431 | if (auto TypeArray = Ty->getTypeArray()) { |
432 | if (TypeArray.size()) |
433 | ReturnTy = TypeArray[0]; |
434 | } |
435 | |
436 | // Add CxxReturnUdt option to functions that return nontrivial record types |
437 | // or methods that return record types. |
438 | if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(Val: ReturnTy)) |
439 | if (isNonTrivial(DCTy: ReturnDCTy) || ClassTy) |
440 | FO |= FunctionOptions::CxxReturnUdt; |
441 | |
442 | // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison. |
443 | if (ClassTy && isNonTrivial(DCTy: ClassTy) && SPName == ClassTy->getName()) { |
444 | FO |= FunctionOptions::Constructor; |
445 | |
446 | // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag. |
447 | |
448 | } |
449 | return FO; |
450 | } |
451 | |
452 | TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP, |
453 | const DICompositeType *Class) { |
454 | // Always use the method declaration as the key for the function type. The |
455 | // method declaration contains the this adjustment. |
456 | if (SP->getDeclaration()) |
457 | SP = SP->getDeclaration(); |
458 | assert(!SP->getDeclaration() && "should use declaration as key" ); |
459 | |
460 | // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide |
461 | // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}. |
462 | auto I = TypeIndices.find(Val: {SP, Class}); |
463 | if (I != TypeIndices.end()) |
464 | return I->second; |
465 | |
466 | // Make sure complete type info for the class is emitted *after* the member |
467 | // function type, as the complete class type is likely to reference this |
468 | // member function type. |
469 | TypeLoweringScope S(*this); |
470 | const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0; |
471 | |
472 | FunctionOptions FO = getFunctionOptions(Ty: SP->getType(), ClassTy: Class, SPName: SP->getName()); |
473 | TypeIndex TI = lowerTypeMemberFunction( |
474 | Ty: SP->getType(), ClassTy: Class, ThisAdjustment: SP->getThisAdjustment(), IsStaticMethod, FO); |
475 | return recordTypeIndexForDINode(Node: SP, TI, ClassTy: Class); |
476 | } |
477 | |
478 | TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node, |
479 | TypeIndex TI, |
480 | const DIType *ClassTy) { |
481 | auto InsertResult = TypeIndices.insert(KV: {{Node, ClassTy}, TI}); |
482 | (void)InsertResult; |
483 | assert(InsertResult.second && "DINode was already assigned a type index" ); |
484 | return TI; |
485 | } |
486 | |
487 | unsigned CodeViewDebug::getPointerSizeInBytes() { |
488 | return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8; |
489 | } |
490 | |
491 | void CodeViewDebug::recordLocalVariable(LocalVariable &&Var, |
492 | const LexicalScope *LS) { |
493 | if (const DILocation *InlinedAt = LS->getInlinedAt()) { |
494 | // This variable was inlined. Associate it with the InlineSite. |
495 | const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram(); |
496 | InlineSite &Site = getInlineSite(InlinedAt, Inlinee); |
497 | Site.InlinedLocals.emplace_back(Args: std::move(Var)); |
498 | } else { |
499 | // This variable goes into the corresponding lexical scope. |
500 | ScopeVariables[LS].emplace_back(Args: std::move(Var)); |
501 | } |
502 | } |
503 | |
504 | static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs, |
505 | const DILocation *Loc) { |
506 | if (!llvm::is_contained(Range&: Locs, Element: Loc)) |
507 | Locs.push_back(Elt: Loc); |
508 | } |
509 | |
510 | void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL, |
511 | const MachineFunction *MF) { |
512 | // Skip this instruction if it has the same location as the previous one. |
513 | if (!DL || DL == PrevInstLoc) |
514 | return; |
515 | |
516 | const DIScope *Scope = DL->getScope(); |
517 | if (!Scope) |
518 | return; |
519 | |
520 | // Skip this line if it is longer than the maximum we can record. |
521 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
522 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
523 | LI.isNeverStepInto()) |
524 | return; |
525 | |
526 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
527 | if (CI.getStartColumn() != DL.getCol()) |
528 | return; |
529 | |
530 | if (!CurFn->HaveLineInfo) |
531 | CurFn->HaveLineInfo = true; |
532 | unsigned FileId = 0; |
533 | if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile()) |
534 | FileId = CurFn->LastFileId; |
535 | else |
536 | FileId = CurFn->LastFileId = maybeRecordFile(F: DL->getFile()); |
537 | PrevInstLoc = DL; |
538 | |
539 | unsigned FuncId = CurFn->FuncId; |
540 | if (const DILocation *SiteLoc = DL->getInlinedAt()) { |
541 | const DILocation *Loc = DL.get(); |
542 | |
543 | // If this location was actually inlined from somewhere else, give it the ID |
544 | // of the inline call site. |
545 | FuncId = |
546 | getInlineSite(InlinedAt: SiteLoc, Inlinee: Loc->getScope()->getSubprogram()).SiteFuncId; |
547 | |
548 | // Ensure we have links in the tree of inline call sites. |
549 | bool FirstLoc = true; |
550 | while ((SiteLoc = Loc->getInlinedAt())) { |
551 | InlineSite &Site = |
552 | getInlineSite(InlinedAt: SiteLoc, Inlinee: Loc->getScope()->getSubprogram()); |
553 | if (!FirstLoc) |
554 | addLocIfNotPresent(Locs&: Site.ChildSites, Loc); |
555 | FirstLoc = false; |
556 | Loc = SiteLoc; |
557 | } |
558 | addLocIfNotPresent(Locs&: CurFn->ChildSites, Loc); |
559 | } |
560 | |
561 | OS.emitCVLocDirective(FunctionId: FuncId, FileNo: FileId, Line: DL.getLine(), Column: DL.getCol(), |
562 | /*PrologueEnd=*/false, /*IsStmt=*/false, |
563 | FileName: DL->getFilename(), Loc: SMLoc()); |
564 | } |
565 | |
566 | void CodeViewDebug::emitCodeViewMagicVersion() { |
567 | OS.emitValueToAlignment(Alignment: Align(4)); |
568 | OS.AddComment(T: "Debug section magic" ); |
569 | OS.emitInt32(Value: COFF::DEBUG_SECTION_MAGIC); |
570 | } |
571 | |
572 | static SourceLanguage MapDWLangToCVLang(unsigned DWLang) { |
573 | switch (DWLang) { |
574 | case dwarf::DW_LANG_C: |
575 | case dwarf::DW_LANG_C89: |
576 | case dwarf::DW_LANG_C99: |
577 | case dwarf::DW_LANG_C11: |
578 | return SourceLanguage::C; |
579 | case dwarf::DW_LANG_C_plus_plus: |
580 | case dwarf::DW_LANG_C_plus_plus_03: |
581 | case dwarf::DW_LANG_C_plus_plus_11: |
582 | case dwarf::DW_LANG_C_plus_plus_14: |
583 | return SourceLanguage::Cpp; |
584 | case dwarf::DW_LANG_Fortran77: |
585 | case dwarf::DW_LANG_Fortran90: |
586 | case dwarf::DW_LANG_Fortran95: |
587 | case dwarf::DW_LANG_Fortran03: |
588 | case dwarf::DW_LANG_Fortran08: |
589 | return SourceLanguage::Fortran; |
590 | case dwarf::DW_LANG_Pascal83: |
591 | return SourceLanguage::Pascal; |
592 | case dwarf::DW_LANG_Cobol74: |
593 | case dwarf::DW_LANG_Cobol85: |
594 | return SourceLanguage::Cobol; |
595 | case dwarf::DW_LANG_Java: |
596 | return SourceLanguage::Java; |
597 | case dwarf::DW_LANG_D: |
598 | return SourceLanguage::D; |
599 | case dwarf::DW_LANG_Swift: |
600 | return SourceLanguage::Swift; |
601 | case dwarf::DW_LANG_Rust: |
602 | return SourceLanguage::Rust; |
603 | case dwarf::DW_LANG_ObjC: |
604 | return SourceLanguage::ObjC; |
605 | case dwarf::DW_LANG_ObjC_plus_plus: |
606 | return SourceLanguage::ObjCpp; |
607 | default: |
608 | // There's no CodeView representation for this language, and CV doesn't |
609 | // have an "unknown" option for the language field, so we'll use MASM, |
610 | // as it's very low level. |
611 | return SourceLanguage::Masm; |
612 | } |
613 | } |
614 | |
615 | void CodeViewDebug::beginModule(Module *M) { |
616 | // If COFF debug section is not available, skip any debug info related stuff. |
617 | if (!Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) { |
618 | Asm = nullptr; |
619 | return; |
620 | } |
621 | |
622 | CompilerInfoAsm = Asm; |
623 | TheCPU = mapArchToCVCPUType(Type: M->getTargetTriple().getArch()); |
624 | |
625 | // Get the current source language. |
626 | const MDNode *Node; |
627 | if (Asm->hasDebugInfo()) { |
628 | Node = *M->debug_compile_units_begin(); |
629 | } else { |
630 | // When emitting only compiler information, we may have only NoDebug CUs, |
631 | // which would be skipped by debug_compile_units_begin. |
632 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata(Name: "llvm.dbg.cu" ); |
633 | Node = *CUs->operands().begin(); |
634 | } |
635 | const auto *CU = cast<DICompileUnit>(Val: Node); |
636 | |
637 | CurrentSourceLanguage = MapDWLangToCVLang(DWLang: CU->getSourceLanguage()); |
638 | if (!M->getCodeViewFlag() || |
639 | CU->getEmissionKind() == DICompileUnit::NoDebug) { |
640 | Asm = nullptr; |
641 | return; |
642 | } |
643 | |
644 | collectGlobalVariableInfo(); |
645 | |
646 | // Check if we should emit type record hashes. |
647 | ConstantInt *GH = |
648 | mdconst::extract_or_null<ConstantInt>(MD: M->getModuleFlag(Key: "CodeViewGHash" )); |
649 | EmitDebugGlobalHashes = GH && !GH->isZero(); |
650 | } |
651 | |
652 | void CodeViewDebug::endModule() { |
653 | if (!CompilerInfoAsm) |
654 | return; |
655 | |
656 | // The COFF .debug$S section consists of several subsections, each starting |
657 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
658 | // of the payload followed by the payload itself. The subsections are 4-byte |
659 | // aligned. |
660 | |
661 | // Use the generic .debug$S section, and make a subsection for all the inlined |
662 | // subprograms. |
663 | switchToDebugSectionForSymbol(GVSym: nullptr); |
664 | |
665 | MCSymbol *CompilerInfo = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
666 | emitObjName(); |
667 | emitCompilerInformation(); |
668 | endCVSubsection(EndLabel: CompilerInfo); |
669 | if (!Asm) |
670 | return; |
671 | |
672 | emitSecureHotPatchInformation(); |
673 | |
674 | emitInlineeLinesSubsection(); |
675 | |
676 | // Emit per-function debug information. |
677 | for (auto &P : FnDebugInfo) |
678 | if (!P.first->isDeclarationForLinker()) |
679 | emitDebugInfoForFunction(GV: P.first, FI&: *P.second); |
680 | |
681 | // Get types used by globals without emitting anything. |
682 | // This is meant to collect all static const data members so they can be |
683 | // emitted as globals. |
684 | collectDebugInfoForGlobals(); |
685 | |
686 | // Emit retained types. |
687 | emitDebugInfoForRetainedTypes(); |
688 | |
689 | // Emit global variable debug information. |
690 | setCurrentSubprogram(nullptr); |
691 | emitDebugInfoForGlobals(); |
692 | |
693 | // Switch back to the generic .debug$S section after potentially processing |
694 | // comdat symbol sections. |
695 | switchToDebugSectionForSymbol(GVSym: nullptr); |
696 | |
697 | // Emit UDT records for any types used by global variables. |
698 | if (!GlobalUDTs.empty()) { |
699 | MCSymbol *SymbolsEnd = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
700 | emitDebugInfoForUDTs(UDTs: GlobalUDTs); |
701 | endCVSubsection(EndLabel: SymbolsEnd); |
702 | } |
703 | |
704 | // This subsection holds a file index to offset in string table table. |
705 | OS.AddComment(T: "File index to string table offset subsection" ); |
706 | OS.emitCVFileChecksumsDirective(); |
707 | |
708 | // This subsection holds the string table. |
709 | OS.AddComment(T: "String table" ); |
710 | OS.emitCVStringTableDirective(); |
711 | |
712 | // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol |
713 | // subsection in the generic .debug$S section at the end. There is no |
714 | // particular reason for this ordering other than to match MSVC. |
715 | emitBuildInfo(); |
716 | |
717 | // Emit type information and hashes last, so that any types we translate while |
718 | // emitting function info are included. |
719 | emitTypeInformation(); |
720 | |
721 | if (EmitDebugGlobalHashes) |
722 | emitTypeGlobalHashes(); |
723 | |
724 | clear(); |
725 | } |
726 | |
727 | static void |
728 | emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S, |
729 | unsigned MaxFixedRecordLength = 0xF00) { |
730 | // The maximum CV record length is 0xFF00. Most of the strings we emit appear |
731 | // after a fixed length portion of the record. The fixed length portion should |
732 | // always be less than 0xF00 (3840) bytes, so truncate the string so that the |
733 | // overall record size is less than the maximum allowed. |
734 | SmallString<32> NullTerminatedString( |
735 | S.take_front(N: MaxRecordLength - MaxFixedRecordLength - 1)); |
736 | NullTerminatedString.push_back(Elt: '\0'); |
737 | OS.emitBytes(Data: NullTerminatedString); |
738 | } |
739 | |
740 | void CodeViewDebug::emitTypeInformation() { |
741 | if (TypeTable.empty()) |
742 | return; |
743 | |
744 | // Start the .debug$T or .debug$P section with 0x4. |
745 | OS.switchSection(Section: Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
746 | emitCodeViewMagicVersion(); |
747 | |
748 | TypeTableCollection Table(TypeTable.records()); |
749 | TypeVisitorCallbackPipeline Pipeline; |
750 | |
751 | // To emit type record using Codeview MCStreamer adapter |
752 | CVMCAdapter CVMCOS(OS, Table); |
753 | TypeRecordMapping typeMapping(CVMCOS); |
754 | Pipeline.addCallbackToPipeline(Callbacks&: typeMapping); |
755 | |
756 | std::optional<TypeIndex> B = Table.getFirst(); |
757 | while (B) { |
758 | // This will fail if the record data is invalid. |
759 | CVType Record = Table.getType(Index: *B); |
760 | |
761 | Error E = codeview::visitTypeRecord(Record, Index: *B, Callbacks&: Pipeline); |
762 | |
763 | if (E) { |
764 | logAllUnhandledErrors(E: std::move(E), OS&: errs(), ErrorBanner: "error: " ); |
765 | llvm_unreachable("produced malformed type record" ); |
766 | } |
767 | |
768 | B = Table.getNext(Prev: *B); |
769 | } |
770 | } |
771 | |
772 | void CodeViewDebug::emitTypeGlobalHashes() { |
773 | if (TypeTable.empty()) |
774 | return; |
775 | |
776 | // Start the .debug$H section with the version and hash algorithm, currently |
777 | // hardcoded to version 0, SHA1. |
778 | OS.switchSection(Section: Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection()); |
779 | |
780 | OS.emitValueToAlignment(Alignment: Align(4)); |
781 | OS.AddComment(T: "Magic" ); |
782 | OS.emitInt32(Value: COFF::DEBUG_HASHES_SECTION_MAGIC); |
783 | OS.AddComment(T: "Section Version" ); |
784 | OS.emitInt16(Value: 0); |
785 | OS.AddComment(T: "Hash Algorithm" ); |
786 | OS.emitInt16(Value: uint16_t(GlobalTypeHashAlg::BLAKE3)); |
787 | |
788 | TypeIndex TI(TypeIndex::FirstNonSimpleIndex); |
789 | for (const auto &GHR : TypeTable.hashes()) { |
790 | if (OS.isVerboseAsm()) { |
791 | // Emit an EOL-comment describing which TypeIndex this hash corresponds |
792 | // to, as well as the stringified SHA1 hash. |
793 | SmallString<32> ; |
794 | raw_svector_ostream (Comment); |
795 | CommentOS << formatv(Fmt: "{0:X+} [{1}]" , Vals: TI.getIndex(), Vals: GHR); |
796 | OS.AddComment(T: Comment); |
797 | ++TI; |
798 | } |
799 | assert(GHR.Hash.size() == 8); |
800 | StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()), |
801 | GHR.Hash.size()); |
802 | OS.emitBinaryData(Data: S); |
803 | } |
804 | } |
805 | |
806 | void CodeViewDebug::emitObjName() { |
807 | MCSymbol *CompilerEnd = beginSymbolRecord(Kind: SymbolKind::S_OBJNAME); |
808 | |
809 | StringRef PathRef(CompilerInfoAsm->TM.Options.ObjectFilenameForDebug); |
810 | llvm::SmallString<256> PathStore(PathRef); |
811 | |
812 | if (PathRef.empty() || PathRef == "-" ) { |
813 | // Don't emit the filename if we're writing to stdout or to /dev/null. |
814 | PathRef = {}; |
815 | } else { |
816 | PathRef = PathStore; |
817 | } |
818 | |
819 | OS.AddComment(T: "Signature" ); |
820 | OS.emitIntValue(Value: 0, Size: 4); |
821 | |
822 | OS.AddComment(T: "Object name" ); |
823 | emitNullTerminatedSymbolName(OS, S: PathRef); |
824 | |
825 | endSymbolRecord(SymEnd: CompilerEnd); |
826 | } |
827 | |
828 | void CodeViewDebug::emitSecureHotPatchInformation() { |
829 | MCSymbol *hotPatchInfo = nullptr; |
830 | |
831 | for (const auto &F : MMI->getModule()->functions()) { |
832 | if (!F.isDeclarationForLinker() && |
833 | F.hasFnAttribute(Kind: "marked_for_windows_hot_patching" )) { |
834 | if (hotPatchInfo == nullptr) |
835 | hotPatchInfo = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
836 | MCSymbol *HotPatchEnd = beginSymbolRecord(Kind: SymbolKind::S_HOTPATCHFUNC); |
837 | auto *SP = F.getSubprogram(); |
838 | OS.AddComment(T: "Function" ); |
839 | OS.emitInt32(Value: getFuncIdForSubprogram(SP).getIndex()); |
840 | OS.AddComment(T: "Name" ); |
841 | emitNullTerminatedSymbolName(OS, S: F.getName()); |
842 | endSymbolRecord(SymEnd: HotPatchEnd); |
843 | } |
844 | } |
845 | |
846 | if (hotPatchInfo != nullptr) |
847 | endCVSubsection(EndLabel: hotPatchInfo); |
848 | } |
849 | |
850 | namespace { |
851 | struct Version { |
852 | int Part[4]; |
853 | }; |
854 | } // end anonymous namespace |
855 | |
856 | // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out |
857 | // the version number. |
858 | static Version parseVersion(StringRef Name) { |
859 | Version V = {.Part: {0}}; |
860 | int N = 0; |
861 | for (const char C : Name) { |
862 | if (isdigit(C)) { |
863 | V.Part[N] *= 10; |
864 | V.Part[N] += C - '0'; |
865 | V.Part[N] = |
866 | std::min<int>(a: V.Part[N], b: std::numeric_limits<uint16_t>::max()); |
867 | } else if (C == '.') { |
868 | ++N; |
869 | if (N >= 4) |
870 | return V; |
871 | } else if (N > 0) |
872 | return V; |
873 | } |
874 | return V; |
875 | } |
876 | |
877 | void CodeViewDebug::emitCompilerInformation() { |
878 | MCSymbol *CompilerEnd = beginSymbolRecord(Kind: SymbolKind::S_COMPILE3); |
879 | uint32_t Flags = 0; |
880 | |
881 | // The low byte of the flags indicates the source language. |
882 | Flags = CurrentSourceLanguage; |
883 | // TODO: Figure out which other flags need to be set. |
884 | if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) { |
885 | Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO); |
886 | } |
887 | using ArchType = llvm::Triple::ArchType; |
888 | ArchType Arch = MMI->getModule()->getTargetTriple().getArch(); |
889 | if (CompilerInfoAsm->TM.Options.Hotpatch || Arch == ArchType::thumb || |
890 | Arch == ArchType::aarch64) { |
891 | Flags |= static_cast<uint32_t>(CompileSym3Flags::HotPatch); |
892 | } |
893 | |
894 | OS.AddComment(T: "Flags and language" ); |
895 | OS.emitInt32(Value: Flags); |
896 | |
897 | OS.AddComment(T: "CPUType" ); |
898 | OS.emitInt16(Value: static_cast<uint64_t>(TheCPU)); |
899 | |
900 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata(Name: "llvm.dbg.cu" ); |
901 | const MDNode *Node = *CUs->operands().begin(); |
902 | const auto *CU = cast<DICompileUnit>(Val: Node); |
903 | |
904 | StringRef CompilerVersion = CU->getProducer(); |
905 | Version FrontVer = parseVersion(Name: CompilerVersion); |
906 | OS.AddComment(T: "Frontend version" ); |
907 | for (int N : FrontVer.Part) { |
908 | OS.emitInt16(Value: N); |
909 | } |
910 | |
911 | // Some Microsoft tools, like Binscope, expect a backend version number of at |
912 | // least 8.something, so we'll coerce the LLVM version into a form that |
913 | // guarantees it'll be big enough without really lying about the version. |
914 | int Major = 1000 * LLVM_VERSION_MAJOR + |
915 | 10 * LLVM_VERSION_MINOR + |
916 | LLVM_VERSION_PATCH; |
917 | // Clamp it for builds that use unusually large version numbers. |
918 | Major = std::min<int>(a: Major, b: std::numeric_limits<uint16_t>::max()); |
919 | Version BackVer = {.Part: { Major, 0, 0, 0 }}; |
920 | OS.AddComment(T: "Backend version" ); |
921 | for (int N : BackVer.Part) |
922 | OS.emitInt16(Value: N); |
923 | |
924 | OS.AddComment(T: "Null-terminated compiler version string" ); |
925 | emitNullTerminatedSymbolName(OS, S: CompilerVersion); |
926 | |
927 | endSymbolRecord(SymEnd: CompilerEnd); |
928 | } |
929 | |
930 | static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable, |
931 | StringRef S) { |
932 | StringIdRecord SIR(TypeIndex(0x0), S); |
933 | return TypeTable.writeLeafType(Record&: SIR); |
934 | } |
935 | |
936 | void CodeViewDebug::emitBuildInfo() { |
937 | // First, make LF_BUILDINFO. It's a sequence of strings with various bits of |
938 | // build info. The known prefix is: |
939 | // - Absolute path of current directory |
940 | // - Compiler path |
941 | // - Main source file path, relative to CWD or absolute |
942 | // - Type server PDB file |
943 | // - Canonical compiler command line |
944 | // If frontend and backend compilation are separated (think llc or LTO), it's |
945 | // not clear if the compiler path should refer to the executable for the |
946 | // frontend or the backend. Leave it blank for now. |
947 | TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {}; |
948 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata(Name: "llvm.dbg.cu" ); |
949 | const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs. |
950 | const auto *CU = cast<DICompileUnit>(Val: Node); |
951 | const DIFile *MainSourceFile = CU->getFile(); |
952 | BuildInfoArgs[BuildInfoRecord::CurrentDirectory] = |
953 | getStringIdTypeIdx(TypeTable, S: MainSourceFile->getDirectory()); |
954 | BuildInfoArgs[BuildInfoRecord::SourceFile] = |
955 | getStringIdTypeIdx(TypeTable, S: MainSourceFile->getFilename()); |
956 | // FIXME: PDB is intentionally blank unless we implement /Zi type servers. |
957 | BuildInfoArgs[BuildInfoRecord::TypeServerPDB] = |
958 | getStringIdTypeIdx(TypeTable, S: "" ); |
959 | BuildInfoArgs[BuildInfoRecord::BuildTool] = |
960 | getStringIdTypeIdx(TypeTable, S: Asm->TM.Options.MCOptions.Argv0); |
961 | BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx( |
962 | TypeTable, S: Asm->TM.Options.MCOptions.CommandlineArgs); |
963 | |
964 | BuildInfoRecord BIR(BuildInfoArgs); |
965 | TypeIndex BuildInfoIndex = TypeTable.writeLeafType(Record&: BIR); |
966 | |
967 | // Make a new .debug$S subsection for the S_BUILDINFO record, which points |
968 | // from the module symbols into the type stream. |
969 | MCSymbol *BISubsecEnd = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
970 | MCSymbol *BIEnd = beginSymbolRecord(Kind: SymbolKind::S_BUILDINFO); |
971 | OS.AddComment(T: "LF_BUILDINFO index" ); |
972 | OS.emitInt32(Value: BuildInfoIndex.getIndex()); |
973 | endSymbolRecord(SymEnd: BIEnd); |
974 | endCVSubsection(EndLabel: BISubsecEnd); |
975 | } |
976 | |
977 | void CodeViewDebug::emitInlineeLinesSubsection() { |
978 | if (InlinedSubprograms.empty()) |
979 | return; |
980 | |
981 | OS.AddComment(T: "Inlinee lines subsection" ); |
982 | MCSymbol *InlineEnd = beginCVSubsection(Kind: DebugSubsectionKind::InlineeLines); |
983 | |
984 | // We emit the checksum info for files. This is used by debuggers to |
985 | // determine if a pdb matches the source before loading it. Visual Studio, |
986 | // for instance, will display a warning that the breakpoints are not valid if |
987 | // the pdb does not match the source. |
988 | OS.AddComment(T: "Inlinee lines signature" ); |
989 | OS.emitInt32(Value: unsigned(InlineeLinesSignature::Normal)); |
990 | |
991 | for (const DISubprogram *SP : InlinedSubprograms) { |
992 | assert(TypeIndices.count({SP, nullptr})); |
993 | TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}]; |
994 | |
995 | OS.addBlankLine(); |
996 | unsigned FileId = maybeRecordFile(F: SP->getFile()); |
997 | OS.AddComment(T: "Inlined function " + SP->getName() + " starts at " + |
998 | SP->getFilename() + Twine(':') + Twine(SP->getLine())); |
999 | OS.addBlankLine(); |
1000 | OS.AddComment(T: "Type index of inlined function" ); |
1001 | OS.emitInt32(Value: InlineeIdx.getIndex()); |
1002 | OS.AddComment(T: "Offset into filechecksum table" ); |
1003 | OS.emitCVFileChecksumOffsetDirective(FileNo: FileId); |
1004 | OS.AddComment(T: "Starting line number" ); |
1005 | OS.emitInt32(Value: SP->getLine()); |
1006 | } |
1007 | |
1008 | endCVSubsection(EndLabel: InlineEnd); |
1009 | } |
1010 | |
1011 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
1012 | const DILocation *InlinedAt, |
1013 | const InlineSite &Site) { |
1014 | assert(TypeIndices.count({Site.Inlinee, nullptr})); |
1015 | TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}]; |
1016 | |
1017 | // SymbolRecord |
1018 | MCSymbol *InlineEnd = beginSymbolRecord(Kind: SymbolKind::S_INLINESITE); |
1019 | |
1020 | OS.AddComment(T: "PtrParent" ); |
1021 | OS.emitInt32(Value: 0); |
1022 | OS.AddComment(T: "PtrEnd" ); |
1023 | OS.emitInt32(Value: 0); |
1024 | OS.AddComment(T: "Inlinee type index" ); |
1025 | OS.emitInt32(Value: InlineeIdx.getIndex()); |
1026 | |
1027 | unsigned FileId = maybeRecordFile(F: Site.Inlinee->getFile()); |
1028 | unsigned StartLineNum = Site.Inlinee->getLine(); |
1029 | |
1030 | OS.emitCVInlineLinetableDirective(PrimaryFunctionId: Site.SiteFuncId, SourceFileId: FileId, SourceLineNum: StartLineNum, |
1031 | FnStartSym: FI.Begin, FnEndSym: FI.End); |
1032 | |
1033 | endSymbolRecord(SymEnd: InlineEnd); |
1034 | |
1035 | emitLocalVariableList(FI, Locals: Site.InlinedLocals); |
1036 | |
1037 | // Recurse on child inlined call sites before closing the scope. |
1038 | for (const DILocation *ChildSite : Site.ChildSites) { |
1039 | auto I = FI.InlineSites.find(x: ChildSite); |
1040 | assert(I != FI.InlineSites.end() && |
1041 | "child site not in function inline site map" ); |
1042 | emitInlinedCallSite(FI, InlinedAt: ChildSite, Site: I->second); |
1043 | } |
1044 | |
1045 | // Close the scope. |
1046 | emitEndSymbolRecord(EndKind: SymbolKind::S_INLINESITE_END); |
1047 | } |
1048 | |
1049 | void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) { |
1050 | // If we have a symbol, it may be in a section that is COMDAT. If so, find the |
1051 | // comdat key. A section may be comdat because of -ffunction-sections or |
1052 | // because it is comdat in the IR. |
1053 | MCSectionCOFF *GVSec = |
1054 | GVSym ? dyn_cast<MCSectionCOFF>(Val: &GVSym->getSection()) : nullptr; |
1055 | const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr; |
1056 | |
1057 | MCSectionCOFF *DebugSec = cast<MCSectionCOFF>( |
1058 | Val: CompilerInfoAsm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
1059 | DebugSec = OS.getContext().getAssociativeCOFFSection(Sec: DebugSec, KeySym); |
1060 | |
1061 | OS.switchSection(Section: DebugSec); |
1062 | |
1063 | // Emit the magic version number if this is the first time we've switched to |
1064 | // this section. |
1065 | if (ComdatDebugSections.insert(V: DebugSec).second) |
1066 | emitCodeViewMagicVersion(); |
1067 | } |
1068 | |
1069 | // Emit an S_THUNK32/S_END symbol pair for a thunk routine. |
1070 | // The only supported thunk ordinal is currently the standard type. |
1071 | void CodeViewDebug::emitDebugInfoForThunk(const Function *GV, |
1072 | FunctionInfo &FI, |
1073 | const MCSymbol *Fn) { |
1074 | std::string FuncName = |
1075 | std::string(GlobalValue::dropLLVMManglingEscape(Name: GV->getName())); |
1076 | const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind. |
1077 | |
1078 | OS.AddComment(T: "Symbol subsection for " + Twine(FuncName)); |
1079 | MCSymbol *SymbolsEnd = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
1080 | |
1081 | // Emit S_THUNK32 |
1082 | MCSymbol *ThunkRecordEnd = beginSymbolRecord(Kind: SymbolKind::S_THUNK32); |
1083 | OS.AddComment(T: "PtrParent" ); |
1084 | OS.emitInt32(Value: 0); |
1085 | OS.AddComment(T: "PtrEnd" ); |
1086 | OS.emitInt32(Value: 0); |
1087 | OS.AddComment(T: "PtrNext" ); |
1088 | OS.emitInt32(Value: 0); |
1089 | OS.AddComment(T: "Thunk section relative address" ); |
1090 | OS.emitCOFFSecRel32(Symbol: Fn, /*Offset=*/0); |
1091 | OS.AddComment(T: "Thunk section index" ); |
1092 | OS.emitCOFFSectionIndex(Symbol: Fn); |
1093 | OS.AddComment(T: "Code size" ); |
1094 | OS.emitAbsoluteSymbolDiff(Hi: FI.End, Lo: Fn, Size: 2); |
1095 | OS.AddComment(T: "Ordinal" ); |
1096 | OS.emitInt8(Value: unsigned(ordinal)); |
1097 | OS.AddComment(T: "Function name" ); |
1098 | emitNullTerminatedSymbolName(OS, S: FuncName); |
1099 | // Additional fields specific to the thunk ordinal would go here. |
1100 | endSymbolRecord(SymEnd: ThunkRecordEnd); |
1101 | |
1102 | // Local variables/inlined routines are purposely omitted here. The point of |
1103 | // marking this as a thunk is so Visual Studio will NOT stop in this routine. |
1104 | |
1105 | // Emit S_PROC_ID_END |
1106 | emitEndSymbolRecord(EndKind: SymbolKind::S_PROC_ID_END); |
1107 | |
1108 | endCVSubsection(EndLabel: SymbolsEnd); |
1109 | } |
1110 | |
1111 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
1112 | FunctionInfo &FI) { |
1113 | // For each function there is a separate subsection which holds the PC to |
1114 | // file:line table. |
1115 | const MCSymbol *Fn = Asm->getSymbol(GV); |
1116 | assert(Fn); |
1117 | |
1118 | // Switch to the to a comdat section, if appropriate. |
1119 | switchToDebugSectionForSymbol(GVSym: Fn); |
1120 | |
1121 | std::string FuncName; |
1122 | auto *SP = GV->getSubprogram(); |
1123 | assert(SP); |
1124 | setCurrentSubprogram(SP); |
1125 | |
1126 | if (SP->isThunk()) { |
1127 | emitDebugInfoForThunk(GV, FI, Fn); |
1128 | return; |
1129 | } |
1130 | |
1131 | // If we have a display name, build the fully qualified name by walking the |
1132 | // chain of scopes. |
1133 | if (!SP->getName().empty()) |
1134 | FuncName = getFullyQualifiedName(Scope: SP->getScope(), Name: SP->getName()); |
1135 | |
1136 | // If our DISubprogram name is empty, use the mangled name. |
1137 | if (FuncName.empty()) |
1138 | FuncName = std::string(GlobalValue::dropLLVMManglingEscape(Name: GV->getName())); |
1139 | |
1140 | // Emit FPO data, but only on 32-bit x86. No other platforms use it. |
1141 | if (MMI->getModule()->getTargetTriple().getArch() == Triple::x86) |
1142 | OS.emitCVFPOData(ProcSym: Fn); |
1143 | |
1144 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
1145 | OS.AddComment(T: "Symbol subsection for " + Twine(FuncName)); |
1146 | MCSymbol *SymbolsEnd = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
1147 | { |
1148 | SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID |
1149 | : SymbolKind::S_GPROC32_ID; |
1150 | MCSymbol *ProcRecordEnd = beginSymbolRecord(Kind: ProcKind); |
1151 | |
1152 | // These fields are filled in by tools like CVPACK which run after the fact. |
1153 | OS.AddComment(T: "PtrParent" ); |
1154 | OS.emitInt32(Value: 0); |
1155 | OS.AddComment(T: "PtrEnd" ); |
1156 | OS.emitInt32(Value: 0); |
1157 | OS.AddComment(T: "PtrNext" ); |
1158 | OS.emitInt32(Value: 0); |
1159 | // This is the important bit that tells the debugger where the function |
1160 | // code is located and what's its size: |
1161 | OS.AddComment(T: "Code size" ); |
1162 | OS.emitAbsoluteSymbolDiff(Hi: FI.End, Lo: Fn, Size: 4); |
1163 | OS.AddComment(T: "Offset after prologue" ); |
1164 | OS.emitInt32(Value: 0); |
1165 | OS.AddComment(T: "Offset before epilogue" ); |
1166 | OS.emitInt32(Value: 0); |
1167 | OS.AddComment(T: "Function type index" ); |
1168 | OS.emitInt32(Value: getFuncIdForSubprogram(SP: GV->getSubprogram()).getIndex()); |
1169 | OS.AddComment(T: "Function section relative address" ); |
1170 | OS.emitCOFFSecRel32(Symbol: Fn, /*Offset=*/0); |
1171 | OS.AddComment(T: "Function section index" ); |
1172 | OS.emitCOFFSectionIndex(Symbol: Fn); |
1173 | OS.AddComment(T: "Flags" ); |
1174 | ProcSymFlags ProcFlags = ProcSymFlags::HasOptimizedDebugInfo; |
1175 | if (FI.HasFramePointer) |
1176 | ProcFlags |= ProcSymFlags::HasFP; |
1177 | if (GV->hasFnAttribute(Kind: Attribute::NoReturn)) |
1178 | ProcFlags |= ProcSymFlags::IsNoReturn; |
1179 | if (GV->hasFnAttribute(Kind: Attribute::NoInline)) |
1180 | ProcFlags |= ProcSymFlags::IsNoInline; |
1181 | OS.emitInt8(Value: static_cast<uint8_t>(ProcFlags)); |
1182 | // Emit the function display name as a null-terminated string. |
1183 | OS.AddComment(T: "Function name" ); |
1184 | // Truncate the name so we won't overflow the record length field. |
1185 | emitNullTerminatedSymbolName(OS, S: FuncName); |
1186 | endSymbolRecord(SymEnd: ProcRecordEnd); |
1187 | |
1188 | MCSymbol *FrameProcEnd = beginSymbolRecord(Kind: SymbolKind::S_FRAMEPROC); |
1189 | // Subtract out the CSR size since MSVC excludes that and we include it. |
1190 | OS.AddComment(T: "FrameSize" ); |
1191 | OS.emitInt32(Value: FI.FrameSize - FI.CSRSize); |
1192 | OS.AddComment(T: "Padding" ); |
1193 | OS.emitInt32(Value: 0); |
1194 | OS.AddComment(T: "Offset of padding" ); |
1195 | OS.emitInt32(Value: 0); |
1196 | OS.AddComment(T: "Bytes of callee saved registers" ); |
1197 | OS.emitInt32(Value: FI.CSRSize); |
1198 | OS.AddComment(T: "Exception handler offset" ); |
1199 | OS.emitInt32(Value: 0); |
1200 | OS.AddComment(T: "Exception handler section" ); |
1201 | OS.emitInt16(Value: 0); |
1202 | OS.AddComment(T: "Flags (defines frame register)" ); |
1203 | OS.emitInt32(Value: uint32_t(FI.FrameProcOpts)); |
1204 | endSymbolRecord(SymEnd: FrameProcEnd); |
1205 | |
1206 | emitInlinees(Inlinees: FI.Inlinees); |
1207 | emitLocalVariableList(FI, Locals: FI.Locals); |
1208 | emitGlobalVariableList(Globals: FI.Globals); |
1209 | emitLexicalBlockList(Blocks: FI.ChildBlocks, FI); |
1210 | |
1211 | // Emit inlined call site information. Only emit functions inlined directly |
1212 | // into the parent function. We'll emit the other sites recursively as part |
1213 | // of their parent inline site. |
1214 | for (const DILocation *InlinedAt : FI.ChildSites) { |
1215 | auto I = FI.InlineSites.find(x: InlinedAt); |
1216 | assert(I != FI.InlineSites.end() && |
1217 | "child site not in function inline site map" ); |
1218 | emitInlinedCallSite(FI, InlinedAt, Site: I->second); |
1219 | } |
1220 | |
1221 | for (auto Annot : FI.Annotations) { |
1222 | MCSymbol *Label = Annot.first; |
1223 | MDTuple *Strs = cast<MDTuple>(Val: Annot.second); |
1224 | MCSymbol *AnnotEnd = beginSymbolRecord(Kind: SymbolKind::S_ANNOTATION); |
1225 | OS.emitCOFFSecRel32(Symbol: Label, /*Offset=*/0); |
1226 | // FIXME: Make sure we don't overflow the max record size. |
1227 | OS.emitCOFFSectionIndex(Symbol: Label); |
1228 | OS.emitInt16(Value: Strs->getNumOperands()); |
1229 | for (Metadata *MD : Strs->operands()) { |
1230 | // MDStrings are null terminated, so we can do EmitBytes and get the |
1231 | // nice .asciz directive. |
1232 | StringRef Str = cast<MDString>(Val: MD)->getString(); |
1233 | assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString" ); |
1234 | OS.emitBytes(Data: StringRef(Str.data(), Str.size() + 1)); |
1235 | } |
1236 | endSymbolRecord(SymEnd: AnnotEnd); |
1237 | } |
1238 | |
1239 | for (auto HeapAllocSite : FI.HeapAllocSites) { |
1240 | const MCSymbol *BeginLabel = std::get<0>(t&: HeapAllocSite); |
1241 | const MCSymbol *EndLabel = std::get<1>(t&: HeapAllocSite); |
1242 | const DIType *DITy = std::get<2>(t&: HeapAllocSite); |
1243 | MCSymbol *HeapAllocEnd = beginSymbolRecord(Kind: SymbolKind::S_HEAPALLOCSITE); |
1244 | OS.AddComment(T: "Call site offset" ); |
1245 | OS.emitCOFFSecRel32(Symbol: BeginLabel, /*Offset=*/0); |
1246 | OS.AddComment(T: "Call site section index" ); |
1247 | OS.emitCOFFSectionIndex(Symbol: BeginLabel); |
1248 | OS.AddComment(T: "Call instruction length" ); |
1249 | OS.emitAbsoluteSymbolDiff(Hi: EndLabel, Lo: BeginLabel, Size: 2); |
1250 | OS.AddComment(T: "Type index" ); |
1251 | OS.emitInt32(Value: getCompleteTypeIndex(Ty: DITy).getIndex()); |
1252 | endSymbolRecord(SymEnd: HeapAllocEnd); |
1253 | } |
1254 | |
1255 | if (SP != nullptr) |
1256 | emitDebugInfoForUDTs(UDTs: LocalUDTs); |
1257 | |
1258 | emitDebugInfoForJumpTables(FI); |
1259 | |
1260 | // We're done with this function. |
1261 | emitEndSymbolRecord(EndKind: SymbolKind::S_PROC_ID_END); |
1262 | } |
1263 | endCVSubsection(EndLabel: SymbolsEnd); |
1264 | |
1265 | // We have an assembler directive that takes care of the whole line table. |
1266 | OS.emitCVLinetableDirective(FunctionId: FI.FuncId, FnStart: Fn, FnEnd: FI.End); |
1267 | } |
1268 | |
1269 | CodeViewDebug::LocalVarDef |
1270 | CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) { |
1271 | LocalVarDef DR; |
1272 | DR.InMemory = -1; |
1273 | DR.DataOffset = Offset; |
1274 | assert(DR.DataOffset == Offset && "truncation" ); |
1275 | DR.IsSubfield = 0; |
1276 | DR.StructOffset = 0; |
1277 | DR.CVRegister = CVRegister; |
1278 | return DR; |
1279 | } |
1280 | |
1281 | void CodeViewDebug::collectVariableInfoFromMFTable( |
1282 | DenseSet<InlinedEntity> &Processed) { |
1283 | const MachineFunction &MF = *Asm->MF; |
1284 | const TargetSubtargetInfo &TSI = MF.getSubtarget(); |
1285 | const TargetFrameLowering *TFI = TSI.getFrameLowering(); |
1286 | const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); |
1287 | |
1288 | for (const MachineFunction::VariableDbgInfo &VI : |
1289 | MF.getInStackSlotVariableDbgInfo()) { |
1290 | if (!VI.Var) |
1291 | continue; |
1292 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
1293 | "Expected inlined-at fields to agree" ); |
1294 | |
1295 | Processed.insert(V: InlinedEntity(VI.Var, VI.Loc->getInlinedAt())); |
1296 | LexicalScope *Scope = LScopes.findLexicalScope(DL: VI.Loc); |
1297 | |
1298 | // If variable scope is not found then skip this variable. |
1299 | if (!Scope) |
1300 | continue; |
1301 | |
1302 | // If the variable has an attached offset expression, extract it. |
1303 | // FIXME: Try to handle DW_OP_deref as well. |
1304 | int64_t ExprOffset = 0; |
1305 | bool Deref = false; |
1306 | if (VI.Expr) { |
1307 | // If there is one DW_OP_deref element, use offset of 0 and keep going. |
1308 | if (VI.Expr->getNumElements() == 1 && |
1309 | VI.Expr->getElement(I: 0) == llvm::dwarf::DW_OP_deref) |
1310 | Deref = true; |
1311 | else if (!VI.Expr->extractIfOffset(Offset&: ExprOffset)) |
1312 | continue; |
1313 | } |
1314 | |
1315 | // Get the frame register used and the offset. |
1316 | Register FrameReg; |
1317 | StackOffset FrameOffset = |
1318 | TFI->getFrameIndexReference(MF: *Asm->MF, FI: VI.getStackSlot(), FrameReg); |
1319 | uint16_t CVReg = TRI->getCodeViewRegNum(RegNum: FrameReg); |
1320 | |
1321 | assert(!FrameOffset.getScalable() && |
1322 | "Frame offsets with a scalable component are not supported" ); |
1323 | |
1324 | // Calculate the label ranges. |
1325 | LocalVarDef DefRange = |
1326 | createDefRangeMem(CVRegister: CVReg, Offset: FrameOffset.getFixed() + ExprOffset); |
1327 | |
1328 | LocalVariable Var; |
1329 | Var.DIVar = VI.Var; |
1330 | |
1331 | for (const InsnRange &Range : Scope->getRanges()) { |
1332 | const MCSymbol *Begin = getLabelBeforeInsn(MI: Range.first); |
1333 | const MCSymbol *End = getLabelAfterInsn(MI: Range.second); |
1334 | End = End ? End : Asm->getFunctionEnd(); |
1335 | Var.DefRanges[DefRange].emplace_back(Args&: Begin, Args&: End); |
1336 | } |
1337 | |
1338 | if (Deref) |
1339 | Var.UseReferenceType = true; |
1340 | |
1341 | recordLocalVariable(Var: std::move(Var), LS: Scope); |
1342 | } |
1343 | } |
1344 | |
1345 | static bool canUseReferenceType(const DbgVariableLocation &Loc) { |
1346 | return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0; |
1347 | } |
1348 | |
1349 | static bool needsReferenceType(const DbgVariableLocation &Loc) { |
1350 | return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0; |
1351 | } |
1352 | |
1353 | void CodeViewDebug::calculateRanges( |
1354 | LocalVariable &Var, const DbgValueHistoryMap::Entries &Entries) { |
1355 | const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo(); |
1356 | |
1357 | // Calculate the definition ranges. |
1358 | for (auto I = Entries.begin(), E = Entries.end(); I != E; ++I) { |
1359 | const auto &Entry = *I; |
1360 | if (!Entry.isDbgValue()) |
1361 | continue; |
1362 | const MachineInstr *DVInst = Entry.getInstr(); |
1363 | assert(DVInst->isDebugValue() && "Invalid History entry" ); |
1364 | // FIXME: Find a way to represent constant variables, since they are |
1365 | // relatively common. |
1366 | std::optional<DbgVariableLocation> Location = |
1367 | DbgVariableLocation::extractFromMachineInstruction(Instruction: *DVInst); |
1368 | if (!Location) |
1369 | { |
1370 | // When we don't have a location this is usually because LLVM has |
1371 | // transformed it into a constant and we only have an llvm.dbg.value. We |
1372 | // can't represent these well in CodeView since S_LOCAL only works on |
1373 | // registers and memory locations. Instead, we will pretend this to be a |
1374 | // constant value to at least have it show up in the debugger. |
1375 | auto Op = DVInst->getDebugOperand(Index: 0); |
1376 | if (Op.isImm()) |
1377 | Var.ConstantValue = APSInt(APInt(64, Op.getImm()), false); |
1378 | continue; |
1379 | } |
1380 | |
1381 | // CodeView can only express variables in register and variables in memory |
1382 | // at a constant offset from a register. However, for variables passed |
1383 | // indirectly by pointer, it is common for that pointer to be spilled to a |
1384 | // stack location. For the special case of one offseted load followed by a |
1385 | // zero offset load (a pointer spilled to the stack), we change the type of |
1386 | // the local variable from a value type to a reference type. This tricks the |
1387 | // debugger into doing the load for us. |
1388 | if (Var.UseReferenceType) { |
1389 | // We're using a reference type. Drop the last zero offset load. |
1390 | if (canUseReferenceType(Loc: *Location)) |
1391 | Location->LoadChain.pop_back(); |
1392 | else |
1393 | continue; |
1394 | } else if (needsReferenceType(Loc: *Location)) { |
1395 | // This location can't be expressed without switching to a reference type. |
1396 | // Start over using that. |
1397 | Var.UseReferenceType = true; |
1398 | Var.DefRanges.clear(); |
1399 | calculateRanges(Var, Entries); |
1400 | return; |
1401 | } |
1402 | |
1403 | // We can only handle a register or an offseted load of a register. |
1404 | if (!Location->Register || Location->LoadChain.size() > 1) |
1405 | continue; |
1406 | |
1407 | // Codeview can only express byte-aligned offsets, ensure that we have a |
1408 | // byte-boundaried location. |
1409 | if (Location->FragmentInfo) |
1410 | if (Location->FragmentInfo->OffsetInBits % 8) |
1411 | continue; |
1412 | |
1413 | LocalVarDef DR; |
1414 | DR.CVRegister = TRI->getCodeViewRegNum(RegNum: Location->Register); |
1415 | DR.InMemory = !Location->LoadChain.empty(); |
1416 | DR.DataOffset = |
1417 | !Location->LoadChain.empty() ? Location->LoadChain.back() : 0; |
1418 | if (Location->FragmentInfo) { |
1419 | DR.IsSubfield = true; |
1420 | DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8; |
1421 | } else { |
1422 | DR.IsSubfield = false; |
1423 | DR.StructOffset = 0; |
1424 | } |
1425 | |
1426 | // Compute the label range. |
1427 | const MCSymbol *Begin = getLabelBeforeInsn(MI: Entry.getInstr()); |
1428 | const MCSymbol *End; |
1429 | if (Entry.getEndIndex() != DbgValueHistoryMap::NoEntry) { |
1430 | auto &EndingEntry = Entries[Entry.getEndIndex()]; |
1431 | End = EndingEntry.isDbgValue() |
1432 | ? getLabelBeforeInsn(MI: EndingEntry.getInstr()) |
1433 | : getLabelAfterInsn(MI: EndingEntry.getInstr()); |
1434 | } else |
1435 | End = Asm->getFunctionEnd(); |
1436 | |
1437 | // If the last range end is our begin, just extend the last range. |
1438 | // Otherwise make a new range. |
1439 | SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R = |
1440 | Var.DefRanges[DR]; |
1441 | if (!R.empty() && R.back().second == Begin) |
1442 | R.back().second = End; |
1443 | else |
1444 | R.emplace_back(Args&: Begin, Args&: End); |
1445 | |
1446 | // FIXME: Do more range combining. |
1447 | } |
1448 | } |
1449 | |
1450 | void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) { |
1451 | DenseSet<InlinedEntity> Processed; |
1452 | // Grab the variable info that was squirreled away in the MMI side-table. |
1453 | collectVariableInfoFromMFTable(Processed); |
1454 | |
1455 | for (const auto &I : DbgValues) { |
1456 | InlinedEntity IV = I.first; |
1457 | if (Processed.count(V: IV)) |
1458 | continue; |
1459 | const DILocalVariable *DIVar = cast<DILocalVariable>(Val: IV.first); |
1460 | const DILocation *InlinedAt = IV.second; |
1461 | |
1462 | // Instruction ranges, specifying where IV is accessible. |
1463 | const auto &Entries = I.second; |
1464 | |
1465 | LexicalScope *Scope = nullptr; |
1466 | if (InlinedAt) |
1467 | Scope = LScopes.findInlinedScope(N: DIVar->getScope(), IA: InlinedAt); |
1468 | else |
1469 | Scope = LScopes.findLexicalScope(N: DIVar->getScope()); |
1470 | // If variable scope is not found then skip this variable. |
1471 | if (!Scope) |
1472 | continue; |
1473 | |
1474 | LocalVariable Var; |
1475 | Var.DIVar = DIVar; |
1476 | |
1477 | calculateRanges(Var, Entries); |
1478 | recordLocalVariable(Var: std::move(Var), LS: Scope); |
1479 | } |
1480 | } |
1481 | |
1482 | void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) { |
1483 | const TargetSubtargetInfo &TSI = MF->getSubtarget(); |
1484 | const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); |
1485 | const MachineFrameInfo &MFI = MF->getFrameInfo(); |
1486 | const Function &GV = MF->getFunction(); |
1487 | auto Insertion = FnDebugInfo.insert(KV: {&GV, std::make_unique<FunctionInfo>()}); |
1488 | assert(Insertion.second && "function already has info" ); |
1489 | CurFn = Insertion.first->second.get(); |
1490 | CurFn->FuncId = NextFuncId++; |
1491 | CurFn->Begin = Asm->getFunctionBegin(); |
1492 | |
1493 | // The S_FRAMEPROC record reports the stack size, and how many bytes of |
1494 | // callee-saved registers were used. For targets that don't use a PUSH |
1495 | // instruction (AArch64), this will be zero. |
1496 | CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters(); |
1497 | CurFn->FrameSize = MFI.getStackSize(); |
1498 | CurFn->OffsetAdjustment = MFI.getOffsetAdjustment(); |
1499 | CurFn->HasStackRealignment = TRI->hasStackRealignment(MF: *MF); |
1500 | |
1501 | // For this function S_FRAMEPROC record, figure out which codeview register |
1502 | // will be the frame pointer. |
1503 | CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None. |
1504 | CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None. |
1505 | if (CurFn->FrameSize > 0) { |
1506 | if (!TSI.getFrameLowering()->hasFP(MF: *MF)) { |
1507 | CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr; |
1508 | CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr; |
1509 | } else { |
1510 | CurFn->HasFramePointer = true; |
1511 | // If there is an FP, parameters are always relative to it. |
1512 | CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr; |
1513 | if (CurFn->HasStackRealignment) { |
1514 | // If the stack needs realignment, locals are relative to SP or VFRAME. |
1515 | CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr; |
1516 | } else { |
1517 | // Otherwise, locals are relative to EBP, and we probably have VLAs or |
1518 | // other stack adjustments. |
1519 | CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr; |
1520 | } |
1521 | } |
1522 | } |
1523 | |
1524 | // Compute other frame procedure options. |
1525 | FrameProcedureOptions FPO = FrameProcedureOptions::None; |
1526 | if (MFI.hasVarSizedObjects()) |
1527 | FPO |= FrameProcedureOptions::HasAlloca; |
1528 | if (MF->exposesReturnsTwice()) |
1529 | FPO |= FrameProcedureOptions::HasSetJmp; |
1530 | // FIXME: Set HasLongJmp if we ever track that info. |
1531 | if (MF->hasInlineAsm()) |
1532 | FPO |= FrameProcedureOptions::HasInlineAssembly; |
1533 | if (GV.hasPersonalityFn()) { |
1534 | if (isAsynchronousEHPersonality( |
1535 | Pers: classifyEHPersonality(Pers: GV.getPersonalityFn()))) |
1536 | FPO |= FrameProcedureOptions::HasStructuredExceptionHandling; |
1537 | else |
1538 | FPO |= FrameProcedureOptions::HasExceptionHandling; |
1539 | } |
1540 | if (GV.hasFnAttribute(Kind: Attribute::InlineHint)) |
1541 | FPO |= FrameProcedureOptions::MarkedInline; |
1542 | if (GV.hasFnAttribute(Kind: Attribute::Naked)) |
1543 | FPO |= FrameProcedureOptions::Naked; |
1544 | if (MFI.hasStackProtectorIndex()) { |
1545 | FPO |= FrameProcedureOptions::SecurityChecks; |
1546 | if (GV.hasFnAttribute(Kind: Attribute::StackProtectStrong) || |
1547 | GV.hasFnAttribute(Kind: Attribute::StackProtectReq)) { |
1548 | FPO |= FrameProcedureOptions::StrictSecurityChecks; |
1549 | } |
1550 | } else if (!GV.hasStackProtectorFnAttr()) { |
1551 | // __declspec(safebuffers) disables stack guards. |
1552 | FPO |= FrameProcedureOptions::SafeBuffers; |
1553 | } |
1554 | FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U); |
1555 | FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U); |
1556 | if (Asm->TM.getOptLevel() != CodeGenOptLevel::None && !GV.hasOptSize() && |
1557 | !GV.hasOptNone()) |
1558 | FPO |= FrameProcedureOptions::OptimizedForSpeed; |
1559 | if (GV.hasProfileData()) { |
1560 | FPO |= FrameProcedureOptions::ValidProfileCounts; |
1561 | FPO |= FrameProcedureOptions::ProfileGuidedOptimization; |
1562 | } |
1563 | // FIXME: Set GuardCfg when it is implemented. |
1564 | CurFn->FrameProcOpts = FPO; |
1565 | |
1566 | OS.emitCVFuncIdDirective(FunctionId: CurFn->FuncId); |
1567 | |
1568 | // Find the end of the function prolog. First known non-DBG_VALUE and |
1569 | // non-frame setup location marks the beginning of the function body. |
1570 | // FIXME: is there a simpler a way to do this? Can we just search |
1571 | // for the first instruction of the function, not the last of the prolog? |
1572 | DebugLoc PrologEndLoc; |
1573 | bool EmptyPrologue = true; |
1574 | for (const auto &MBB : *MF) { |
1575 | for (const auto &MI : MBB) { |
1576 | if (!MI.isMetaInstruction() && !MI.getFlag(Flag: MachineInstr::FrameSetup) && |
1577 | MI.getDebugLoc()) { |
1578 | PrologEndLoc = MI.getDebugLoc(); |
1579 | break; |
1580 | } else if (!MI.isMetaInstruction()) { |
1581 | EmptyPrologue = false; |
1582 | } |
1583 | } |
1584 | } |
1585 | |
1586 | // Record beginning of function if we have a non-empty prologue. |
1587 | if (PrologEndLoc && !EmptyPrologue) { |
1588 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
1589 | maybeRecordLocation(DL: FnStartDL, MF); |
1590 | } |
1591 | |
1592 | // Find heap alloc sites and emit labels around them. |
1593 | for (const auto &MBB : *MF) { |
1594 | for (const auto &MI : MBB) { |
1595 | if (MI.getHeapAllocMarker()) { |
1596 | requestLabelBeforeInsn(MI: &MI); |
1597 | requestLabelAfterInsn(MI: &MI); |
1598 | } |
1599 | } |
1600 | } |
1601 | |
1602 | // Mark branches that may potentially be using jump tables with labels. |
1603 | bool isThumb = MMI->getModule()->getTargetTriple().getArch() == |
1604 | llvm::Triple::ArchType::thumb; |
1605 | discoverJumpTableBranches(MF, isThumb); |
1606 | } |
1607 | |
1608 | static bool shouldEmitUdt(const DIType *T) { |
1609 | if (!T) |
1610 | return false; |
1611 | |
1612 | // MSVC does not emit UDTs for typedefs that are scoped to classes. |
1613 | if (T->getTag() == dwarf::DW_TAG_typedef) { |
1614 | if (DIScope *Scope = T->getScope()) { |
1615 | switch (Scope->getTag()) { |
1616 | case dwarf::DW_TAG_structure_type: |
1617 | case dwarf::DW_TAG_class_type: |
1618 | case dwarf::DW_TAG_union_type: |
1619 | return false; |
1620 | default: |
1621 | // do nothing. |
1622 | ; |
1623 | } |
1624 | } |
1625 | } |
1626 | |
1627 | while (true) { |
1628 | if (!T || T->isForwardDecl()) |
1629 | return false; |
1630 | |
1631 | const DIDerivedType *DT = dyn_cast<DIDerivedType>(Val: T); |
1632 | if (!DT) |
1633 | return true; |
1634 | T = DT->getBaseType(); |
1635 | } |
1636 | return true; |
1637 | } |
1638 | |
1639 | void CodeViewDebug::addToUDTs(const DIType *Ty) { |
1640 | // Don't record empty UDTs. |
1641 | if (Ty->getName().empty()) |
1642 | return; |
1643 | if (!shouldEmitUdt(T: Ty)) |
1644 | return; |
1645 | |
1646 | SmallVector<StringRef, 5> ParentScopeNames; |
1647 | const DISubprogram *ClosestSubprogram = |
1648 | collectParentScopeNames(Scope: Ty->getScope(), QualifiedNameComponents&: ParentScopeNames); |
1649 | |
1650 | std::string FullyQualifiedName = |
1651 | formatNestedName(QualifiedNameComponents: ParentScopeNames, TypeName: getPrettyScopeName(Scope: Ty)); |
1652 | |
1653 | if (ClosestSubprogram == nullptr) { |
1654 | GlobalUDTs.emplace_back(args: std::move(FullyQualifiedName), args&: Ty); |
1655 | } else if (ClosestSubprogram == CurrentSubprogram) { |
1656 | LocalUDTs.emplace_back(args: std::move(FullyQualifiedName), args&: Ty); |
1657 | } |
1658 | |
1659 | // TODO: What if the ClosestSubprogram is neither null or the current |
1660 | // subprogram? Currently, the UDT just gets dropped on the floor. |
1661 | // |
1662 | // The current behavior is not desirable. To get maximal fidelity, we would |
1663 | // need to perform all type translation before beginning emission of .debug$S |
1664 | // and then make LocalUDTs a member of FunctionInfo |
1665 | } |
1666 | |
1667 | TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) { |
1668 | // Generic dispatch for lowering an unknown type. |
1669 | switch (Ty->getTag()) { |
1670 | case dwarf::DW_TAG_array_type: |
1671 | return lowerTypeArray(Ty: cast<DICompositeType>(Val: Ty)); |
1672 | case dwarf::DW_TAG_typedef: |
1673 | return lowerTypeAlias(Ty: cast<DIDerivedType>(Val: Ty)); |
1674 | case dwarf::DW_TAG_base_type: |
1675 | return lowerTypeBasic(Ty: cast<DIBasicType>(Val: Ty)); |
1676 | case dwarf::DW_TAG_pointer_type: |
1677 | if (cast<DIDerivedType>(Val: Ty)->getName() == "__vtbl_ptr_type" ) |
1678 | return lowerTypeVFTableShape(Ty: cast<DIDerivedType>(Val: Ty)); |
1679 | [[fallthrough]]; |
1680 | case dwarf::DW_TAG_reference_type: |
1681 | case dwarf::DW_TAG_rvalue_reference_type: |
1682 | return lowerTypePointer(Ty: cast<DIDerivedType>(Val: Ty)); |
1683 | case dwarf::DW_TAG_ptr_to_member_type: |
1684 | return lowerTypeMemberPointer(Ty: cast<DIDerivedType>(Val: Ty)); |
1685 | case dwarf::DW_TAG_restrict_type: |
1686 | case dwarf::DW_TAG_const_type: |
1687 | case dwarf::DW_TAG_volatile_type: |
1688 | // TODO: add support for DW_TAG_atomic_type here |
1689 | return lowerTypeModifier(Ty: cast<DIDerivedType>(Val: Ty)); |
1690 | case dwarf::DW_TAG_subroutine_type: |
1691 | if (ClassTy) { |
1692 | // The member function type of a member function pointer has no |
1693 | // ThisAdjustment. |
1694 | return lowerTypeMemberFunction(Ty: cast<DISubroutineType>(Val: Ty), ClassTy, |
1695 | /*ThisAdjustment=*/0, |
1696 | /*IsStaticMethod=*/false); |
1697 | } |
1698 | return lowerTypeFunction(Ty: cast<DISubroutineType>(Val: Ty)); |
1699 | case dwarf::DW_TAG_enumeration_type: |
1700 | return lowerTypeEnum(Ty: cast<DICompositeType>(Val: Ty)); |
1701 | case dwarf::DW_TAG_class_type: |
1702 | case dwarf::DW_TAG_structure_type: |
1703 | return lowerTypeClass(Ty: cast<DICompositeType>(Val: Ty)); |
1704 | case dwarf::DW_TAG_union_type: |
1705 | return lowerTypeUnion(Ty: cast<DICompositeType>(Val: Ty)); |
1706 | case dwarf::DW_TAG_string_type: |
1707 | return lowerTypeString(Ty: cast<DIStringType>(Val: Ty)); |
1708 | case dwarf::DW_TAG_unspecified_type: |
1709 | if (Ty->getName() == "decltype(nullptr)" ) |
1710 | return TypeIndex::NullptrT(); |
1711 | return TypeIndex::None(); |
1712 | default: |
1713 | // Use the null type index. |
1714 | return TypeIndex(); |
1715 | } |
1716 | } |
1717 | |
1718 | TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) { |
1719 | TypeIndex UnderlyingTypeIndex = getTypeIndex(Ty: Ty->getBaseType()); |
1720 | StringRef TypeName = Ty->getName(); |
1721 | |
1722 | addToUDTs(Ty); |
1723 | |
1724 | if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) && |
1725 | TypeName == "HRESULT" ) |
1726 | return TypeIndex(SimpleTypeKind::HResult); |
1727 | if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) && |
1728 | TypeName == "wchar_t" ) |
1729 | return TypeIndex(SimpleTypeKind::WideCharacter); |
1730 | |
1731 | return UnderlyingTypeIndex; |
1732 | } |
1733 | |
1734 | TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) { |
1735 | const DIType *ElementType = Ty->getBaseType(); |
1736 | TypeIndex ElementTypeIndex = getTypeIndex(Ty: ElementType); |
1737 | // IndexType is size_t, which depends on the bitness of the target. |
1738 | TypeIndex IndexType = getPointerSizeInBytes() == 8 |
1739 | ? TypeIndex(SimpleTypeKind::UInt64Quad) |
1740 | : TypeIndex(SimpleTypeKind::UInt32Long); |
1741 | |
1742 | uint64_t ElementSize = getBaseTypeSize(Ty: ElementType) / 8; |
1743 | |
1744 | // Add subranges to array type. |
1745 | DINodeArray Elements = Ty->getElements(); |
1746 | for (int i = Elements.size() - 1; i >= 0; --i) { |
1747 | const DINode *Element = Elements[i]; |
1748 | assert(Element->getTag() == dwarf::DW_TAG_subrange_type); |
1749 | |
1750 | const DISubrange *Subrange = cast<DISubrange>(Val: Element); |
1751 | int64_t Count = -1; |
1752 | |
1753 | // If Subrange has a Count field, use it. |
1754 | // Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1), |
1755 | // where lowerbound is from the LowerBound field of the Subrange, |
1756 | // or the language default lowerbound if that field is unspecified. |
1757 | if (auto *CI = dyn_cast_if_present<ConstantInt *>(Val: Subrange->getCount())) |
1758 | Count = CI->getSExtValue(); |
1759 | else if (auto *UI = dyn_cast_if_present<ConstantInt *>( |
1760 | Val: Subrange->getUpperBound())) { |
1761 | // Fortran uses 1 as the default lowerbound; other languages use 0. |
1762 | int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0; |
1763 | auto *LI = dyn_cast_if_present<ConstantInt *>(Val: Subrange->getLowerBound()); |
1764 | Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound; |
1765 | Count = UI->getSExtValue() - Lowerbound + 1; |
1766 | } |
1767 | |
1768 | // Forward declarations of arrays without a size and VLAs use a count of -1. |
1769 | // Emit a count of zero in these cases to match what MSVC does for arrays |
1770 | // without a size. MSVC doesn't support VLAs, so it's not clear what we |
1771 | // should do for them even if we could distinguish them. |
1772 | if (Count == -1) |
1773 | Count = 0; |
1774 | |
1775 | // Update the element size and element type index for subsequent subranges. |
1776 | ElementSize *= Count; |
1777 | |
1778 | // If this is the outermost array, use the size from the array. It will be |
1779 | // more accurate if we had a VLA or an incomplete element type size. |
1780 | uint64_t ArraySize = |
1781 | (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize; |
1782 | |
1783 | StringRef Name = (i == 0) ? Ty->getName() : "" ; |
1784 | ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name); |
1785 | ElementTypeIndex = TypeTable.writeLeafType(Record&: AR); |
1786 | } |
1787 | |
1788 | return ElementTypeIndex; |
1789 | } |
1790 | |
1791 | // This function lowers a Fortran character type (DIStringType). |
1792 | // Note that it handles only the character*n variant (using SizeInBits |
1793 | // field in DIString to describe the type size) at the moment. |
1794 | // Other variants (leveraging the StringLength and StringLengthExp |
1795 | // fields in DIStringType) remain TBD. |
1796 | TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) { |
1797 | TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter); |
1798 | uint64_t ArraySize = Ty->getSizeInBits() >> 3; |
1799 | StringRef Name = Ty->getName(); |
1800 | // IndexType is size_t, which depends on the bitness of the target. |
1801 | TypeIndex IndexType = getPointerSizeInBytes() == 8 |
1802 | ? TypeIndex(SimpleTypeKind::UInt64Quad) |
1803 | : TypeIndex(SimpleTypeKind::UInt32Long); |
1804 | |
1805 | // Create a type of character array of ArraySize. |
1806 | ArrayRecord AR(CharType, IndexType, ArraySize, Name); |
1807 | |
1808 | return TypeTable.writeLeafType(Record&: AR); |
1809 | } |
1810 | |
1811 | TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) { |
1812 | TypeIndex Index; |
1813 | dwarf::TypeKind Kind; |
1814 | uint32_t ByteSize; |
1815 | |
1816 | Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding()); |
1817 | ByteSize = Ty->getSizeInBits() / 8; |
1818 | |
1819 | SimpleTypeKind STK = SimpleTypeKind::None; |
1820 | switch (Kind) { |
1821 | case dwarf::DW_ATE_address: |
1822 | // FIXME: Translate |
1823 | break; |
1824 | case dwarf::DW_ATE_boolean: |
1825 | switch (ByteSize) { |
1826 | case 1: STK = SimpleTypeKind::Boolean8; break; |
1827 | case 2: STK = SimpleTypeKind::Boolean16; break; |
1828 | case 4: STK = SimpleTypeKind::Boolean32; break; |
1829 | case 8: STK = SimpleTypeKind::Boolean64; break; |
1830 | case 16: STK = SimpleTypeKind::Boolean128; break; |
1831 | } |
1832 | break; |
1833 | case dwarf::DW_ATE_complex_float: |
1834 | // The CodeView size for a complex represents the size of |
1835 | // an individual component. |
1836 | switch (ByteSize) { |
1837 | case 4: STK = SimpleTypeKind::Complex16; break; |
1838 | case 8: STK = SimpleTypeKind::Complex32; break; |
1839 | case 16: STK = SimpleTypeKind::Complex64; break; |
1840 | case 20: STK = SimpleTypeKind::Complex80; break; |
1841 | case 32: STK = SimpleTypeKind::Complex128; break; |
1842 | } |
1843 | break; |
1844 | case dwarf::DW_ATE_float: |
1845 | switch (ByteSize) { |
1846 | case 2: STK = SimpleTypeKind::Float16; break; |
1847 | case 4: STK = SimpleTypeKind::Float32; break; |
1848 | case 6: STK = SimpleTypeKind::Float48; break; |
1849 | case 8: STK = SimpleTypeKind::Float64; break; |
1850 | case 10: STK = SimpleTypeKind::Float80; break; |
1851 | case 16: STK = SimpleTypeKind::Float128; break; |
1852 | } |
1853 | break; |
1854 | case dwarf::DW_ATE_signed: |
1855 | switch (ByteSize) { |
1856 | case 1: STK = SimpleTypeKind::SignedCharacter; break; |
1857 | case 2: STK = SimpleTypeKind::Int16Short; break; |
1858 | case 4: STK = SimpleTypeKind::Int32; break; |
1859 | case 8: STK = SimpleTypeKind::Int64Quad; break; |
1860 | case 16: STK = SimpleTypeKind::Int128Oct; break; |
1861 | } |
1862 | break; |
1863 | case dwarf::DW_ATE_unsigned: |
1864 | switch (ByteSize) { |
1865 | case 1: STK = SimpleTypeKind::UnsignedCharacter; break; |
1866 | case 2: STK = SimpleTypeKind::UInt16Short; break; |
1867 | case 4: STK = SimpleTypeKind::UInt32; break; |
1868 | case 8: STK = SimpleTypeKind::UInt64Quad; break; |
1869 | case 16: STK = SimpleTypeKind::UInt128Oct; break; |
1870 | } |
1871 | break; |
1872 | case dwarf::DW_ATE_UTF: |
1873 | switch (ByteSize) { |
1874 | case 1: STK = SimpleTypeKind::Character8; break; |
1875 | case 2: STK = SimpleTypeKind::Character16; break; |
1876 | case 4: STK = SimpleTypeKind::Character32; break; |
1877 | } |
1878 | break; |
1879 | case dwarf::DW_ATE_signed_char: |
1880 | if (ByteSize == 1) |
1881 | STK = SimpleTypeKind::SignedCharacter; |
1882 | break; |
1883 | case dwarf::DW_ATE_unsigned_char: |
1884 | if (ByteSize == 1) |
1885 | STK = SimpleTypeKind::UnsignedCharacter; |
1886 | break; |
1887 | default: |
1888 | break; |
1889 | } |
1890 | |
1891 | // Apply some fixups based on the source-level type name. |
1892 | // Include some amount of canonicalization from an old naming scheme Clang |
1893 | // used to use for integer types (in an outdated effort to be compatible with |
1894 | // GCC's debug info/GDB's behavior, which has since been addressed). |
1895 | if (STK == SimpleTypeKind::Int32 && |
1896 | (Ty->getName() == "long int" || Ty->getName() == "long" )) |
1897 | STK = SimpleTypeKind::Int32Long; |
1898 | if (STK == SimpleTypeKind::UInt32 && (Ty->getName() == "long unsigned int" || |
1899 | Ty->getName() == "unsigned long" )) |
1900 | STK = SimpleTypeKind::UInt32Long; |
1901 | if (STK == SimpleTypeKind::UInt16Short && |
1902 | (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t" )) |
1903 | STK = SimpleTypeKind::WideCharacter; |
1904 | if ((STK == SimpleTypeKind::SignedCharacter || |
1905 | STK == SimpleTypeKind::UnsignedCharacter) && |
1906 | Ty->getName() == "char" ) |
1907 | STK = SimpleTypeKind::NarrowCharacter; |
1908 | |
1909 | return TypeIndex(STK); |
1910 | } |
1911 | |
1912 | TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty, |
1913 | PointerOptions PO) { |
1914 | TypeIndex PointeeTI = getTypeIndex(Ty: Ty->getBaseType()); |
1915 | |
1916 | // Pointers to simple types without any options can use SimpleTypeMode, rather |
1917 | // than having a dedicated pointer type record. |
1918 | if (PointeeTI.isSimple() && PO == PointerOptions::None && |
1919 | PointeeTI.getSimpleMode() == SimpleTypeMode::Direct && |
1920 | Ty->getTag() == dwarf::DW_TAG_pointer_type) { |
1921 | SimpleTypeMode Mode = Ty->getSizeInBits() == 64 |
1922 | ? SimpleTypeMode::NearPointer64 |
1923 | : SimpleTypeMode::NearPointer32; |
1924 | return TypeIndex(PointeeTI.getSimpleKind(), Mode); |
1925 | } |
1926 | |
1927 | PointerKind PK = |
1928 | Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32; |
1929 | PointerMode PM = PointerMode::Pointer; |
1930 | switch (Ty->getTag()) { |
1931 | default: llvm_unreachable("not a pointer tag type" ); |
1932 | case dwarf::DW_TAG_pointer_type: |
1933 | PM = PointerMode::Pointer; |
1934 | break; |
1935 | case dwarf::DW_TAG_reference_type: |
1936 | PM = PointerMode::LValueReference; |
1937 | break; |
1938 | case dwarf::DW_TAG_rvalue_reference_type: |
1939 | PM = PointerMode::RValueReference; |
1940 | break; |
1941 | } |
1942 | |
1943 | if (Ty->isObjectPointer()) |
1944 | PO |= PointerOptions::Const; |
1945 | |
1946 | PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8); |
1947 | return TypeTable.writeLeafType(Record&: PR); |
1948 | } |
1949 | |
1950 | static PointerToMemberRepresentation |
1951 | translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) { |
1952 | // SizeInBytes being zero generally implies that the member pointer type was |
1953 | // incomplete, which can happen if it is part of a function prototype. In this |
1954 | // case, use the unknown model instead of the general model. |
1955 | if (IsPMF) { |
1956 | switch (Flags & DINode::FlagPtrToMemberRep) { |
1957 | case 0: |
1958 | return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown |
1959 | : PointerToMemberRepresentation::GeneralFunction; |
1960 | case DINode::FlagSingleInheritance: |
1961 | return PointerToMemberRepresentation::SingleInheritanceFunction; |
1962 | case DINode::FlagMultipleInheritance: |
1963 | return PointerToMemberRepresentation::MultipleInheritanceFunction; |
1964 | case DINode::FlagVirtualInheritance: |
1965 | return PointerToMemberRepresentation::VirtualInheritanceFunction; |
1966 | } |
1967 | } else { |
1968 | switch (Flags & DINode::FlagPtrToMemberRep) { |
1969 | case 0: |
1970 | return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown |
1971 | : PointerToMemberRepresentation::GeneralData; |
1972 | case DINode::FlagSingleInheritance: |
1973 | return PointerToMemberRepresentation::SingleInheritanceData; |
1974 | case DINode::FlagMultipleInheritance: |
1975 | return PointerToMemberRepresentation::MultipleInheritanceData; |
1976 | case DINode::FlagVirtualInheritance: |
1977 | return PointerToMemberRepresentation::VirtualInheritanceData; |
1978 | } |
1979 | } |
1980 | llvm_unreachable("invalid ptr to member representation" ); |
1981 | } |
1982 | |
1983 | TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty, |
1984 | PointerOptions PO) { |
1985 | assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type); |
1986 | bool IsPMF = isa<DISubroutineType>(Val: Ty->getBaseType()); |
1987 | TypeIndex ClassTI = getTypeIndex(Ty: Ty->getClassType()); |
1988 | TypeIndex PointeeTI = |
1989 | getTypeIndex(Ty: Ty->getBaseType(), ClassTy: IsPMF ? Ty->getClassType() : nullptr); |
1990 | PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64 |
1991 | : PointerKind::Near32; |
1992 | PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction |
1993 | : PointerMode::PointerToDataMember; |
1994 | |
1995 | assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big" ); |
1996 | uint8_t SizeInBytes = Ty->getSizeInBits() / 8; |
1997 | MemberPointerInfo MPI( |
1998 | ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Flags: Ty->getFlags())); |
1999 | PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI); |
2000 | return TypeTable.writeLeafType(Record&: PR); |
2001 | } |
2002 | |
2003 | /// Given a DWARF calling convention, get the CodeView equivalent. If we don't |
2004 | /// have a translation, use the NearC convention. |
2005 | static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) { |
2006 | switch (DwarfCC) { |
2007 | case dwarf::DW_CC_normal: return CallingConvention::NearC; |
2008 | case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast; |
2009 | case dwarf::DW_CC_BORLAND_thiscall: return CallingConvention::ThisCall; |
2010 | case dwarf::DW_CC_BORLAND_stdcall: return CallingConvention::NearStdCall; |
2011 | case dwarf::DW_CC_BORLAND_pascal: return CallingConvention::NearPascal; |
2012 | case dwarf::DW_CC_LLVM_vectorcall: return CallingConvention::NearVector; |
2013 | } |
2014 | return CallingConvention::NearC; |
2015 | } |
2016 | |
2017 | TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) { |
2018 | ModifierOptions Mods = ModifierOptions::None; |
2019 | PointerOptions PO = PointerOptions::None; |
2020 | bool IsModifier = true; |
2021 | const DIType *BaseTy = Ty; |
2022 | while (IsModifier && BaseTy) { |
2023 | // FIXME: Need to add DWARF tags for __unaligned and _Atomic |
2024 | switch (BaseTy->getTag()) { |
2025 | case dwarf::DW_TAG_const_type: |
2026 | Mods |= ModifierOptions::Const; |
2027 | PO |= PointerOptions::Const; |
2028 | break; |
2029 | case dwarf::DW_TAG_volatile_type: |
2030 | Mods |= ModifierOptions::Volatile; |
2031 | PO |= PointerOptions::Volatile; |
2032 | break; |
2033 | case dwarf::DW_TAG_restrict_type: |
2034 | // Only pointer types be marked with __restrict. There is no known flag |
2035 | // for __restrict in LF_MODIFIER records. |
2036 | PO |= PointerOptions::Restrict; |
2037 | break; |
2038 | default: |
2039 | IsModifier = false; |
2040 | break; |
2041 | } |
2042 | if (IsModifier) |
2043 | BaseTy = cast<DIDerivedType>(Val: BaseTy)->getBaseType(); |
2044 | } |
2045 | |
2046 | // Check if the inner type will use an LF_POINTER record. If so, the |
2047 | // qualifiers will go in the LF_POINTER record. This comes up for types like |
2048 | // 'int *const' and 'int *__restrict', not the more common cases like 'const |
2049 | // char *'. |
2050 | if (BaseTy) { |
2051 | switch (BaseTy->getTag()) { |
2052 | case dwarf::DW_TAG_pointer_type: |
2053 | case dwarf::DW_TAG_reference_type: |
2054 | case dwarf::DW_TAG_rvalue_reference_type: |
2055 | return lowerTypePointer(Ty: cast<DIDerivedType>(Val: BaseTy), PO); |
2056 | case dwarf::DW_TAG_ptr_to_member_type: |
2057 | return lowerTypeMemberPointer(Ty: cast<DIDerivedType>(Val: BaseTy), PO); |
2058 | default: |
2059 | break; |
2060 | } |
2061 | } |
2062 | |
2063 | TypeIndex ModifiedTI = getTypeIndex(Ty: BaseTy); |
2064 | |
2065 | // Return the base type index if there aren't any modifiers. For example, the |
2066 | // metadata could contain restrict wrappers around non-pointer types. |
2067 | if (Mods == ModifierOptions::None) |
2068 | return ModifiedTI; |
2069 | |
2070 | ModifierRecord MR(ModifiedTI, Mods); |
2071 | return TypeTable.writeLeafType(Record&: MR); |
2072 | } |
2073 | |
2074 | TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) { |
2075 | SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices; |
2076 | for (const DIType *ArgType : Ty->getTypeArray()) |
2077 | ReturnAndArgTypeIndices.push_back(Elt: getTypeIndex(Ty: ArgType)); |
2078 | |
2079 | // MSVC uses type none for variadic argument. |
2080 | if (ReturnAndArgTypeIndices.size() > 1 && |
2081 | ReturnAndArgTypeIndices.back() == TypeIndex::Void()) { |
2082 | ReturnAndArgTypeIndices.back() = TypeIndex::None(); |
2083 | } |
2084 | TypeIndex ReturnTypeIndex = TypeIndex::Void(); |
2085 | ArrayRef<TypeIndex> ArgTypeIndices = {}; |
2086 | if (!ReturnAndArgTypeIndices.empty()) { |
2087 | auto ReturnAndArgTypesRef = ArrayRef(ReturnAndArgTypeIndices); |
2088 | ReturnTypeIndex = ReturnAndArgTypesRef.front(); |
2089 | ArgTypeIndices = ReturnAndArgTypesRef.drop_front(); |
2090 | } |
2091 | |
2092 | ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices); |
2093 | TypeIndex ArgListIndex = TypeTable.writeLeafType(Record&: ArgListRec); |
2094 | |
2095 | CallingConvention CC = dwarfCCToCodeView(DwarfCC: Ty->getCC()); |
2096 | |
2097 | FunctionOptions FO = getFunctionOptions(Ty); |
2098 | ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(), |
2099 | ArgListIndex); |
2100 | return TypeTable.writeLeafType(Record&: Procedure); |
2101 | } |
2102 | |
2103 | TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty, |
2104 | const DIType *ClassTy, |
2105 | int ThisAdjustment, |
2106 | bool IsStaticMethod, |
2107 | FunctionOptions FO) { |
2108 | // Lower the containing class type. |
2109 | TypeIndex ClassType = getTypeIndex(Ty: ClassTy); |
2110 | |
2111 | DITypeRefArray ReturnAndArgs = Ty->getTypeArray(); |
2112 | |
2113 | unsigned Index = 0; |
2114 | SmallVector<TypeIndex, 8> ArgTypeIndices; |
2115 | TypeIndex ReturnTypeIndex = TypeIndex::Void(); |
2116 | if (ReturnAndArgs.size() > Index) { |
2117 | ReturnTypeIndex = getTypeIndex(Ty: ReturnAndArgs[Index++]); |
2118 | } |
2119 | |
2120 | // If the first argument is a pointer type and this isn't a static method, |
2121 | // treat it as the special 'this' parameter, which is encoded separately from |
2122 | // the arguments. |
2123 | TypeIndex ThisTypeIndex; |
2124 | if (!IsStaticMethod && ReturnAndArgs.size() > Index) { |
2125 | if (const DIDerivedType *PtrTy = |
2126 | dyn_cast_or_null<DIDerivedType>(Val: ReturnAndArgs[Index])) { |
2127 | if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) { |
2128 | ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, SubroutineTy: Ty); |
2129 | Index++; |
2130 | } |
2131 | } |
2132 | } |
2133 | |
2134 | while (Index < ReturnAndArgs.size()) |
2135 | ArgTypeIndices.push_back(Elt: getTypeIndex(Ty: ReturnAndArgs[Index++])); |
2136 | |
2137 | // MSVC uses type none for variadic argument. |
2138 | if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void()) |
2139 | ArgTypeIndices.back() = TypeIndex::None(); |
2140 | |
2141 | ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices); |
2142 | TypeIndex ArgListIndex = TypeTable.writeLeafType(Record&: ArgListRec); |
2143 | |
2144 | CallingConvention CC = dwarfCCToCodeView(DwarfCC: Ty->getCC()); |
2145 | |
2146 | MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO, |
2147 | ArgTypeIndices.size(), ArgListIndex, ThisAdjustment); |
2148 | return TypeTable.writeLeafType(Record&: MFR); |
2149 | } |
2150 | |
2151 | TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) { |
2152 | unsigned VSlotCount = |
2153 | Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize()); |
2154 | SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near); |
2155 | |
2156 | VFTableShapeRecord VFTSR(Slots); |
2157 | return TypeTable.writeLeafType(Record&: VFTSR); |
2158 | } |
2159 | |
2160 | static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) { |
2161 | switch (Flags & DINode::FlagAccessibility) { |
2162 | case DINode::FlagPrivate: return MemberAccess::Private; |
2163 | case DINode::FlagPublic: return MemberAccess::Public; |
2164 | case DINode::FlagProtected: return MemberAccess::Protected; |
2165 | case 0: |
2166 | // If there was no explicit access control, provide the default for the tag. |
2167 | return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private |
2168 | : MemberAccess::Public; |
2169 | } |
2170 | llvm_unreachable("access flags are exclusive" ); |
2171 | } |
2172 | |
2173 | static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) { |
2174 | if (SP->isArtificial()) |
2175 | return MethodOptions::CompilerGenerated; |
2176 | |
2177 | // FIXME: Handle other MethodOptions. |
2178 | |
2179 | return MethodOptions::None; |
2180 | } |
2181 | |
2182 | static MethodKind translateMethodKindFlags(const DISubprogram *SP, |
2183 | bool Introduced) { |
2184 | if (SP->getFlags() & DINode::FlagStaticMember) |
2185 | return MethodKind::Static; |
2186 | |
2187 | switch (SP->getVirtuality()) { |
2188 | case dwarf::DW_VIRTUALITY_none: |
2189 | break; |
2190 | case dwarf::DW_VIRTUALITY_virtual: |
2191 | return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual; |
2192 | case dwarf::DW_VIRTUALITY_pure_virtual: |
2193 | return Introduced ? MethodKind::PureIntroducingVirtual |
2194 | : MethodKind::PureVirtual; |
2195 | default: |
2196 | llvm_unreachable("unhandled virtuality case" ); |
2197 | } |
2198 | |
2199 | return MethodKind::Vanilla; |
2200 | } |
2201 | |
2202 | static TypeRecordKind getRecordKind(const DICompositeType *Ty) { |
2203 | switch (Ty->getTag()) { |
2204 | case dwarf::DW_TAG_class_type: |
2205 | return TypeRecordKind::Class; |
2206 | case dwarf::DW_TAG_structure_type: |
2207 | return TypeRecordKind::Struct; |
2208 | default: |
2209 | llvm_unreachable("unexpected tag" ); |
2210 | } |
2211 | } |
2212 | |
2213 | /// Return ClassOptions that should be present on both the forward declaration |
2214 | /// and the defintion of a tag type. |
2215 | static ClassOptions getCommonClassOptions(const DICompositeType *Ty) { |
2216 | ClassOptions CO = ClassOptions::None; |
2217 | |
2218 | // MSVC always sets this flag, even for local types. Clang doesn't always |
2219 | // appear to give every type a linkage name, which may be problematic for us. |
2220 | // FIXME: Investigate the consequences of not following them here. |
2221 | if (!Ty->getIdentifier().empty()) |
2222 | CO |= ClassOptions::HasUniqueName; |
2223 | |
2224 | // Put the Nested flag on a type if it appears immediately inside a tag type. |
2225 | // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass |
2226 | // here. That flag is only set on definitions, and not forward declarations. |
2227 | const DIScope *ImmediateScope = Ty->getScope(); |
2228 | if (ImmediateScope && isa<DICompositeType>(Val: ImmediateScope)) |
2229 | CO |= ClassOptions::Nested; |
2230 | |
2231 | // Put the Scoped flag on function-local types. MSVC puts this flag for enum |
2232 | // type only when it has an immediate function scope. Clang never puts enums |
2233 | // inside DILexicalBlock scopes. Enum types, as generated by clang, are |
2234 | // always in function, class, or file scopes. |
2235 | if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) { |
2236 | if (ImmediateScope && isa<DISubprogram>(Val: ImmediateScope)) |
2237 | CO |= ClassOptions::Scoped; |
2238 | } else { |
2239 | for (const DIScope *Scope = ImmediateScope; Scope != nullptr; |
2240 | Scope = Scope->getScope()) { |
2241 | if (isa<DISubprogram>(Val: Scope)) { |
2242 | CO |= ClassOptions::Scoped; |
2243 | break; |
2244 | } |
2245 | } |
2246 | } |
2247 | |
2248 | return CO; |
2249 | } |
2250 | |
2251 | void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) { |
2252 | switch (Ty->getTag()) { |
2253 | case dwarf::DW_TAG_class_type: |
2254 | case dwarf::DW_TAG_structure_type: |
2255 | case dwarf::DW_TAG_union_type: |
2256 | case dwarf::DW_TAG_enumeration_type: |
2257 | break; |
2258 | default: |
2259 | return; |
2260 | } |
2261 | |
2262 | if (const auto *File = Ty->getFile()) { |
2263 | StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File)); |
2264 | TypeIndex SIDI = TypeTable.writeLeafType(Record&: SIDR); |
2265 | |
2266 | UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine()); |
2267 | TypeTable.writeLeafType(Record&: USLR); |
2268 | } |
2269 | } |
2270 | |
2271 | TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) { |
2272 | ClassOptions CO = getCommonClassOptions(Ty); |
2273 | TypeIndex FTI; |
2274 | unsigned EnumeratorCount = 0; |
2275 | |
2276 | if (Ty->isForwardDecl()) { |
2277 | CO |= ClassOptions::ForwardReference; |
2278 | } else { |
2279 | ContinuationRecordBuilder ContinuationBuilder; |
2280 | ContinuationBuilder.begin(RecordKind: ContinuationRecordKind::FieldList); |
2281 | for (const DINode *Element : Ty->getElements()) { |
2282 | // We assume that the frontend provides all members in source declaration |
2283 | // order, which is what MSVC does. |
2284 | if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Val: Element)) { |
2285 | // FIXME: Is it correct to always emit these as unsigned here? |
2286 | EnumeratorRecord ER(MemberAccess::Public, |
2287 | APSInt(Enumerator->getValue(), true), |
2288 | Enumerator->getName()); |
2289 | ContinuationBuilder.writeMemberType(Record&: ER); |
2290 | EnumeratorCount++; |
2291 | } |
2292 | } |
2293 | FTI = TypeTable.insertRecord(Builder&: ContinuationBuilder); |
2294 | } |
2295 | |
2296 | std::string FullName = getFullyQualifiedName(Ty); |
2297 | |
2298 | EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(), |
2299 | getTypeIndex(Ty: Ty->getBaseType())); |
2300 | TypeIndex EnumTI = TypeTable.writeLeafType(Record&: ER); |
2301 | |
2302 | addUDTSrcLine(Ty, TI: EnumTI); |
2303 | |
2304 | return EnumTI; |
2305 | } |
2306 | |
2307 | //===----------------------------------------------------------------------===// |
2308 | // ClassInfo |
2309 | //===----------------------------------------------------------------------===// |
2310 | |
2311 | struct llvm::ClassInfo { |
2312 | struct MemberInfo { |
2313 | const DIDerivedType *MemberTypeNode; |
2314 | uint64_t BaseOffset; |
2315 | }; |
2316 | // [MemberInfo] |
2317 | using MemberList = std::vector<MemberInfo>; |
2318 | |
2319 | using MethodsList = TinyPtrVector<const DISubprogram *>; |
2320 | // MethodName -> MethodsList |
2321 | using MethodsMap = MapVector<MDString *, MethodsList>; |
2322 | |
2323 | /// Base classes. |
2324 | std::vector<const DIDerivedType *> Inheritance; |
2325 | |
2326 | /// Direct members. |
2327 | MemberList Members; |
2328 | // Direct overloaded methods gathered by name. |
2329 | MethodsMap Methods; |
2330 | |
2331 | TypeIndex VShapeTI; |
2332 | |
2333 | std::vector<const DIType *> NestedTypes; |
2334 | }; |
2335 | |
2336 | void CodeViewDebug::clear() { |
2337 | assert(CurFn == nullptr); |
2338 | FileIdMap.clear(); |
2339 | FnDebugInfo.clear(); |
2340 | FileToFilepathMap.clear(); |
2341 | LocalUDTs.clear(); |
2342 | GlobalUDTs.clear(); |
2343 | TypeIndices.clear(); |
2344 | CompleteTypeIndices.clear(); |
2345 | ScopeGlobals.clear(); |
2346 | CVGlobalVariableOffsets.clear(); |
2347 | } |
2348 | |
2349 | void CodeViewDebug::collectMemberInfo(ClassInfo &Info, |
2350 | const DIDerivedType *DDTy) { |
2351 | if (!DDTy->getName().empty()) { |
2352 | Info.Members.push_back(x: {.MemberTypeNode: DDTy, .BaseOffset: 0}); |
2353 | |
2354 | // Collect static const data members with values. |
2355 | if ((DDTy->getFlags() & DINode::FlagStaticMember) == |
2356 | DINode::FlagStaticMember) { |
2357 | if (DDTy->getConstant() && (isa<ConstantInt>(Val: DDTy->getConstant()) || |
2358 | isa<ConstantFP>(Val: DDTy->getConstant()))) |
2359 | StaticConstMembers.push_back(Elt: DDTy); |
2360 | } |
2361 | |
2362 | return; |
2363 | } |
2364 | |
2365 | // An unnamed member may represent a nested struct or union. Attempt to |
2366 | // interpret the unnamed member as a DICompositeType possibly wrapped in |
2367 | // qualifier types. Add all the indirect fields to the current record if that |
2368 | // succeeds, and drop the member if that fails. |
2369 | assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!" ); |
2370 | uint64_t Offset = DDTy->getOffsetInBits(); |
2371 | const DIType *Ty = DDTy->getBaseType(); |
2372 | bool FullyResolved = false; |
2373 | while (!FullyResolved) { |
2374 | switch (Ty->getTag()) { |
2375 | case dwarf::DW_TAG_const_type: |
2376 | case dwarf::DW_TAG_volatile_type: |
2377 | // FIXME: we should apply the qualifier types to the indirect fields |
2378 | // rather than dropping them. |
2379 | Ty = cast<DIDerivedType>(Val: Ty)->getBaseType(); |
2380 | break; |
2381 | default: |
2382 | FullyResolved = true; |
2383 | break; |
2384 | } |
2385 | } |
2386 | |
2387 | const DICompositeType *DCTy = dyn_cast<DICompositeType>(Val: Ty); |
2388 | if (!DCTy) |
2389 | return; |
2390 | |
2391 | ClassInfo NestedInfo = collectClassInfo(Ty: DCTy); |
2392 | for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members) |
2393 | Info.Members.push_back( |
2394 | x: {.MemberTypeNode: IndirectField.MemberTypeNode, .BaseOffset: IndirectField.BaseOffset + Offset}); |
2395 | } |
2396 | |
2397 | ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) { |
2398 | ClassInfo Info; |
2399 | // Add elements to structure type. |
2400 | DINodeArray Elements = Ty->getElements(); |
2401 | for (auto *Element : Elements) { |
2402 | // We assume that the frontend provides all members in source declaration |
2403 | // order, which is what MSVC does. |
2404 | if (!Element) |
2405 | continue; |
2406 | if (auto *SP = dyn_cast<DISubprogram>(Val: Element)) { |
2407 | Info.Methods[SP->getRawName()].push_back(NewVal: SP); |
2408 | } else if (auto *DDTy = dyn_cast<DIDerivedType>(Val: Element)) { |
2409 | if (DDTy->getTag() == dwarf::DW_TAG_member) { |
2410 | collectMemberInfo(Info, DDTy); |
2411 | } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) { |
2412 | Info.Inheritance.push_back(x: DDTy); |
2413 | } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type && |
2414 | DDTy->getName() == "__vtbl_ptr_type" ) { |
2415 | Info.VShapeTI = getTypeIndex(Ty: DDTy); |
2416 | } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) { |
2417 | Info.NestedTypes.push_back(x: DDTy); |
2418 | } else if (DDTy->getTag() == dwarf::DW_TAG_friend) { |
2419 | // Ignore friend members. It appears that MSVC emitted info about |
2420 | // friends in the past, but modern versions do not. |
2421 | } |
2422 | } else if (auto *Composite = dyn_cast<DICompositeType>(Val: Element)) { |
2423 | Info.NestedTypes.push_back(x: Composite); |
2424 | } |
2425 | // Skip other unrecognized kinds of elements. |
2426 | } |
2427 | return Info; |
2428 | } |
2429 | |
2430 | static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) { |
2431 | // This routine is used by lowerTypeClass and lowerTypeUnion to determine |
2432 | // if a complete type should be emitted instead of a forward reference. |
2433 | return Ty->getName().empty() && Ty->getIdentifier().empty() && |
2434 | !Ty->isForwardDecl(); |
2435 | } |
2436 | |
2437 | TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) { |
2438 | // Emit the complete type for unnamed structs. C++ classes with methods |
2439 | // which have a circular reference back to the class type are expected to |
2440 | // be named by the front-end and should not be "unnamed". C unnamed |
2441 | // structs should not have circular references. |
2442 | if (shouldAlwaysEmitCompleteClassType(Ty)) { |
2443 | // If this unnamed complete type is already in the process of being defined |
2444 | // then the description of the type is malformed and cannot be emitted |
2445 | // into CodeView correctly so report a fatal error. |
2446 | auto I = CompleteTypeIndices.find(Val: Ty); |
2447 | if (I != CompleteTypeIndices.end() && I->second == TypeIndex()) |
2448 | report_fatal_error(reason: "cannot debug circular reference to unnamed type" ); |
2449 | return getCompleteTypeIndex(Ty); |
2450 | } |
2451 | |
2452 | // First, construct the forward decl. Don't look into Ty to compute the |
2453 | // forward decl options, since it might not be available in all TUs. |
2454 | TypeRecordKind Kind = getRecordKind(Ty); |
2455 | ClassOptions CO = |
2456 | ClassOptions::ForwardReference | getCommonClassOptions(Ty); |
2457 | std::string FullName = getFullyQualifiedName(Ty); |
2458 | ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0, |
2459 | FullName, Ty->getIdentifier()); |
2460 | TypeIndex FwdDeclTI = TypeTable.writeLeafType(Record&: CR); |
2461 | if (!Ty->isForwardDecl()) |
2462 | DeferredCompleteTypes.push_back(Elt: Ty); |
2463 | return FwdDeclTI; |
2464 | } |
2465 | |
2466 | TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) { |
2467 | // Construct the field list and complete type record. |
2468 | TypeRecordKind Kind = getRecordKind(Ty); |
2469 | ClassOptions CO = getCommonClassOptions(Ty); |
2470 | TypeIndex FieldTI; |
2471 | TypeIndex VShapeTI; |
2472 | unsigned FieldCount; |
2473 | bool ContainsNestedClass; |
2474 | std::tie(args&: FieldTI, args&: VShapeTI, args&: FieldCount, args&: ContainsNestedClass) = |
2475 | lowerRecordFieldList(Ty); |
2476 | |
2477 | if (ContainsNestedClass) |
2478 | CO |= ClassOptions::ContainsNestedClass; |
2479 | |
2480 | // MSVC appears to set this flag by searching any destructor or method with |
2481 | // FunctionOptions::Constructor among the emitted members. Clang AST has all |
2482 | // the members, however special member functions are not yet emitted into |
2483 | // debug information. For now checking a class's non-triviality seems enough. |
2484 | // FIXME: not true for a nested unnamed struct. |
2485 | if (isNonTrivial(DCTy: Ty)) |
2486 | CO |= ClassOptions::HasConstructorOrDestructor; |
2487 | |
2488 | std::string FullName = getFullyQualifiedName(Ty); |
2489 | |
2490 | uint64_t SizeInBytes = Ty->getSizeInBits() / 8; |
2491 | |
2492 | ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI, |
2493 | SizeInBytes, FullName, Ty->getIdentifier()); |
2494 | TypeIndex ClassTI = TypeTable.writeLeafType(Record&: CR); |
2495 | |
2496 | addUDTSrcLine(Ty, TI: ClassTI); |
2497 | |
2498 | addToUDTs(Ty); |
2499 | |
2500 | return ClassTI; |
2501 | } |
2502 | |
2503 | TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) { |
2504 | // Emit the complete type for unnamed unions. |
2505 | if (shouldAlwaysEmitCompleteClassType(Ty)) |
2506 | return getCompleteTypeIndex(Ty); |
2507 | |
2508 | ClassOptions CO = |
2509 | ClassOptions::ForwardReference | getCommonClassOptions(Ty); |
2510 | std::string FullName = getFullyQualifiedName(Ty); |
2511 | UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier()); |
2512 | TypeIndex FwdDeclTI = TypeTable.writeLeafType(Record&: UR); |
2513 | if (!Ty->isForwardDecl()) |
2514 | DeferredCompleteTypes.push_back(Elt: Ty); |
2515 | return FwdDeclTI; |
2516 | } |
2517 | |
2518 | TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) { |
2519 | ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty); |
2520 | TypeIndex FieldTI; |
2521 | unsigned FieldCount; |
2522 | bool ContainsNestedClass; |
2523 | std::tie(args&: FieldTI, args: std::ignore, args&: FieldCount, args&: ContainsNestedClass) = |
2524 | lowerRecordFieldList(Ty); |
2525 | |
2526 | if (ContainsNestedClass) |
2527 | CO |= ClassOptions::ContainsNestedClass; |
2528 | |
2529 | uint64_t SizeInBytes = Ty->getSizeInBits() / 8; |
2530 | std::string FullName = getFullyQualifiedName(Ty); |
2531 | |
2532 | UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName, |
2533 | Ty->getIdentifier()); |
2534 | TypeIndex UnionTI = TypeTable.writeLeafType(Record&: UR); |
2535 | |
2536 | addUDTSrcLine(Ty, TI: UnionTI); |
2537 | |
2538 | addToUDTs(Ty); |
2539 | |
2540 | return UnionTI; |
2541 | } |
2542 | |
2543 | std::tuple<TypeIndex, TypeIndex, unsigned, bool> |
2544 | CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) { |
2545 | // Manually count members. MSVC appears to count everything that generates a |
2546 | // field list record. Each individual overload in a method overload group |
2547 | // contributes to this count, even though the overload group is a single field |
2548 | // list record. |
2549 | unsigned MemberCount = 0; |
2550 | ClassInfo Info = collectClassInfo(Ty); |
2551 | ContinuationRecordBuilder ContinuationBuilder; |
2552 | ContinuationBuilder.begin(RecordKind: ContinuationRecordKind::FieldList); |
2553 | |
2554 | // Create base classes. |
2555 | for (const DIDerivedType *I : Info.Inheritance) { |
2556 | if (I->getFlags() & DINode::FlagVirtual) { |
2557 | // Virtual base. |
2558 | unsigned VBPtrOffset = I->getVBPtrOffset(); |
2559 | // FIXME: Despite the accessor name, the offset is really in bytes. |
2560 | unsigned VBTableIndex = I->getOffsetInBits() / 4; |
2561 | auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase |
2562 | ? TypeRecordKind::IndirectVirtualBaseClass |
2563 | : TypeRecordKind::VirtualBaseClass; |
2564 | VirtualBaseClassRecord VBCR( |
2565 | RecordKind, translateAccessFlags(RecordTag: Ty->getTag(), Flags: I->getFlags()), |
2566 | getTypeIndex(Ty: I->getBaseType()), getVBPTypeIndex(), VBPtrOffset, |
2567 | VBTableIndex); |
2568 | |
2569 | ContinuationBuilder.writeMemberType(Record&: VBCR); |
2570 | MemberCount++; |
2571 | } else { |
2572 | assert(I->getOffsetInBits() % 8 == 0 && |
2573 | "bases must be on byte boundaries" ); |
2574 | BaseClassRecord BCR(translateAccessFlags(RecordTag: Ty->getTag(), Flags: I->getFlags()), |
2575 | getTypeIndex(Ty: I->getBaseType()), |
2576 | I->getOffsetInBits() / 8); |
2577 | ContinuationBuilder.writeMemberType(Record&: BCR); |
2578 | MemberCount++; |
2579 | } |
2580 | } |
2581 | |
2582 | // Create members. |
2583 | for (ClassInfo::MemberInfo &MemberInfo : Info.Members) { |
2584 | const DIDerivedType *Member = MemberInfo.MemberTypeNode; |
2585 | TypeIndex MemberBaseType = getTypeIndex(Ty: Member->getBaseType()); |
2586 | StringRef MemberName = Member->getName(); |
2587 | MemberAccess Access = |
2588 | translateAccessFlags(RecordTag: Ty->getTag(), Flags: Member->getFlags()); |
2589 | |
2590 | if (Member->isStaticMember()) { |
2591 | StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName); |
2592 | ContinuationBuilder.writeMemberType(Record&: SDMR); |
2593 | MemberCount++; |
2594 | continue; |
2595 | } |
2596 | |
2597 | // Virtual function pointer member. |
2598 | if ((Member->getFlags() & DINode::FlagArtificial) && |
2599 | Member->getName().starts_with(Prefix: "_vptr$" )) { |
2600 | VFPtrRecord VFPR(getTypeIndex(Ty: Member->getBaseType())); |
2601 | ContinuationBuilder.writeMemberType(Record&: VFPR); |
2602 | MemberCount++; |
2603 | continue; |
2604 | } |
2605 | |
2606 | // Data member. |
2607 | uint64_t MemberOffsetInBits = |
2608 | Member->getOffsetInBits() + MemberInfo.BaseOffset; |
2609 | if (Member->isBitField()) { |
2610 | uint64_t StartBitOffset = MemberOffsetInBits; |
2611 | if (const auto *CI = |
2612 | dyn_cast_or_null<ConstantInt>(Val: Member->getStorageOffsetInBits())) { |
2613 | MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset; |
2614 | } |
2615 | StartBitOffset -= MemberOffsetInBits; |
2616 | BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(), |
2617 | StartBitOffset); |
2618 | MemberBaseType = TypeTable.writeLeafType(Record&: BFR); |
2619 | } |
2620 | uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8; |
2621 | DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes, |
2622 | MemberName); |
2623 | ContinuationBuilder.writeMemberType(Record&: DMR); |
2624 | MemberCount++; |
2625 | } |
2626 | |
2627 | // Create methods |
2628 | for (auto &MethodItr : Info.Methods) { |
2629 | StringRef Name = MethodItr.first->getString(); |
2630 | |
2631 | std::vector<OneMethodRecord> Methods; |
2632 | for (const DISubprogram *SP : MethodItr.second) { |
2633 | TypeIndex MethodType = getMemberFunctionType(SP, Class: Ty); |
2634 | bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual; |
2635 | |
2636 | unsigned VFTableOffset = -1; |
2637 | if (Introduced) |
2638 | VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes(); |
2639 | |
2640 | Methods.push_back(x: OneMethodRecord( |
2641 | MethodType, translateAccessFlags(RecordTag: Ty->getTag(), Flags: SP->getFlags()), |
2642 | translateMethodKindFlags(SP, Introduced), |
2643 | translateMethodOptionFlags(SP), VFTableOffset, Name)); |
2644 | MemberCount++; |
2645 | } |
2646 | assert(!Methods.empty() && "Empty methods map entry" ); |
2647 | if (Methods.size() == 1) |
2648 | ContinuationBuilder.writeMemberType(Record&: Methods[0]); |
2649 | else { |
2650 | // FIXME: Make this use its own ContinuationBuilder so that |
2651 | // MethodOverloadList can be split correctly. |
2652 | MethodOverloadListRecord MOLR(Methods); |
2653 | TypeIndex MethodList = TypeTable.writeLeafType(Record&: MOLR); |
2654 | |
2655 | OverloadedMethodRecord OMR(Methods.size(), MethodList, Name); |
2656 | ContinuationBuilder.writeMemberType(Record&: OMR); |
2657 | } |
2658 | } |
2659 | |
2660 | // Create nested classes. |
2661 | for (const DIType *Nested : Info.NestedTypes) { |
2662 | NestedTypeRecord R(getTypeIndex(Ty: Nested), Nested->getName()); |
2663 | ContinuationBuilder.writeMemberType(Record&: R); |
2664 | MemberCount++; |
2665 | } |
2666 | |
2667 | TypeIndex FieldTI = TypeTable.insertRecord(Builder&: ContinuationBuilder); |
2668 | return std::make_tuple(args&: FieldTI, args&: Info.VShapeTI, args&: MemberCount, |
2669 | args: !Info.NestedTypes.empty()); |
2670 | } |
2671 | |
2672 | TypeIndex CodeViewDebug::getVBPTypeIndex() { |
2673 | if (!VBPType.getIndex()) { |
2674 | // Make a 'const int *' type. |
2675 | ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const); |
2676 | TypeIndex ModifiedTI = TypeTable.writeLeafType(Record&: MR); |
2677 | |
2678 | PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64 |
2679 | : PointerKind::Near32; |
2680 | PointerMode PM = PointerMode::Pointer; |
2681 | PointerOptions PO = PointerOptions::None; |
2682 | PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes()); |
2683 | VBPType = TypeTable.writeLeafType(Record&: PR); |
2684 | } |
2685 | |
2686 | return VBPType; |
2687 | } |
2688 | |
2689 | TypeIndex CodeViewDebug::getTypeIndex(const DIType *Ty, const DIType *ClassTy) { |
2690 | // The null DIType is the void type. Don't try to hash it. |
2691 | if (!Ty) |
2692 | return TypeIndex::Void(); |
2693 | |
2694 | // Check if we've already translated this type. Don't try to do a |
2695 | // get-or-create style insertion that caches the hash lookup across the |
2696 | // lowerType call. It will update the TypeIndices map. |
2697 | auto I = TypeIndices.find(Val: {Ty, ClassTy}); |
2698 | if (I != TypeIndices.end()) |
2699 | return I->second; |
2700 | |
2701 | TypeLoweringScope S(*this); |
2702 | TypeIndex TI = lowerType(Ty, ClassTy); |
2703 | return recordTypeIndexForDINode(Node: Ty, TI, ClassTy); |
2704 | } |
2705 | |
2706 | codeview::TypeIndex |
2707 | CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy, |
2708 | const DISubroutineType *SubroutineTy) { |
2709 | assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type && |
2710 | "this type must be a pointer type" ); |
2711 | |
2712 | PointerOptions Options = PointerOptions::None; |
2713 | if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference) |
2714 | Options = PointerOptions::LValueRefThisPointer; |
2715 | else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference) |
2716 | Options = PointerOptions::RValueRefThisPointer; |
2717 | |
2718 | // Check if we've already translated this type. If there is no ref qualifier |
2719 | // on the function then we look up this pointer type with no associated class |
2720 | // so that the TypeIndex for the this pointer can be shared with the type |
2721 | // index for other pointers to this class type. If there is a ref qualifier |
2722 | // then we lookup the pointer using the subroutine as the parent type. |
2723 | auto I = TypeIndices.find(Val: {PtrTy, SubroutineTy}); |
2724 | if (I != TypeIndices.end()) |
2725 | return I->second; |
2726 | |
2727 | TypeLoweringScope S(*this); |
2728 | TypeIndex TI = lowerTypePointer(Ty: PtrTy, PO: Options); |
2729 | return recordTypeIndexForDINode(Node: PtrTy, TI, ClassTy: SubroutineTy); |
2730 | } |
2731 | |
2732 | TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(const DIType *Ty) { |
2733 | PointerRecord PR(getTypeIndex(Ty), |
2734 | getPointerSizeInBytes() == 8 ? PointerKind::Near64 |
2735 | : PointerKind::Near32, |
2736 | PointerMode::LValueReference, PointerOptions::None, |
2737 | Ty->getSizeInBits() / 8); |
2738 | return TypeTable.writeLeafType(Record&: PR); |
2739 | } |
2740 | |
2741 | TypeIndex CodeViewDebug::getCompleteTypeIndex(const DIType *Ty) { |
2742 | // The null DIType is the void type. Don't try to hash it. |
2743 | if (!Ty) |
2744 | return TypeIndex::Void(); |
2745 | |
2746 | // Look through typedefs when getting the complete type index. Call |
2747 | // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are |
2748 | // emitted only once. |
2749 | if (Ty->getTag() == dwarf::DW_TAG_typedef) |
2750 | (void)getTypeIndex(Ty); |
2751 | while (Ty->getTag() == dwarf::DW_TAG_typedef) |
2752 | Ty = cast<DIDerivedType>(Val: Ty)->getBaseType(); |
2753 | |
2754 | // If this is a non-record type, the complete type index is the same as the |
2755 | // normal type index. Just call getTypeIndex. |
2756 | switch (Ty->getTag()) { |
2757 | case dwarf::DW_TAG_class_type: |
2758 | case dwarf::DW_TAG_structure_type: |
2759 | case dwarf::DW_TAG_union_type: |
2760 | break; |
2761 | default: |
2762 | return getTypeIndex(Ty); |
2763 | } |
2764 | |
2765 | const auto *CTy = cast<DICompositeType>(Val: Ty); |
2766 | |
2767 | TypeLoweringScope S(*this); |
2768 | |
2769 | // Make sure the forward declaration is emitted first. It's unclear if this |
2770 | // is necessary, but MSVC does it, and we should follow suit until we can show |
2771 | // otherwise. |
2772 | // We only emit a forward declaration for named types. |
2773 | if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) { |
2774 | TypeIndex FwdDeclTI = getTypeIndex(Ty: CTy); |
2775 | |
2776 | // Just use the forward decl if we don't have complete type info. This |
2777 | // might happen if the frontend is using modules and expects the complete |
2778 | // definition to be emitted elsewhere. |
2779 | if (CTy->isForwardDecl()) |
2780 | return FwdDeclTI; |
2781 | } |
2782 | |
2783 | // Check if we've already translated the complete record type. |
2784 | // Insert the type with a null TypeIndex to signify that the type is currently |
2785 | // being lowered. |
2786 | auto InsertResult = CompleteTypeIndices.try_emplace(Key: CTy); |
2787 | if (!InsertResult.second) |
2788 | return InsertResult.first->second; |
2789 | |
2790 | TypeIndex TI; |
2791 | switch (CTy->getTag()) { |
2792 | case dwarf::DW_TAG_class_type: |
2793 | case dwarf::DW_TAG_structure_type: |
2794 | TI = lowerCompleteTypeClass(Ty: CTy); |
2795 | break; |
2796 | case dwarf::DW_TAG_union_type: |
2797 | TI = lowerCompleteTypeUnion(Ty: CTy); |
2798 | break; |
2799 | default: |
2800 | llvm_unreachable("not a record" ); |
2801 | } |
2802 | |
2803 | // Update the type index associated with this CompositeType. This cannot |
2804 | // use the 'InsertResult' iterator above because it is potentially |
2805 | // invalidated by map insertions which can occur while lowering the class |
2806 | // type above. |
2807 | CompleteTypeIndices[CTy] = TI; |
2808 | return TI; |
2809 | } |
2810 | |
2811 | /// Emit all the deferred complete record types. Try to do this in FIFO order, |
2812 | /// and do this until fixpoint, as each complete record type typically |
2813 | /// references |
2814 | /// many other record types. |
2815 | void CodeViewDebug::emitDeferredCompleteTypes() { |
2816 | SmallVector<const DICompositeType *, 4> TypesToEmit; |
2817 | while (!DeferredCompleteTypes.empty()) { |
2818 | std::swap(LHS&: DeferredCompleteTypes, RHS&: TypesToEmit); |
2819 | for (const DICompositeType *RecordTy : TypesToEmit) |
2820 | getCompleteTypeIndex(Ty: RecordTy); |
2821 | TypesToEmit.clear(); |
2822 | } |
2823 | } |
2824 | |
2825 | void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI, |
2826 | ArrayRef<LocalVariable> Locals) { |
2827 | // Get the sorted list of parameters and emit them first. |
2828 | SmallVector<const LocalVariable *, 6> Params; |
2829 | for (const LocalVariable &L : Locals) |
2830 | if (L.DIVar->isParameter()) |
2831 | Params.push_back(Elt: &L); |
2832 | llvm::sort(C&: Params, Comp: [](const LocalVariable *L, const LocalVariable *R) { |
2833 | return L->DIVar->getArg() < R->DIVar->getArg(); |
2834 | }); |
2835 | for (const LocalVariable *L : Params) |
2836 | emitLocalVariable(FI, Var: *L); |
2837 | |
2838 | // Next emit all non-parameters in the order that we found them. |
2839 | for (const LocalVariable &L : Locals) { |
2840 | if (!L.DIVar->isParameter()) { |
2841 | if (L.ConstantValue) { |
2842 | // If ConstantValue is set we will emit it as a S_CONSTANT instead of a |
2843 | // S_LOCAL in order to be able to represent it at all. |
2844 | const DIType *Ty = L.DIVar->getType(); |
2845 | APSInt Val(*L.ConstantValue); |
2846 | emitConstantSymbolRecord(DTy: Ty, Value&: Val, QualifiedName: std::string(L.DIVar->getName())); |
2847 | } else { |
2848 | emitLocalVariable(FI, Var: L); |
2849 | } |
2850 | } |
2851 | } |
2852 | } |
2853 | |
2854 | void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI, |
2855 | const LocalVariable &Var) { |
2856 | // LocalSym record, see SymbolRecord.h for more info. |
2857 | MCSymbol *LocalEnd = beginSymbolRecord(Kind: SymbolKind::S_LOCAL); |
2858 | |
2859 | LocalSymFlags Flags = LocalSymFlags::None; |
2860 | if (Var.DIVar->isParameter()) |
2861 | Flags |= LocalSymFlags::IsParameter; |
2862 | if (Var.DefRanges.empty()) |
2863 | Flags |= LocalSymFlags::IsOptimizedOut; |
2864 | |
2865 | OS.AddComment(T: "TypeIndex" ); |
2866 | TypeIndex TI = Var.UseReferenceType |
2867 | ? getTypeIndexForReferenceTo(Ty: Var.DIVar->getType()) |
2868 | : getCompleteTypeIndex(Ty: Var.DIVar->getType()); |
2869 | OS.emitInt32(Value: TI.getIndex()); |
2870 | OS.AddComment(T: "Flags" ); |
2871 | OS.emitInt16(Value: static_cast<uint16_t>(Flags)); |
2872 | // Truncate the name so we won't overflow the record length field. |
2873 | emitNullTerminatedSymbolName(OS, S: Var.DIVar->getName()); |
2874 | endSymbolRecord(SymEnd: LocalEnd); |
2875 | |
2876 | // Calculate the on disk prefix of the appropriate def range record. The |
2877 | // records and on disk formats are described in SymbolRecords.h. BytePrefix |
2878 | // should be big enough to hold all forms without memory allocation. |
2879 | SmallString<20> BytePrefix; |
2880 | for (const auto &Pair : Var.DefRanges) { |
2881 | LocalVarDef DefRange = Pair.first; |
2882 | const auto &Ranges = Pair.second; |
2883 | BytePrefix.clear(); |
2884 | if (DefRange.InMemory) { |
2885 | int Offset = DefRange.DataOffset; |
2886 | unsigned Reg = DefRange.CVRegister; |
2887 | |
2888 | // 32-bit x86 call sequences often use PUSH instructions, which disrupt |
2889 | // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0, |
2890 | // instead. In frames without stack realignment, $T0 will be the CFA. |
2891 | if (RegisterId(Reg) == RegisterId::ESP) { |
2892 | Reg = unsigned(RegisterId::VFRAME); |
2893 | Offset += FI.OffsetAdjustment; |
2894 | } |
2895 | |
2896 | // If we can use the chosen frame pointer for the frame and this isn't a |
2897 | // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record. |
2898 | // Otherwise, use S_DEFRANGE_REGISTER_REL. |
2899 | EncodedFramePtrReg EncFP = encodeFramePtrReg(Reg: RegisterId(Reg), CPU: TheCPU); |
2900 | if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None && |
2901 | (bool(Flags & LocalSymFlags::IsParameter) |
2902 | ? (EncFP == FI.EncodedParamFramePtrReg) |
2903 | : (EncFP == FI.EncodedLocalFramePtrReg))) { |
2904 | DefRangeFramePointerRelHeader DRHdr; |
2905 | DRHdr.Offset = Offset; |
2906 | OS.emitCVDefRangeDirective(Ranges, DRHdr); |
2907 | } else { |
2908 | uint16_t RegRelFlags = 0; |
2909 | if (DefRange.IsSubfield) { |
2910 | RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag | |
2911 | (DefRange.StructOffset |
2912 | << DefRangeRegisterRelSym::OffsetInParentShift); |
2913 | } |
2914 | DefRangeRegisterRelHeader DRHdr; |
2915 | DRHdr.Register = Reg; |
2916 | DRHdr.Flags = RegRelFlags; |
2917 | DRHdr.BasePointerOffset = Offset; |
2918 | OS.emitCVDefRangeDirective(Ranges, DRHdr); |
2919 | } |
2920 | } else { |
2921 | assert(DefRange.DataOffset == 0 && "unexpected offset into register" ); |
2922 | if (DefRange.IsSubfield) { |
2923 | DefRangeSubfieldRegisterHeader DRHdr; |
2924 | DRHdr.Register = DefRange.CVRegister; |
2925 | DRHdr.MayHaveNoName = 0; |
2926 | DRHdr.OffsetInParent = DefRange.StructOffset; |
2927 | OS.emitCVDefRangeDirective(Ranges, DRHdr); |
2928 | } else { |
2929 | DefRangeRegisterHeader DRHdr; |
2930 | DRHdr.Register = DefRange.CVRegister; |
2931 | DRHdr.MayHaveNoName = 0; |
2932 | OS.emitCVDefRangeDirective(Ranges, DRHdr); |
2933 | } |
2934 | } |
2935 | } |
2936 | } |
2937 | |
2938 | void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks, |
2939 | const FunctionInfo& FI) { |
2940 | for (LexicalBlock *Block : Blocks) |
2941 | emitLexicalBlock(Block: *Block, FI); |
2942 | } |
2943 | |
2944 | /// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a |
2945 | /// lexical block scope. |
2946 | void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block, |
2947 | const FunctionInfo& FI) { |
2948 | MCSymbol *RecordEnd = beginSymbolRecord(Kind: SymbolKind::S_BLOCK32); |
2949 | OS.AddComment(T: "PtrParent" ); |
2950 | OS.emitInt32(Value: 0); // PtrParent |
2951 | OS.AddComment(T: "PtrEnd" ); |
2952 | OS.emitInt32(Value: 0); // PtrEnd |
2953 | OS.AddComment(T: "Code size" ); |
2954 | OS.emitAbsoluteSymbolDiff(Hi: Block.End, Lo: Block.Begin, Size: 4); // Code Size |
2955 | OS.AddComment(T: "Function section relative address" ); |
2956 | OS.emitCOFFSecRel32(Symbol: Block.Begin, /*Offset=*/0); // Func Offset |
2957 | OS.AddComment(T: "Function section index" ); |
2958 | OS.emitCOFFSectionIndex(Symbol: FI.Begin); // Func Symbol |
2959 | OS.AddComment(T: "Lexical block name" ); |
2960 | emitNullTerminatedSymbolName(OS, S: Block.Name); // Name |
2961 | endSymbolRecord(SymEnd: RecordEnd); |
2962 | |
2963 | // Emit variables local to this lexical block. |
2964 | emitLocalVariableList(FI, Locals: Block.Locals); |
2965 | emitGlobalVariableList(Globals: Block.Globals); |
2966 | |
2967 | // Emit lexical blocks contained within this block. |
2968 | emitLexicalBlockList(Blocks: Block.Children, FI); |
2969 | |
2970 | // Close the lexical block scope. |
2971 | emitEndSymbolRecord(EndKind: SymbolKind::S_END); |
2972 | } |
2973 | |
2974 | /// Convenience routine for collecting lexical block information for a list |
2975 | /// of lexical scopes. |
2976 | void CodeViewDebug::collectLexicalBlockInfo( |
2977 | SmallVectorImpl<LexicalScope *> &Scopes, |
2978 | SmallVectorImpl<LexicalBlock *> &Blocks, |
2979 | SmallVectorImpl<LocalVariable> &Locals, |
2980 | SmallVectorImpl<CVGlobalVariable> &Globals) { |
2981 | for (LexicalScope *Scope : Scopes) |
2982 | collectLexicalBlockInfo(Scope&: *Scope, ParentBlocks&: Blocks, ParentLocals&: Locals, ParentGlobals&: Globals); |
2983 | } |
2984 | |
2985 | /// Populate the lexical blocks and local variable lists of the parent with |
2986 | /// information about the specified lexical scope. |
2987 | void CodeViewDebug::collectLexicalBlockInfo( |
2988 | LexicalScope &Scope, |
2989 | SmallVectorImpl<LexicalBlock *> &ParentBlocks, |
2990 | SmallVectorImpl<LocalVariable> &ParentLocals, |
2991 | SmallVectorImpl<CVGlobalVariable> &ParentGlobals) { |
2992 | if (Scope.isAbstractScope()) |
2993 | return; |
2994 | |
2995 | // Gather information about the lexical scope including local variables, |
2996 | // global variables, and address ranges. |
2997 | bool IgnoreScope = false; |
2998 | auto LI = ScopeVariables.find(Val: &Scope); |
2999 | SmallVectorImpl<LocalVariable> *Locals = |
3000 | LI != ScopeVariables.end() ? &LI->second : nullptr; |
3001 | auto GI = ScopeGlobals.find(Val: Scope.getScopeNode()); |
3002 | SmallVectorImpl<CVGlobalVariable> *Globals = |
3003 | GI != ScopeGlobals.end() ? GI->second.get() : nullptr; |
3004 | const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Val: Scope.getScopeNode()); |
3005 | const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges(); |
3006 | |
3007 | // Ignore lexical scopes which do not contain variables. |
3008 | if (!Locals && !Globals) |
3009 | IgnoreScope = true; |
3010 | |
3011 | // Ignore lexical scopes which are not lexical blocks. |
3012 | if (!DILB) |
3013 | IgnoreScope = true; |
3014 | |
3015 | // Ignore scopes which have too many address ranges to represent in the |
3016 | // current CodeView format or do not have a valid address range. |
3017 | // |
3018 | // For lexical scopes with multiple address ranges you may be tempted to |
3019 | // construct a single range covering every instruction where the block is |
3020 | // live and everything in between. Unfortunately, Visual Studio only |
3021 | // displays variables from the first matching lexical block scope. If the |
3022 | // first lexical block contains exception handling code or cold code which |
3023 | // is moved to the bottom of the routine creating a single range covering |
3024 | // nearly the entire routine, then it will hide all other lexical blocks |
3025 | // and the variables they contain. |
3026 | if (Ranges.size() != 1 || !getLabelAfterInsn(MI: Ranges.front().second)) |
3027 | IgnoreScope = true; |
3028 | |
3029 | if (IgnoreScope) { |
3030 | // This scope can be safely ignored and eliminating it will reduce the |
3031 | // size of the debug information. Be sure to collect any variable and scope |
3032 | // information from the this scope or any of its children and collapse them |
3033 | // into the parent scope. |
3034 | if (Locals) |
3035 | ParentLocals.append(in_start: Locals->begin(), in_end: Locals->end()); |
3036 | if (Globals) |
3037 | ParentGlobals.append(in_start: Globals->begin(), in_end: Globals->end()); |
3038 | collectLexicalBlockInfo(Scopes&: Scope.getChildren(), |
3039 | Blocks&: ParentBlocks, |
3040 | Locals&: ParentLocals, |
3041 | Globals&: ParentGlobals); |
3042 | return; |
3043 | } |
3044 | |
3045 | // Create a new CodeView lexical block for this lexical scope. If we've |
3046 | // seen this DILexicalBlock before then the scope tree is malformed and |
3047 | // we can handle this gracefully by not processing it a second time. |
3048 | auto BlockInsertion = CurFn->LexicalBlocks.try_emplace(k: DILB); |
3049 | if (!BlockInsertion.second) |
3050 | return; |
3051 | |
3052 | // Create a lexical block containing the variables and collect the |
3053 | // lexical block information for the children. |
3054 | const InsnRange &Range = Ranges.front(); |
3055 | assert(Range.first && Range.second); |
3056 | LexicalBlock &Block = BlockInsertion.first->second; |
3057 | Block.Begin = getLabelBeforeInsn(MI: Range.first); |
3058 | Block.End = getLabelAfterInsn(MI: Range.second); |
3059 | assert(Block.Begin && "missing label for scope begin" ); |
3060 | assert(Block.End && "missing label for scope end" ); |
3061 | Block.Name = DILB->getName(); |
3062 | if (Locals) |
3063 | Block.Locals = std::move(*Locals); |
3064 | if (Globals) |
3065 | Block.Globals = std::move(*Globals); |
3066 | ParentBlocks.push_back(Elt: &Block); |
3067 | collectLexicalBlockInfo(Scopes&: Scope.getChildren(), |
3068 | Blocks&: Block.Children, |
3069 | Locals&: Block.Locals, |
3070 | Globals&: Block.Globals); |
3071 | } |
3072 | |
3073 | void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) { |
3074 | const Function &GV = MF->getFunction(); |
3075 | assert(FnDebugInfo.count(&GV)); |
3076 | assert(CurFn == FnDebugInfo[&GV].get()); |
3077 | |
3078 | collectVariableInfo(SP: GV.getSubprogram()); |
3079 | |
3080 | // Build the lexical block structure to emit for this routine. |
3081 | if (LexicalScope *CFS = LScopes.getCurrentFunctionScope()) |
3082 | collectLexicalBlockInfo(Scope&: *CFS, |
3083 | ParentBlocks&: CurFn->ChildBlocks, |
3084 | ParentLocals&: CurFn->Locals, |
3085 | ParentGlobals&: CurFn->Globals); |
3086 | |
3087 | // Clear the scope and variable information from the map which will not be |
3088 | // valid after we have finished processing this routine. This also prepares |
3089 | // the map for the subsequent routine. |
3090 | ScopeVariables.clear(); |
3091 | |
3092 | // Don't emit anything if we don't have any line tables. |
3093 | // Thunks are compiler-generated and probably won't have source correlation. |
3094 | if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) { |
3095 | FnDebugInfo.erase(Key: &GV); |
3096 | CurFn = nullptr; |
3097 | return; |
3098 | } |
3099 | |
3100 | // Find heap alloc sites and add to list. |
3101 | for (const auto &MBB : *MF) { |
3102 | for (const auto &MI : MBB) { |
3103 | if (MDNode *MD = MI.getHeapAllocMarker()) { |
3104 | CurFn->HeapAllocSites.push_back(x: std::make_tuple(args: getLabelBeforeInsn(MI: &MI), |
3105 | args: getLabelAfterInsn(MI: &MI), |
3106 | args: dyn_cast<DIType>(Val: MD))); |
3107 | } |
3108 | } |
3109 | } |
3110 | |
3111 | bool isThumb = MMI->getModule()->getTargetTriple().getArch() == |
3112 | llvm::Triple::ArchType::thumb; |
3113 | collectDebugInfoForJumpTables(MF, isThumb); |
3114 | |
3115 | CurFn->Annotations = MF->getCodeViewAnnotations(); |
3116 | |
3117 | CurFn->End = Asm->getFunctionEnd(); |
3118 | |
3119 | CurFn = nullptr; |
3120 | } |
3121 | |
3122 | // Usable locations are valid with non-zero line numbers. A line number of zero |
3123 | // corresponds to optimized code that doesn't have a distinct source location. |
3124 | // In this case, we try to use the previous or next source location depending on |
3125 | // the context. |
3126 | static bool isUsableDebugLoc(DebugLoc DL) { |
3127 | return DL && DL.getLine() != 0; |
3128 | } |
3129 | |
3130 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
3131 | DebugHandlerBase::beginInstruction(MI); |
3132 | |
3133 | // Ignore DBG_VALUE and DBG_LABEL locations and function prologue. |
3134 | if (!Asm || !CurFn || MI->isDebugInstr() || |
3135 | MI->getFlag(Flag: MachineInstr::FrameSetup)) |
3136 | return; |
3137 | |
3138 | // If the first instruction of a new MBB has no location, find the first |
3139 | // instruction with a location and use that. |
3140 | DebugLoc DL = MI->getDebugLoc(); |
3141 | if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) { |
3142 | for (const auto &NextMI : *MI->getParent()) { |
3143 | if (NextMI.isDebugInstr()) |
3144 | continue; |
3145 | DL = NextMI.getDebugLoc(); |
3146 | if (isUsableDebugLoc(DL)) |
3147 | break; |
3148 | } |
3149 | // FIXME: Handle the case where the BB has no valid locations. This would |
3150 | // probably require doing a real dataflow analysis. |
3151 | } |
3152 | PrevInstBB = MI->getParent(); |
3153 | |
3154 | // If we still don't have a debug location, don't record a location. |
3155 | if (!isUsableDebugLoc(DL)) |
3156 | return; |
3157 | |
3158 | maybeRecordLocation(DL, MF: Asm->MF); |
3159 | } |
3160 | |
3161 | MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) { |
3162 | MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(), |
3163 | *EndLabel = MMI->getContext().createTempSymbol(); |
3164 | OS.emitInt32(Value: unsigned(Kind)); |
3165 | OS.AddComment(T: "Subsection size" ); |
3166 | OS.emitAbsoluteSymbolDiff(Hi: EndLabel, Lo: BeginLabel, Size: 4); |
3167 | OS.emitLabel(Symbol: BeginLabel); |
3168 | return EndLabel; |
3169 | } |
3170 | |
3171 | void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) { |
3172 | OS.emitLabel(Symbol: EndLabel); |
3173 | // Every subsection must be aligned to a 4-byte boundary. |
3174 | OS.emitValueToAlignment(Alignment: Align(4)); |
3175 | } |
3176 | |
3177 | static StringRef getSymbolName(SymbolKind SymKind) { |
3178 | for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames()) |
3179 | if (EE.Value == SymKind) |
3180 | return EE.Name; |
3181 | return "" ; |
3182 | } |
3183 | |
3184 | MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) { |
3185 | MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(), |
3186 | *EndLabel = MMI->getContext().createTempSymbol(); |
3187 | OS.AddComment(T: "Record length" ); |
3188 | OS.emitAbsoluteSymbolDiff(Hi: EndLabel, Lo: BeginLabel, Size: 2); |
3189 | OS.emitLabel(Symbol: BeginLabel); |
3190 | if (OS.isVerboseAsm()) |
3191 | OS.AddComment(T: "Record kind: " + getSymbolName(SymKind)); |
3192 | OS.emitInt16(Value: unsigned(SymKind)); |
3193 | return EndLabel; |
3194 | } |
3195 | |
3196 | void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) { |
3197 | // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid |
3198 | // an extra copy of every symbol record in LLD. This increases object file |
3199 | // size by less than 1% in the clang build, and is compatible with the Visual |
3200 | // C++ linker. |
3201 | OS.emitValueToAlignment(Alignment: Align(4)); |
3202 | OS.emitLabel(Symbol: SymEnd); |
3203 | } |
3204 | |
3205 | void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) { |
3206 | OS.AddComment(T: "Record length" ); |
3207 | OS.emitInt16(Value: 2); |
3208 | if (OS.isVerboseAsm()) |
3209 | OS.AddComment(T: "Record kind: " + getSymbolName(SymKind: EndKind)); |
3210 | OS.emitInt16(Value: uint16_t(EndKind)); // Record Kind |
3211 | } |
3212 | |
3213 | void CodeViewDebug::emitDebugInfoForUDTs( |
3214 | const std::vector<std::pair<std::string, const DIType *>> &UDTs) { |
3215 | #ifndef NDEBUG |
3216 | size_t OriginalSize = UDTs.size(); |
3217 | #endif |
3218 | for (const auto &UDT : UDTs) { |
3219 | const DIType *T = UDT.second; |
3220 | assert(shouldEmitUdt(T)); |
3221 | MCSymbol *UDTRecordEnd = beginSymbolRecord(SymKind: SymbolKind::S_UDT); |
3222 | OS.AddComment(T: "Type" ); |
3223 | OS.emitInt32(Value: getCompleteTypeIndex(Ty: T).getIndex()); |
3224 | assert(OriginalSize == UDTs.size() && |
3225 | "getCompleteTypeIndex found new UDTs!" ); |
3226 | emitNullTerminatedSymbolName(OS, S: UDT.first); |
3227 | endSymbolRecord(SymEnd: UDTRecordEnd); |
3228 | } |
3229 | } |
3230 | |
3231 | void CodeViewDebug::collectGlobalVariableInfo() { |
3232 | DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *> |
3233 | GlobalMap; |
3234 | for (const GlobalVariable &GV : MMI->getModule()->globals()) { |
3235 | SmallVector<DIGlobalVariableExpression *, 1> GVEs; |
3236 | GV.getDebugInfo(GVs&: GVEs); |
3237 | for (const auto *GVE : GVEs) |
3238 | GlobalMap[GVE] = &GV; |
3239 | } |
3240 | |
3241 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata(Name: "llvm.dbg.cu" ); |
3242 | for (const MDNode *Node : CUs->operands()) { |
3243 | const auto *CU = cast<DICompileUnit>(Val: Node); |
3244 | for (const auto *GVE : CU->getGlobalVariables()) { |
3245 | const DIGlobalVariable *DIGV = GVE->getVariable(); |
3246 | const DIExpression *DIE = GVE->getExpression(); |
3247 | // Don't emit string literals in CodeView, as the only useful parts are |
3248 | // generally the filename and line number, which isn't possible to output |
3249 | // in CodeView. String literals should be the only unnamed GlobalVariable |
3250 | // with debug info. |
3251 | if (DIGV->getName().empty()) continue; |
3252 | |
3253 | if ((DIE->getNumElements() == 2) && |
3254 | (DIE->getElement(I: 0) == dwarf::DW_OP_plus_uconst)) |
3255 | // Record the constant offset for the variable. |
3256 | // |
3257 | // A Fortran common block uses this idiom to encode the offset |
3258 | // of a variable from the common block's starting address. |
3259 | CVGlobalVariableOffsets.insert( |
3260 | KV: std::make_pair(x&: DIGV, y: DIE->getElement(I: 1))); |
3261 | |
3262 | // Emit constant global variables in a global symbol section. |
3263 | if (GlobalMap.count(Val: GVE) == 0 && DIE->isConstant()) { |
3264 | CVGlobalVariable CVGV = {.DIGV: DIGV, .GVInfo: DIE}; |
3265 | GlobalVariables.emplace_back(Args: std::move(CVGV)); |
3266 | } |
3267 | |
3268 | const auto *GV = GlobalMap.lookup(Val: GVE); |
3269 | if (!GV || GV->isDeclarationForLinker()) |
3270 | continue; |
3271 | |
3272 | DIScope *Scope = DIGV->getScope(); |
3273 | SmallVector<CVGlobalVariable, 1> *VariableList; |
3274 | if (Scope && isa<DILocalScope>(Val: Scope)) { |
3275 | // Locate a global variable list for this scope, creating one if |
3276 | // necessary. |
3277 | auto Insertion = ScopeGlobals.insert( |
3278 | KV: {Scope, std::unique_ptr<GlobalVariableList>()}); |
3279 | if (Insertion.second) |
3280 | Insertion.first->second = std::make_unique<GlobalVariableList>(); |
3281 | VariableList = Insertion.first->second.get(); |
3282 | } else if (GV->hasComdat()) |
3283 | // Emit this global variable into a COMDAT section. |
3284 | VariableList = &ComdatVariables; |
3285 | else |
3286 | // Emit this global variable in a single global symbol section. |
3287 | VariableList = &GlobalVariables; |
3288 | CVGlobalVariable CVGV = {.DIGV: DIGV, .GVInfo: GV}; |
3289 | VariableList->emplace_back(Args: std::move(CVGV)); |
3290 | } |
3291 | } |
3292 | } |
3293 | |
3294 | void CodeViewDebug::collectDebugInfoForGlobals() { |
3295 | for (const CVGlobalVariable &CVGV : GlobalVariables) { |
3296 | const DIGlobalVariable *DIGV = CVGV.DIGV; |
3297 | const DIScope *Scope = DIGV->getScope(); |
3298 | getCompleteTypeIndex(Ty: DIGV->getType()); |
3299 | getFullyQualifiedName(Scope, Name: DIGV->getName()); |
3300 | } |
3301 | |
3302 | for (const CVGlobalVariable &CVGV : ComdatVariables) { |
3303 | const DIGlobalVariable *DIGV = CVGV.DIGV; |
3304 | const DIScope *Scope = DIGV->getScope(); |
3305 | getCompleteTypeIndex(Ty: DIGV->getType()); |
3306 | getFullyQualifiedName(Scope, Name: DIGV->getName()); |
3307 | } |
3308 | } |
3309 | |
3310 | void CodeViewDebug::emitDebugInfoForGlobals() { |
3311 | // First, emit all globals that are not in a comdat in a single symbol |
3312 | // substream. MSVC doesn't like it if the substream is empty, so only open |
3313 | // it if we have at least one global to emit. |
3314 | switchToDebugSectionForSymbol(GVSym: nullptr); |
3315 | if (!GlobalVariables.empty() || !StaticConstMembers.empty()) { |
3316 | OS.AddComment(T: "Symbol subsection for globals" ); |
3317 | MCSymbol *EndLabel = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
3318 | emitGlobalVariableList(Globals: GlobalVariables); |
3319 | emitStaticConstMemberList(); |
3320 | endCVSubsection(EndLabel); |
3321 | } |
3322 | |
3323 | // Second, emit each global that is in a comdat into its own .debug$S |
3324 | // section along with its own symbol substream. |
3325 | for (const CVGlobalVariable &CVGV : ComdatVariables) { |
3326 | const GlobalVariable *GV = cast<const GlobalVariable *>(Val: CVGV.GVInfo); |
3327 | MCSymbol *GVSym = Asm->getSymbol(GV); |
3328 | OS.AddComment(T: "Symbol subsection for " + |
3329 | Twine(GlobalValue::dropLLVMManglingEscape(Name: GV->getName()))); |
3330 | switchToDebugSectionForSymbol(GVSym); |
3331 | MCSymbol *EndLabel = beginCVSubsection(Kind: DebugSubsectionKind::Symbols); |
3332 | // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions. |
3333 | emitDebugInfoForGlobal(CVGV); |
3334 | endCVSubsection(EndLabel); |
3335 | } |
3336 | } |
3337 | |
3338 | void CodeViewDebug::emitDebugInfoForRetainedTypes() { |
3339 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata(Name: "llvm.dbg.cu" ); |
3340 | for (const MDNode *Node : CUs->operands()) { |
3341 | for (auto *Ty : cast<DICompileUnit>(Val: Node)->getRetainedTypes()) { |
3342 | if (DIType *RT = dyn_cast<DIType>(Val: Ty)) { |
3343 | getTypeIndex(Ty: RT); |
3344 | // FIXME: Add to global/local DTU list. |
3345 | } |
3346 | } |
3347 | } |
3348 | } |
3349 | |
3350 | // Emit each global variable in the specified array. |
3351 | void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) { |
3352 | for (const CVGlobalVariable &CVGV : Globals) { |
3353 | // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions. |
3354 | emitDebugInfoForGlobal(CVGV); |
3355 | } |
3356 | } |
3357 | |
3358 | void CodeViewDebug::emitConstantSymbolRecord(const DIType *DTy, APSInt &Value, |
3359 | const std::string &QualifiedName) { |
3360 | MCSymbol *SConstantEnd = beginSymbolRecord(SymKind: SymbolKind::S_CONSTANT); |
3361 | OS.AddComment(T: "Type" ); |
3362 | OS.emitInt32(Value: getTypeIndex(Ty: DTy).getIndex()); |
3363 | |
3364 | OS.AddComment(T: "Value" ); |
3365 | |
3366 | // Encoded integers shouldn't need more than 10 bytes. |
3367 | uint8_t Data[10]; |
3368 | BinaryStreamWriter Writer(Data, llvm::endianness::little); |
3369 | CodeViewRecordIO IO(Writer); |
3370 | cantFail(Err: IO.mapEncodedInteger(Value)); |
3371 | StringRef SRef((char *)Data, Writer.getOffset()); |
3372 | OS.emitBinaryData(Data: SRef); |
3373 | |
3374 | OS.AddComment(T: "Name" ); |
3375 | emitNullTerminatedSymbolName(OS, S: QualifiedName); |
3376 | endSymbolRecord(SymEnd: SConstantEnd); |
3377 | } |
3378 | |
3379 | void CodeViewDebug::emitStaticConstMemberList() { |
3380 | for (const DIDerivedType *DTy : StaticConstMembers) { |
3381 | const DIScope *Scope = DTy->getScope(); |
3382 | |
3383 | APSInt Value; |
3384 | if (const ConstantInt *CI = |
3385 | dyn_cast_or_null<ConstantInt>(Val: DTy->getConstant())) |
3386 | Value = APSInt(CI->getValue(), |
3387 | DebugHandlerBase::isUnsignedDIType(Ty: DTy->getBaseType())); |
3388 | else if (const ConstantFP *CFP = |
3389 | dyn_cast_or_null<ConstantFP>(Val: DTy->getConstant())) |
3390 | Value = APSInt(CFP->getValueAPF().bitcastToAPInt(), true); |
3391 | else |
3392 | llvm_unreachable("cannot emit a constant without a value" ); |
3393 | |
3394 | emitConstantSymbolRecord(DTy: DTy->getBaseType(), Value, |
3395 | QualifiedName: getFullyQualifiedName(Scope, Name: DTy->getName())); |
3396 | } |
3397 | } |
3398 | |
3399 | static bool isFloatDIType(const DIType *Ty) { |
3400 | if (isa<DICompositeType>(Val: Ty)) |
3401 | return false; |
3402 | |
3403 | if (auto *DTy = dyn_cast<DIDerivedType>(Val: Ty)) { |
3404 | dwarf::Tag T = (dwarf::Tag)Ty->getTag(); |
3405 | if (T == dwarf::DW_TAG_pointer_type || |
3406 | T == dwarf::DW_TAG_ptr_to_member_type || |
3407 | T == dwarf::DW_TAG_reference_type || |
3408 | T == dwarf::DW_TAG_rvalue_reference_type) |
3409 | return false; |
3410 | assert(DTy->getBaseType() && "Expected valid base type" ); |
3411 | return isFloatDIType(Ty: DTy->getBaseType()); |
3412 | } |
3413 | |
3414 | auto *BTy = cast<DIBasicType>(Val: Ty); |
3415 | return (BTy->getEncoding() == dwarf::DW_ATE_float); |
3416 | } |
3417 | |
3418 | void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) { |
3419 | const DIGlobalVariable *DIGV = CVGV.DIGV; |
3420 | |
3421 | const DIScope *Scope = DIGV->getScope(); |
3422 | // For static data members, get the scope from the declaration. |
3423 | if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>( |
3424 | Val: DIGV->getRawStaticDataMemberDeclaration())) |
3425 | Scope = MemberDecl->getScope(); |
3426 | // For static local variables and Fortran, the scoping portion is elided |
3427 | // in its name so that we can reference the variable in the command line |
3428 | // of the VS debugger. |
3429 | std::string QualifiedName = |
3430 | (moduleIsInFortran() || (Scope && isa<DILocalScope>(Val: Scope))) |
3431 | ? std::string(DIGV->getName()) |
3432 | : getFullyQualifiedName(Scope, Name: DIGV->getName()); |
3433 | |
3434 | if (const GlobalVariable *GV = |
3435 | dyn_cast_if_present<const GlobalVariable *>(Val: CVGV.GVInfo)) { |
3436 | // DataSym record, see SymbolRecord.h for more info. Thread local data |
3437 | // happens to have the same format as global data. |
3438 | MCSymbol *GVSym = Asm->getSymbol(GV); |
3439 | SymbolKind DataSym = GV->isThreadLocal() |
3440 | ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD32 |
3441 | : SymbolKind::S_GTHREAD32) |
3442 | : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA32 |
3443 | : SymbolKind::S_GDATA32); |
3444 | MCSymbol *DataEnd = beginSymbolRecord(SymKind: DataSym); |
3445 | OS.AddComment(T: "Type" ); |
3446 | OS.emitInt32(Value: getCompleteTypeIndex(Ty: DIGV->getType()).getIndex()); |
3447 | OS.AddComment(T: "DataOffset" ); |
3448 | |
3449 | // Use the offset seen while collecting info on globals. |
3450 | uint64_t Offset = CVGlobalVariableOffsets.lookup(Val: DIGV); |
3451 | OS.emitCOFFSecRel32(Symbol: GVSym, Offset); |
3452 | |
3453 | OS.AddComment(T: "Segment" ); |
3454 | OS.emitCOFFSectionIndex(Symbol: GVSym); |
3455 | OS.AddComment(T: "Name" ); |
3456 | const unsigned LengthOfDataRecord = 12; |
3457 | emitNullTerminatedSymbolName(OS, S: QualifiedName, MaxFixedRecordLength: LengthOfDataRecord); |
3458 | endSymbolRecord(SymEnd: DataEnd); |
3459 | } else { |
3460 | const DIExpression *DIE = cast<const DIExpression *>(Val: CVGV.GVInfo); |
3461 | assert(DIE->isConstant() && |
3462 | "Global constant variables must contain a constant expression." ); |
3463 | |
3464 | // Use unsigned for floats. |
3465 | bool isUnsigned = isFloatDIType(Ty: DIGV->getType()) |
3466 | ? true |
3467 | : DebugHandlerBase::isUnsignedDIType(Ty: DIGV->getType()); |
3468 | APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(I: 1)), isUnsigned); |
3469 | emitConstantSymbolRecord(DTy: DIGV->getType(), Value, QualifiedName); |
3470 | } |
3471 | } |
3472 | |
3473 | void forEachJumpTableBranch( |
3474 | const MachineFunction *MF, bool isThumb, |
3475 | const std::function<void(const MachineJumpTableInfo &, const MachineInstr &, |
3476 | int64_t)> &Callback) { |
3477 | auto JTI = MF->getJumpTableInfo(); |
3478 | if (JTI && !JTI->isEmpty()) { |
3479 | #ifndef NDEBUG |
3480 | auto UsedJTs = llvm::SmallBitVector(JTI->getJumpTables().size()); |
3481 | #endif |
3482 | for (const auto &MBB : *MF) { |
3483 | // Search for indirect branches... |
3484 | const auto LastMI = MBB.getFirstTerminator(); |
3485 | if (LastMI != MBB.end() && LastMI->isIndirectBranch()) { |
3486 | if (isThumb) { |
3487 | // ... that directly use jump table operands. |
3488 | // NOTE: ARM uses pattern matching to lower its BR_JT SDNode to |
3489 | // machine instructions, hence inserting a JUMP_TABLE_DEBUG_INFO node |
3490 | // interferes with this process *but* the resulting pseudo-instruction |
3491 | // uses a Jump Table operand, so extract the jump table index directly |
3492 | // from that. |
3493 | for (const auto &MO : LastMI->operands()) { |
3494 | if (MO.isJTI()) { |
3495 | unsigned Index = MO.getIndex(); |
3496 | #ifndef NDEBUG |
3497 | UsedJTs.set(Index); |
3498 | #endif |
3499 | Callback(*JTI, *LastMI, Index); |
3500 | break; |
3501 | } |
3502 | } |
3503 | } else { |
3504 | // ... that have jump table debug info. |
3505 | // NOTE: The debug info is inserted as a JUMP_TABLE_DEBUG_INFO node |
3506 | // when lowering the BR_JT SDNode to an indirect branch. |
3507 | for (auto I = MBB.instr_rbegin(), E = MBB.instr_rend(); I != E; ++I) { |
3508 | if (I->isJumpTableDebugInfo()) { |
3509 | unsigned Index = I->getOperand(i: 0).getImm(); |
3510 | #ifndef NDEBUG |
3511 | UsedJTs.set(Index); |
3512 | #endif |
3513 | Callback(*JTI, *LastMI, Index); |
3514 | break; |
3515 | } |
3516 | } |
3517 | } |
3518 | } |
3519 | } |
3520 | #ifndef NDEBUG |
3521 | assert(UsedJTs.all() && |
3522 | "Some of jump tables were not used in a debug info instruction" ); |
3523 | #endif |
3524 | } |
3525 | } |
3526 | |
3527 | void CodeViewDebug::discoverJumpTableBranches(const MachineFunction *MF, |
3528 | bool isThumb) { |
3529 | forEachJumpTableBranch( |
3530 | MF, isThumb, |
3531 | Callback: [this](const MachineJumpTableInfo &, const MachineInstr &BranchMI, |
3532 | int64_t) { requestLabelBeforeInsn(MI: &BranchMI); }); |
3533 | } |
3534 | |
3535 | void CodeViewDebug::collectDebugInfoForJumpTables(const MachineFunction *MF, |
3536 | bool isThumb) { |
3537 | forEachJumpTableBranch( |
3538 | MF, isThumb, |
3539 | Callback: [this, MF](const MachineJumpTableInfo &JTI, const MachineInstr &BranchMI, |
3540 | int64_t JumpTableIndex) { |
3541 | // For label-difference jump tables, find the base expression. |
3542 | // Otherwise the jump table uses an absolute address (so no base |
3543 | // is required). |
3544 | const MCSymbol *Base; |
3545 | uint64_t BaseOffset = 0; |
3546 | const MCSymbol *Branch = getLabelBeforeInsn(MI: &BranchMI); |
3547 | JumpTableEntrySize EntrySize; |
3548 | switch (JTI.getEntryKind()) { |
3549 | case MachineJumpTableInfo::EK_Custom32: |
3550 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
3551 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
3552 | llvm_unreachable( |
3553 | "EK_Custom32, EK_GPRel32BlockAddress, and " |
3554 | "EK_GPRel64BlockAddress should never be emitted for COFF" ); |
3555 | case MachineJumpTableInfo::EK_BlockAddress: |
3556 | // Each entry is an absolute address. |
3557 | EntrySize = JumpTableEntrySize::Pointer; |
3558 | Base = nullptr; |
3559 | break; |
3560 | case MachineJumpTableInfo::EK_Inline: |
3561 | case MachineJumpTableInfo::EK_LabelDifference32: |
3562 | case MachineJumpTableInfo::EK_LabelDifference64: |
3563 | // Ask the AsmPrinter. |
3564 | std::tie(args&: Base, args&: BaseOffset, args&: Branch, args&: EntrySize) = |
3565 | Asm->getCodeViewJumpTableInfo(JTI: JumpTableIndex, BranchInstr: &BranchMI, BranchLabel: Branch); |
3566 | break; |
3567 | } |
3568 | |
3569 | CurFn->JumpTables.push_back( |
3570 | x: {.EntrySize: EntrySize, .Base: Base, .BaseOffset: BaseOffset, .Branch: Branch, |
3571 | .Table: MF->getJTISymbol(JTI: JumpTableIndex, Ctx&: MMI->getContext()), |
3572 | .TableSize: JTI.getJumpTables()[JumpTableIndex].MBBs.size()}); |
3573 | }); |
3574 | } |
3575 | |
3576 | void CodeViewDebug::emitDebugInfoForJumpTables(const FunctionInfo &FI) { |
3577 | for (auto JumpTable : FI.JumpTables) { |
3578 | MCSymbol *JumpTableEnd = beginSymbolRecord(SymKind: SymbolKind::S_ARMSWITCHTABLE); |
3579 | if (JumpTable.Base) { |
3580 | OS.AddComment(T: "Base offset" ); |
3581 | OS.emitCOFFSecRel32(Symbol: JumpTable.Base, Offset: JumpTable.BaseOffset); |
3582 | OS.AddComment(T: "Base section index" ); |
3583 | OS.emitCOFFSectionIndex(Symbol: JumpTable.Base); |
3584 | } else { |
3585 | OS.AddComment(T: "Base offset" ); |
3586 | OS.emitInt32(Value: 0); |
3587 | OS.AddComment(T: "Base section index" ); |
3588 | OS.emitInt16(Value: 0); |
3589 | } |
3590 | OS.AddComment(T: "Switch type" ); |
3591 | OS.emitInt16(Value: static_cast<uint16_t>(JumpTable.EntrySize)); |
3592 | OS.AddComment(T: "Branch offset" ); |
3593 | OS.emitCOFFSecRel32(Symbol: JumpTable.Branch, /*Offset=*/0); |
3594 | OS.AddComment(T: "Table offset" ); |
3595 | OS.emitCOFFSecRel32(Symbol: JumpTable.Table, /*Offset=*/0); |
3596 | OS.AddComment(T: "Branch section index" ); |
3597 | OS.emitCOFFSectionIndex(Symbol: JumpTable.Branch); |
3598 | OS.AddComment(T: "Table section index" ); |
3599 | OS.emitCOFFSectionIndex(Symbol: JumpTable.Table); |
3600 | OS.AddComment(T: "Entries count" ); |
3601 | OS.emitInt32(Value: JumpTable.TableSize); |
3602 | endSymbolRecord(SymEnd: JumpTableEnd); |
3603 | } |
3604 | } |
3605 | |
3606 | void CodeViewDebug::emitInlinees( |
3607 | const SmallSet<codeview::TypeIndex, 1> &Inlinees) { |
3608 | // Divide the list of inlinees into chunks such that each chunk fits within |
3609 | // one record. |
3610 | constexpr size_t ChunkSize = |
3611 | (MaxRecordLength - sizeof(SymbolKind) - sizeof(uint32_t)) / |
3612 | sizeof(uint32_t); |
3613 | |
3614 | SmallVector<TypeIndex> SortedInlinees{Inlinees.begin(), Inlinees.end()}; |
3615 | llvm::sort(C&: SortedInlinees); |
3616 | |
3617 | size_t CurrentIndex = 0; |
3618 | while (CurrentIndex < SortedInlinees.size()) { |
3619 | auto Symbol = beginSymbolRecord(SymKind: SymbolKind::S_INLINEES); |
3620 | auto CurrentChunkSize = |
3621 | std::min(a: ChunkSize, b: SortedInlinees.size() - CurrentIndex); |
3622 | OS.AddComment(T: "Count" ); |
3623 | OS.emitInt32(Value: CurrentChunkSize); |
3624 | |
3625 | const size_t CurrentChunkEnd = CurrentIndex + CurrentChunkSize; |
3626 | for (; CurrentIndex < CurrentChunkEnd; ++CurrentIndex) { |
3627 | OS.AddComment(T: "Inlinee" ); |
3628 | OS.emitInt32(Value: SortedInlinees[CurrentIndex].getIndex()); |
3629 | } |
3630 | endSymbolRecord(SymEnd: Symbol); |
3631 | } |
3632 | } |
3633 | |