blob: 7849196432b89ec0c682a25e8c143125b9cae175 [file] [log] [blame]
Reid Kleckner2214ed82016-01-29 00:49:42 +00001//===- MCCodeView.h - Machine Code CodeView support -------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Reid Kleckner2214ed82016-01-29 00:49:42 +00006//
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"
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000015#include "llvm/ADT/StringExtras.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000016#include "llvm/DebugInfo/CodeView/CodeView.h"
17#include "llvm/DebugInfo/CodeView/Line.h"
David Majnemer6fcbd7e2016-01-29 19:24:12 +000018#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turnerd4273832017-05-30 21:53:05 +000019#include "llvm/MC/MCAsmLayout.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000020#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCObjectStreamer.h"
David Majnemer408b5e62016-02-05 01:55:49 +000022#include "llvm/MC/MCValue.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000023#include "llvm/Support/EndianStream.h"
Reid Kleckner2214ed82016-01-29 00:49:42 +000024
25using namespace llvm;
26using namespace llvm::codeview;
27
28CodeViewContext::CodeViewContext() {}
29
30CodeViewContext::~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.
39bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const {
40 unsigned Idx = FileNumber - 1;
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000041 if (Idx < Files.size())
42 return Files[Idx].Assigned;
Reid Kleckner2214ed82016-01-29 00:49:42 +000043 return false;
44}
45
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000046bool CodeViewContext::addFile(MCStreamer &OS, unsigned FileNumber,
47 StringRef Filename,
48 ArrayRef<uint8_t> ChecksumBytes,
49 uint8_t ChecksumKind) {
Reid Kleckner2214ed82016-01-29 00:49:42 +000050 assert(FileNumber > 0);
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000051 auto FilenameOffset = addToStringTable(Filename);
52 Filename = FilenameOffset.first;
Reid Kleckner2214ed82016-01-29 00:49:42 +000053 unsigned Idx = FileNumber - 1;
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000054 if (Idx >= Files.size())
55 Files.resize(Idx + 1);
Reid Kleckner2214ed82016-01-29 00:49:42 +000056
57 if (Filename.empty())
58 Filename = "<stdin>";
59
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000060 if (Files[Idx].Assigned)
Reid Kleckner2214ed82016-01-29 00:49:42 +000061 return false;
62
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000063 FilenameOffset = addToStringTable(Filename);
64 Filename = FilenameOffset.first;
65 unsigned Offset = FilenameOffset.second;
Reid Kleckner2214ed82016-01-29 00:49:42 +000066
Reid Kleckner26fa1bf2017-09-19 18:14:45 +000067 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
Reid Kleckner2214ed82016-01-29 00:49:42 +000075 return true;
76}
77
Reid Klecknerb5258722018-01-18 22:55:14 +000078MCCVFunctionInfo *CodeViewContext::getCVFunctionInfo(unsigned FuncId) {
79 if (FuncId >= Functions.size())
80 return nullptr;
81 if (Functions[FuncId].isUnallocatedFunctionInfo())
82 return nullptr;
83 return &Functions[FuncId];
84}
85
Reid Klecknera9f4cc92016-09-07 16:15:31 +000086bool CodeViewContext::recordFunctionId(unsigned FuncId) {
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
99bool 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
Reid Kleckner689f7732018-08-28 23:25:59 +0000130void CodeViewContext::recordCVLoc(MCContext &Ctx, const MCSymbol *Label,
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
Reid Kleckner2214ed82016-01-29 00:49:42 +0000138MCDataFragment *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
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000147std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {
Reid Kleckner2214ed82016-01-29 00:49:42 +0000148 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.
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000152 std::pair<StringRef, unsigned> Ret =
153 std::make_pair(Insertion.first->first(), Insertion.first->second);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000154 if (Insertion.second) {
155 // The string map key is always null terminated.
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000156 Contents.append(Ret.first.begin(), Ret.first.end() + 1);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000157 }
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000158 return Ret;
Reid Kleckner2214ed82016-01-29 00:49:42 +0000159}
160
161unsigned 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
170void CodeViewContext::emitStringTable(MCObjectStreamer &OS) {
171 MCContext &Ctx = OS.getContext();
David Blaikiea0b44ef2016-01-29 02:23:13 +0000172 MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false),
173 *StringEnd = Ctx.createTempSymbol("strtab_end", false);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000174
Fangrui Song692e0c92020-02-29 08:25:22 -0800175 OS.emitInt32(uint32_t(DebugSubsectionKind::StringTable));
Reid Kleckner2214ed82016-01-29 00:49:42 +0000176 OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4);
Fangrui Song6d2d5892020-02-14 19:21:58 -0800177 OS.emitLabel(StringBegin);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000178
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
Fangrui Song6d2d5892020-02-14 19:21:58 -0800187 OS.emitValueToAlignment(4, 0);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000188
Fangrui Song6d2d5892020-02-14 19:21:58 -0800189 OS.emitLabel(StringEnd);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000190}
191
192void CodeViewContext::emitFileChecksums(MCObjectStreamer &OS) {
Reid Kleckneree641c22016-06-08 17:50:29 +0000193 // Do nothing if there are no file checksums. Microsoft's linker rejects empty
194 // CodeView substreams.
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000195 if (Files.empty())
Reid Kleckneree641c22016-06-08 17:50:29 +0000196 return;
197
Reid Kleckner2214ed82016-01-29 00:49:42 +0000198 MCContext &Ctx = OS.getContext();
David Blaikiea0b44ef2016-01-29 02:23:13 +0000199 MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false),
200 *FileEnd = Ctx.createTempSymbol("filechecksums_end", false);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000201
Fangrui Song692e0c92020-02-29 08:25:22 -0800202 OS.emitInt32(uint32_t(DebugSubsectionKind::FileChecksums));
Reid Kleckner2214ed82016-01-29 00:49:42 +0000203 OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4);
Fangrui Song6d2d5892020-02-14 19:21:58 -0800204 OS.emitLabel(FileBegin);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000205
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000206 unsigned CurrentOffset = 0;
207
Reid Kleckner2214ed82016-01-29 00:49:42 +0000208 // Emit an array of FileChecksum entries. We index into this table using the
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000209 // 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) {
Fangrui Songa55daa12020-02-14 18:16:24 -0800212 OS.emitAssignment(File.ChecksumTableOffset,
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000213 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
Fangrui Song692e0c92020-02-29 08:25:22 -0800224 OS.emitInt32(File.StringTableOffset);
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000225
226 if (!File.ChecksumKind) {
227 // There is no checksum. Therefore zero the next two fields and align
228 // back to 4 bytes.
Fangrui Song692e0c92020-02-29 08:25:22 -0800229 OS.emitInt32(0);
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000230 continue;
231 }
Fangrui Song692e0c92020-02-29 08:25:22 -0800232 OS.emitInt8(static_cast<uint8_t>(File.Checksum.size()));
233 OS.emitInt8(File.ChecksumKind);
Fangrui Songa55daa12020-02-14 18:16:24 -0800234 OS.emitBytes(toStringRef(File.Checksum));
Fangrui Song6d2d5892020-02-14 19:21:58 -0800235 OS.emitValueToAlignment(4);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000236 }
237
Fangrui Song6d2d5892020-02-14 19:21:58 -0800238 OS.emitLabel(FileEnd);
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000239
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.
247void CodeViewContext::emitFileChecksumOffset(MCObjectStreamer &OS,
248 unsigned FileNo) {
249 unsigned Idx = FileNo - 1;
250
251 if (Idx >= Files.size())
252 Files.resize(Idx + 1);
253
254 if (ChecksumOffsetsAssigned) {
Fangrui Song6d2d5892020-02-14 19:21:58 -0800255 OS.emitSymbolValue(Files[Idx].ChecksumTableOffset, 4);
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000256 return;
257 }
258
259 const MCSymbolRefExpr *SRE =
260 MCSymbolRefExpr::create(Files[Idx].ChecksumTableOffset, OS.getContext());
261
Fangrui Song6d2d5892020-02-14 19:21:58 -0800262 OS.emitValueImpl(SRE, 4);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000263}
264
Reid Kleckner689f7732018-08-28 23:25:59 +0000265void CodeViewContext::addLineEntry(const MCCVLoc &LineEntry) {
Reid Klecknerb5258722018-01-18 22:55:14 +0000266 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
Reid Kleckner689f7732018-08-28 23:25:59 +0000274std::vector<MCCVLoc>
Reid Klecknerb5258722018-01-18 22:55:14 +0000275CodeViewContext::getFunctionLineEntries(unsigned FuncId) {
Reid Kleckner689f7732018-08-28 23:25:59 +0000276 std::vector<MCCVLoc> FilteredLines;
Reid Klecknerb5258722018-01-18 22:55:14 +0000277 auto I = MCCVLineStartStop.find(FuncId);
Reid Kleckner7897a782018-01-18 22:55:43 +0000278 if (I != MCCVLineStartStop.end()) {
279 MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(FuncId);
Reid Klecknerb5258722018-01-18 22:55:14 +0000280 for (size_t Idx = I->second.first, End = I->second.second; Idx != End;
Reid Kleckner7897a782018-01-18 22:55:43 +0000281 ++Idx) {
282 unsigned LocationFuncId = MCCVLines[Idx].getFunctionId();
283 if (LocationFuncId == FuncId) {
284 // This was a .cv_loc directly for FuncId, so record it.
Reid Klecknerb5258722018-01-18 22:55:14 +0000285 FilteredLines.push_back(MCCVLines[Idx]);
Reid Kleckner7897a782018-01-18 22:55:43 +0000286 } 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) {
Reid Kleckner689f7732018-08-28 23:25:59 +0000299 FilteredLines.push_back(MCCVLoc(
Reid Kleckner7897a782018-01-18 22:55:43 +0000300 MCCVLines[Idx].getLabel(),
Reid Kleckner689f7732018-08-28 23:25:59 +0000301 FuncId, IA.File, IA.Line, IA.Col, false, false));
Reid Kleckner7897a782018-01-18 22:55:43 +0000302 }
303 }
304 }
305 }
306 }
Reid Klecknerb5258722018-01-18 22:55:14 +0000307 return FilteredLines;
308}
309
310std::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
Reid Kleckner689f7732018-08-28 23:25:59 +0000318ArrayRef<MCCVLoc> CodeViewContext::getLinesForExtent(size_t L, size_t R) {
Reid Klecknerb5258722018-01-18 22:55:14 +0000319 if (R <= L)
320 return None;
321 if (L >= MCCVLines.size())
322 return None;
323 return makeArrayRef(&MCCVLines[L], R - L);
324}
325
Reid Kleckner2214ed82016-01-29 00:49:42 +0000326void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS,
327 unsigned FuncId,
328 const MCSymbol *FuncBegin,
329 const MCSymbol *FuncEnd) {
330 MCContext &Ctx = OS.getContext();
David Blaikiea0b44ef2016-01-29 02:23:13 +0000331 MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false),
332 *LineEnd = Ctx.createTempSymbol("linetable_end", false);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000333
Fangrui Song692e0c92020-02-29 08:25:22 -0800334 OS.emitInt32(uint32_t(DebugSubsectionKind::Lines));
Reid Kleckner2214ed82016-01-29 00:49:42 +0000335 OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4);
Fangrui Song6d2d5892020-02-14 19:21:58 -0800336 OS.emitLabel(LineBegin);
Keno Fischerf7d84ee2017-01-02 03:00:19 +0000337 OS.EmitCOFFSecRel32(FuncBegin, /*Offset=*/0);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000338 OS.EmitCOFFSectionIndex(FuncBegin);
339
340 // Actual line info.
Reid Kleckner689f7732018-08-28 23:25:59 +0000341 std::vector<MCCVLoc> Locs = getFunctionLineEntries(FuncId);
342 bool HaveColumns = any_of(Locs, [](const MCCVLoc &LineEntry) {
Reid Kleckner2214ed82016-01-29 00:49:42 +0000343 return LineEntry.getColumn() != 0;
344 });
Fangrui Song692e0c92020-02-29 08:25:22 -0800345 OS.emitInt16(HaveColumns ? int(LF_HaveColumns) : 0);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000346 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 =
Reid Kleckner689f7732018-08-28 23:25:59 +0000352 std::find_if(I, E, [CurFileNum](const MCCVLoc &Loc) {
Reid Kleckner2214ed82016-01-29 00:49:42 +0000353 return Loc.getFileNum() != CurFileNum;
354 });
355 unsigned EntryCount = FileSegEnd - I;
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000356 OS.AddComment(
357 "Segment for file '" +
358 Twine(getStringTableFragment()
359 ->getContents()[Files[CurFileNum - 1].StringTableOffset]) +
360 "' begins");
Shengchen Kanc0313782020-04-20 19:28:13 -0700361 OS.emitCVFileChecksumOffsetDirective(CurFileNum);
Fangrui Song692e0c92020-02-29 08:25:22 -0800362 OS.emitInt32(EntryCount);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000363 uint32_t SegmentSize = 12;
364 SegmentSize += 8 * EntryCount;
365 if (HaveColumns)
366 SegmentSize += 4 * EntryCount;
Fangrui Song692e0c92020-02-29 08:25:22 -0800367 OS.emitInt32(SegmentSize);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000368
369 for (auto J = I; J != FileSegEnd; ++J) {
370 OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4);
371 unsigned LineData = J->getLine();
372 if (J->isStmt())
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000373 LineData |= LineInfo::StatementFlag;
Fangrui Song692e0c92020-02-29 08:25:22 -0800374 OS.emitInt32(LineData);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000375 }
376 if (HaveColumns) {
377 for (auto J = I; J != FileSegEnd; ++J) {
Fangrui Song692e0c92020-02-29 08:25:22 -0800378 OS.emitInt16(J->getColumn());
379 OS.emitInt16(0);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000380 }
381 }
382 I = FileSegEnd;
383 }
Fangrui Song6d2d5892020-02-14 19:21:58 -0800384 OS.emitLabel(LineEnd);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000385}
386
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000387static bool compressAnnotation(uint32_t Data, SmallVectorImpl<char> &Buffer) {
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
Zachary Turner63a28462016-05-17 23:50:21 +0000410static bool compressAnnotation(BinaryAnnotationsOpCode Annotation,
411 SmallVectorImpl<char> &Buffer) {
412 return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer);
413}
414
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000415static uint32_t encodeSignedNumber(uint32_t Data) {
416 if (Data >> 31)
417 return ((-Data) << 1) | 1;
418 return Data << 1;
419}
420
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000421void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS,
422 unsigned PrimaryFunctionId,
423 unsigned SourceFileId,
424 unsigned SourceLineNum,
425 const MCSymbol *FnStartSym,
426 const MCSymbol *FnEndSym) {
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000427 // Create and insert a fragment into the current section that will be encoded
428 // later.
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000429 new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId,
430 SourceLineNum, FnStartSym, FnEndSym,
431 OS.getCurrentSectionOnly());
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000432}
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000433
Reid Kleckner94ee0722018-12-17 21:49:35 +0000434MCFragment *CodeViewContext::emitDefRange(
David Majnemer408b5e62016-02-05 01:55:49 +0000435 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.
Reid Kleckner94ee0722018-12-17 21:49:35 +0000440 return new MCCVDefRangeFragment(Ranges, FixedSizePortion,
David Majnemer408b5e62016-02-05 01:55:49 +0000441 OS.getCurrentSectionOnly());
442}
443
Reid Klecknercb91e7d2016-02-04 00:21:42 +0000444static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin,
445 const MCSymbol *End) {
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000446 MCContext &Ctx = Layout.getAssembler().getContext();
447 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
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}
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000460
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000461void CodeViewContext::encodeInlineLineTable(MCAsmLayout &Layout,
462 MCCVInlineLineTableFragment &Frag) {
463 size_t LocBegin;
464 size_t LocEnd;
465 std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId);
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000466
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);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000472 LocBegin = std::min(LocBegin, Extent.first);
473 LocEnd = std::max(LocEnd, Extent.second);
474 }
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000475
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000476 if (LocBegin >= LocEnd)
477 return;
Reid Kleckner689f7732018-08-28 23:25:59 +0000478 ArrayRef<MCCVLoc> Locs = getLinesForExtent(LocBegin, LocEnd);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000479 if (Locs.empty())
480 return;
481
Reid Kleckner2c6430f2018-04-25 23:34:15 +0000482 // Check that the locations are all in the same section.
483#ifndef NDEBUG
484 const MCSection *FirstSec = &Locs.front().getLabel()->getSection();
Reid Kleckner689f7732018-08-28 23:25:59 +0000485 for (const MCCVLoc &Loc : Locs) {
Reid Kleckner2c6430f2018-04-25 23:34:15 +0000486 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
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000495 // 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.
Reid Kleckner689f7732018-08-28 23:25:59 +0000498 MCCVLoc StartLoc = Locs.front();
499 StartLoc.setLabel(Frag.getFnStartSym());
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000500 StartLoc.setFileNum(Frag.StartFileId);
501 StartLoc.setLine(Frag.StartLineNum);
Reid Klecknerc29b4f02016-07-14 23:47:15 +0000502 bool HaveOpenRange = false;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000503
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000504 const MCSymbol *LastLabel = Frag.getFnStartSym();
505 MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc;
506 LastSourceLoc.File = Frag.StartFileId;
507 LastSourceLoc.Line = Frag.StartLineNum;
508
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000509 SmallVectorImpl<char> &Buffer = Frag.getContents();
510 Buffer.clear(); // Clear old contents if we went through relaxation.
Reid Kleckner689f7732018-08-28 23:25:59 +0000511 for (const MCCVLoc &Loc : Locs) {
Reid Klecknerbb96df62016-10-05 22:36:07 +0000512 // Exit early if our line table would produce an oversized InlineSiteSym
513 // record. Account for the ChangeCodeLength annotation emitted after the
514 // loop ends.
Zachary Turner46225b12016-12-16 22:48:14 +0000515 constexpr uint32_t InlineSiteSize = 12;
516 constexpr uint32_t AnnotationSize = 8;
517 size_t MaxBufferSize = MaxRecordLength - InlineSiteSize - AnnotationSize;
Reid Klecknerbb96df62016-10-05 22:36:07 +0000518 if (Buffer.size() >= MaxBufferSize)
519 break;
520
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000521 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());
536 compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
537 compressAnnotation(Length, Buffer);
Reid Klecknereddd6c22019-05-31 22:55:03 +0000538 LastLabel = Loc.getLabel();
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000539 }
540 HaveOpenRange = false;
541 continue;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000542 }
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000543 }
Reid Klecknerc29b4f02016-07-14 23:47:15 +0000544
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000545 // 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)
Reid Klecknerc29b4f02016-07-14 23:47:15 +0000550 continue;
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000551
Reid Klecknerc29b4f02016-07-14 23:47:15 +0000552 HaveOpenRange = true;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000553
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000554 if (CurSourceLoc.File != LastSourceLoc.File) {
Reid Kleckner26fa1bf2017-09-19 18:14:45 +0000555 unsigned FileOffset = static_cast<const MCConstantExpr *>(
556 Files[CurSourceLoc.File - 1]
557 .ChecksumTableOffset->getVariableValue())
558 ->getValue();
Zachary Turner63a28462016-05-17 23:50:21 +0000559 compressAnnotation(BinaryAnnotationsOpCode::ChangeFile, Buffer);
Reid Kleckner344078f2016-02-19 23:55:38 +0000560 compressAnnotation(FileOffset, Buffer);
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000561 }
562
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000563 int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000564 unsigned EncodedLineDelta = encodeSignedNumber(LineDelta);
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000565 unsigned CodeDelta = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
566 if (CodeDelta == 0 && LineDelta != 0) {
Zachary Turner63a28462016-05-17 23:50:21 +0000567 compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000568 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;
Zachary Turner63a28462016-05-17 23:50:21 +0000574 compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset,
575 Buffer);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000576 compressAnnotation(Operand, Buffer);
577 } else {
578 // Otherwise use the separate line and code deltas.
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000579 if (LineDelta != 0) {
580 compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);
581 compressAnnotation(EncodedLineDelta, Buffer);
582 }
Zachary Turner63a28462016-05-17 23:50:21 +0000583 compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffset, Buffer);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000584 compressAnnotation(CodeDelta, Buffer);
585 }
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000586
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000587 LastLabel = Loc.getLabel();
588 LastSourceLoc = CurSourceLoc;
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000589 }
David Majnemerc9911f22016-02-02 19:22:34 +0000590
Reid Klecknerc29b4f02016-07-14 23:47:15 +0000591 assert(HaveOpenRange);
David Majnemerc9911f22016-02-02 19:22:34 +0000592
593 unsigned EndSymLength =
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000594 computeLabelDiff(Layout, LastLabel, Frag.getFnEndSym());
David Majnemerc9911f22016-02-02 19:22:34 +0000595 unsigned LocAfterLength = ~0U;
Reid Kleckner689f7732018-08-28 23:25:59 +0000596 ArrayRef<MCCVLoc> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1);
Reid Klecknercb91e7d2016-02-04 00:21:42 +0000597 if (!LocAfter.empty()) {
598 // Only try to compute this difference if we're in the same section.
Reid Kleckner689f7732018-08-28 23:25:59 +0000599 const MCCVLoc &Loc = LocAfter[0];
Sam Clegg5e102ee2018-01-12 18:05:40 +0000600 if (&Loc.getLabel()->getSection() == &LastLabel->getSection())
Reid Klecknera9f4cc92016-09-07 16:15:31 +0000601 LocAfterLength = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
Reid Klecknercb91e7d2016-02-04 00:21:42 +0000602 }
David Majnemerc9911f22016-02-02 19:22:34 +0000603
Zachary Turner63a28462016-05-17 23:50:21 +0000604 compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
David Majnemerc9911f22016-02-02 19:22:34 +0000605 compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer);
David Majnemer6fcbd7e2016-01-29 19:24:12 +0000606}
607
David Majnemer408b5e62016-02-05 01:55:49 +0000608void CodeViewContext::encodeDefRange(MCAsmLayout &Layout,
609 MCCVDefRangeFragment &Frag) {
610 MCContext &Ctx = Layout.getAssembler().getContext();
611 SmallVectorImpl<char> &Contents = Frag.getContents();
612 Contents.clear();
613 SmallVectorImpl<MCFixup> &Fixups = Frag.getFixups();
614 Fixups.clear();
615 raw_svector_ostream OS(Contents);
616
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000617 // Compute all the sizes up front.
618 SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes;
619 const MCSymbol *LastLabel = nullptr;
David Majnemer408b5e62016-02-05 01:55:49 +0000620 for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) {
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000621 unsigned GapSize =
622 LastLabel ? computeLabelDiff(Layout, LastLabel, Range.first) : 0;
David Majnemer408b5e62016-02-05 01:55:49 +0000623 unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second);
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000624 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
Peter Collingbournee3f65292018-05-18 19:46:24 +0000643 support::endian::Writer LEWriter(OS, support::little);
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000644
David Majnemer408b5e62016-02-05 01:55:49 +0000645 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
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000651 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx);
David Majnemer408b5e62016-02-05 01:55:49 +0000652 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.
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000662 size_t RecordSize = FixedSizePortion.size() +
663 sizeof(LocalVariableAddrRange) + 4 * NumGaps;
Reid Kleckner11cf0532017-01-24 16:57:55 +0000664 // Write out the record size.
665 LEWriter.write<uint16_t>(RecordSize);
David Majnemer408b5e62016-02-05 01:55:49 +0000666 // 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));
Reid Kleckner11cf0532017-01-24 16:57:55 +0000671 LEWriter.write<uint32_t>(0); // Fixup for code start.
David Majnemer408b5e62016-02-05 01:55:49 +0000672 // 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));
Reid Kleckner11cf0532017-01-24 16:57:55 +0000674 LEWriter.write<uint16_t>(0); // Fixup for section index.
David Majnemer408b5e62016-02-05 01:55:49 +0000675 // Write down the range's extent.
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000676 LEWriter.write<uint16_t>(Chunk);
David Majnemer408b5e62016-02-05 01:55:49 +0000677
678 // Move on to the next range.
679 Bias += Chunk;
680 RangeSize -= Chunk;
681 } while (RangeSize > 0);
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000682
683 // Emit the gaps afterwards.
Reid Kleckner11cf0532017-01-24 16:57:55 +0000684 assert((NumGaps == 0 || Bias <= MaxDefRange) &&
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000685 "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);
Reid Kleckner11cf0532017-01-24 16:57:55 +0000692 LEWriter.write<uint16_t>(GapSize);
Reid Klecknerbe82d3e2016-09-15 22:05:08 +0000693 GapStartOffset += GapSize + RangeSize;
694 }
David Majnemer408b5e62016-02-05 01:55:49 +0000695 }
696}