LLVM API Documentation
00001 //===-- llvm/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp --*- C++ -*--===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains support for writing line tables info into COFF files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "WinCodeViewLineTables.h" 00015 #include "llvm/MC/MCExpr.h" 00016 #include "llvm/MC/MCSymbol.h" 00017 #include "llvm/Support/COFF.h" 00018 00019 namespace llvm { 00020 00021 StringRef WinCodeViewLineTables::getFullFilepath(const MDNode *S) { 00022 assert(S); 00023 DIDescriptor D(S); 00024 assert((D.isCompileUnit() || D.isFile() || D.isSubprogram() || 00025 D.isLexicalBlockFile() || D.isLexicalBlock()) && 00026 "Unexpected scope info"); 00027 00028 DIScope Scope(S); 00029 StringRef Dir = Scope.getDirectory(), 00030 Filename = Scope.getFilename(); 00031 char *&Result = DirAndFilenameToFilepathMap[std::make_pair(Dir, Filename)]; 00032 if (Result) 00033 return Result; 00034 00035 // Clang emits directory and relative filename info into the IR, but CodeView 00036 // operates on full paths. We could change Clang to emit full paths too, but 00037 // that would increase the IR size and probably not needed for other users. 00038 // For now, just concatenate and canonicalize the path here. 00039 std::string Filepath; 00040 if (Filename.find(':') == 1) 00041 Filepath = Filename; 00042 else 00043 Filepath = (Dir + Twine("\\") + Filename).str(); 00044 00045 // Canonicalize the path. We have to do it textually because we may no longer 00046 // have access the file in the filesystem. 00047 // First, replace all slashes with backslashes. 00048 std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); 00049 00050 // Remove all "\.\" with "\". 00051 size_t Cursor = 0; 00052 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) 00053 Filepath.erase(Cursor, 2); 00054 00055 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original 00056 // path should be well-formatted, e.g. start with a drive letter, etc. 00057 Cursor = 0; 00058 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { 00059 // Something's wrong if the path starts with "\..\", abort. 00060 if (Cursor == 0) 00061 break; 00062 00063 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); 00064 if (PrevSlash == std::string::npos) 00065 // Something's wrong, abort. 00066 break; 00067 00068 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); 00069 // The next ".." might be following the one we've just erased. 00070 Cursor = PrevSlash; 00071 } 00072 00073 // Remove all duplicate backslashes. 00074 Cursor = 0; 00075 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) 00076 Filepath.erase(Cursor, 1); 00077 00078 Result = strdup(Filepath.c_str()); 00079 return StringRef(Result); 00080 } 00081 00082 void WinCodeViewLineTables::maybeRecordLocation(DebugLoc DL, 00083 const MachineFunction *MF) { 00084 const MDNode *Scope = DL.getScope(MF->getFunction()->getContext()); 00085 if (!Scope) 00086 return; 00087 StringRef Filename = getFullFilepath(Scope); 00088 00089 // Skip this instruction if it has the same file:line as the previous one. 00090 assert(CurFn); 00091 if (!CurFn->Instrs.empty()) { 00092 const InstrInfoTy &LastInstr = InstrInfo[CurFn->Instrs.back()]; 00093 if (LastInstr.Filename == Filename && LastInstr.LineNumber == DL.getLine()) 00094 return; 00095 } 00096 FileNameRegistry.add(Filename); 00097 00098 MCSymbol *MCL = Asm->MMI->getContext().CreateTempSymbol(); 00099 Asm->OutStreamer.EmitLabel(MCL); 00100 CurFn->Instrs.push_back(MCL); 00101 InstrInfo[MCL] = InstrInfoTy(Filename, DL.getLine()); 00102 } 00103 00104 WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *AP) 00105 : Asm(nullptr), CurFn(nullptr) { 00106 MachineModuleInfo *MMI = AP->MMI; 00107 00108 // If module doesn't have named metadata anchors or COFF debug section 00109 // is not available, skip any debug info related stuff. 00110 if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || 00111 !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) 00112 return; 00113 00114 // Tell MMI that we have debug info. 00115 MMI->setDebugInfoAvailability(true); 00116 Asm = AP; 00117 } 00118 00119 static void EmitLabelDiff(MCStreamer &Streamer, 00120 const MCSymbol *From, const MCSymbol *To) { 00121 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 00122 MCContext &Context = Streamer.getContext(); 00123 const MCExpr *FromRef = MCSymbolRefExpr::Create(From, Variant, Context), 00124 *ToRef = MCSymbolRefExpr::Create(To, Variant, Context); 00125 const MCExpr *AddrDelta = 00126 MCBinaryExpr::Create(MCBinaryExpr::Sub, ToRef, FromRef, Context); 00127 Streamer.EmitValue(AddrDelta, 4); 00128 } 00129 00130 void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) { 00131 // For each function there is a separate subsection 00132 // which holds the PC to file:line table. 00133 const MCSymbol *Fn = Asm->getSymbol(GV); 00134 assert(Fn); 00135 00136 const FunctionInfo &FI = FnDebugInfo[GV]; 00137 if (FI.Instrs.empty()) 00138 return; 00139 assert(FI.End && "Don't know where the function ends?"); 00140 00141 // PCs/Instructions are grouped into segments sharing the same filename. 00142 // Pre-calculate the lengths (in instructions) of these segments and store 00143 // them in a map for convenience. Each index in the map is the sequential 00144 // number of the respective instruction that starts a new segment. 00145 DenseMap<size_t, size_t> FilenameSegmentLengths; 00146 size_t LastSegmentEnd = 0; 00147 StringRef PrevFilename = InstrInfo[FI.Instrs[0]].Filename; 00148 for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) { 00149 if (PrevFilename == InstrInfo[FI.Instrs[J]].Filename) 00150 continue; 00151 FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd; 00152 LastSegmentEnd = J; 00153 PrevFilename = InstrInfo[FI.Instrs[J]].Filename; 00154 } 00155 FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd; 00156 00157 // Emit the control code of the subsection followed by the payload size. 00158 Asm->OutStreamer.AddComment( 00159 "Linetable subsection for " + Twine(Fn->getName())); 00160 Asm->EmitInt32(COFF::DEBUG_LINE_TABLE_SUBSECTION); 00161 MCSymbol *SubsectionBegin = Asm->MMI->getContext().CreateTempSymbol(), 00162 *SubsectionEnd = Asm->MMI->getContext().CreateTempSymbol(); 00163 EmitLabelDiff(Asm->OutStreamer, SubsectionBegin, SubsectionEnd); 00164 Asm->OutStreamer.EmitLabel(SubsectionBegin); 00165 00166 // Identify the function this subsection is for. 00167 Asm->OutStreamer.EmitCOFFSecRel32(Fn); 00168 Asm->OutStreamer.EmitCOFFSectionIndex(Fn); 00169 00170 // Length of the function's code, in bytes. 00171 EmitLabelDiff(Asm->OutStreamer, Fn, FI.End); 00172 00173 // PC-to-linenumber lookup table: 00174 MCSymbol *FileSegmentEnd = nullptr; 00175 for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) { 00176 MCSymbol *Instr = FI.Instrs[J]; 00177 assert(InstrInfo.count(Instr)); 00178 00179 if (FilenameSegmentLengths.count(J)) { 00180 // We came to a beginning of a new filename segment. 00181 if (FileSegmentEnd) 00182 Asm->OutStreamer.EmitLabel(FileSegmentEnd); 00183 StringRef CurFilename = InstrInfo[FI.Instrs[J]].Filename; 00184 assert(FileNameRegistry.Infos.count(CurFilename)); 00185 size_t IndexInStringTable = 00186 FileNameRegistry.Infos[CurFilename].FilenameID; 00187 // Each segment starts with the offset of the filename 00188 // in the string table. 00189 Asm->OutStreamer.AddComment( 00190 "Segment for file '" + Twine(CurFilename) + "' begins"); 00191 MCSymbol *FileSegmentBegin = Asm->MMI->getContext().CreateTempSymbol(); 00192 Asm->OutStreamer.EmitLabel(FileSegmentBegin); 00193 Asm->EmitInt32(8 * IndexInStringTable); 00194 00195 // Number of PC records in the lookup table. 00196 size_t SegmentLength = FilenameSegmentLengths[J]; 00197 Asm->EmitInt32(SegmentLength); 00198 00199 // Full size of the segment for this filename, including the prev two 00200 // records. 00201 FileSegmentEnd = Asm->MMI->getContext().CreateTempSymbol(); 00202 EmitLabelDiff(Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd); 00203 } 00204 00205 // The first PC with the given linenumber and the linenumber itself. 00206 EmitLabelDiff(Asm->OutStreamer, Fn, Instr); 00207 Asm->EmitInt32(InstrInfo[Instr].LineNumber); 00208 } 00209 00210 if (FileSegmentEnd) 00211 Asm->OutStreamer.EmitLabel(FileSegmentEnd); 00212 Asm->OutStreamer.EmitLabel(SubsectionEnd); 00213 } 00214 00215 void WinCodeViewLineTables::endModule() { 00216 if (FnDebugInfo.empty()) 00217 return; 00218 00219 assert(Asm != nullptr); 00220 Asm->OutStreamer.SwitchSection( 00221 Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); 00222 Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC); 00223 00224 // The COFF .debug$S section consists of several subsections, each starting 00225 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length 00226 // of the payload followed by the payload itself. The subsections are 4-byte 00227 // aligned. 00228 00229 for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I) 00230 emitDebugInfoForFunction(VisitedFunctions[I]); 00231 00232 // This subsection holds a file index to offset in string table table. 00233 Asm->OutStreamer.AddComment("File index to string table offset subsection"); 00234 Asm->EmitInt32(COFF::DEBUG_INDEX_SUBSECTION); 00235 size_t NumFilenames = FileNameRegistry.Infos.size(); 00236 Asm->EmitInt32(8 * NumFilenames); 00237 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) { 00238 StringRef Filename = FileNameRegistry.Filenames[I]; 00239 // For each unique filename, just write it's offset in the string table. 00240 Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset); 00241 // The function name offset is not followed by any additional data. 00242 Asm->EmitInt32(0); 00243 } 00244 00245 // This subsection holds the string table. 00246 Asm->OutStreamer.AddComment("String table"); 00247 Asm->EmitInt32(COFF::DEBUG_STRING_TABLE_SUBSECTION); 00248 Asm->EmitInt32(FileNameRegistry.LastOffset); 00249 // The payload starts with a null character. 00250 Asm->EmitInt8(0); 00251 00252 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) { 00253 // Just emit unique filenames one by one, separated by a null character. 00254 Asm->OutStreamer.EmitBytes(FileNameRegistry.Filenames[I]); 00255 Asm->EmitInt8(0); 00256 } 00257 00258 // No more subsections. Fill with zeros to align the end of the section by 4. 00259 Asm->OutStreamer.EmitFill((-FileNameRegistry.LastOffset) % 4, 0); 00260 00261 clear(); 00262 } 00263 00264 void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) { 00265 assert(!CurFn && "Can't process two functions at once!"); 00266 00267 if (!Asm || !Asm->MMI->hasDebugInfo()) 00268 return; 00269 00270 const Function *GV = MF->getFunction(); 00271 assert(FnDebugInfo.count(GV) == false); 00272 VisitedFunctions.push_back(GV); 00273 CurFn = &FnDebugInfo[GV]; 00274 00275 // Find the end of the function prolog. 00276 // FIXME: is there a simpler a way to do this? Can we just search 00277 // for the first instruction of the function, not the last of the prolog? 00278 DebugLoc PrologEndLoc; 00279 bool EmptyPrologue = true; 00280 for (const auto &MBB : *MF) { 00281 if (!PrologEndLoc.isUnknown()) 00282 break; 00283 for (const auto &MI : MBB) { 00284 if (MI.isDebugValue()) 00285 continue; 00286 00287 // First known non-DBG_VALUE and non-frame setup location marks 00288 // the beginning of the function body. 00289 // FIXME: do we need the first subcondition? 00290 if (!MI.getFlag(MachineInstr::FrameSetup) && 00291 (!MI.getDebugLoc().isUnknown())) { 00292 PrologEndLoc = MI.getDebugLoc(); 00293 break; 00294 } 00295 EmptyPrologue = false; 00296 } 00297 } 00298 // Record beginning of function if we have a non-empty prologue. 00299 if (!PrologEndLoc.isUnknown() && !EmptyPrologue) { 00300 DebugLoc FnStartDL = 00301 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext()); 00302 maybeRecordLocation(FnStartDL, MF); 00303 } 00304 } 00305 00306 void WinCodeViewLineTables::endFunction(const MachineFunction *MF) { 00307 if (!Asm || !CurFn) // We haven't created any debug info for this function. 00308 return; 00309 00310 const Function *GV = MF->getFunction(); 00311 assert(FnDebugInfo.count(GV)); 00312 assert(CurFn == &FnDebugInfo[GV]); 00313 00314 if (CurFn->Instrs.empty()) { 00315 FnDebugInfo.erase(GV); 00316 VisitedFunctions.pop_back(); 00317 } else { 00318 // Define end label for subprogram. 00319 MCSymbol *FunctionEndSym = Asm->OutStreamer.getContext().CreateTempSymbol(); 00320 Asm->OutStreamer.EmitLabel(FunctionEndSym); 00321 CurFn->End = FunctionEndSym; 00322 } 00323 CurFn = nullptr; 00324 } 00325 00326 void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) { 00327 // Ignore DBG_VALUE locations and function prologue. 00328 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) 00329 return; 00330 DebugLoc DL = MI->getDebugLoc(); 00331 if (DL == PrevInstLoc || DL.isUnknown()) 00332 return; 00333 maybeRecordLocation(DL, Asm->MF); 00334 } 00335 }