blob: 39a76d629ad8106c243e68b7569627f0244e72fe [file] [log] [blame]
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00001//===-- 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)];
Craig Topper353eda42014-04-24 06:44:33 +000032 if (Result)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000033 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
Yaron Keren75e0c4b2015-03-27 17:51:30 +000043 Filepath = (Dir + "\\" + Filename).str();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000044
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) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +000084 const MDNode *Scope = DL.getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000085 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)
Craig Topper353eda42014-04-24 06:44:33 +0000105 : Asm(nullptr), CurFn(nullptr) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000106 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
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000119void WinCodeViewLineTables::endModule() {
120 if (FnDebugInfo.empty())
121 return;
122
123 assert(Asm != nullptr);
124 Asm->OutStreamer.SwitchSection(
125 Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
126 Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
127
128 // The COFF .debug$S section consists of several subsections, each starting
129 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
130 // of the payload followed by the payload itself. The subsections are 4-byte
131 // aligned.
132
133 // Emit per-function debug information. This code is extracted into a
134 // separate function for readability.
135 for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I)
136 emitDebugInfoForFunction(VisitedFunctions[I]);
137
138 // This subsection holds a file index to offset in string table table.
139 Asm->OutStreamer.AddComment("File index to string table offset subsection");
140 Asm->EmitInt32(COFF::DEBUG_INDEX_SUBSECTION);
141 size_t NumFilenames = FileNameRegistry.Infos.size();
142 Asm->EmitInt32(8 * NumFilenames);
143 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
144 StringRef Filename = FileNameRegistry.Filenames[I];
145 // For each unique filename, just write its offset in the string table.
146 Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset);
147 // The function name offset is not followed by any additional data.
148 Asm->EmitInt32(0);
149 }
150
151 // This subsection holds the string table.
152 Asm->OutStreamer.AddComment("String table");
153 Asm->EmitInt32(COFF::DEBUG_STRING_TABLE_SUBSECTION);
154 Asm->EmitInt32(FileNameRegistry.LastOffset);
155 // The payload starts with a null character.
156 Asm->EmitInt8(0);
157
158 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
159 // Just emit unique filenames one by one, separated by a null character.
160 Asm->OutStreamer.EmitBytes(FileNameRegistry.Filenames[I]);
161 Asm->EmitInt8(0);
162 }
163
164 // No more subsections. Fill with zeros to align the end of the section by 4.
165 Asm->OutStreamer.EmitFill((-FileNameRegistry.LastOffset) % 4, 0);
166
167 clear();
168}
169
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000170static void EmitLabelDiff(MCStreamer &Streamer,
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000171 const MCSymbol *From, const MCSymbol *To,
172 unsigned int Size = 4) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000173 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
174 MCContext &Context = Streamer.getContext();
175 const MCExpr *FromRef = MCSymbolRefExpr::Create(From, Variant, Context),
176 *ToRef = MCSymbolRefExpr::Create(To, Variant, Context);
177 const MCExpr *AddrDelta =
178 MCBinaryExpr::Create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000179 Streamer.EmitValue(AddrDelta, Size);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000180}
181
182void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) {
183 // For each function there is a separate subsection
184 // which holds the PC to file:line table.
185 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000186 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000187
188 const FunctionInfo &FI = FnDebugInfo[GV];
189 if (FI.Instrs.empty())
190 return;
191 assert(FI.End && "Don't know where the function ends?");
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000192
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000193 StringRef GVName = GV->getName();
194 StringRef FuncName;
195 if (DISubprogram SP = getDISubprogram(GV))
196 FuncName = SP.getDisplayName();
197
Timur Iskhodzhanov0e76a162014-11-12 20:21:20 +0000198 // FIXME Clang currently sets DisplayName to "bar" for a C++
199 // "namespace_foo::bar" function, see PR21528. Luckily, dbghelp.dll is trying
200 // to demangle display names anyways, so let's just put a mangled name into
201 // the symbols subsection until Clang gives us what we need.
202 if (GVName.startswith("\01?"))
203 FuncName = GVName.substr(1);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000204 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
205 MCSymbol *SymbolsBegin = Asm->MMI->getContext().CreateTempSymbol(),
206 *SymbolsEnd = Asm->MMI->getContext().CreateTempSymbol();
207 Asm->OutStreamer.AddComment("Symbol subsection for " + Twine(FuncName));
208 Asm->EmitInt32(COFF::DEBUG_SYMBOL_SUBSECTION);
209 EmitLabelDiff(Asm->OutStreamer, SymbolsBegin, SymbolsEnd);
210 Asm->OutStreamer.EmitLabel(SymbolsBegin);
211 {
212 MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().CreateTempSymbol(),
213 *ProcSegmentEnd = Asm->MMI->getContext().CreateTempSymbol();
214 EmitLabelDiff(Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2);
215 Asm->OutStreamer.EmitLabel(ProcSegmentBegin);
216
217 Asm->EmitInt16(COFF::DEBUG_SYMBOL_TYPE_PROC_START);
218 // Some bytes of this segment don't seem to be required for basic debugging,
219 // so just fill them with zeroes.
220 Asm->OutStreamer.EmitFill(12, 0);
221 // This is the important bit that tells the debugger where the function
222 // code is located and what's its size:
223 EmitLabelDiff(Asm->OutStreamer, Fn, FI.End);
224 Asm->OutStreamer.EmitFill(12, 0);
225 Asm->OutStreamer.EmitCOFFSecRel32(Fn);
226 Asm->OutStreamer.EmitCOFFSectionIndex(Fn);
227 Asm->EmitInt8(0);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000228 // Emit the function display name as a null-terminated string.
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000229 Asm->OutStreamer.EmitBytes(FuncName);
230 Asm->EmitInt8(0);
231 Asm->OutStreamer.EmitLabel(ProcSegmentEnd);
232
233 // We're done with this function.
234 Asm->EmitInt16(0x0002);
235 Asm->EmitInt16(COFF::DEBUG_SYMBOL_TYPE_PROC_END);
236 }
237 Asm->OutStreamer.EmitLabel(SymbolsEnd);
238 // Every subsection must be aligned to a 4-byte boundary.
239 Asm->OutStreamer.EmitFill((-FuncName.size()) % 4, 0);
240
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000241 // PCs/Instructions are grouped into segments sharing the same filename.
242 // Pre-calculate the lengths (in instructions) of these segments and store
243 // them in a map for convenience. Each index in the map is the sequential
244 // number of the respective instruction that starts a new segment.
245 DenseMap<size_t, size_t> FilenameSegmentLengths;
246 size_t LastSegmentEnd = 0;
247 StringRef PrevFilename = InstrInfo[FI.Instrs[0]].Filename;
248 for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) {
249 if (PrevFilename == InstrInfo[FI.Instrs[J]].Filename)
250 continue;
251 FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd;
252 LastSegmentEnd = J;
253 PrevFilename = InstrInfo[FI.Instrs[J]].Filename;
254 }
255 FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd;
256
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000257 // Emit a line table subsection, requred to do PC-to-file:line lookup.
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000258 Asm->OutStreamer.AddComment("Line table subsection for " + Twine(FuncName));
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000259 Asm->EmitInt32(COFF::DEBUG_LINE_TABLE_SUBSECTION);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000260 MCSymbol *LineTableBegin = Asm->MMI->getContext().CreateTempSymbol(),
261 *LineTableEnd = Asm->MMI->getContext().CreateTempSymbol();
262 EmitLabelDiff(Asm->OutStreamer, LineTableBegin, LineTableEnd);
263 Asm->OutStreamer.EmitLabel(LineTableBegin);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000264
265 // Identify the function this subsection is for.
266 Asm->OutStreamer.EmitCOFFSecRel32(Fn);
267 Asm->OutStreamer.EmitCOFFSectionIndex(Fn);
Timur Iskhodzhanov5fcaeeb2014-10-08 18:01:49 +0000268 // Insert padding after a 16-bit section index.
269 Asm->EmitInt16(0);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000270
271 // Length of the function's code, in bytes.
272 EmitLabelDiff(Asm->OutStreamer, Fn, FI.End);
273
274 // PC-to-linenumber lookup table:
Craig Topper353eda42014-04-24 06:44:33 +0000275 MCSymbol *FileSegmentEnd = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000276 for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) {
277 MCSymbol *Instr = FI.Instrs[J];
278 assert(InstrInfo.count(Instr));
279
280 if (FilenameSegmentLengths.count(J)) {
281 // We came to a beginning of a new filename segment.
282 if (FileSegmentEnd)
283 Asm->OutStreamer.EmitLabel(FileSegmentEnd);
284 StringRef CurFilename = InstrInfo[FI.Instrs[J]].Filename;
285 assert(FileNameRegistry.Infos.count(CurFilename));
286 size_t IndexInStringTable =
287 FileNameRegistry.Infos[CurFilename].FilenameID;
288 // Each segment starts with the offset of the filename
289 // in the string table.
290 Asm->OutStreamer.AddComment(
291 "Segment for file '" + Twine(CurFilename) + "' begins");
292 MCSymbol *FileSegmentBegin = Asm->MMI->getContext().CreateTempSymbol();
293 Asm->OutStreamer.EmitLabel(FileSegmentBegin);
294 Asm->EmitInt32(8 * IndexInStringTable);
295
296 // Number of PC records in the lookup table.
297 size_t SegmentLength = FilenameSegmentLengths[J];
298 Asm->EmitInt32(SegmentLength);
299
300 // Full size of the segment for this filename, including the prev two
301 // records.
302 FileSegmentEnd = Asm->MMI->getContext().CreateTempSymbol();
303 EmitLabelDiff(Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd);
304 }
305
306 // The first PC with the given linenumber and the linenumber itself.
307 EmitLabelDiff(Asm->OutStreamer, Fn, Instr);
308 Asm->EmitInt32(InstrInfo[Instr].LineNumber);
309 }
310
311 if (FileSegmentEnd)
312 Asm->OutStreamer.EmitLabel(FileSegmentEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000313 Asm->OutStreamer.EmitLabel(LineTableEnd);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000314}
315
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000316void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
317 assert(!CurFn && "Can't process two functions at once!");
318
319 if (!Asm || !Asm->MMI->hasDebugInfo())
320 return;
321
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000322 const Function *GV = MF->getFunction();
323 assert(FnDebugInfo.count(GV) == false);
324 VisitedFunctions.push_back(GV);
325 CurFn = &FnDebugInfo[GV];
326
327 // Find the end of the function prolog.
328 // FIXME: is there a simpler a way to do this? Can we just search
329 // for the first instruction of the function, not the last of the prolog?
330 DebugLoc PrologEndLoc;
331 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000332 for (const auto &MBB : *MF) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000333 if (PrologEndLoc)
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000334 break;
335 for (const auto &MI : MBB) {
336 if (MI.isDebugValue())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000337 continue;
338
339 // First known non-DBG_VALUE and non-frame setup location marks
340 // the beginning of the function body.
341 // FIXME: do we need the first subcondition?
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000342 if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000343 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000344 break;
345 }
346 EmptyPrologue = false;
347 }
348 }
349 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000350 if (PrologEndLoc && !EmptyPrologue) {
351 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000352 maybeRecordLocation(FnStartDL, MF);
353 }
354}
355
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000356void WinCodeViewLineTables::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000357 if (!Asm || !CurFn) // We haven't created any debug info for this function.
358 return;
359
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000360 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000361 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000362 assert(CurFn == &FnDebugInfo[GV]);
363
364 if (CurFn->Instrs.empty()) {
365 FnDebugInfo.erase(GV);
366 VisitedFunctions.pop_back();
367 } else {
Rafael Espindola07c03d32015-03-05 02:05:42 +0000368 CurFn->End = Asm->getFunctionEnd();
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000369 }
Craig Topper353eda42014-04-24 06:44:33 +0000370 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000371}
372
373void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) {
374 // Ignore DBG_VALUE locations and function prologue.
375 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
376 return;
377 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000378 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000379 return;
380 maybeRecordLocation(DL, Asm->MF);
381}
382}