brintos

brintos / llvm-project-archived public Read only

0
0
Text · 25.2 KiB · 335934a Raw
699 lines · cpp
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-exception6//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"16#include "llvm/DebugInfo/CodeView/CodeView.h"17#include "llvm/DebugInfo/CodeView/Line.h"18#include "llvm/DebugInfo/CodeView/SymbolRecord.h"19#include "llvm/MC/MCAssembler.h"20#include "llvm/MC/MCContext.h"21#include "llvm/MC/MCObjectStreamer.h"22#include "llvm/MC/MCValue.h"23#include "llvm/Support/EndianStream.h"24 25using namespace llvm;26using namespace llvm::codeview;27 28void CodeViewContext::finish() {29  if (!StrTabFragment)30    return;31  assert(StrTabFragment->getKind() == MCFragment::FT_Data);32  StrTabFragment->setVarContents(StrTab);33}34 35/// This is a valid number for use with .cv_loc if we've already seen a .cv_file36/// for it.37bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const {38  unsigned Idx = FileNumber - 1;39  if (Idx < Files.size())40    return Files[Idx].Assigned;41  return false;42}43 44bool CodeViewContext::addFile(MCStreamer &OS, unsigned FileNumber,45                              StringRef Filename,46                              ArrayRef<uint8_t> ChecksumBytes,47                              uint8_t ChecksumKind) {48  assert(FileNumber > 0);49  auto FilenameOffset = addToStringTable(Filename);50  Filename = FilenameOffset.first;51  unsigned Idx = FileNumber - 1;52  if (Idx >= Files.size())53    Files.resize(Idx + 1);54 55  if (Filename.empty())56    Filename = "<stdin>";57 58  if (Files[Idx].Assigned)59    return false;60 61  FilenameOffset = addToStringTable(Filename);62  Filename = FilenameOffset.first;63  unsigned Offset = FilenameOffset.second;64 65  auto ChecksumOffsetSymbol =66      OS.getContext().createTempSymbol("checksum_offset", false);67  Files[Idx].StringTableOffset = Offset;68  Files[Idx].ChecksumTableOffset = ChecksumOffsetSymbol;69  Files[Idx].Assigned = true;70  Files[Idx].Checksum = ChecksumBytes;71  Files[Idx].ChecksumKind = ChecksumKind;72 73  return true;74}75 76MCCVFunctionInfo *CodeViewContext::getCVFunctionInfo(unsigned FuncId) {77  if (FuncId >= Functions.size())78    return nullptr;79  if (Functions[FuncId].isUnallocatedFunctionInfo())80    return nullptr;81  return &Functions[FuncId];82}83 84bool CodeViewContext::recordFunctionId(unsigned FuncId) {85  if (FuncId >= Functions.size())86    Functions.resize(FuncId + 1);87 88  // Return false if this function info was already allocated.89  if (!Functions[FuncId].isUnallocatedFunctionInfo())90    return false;91 92  // Mark this as an allocated normal function, and leave the rest alone.93  Functions[FuncId].ParentFuncIdPlusOne = MCCVFunctionInfo::FunctionSentinel;94  return true;95}96 97bool CodeViewContext::recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc,98                                              unsigned IAFile, unsigned IALine,99                                              unsigned IACol) {100  if (FuncId >= Functions.size())101    Functions.resize(FuncId + 1);102 103  // Return false if this function info was already allocated.104  if (!Functions[FuncId].isUnallocatedFunctionInfo())105    return false;106 107  MCCVFunctionInfo::LineInfo InlinedAt;108  InlinedAt.File = IAFile;109  InlinedAt.Line = IALine;110  InlinedAt.Col = IACol;111 112  // Mark this as an inlined call site and record call site line info.113  MCCVFunctionInfo *Info = &Functions[FuncId];114  Info->ParentFuncIdPlusOne = IAFunc + 1;115  Info->InlinedAt = InlinedAt;116 117  // Walk up the call chain adding this function id to the InlinedAtMap of all118  // transitive callers until we hit a real function.119  while (Info->isInlinedCallSite()) {120    InlinedAt = Info->InlinedAt;121    Info = getCVFunctionInfo(Info->getParentFuncId());122    Info->InlinedAtMap[FuncId] = InlinedAt;123  }124 125  return true;126}127 128void CodeViewContext::recordCVLoc(MCContext &Ctx, const MCSymbol *Label,129                                  unsigned FunctionId, unsigned FileNo,130                                  unsigned Line, unsigned Column,131                                  bool PrologueEnd, bool IsStmt) {132  addLineEntry(MCCVLoc{133      Label, FunctionId, FileNo, Line, Column, PrologueEnd, IsStmt});134}135 136std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {137  auto Insertion =138      StringTable.insert(std::make_pair(S, unsigned(StrTab.size())));139  // Return the string from the table, since it is stable.140  std::pair<StringRef, unsigned> Ret =141      std::make_pair(Insertion.first->first(), Insertion.first->second);142  if (Insertion.second) {143    // The string map key is always null terminated.144    StrTab.append(Ret.first.begin(), Ret.first.end() + 1);145  }146  return Ret;147}148 149unsigned CodeViewContext::getStringTableOffset(StringRef S) {150  // A string table offset of zero is always the empty string.151  if (S.empty())152    return 0;153  auto I = StringTable.find(S);154  assert(I != StringTable.end());155  return I->second;156}157 158void CodeViewContext::emitStringTable(MCObjectStreamer &OS) {159  MCContext &Ctx = OS.getContext();160  MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false),161           *StringEnd = Ctx.createTempSymbol("strtab_end", false);162 163  OS.emitInt32(uint32_t(DebugSubsectionKind::StringTable));164  OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4);165  OS.emitLabel(StringBegin);166 167  // Put the string table data fragment here, if we haven't already put it168  // somewhere else. If somebody wants two string tables in their .s file, one169  // will just be empty.170  if (!StrTabFragment) {171    OS.newFragment();172    StrTabFragment = OS.getCurrentFragment();173    OS.newFragment();174  }175 176  OS.emitValueToAlignment(Align(4), 0);177 178  OS.emitLabel(StringEnd);179}180 181void CodeViewContext::emitFileChecksums(MCObjectStreamer &OS) {182  // Do nothing if there are no file checksums. Microsoft's linker rejects empty183  // CodeView substreams.184  if (Files.empty())185    return;186 187  MCContext &Ctx = OS.getContext();188  MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false),189           *FileEnd = Ctx.createTempSymbol("filechecksums_end", false);190 191  OS.emitInt32(uint32_t(DebugSubsectionKind::FileChecksums));192  OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4);193  OS.emitLabel(FileBegin);194 195  unsigned CurrentOffset = 0;196 197  // Emit an array of FileChecksum entries. We index into this table using the198  // user-provided file number.  Each entry may be a variable number of bytes199  // determined by the checksum kind and size.200  for (auto File : Files) {201    OS.emitAssignment(File.ChecksumTableOffset,202                      MCConstantExpr::create(CurrentOffset, Ctx));203    CurrentOffset += 4; // String table offset.204    if (!File.ChecksumKind) {205      CurrentOffset +=206          4; // One byte each for checksum size and kind, then align to 4 bytes.207    } else {208      CurrentOffset += 2; // One byte each for checksum size and kind.209      CurrentOffset += File.Checksum.size();210      CurrentOffset = alignTo(CurrentOffset, 4);211    }212 213    OS.emitInt32(File.StringTableOffset);214 215    if (!File.ChecksumKind) {216      // There is no checksum.  Therefore zero the next two fields and align217      // back to 4 bytes.218      OS.emitInt32(0);219      continue;220    }221    OS.emitInt8(static_cast<uint8_t>(File.Checksum.size()));222    OS.emitInt8(File.ChecksumKind);223    OS.emitBytes(toStringRef(File.Checksum));224    OS.emitValueToAlignment(Align(4));225  }226 227  OS.emitLabel(FileEnd);228 229  ChecksumOffsetsAssigned = true;230}231 232// Output checksum table offset of the given file number.  It is possible that233// not all files have been registered yet, and so the offset cannot be234// calculated.  In this case a symbol representing the offset is emitted, and235// the value of this symbol will be fixed up at a later time.236void CodeViewContext::emitFileChecksumOffset(MCObjectStreamer &OS,237                                             unsigned FileNo) {238  unsigned Idx = FileNo - 1;239 240  if (Idx >= Files.size())241    Files.resize(Idx + 1);242 243  if (ChecksumOffsetsAssigned) {244    OS.emitSymbolValue(Files[Idx].ChecksumTableOffset, 4);245    return;246  }247 248  const MCSymbolRefExpr *SRE =249      MCSymbolRefExpr::create(Files[Idx].ChecksumTableOffset, OS.getContext());250 251  OS.emitValueImpl(SRE, 4);252}253 254void CodeViewContext::addLineEntry(const MCCVLoc &LineEntry) {255  size_t Offset = MCCVLines.size();256  auto I = MCCVLineStartStop.insert(257      {LineEntry.getFunctionId(), {Offset, Offset + 1}});258  if (!I.second)259    I.first->second.second = Offset + 1;260  MCCVLines.push_back(LineEntry);261}262 263std::vector<MCCVLoc>264CodeViewContext::getFunctionLineEntries(unsigned FuncId) {265  std::vector<MCCVLoc> FilteredLines;266  size_t LocBegin;267  size_t LocEnd;268  std::tie(LocBegin, LocEnd) = getLineExtentIncludingInlinees(FuncId);269  if (LocBegin >= LocEnd) {270    return FilteredLines;271  }272 273  MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(FuncId);274  for (size_t Idx = LocBegin; Idx != LocEnd; ++Idx) {275    unsigned LocationFuncId = MCCVLines[Idx].getFunctionId();276    if (LocationFuncId == FuncId) {277      // This was a .cv_loc directly for FuncId, so record it.278      FilteredLines.push_back(MCCVLines[Idx]);279    } else {280      // Check if the current location is inlined in this function. If it is,281      // synthesize a statement .cv_loc at the original inlined call site.282      auto I = SiteInfo->InlinedAtMap.find(LocationFuncId);283      if (I != SiteInfo->InlinedAtMap.end()) {284        MCCVFunctionInfo::LineInfo &IA = I->second;285        // Only add the location if it differs from the previous location.286        // Large inlined calls will have many .cv_loc entries and we only need287        // one line table entry in the parent function.288        if (FilteredLines.empty() ||289            FilteredLines.back().getFileNum() != IA.File ||290            FilteredLines.back().getLine() != IA.Line ||291            FilteredLines.back().getColumn() != IA.Col) {292          FilteredLines.push_back(MCCVLoc(MCCVLines[Idx].getLabel(), FuncId,293                                          IA.File, IA.Line, IA.Col, false,294                                          false));295        }296      }297    }298  }299  return FilteredLines;300}301 302std::pair<size_t, size_t> CodeViewContext::getLineExtent(unsigned FuncId) {303  auto I = MCCVLineStartStop.find(FuncId);304  // Return an empty extent if there are no cv_locs for this function id.305  if (I == MCCVLineStartStop.end())306    return {~0ULL, 0};307  return I->second;308}309 310std::pair<size_t, size_t>311CodeViewContext::getLineExtentIncludingInlinees(unsigned FuncId) {312  size_t LocBegin;313  size_t LocEnd;314  std::tie(LocBegin, LocEnd) = getLineExtent(FuncId);315 316  // Include all child inline call sites in our extent.317  MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(FuncId);318  if (SiteInfo) {319    for (auto &KV : SiteInfo->InlinedAtMap) {320      unsigned ChildId = KV.first;321      auto Extent = getLineExtent(ChildId);322      LocBegin = std::min(LocBegin, Extent.first);323      LocEnd = std::max(LocEnd, Extent.second);324    }325  }326 327  return {LocBegin, LocEnd};328}329 330ArrayRef<MCCVLoc> CodeViewContext::getLinesForExtent(size_t L, size_t R) {331  if (R <= L)332    return {};333  if (L >= MCCVLines.size())334    return {};335  return ArrayRef(&MCCVLines[L], R - L);336}337 338void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS,339                                               unsigned FuncId,340                                               const MCSymbol *FuncBegin,341                                               const MCSymbol *FuncEnd) {342  MCContext &Ctx = OS.getContext();343  MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false),344           *LineEnd = Ctx.createTempSymbol("linetable_end", false);345 346  OS.emitInt32(uint32_t(DebugSubsectionKind::Lines));347  OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4);348  OS.emitLabel(LineBegin);349  OS.emitCOFFSecRel32(FuncBegin, /*Offset=*/0);350  OS.emitCOFFSectionIndex(FuncBegin);351 352  // Actual line info.353  std::vector<MCCVLoc> Locs = getFunctionLineEntries(FuncId);354  bool HaveColumns = any_of(Locs, [](const MCCVLoc &LineEntry) {355    return LineEntry.getColumn() != 0;356  });357  OS.emitInt16(HaveColumns ? int(LF_HaveColumns) : 0);358  OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4);359 360  for (auto I = Locs.begin(), E = Locs.end(); I != E;) {361    // Emit a file segment for the run of locations that share a file id.362    unsigned CurFileNum = I->getFileNum();363    auto FileSegEnd =364        std::find_if(I, E, [CurFileNum](const MCCVLoc &Loc) {365          return Loc.getFileNum() != CurFileNum;366        });367    unsigned EntryCount = FileSegEnd - I;368    OS.AddComment("Segment for file '" +369                  Twine(StrTab[Files[CurFileNum - 1].StringTableOffset]) +370                  "' begins");371    OS.emitCVFileChecksumOffsetDirective(CurFileNum);372    OS.emitInt32(EntryCount);373    uint32_t SegmentSize = 12;374    SegmentSize += 8 * EntryCount;375    if (HaveColumns)376      SegmentSize += 4 * EntryCount;377    OS.emitInt32(SegmentSize);378 379    for (auto J = I; J != FileSegEnd; ++J) {380      OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4);381      unsigned LineData = J->getLine();382      if (J->isStmt())383        LineData |= LineInfo::StatementFlag;384      OS.emitInt32(LineData);385    }386    if (HaveColumns) {387      for (auto J = I; J != FileSegEnd; ++J) {388        OS.emitInt16(J->getColumn());389        OS.emitInt16(0);390      }391    }392    I = FileSegEnd;393  }394  OS.emitLabel(LineEnd);395}396 397static bool compressAnnotation(uint32_t Data, SmallVectorImpl<char> &Buffer) {398  if (isUInt<7>(Data)) {399    Buffer.push_back(Data);400    return true;401  }402 403  if (isUInt<14>(Data)) {404    Buffer.push_back((Data >> 8) | 0x80);405    Buffer.push_back(Data & 0xff);406    return true;407  }408 409  if (isUInt<29>(Data)) {410    Buffer.push_back((Data >> 24) | 0xC0);411    Buffer.push_back((Data >> 16) & 0xff);412    Buffer.push_back((Data >> 8) & 0xff);413    Buffer.push_back(Data & 0xff);414    return true;415  }416 417  return false;418}419 420static bool compressAnnotation(BinaryAnnotationsOpCode Annotation,421                               SmallVectorImpl<char> &Buffer) {422  return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer);423}424 425static uint32_t encodeSignedNumber(uint32_t Data) {426  if (Data >> 31)427    return ((-Data) << 1) | 1;428  return Data << 1;429}430 431void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS,432                                                     unsigned PrimaryFunctionId,433                                                     unsigned SourceFileId,434                                                     unsigned SourceLineNum,435                                                     const MCSymbol *FnStartSym,436                                                     const MCSymbol *FnEndSym) {437  // Create and insert a fragment into the current section that will be encoded438  // later.439  OS.newSpecialFragment<MCCVInlineLineTableFragment>(440      PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);441}442 443void CodeViewContext::emitDefRange(444    MCObjectStreamer &OS,445    ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,446    StringRef FixedSizePortion) {447  // Store `Ranges` and `FixedSizePortion` in the context, returning references,448  // as MCCVDefRangeFragment does not own these objects.449  FixedSizePortion = MCCtx->allocateString(FixedSizePortion);450  auto &Saved = DefRangeStorage.emplace_back(Ranges.begin(), Ranges.end());451  // Create and insert a fragment into the current section that will be encoded452  // later.453  OS.newSpecialFragment<MCCVDefRangeFragment>(Saved, FixedSizePortion);454}455 456static unsigned computeLabelDiff(const MCAssembler &Asm, const MCSymbol *Begin,457                                 const MCSymbol *End) {458  MCContext &Ctx = Asm.getContext();459  const MCExpr *BeginRef = MCSymbolRefExpr::create(Begin, Ctx),460               *EndRef = MCSymbolRefExpr::create(End, Ctx);461  const MCExpr *AddrDelta =462      MCBinaryExpr::create(MCBinaryExpr::Sub, EndRef, BeginRef, Ctx);463  int64_t Result;464  bool Success = AddrDelta->evaluateKnownAbsolute(Result, Asm);465  assert(Success && "failed to evaluate label difference as absolute");466  (void)Success;467  assert(Result >= 0 && "negative label difference requested");468  assert(Result < UINT_MAX && "label difference greater than 2GB");469  return unsigned(Result);470}471 472void CodeViewContext::encodeInlineLineTable(const MCAssembler &Asm,473                                            MCCVInlineLineTableFragment &Frag) {474  size_t LocBegin;475  size_t LocEnd;476  std::tie(LocBegin, LocEnd) = getLineExtentIncludingInlinees(Frag.SiteFuncId);477 478  if (LocBegin >= LocEnd)479    return;480  ArrayRef<MCCVLoc> Locs = getLinesForExtent(LocBegin, LocEnd);481  if (Locs.empty())482    return;483 484  // Check that the locations are all in the same section.485#ifndef NDEBUG486  const MCSection *FirstSec = &Locs.front().getLabel()->getSection();487  for (const MCCVLoc &Loc : Locs) {488    if (&Loc.getLabel()->getSection() != FirstSec) {489      errs() << ".cv_loc " << Loc.getFunctionId() << ' ' << Loc.getFileNum()490             << ' ' << Loc.getLine() << ' ' << Loc.getColumn()491             << " is in the wrong section\n";492      llvm_unreachable(".cv_loc crosses sections");493    }494  }495#endif496 497  // Make an artificial start location using the function start and the inlinee498  // lines start location information. All deltas start relative to this499  // location.500  MCCVLoc StartLoc = Locs.front();501  StartLoc.setLabel(Frag.getFnStartSym());502  StartLoc.setFileNum(Frag.StartFileId);503  StartLoc.setLine(Frag.StartLineNum);504  bool HaveOpenRange = false;505 506  const MCSymbol *LastLabel = Frag.getFnStartSym();507  MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc;508  LastSourceLoc.File = Frag.StartFileId;509  LastSourceLoc.Line = Frag.StartLineNum;510 511  MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(Frag.SiteFuncId);512 513  SmallVector<char, 0> Buffer;514  for (const MCCVLoc &Loc : Locs) {515    // Exit early if our line table would produce an oversized InlineSiteSym516    // record. Account for the ChangeCodeLength annotation emitted after the517    // loop ends.518    constexpr uint32_t InlineSiteSize = 12;519    constexpr uint32_t AnnotationSize = 8;520    size_t MaxBufferSize = MaxRecordLength - InlineSiteSize - AnnotationSize;521    if (Buffer.size() >= MaxBufferSize)522      break;523 524    if (Loc.getFunctionId() == Frag.SiteFuncId) {525      CurSourceLoc.File = Loc.getFileNum();526      CurSourceLoc.Line = Loc.getLine();527    } else {528      auto I = SiteInfo->InlinedAtMap.find(Loc.getFunctionId());529      if (I != SiteInfo->InlinedAtMap.end()) {530        // This .cv_loc is from a child inline call site. Use the source531        // location of the inlined call site instead of the .cv_loc directive532        // source location.533        CurSourceLoc = I->second;534      } else {535        // We've hit a cv_loc not attributed to this inline call site. Use this536        // label to end the PC range.537        if (HaveOpenRange) {538          unsigned Length = computeLabelDiff(Asm, LastLabel, Loc.getLabel());539          compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);540          compressAnnotation(Length, Buffer);541          LastLabel = Loc.getLabel();542        }543        HaveOpenRange = false;544        continue;545      }546    }547 548    // Skip this .cv_loc if we have an open range and this isn't a meaningful549    // source location update. The current table format does not support column550    // info, so we can skip updates for those.551    if (HaveOpenRange && CurSourceLoc.File == LastSourceLoc.File &&552        CurSourceLoc.Line == LastSourceLoc.Line)553      continue;554 555    HaveOpenRange = true;556 557    if (CurSourceLoc.File != LastSourceLoc.File) {558      unsigned FileOffset = static_cast<const MCConstantExpr *>(559                                Files[CurSourceLoc.File - 1]560                                    .ChecksumTableOffset->getVariableValue())561                                ->getValue();562      compressAnnotation(BinaryAnnotationsOpCode::ChangeFile, Buffer);563      compressAnnotation(FileOffset, Buffer);564    }565 566    int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line;567    unsigned EncodedLineDelta = encodeSignedNumber(LineDelta);568    unsigned CodeDelta = computeLabelDiff(Asm, LastLabel, Loc.getLabel());569    if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) {570      // The ChangeCodeOffsetAndLineOffset combination opcode is used when the571      // encoded line delta uses 3 or fewer set bits and the code offset fits572      // in one nibble.573      unsigned Operand = (EncodedLineDelta << 4) | CodeDelta;574      compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset,575                         Buffer);576      compressAnnotation(Operand, Buffer);577    } else {578      // Otherwise use the separate line and code deltas.579      if (LineDelta != 0) {580        compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);581        compressAnnotation(EncodedLineDelta, Buffer);582      }583      compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffset, Buffer);584      compressAnnotation(CodeDelta, Buffer);585    }586 587    LastLabel = Loc.getLabel();588    LastSourceLoc = CurSourceLoc;589  }590 591  assert(HaveOpenRange);592 593  unsigned EndSymLength =594      computeLabelDiff(Asm, 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(Asm, LastLabel, Loc.getLabel());602  }603 604  compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);605  compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer);606  Frag.setVarContents(Buffer);607}608 609void CodeViewContext::encodeDefRange(const MCAssembler &Asm,610                                     MCCVDefRangeFragment &Frag) {611  MCContext &Ctx = Asm.getContext();612  SmallVector<char, 0> Contents;613  SmallVector<MCFixup, 0> Fixups;614  raw_svector_ostream OS(Contents);615 616  // Compute all the sizes up front.617  SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes;618  const MCSymbol *LastLabel = nullptr;619  for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) {620    unsigned GapSize =621        LastLabel ? computeLabelDiff(Asm, LastLabel, Range.first) : 0;622    unsigned RangeSize = computeLabelDiff(Asm, Range.first, Range.second);623    GapAndRangeSizes.push_back({GapSize, RangeSize});624    LastLabel = Range.second;625  }626 627  // Write down each range where the variable is defined.628  for (size_t I = 0, E = Frag.getRanges().size(); I != E;) {629    // If the range size of multiple consecutive ranges is under the max,630    // combine the ranges and emit some gaps.631    const MCSymbol *RangeBegin = Frag.getRanges()[I].first;632    unsigned RangeSize = GapAndRangeSizes[I].second;633    size_t J = I + 1;634    for (; J != E; ++J) {635      unsigned GapAndRangeSize = GapAndRangeSizes[J].first + GapAndRangeSizes[J].second;636      if (RangeSize + GapAndRangeSize > MaxDefRange)637        break;638      RangeSize += GapAndRangeSize;639    }640    unsigned NumGaps = J - I - 1;641 642    support::endian::Writer LEWriter(OS, llvm::endianness::little);643 644    unsigned Bias = 0;645    // We must split the range into chunks of MaxDefRange, this is a fundamental646    // limitation of the file format.647    do {648      uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize);649 650      const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx);651      const MCBinaryExpr *BE =652          MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx);653 654      // Each record begins with a 2-byte number indicating how large the record655      // is.656      StringRef FixedSizePortion = Frag.getFixedSizePortion();657      // Our record is a fixed sized prefix and a LocalVariableAddrRange that we658      // are artificially constructing.659      size_t RecordSize = FixedSizePortion.size() +660                          sizeof(LocalVariableAddrRange) + 4 * NumGaps;661      // Write out the record size.662      LEWriter.write<uint16_t>(RecordSize);663      // Write out the fixed size prefix.664      OS << FixedSizePortion;665      // Make space for a fixup that will eventually have a section relative666      // relocation pointing at the offset where the variable becomes live.667      Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4));668      LEWriter.write<uint32_t>(0); // Fixup for code start.669      // Make space for a fixup that will record the section index for the code.670      Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2));671      LEWriter.write<uint16_t>(0); // Fixup for section index.672      // Write down the range's extent.673      LEWriter.write<uint16_t>(Chunk);674 675      // Move on to the next range.676      Bias += Chunk;677      RangeSize -= Chunk;678    } while (RangeSize > 0);679 680    // Emit the gaps afterwards.681    assert((NumGaps == 0 || Bias <= MaxDefRange) &&682           "large ranges should not have gaps");683    unsigned GapStartOffset = GapAndRangeSizes[I].second;684    for (++I; I != J; ++I) {685      unsigned GapSize, RangeSize;686      assert(I < GapAndRangeSizes.size());687      std::tie(GapSize, RangeSize) = GapAndRangeSizes[I];688      LEWriter.write<uint16_t>(GapStartOffset);689      LEWriter.write<uint16_t>(GapSize);690      GapStartOffset += GapSize + RangeSize;691    }692  }693 694  Frag.setVarContents(Contents);695  assert(Fixups.size() < 256 && "Store fixups outside of MCFragment's VarFixup "696                                "storage if the number ever exceeds 256");697  Frag.setVarFixups(Fixups);698}699