LLVM  10.0.0svn
MCCodeView.cpp
Go to the documentation of this file.
1 //===- MCCodeView.h - Machine Code CodeView support -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Holds state from .cv_file and .cv_loc directives for later emission.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/MC/MCCodeView.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCValue.h"
24 
25 using namespace llvm;
26 using namespace llvm::codeview;
27 
29 
31  // If someone inserted strings into the string table but never actually
32  // emitted them somewhere, clean up the fragment.
33  if (!InsertedStrTabFragment)
34  delete StrTabFragment;
35 }
36 
37 /// This is a valid number for use with .cv_loc if we've already seen a .cv_file
38 /// for it.
39 bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const {
40  unsigned Idx = FileNumber - 1;
41  if (Idx < Files.size())
42  return Files[Idx].Assigned;
43  return false;
44 }
45 
46 bool CodeViewContext::addFile(MCStreamer &OS, unsigned FileNumber,
47  StringRef Filename,
48  ArrayRef<uint8_t> ChecksumBytes,
49  uint8_t ChecksumKind) {
50  assert(FileNumber > 0);
51  auto FilenameOffset = addToStringTable(Filename);
52  Filename = FilenameOffset.first;
53  unsigned Idx = FileNumber - 1;
54  if (Idx >= Files.size())
55  Files.resize(Idx + 1);
56 
57  if (Filename.empty())
58  Filename = "<stdin>";
59 
60  if (Files[Idx].Assigned)
61  return false;
62 
63  FilenameOffset = addToStringTable(Filename);
64  Filename = FilenameOffset.first;
65  unsigned Offset = FilenameOffset.second;
66 
67  auto ChecksumOffsetSymbol =
68  OS.getContext().createTempSymbol("checksum_offset", false);
69  Files[Idx].StringTableOffset = Offset;
70  Files[Idx].ChecksumTableOffset = ChecksumOffsetSymbol;
71  Files[Idx].Assigned = true;
72  Files[Idx].Checksum = ChecksumBytes;
73  Files[Idx].ChecksumKind = ChecksumKind;
74 
75  return true;
76 }
77 
79  if (FuncId >= Functions.size())
80  return nullptr;
81  if (Functions[FuncId].isUnallocatedFunctionInfo())
82  return nullptr;
83  return &Functions[FuncId];
84 }
85 
87  if (FuncId >= Functions.size())
88  Functions.resize(FuncId + 1);
89 
90  // Return false if this function info was already allocated.
91  if (!Functions[FuncId].isUnallocatedFunctionInfo())
92  return false;
93 
94  // Mark this as an allocated normal function, and leave the rest alone.
95  Functions[FuncId].ParentFuncIdPlusOne = MCCVFunctionInfo::FunctionSentinel;
96  return true;
97 }
98 
99 bool CodeViewContext::recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc,
100  unsigned IAFile, unsigned IALine,
101  unsigned IACol) {
102  if (FuncId >= Functions.size())
103  Functions.resize(FuncId + 1);
104 
105  // Return false if this function info was already allocated.
106  if (!Functions[FuncId].isUnallocatedFunctionInfo())
107  return false;
108 
109  MCCVFunctionInfo::LineInfo InlinedAt;
110  InlinedAt.File = IAFile;
111  InlinedAt.Line = IALine;
112  InlinedAt.Col = IACol;
113 
114  // Mark this as an inlined call site and record call site line info.
115  MCCVFunctionInfo *Info = &Functions[FuncId];
116  Info->ParentFuncIdPlusOne = IAFunc + 1;
117  Info->InlinedAt = InlinedAt;
118 
119  // Walk up the call chain adding this function id to the InlinedAtMap of all
120  // transitive callers until we hit a real function.
121  while (Info->isInlinedCallSite()) {
122  InlinedAt = Info->InlinedAt;
123  Info = getCVFunctionInfo(Info->getParentFuncId());
124  Info->InlinedAtMap[FuncId] = InlinedAt;
125  }
126 
127  return true;
128 }
129 
131  unsigned FunctionId, unsigned FileNo,
132  unsigned Line, unsigned Column,
133  bool PrologueEnd, bool IsStmt) {
134  addLineEntry(MCCVLoc{
135  Label, FunctionId, FileNo, Line, Column, PrologueEnd, IsStmt});
136 }
137 
138 MCDataFragment *CodeViewContext::getStringTableFragment() {
139  if (!StrTabFragment) {
140  StrTabFragment = new MCDataFragment();
141  // Start a new string table out with a null byte.
142  StrTabFragment->getContents().push_back('\0');
143  }
144  return StrTabFragment;
145 }
146 
147 std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {
148  SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents();
149  auto Insertion =
150  StringTable.insert(std::make_pair(S, unsigned(Contents.size())));
151  // Return the string from the table, since it is stable.
152  std::pair<StringRef, unsigned> Ret =
153  std::make_pair(Insertion.first->first(), Insertion.first->second);
154  if (Insertion.second) {
155  // The string map key is always null terminated.
156  Contents.append(Ret.first.begin(), Ret.first.end() + 1);
157  }
158  return Ret;
159 }
160 
161 unsigned CodeViewContext::getStringTableOffset(StringRef S) {
162  // A string table offset of zero is always the empty string.
163  if (S.empty())
164  return 0;
165  auto I = StringTable.find(S);
166  assert(I != StringTable.end());
167  return I->second;
168 }
169 
171  MCContext &Ctx = OS.getContext();
172  MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false),
173  *StringEnd = Ctx.createTempSymbol("strtab_end", false);
174 
176  OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4);
177  OS.EmitLabel(StringBegin);
178 
179  // Put the string table data fragment here, if we haven't already put it
180  // somewhere else. If somebody wants two string tables in their .s file, one
181  // will just be empty.
182  if (!InsertedStrTabFragment) {
183  OS.insert(getStringTableFragment());
184  InsertedStrTabFragment = true;
185  }
186 
187  OS.EmitValueToAlignment(4, 0);
188 
189  OS.EmitLabel(StringEnd);
190 }
191 
193  // Do nothing if there are no file checksums. Microsoft's linker rejects empty
194  // CodeView substreams.
195  if (Files.empty())
196  return;
197 
198  MCContext &Ctx = OS.getContext();
199  MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false),
200  *FileEnd = Ctx.createTempSymbol("filechecksums_end", false);
201 
203  OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4);
204  OS.EmitLabel(FileBegin);
205 
206  unsigned CurrentOffset = 0;
207 
208  // Emit an array of FileChecksum entries. We index into this table using the
209  // user-provided file number. Each entry may be a variable number of bytes
210  // determined by the checksum kind and size.
211  for (auto File : Files) {
212  OS.EmitAssignment(File.ChecksumTableOffset,
213  MCConstantExpr::create(CurrentOffset, Ctx));
214  CurrentOffset += 4; // String table offset.
215  if (!File.ChecksumKind) {
216  CurrentOffset +=
217  4; // One byte each for checksum size and kind, then align to 4 bytes.
218  } else {
219  CurrentOffset += 2; // One byte each for checksum size and kind.
220  CurrentOffset += File.Checksum.size();
221  CurrentOffset = alignTo(CurrentOffset, 4);
222  }
223 
224  OS.EmitIntValue(File.StringTableOffset, 4);
225 
226  if (!File.ChecksumKind) {
227  // There is no checksum. Therefore zero the next two fields and align
228  // back to 4 bytes.
229  OS.EmitIntValue(0, 4);
230  continue;
231  }
232  OS.EmitIntValue(static_cast<uint8_t>(File.Checksum.size()), 1);
233  OS.EmitIntValue(File.ChecksumKind, 1);
234  OS.EmitBytes(toStringRef(File.Checksum));
235  OS.EmitValueToAlignment(4);
236  }
237 
238  OS.EmitLabel(FileEnd);
239 
240  ChecksumOffsetsAssigned = true;
241 }
242 
243 // Output checksum table offset of the given file number. It is possible that
244 // not all files have been registered yet, and so the offset cannot be
245 // calculated. In this case a symbol representing the offset is emitted, and
246 // the value of this symbol will be fixed up at a later time.
248  unsigned FileNo) {
249  unsigned Idx = FileNo - 1;
250 
251  if (Idx >= Files.size())
252  Files.resize(Idx + 1);
253 
254  if (ChecksumOffsetsAssigned) {
255  OS.EmitSymbolValue(Files[Idx].ChecksumTableOffset, 4);
256  return;
257  }
258 
259  const MCSymbolRefExpr *SRE =
260  MCSymbolRefExpr::create(Files[Idx].ChecksumTableOffset, OS.getContext());
261 
262  OS.EmitValueImpl(SRE, 4);
263 }
264 
265 void CodeViewContext::addLineEntry(const MCCVLoc &LineEntry) {
266  size_t Offset = MCCVLines.size();
267  auto I = MCCVLineStartStop.insert(
268  {LineEntry.getFunctionId(), {Offset, Offset + 1}});
269  if (!I.second)
270  I.first->second.second = Offset + 1;
271  MCCVLines.push_back(LineEntry);
272 }
273 
274 std::vector<MCCVLoc>
276  std::vector<MCCVLoc> FilteredLines;
277  auto I = MCCVLineStartStop.find(FuncId);
278  if (I != MCCVLineStartStop.end()) {
279  MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(FuncId);
280  for (size_t Idx = I->second.first, End = I->second.second; Idx != End;
281  ++Idx) {
282  unsigned LocationFuncId = MCCVLines[Idx].getFunctionId();
283  if (LocationFuncId == FuncId) {
284  // This was a .cv_loc directly for FuncId, so record it.
285  FilteredLines.push_back(MCCVLines[Idx]);
286  } else {
287  // Check if the current location is inlined in this function. If it is,
288  // synthesize a statement .cv_loc at the original inlined call site.
289  auto I = SiteInfo->InlinedAtMap.find(LocationFuncId);
290  if (I != SiteInfo->InlinedAtMap.end()) {
291  MCCVFunctionInfo::LineInfo &IA = I->second;
292  // Only add the location if it differs from the previous location.
293  // Large inlined calls will have many .cv_loc entries and we only need
294  // one line table entry in the parent function.
295  if (FilteredLines.empty() ||
296  FilteredLines.back().getFileNum() != IA.File ||
297  FilteredLines.back().getLine() != IA.Line ||
298  FilteredLines.back().getColumn() != IA.Col) {
299  FilteredLines.push_back(MCCVLoc(
300  MCCVLines[Idx].getLabel(),
301  FuncId, IA.File, IA.Line, IA.Col, false, false));
302  }
303  }
304  }
305  }
306  }
307  return FilteredLines;
308 }
309 
310 std::pair<size_t, size_t> CodeViewContext::getLineExtent(unsigned FuncId) {
311  auto I = MCCVLineStartStop.find(FuncId);
312  // Return an empty extent if there are no cv_locs for this function id.
313  if (I == MCCVLineStartStop.end())
314  return {~0ULL, 0};
315  return I->second;
316 }
317 
319  if (R <= L)
320  return None;
321  if (L >= MCCVLines.size())
322  return None;
323  return makeArrayRef(&MCCVLines[L], R - L);
324 }
325 
327  unsigned FuncId,
328  const MCSymbol *FuncBegin,
329  const MCSymbol *FuncEnd) {
330  MCContext &Ctx = OS.getContext();
331  MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false),
332  *LineEnd = Ctx.createTempSymbol("linetable_end", false);
333 
334  OS.EmitIntValue(unsigned(DebugSubsectionKind::Lines), 4);
335  OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4);
336  OS.EmitLabel(LineBegin);
337  OS.EmitCOFFSecRel32(FuncBegin, /*Offset=*/0);
338  OS.EmitCOFFSectionIndex(FuncBegin);
339 
340  // Actual line info.
341  std::vector<MCCVLoc> Locs = getFunctionLineEntries(FuncId);
342  bool HaveColumns = any_of(Locs, [](const MCCVLoc &LineEntry) {
343  return LineEntry.getColumn() != 0;
344  });
345  OS.EmitIntValue(HaveColumns ? int(LF_HaveColumns) : 0, 2);
346  OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4);
347 
348  for (auto I = Locs.begin(), E = Locs.end(); I != E;) {
349  // Emit a file segment for the run of locations that share a file id.
350  unsigned CurFileNum = I->getFileNum();
351  auto FileSegEnd =
352  std::find_if(I, E, [CurFileNum](const MCCVLoc &Loc) {
353  return Loc.getFileNum() != CurFileNum;
354  });
355  unsigned EntryCount = FileSegEnd - I;
356  OS.AddComment(
357  "Segment for file '" +
358  Twine(getStringTableFragment()
359  ->getContents()[Files[CurFileNum - 1].StringTableOffset]) +
360  "' begins");
361  OS.EmitCVFileChecksumOffsetDirective(CurFileNum);
362  OS.EmitIntValue(EntryCount, 4);
363  uint32_t SegmentSize = 12;
364  SegmentSize += 8 * EntryCount;
365  if (HaveColumns)
366  SegmentSize += 4 * EntryCount;
367  OS.EmitIntValue(SegmentSize, 4);
368 
369  for (auto J = I; J != FileSegEnd; ++J) {
370  OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4);
371  unsigned LineData = J->getLine();
372  if (J->isStmt())
373  LineData |= LineInfo::StatementFlag;
374  OS.EmitIntValue(LineData, 4);
375  }
376  if (HaveColumns) {
377  for (auto J = I; J != FileSegEnd; ++J) {
378  OS.EmitIntValue(J->getColumn(), 2);
379  OS.EmitIntValue(0, 2);
380  }
381  }
382  I = FileSegEnd;
383  }
384  OS.EmitLabel(LineEnd);
385 }
386 
388  if (isUInt<7>(Data)) {
389  Buffer.push_back(Data);
390  return true;
391  }
392 
393  if (isUInt<14>(Data)) {
394  Buffer.push_back((Data >> 8) | 0x80);
395  Buffer.push_back(Data & 0xff);
396  return true;
397  }
398 
399  if (isUInt<29>(Data)) {
400  Buffer.push_back((Data >> 24) | 0xC0);
401  Buffer.push_back((Data >> 16) & 0xff);
402  Buffer.push_back((Data >> 8) & 0xff);
403  Buffer.push_back(Data & 0xff);
404  return true;
405  }
406 
407  return false;
408 }
409 
411  SmallVectorImpl<char> &Buffer) {
412  return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer);
413 }
414 
416  if (Data >> 31)
417  return ((-Data) << 1) | 1;
418  return Data << 1;
419 }
420 
422  unsigned PrimaryFunctionId,
423  unsigned SourceFileId,
424  unsigned SourceLineNum,
425  const MCSymbol *FnStartSym,
426  const MCSymbol *FnEndSym) {
427  // Create and insert a fragment into the current section that will be encoded
428  // later.
429  new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId,
430  SourceLineNum, FnStartSym, FnEndSym,
431  OS.getCurrentSectionOnly());
432 }
433 
435  MCObjectStreamer &OS,
436  ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
437  StringRef FixedSizePortion) {
438  // Create and insert a fragment into the current section that will be encoded
439  // later.
440  return new MCCVDefRangeFragment(Ranges, FixedSizePortion,
441  OS.getCurrentSectionOnly());
442 }
443 
444 static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin,
445  const MCSymbol *End) {
446  MCContext &Ctx = Layout.getAssembler().getContext();
448  const MCExpr *BeginRef = MCSymbolRefExpr::create(Begin, Variant, Ctx),
449  *EndRef = MCSymbolRefExpr::create(End, Variant, Ctx);
450  const MCExpr *AddrDelta =
451  MCBinaryExpr::create(MCBinaryExpr::Sub, EndRef, BeginRef, Ctx);
452  int64_t Result;
453  bool Success = AddrDelta->evaluateKnownAbsolute(Result, Layout);
454  assert(Success && "failed to evaluate label difference as absolute");
455  (void)Success;
456  assert(Result >= 0 && "negative label difference requested");
457  assert(Result < UINT_MAX && "label difference greater than 2GB");
458  return unsigned(Result);
459 }
460 
463  size_t LocBegin;
464  size_t LocEnd;
465  std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId);
466 
467  // Include all child inline call sites in our .cv_loc extent.
468  MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(Frag.SiteFuncId);
469  for (auto &KV : SiteInfo->InlinedAtMap) {
470  unsigned ChildId = KV.first;
471  auto Extent = getLineExtent(ChildId);
472  LocBegin = std::min(LocBegin, Extent.first);
473  LocEnd = std::max(LocEnd, Extent.second);
474  }
475 
476  if (LocBegin >= LocEnd)
477  return;
478  ArrayRef<MCCVLoc> Locs = getLinesForExtent(LocBegin, LocEnd);
479  if (Locs.empty())
480  return;
481 
482  // Check that the locations are all in the same section.
483 #ifndef NDEBUG
484  const MCSection *FirstSec = &Locs.front().getLabel()->getSection();
485  for (const MCCVLoc &Loc : Locs) {
486  if (&Loc.getLabel()->getSection() != FirstSec) {
487  errs() << ".cv_loc " << Loc.getFunctionId() << ' ' << Loc.getFileNum()
488  << ' ' << Loc.getLine() << ' ' << Loc.getColumn()
489  << " is in the wrong section\n";
490  llvm_unreachable(".cv_loc crosses sections");
491  }
492  }
493 #endif
494 
495  // Make an artificial start location using the function start and the inlinee
496  // lines start location information. All deltas start relative to this
497  // location.
498  MCCVLoc StartLoc = Locs.front();
499  StartLoc.setLabel(Frag.getFnStartSym());
500  StartLoc.setFileNum(Frag.StartFileId);
501  StartLoc.setLine(Frag.StartLineNum);
502  bool HaveOpenRange = false;
503 
504  const MCSymbol *LastLabel = Frag.getFnStartSym();
505  MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc;
506  LastSourceLoc.File = Frag.StartFileId;
507  LastSourceLoc.Line = Frag.StartLineNum;
508 
509  SmallVectorImpl<char> &Buffer = Frag.getContents();
510  Buffer.clear(); // Clear old contents if we went through relaxation.
511  for (const MCCVLoc &Loc : Locs) {
512  // Exit early if our line table would produce an oversized InlineSiteSym
513  // record. Account for the ChangeCodeLength annotation emitted after the
514  // loop ends.
515  constexpr uint32_t InlineSiteSize = 12;
516  constexpr uint32_t AnnotationSize = 8;
517  size_t MaxBufferSize = MaxRecordLength - InlineSiteSize - AnnotationSize;
518  if (Buffer.size() >= MaxBufferSize)
519  break;
520 
521  if (Loc.getFunctionId() == Frag.SiteFuncId) {
522  CurSourceLoc.File = Loc.getFileNum();
523  CurSourceLoc.Line = Loc.getLine();
524  } else {
525  auto I = SiteInfo->InlinedAtMap.find(Loc.getFunctionId());
526  if (I != SiteInfo->InlinedAtMap.end()) {
527  // This .cv_loc is from a child inline call site. Use the source
528  // location of the inlined call site instead of the .cv_loc directive
529  // source location.
530  CurSourceLoc = I->second;
531  } else {
532  // We've hit a cv_loc not attributed to this inline call site. Use this
533  // label to end the PC range.
534  if (HaveOpenRange) {
535  unsigned Length = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
537  compressAnnotation(Length, Buffer);
538  LastLabel = Loc.getLabel();
539  }
540  HaveOpenRange = false;
541  continue;
542  }
543  }
544 
545  // Skip this .cv_loc if we have an open range and this isn't a meaningful
546  // source location update. The current table format does not support column
547  // info, so we can skip updates for those.
548  if (HaveOpenRange && CurSourceLoc.File == LastSourceLoc.File &&
549  CurSourceLoc.Line == LastSourceLoc.Line)
550  continue;
551 
552  HaveOpenRange = true;
553 
554  if (CurSourceLoc.File != LastSourceLoc.File) {
555  unsigned FileOffset = static_cast<const MCConstantExpr *>(
556  Files[CurSourceLoc.File - 1]
557  .ChecksumTableOffset->getVariableValue())
558  ->getValue();
560  compressAnnotation(FileOffset, Buffer);
561  }
562 
563  int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line;
564  unsigned EncodedLineDelta = encodeSignedNumber(LineDelta);
565  unsigned CodeDelta = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
566  if (CodeDelta == 0 && LineDelta != 0) {
568  compressAnnotation(EncodedLineDelta, Buffer);
569  } else if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) {
570  // The ChangeCodeOffsetAndLineOffset combination opcode is used when the
571  // encoded line delta uses 3 or fewer set bits and the code offset fits
572  // in one nibble.
573  unsigned Operand = (EncodedLineDelta << 4) | CodeDelta;
575  Buffer);
576  compressAnnotation(Operand, Buffer);
577  } else {
578  // Otherwise use the separate line and code deltas.
579  if (LineDelta != 0) {
581  compressAnnotation(EncodedLineDelta, Buffer);
582  }
584  compressAnnotation(CodeDelta, Buffer);
585  }
586 
587  LastLabel = Loc.getLabel();
588  LastSourceLoc = CurSourceLoc;
589  }
590 
591  assert(HaveOpenRange);
592 
593  unsigned EndSymLength =
594  computeLabelDiff(Layout, LastLabel, Frag.getFnEndSym());
595  unsigned LocAfterLength = ~0U;
596  ArrayRef<MCCVLoc> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1);
597  if (!LocAfter.empty()) {
598  // Only try to compute this difference if we're in the same section.
599  const MCCVLoc &Loc = LocAfter[0];
600  if (&Loc.getLabel()->getSection() == &LastLabel->getSection())
601  LocAfterLength = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
602  }
603 
605  compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer);
606 }
607 
609  MCCVDefRangeFragment &Frag) {
610  MCContext &Ctx = Layout.getAssembler().getContext();
611  SmallVectorImpl<char> &Contents = Frag.getContents();
612  Contents.clear();
614  Fixups.clear();
615  raw_svector_ostream OS(Contents);
616 
617  // Compute all the sizes up front.
618  SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes;
619  const MCSymbol *LastLabel = nullptr;
620  for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) {
621  unsigned GapSize =
622  LastLabel ? computeLabelDiff(Layout, LastLabel, Range.first) : 0;
623  unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second);
624  GapAndRangeSizes.push_back({GapSize, RangeSize});
625  LastLabel = Range.second;
626  }
627 
628  // Write down each range where the variable is defined.
629  for (size_t I = 0, E = Frag.getRanges().size(); I != E;) {
630  // If the range size of multiple consecutive ranges is under the max,
631  // combine the ranges and emit some gaps.
632  const MCSymbol *RangeBegin = Frag.getRanges()[I].first;
633  unsigned RangeSize = GapAndRangeSizes[I].second;
634  size_t J = I + 1;
635  for (; J != E; ++J) {
636  unsigned GapAndRangeSize = GapAndRangeSizes[J].first + GapAndRangeSizes[J].second;
637  if (RangeSize + GapAndRangeSize > MaxDefRange)
638  break;
639  RangeSize += GapAndRangeSize;
640  }
641  unsigned NumGaps = J - I - 1;
642 
644 
645  unsigned Bias = 0;
646  // We must split the range into chunks of MaxDefRange, this is a fundamental
647  // limitation of the file format.
648  do {
649  uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize);
650 
651  const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx);
652  const MCBinaryExpr *BE =
653  MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx);
654  MCValue Res;
655  BE->evaluateAsRelocatable(Res, &Layout, /*Fixup=*/nullptr);
656 
657  // Each record begins with a 2-byte number indicating how large the record
658  // is.
659  StringRef FixedSizePortion = Frag.getFixedSizePortion();
660  // Our record is a fixed sized prefix and a LocalVariableAddrRange that we
661  // are artificially constructing.
662  size_t RecordSize = FixedSizePortion.size() +
663  sizeof(LocalVariableAddrRange) + 4 * NumGaps;
664  // Write out the record size.
665  LEWriter.write<uint16_t>(RecordSize);
666  // Write out the fixed size prefix.
667  OS << FixedSizePortion;
668  // Make space for a fixup that will eventually have a section relative
669  // relocation pointing at the offset where the variable becomes live.
670  Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4));
671  LEWriter.write<uint32_t>(0); // Fixup for code start.
672  // Make space for a fixup that will record the section index for the code.
673  Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2));
674  LEWriter.write<uint16_t>(0); // Fixup for section index.
675  // Write down the range's extent.
676  LEWriter.write<uint16_t>(Chunk);
677 
678  // Move on to the next range.
679  Bias += Chunk;
680  RangeSize -= Chunk;
681  } while (RangeSize > 0);
682 
683  // Emit the gaps afterwards.
684  assert((NumGaps == 0 || Bias <= MaxDefRange) &&
685  "large ranges should not have gaps");
686  unsigned GapStartOffset = GapAndRangeSizes[I].second;
687  for (++I; I != J; ++I) {
688  unsigned GapSize, RangeSize;
689  assert(I < GapAndRangeSizes.size());
690  std::tie(GapSize, RangeSize) = GapAndRangeSizes[I];
691  LEWriter.write<uint16_t>(GapStartOffset);
692  LEWriter.write<uint16_t>(GapSize);
693  GapStartOffset += GapSize + RangeSize;
694  }
695  }
696 }
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:151
const NoneType None
Definition: None.h:23
Profile::FuncID FuncId
Definition: Profile.cpp:321
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F)
Definition: MCCodeView.cpp:608
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
Instances of this class represent the information from a .cv_loc directive.
Definition: MCCodeView.h:31
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
void emitLineTableForFunction(MCObjectStreamer &OS, unsigned FuncId, const MCSymbol *FuncBegin, const MCSymbol *FuncEnd)
Emits a line table substream.
Definition: MCCodeView.cpp:326
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:327
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
Definition: MCStreamer.cpp:165
This represents an "assembler immediate".
Definition: MCValue.h:39
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
SmallString< 8 > & getContents()
Definition: MCFragment.h:620
void push_back(const T &Elt)
Definition: SmallVector.h:211
std::pair< StringRef, unsigned > addToStringTable(StringRef S)
Add something to the string table.
Definition: MCCodeView.cpp:147
MCFragment * emitDefRange(MCObjectStreamer &OS, ArrayRef< std::pair< const MCSymbol *, const MCSymbol *>> Ranges, StringRef FixedSizePortion)
Definition: MCCodeView.cpp:434
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
void EmitCVFileChecksumOffsetDirective(unsigned FileNo) override
This implements the CodeView &#39;.cv_filechecksumoffset&#39; assembler directive.
StringRef getFixedSizePortion() const
Definition: MCFragment.h:653
DenseMap< unsigned, LineInfo > InlinedAtMap
Map from inlined call site id to the inlined at location to use for that call site.
Definition: MCCodeView.h:117
bool addFile(MCStreamer &OS, unsigned FileNumber, StringRef Filename, ArrayRef< uint8_t > ChecksumBytes, uint8_t ChecksumKind)
Definition: MCCodeView.cpp:46
static bool compressAnnotation(uint32_t Data, SmallVectorImpl< char > &Buffer)
Definition: MCCodeView.cpp:387
void emitInlineLineTableForFunction(MCObjectStreamer &OS, unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
Definition: MCCodeView.cpp:421
MCContext & getContext() const
Definition: MCAssembler.h:284
MCContext & getContext() const
Definition: MCStreamer.h:252
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
static uint32_t encodeSignedNumber(uint32_t Data)
Definition: MCCodeView.cpp:415
A four-byte section relative fixup.
Definition: MCFixup.h:43
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
Context object for machine code objects.
Definition: MCContext.h:65
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:311
A two-byte section relative fixup.
Definition: MCFixup.h:42
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:665
Streaming object file generation interface.
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
bool isInlinedCallSite() const
Returns true if this represents an inlined call site, meaning ParentFuncIdPlusOne is neither zero nor...
Definition: MCCodeView.h:125
unsigned getParentFuncId() const
Definition: MCCodeView.h:130
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:465
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:198
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:130
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:158
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) override
Emit the absolute difference between two symbols if possible.
void encodeInlineLineTable(MCAsmLayout &Layout, MCCVInlineLineTableFragment &F)
Encodes the binary annotations once we have a layout.
Definition: MCCodeView.cpp:461
Streaming machine code generation interface.
Definition: MCStreamer.h:190
bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const
Definition: MCExpr.cpp:480
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:225
void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void setLabel(const MCSymbol *L)
Definition: MCCodeView.h:67
void insert(MCFragment *F)
Instrumentation for Order File
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:224
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1172
std::vector< MCCVLoc > getFunctionLineEntries(unsigned FuncId)
Definition: MCCodeView.cpp:275
void setFileNum(unsigned fileNum)
Set the FileNum of this MCCVLoc.
Definition: MCCodeView.h:72
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:52
Binary assembler expressions.
Definition: MCExpr.h:421
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:93
size_t size() const
Definition: SmallVector.h:52
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:631
MCCVFunctionInfo * getCVFunctionInfo(unsigned FuncId)
Retreive the function info if this is a valid function id, or nullptr.
Definition: MCCodeView.cpp:78
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setLine(unsigned line)
Set the Line of this MCCVLoc.
Definition: MCCodeView.h:75
void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin, const MCSymbol *End)
Definition: MCCodeView.cpp:444
void write(ArrayRef< value_type > Val)
Definition: EndianStream.h:55
unsigned ParentFuncIdPlusOne
If this represents an inlined call site, then ParentFuncIdPlusOne will be the parent function id plus...
Definition: MCCodeView.h:97
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
unsigned getFileNum() const
Get the FileNum of this MCCVLoc.
Definition: MCCodeView.h:56
const MCSymbol * getFnEndSym() const
Definition: MCFragment.h:618
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:345
void recordCVLoc(MCContext &Ctx, const MCSymbol *Label, unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt)
Saves the information from the currently parsed .cv_loc directive and sets CVLocSeen.
Definition: MCCodeView.cpp:130
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:268
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
Fragment representing the binary annotations produced by the .cv_inline_linetable directive...
Definition: MCFragment.h:593
const MCSymbol * getFnStartSym() const
Definition: MCFragment.h:617
#define Success
bool recordFunctionId(unsigned FuncId)
Records the function id of a normal function.
Definition: MCCodeView.cpp:86
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:51
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:126
std::pair< size_t, size_t > getLineExtent(unsigned FuncId)
Definition: MCCodeView.cpp:310
ArrayRef< MCCVLoc > getLinesForExtent(size_t L, size_t R)
Definition: MCCodeView.cpp:318
bool recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc, unsigned IAFile, unsigned IALine, unsigned IACol)
Records the function id of an inlined call site.
Definition: MCCodeView.cpp:99
void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol.
#define I(x, y, z)
Definition: MD5.cpp:58
void addLineEntry(const MCCVLoc &LineEntry)
Add a line entry.
Definition: MCCodeView.cpp:265
bool isValidFileNumber(unsigned FileNumber) const
This is a valid number for use with .cv_loc if we&#39;ve already seen a .cv_file for it.
Definition: MCCodeView.cpp:39
void emitStringTable(MCObjectStreamer &OS)
Emits the string table substream.
Definition: MCCodeView.cpp:170
void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo)
Emits the offset into the checksum table of the given file number.
Definition: MCCodeView.cpp:247
void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
const MCSymbol * getLabel() const
Definition: MCCodeView.h:51
Fragment for data and encoded instructions.
Definition: MCFragment.h:243
virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset)
Emits a COFF section relative relocation.
Definition: MCStreamer.cpp:931
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > getRanges() const
Definition: MCFragment.h:649
unsigned getColumn() const
Get the Column of this MCCVLoc.
Definition: MCCodeView.h:62
Subtraction.
Definition: MCExpr.h:445
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Information describing a function or inlined call site introduced by .cv_func_id or ...
Definition: MCCodeView.h:91
unsigned getFunctionId() const
Definition: MCCodeView.h:53
void emitFileChecksums(MCObjectStreamer &OS)
Emits the file checksum substream.
Definition: MCCodeView.cpp:192
virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol)
Emits a COFF section index.
Definition: MCStreamer.cpp:929
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143