Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 1 | //===- MCCodeView.h - Machine Code CodeView support -------------*- 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 | // Holds state from .cv_file and .cv_loc directives for later emission. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/MC/MCCodeView.h" |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmLayout.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
| 18 | #include "llvm/DebugInfo/CodeView/Line.h" |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCObjectStreamer.h" |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCValue.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 23 | #include "llvm/Support/COFF.h" |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 24 | #include "llvm/Support/EndianStream.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace llvm::codeview; |
| 28 | |
| 29 | CodeViewContext::CodeViewContext() {} |
| 30 | |
| 31 | CodeViewContext::~CodeViewContext() { |
| 32 | // If someone inserted strings into the string table but never actually |
| 33 | // emitted them somewhere, clean up the fragment. |
| 34 | if (!InsertedStrTabFragment) |
| 35 | delete StrTabFragment; |
| 36 | } |
| 37 | |
| 38 | /// This is a valid number for use with .cv_loc if we've already seen a .cv_file |
| 39 | /// for it. |
| 40 | bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const { |
| 41 | unsigned Idx = FileNumber - 1; |
| 42 | if (Idx < Filenames.size()) |
| 43 | return !Filenames[Idx].empty(); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool CodeViewContext::addFile(unsigned FileNumber, StringRef Filename) { |
| 48 | assert(FileNumber > 0); |
| 49 | Filename = addToStringTable(Filename); |
| 50 | unsigned Idx = FileNumber - 1; |
| 51 | if (Idx >= Filenames.size()) |
| 52 | Filenames.resize(Idx + 1); |
| 53 | |
| 54 | if (Filename.empty()) |
| 55 | Filename = "<stdin>"; |
| 56 | |
| 57 | if (!Filenames[Idx].empty()) |
| 58 | return false; |
| 59 | |
| 60 | // FIXME: We should store the string table offset of the filename, rather than |
| 61 | // the filename itself for efficiency. |
| 62 | Filename = addToStringTable(Filename); |
| 63 | |
| 64 | Filenames[Idx] = Filename; |
| 65 | return true; |
| 66 | } |
| 67 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 68 | bool CodeViewContext::recordFunctionId(unsigned FuncId) { |
| 69 | if (FuncId >= Functions.size()) |
| 70 | Functions.resize(FuncId + 1); |
| 71 | |
| 72 | // Return false if this function info was already allocated. |
| 73 | if (!Functions[FuncId].isUnallocatedFunctionInfo()) |
| 74 | return false; |
| 75 | |
| 76 | // Mark this as an allocated normal function, and leave the rest alone. |
| 77 | Functions[FuncId].ParentFuncIdPlusOne = MCCVFunctionInfo::FunctionSentinel; |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool CodeViewContext::recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc, |
| 82 | unsigned IAFile, unsigned IALine, |
| 83 | unsigned IACol) { |
| 84 | if (FuncId >= Functions.size()) |
| 85 | Functions.resize(FuncId + 1); |
| 86 | |
| 87 | // Return false if this function info was already allocated. |
| 88 | if (!Functions[FuncId].isUnallocatedFunctionInfo()) |
| 89 | return false; |
| 90 | |
| 91 | MCCVFunctionInfo::LineInfo InlinedAt; |
| 92 | InlinedAt.File = IAFile; |
| 93 | InlinedAt.Line = IALine; |
| 94 | InlinedAt.Col = IACol; |
| 95 | |
| 96 | // Mark this as an inlined call site and record call site line info. |
| 97 | MCCVFunctionInfo *Info = &Functions[FuncId]; |
| 98 | Info->ParentFuncIdPlusOne = IAFunc + 1; |
| 99 | Info->InlinedAt = InlinedAt; |
| 100 | |
| 101 | // Walk up the call chain adding this function id to the InlinedAtMap of all |
| 102 | // transitive callers until we hit a real function. |
| 103 | while (Info->isInlinedCallSite()) { |
| 104 | InlinedAt = Info->InlinedAt; |
| 105 | Info = getCVFunctionInfo(Info->getParentFuncId()); |
| 106 | Info->InlinedAtMap[FuncId] = InlinedAt; |
| 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 112 | MCDataFragment *CodeViewContext::getStringTableFragment() { |
| 113 | if (!StrTabFragment) { |
| 114 | StrTabFragment = new MCDataFragment(); |
| 115 | // Start a new string table out with a null byte. |
| 116 | StrTabFragment->getContents().push_back('\0'); |
| 117 | } |
| 118 | return StrTabFragment; |
| 119 | } |
| 120 | |
| 121 | StringRef CodeViewContext::addToStringTable(StringRef S) { |
| 122 | SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents(); |
| 123 | auto Insertion = |
| 124 | StringTable.insert(std::make_pair(S, unsigned(Contents.size()))); |
| 125 | // Return the string from the table, since it is stable. |
| 126 | S = Insertion.first->first(); |
| 127 | if (Insertion.second) { |
| 128 | // The string map key is always null terminated. |
| 129 | Contents.append(S.begin(), S.end() + 1); |
| 130 | } |
| 131 | return S; |
| 132 | } |
| 133 | |
| 134 | unsigned CodeViewContext::getStringTableOffset(StringRef S) { |
| 135 | // A string table offset of zero is always the empty string. |
| 136 | if (S.empty()) |
| 137 | return 0; |
| 138 | auto I = StringTable.find(S); |
| 139 | assert(I != StringTable.end()); |
| 140 | return I->second; |
| 141 | } |
| 142 | |
| 143 | void CodeViewContext::emitStringTable(MCObjectStreamer &OS) { |
| 144 | MCContext &Ctx = OS.getContext(); |
David Blaikie | a0b44ef | 2016-01-29 02:23:13 +0000 | [diff] [blame] | 145 | MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false), |
| 146 | *StringEnd = Ctx.createTempSymbol("strtab_end", false); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 147 | |
| 148 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::StringTable), 4); |
| 149 | OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4); |
| 150 | OS.EmitLabel(StringBegin); |
| 151 | |
| 152 | // Put the string table data fragment here, if we haven't already put it |
| 153 | // somewhere else. If somebody wants two string tables in their .s file, one |
| 154 | // will just be empty. |
| 155 | if (!InsertedStrTabFragment) { |
| 156 | OS.insert(getStringTableFragment()); |
| 157 | InsertedStrTabFragment = true; |
| 158 | } |
| 159 | |
| 160 | OS.EmitValueToAlignment(4, 0); |
| 161 | |
| 162 | OS.EmitLabel(StringEnd); |
| 163 | } |
| 164 | |
| 165 | void CodeViewContext::emitFileChecksums(MCObjectStreamer &OS) { |
Reid Kleckner | ee641c2 | 2016-06-08 17:50:29 +0000 | [diff] [blame] | 166 | // Do nothing if there are no file checksums. Microsoft's linker rejects empty |
| 167 | // CodeView substreams. |
| 168 | if (Filenames.empty()) |
| 169 | return; |
| 170 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 171 | MCContext &Ctx = OS.getContext(); |
David Blaikie | a0b44ef | 2016-01-29 02:23:13 +0000 | [diff] [blame] | 172 | MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false), |
| 173 | *FileEnd = Ctx.createTempSymbol("filechecksums_end", false); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 174 | |
| 175 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::FileChecksums), 4); |
| 176 | OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4); |
| 177 | OS.EmitLabel(FileBegin); |
| 178 | |
| 179 | // Emit an array of FileChecksum entries. We index into this table using the |
| 180 | // user-provided file number. Each entry is currently 8 bytes, as we don't |
| 181 | // emit checksums. |
| 182 | for (StringRef Filename : Filenames) { |
| 183 | OS.EmitIntValue(getStringTableOffset(Filename), 4); |
| 184 | // Zero the next two fields and align back to 4 bytes. This indicates that |
| 185 | // no checksum is present. |
| 186 | OS.EmitIntValue(0, 4); |
| 187 | } |
| 188 | |
| 189 | OS.EmitLabel(FileEnd); |
| 190 | } |
| 191 | |
| 192 | void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS, |
| 193 | unsigned FuncId, |
| 194 | const MCSymbol *FuncBegin, |
| 195 | const MCSymbol *FuncEnd) { |
| 196 | MCContext &Ctx = OS.getContext(); |
David Blaikie | a0b44ef | 2016-01-29 02:23:13 +0000 | [diff] [blame] | 197 | MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false), |
| 198 | *LineEnd = Ctx.createTempSymbol("linetable_end", false); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 199 | |
| 200 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::Lines), 4); |
| 201 | OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4); |
| 202 | OS.EmitLabel(LineBegin); |
| 203 | OS.EmitCOFFSecRel32(FuncBegin); |
| 204 | OS.EmitCOFFSectionIndex(FuncBegin); |
| 205 | |
| 206 | // Actual line info. |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 207 | std::vector<MCCVLineEntry> Locs = getFunctionLineEntries(FuncId); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 208 | bool HaveColumns = any_of(Locs, [](const MCCVLineEntry &LineEntry) { |
| 209 | return LineEntry.getColumn() != 0; |
| 210 | }); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 211 | OS.EmitIntValue(HaveColumns ? int(LineFlags::HaveColumns) : 0, 2); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 212 | OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4); |
| 213 | |
| 214 | for (auto I = Locs.begin(), E = Locs.end(); I != E;) { |
| 215 | // Emit a file segment for the run of locations that share a file id. |
| 216 | unsigned CurFileNum = I->getFileNum(); |
| 217 | auto FileSegEnd = |
| 218 | std::find_if(I, E, [CurFileNum](const MCCVLineEntry &Loc) { |
| 219 | return Loc.getFileNum() != CurFileNum; |
| 220 | }); |
| 221 | unsigned EntryCount = FileSegEnd - I; |
| 222 | OS.AddComment("Segment for file '" + Twine(Filenames[CurFileNum - 1]) + |
| 223 | "' begins"); |
| 224 | OS.EmitIntValue(8 * (CurFileNum - 1), 4); |
| 225 | OS.EmitIntValue(EntryCount, 4); |
| 226 | uint32_t SegmentSize = 12; |
| 227 | SegmentSize += 8 * EntryCount; |
| 228 | if (HaveColumns) |
| 229 | SegmentSize += 4 * EntryCount; |
| 230 | OS.EmitIntValue(SegmentSize, 4); |
| 231 | |
| 232 | for (auto J = I; J != FileSegEnd; ++J) { |
| 233 | OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4); |
| 234 | unsigned LineData = J->getLine(); |
| 235 | if (J->isStmt()) |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 236 | LineData |= LineInfo::StatementFlag; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 237 | OS.EmitIntValue(LineData, 4); |
| 238 | } |
| 239 | if (HaveColumns) { |
| 240 | for (auto J = I; J != FileSegEnd; ++J) { |
| 241 | OS.EmitIntValue(J->getColumn(), 2); |
| 242 | OS.EmitIntValue(0, 2); |
| 243 | } |
| 244 | } |
| 245 | I = FileSegEnd; |
| 246 | } |
| 247 | OS.EmitLabel(LineEnd); |
| 248 | } |
| 249 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 250 | static bool compressAnnotation(uint32_t Data, SmallVectorImpl<char> &Buffer) { |
| 251 | if (isUInt<7>(Data)) { |
| 252 | Buffer.push_back(Data); |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | if (isUInt<14>(Data)) { |
| 257 | Buffer.push_back((Data >> 8) | 0x80); |
| 258 | Buffer.push_back(Data & 0xff); |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | if (isUInt<29>(Data)) { |
| 263 | Buffer.push_back((Data >> 24) | 0xC0); |
| 264 | Buffer.push_back((Data >> 16) & 0xff); |
| 265 | Buffer.push_back((Data >> 8) & 0xff); |
| 266 | Buffer.push_back(Data & 0xff); |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | return false; |
| 271 | } |
| 272 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 273 | static bool compressAnnotation(BinaryAnnotationsOpCode Annotation, |
| 274 | SmallVectorImpl<char> &Buffer) { |
| 275 | return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer); |
| 276 | } |
| 277 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 278 | static uint32_t encodeSignedNumber(uint32_t Data) { |
| 279 | if (Data >> 31) |
| 280 | return ((-Data) << 1) | 1; |
| 281 | return Data << 1; |
| 282 | } |
| 283 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 284 | void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS, |
| 285 | unsigned PrimaryFunctionId, |
| 286 | unsigned SourceFileId, |
| 287 | unsigned SourceLineNum, |
| 288 | const MCSymbol *FnStartSym, |
| 289 | const MCSymbol *FnEndSym) { |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 290 | // Create and insert a fragment into the current section that will be encoded |
| 291 | // later. |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 292 | new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId, |
| 293 | SourceLineNum, FnStartSym, FnEndSym, |
| 294 | OS.getCurrentSectionOnly()); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 295 | } |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 296 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 297 | void CodeViewContext::emitDefRange( |
| 298 | MCObjectStreamer &OS, |
| 299 | ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, |
| 300 | StringRef FixedSizePortion) { |
| 301 | // Create and insert a fragment into the current section that will be encoded |
| 302 | // later. |
| 303 | new MCCVDefRangeFragment(Ranges, FixedSizePortion, |
| 304 | OS.getCurrentSectionOnly()); |
| 305 | } |
| 306 | |
Reid Kleckner | cb91e7d | 2016-02-04 00:21:42 +0000 | [diff] [blame] | 307 | static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin, |
| 308 | const MCSymbol *End) { |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 309 | MCContext &Ctx = Layout.getAssembler().getContext(); |
| 310 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 311 | const MCExpr *BeginRef = MCSymbolRefExpr::create(Begin, Variant, Ctx), |
| 312 | *EndRef = MCSymbolRefExpr::create(End, Variant, Ctx); |
| 313 | const MCExpr *AddrDelta = |
| 314 | MCBinaryExpr::create(MCBinaryExpr::Sub, EndRef, BeginRef, Ctx); |
| 315 | int64_t Result; |
| 316 | bool Success = AddrDelta->evaluateKnownAbsolute(Result, Layout); |
| 317 | assert(Success && "failed to evaluate label difference as absolute"); |
| 318 | (void)Success; |
| 319 | assert(Result >= 0 && "negative label difference requested"); |
| 320 | assert(Result < UINT_MAX && "label difference greater than 2GB"); |
| 321 | return unsigned(Result); |
| 322 | } |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 323 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 324 | void CodeViewContext::encodeInlineLineTable(MCAsmLayout &Layout, |
| 325 | MCCVInlineLineTableFragment &Frag) { |
| 326 | size_t LocBegin; |
| 327 | size_t LocEnd; |
| 328 | std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 329 | |
| 330 | // Include all child inline call sites in our .cv_loc extent. |
| 331 | MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(Frag.SiteFuncId); |
| 332 | for (auto &KV : SiteInfo->InlinedAtMap) { |
| 333 | unsigned ChildId = KV.first; |
| 334 | auto Extent = getLineExtent(ChildId); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 335 | LocBegin = std::min(LocBegin, Extent.first); |
| 336 | LocEnd = std::max(LocEnd, Extent.second); |
| 337 | } |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 338 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 339 | if (LocBegin >= LocEnd) |
| 340 | return; |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 341 | ArrayRef<MCCVLineEntry> Locs = getLinesForExtent(LocBegin, LocEnd); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 342 | if (Locs.empty()) |
| 343 | return; |
| 344 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 345 | // Make an artificial start location using the function start and the inlinee |
| 346 | // lines start location information. All deltas start relative to this |
| 347 | // location. |
| 348 | MCCVLineEntry StartLoc(Frag.getFnStartSym(), MCCVLoc(Locs.front())); |
| 349 | StartLoc.setFileNum(Frag.StartFileId); |
| 350 | StartLoc.setLine(Frag.StartLineNum); |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 351 | bool HaveOpenRange = false; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 352 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 353 | const MCSymbol *LastLabel = Frag.getFnStartSym(); |
| 354 | MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc; |
| 355 | LastSourceLoc.File = Frag.StartFileId; |
| 356 | LastSourceLoc.Line = Frag.StartLineNum; |
| 357 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 358 | SmallVectorImpl<char> &Buffer = Frag.getContents(); |
| 359 | Buffer.clear(); // Clear old contents if we went through relaxation. |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 360 | for (const MCCVLineEntry &Loc : Locs) { |
Reid Kleckner | bb96df6 | 2016-10-05 22:36:07 +0000 | [diff] [blame] | 361 | // Exit early if our line table would produce an oversized InlineSiteSym |
| 362 | // record. Account for the ChangeCodeLength annotation emitted after the |
| 363 | // loop ends. |
| 364 | size_t MaxBufferSize = MaxRecordLength - sizeof(InlineSiteSym::Hdr) - 8; |
| 365 | if (Buffer.size() >= MaxBufferSize) |
| 366 | break; |
| 367 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 368 | if (Loc.getFunctionId() == Frag.SiteFuncId) { |
| 369 | CurSourceLoc.File = Loc.getFileNum(); |
| 370 | CurSourceLoc.Line = Loc.getLine(); |
| 371 | } else { |
| 372 | auto I = SiteInfo->InlinedAtMap.find(Loc.getFunctionId()); |
| 373 | if (I != SiteInfo->InlinedAtMap.end()) { |
| 374 | // This .cv_loc is from a child inline call site. Use the source |
| 375 | // location of the inlined call site instead of the .cv_loc directive |
| 376 | // source location. |
| 377 | CurSourceLoc = I->second; |
| 378 | } else { |
| 379 | // We've hit a cv_loc not attributed to this inline call site. Use this |
| 380 | // label to end the PC range. |
| 381 | if (HaveOpenRange) { |
| 382 | unsigned Length = computeLabelDiff(Layout, LastLabel, Loc.getLabel()); |
| 383 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer); |
| 384 | compressAnnotation(Length, Buffer); |
| 385 | LastLabel = Loc.getLabel(); |
| 386 | } |
| 387 | HaveOpenRange = false; |
| 388 | continue; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 389 | } |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 390 | } |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 391 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 392 | // Skip this .cv_loc if we have an open range and this isn't a meaningful |
| 393 | // source location update. The current table format does not support column |
| 394 | // info, so we can skip updates for those. |
| 395 | if (HaveOpenRange && CurSourceLoc.File == LastSourceLoc.File && |
| 396 | CurSourceLoc.Line == LastSourceLoc.Line) |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 397 | continue; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 398 | |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 399 | HaveOpenRange = true; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 400 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 401 | if (CurSourceLoc.File != LastSourceLoc.File) { |
Reid Kleckner | 344078f | 2016-02-19 23:55:38 +0000 | [diff] [blame] | 402 | // File ids are 1 based, and each file checksum table entry is 8 bytes |
| 403 | // long. See emitFileChecksums above. |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 404 | unsigned FileOffset = 8 * (CurSourceLoc.File - 1); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 405 | compressAnnotation(BinaryAnnotationsOpCode::ChangeFile, Buffer); |
Reid Kleckner | 344078f | 2016-02-19 23:55:38 +0000 | [diff] [blame] | 406 | compressAnnotation(FileOffset, Buffer); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 409 | int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 410 | unsigned EncodedLineDelta = encodeSignedNumber(LineDelta); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 411 | unsigned CodeDelta = computeLabelDiff(Layout, LastLabel, Loc.getLabel()); |
| 412 | if (CodeDelta == 0 && LineDelta != 0) { |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 413 | compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 414 | compressAnnotation(EncodedLineDelta, Buffer); |
| 415 | } else if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) { |
| 416 | // The ChangeCodeOffsetAndLineOffset combination opcode is used when the |
| 417 | // encoded line delta uses 3 or fewer set bits and the code offset fits |
| 418 | // in one nibble. |
| 419 | unsigned Operand = (EncodedLineDelta << 4) | CodeDelta; |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 420 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset, |
| 421 | Buffer); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 422 | compressAnnotation(Operand, Buffer); |
| 423 | } else { |
| 424 | // Otherwise use the separate line and code deltas. |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 425 | if (LineDelta != 0) { |
| 426 | compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer); |
| 427 | compressAnnotation(EncodedLineDelta, Buffer); |
| 428 | } |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 429 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffset, Buffer); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 430 | compressAnnotation(CodeDelta, Buffer); |
| 431 | } |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 432 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 433 | LastLabel = Loc.getLabel(); |
| 434 | LastSourceLoc = CurSourceLoc; |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 435 | } |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 436 | |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 437 | assert(HaveOpenRange); |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 438 | |
| 439 | unsigned EndSymLength = |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 440 | computeLabelDiff(Layout, LastLabel, Frag.getFnEndSym()); |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 441 | unsigned LocAfterLength = ~0U; |
| 442 | ArrayRef<MCCVLineEntry> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1); |
Reid Kleckner | cb91e7d | 2016-02-04 00:21:42 +0000 | [diff] [blame] | 443 | if (!LocAfter.empty()) { |
| 444 | // Only try to compute this difference if we're in the same section. |
| 445 | const MCCVLineEntry &Loc = LocAfter[0]; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 446 | if (&Loc.getLabel()->getSection(false) == &LastLabel->getSection(false)) |
| 447 | LocAfterLength = computeLabelDiff(Layout, LastLabel, Loc.getLabel()); |
Reid Kleckner | cb91e7d | 2016-02-04 00:21:42 +0000 | [diff] [blame] | 448 | } |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 449 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 450 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer); |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 451 | compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 452 | } |
| 453 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 454 | void CodeViewContext::encodeDefRange(MCAsmLayout &Layout, |
| 455 | MCCVDefRangeFragment &Frag) { |
| 456 | MCContext &Ctx = Layout.getAssembler().getContext(); |
| 457 | SmallVectorImpl<char> &Contents = Frag.getContents(); |
| 458 | Contents.clear(); |
| 459 | SmallVectorImpl<MCFixup> &Fixups = Frag.getFixups(); |
| 460 | Fixups.clear(); |
| 461 | raw_svector_ostream OS(Contents); |
| 462 | |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 463 | // Compute all the sizes up front. |
| 464 | SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes; |
| 465 | const MCSymbol *LastLabel = nullptr; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 466 | for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) { |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 467 | unsigned GapSize = |
| 468 | LastLabel ? computeLabelDiff(Layout, LastLabel, Range.first) : 0; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 469 | unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second); |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 470 | GapAndRangeSizes.push_back({GapSize, RangeSize}); |
| 471 | LastLabel = Range.second; |
| 472 | } |
| 473 | |
| 474 | // Write down each range where the variable is defined. |
| 475 | for (size_t I = 0, E = Frag.getRanges().size(); I != E;) { |
| 476 | // If the range size of multiple consecutive ranges is under the max, |
| 477 | // combine the ranges and emit some gaps. |
| 478 | const MCSymbol *RangeBegin = Frag.getRanges()[I].first; |
| 479 | unsigned RangeSize = GapAndRangeSizes[I].second; |
| 480 | size_t J = I + 1; |
| 481 | for (; J != E; ++J) { |
| 482 | unsigned GapAndRangeSize = GapAndRangeSizes[J].first + GapAndRangeSizes[J].second; |
| 483 | if (RangeSize + GapAndRangeSize > MaxDefRange) |
| 484 | break; |
| 485 | RangeSize += GapAndRangeSize; |
| 486 | } |
| 487 | unsigned NumGaps = J - I - 1; |
| 488 | |
| 489 | support::endian::Writer<support::little> LEWriter(OS); |
| 490 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 491 | unsigned Bias = 0; |
| 492 | // We must split the range into chunks of MaxDefRange, this is a fundamental |
| 493 | // limitation of the file format. |
| 494 | do { |
| 495 | uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize); |
| 496 | |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 497 | const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 498 | const MCBinaryExpr *BE = |
| 499 | MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx); |
| 500 | MCValue Res; |
| 501 | BE->evaluateAsRelocatable(Res, &Layout, /*Fixup=*/nullptr); |
| 502 | |
| 503 | // Each record begins with a 2-byte number indicating how large the record |
| 504 | // is. |
| 505 | StringRef FixedSizePortion = Frag.getFixedSizePortion(); |
| 506 | // Our record is a fixed sized prefix and a LocalVariableAddrRange that we |
| 507 | // are artificially constructing. |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 508 | size_t RecordSize = FixedSizePortion.size() + |
| 509 | sizeof(LocalVariableAddrRange) + 4 * NumGaps; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 510 | // Write out the recrod size. |
| 511 | support::endian::Writer<support::little>(OS).write<uint16_t>(RecordSize); |
| 512 | // Write out the fixed size prefix. |
| 513 | OS << FixedSizePortion; |
| 514 | // Make space for a fixup that will eventually have a section relative |
| 515 | // relocation pointing at the offset where the variable becomes live. |
| 516 | Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4)); |
| 517 | Contents.resize(Contents.size() + 4); // Fixup for code start. |
| 518 | // Make space for a fixup that will record the section index for the code. |
| 519 | Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2)); |
| 520 | Contents.resize(Contents.size() + 2); // Fixup for section index. |
| 521 | // Write down the range's extent. |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 522 | LEWriter.write<uint16_t>(Chunk); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 523 | |
| 524 | // Move on to the next range. |
| 525 | Bias += Chunk; |
| 526 | RangeSize -= Chunk; |
| 527 | } while (RangeSize > 0); |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 528 | |
| 529 | // Emit the gaps afterwards. |
| 530 | assert((NumGaps == 0 || Bias < MaxDefRange) && |
| 531 | "large ranges should not have gaps"); |
| 532 | unsigned GapStartOffset = GapAndRangeSizes[I].second; |
| 533 | for (++I; I != J; ++I) { |
| 534 | unsigned GapSize, RangeSize; |
| 535 | assert(I < GapAndRangeSizes.size()); |
| 536 | std::tie(GapSize, RangeSize) = GapAndRangeSizes[I]; |
| 537 | LEWriter.write<uint16_t>(GapStartOffset); |
| 538 | LEWriter.write<uint16_t>(RangeSize); |
| 539 | GapStartOffset += GapSize + RangeSize; |
| 540 | } |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 544 | // |
| 545 | // This is called when an instruction is assembled into the specified section |
| 546 | // and if there is information from the last .cv_loc directive that has yet to have |
| 547 | // a line entry made for it is made. |
| 548 | // |
| 549 | void MCCVLineEntry::Make(MCObjectStreamer *MCOS) { |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 550 | CodeViewContext &CVC = MCOS->getContext().getCVContext(); |
| 551 | if (!CVC.getCVLocSeen()) |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 552 | return; |
| 553 | |
| 554 | // Create a symbol at in the current section for use in the line entry. |
| 555 | MCSymbol *LineSym = MCOS->getContext().createTempSymbol(); |
| 556 | // Set the value of the symbol to use for the MCCVLineEntry. |
| 557 | MCOS->EmitLabel(LineSym); |
| 558 | |
| 559 | // Get the current .loc info saved in the context. |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 560 | const MCCVLoc &CVLoc = CVC.getCurrentCVLoc(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 561 | |
| 562 | // Create a (local) line entry with the symbol and the current .loc info. |
| 563 | MCCVLineEntry LineEntry(LineSym, CVLoc); |
| 564 | |
| 565 | // clear CVLocSeen saying the current .loc info is now used. |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 566 | CVC.clearCVLocSeen(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 567 | |
| 568 | // Add the line entry to this section's entries. |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 569 | CVC.addLineEntry(LineEntry); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 570 | } |