WinCodeViewLineTables.cpp 12 KB

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