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