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