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"
15#include "llvm/MC/MCAsmLayout.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/DebugInfo/CodeView/CodeView.h"
18#include "llvm/DebugInfo/CodeView/Line.h"
19#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCObjectStreamer.h"
22#include "llvm/MC/MCValue.h"
23#include "llvm/Support/COFF.h"
24#include "llvm/Support/EndianStream.h"
25
26using namespace llvm;
27using namespace llvm::codeview;
28
29CodeViewContext::CodeViewContext() {}
30
31CodeViewContext::~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.
40bool 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
47bool 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
68MCDataFragment *CodeViewContext::getStringTableFragment() {
69  if (!StrTabFragment) {
70    StrTabFragment = new MCDataFragment();
71    // Start a new string table out with a null byte.
72    StrTabFragment->getContents().push_back('\0');
73  }
74  return StrTabFragment;
75}
76
77StringRef CodeViewContext::addToStringTable(StringRef S) {
78  SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents();
79  auto Insertion =
80      StringTable.insert(std::make_pair(S, unsigned(Contents.size())));
81  // Return the string from the table, since it is stable.
82  S = Insertion.first->first();
83  if (Insertion.second) {
84    // The string map key is always null terminated.
85    Contents.append(S.begin(), S.end() + 1);
86  }
87  return S;
88}
89
90unsigned CodeViewContext::getStringTableOffset(StringRef S) {
91  // A string table offset of zero is always the empty string.
92  if (S.empty())
93    return 0;
94  auto I = StringTable.find(S);
95  assert(I != StringTable.end());
96  return I->second;
97}
98
99void CodeViewContext::emitStringTable(MCObjectStreamer &OS) {
100  MCContext &Ctx = OS.getContext();
101  MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false),
102           *StringEnd = Ctx.createTempSymbol("strtab_end", false);
103
104  OS.EmitIntValue(unsigned(ModuleSubstreamKind::StringTable), 4);
105  OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4);
106  OS.EmitLabel(StringBegin);
107
108  // Put the string table data fragment here, if we haven't already put it
109  // somewhere else. If somebody wants two string tables in their .s file, one
110  // will just be empty.
111  if (!InsertedStrTabFragment) {
112    OS.insert(getStringTableFragment());
113    InsertedStrTabFragment = true;
114  }
115
116  OS.EmitValueToAlignment(4, 0);
117
118  OS.EmitLabel(StringEnd);
119}
120
121void CodeViewContext::emitFileChecksums(MCObjectStreamer &OS) {
122  // Do nothing if there are no file checksums. Microsoft's linker rejects empty
123  // CodeView substreams.
124  if (Filenames.empty())
125    return;
126
127  MCContext &Ctx = OS.getContext();
128  MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false),
129           *FileEnd = Ctx.createTempSymbol("filechecksums_end", false);
130
131  OS.EmitIntValue(unsigned(ModuleSubstreamKind::FileChecksums), 4);
132  OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4);
133  OS.EmitLabel(FileBegin);
134
135  // Emit an array of FileChecksum entries. We index into this table using the
136  // user-provided file number. Each entry is currently 8 bytes, as we don't
137  // emit checksums.
138  for (StringRef Filename : Filenames) {
139    OS.EmitIntValue(getStringTableOffset(Filename), 4);
140    // Zero the next two fields and align back to 4 bytes. This indicates that
141    // no checksum is present.
142    OS.EmitIntValue(0, 4);
143  }
144
145  OS.EmitLabel(FileEnd);
146}
147
148void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS,
149                                               unsigned FuncId,
150                                               const MCSymbol *FuncBegin,
151                                               const MCSymbol *FuncEnd) {
152  MCContext &Ctx = OS.getContext();
153  MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false),
154           *LineEnd = Ctx.createTempSymbol("linetable_end", false);
155
156  OS.EmitIntValue(unsigned(ModuleSubstreamKind::Lines), 4);
157  OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4);
158  OS.EmitLabel(LineBegin);
159  OS.EmitCOFFSecRel32(FuncBegin);
160  OS.EmitCOFFSectionIndex(FuncBegin);
161
162  // Actual line info.
163  std::vector<MCCVLineEntry> Locs = getFunctionLineEntries(FuncId);
164  bool HaveColumns = any_of(Locs, [](const MCCVLineEntry &LineEntry) {
165    return LineEntry.getColumn() != 0;
166  });
167  OS.EmitIntValue(HaveColumns ? int(LineFlags::HaveColumns) : 0, 2);
168  OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4);
169
170  for (auto I = Locs.begin(), E = Locs.end(); I != E;) {
171    // Emit a file segment for the run of locations that share a file id.
172    unsigned CurFileNum = I->getFileNum();
173    auto FileSegEnd =
174        std::find_if(I, E, [CurFileNum](const MCCVLineEntry &Loc) {
175          return Loc.getFileNum() != CurFileNum;
176        });
177    unsigned EntryCount = FileSegEnd - I;
178    OS.AddComment("Segment for file '" + Twine(Filenames[CurFileNum - 1]) +
179                  "' begins");
180    OS.EmitIntValue(8 * (CurFileNum - 1), 4);
181    OS.EmitIntValue(EntryCount, 4);
182    uint32_t SegmentSize = 12;
183    SegmentSize += 8 * EntryCount;
184    if (HaveColumns)
185      SegmentSize += 4 * EntryCount;
186    OS.EmitIntValue(SegmentSize, 4);
187
188    for (auto J = I; J != FileSegEnd; ++J) {
189      OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4);
190      unsigned LineData = J->getLine();
191      if (J->isStmt())
192        LineData |= LineInfo::StatementFlag;
193      OS.EmitIntValue(LineData, 4);
194    }
195    if (HaveColumns) {
196      for (auto J = I; J != FileSegEnd; ++J) {
197        OS.EmitIntValue(J->getColumn(), 2);
198        OS.EmitIntValue(0, 2);
199      }
200    }
201    I = FileSegEnd;
202  }
203  OS.EmitLabel(LineEnd);
204}
205
206static bool compressAnnotation(uint32_t Data, SmallVectorImpl<char> &Buffer) {
207  if (isUInt<7>(Data)) {
208    Buffer.push_back(Data);
209    return true;
210  }
211
212  if (isUInt<14>(Data)) {
213    Buffer.push_back((Data >> 8) | 0x80);
214    Buffer.push_back(Data & 0xff);
215    return true;
216  }
217
218  if (isUInt<29>(Data)) {
219    Buffer.push_back((Data >> 24) | 0xC0);
220    Buffer.push_back((Data >> 16) & 0xff);
221    Buffer.push_back((Data >> 8) & 0xff);
222    Buffer.push_back(Data & 0xff);
223    return true;
224  }
225
226  return false;
227}
228
229static bool compressAnnotation(BinaryAnnotationsOpCode Annotation,
230                               SmallVectorImpl<char> &Buffer) {
231  return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer);
232}
233
234static uint32_t encodeSignedNumber(uint32_t Data) {
235  if (Data >> 31)
236    return ((-Data) << 1) | 1;
237  return Data << 1;
238}
239
240void CodeViewContext::emitInlineLineTableForFunction(
241    MCObjectStreamer &OS, unsigned PrimaryFunctionId, unsigned SourceFileId,
242    unsigned SourceLineNum, const MCSymbol *FnStartSym,
243    const MCSymbol *FnEndSym, ArrayRef<unsigned> SecondaryFunctionIds) {
244  // Create and insert a fragment into the current section that will be encoded
245  // later.
246  new MCCVInlineLineTableFragment(
247      PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym,
248      SecondaryFunctionIds, OS.getCurrentSectionOnly());
249}
250
251void CodeViewContext::emitDefRange(
252    MCObjectStreamer &OS,
253    ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
254    StringRef FixedSizePortion) {
255  // Create and insert a fragment into the current section that will be encoded
256  // later.
257  new MCCVDefRangeFragment(Ranges, FixedSizePortion,
258                           OS.getCurrentSectionOnly());
259}
260
261static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin,
262                                 const MCSymbol *End) {
263  MCContext &Ctx = Layout.getAssembler().getContext();
264  MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
265  const MCExpr *BeginRef = MCSymbolRefExpr::create(Begin, Variant, Ctx),
266               *EndRef = MCSymbolRefExpr::create(End, Variant, Ctx);
267  const MCExpr *AddrDelta =
268      MCBinaryExpr::create(MCBinaryExpr::Sub, EndRef, BeginRef, Ctx);
269  int64_t Result;
270  bool Success = AddrDelta->evaluateKnownAbsolute(Result, Layout);
271  assert(Success && "failed to evaluate label difference as absolute");
272  (void)Success;
273  assert(Result >= 0 && "negative label difference requested");
274  assert(Result < UINT_MAX && "label difference greater than 2GB");
275  return unsigned(Result);
276}
277
278void CodeViewContext::encodeInlineLineTable(MCAsmLayout &Layout,
279                                            MCCVInlineLineTableFragment &Frag) {
280  size_t LocBegin;
281  size_t LocEnd;
282  std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId);
283  for (unsigned SecondaryId : Frag.SecondaryFuncs) {
284    auto Extent = getLineExtent(SecondaryId);
285    LocBegin = std::min(LocBegin, Extent.first);
286    LocEnd = std::max(LocEnd, Extent.second);
287  }
288  if (LocBegin >= LocEnd)
289    return;
290  ArrayRef<MCCVLineEntry> Locs = getLinesForExtent(LocBegin, LocEnd);
291  if (Locs.empty())
292    return;
293
294  SmallSet<unsigned, 8> InlinedFuncIds;
295  InlinedFuncIds.insert(Frag.SiteFuncId);
296  InlinedFuncIds.insert(Frag.SecondaryFuncs.begin(), Frag.SecondaryFuncs.end());
297
298  // Make an artificial start location using the function start and the inlinee
299  // lines start location information. All deltas start relative to this
300  // location.
301  MCCVLineEntry StartLoc(Frag.getFnStartSym(), MCCVLoc(Locs.front()));
302  StartLoc.setFileNum(Frag.StartFileId);
303  StartLoc.setLine(Frag.StartLineNum);
304  const MCCVLineEntry *LastLoc = &StartLoc;
305  bool WithinFunction = true;
306
307  SmallVectorImpl<char> &Buffer = Frag.getContents();
308  Buffer.clear(); // Clear old contents if we went through relaxation.
309  for (const MCCVLineEntry &Loc : Locs) {
310    if (!InlinedFuncIds.count(Loc.getFunctionId())) {
311      // We've hit a cv_loc not attributed to this inline call site. Use this
312      // label to end the PC range.
313      if (WithinFunction) {
314        unsigned Length =
315            computeLabelDiff(Layout, LastLoc->getLabel(), Loc.getLabel());
316        compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
317        compressAnnotation(Length, Buffer);
318      }
319      WithinFunction = false;
320      continue;
321    }
322    WithinFunction = true;
323
324    if (Loc.getFileNum() != LastLoc->getFileNum()) {
325      // File ids are 1 based, and each file checksum table entry is 8 bytes
326      // long. See emitFileChecksums above.
327      unsigned FileOffset = 8 * (Loc.getFileNum() - 1);
328      compressAnnotation(BinaryAnnotationsOpCode::ChangeFile, Buffer);
329      compressAnnotation(FileOffset, Buffer);
330    }
331
332    int LineDelta = Loc.getLine() - LastLoc->getLine();
333    if (LineDelta == 0)
334      continue;
335
336    unsigned EncodedLineDelta = encodeSignedNumber(LineDelta);
337    unsigned CodeDelta =
338        computeLabelDiff(Layout, LastLoc->getLabel(), Loc.getLabel());
339    if (CodeDelta == 0) {
340      compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);
341      compressAnnotation(EncodedLineDelta, Buffer);
342    } else if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) {
343      // The ChangeCodeOffsetAndLineOffset combination opcode is used when the
344      // encoded line delta uses 3 or fewer set bits and the code offset fits
345      // in one nibble.
346      unsigned Operand = (EncodedLineDelta << 4) | CodeDelta;
347      compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset,
348                         Buffer);
349      compressAnnotation(Operand, Buffer);
350    } else {
351      // Otherwise use the separate line and code deltas.
352      compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);
353      compressAnnotation(EncodedLineDelta, Buffer);
354      compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffset, Buffer);
355      compressAnnotation(CodeDelta, Buffer);
356    }
357
358    LastLoc = &Loc;
359  }
360
361  assert(WithinFunction);
362
363  unsigned EndSymLength =
364      computeLabelDiff(Layout, LastLoc->getLabel(), Frag.getFnEndSym());
365  unsigned LocAfterLength = ~0U;
366  ArrayRef<MCCVLineEntry> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1);
367  if (!LocAfter.empty()) {
368    // Only try to compute this difference if we're in the same section.
369    const MCCVLineEntry &Loc = LocAfter[0];
370    if (&Loc.getLabel()->getSection(false) ==
371        &LastLoc->getLabel()->getSection(false)) {
372      LocAfterLength =
373          computeLabelDiff(Layout, LastLoc->getLabel(), Loc.getLabel());
374    }
375  }
376
377  compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
378  compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer);
379}
380
381void CodeViewContext::encodeDefRange(MCAsmLayout &Layout,
382                                     MCCVDefRangeFragment &Frag) {
383  MCContext &Ctx = Layout.getAssembler().getContext();
384  SmallVectorImpl<char> &Contents = Frag.getContents();
385  Contents.clear();
386  SmallVectorImpl<MCFixup> &Fixups = Frag.getFixups();
387  Fixups.clear();
388  raw_svector_ostream OS(Contents);
389
390  // Write down each range where the variable is defined.
391  for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) {
392    unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second);
393    unsigned Bias = 0;
394    // We must split the range into chunks of MaxDefRange, this is a fundamental
395    // limitation of the file format.
396    do {
397      uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize);
398
399      const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Range.first, Ctx);
400      const MCBinaryExpr *BE =
401          MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx);
402      MCValue Res;
403      BE->evaluateAsRelocatable(Res, &Layout, /*Fixup=*/nullptr);
404
405      // Each record begins with a 2-byte number indicating how large the record
406      // is.
407      StringRef FixedSizePortion = Frag.getFixedSizePortion();
408      // Our record is a fixed sized prefix and a LocalVariableAddrRange that we
409      // are artificially constructing.
410      size_t RecordSize =
411          FixedSizePortion.size() + sizeof(LocalVariableAddrRange);
412      // Write out the recrod size.
413      support::endian::Writer<support::little>(OS).write<uint16_t>(RecordSize);
414      // Write out the fixed size prefix.
415      OS << FixedSizePortion;
416      // Make space for a fixup that will eventually have a section relative
417      // relocation pointing at the offset where the variable becomes live.
418      Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4));
419      Contents.resize(Contents.size() + 4); // Fixup for code start.
420      // Make space for a fixup that will record the section index for the code.
421      Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2));
422      Contents.resize(Contents.size() + 2); // Fixup for section index.
423      // Write down the range's extent.
424      support::endian::Writer<support::little>(OS).write<uint16_t>(Chunk);
425
426      // Move on to the next range.
427      Bias += Chunk;
428      RangeSize -= Chunk;
429    } while (RangeSize > 0);
430  }
431}
432
433//
434// This is called when an instruction is assembled into the specified section
435// and if there is information from the last .cv_loc directive that has yet to have
436// a line entry made for it is made.
437//
438void MCCVLineEntry::Make(MCObjectStreamer *MCOS) {
439  if (!MCOS->getContext().getCVLocSeen())
440    return;
441
442  // Create a symbol at in the current section for use in the line entry.
443  MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
444  // Set the value of the symbol to use for the MCCVLineEntry.
445  MCOS->EmitLabel(LineSym);
446
447  // Get the current .loc info saved in the context.
448  const MCCVLoc &CVLoc = MCOS->getContext().getCurrentCVLoc();
449
450  // Create a (local) line entry with the symbol and the current .loc info.
451  MCCVLineEntry LineEntry(LineSym, CVLoc);
452
453  // clear CVLocSeen saying the current .loc info is now used.
454  MCOS->getContext().clearCVLocSeen();
455
456  // Add the line entry to this section's entries.
457  MCOS->getContext().getCVContext().addLineEntry(LineEntry);
458}
459