Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===// |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 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 | // |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 10 | // This file contains support for writing Microsoft CodeView debug info. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 14 | #include "CodeViewDebug.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
| 16 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCSymbol.h" |
| 19 | #include "llvm/Support/COFF.h" |
| 20 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 21 | using namespace llvm::codeview; |
| 22 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 25 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 26 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 27 | if (!Filepath.empty()) |
| 28 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 29 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 30 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 31 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 32 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 33 | // operates on full paths. We could change Clang to emit full paths too, but |
| 34 | // that would increase the IR size and probably not needed for other users. |
| 35 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 36 | if (Filename.find(':') == 1) |
| 37 | Filepath = Filename; |
| 38 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 39 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 40 | |
| 41 | // Canonicalize the path. We have to do it textually because we may no longer |
| 42 | // have access the file in the filesystem. |
| 43 | // First, replace all slashes with backslashes. |
| 44 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 45 | |
| 46 | // Remove all "\.\" with "\". |
| 47 | size_t Cursor = 0; |
| 48 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 49 | Filepath.erase(Cursor, 2); |
| 50 | |
| 51 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 52 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 53 | Cursor = 0; |
| 54 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 55 | // Something's wrong if the path starts with "\..\", abort. |
| 56 | if (Cursor == 0) |
| 57 | break; |
| 58 | |
| 59 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 60 | if (PrevSlash == std::string::npos) |
| 61 | // Something's wrong, abort. |
| 62 | break; |
| 63 | |
| 64 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 65 | // The next ".." might be following the one we've just erased. |
| 66 | Cursor = PrevSlash; |
| 67 | } |
| 68 | |
| 69 | // Remove all duplicate backslashes. |
| 70 | Cursor = 0; |
| 71 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 72 | Filepath.erase(Cursor, 1); |
| 73 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 74 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 77 | void CodeViewDebug::maybeRecordLocation(DebugLoc DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 78 | const MachineFunction *MF) { |
| 79 | // Skip this instruction if it has the same location as the previous one. |
| 80 | if (DL == CurFn->LastLoc) |
| 81 | return; |
| 82 | |
| 83 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 84 | if (!Scope) |
| 85 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 86 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 87 | // Skip this line if it is longer than the maximum we can record. |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 88 | if (DL.getLine() > COFF::CVL_MaxLineNumber) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 89 | return; |
| 90 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame] | 91 | CurFn->LastLoc = DL; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 92 | |
| 93 | MCSymbol *MCL = Asm->MMI->getContext().createTempSymbol(); |
| 94 | Asm->OutStreamer->EmitLabel(MCL); |
| 95 | CurFn->Instrs.push_back(MCL); |
| 96 | LabelsAndLocs[MCL] = DL; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 99 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 100 | : Asm(nullptr), CurFn(nullptr) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 101 | MachineModuleInfo *MMI = AP->MMI; |
| 102 | |
| 103 | // If module doesn't have named metadata anchors or COFF debug section |
| 104 | // is not available, skip any debug info related stuff. |
| 105 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 106 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) |
| 107 | return; |
| 108 | |
| 109 | // Tell MMI that we have debug info. |
| 110 | MMI->setDebugInfoAvailability(true); |
| 111 | Asm = AP; |
| 112 | } |
| 113 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 114 | void CodeViewDebug::endModule() { |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 115 | if (FnDebugInfo.empty()) |
| 116 | return; |
| 117 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 118 | // FIXME: For functions that are comdat, we should emit separate .debug$S |
| 119 | // sections that are comdat associative with the main function instead of |
| 120 | // having one big .debug$S section. |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 121 | assert(Asm != nullptr); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 122 | Asm->OutStreamer->SwitchSection( |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 123 | Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 124 | Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC); |
| 125 | |
| 126 | // The COFF .debug$S section consists of several subsections, each starting |
| 127 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 128 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 129 | // aligned. |
| 130 | |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 131 | // Emit per-function debug information. This code is extracted into a |
| 132 | // separate function for readability. |
| 133 | for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I) |
| 134 | emitDebugInfoForFunction(VisitedFunctions[I]); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 135 | |
| 136 | // This subsection holds a file index to offset in string table table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 137 | Asm->OutStreamer->AddComment("File index to string table offset subsection"); |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 138 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::FileChecksums)); |
| 139 | size_t NumFilenames = FileNameRegistry.Infos.size(); |
| 140 | Asm->EmitInt32(8 * NumFilenames); |
| 141 | for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) { |
| 142 | StringRef Filename = FileNameRegistry.Filenames[I]; |
| 143 | // For each unique filename, just write its offset in the string table. |
| 144 | Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset); |
| 145 | // The function name offset is not followed by any additional data. |
| 146 | Asm->EmitInt32(0); |
| 147 | } |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 148 | |
| 149 | // This subsection holds the string table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 150 | Asm->OutStreamer->AddComment("String table"); |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 151 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::StringTable)); |
| 152 | Asm->EmitInt32(FileNameRegistry.LastOffset); |
| 153 | // The payload starts with a null character. |
| 154 | Asm->EmitInt8(0); |
| 155 | |
| 156 | for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) { |
| 157 | // Just emit unique filenames one by one, separated by a null character. |
| 158 | Asm->OutStreamer->EmitBytes(FileNameRegistry.Filenames[I]); |
| 159 | Asm->EmitInt8(0); |
| 160 | } |
| 161 | |
| 162 | // No more subsections. Fill with zeros to align the end of the section by 4. |
| 163 | Asm->OutStreamer->EmitFill((-FileNameRegistry.LastOffset) % 4, 0); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 164 | |
| 165 | clear(); |
| 166 | } |
| 167 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 168 | static void EmitLabelDiff(MCStreamer &Streamer, |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 169 | const MCSymbol *From, const MCSymbol *To, |
| 170 | unsigned int Size = 4) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 171 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 172 | MCContext &Context = Streamer.getContext(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 173 | const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context), |
| 174 | *ToRef = MCSymbolRefExpr::create(To, Variant, Context); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 175 | const MCExpr *AddrDelta = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 176 | MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 177 | Streamer.EmitValue(AddrDelta, Size); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 180 | static const DIFile *getFileFromLoc(DebugLoc DL) { |
| 181 | return DL.get()->getScope()->getFile(); |
| 182 | } |
| 183 | |
| 184 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 185 | // For each function there is a separate subsection |
| 186 | // which holds the PC to file:line table. |
| 187 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 188 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 189 | |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 190 | const FunctionInfo &FI = FnDebugInfo[GV]; |
| 191 | if (FI.Instrs.empty()) |
| 192 | return; |
| 193 | assert(FI.End && "Don't know where the function ends?"); |
| 194 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 195 | StringRef FuncName; |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 196 | if (auto *SP = getDISubprogram(GV)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 197 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 198 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 199 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 200 | if (FuncName.empty()) |
| 201 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 202 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 203 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 204 | MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(), |
| 205 | *SymbolsEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 206 | Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName)); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 207 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols)); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 208 | EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd); |
| 209 | Asm->OutStreamer->EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 210 | { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 211 | MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(), |
| 212 | *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 213 | EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2); |
| 214 | Asm->OutStreamer->EmitLabel(ProcSegmentBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 215 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 216 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID)); |
| 217 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 218 | // Some bytes of this segment don't seem to be required for basic debugging, |
| 219 | // so just fill them with zeroes. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 220 | Asm->OutStreamer->EmitFill(12, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 221 | // This is the important bit that tells the debugger where the function |
| 222 | // code is located and what's its size: |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 223 | EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End); |
| 224 | Asm->OutStreamer->EmitFill(12, 0); |
| 225 | Asm->OutStreamer->EmitCOFFSecRel32(Fn); |
| 226 | Asm->OutStreamer->EmitCOFFSectionIndex(Fn); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 227 | Asm->EmitInt8(0); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 228 | // Emit the function display name as a null-terminated string. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 229 | Asm->OutStreamer->EmitBytes(FuncName); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 230 | Asm->EmitInt8(0); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 231 | Asm->OutStreamer->EmitLabel(ProcSegmentEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 232 | |
| 233 | // We're done with this function. |
| 234 | Asm->EmitInt16(0x0002); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 235 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END)); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 236 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 237 | Asm->OutStreamer->EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 238 | // Every subsection must be aligned to a 4-byte boundary. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 239 | Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 240 | |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 241 | // 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 | const DIFile *PrevFile = getFileFromLoc(LabelsAndLocs[FI.Instrs[0]]); |
| 248 | for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) { |
| 249 | const DIFile *CurFile = getFileFromLoc(LabelsAndLocs[FI.Instrs[J]]); |
| 250 | if (PrevFile == CurFile) |
| 251 | continue; |
| 252 | FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd; |
| 253 | LastSegmentEnd = J; |
| 254 | PrevFile = CurFile; |
| 255 | } |
| 256 | FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd; |
| 257 | |
| 258 | // Emit a line table subsection, required to do PC-to-file:line lookup. |
| 259 | Asm->OutStreamer->AddComment("Line table subsection for " + Twine(FuncName)); |
| 260 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::Lines)); |
| 261 | MCSymbol *LineTableBegin = Asm->MMI->getContext().createTempSymbol(), |
| 262 | *LineTableEnd = Asm->MMI->getContext().createTempSymbol(); |
| 263 | EmitLabelDiff(*Asm->OutStreamer, LineTableBegin, LineTableEnd); |
| 264 | Asm->OutStreamer->EmitLabel(LineTableBegin); |
| 265 | |
| 266 | // Identify the function this subsection is for. |
| 267 | Asm->OutStreamer->EmitCOFFSecRel32(Fn); |
| 268 | Asm->OutStreamer->EmitCOFFSectionIndex(Fn); |
| 269 | // Insert flags after a 16-bit section index. |
| 270 | Asm->EmitInt16(COFF::DEBUG_LINE_TABLES_HAVE_COLUMN_RECORDS); |
| 271 | |
| 272 | // Length of the function's code, in bytes. |
| 273 | EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End); |
| 274 | |
| 275 | // PC-to-linenumber lookup table: |
| 276 | MCSymbol *FileSegmentEnd = nullptr; |
| 277 | |
| 278 | // The start of the last segment: |
| 279 | size_t LastSegmentStart = 0; |
| 280 | |
| 281 | auto FinishPreviousChunk = [&] { |
| 282 | if (!FileSegmentEnd) |
| 283 | return; |
| 284 | for (size_t ColSegI = LastSegmentStart, |
| 285 | ColSegEnd = ColSegI + FilenameSegmentLengths[LastSegmentStart]; |
| 286 | ColSegI != ColSegEnd; ++ColSegI) { |
| 287 | unsigned ColumnNumber = LabelsAndLocs[FI.Instrs[ColSegI]].getCol(); |
| 288 | // Truncate the column number if it is longer than the maximum we can |
| 289 | // record. |
| 290 | if (ColumnNumber > COFF::CVL_MaxColumnNumber) |
| 291 | ColumnNumber = 0; |
| 292 | Asm->EmitInt16(ColumnNumber); // Start column |
| 293 | Asm->EmitInt16(0); // End column |
| 294 | } |
| 295 | Asm->OutStreamer->EmitLabel(FileSegmentEnd); |
| 296 | }; |
| 297 | |
| 298 | for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) { |
| 299 | MCSymbol *Instr = FI.Instrs[J]; |
| 300 | assert(LabelsAndLocs.count(Instr)); |
| 301 | |
| 302 | if (FilenameSegmentLengths.count(J)) { |
| 303 | // We came to a beginning of a new filename segment. |
| 304 | FinishPreviousChunk(); |
| 305 | const DIFile *File = getFileFromLoc(LabelsAndLocs[FI.Instrs[J]]); |
| 306 | StringRef CurFilename = getFullFilepath(File); |
| 307 | size_t IndexInFileTable = FileNameRegistry.add(CurFilename); |
| 308 | // Each segment starts with the offset of the filename |
| 309 | // in the string table. |
| 310 | Asm->OutStreamer->AddComment( |
| 311 | "Segment for file '" + Twine(CurFilename) + "' begins"); |
| 312 | MCSymbol *FileSegmentBegin = Asm->MMI->getContext().createTempSymbol(); |
| 313 | Asm->OutStreamer->EmitLabel(FileSegmentBegin); |
| 314 | Asm->EmitInt32(8 * IndexInFileTable); |
| 315 | |
| 316 | // Number of PC records in the lookup table. |
| 317 | size_t SegmentLength = FilenameSegmentLengths[J]; |
| 318 | Asm->EmitInt32(SegmentLength); |
| 319 | |
| 320 | // Full size of the segment for this filename, including the prev two |
| 321 | // records. |
| 322 | FileSegmentEnd = Asm->MMI->getContext().createTempSymbol(); |
| 323 | EmitLabelDiff(*Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd); |
| 324 | LastSegmentStart = J; |
| 325 | } |
| 326 | |
| 327 | // The first PC with the given linenumber and the linenumber itself. |
| 328 | EmitLabelDiff(*Asm->OutStreamer, Fn, Instr); |
| 329 | uint32_t LineNumber = LabelsAndLocs[Instr].getLine(); |
| 330 | assert(LineNumber <= COFF::CVL_MaxLineNumber); |
| 331 | uint32_t LineData = LineNumber | COFF::CVL_IsStatement; |
| 332 | Asm->EmitInt32(LineData); |
| 333 | } |
| 334 | |
| 335 | FinishPreviousChunk(); |
| 336 | Asm->OutStreamer->EmitLabel(LineTableEnd); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 339 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 340 | assert(!CurFn && "Can't process two functions at once!"); |
| 341 | |
| 342 | if (!Asm || !Asm->MMI->hasDebugInfo()) |
| 343 | return; |
| 344 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 345 | const Function *GV = MF->getFunction(); |
| 346 | assert(FnDebugInfo.count(GV) == false); |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 347 | VisitedFunctions.push_back(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 348 | CurFn = &FnDebugInfo[GV]; |
| 349 | |
| 350 | // Find the end of the function prolog. |
| 351 | // FIXME: is there a simpler a way to do this? Can we just search |
| 352 | // for the first instruction of the function, not the last of the prolog? |
| 353 | DebugLoc PrologEndLoc; |
| 354 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 355 | for (const auto &MBB : *MF) { |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 356 | if (PrologEndLoc) |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 357 | break; |
| 358 | for (const auto &MI : MBB) { |
| 359 | if (MI.isDebugValue()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 360 | continue; |
| 361 | |
| 362 | // First known non-DBG_VALUE and non-frame setup location marks |
| 363 | // the beginning of the function body. |
| 364 | // FIXME: do we need the first subcondition? |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 365 | if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 366 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | EmptyPrologue = false; |
| 370 | } |
| 371 | } |
| 372 | // Record beginning of function if we have a non-empty prologue. |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 373 | if (PrologEndLoc && !EmptyPrologue) { |
| 374 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 375 | maybeRecordLocation(FnStartDL, MF); |
| 376 | } |
| 377 | } |
| 378 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 379 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 380 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 381 | return; |
| 382 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 383 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 384 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 385 | assert(CurFn == &FnDebugInfo[GV]); |
| 386 | |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 387 | if (CurFn->Instrs.empty()) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 388 | FnDebugInfo.erase(GV); |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame^] | 389 | VisitedFunctions.pop_back(); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 390 | } else { |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 391 | CurFn->End = Asm->getFunctionEnd(); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 392 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 393 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 396 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 397 | // Ignore DBG_VALUE locations and function prologue. |
| 398 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 399 | return; |
| 400 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 401 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 402 | return; |
| 403 | maybeRecordLocation(DL, Asm->MF); |
| 404 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 405 | } |