1 //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing Microsoft CodeView debug info.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeViewDebug.h"
15 #include "llvm/DebugInfo/CodeView/CodeView.h"
16 #include "llvm/DebugInfo/CodeView/Line.h"
17 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
18 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
19 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/COFF.h"
23 #include "llvm/Target/TargetSubtargetInfo.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetFrameLowering.h"
26 
27 using namespace llvm;
28 using namespace llvm::codeview;
29 
30 CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
31     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) {
32   // If module doesn't have named metadata anchors or COFF debug section
33   // is not available, skip any debug info related stuff.
34   if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
35       !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {
36     Asm = nullptr;
37     return;
38   }
39 
40   // Tell MMI that we have debug info.
41   MMI->setDebugInfoAvailability(true);
42 }
43 
44 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
45   std::string &Filepath = FileToFilepathMap[File];
46   if (!Filepath.empty())
47     return Filepath;
48 
49   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
50 
51   // Clang emits directory and relative filename info into the IR, but CodeView
52   // operates on full paths.  We could change Clang to emit full paths too, but
53   // that would increase the IR size and probably not needed for other users.
54   // For now, just concatenate and canonicalize the path here.
55   if (Filename.find(':') == 1)
56     Filepath = Filename;
57   else
58     Filepath = (Dir + "\\" + Filename).str();
59 
60   // Canonicalize the path.  We have to do it textually because we may no longer
61   // have access the file in the filesystem.
62   // First, replace all slashes with backslashes.
63   std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
64 
65   // Remove all "\.\" with "\".
66   size_t Cursor = 0;
67   while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
68     Filepath.erase(Cursor, 2);
69 
70   // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
71   // path should be well-formatted, e.g. start with a drive letter, etc.
72   Cursor = 0;
73   while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
74     // Something's wrong if the path starts with "\..\", abort.
75     if (Cursor == 0)
76       break;
77 
78     size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
79     if (PrevSlash == std::string::npos)
80       // Something's wrong, abort.
81       break;
82 
83     Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
84     // The next ".." might be following the one we've just erased.
85     Cursor = PrevSlash;
86   }
87 
88   // Remove all duplicate backslashes.
89   Cursor = 0;
90   while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
91     Filepath.erase(Cursor, 1);
92 
93   return Filepath;
94 }
95 
96 unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
97   unsigned NextId = FileIdMap.size() + 1;
98   auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
99   if (Insertion.second) {
100     // We have to compute the full filepath and emit a .cv_file directive.
101     StringRef FullPath = getFullFilepath(F);
102     NextId = OS.EmitCVFileDirective(NextId, FullPath);
103     assert(NextId == FileIdMap.size() && ".cv_file directive failed");
104   }
105   return Insertion.first->second;
106 }
107 
108 CodeViewDebug::InlineSite &
109 CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
110                              const DISubprogram *Inlinee) {
111   auto Insertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
112   InlineSite *Site = &Insertion.first->second;
113   if (Insertion.second) {
114     Site->SiteFuncId = NextFuncId++;
115     Site->Inlinee = Inlinee;
116     InlinedSubprograms.insert(Inlinee);
117   }
118   return *Site;
119 }
120 
121 void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
122                                         const DILocation *InlinedAt) {
123   if (InlinedAt) {
124     // This variable was inlined. Associate it with the InlineSite.
125     const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
126     InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
127     Site.InlinedLocals.emplace_back(Var);
128   } else {
129     // This variable goes in the main ProcSym.
130     CurFn->Locals.emplace_back(Var);
131   }
132 }
133 
134 static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
135                                const DILocation *Loc) {
136   auto B = Locs.begin(), E = Locs.end();
137   if (std::find(B, E, Loc) == E)
138     Locs.push_back(Loc);
139 }
140 
141 void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
142                                         const MachineFunction *MF) {
143   // Skip this instruction if it has the same location as the previous one.
144   if (DL == CurFn->LastLoc)
145     return;
146 
147   const DIScope *Scope = DL.get()->getScope();
148   if (!Scope)
149     return;
150 
151   // Skip this line if it is longer than the maximum we can record.
152   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
153   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
154       LI.isNeverStepInto())
155     return;
156 
157   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
158   if (CI.getStartColumn() != DL.getCol())
159     return;
160 
161   if (!CurFn->HaveLineInfo)
162     CurFn->HaveLineInfo = true;
163   unsigned FileId = 0;
164   if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
165     FileId = CurFn->LastFileId;
166   else
167     FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
168   CurFn->LastLoc = DL;
169 
170   unsigned FuncId = CurFn->FuncId;
171   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
172     const DILocation *Loc = DL.get();
173 
174     // If this location was actually inlined from somewhere else, give it the ID
175     // of the inline call site.
176     FuncId =
177         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
178 
179     // Ensure we have links in the tree of inline call sites.
180     bool FirstLoc = true;
181     while ((SiteLoc = Loc->getInlinedAt())) {
182       InlineSite &Site =
183           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
184       if (!FirstLoc)
185         addLocIfNotPresent(Site.ChildSites, Loc);
186       FirstLoc = false;
187       Loc = SiteLoc;
188     }
189     addLocIfNotPresent(CurFn->ChildSites, Loc);
190   }
191 
192   OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
193                         /*PrologueEnd=*/false,
194                         /*IsStmt=*/false, DL->getFilename());
195 }
196 
197 void CodeViewDebug::endModule() {
198   if (FnDebugInfo.empty())
199     return;
200 
201   emitTypeInformation();
202 
203   // FIXME: For functions that are comdat, we should emit separate .debug$S
204   // sections that are comdat associative with the main function instead of
205   // having one big .debug$S section.
206   assert(Asm != nullptr);
207   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
208   OS.AddComment("Debug section magic");
209   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
210 
211   // The COFF .debug$S section consists of several subsections, each starting
212   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
213   // of the payload followed by the payload itself.  The subsections are 4-byte
214   // aligned.
215 
216   // Make a subsection for all the inlined subprograms.
217   emitInlineeLinesSubsection();
218 
219   // Emit per-function debug information.
220   for (auto &P : FnDebugInfo)
221     emitDebugInfoForFunction(P.first, P.second);
222 
223   // This subsection holds a file index to offset in string table table.
224   OS.AddComment("File index to string table offset subsection");
225   OS.EmitCVFileChecksumsDirective();
226 
227   // This subsection holds the string table.
228   OS.AddComment("String table");
229   OS.EmitCVStringTableDirective();
230 
231   clear();
232 }
233 
234 void CodeViewDebug::emitTypeInformation() {
235   // Start the .debug$T section with 0x4.
236   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
237   OS.AddComment("Debug section magic");
238   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
239 
240   NamedMDNode *CU_Nodes =
241       MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
242   if (!CU_Nodes)
243     return;
244 
245   // This type info currently only holds function ids for use with inline call
246   // frame info. All functions are assigned a simple 'void ()' type. Emit that
247   // type here.
248   TypeIndex ArgListIdx = getNextTypeIndex();
249   OS.AddComment("Type record length");
250   OS.EmitIntValue(2 + sizeof(ArgList), 2);
251   OS.AddComment("Leaf type: LF_ARGLIST");
252   OS.EmitIntValue(LF_ARGLIST, 2);
253   OS.AddComment("Number of arguments");
254   OS.EmitIntValue(0, 4);
255 
256   TypeIndex VoidProcIdx = getNextTypeIndex();
257   OS.AddComment("Type record length");
258   OS.EmitIntValue(2 + sizeof(ProcedureType), 2);
259   OS.AddComment("Leaf type: LF_PROCEDURE");
260   OS.EmitIntValue(LF_PROCEDURE, 2);
261   OS.AddComment("Return type index");
262   OS.EmitIntValue(TypeIndex::Void().getIndex(), 4);
263   OS.AddComment("Calling convention");
264   OS.EmitIntValue(char(CallingConvention::NearC), 1);
265   OS.AddComment("Function options");
266   OS.EmitIntValue(char(FunctionOptions::None), 1);
267   OS.AddComment("# of parameters");
268   OS.EmitIntValue(0, 2);
269   OS.AddComment("Argument list type index");
270   OS.EmitIntValue(ArgListIdx.getIndex(), 4);
271 
272   for (MDNode *N : CU_Nodes->operands()) {
273     auto *CUNode = cast<DICompileUnit>(N);
274     for (auto *SP : CUNode->getSubprograms()) {
275       StringRef DisplayName = SP->getDisplayName();
276       OS.AddComment("Type record length");
277       OS.EmitIntValue(2 + sizeof(FuncId) + DisplayName.size() + 1, 2);
278       OS.AddComment("Leaf type: LF_FUNC_ID");
279       OS.EmitIntValue(LF_FUNC_ID, 2);
280 
281       OS.AddComment("Scope type index");
282       OS.EmitIntValue(TypeIndex().getIndex(), 4);
283       OS.AddComment("Function type");
284       OS.EmitIntValue(VoidProcIdx.getIndex(), 4);
285       {
286         SmallString<32> NullTerminatedString(DisplayName);
287         if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0')
288           NullTerminatedString.push_back('\0');
289         OS.AddComment("Function name");
290         OS.EmitBytes(NullTerminatedString);
291       }
292 
293       TypeIndex FuncIdIdx = getNextTypeIndex();
294       SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx));
295     }
296   }
297 }
298 
299 void CodeViewDebug::emitInlineeLinesSubsection() {
300   if (InlinedSubprograms.empty())
301     return;
302 
303   MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
304            *InlineEnd = MMI->getContext().createTempSymbol();
305 
306   OS.AddComment("Inlinee lines subsection");
307   OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
308   OS.AddComment("Subsection size");
309   OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
310   OS.EmitLabel(InlineBegin);
311 
312   // We don't provide any extra file info.
313   // FIXME: Find out if debuggers use this info.
314   OS.AddComment("Inlinee lines signature");
315   OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
316 
317   for (const DISubprogram *SP : InlinedSubprograms) {
318     OS.AddBlankLine();
319     TypeIndex TypeId = SubprogramToFuncId[SP];
320     unsigned FileId = maybeRecordFile(SP->getFile());
321     OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
322                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
323     OS.AddBlankLine();
324     // The filechecksum table uses 8 byte entries for now, and file ids start at
325     // 1.
326     unsigned FileOffset = (FileId - 1) * 8;
327     OS.AddComment("Type index of inlined function");
328     OS.EmitIntValue(TypeId.getIndex(), 4);
329     OS.AddComment("Offset into filechecksum table");
330     OS.EmitIntValue(FileOffset, 4);
331     OS.AddComment("Starting line number");
332     OS.EmitIntValue(SP->getLine(), 4);
333   }
334 
335   OS.EmitLabel(InlineEnd);
336 }
337 
338 void CodeViewDebug::collectInlineSiteChildren(
339     SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
340     const InlineSite &Site) {
341   for (const DILocation *ChildSiteLoc : Site.ChildSites) {
342     auto I = FI.InlineSites.find(ChildSiteLoc);
343     const InlineSite &ChildSite = I->second;
344     Children.push_back(ChildSite.SiteFuncId);
345     collectInlineSiteChildren(Children, FI, ChildSite);
346   }
347 }
348 
349 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
350                                         const DILocation *InlinedAt,
351                                         const InlineSite &Site) {
352   MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
353            *InlineEnd = MMI->getContext().createTempSymbol();
354 
355   assert(SubprogramToFuncId.count(Site.Inlinee));
356   TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee];
357 
358   // SymbolRecord
359   OS.AddComment("Record length");
360   OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2);   // RecordLength
361   OS.EmitLabel(InlineBegin);
362   OS.AddComment("Record kind: S_INLINESITE");
363   OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind
364 
365   OS.AddComment("PtrParent");
366   OS.EmitIntValue(0, 4);
367   OS.AddComment("PtrEnd");
368   OS.EmitIntValue(0, 4);
369   OS.AddComment("Inlinee type index");
370   OS.EmitIntValue(InlineeIdx.getIndex(), 4);
371 
372   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
373   unsigned StartLineNum = Site.Inlinee->getLine();
374   SmallVector<unsigned, 3> SecondaryFuncIds;
375   collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
376 
377   OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
378                                     FI.Begin, FI.End, SecondaryFuncIds);
379 
380   OS.EmitLabel(InlineEnd);
381 
382   for (const LocalVariable &Var : Site.InlinedLocals)
383     emitLocalVariable(Var);
384 
385   // Recurse on child inlined call sites before closing the scope.
386   for (const DILocation *ChildSite : Site.ChildSites) {
387     auto I = FI.InlineSites.find(ChildSite);
388     assert(I != FI.InlineSites.end() &&
389            "child site not in function inline site map");
390     emitInlinedCallSite(FI, ChildSite, I->second);
391   }
392 
393   // Close the scope.
394   OS.AddComment("Record length");
395   OS.EmitIntValue(2, 2);                                  // RecordLength
396   OS.AddComment("Record kind: S_INLINESITE_END");
397   OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind
398 }
399 
400 static void emitNullTerminatedString(MCStreamer &OS, StringRef S,
401                                      size_t MaxSize) {
402   S = S.substr(0, MaxSize);
403   SmallString<32> NullTerminatedString(S);
404   NullTerminatedString.push_back('\0');
405   OS.EmitBytes(NullTerminatedString);
406 }
407 
408 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
409                                              FunctionInfo &FI) {
410   // For each function there is a separate subsection
411   // which holds the PC to file:line table.
412   const MCSymbol *Fn = Asm->getSymbol(GV);
413   assert(Fn);
414 
415   StringRef FuncName;
416   if (auto *SP = GV->getSubprogram())
417     FuncName = SP->getDisplayName();
418 
419   // If our DISubprogram name is empty, use the mangled name.
420   if (FuncName.empty())
421     FuncName = GlobalValue::getRealLinkageName(GV->getName());
422 
423   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
424   MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
425            *SymbolsEnd = MMI->getContext().createTempSymbol();
426   OS.AddComment("Symbol subsection for " + Twine(FuncName));
427   OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
428   OS.AddComment("Subsection size");
429   OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
430   OS.EmitLabel(SymbolsBegin);
431   {
432     MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
433              *ProcRecordEnd = MMI->getContext().createTempSymbol();
434     OS.AddComment("Record length");
435     OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
436     OS.EmitLabel(ProcRecordBegin);
437 
438     OS.AddComment("Record kind: S_GPROC32_ID");
439     OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2);
440 
441     // These fields are filled in by tools like CVPACK which run after the fact.
442     OS.AddComment("PtrParent");
443     OS.EmitIntValue(0, 4);
444     OS.AddComment("PtrEnd");
445     OS.EmitIntValue(0, 4);
446     OS.AddComment("PtrNext");
447     OS.EmitIntValue(0, 4);
448     // This is the important bit that tells the debugger where the function
449     // code is located and what's its size:
450     OS.AddComment("Code size");
451     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
452     OS.AddComment("Offset after prologue");
453     OS.EmitIntValue(0, 4);
454     OS.AddComment("Offset before epilogue");
455     OS.EmitIntValue(0, 4);
456     OS.AddComment("Function type index");
457     OS.EmitIntValue(0, 4);
458     OS.AddComment("Function section relative address");
459     OS.EmitCOFFSecRel32(Fn);
460     OS.AddComment("Function section index");
461     OS.EmitCOFFSectionIndex(Fn);
462     OS.AddComment("Flags");
463     OS.EmitIntValue(0, 1);
464     // Emit the function display name as a null-terminated string.
465     OS.AddComment("Function name");
466     // Truncate the name so we won't overflow the record length field.
467     emitNullTerminatedString(OS, FuncName, 0xffd9);
468     OS.EmitLabel(ProcRecordEnd);
469 
470     for (const LocalVariable &Var : FI.Locals)
471       emitLocalVariable(Var);
472 
473     // Emit inlined call site information. Only emit functions inlined directly
474     // into the parent function. We'll emit the other sites recursively as part
475     // of their parent inline site.
476     for (const DILocation *InlinedAt : FI.ChildSites) {
477       auto I = FI.InlineSites.find(InlinedAt);
478       assert(I != FI.InlineSites.end() &&
479              "child site not in function inline site map");
480       emitInlinedCallSite(FI, InlinedAt, I->second);
481     }
482 
483     // We're done with this function.
484     OS.AddComment("Record length");
485     OS.EmitIntValue(0x0002, 2);
486     OS.AddComment("Record kind: S_PROC_ID_END");
487     OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2);
488   }
489   OS.EmitLabel(SymbolsEnd);
490   // Every subsection must be aligned to a 4-byte boundary.
491   OS.EmitValueToAlignment(4);
492 
493   // We have an assembler directive that takes care of the whole line table.
494   OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
495 }
496 
497 CodeViewDebug::LocalVarDefRange
498 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
499   LocalVarDefRange DR;
500   DR.InMemory = -1;
501   DR.DataOffset = Offset;
502   assert(DR.DataOffset == Offset && "truncation");
503   DR.StructOffset = 0;
504   DR.CVRegister = CVRegister;
505   return DR;
506 }
507 
508 CodeViewDebug::LocalVarDefRange
509 CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
510   LocalVarDefRange DR;
511   DR.InMemory = 0;
512   DR.DataOffset = 0;
513   DR.StructOffset = 0;
514   DR.CVRegister = CVRegister;
515   return DR;
516 }
517 
518 void CodeViewDebug::collectVariableInfoFromMMITable(
519     DenseSet<InlinedVariable> &Processed) {
520   const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
521   const TargetFrameLowering *TFI = TSI.getFrameLowering();
522   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
523 
524   for (const MachineModuleInfo::VariableDbgInfo &VI :
525        MMI->getVariableDbgInfo()) {
526     if (!VI.Var)
527       continue;
528     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
529            "Expected inlined-at fields to agree");
530 
531     Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
532     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
533 
534     // If variable scope is not found then skip this variable.
535     if (!Scope)
536       continue;
537 
538     // Get the frame register used and the offset.
539     unsigned FrameReg = 0;
540     int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
541     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
542 
543     // Calculate the label ranges.
544     LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
545     for (const InsnRange &Range : Scope->getRanges()) {
546       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
547       const MCSymbol *End = getLabelAfterInsn(Range.second);
548       End = End ? End : Asm->getFunctionEnd();
549       DefRange.Ranges.emplace_back(Begin, End);
550     }
551 
552     LocalVariable Var;
553     Var.DIVar = VI.Var;
554     Var.DefRanges.emplace_back(std::move(DefRange));
555     recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
556   }
557 }
558 
559 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
560   DenseSet<InlinedVariable> Processed;
561   // Grab the variable info that was squirreled away in the MMI side-table.
562   collectVariableInfoFromMMITable(Processed);
563 
564   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
565 
566   for (const auto &I : DbgValues) {
567     InlinedVariable IV = I.first;
568     if (Processed.count(IV))
569       continue;
570     const DILocalVariable *DIVar = IV.first;
571     const DILocation *InlinedAt = IV.second;
572 
573     // Instruction ranges, specifying where IV is accessible.
574     const auto &Ranges = I.second;
575 
576     LexicalScope *Scope = nullptr;
577     if (InlinedAt)
578       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
579     else
580       Scope = LScopes.findLexicalScope(DIVar->getScope());
581     // If variable scope is not found then skip this variable.
582     if (!Scope)
583       continue;
584 
585     LocalVariable Var;
586     Var.DIVar = DIVar;
587 
588     // Calculate the definition ranges.
589     for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
590       const InsnRange &Range = *I;
591       const MachineInstr *DVInst = Range.first;
592       assert(DVInst->isDebugValue() && "Invalid History entry");
593       const DIExpression *DIExpr = DVInst->getDebugExpression();
594 
595       // Bail if there is a complex DWARF expression for now.
596       if (DIExpr && DIExpr->getNumElements() > 0)
597         continue;
598 
599       // Bail if operand 0 is not a valid register. This means the variable is a
600       // simple constant, or is described by a complex expression.
601       // FIXME: Find a way to represent constant variables, since they are
602       // relatively common.
603       unsigned Reg =
604           DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
605       if (Reg == 0)
606         continue;
607 
608       // Handle the two cases we can handle: indirect in memory and in register.
609       bool IsIndirect = DVInst->getOperand(1).isImm();
610       unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
611       {
612         LocalVarDefRange DefRange;
613         if (IsIndirect) {
614           int64_t Offset = DVInst->getOperand(1).getImm();
615           DefRange = createDefRangeMem(CVReg, Offset);
616         } else {
617           DefRange = createDefRangeReg(CVReg);
618         }
619         if (Var.DefRanges.empty() ||
620             Var.DefRanges.back().isDifferentLocation(DefRange)) {
621           Var.DefRanges.emplace_back(std::move(DefRange));
622         }
623       }
624 
625       // Compute the label range.
626       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
627       const MCSymbol *End = getLabelAfterInsn(Range.second);
628       if (!End) {
629         if (std::next(I) != E)
630           End = getLabelBeforeInsn(std::next(I)->first);
631         else
632           End = Asm->getFunctionEnd();
633       }
634 
635       // If the last range end is our begin, just extend the last range.
636       // Otherwise make a new range.
637       SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
638           Var.DefRanges.back().Ranges;
639       if (!Ranges.empty() && Ranges.back().second == Begin)
640         Ranges.back().second = End;
641       else
642         Ranges.emplace_back(Begin, End);
643 
644       // FIXME: Do more range combining.
645     }
646 
647     recordLocalVariable(std::move(Var), InlinedAt);
648   }
649 }
650 
651 void CodeViewDebug::beginFunction(const MachineFunction *MF) {
652   assert(!CurFn && "Can't process two functions at once!");
653 
654   if (!Asm || !MMI->hasDebugInfo())
655     return;
656 
657   DebugHandlerBase::beginFunction(MF);
658 
659   const Function *GV = MF->getFunction();
660   assert(FnDebugInfo.count(GV) == false);
661   CurFn = &FnDebugInfo[GV];
662   CurFn->FuncId = NextFuncId++;
663   CurFn->Begin = Asm->getFunctionBegin();
664 
665   // Find the end of the function prolog.  First known non-DBG_VALUE and
666   // non-frame setup location marks the beginning of the function body.
667   // FIXME: is there a simpler a way to do this? Can we just search
668   // for the first instruction of the function, not the last of the prolog?
669   DebugLoc PrologEndLoc;
670   bool EmptyPrologue = true;
671   for (const auto &MBB : *MF) {
672     for (const auto &MI : MBB) {
673       if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
674           MI.getDebugLoc()) {
675         PrologEndLoc = MI.getDebugLoc();
676         break;
677       } else if (!MI.isDebugValue()) {
678         EmptyPrologue = false;
679       }
680     }
681   }
682 
683   // Record beginning of function if we have a non-empty prologue.
684   if (PrologEndLoc && !EmptyPrologue) {
685     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
686     maybeRecordLocation(FnStartDL, MF);
687   }
688 }
689 
690 void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
691   // LocalSym record, see SymbolRecord.h for more info.
692   MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
693            *LocalEnd = MMI->getContext().createTempSymbol();
694   OS.AddComment("Record length");
695   OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
696   OS.EmitLabel(LocalBegin);
697 
698   OS.AddComment("Record kind: S_LOCAL");
699   OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2);
700 
701   uint16_t Flags = 0;
702   if (Var.DIVar->isParameter())
703     Flags |= LocalSym::IsParameter;
704   if (Var.DefRanges.empty())
705     Flags |= LocalSym::IsOptimizedOut;
706 
707   OS.AddComment("TypeIndex");
708   OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
709   OS.AddComment("Flags");
710   OS.EmitIntValue(Flags, 2);
711   // Truncate the name so we won't overflow the record length field.
712   emitNullTerminatedString(OS, Var.DIVar->getName(), 0xfff6);
713   OS.EmitLabel(LocalEnd);
714 
715   // Calculate the on disk prefix of the appropriate def range record. The
716   // records and on disk formats are described in SymbolRecords.h. BytePrefix
717   // should be big enough to hold all forms without memory allocation.
718   SmallString<20> BytePrefix;
719   for (const LocalVarDefRange &DefRange : Var.DefRanges) {
720     BytePrefix.clear();
721     // FIXME: Handle bitpieces.
722     if (DefRange.StructOffset != 0)
723       continue;
724 
725     if (DefRange.InMemory) {
726       DefRangeRegisterRelSym Sym{};
727       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
728       Sym.BaseRegister = DefRange.CVRegister;
729       Sym.Flags = 0; // Unclear what matters here.
730       Sym.BasePointerOffset = DefRange.DataOffset;
731       BytePrefix +=
732           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
733       BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
734                               sizeof(Sym) - sizeof(LocalVariableAddrRange));
735     } else {
736       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
737       DefRangeRegisterSym Sym{};
738       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
739       Sym.Register = DefRange.CVRegister;
740       Sym.MayHaveNoName = 0; // Unclear what matters here.
741       BytePrefix +=
742           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
743       BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
744                               sizeof(Sym) - sizeof(LocalVariableAddrRange));
745     }
746     OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
747   }
748 }
749 
750 void CodeViewDebug::endFunction(const MachineFunction *MF) {
751   if (!Asm || !CurFn)  // We haven't created any debug info for this function.
752     return;
753 
754   const Function *GV = MF->getFunction();
755   assert(FnDebugInfo.count(GV));
756   assert(CurFn == &FnDebugInfo[GV]);
757 
758   collectVariableInfo(GV->getSubprogram());
759 
760   DebugHandlerBase::endFunction(MF);
761 
762   // Don't emit anything if we don't have any line tables.
763   if (!CurFn->HaveLineInfo) {
764     FnDebugInfo.erase(GV);
765     CurFn = nullptr;
766     return;
767   }
768 
769   CurFn->End = Asm->getFunctionEnd();
770 
771   CurFn = nullptr;
772 }
773 
774 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
775   DebugHandlerBase::beginInstruction(MI);
776 
777   // Ignore DBG_VALUE locations and function prologue.
778   if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
779     return;
780   DebugLoc DL = MI->getDebugLoc();
781   if (DL == PrevInstLoc || !DL)
782     return;
783   maybeRecordLocation(DL, Asm->MF);
784 }
785