1//===-- llvm/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.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 line tables info into COFF files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "WinCodeViewLineTables.h"
15#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCSymbol.h"
17#include "llvm/Support/COFF.h"
18
19namespace llvm {
20
21StringRef WinCodeViewLineTables::getFullFilepath(const MDNode *S) {
22  assert(S);
23  DIDescriptor D(S);
24  assert((D.isCompileUnit() || D.isFile() || D.isSubprogram() ||
25          D.isLexicalBlockFile() || D.isLexicalBlock()) &&
26         "Unexpected scope info");
27
28  DIScope Scope(S);
29  StringRef Dir = Scope.getDirectory(),
30            Filename = Scope.getFilename();
31  char *&Result = DirAndFilenameToFilepathMap[std::make_pair(Dir, Filename)];
32  if (Result)
33    return Result;
34
35  // Clang emits directory and relative filename info into the IR, but CodeView
36  // operates on full paths.  We could change Clang to emit full paths too, but
37  // that would increase the IR size and probably not needed for other users.
38  // For now, just concatenate and canonicalize the path here.
39  std::string Filepath;
40  if (Filename.find(':') == 1)
41    Filepath = Filename;
42  else
43    Filepath = (Dir + Twine("\\") + Filename).str();
44
45  // Canonicalize the path.  We have to do it textually because we may no longer
46  // have access the file in the filesystem.
47  // First, replace all slashes with backslashes.
48  std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
49
50  // Remove all "\.\" with "\".
51  size_t Cursor = 0;
52  while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
53    Filepath.erase(Cursor, 2);
54
55  // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
56  // path should be well-formatted, e.g. start with a drive letter, etc.
57  Cursor = 0;
58  while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
59    // Something's wrong if the path starts with "\..\", abort.
60    if (Cursor == 0)
61      break;
62
63    size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
64    if (PrevSlash == std::string::npos)
65      // Something's wrong, abort.
66      break;
67
68    Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
69    // The next ".." might be following the one we've just erased.
70    Cursor = PrevSlash;
71  }
72
73  // Remove all duplicate backslashes.
74  Cursor = 0;
75  while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
76    Filepath.erase(Cursor, 1);
77
78  Result = strdup(Filepath.c_str());
79  return StringRef(Result);
80}
81
82void WinCodeViewLineTables::maybeRecordLocation(DebugLoc DL,
83                                                const MachineFunction *MF) {
84  const MDNode *Scope = DL.getScope(MF->getFunction()->getContext());
85  if (!Scope)
86    return;
87  StringRef Filename = getFullFilepath(Scope);
88
89  // Skip this instruction if it has the same file:line as the previous one.
90  assert(CurFn);
91  if (!CurFn->Instrs.empty()) {
92    const InstrInfoTy &LastInstr = InstrInfo[CurFn->Instrs.back()];
93    if (LastInstr.Filename == Filename && LastInstr.LineNumber == DL.getLine())
94      return;
95  }
96  FileNameRegistry.add(Filename);
97
98  MCSymbol *MCL = Asm->MMI->getContext().CreateTempSymbol();
99  Asm->OutStreamer.EmitLabel(MCL);
100  CurFn->Instrs.push_back(MCL);
101  InstrInfo[MCL] = InstrInfoTy(Filename, DL.getLine());
102}
103
104WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *AP)
105    : Asm(nullptr), CurFn(nullptr) {
106  MachineModuleInfo *MMI = AP->MMI;
107
108  // If module doesn't have named metadata anchors or COFF debug section
109  // is not available, skip any debug info related stuff.
110  if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
111      !AP->getObjFileLowering().getCOFFDebugSymbolsSection())
112    return;
113
114  // Tell MMI that we have debug info.
115  MMI->setDebugInfoAvailability(true);
116  Asm = AP;
117}
118
119static void EmitLabelDiff(MCStreamer &Streamer,
120                          const MCSymbol *From, const MCSymbol *To) {
121  MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
122  MCContext &Context = Streamer.getContext();
123  const MCExpr *FromRef = MCSymbolRefExpr::Create(From, Variant, Context),
124               *ToRef   = MCSymbolRefExpr::Create(To, Variant, Context);
125  const MCExpr *AddrDelta =
126      MCBinaryExpr::Create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
127  Streamer.EmitValue(AddrDelta, 4);
128}
129
130void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) {
131  // For each function there is a separate subsection
132  // which holds the PC to file:line table.
133  const MCSymbol *Fn = Asm->getSymbol(GV);
134  assert(Fn);
135
136  const FunctionInfo &FI = FnDebugInfo[GV];
137  if (FI.Instrs.empty())
138    return;
139  assert(FI.End && "Don't know where the function ends?");
140
141  // PCs/Instructions are grouped into segments sharing the same filename.
142  // Pre-calculate the lengths (in instructions) of these segments and store
143  // them in a map for convenience.  Each index in the map is the sequential
144  // number of the respective instruction that starts a new segment.
145  DenseMap<size_t, size_t> FilenameSegmentLengths;
146  size_t LastSegmentEnd = 0;
147  StringRef PrevFilename = InstrInfo[FI.Instrs[0]].Filename;
148  for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) {
149    if (PrevFilename == InstrInfo[FI.Instrs[J]].Filename)
150      continue;
151    FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd;
152    LastSegmentEnd = J;
153    PrevFilename = InstrInfo[FI.Instrs[J]].Filename;
154  }
155  FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd;
156
157  // Emit the control code of the subsection followed by the payload size.
158  Asm->OutStreamer.AddComment(
159      "Linetable subsection for " + Twine(Fn->getName()));
160  Asm->EmitInt32(COFF::DEBUG_LINE_TABLE_SUBSECTION);
161  MCSymbol *SubsectionBegin = Asm->MMI->getContext().CreateTempSymbol(),
162           *SubsectionEnd = Asm->MMI->getContext().CreateTempSymbol();
163  EmitLabelDiff(Asm->OutStreamer, SubsectionBegin, SubsectionEnd);
164  Asm->OutStreamer.EmitLabel(SubsectionBegin);
165
166  // Identify the function this subsection is for.
167  Asm->OutStreamer.EmitCOFFSecRel32(Fn);
168  Asm->OutStreamer.EmitCOFFSectionIndex(Fn);
169
170  // Length of the function's code, in bytes.
171  EmitLabelDiff(Asm->OutStreamer, Fn, FI.End);
172
173  // PC-to-linenumber lookup table:
174  MCSymbol *FileSegmentEnd = nullptr;
175  for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) {
176    MCSymbol *Instr = FI.Instrs[J];
177    assert(InstrInfo.count(Instr));
178
179    if (FilenameSegmentLengths.count(J)) {
180      // We came to a beginning of a new filename segment.
181      if (FileSegmentEnd)
182        Asm->OutStreamer.EmitLabel(FileSegmentEnd);
183      StringRef CurFilename = InstrInfo[FI.Instrs[J]].Filename;
184      assert(FileNameRegistry.Infos.count(CurFilename));
185      size_t IndexInStringTable =
186          FileNameRegistry.Infos[CurFilename].FilenameID;
187      // Each segment starts with the offset of the filename
188      // in the string table.
189      Asm->OutStreamer.AddComment(
190          "Segment for file '" + Twine(CurFilename) + "' begins");
191      MCSymbol *FileSegmentBegin = Asm->MMI->getContext().CreateTempSymbol();
192      Asm->OutStreamer.EmitLabel(FileSegmentBegin);
193      Asm->EmitInt32(8 * IndexInStringTable);
194
195      // Number of PC records in the lookup table.
196      size_t SegmentLength = FilenameSegmentLengths[J];
197      Asm->EmitInt32(SegmentLength);
198
199      // Full size of the segment for this filename, including the prev two
200      // records.
201      FileSegmentEnd = Asm->MMI->getContext().CreateTempSymbol();
202      EmitLabelDiff(Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd);
203    }
204
205    // The first PC with the given linenumber and the linenumber itself.
206    EmitLabelDiff(Asm->OutStreamer, Fn, Instr);
207    Asm->EmitInt32(InstrInfo[Instr].LineNumber);
208  }
209
210  if (FileSegmentEnd)
211    Asm->OutStreamer.EmitLabel(FileSegmentEnd);
212  Asm->OutStreamer.EmitLabel(SubsectionEnd);
213}
214
215void WinCodeViewLineTables::endModule() {
216  if (FnDebugInfo.empty())
217    return;
218
219  assert(Asm != nullptr);
220  Asm->OutStreamer.SwitchSection(
221      Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
222  Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
223
224  // The COFF .debug$S section consists of several subsections, each starting
225  // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
226  // of the payload followed by the payload itself.  The subsections are 4-byte
227  // aligned.
228
229  for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I)
230    emitDebugInfoForFunction(VisitedFunctions[I]);
231
232  // This subsection holds a file index to offset in string table table.
233  Asm->OutStreamer.AddComment("File index to string table offset subsection");
234  Asm->EmitInt32(COFF::DEBUG_INDEX_SUBSECTION);
235  size_t NumFilenames = FileNameRegistry.Infos.size();
236  Asm->EmitInt32(8 * NumFilenames);
237  for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
238    StringRef Filename = FileNameRegistry.Filenames[I];
239    // For each unique filename, just write it's offset in the string table.
240    Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset);
241    // The function name offset is not followed by any additional data.
242    Asm->EmitInt32(0);
243  }
244
245  // This subsection holds the string table.
246  Asm->OutStreamer.AddComment("String table");
247  Asm->EmitInt32(COFF::DEBUG_STRING_TABLE_SUBSECTION);
248  Asm->EmitInt32(FileNameRegistry.LastOffset);
249  // The payload starts with a null character.
250  Asm->EmitInt8(0);
251
252  for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
253    // Just emit unique filenames one by one, separated by a null character.
254    Asm->OutStreamer.EmitBytes(FileNameRegistry.Filenames[I]);
255    Asm->EmitInt8(0);
256  }
257
258  // No more subsections. Fill with zeros to align the end of the section by 4.
259  Asm->OutStreamer.EmitFill((-FileNameRegistry.LastOffset) % 4, 0);
260
261  clear();
262}
263
264void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
265  assert(!CurFn && "Can't process two functions at once!");
266
267  if (!Asm || !Asm->MMI->hasDebugInfo())
268    return;
269
270  const Function *GV = MF->getFunction();
271  assert(FnDebugInfo.count(GV) == false);
272  VisitedFunctions.push_back(GV);
273  CurFn = &FnDebugInfo[GV];
274
275  // Find the end of the function prolog.
276  // FIXME: is there a simpler a way to do this? Can we just search
277  // for the first instruction of the function, not the last of the prolog?
278  DebugLoc PrologEndLoc;
279  bool EmptyPrologue = true;
280  for (const auto &MBB : *MF) {
281    if (!PrologEndLoc.isUnknown())
282      break;
283    for (const auto &MI : MBB) {
284      if (MI.isDebugValue())
285        continue;
286
287      // First known non-DBG_VALUE and non-frame setup location marks
288      // the beginning of the function body.
289      // FIXME: do we need the first subcondition?
290      if (!MI.getFlag(MachineInstr::FrameSetup) &&
291          (!MI.getDebugLoc().isUnknown())) {
292        PrologEndLoc = MI.getDebugLoc();
293        break;
294      }
295      EmptyPrologue = false;
296    }
297  }
298  // Record beginning of function if we have a non-empty prologue.
299  if (!PrologEndLoc.isUnknown() && !EmptyPrologue) {
300    DebugLoc FnStartDL =
301        PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
302    maybeRecordLocation(FnStartDL, MF);
303  }
304}
305
306void WinCodeViewLineTables::endFunction(const MachineFunction *MF) {
307  if (!Asm || !CurFn)  // We haven't created any debug info for this function.
308    return;
309
310  const Function *GV = MF->getFunction();
311  assert(FnDebugInfo.count(GV));
312  assert(CurFn == &FnDebugInfo[GV]);
313
314  if (CurFn->Instrs.empty()) {
315    FnDebugInfo.erase(GV);
316    VisitedFunctions.pop_back();
317  } else {
318    // Define end label for subprogram.
319    MCSymbol *FunctionEndSym = Asm->OutStreamer.getContext().CreateTempSymbol();
320    Asm->OutStreamer.EmitLabel(FunctionEndSym);
321    CurFn->End = FunctionEndSym;
322  }
323  CurFn = nullptr;
324}
325
326void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) {
327  // Ignore DBG_VALUE locations and function prologue.
328  if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
329    return;
330  DebugLoc DL = MI->getDebugLoc();
331  if (DL == PrevInstLoc || DL.isUnknown())
332    return;
333  maybeRecordLocation(DL, Asm->MF);
334}
335}
336