LLVM  16.0.0git
MCCodeView.cpp
Go to the documentation of this file.
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-exception
6 //
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"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCValue.h"
25 
26 using namespace llvm;
27 using namespace llvm::codeview;
28 
30 
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.
40 bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const {
41  unsigned Idx = FileNumber - 1;
42  if (Idx < Files.size())
43  return Files[Idx].Assigned;
44  return false;
45 }
46 
47 bool CodeViewContext::addFile(MCStreamer &OS, unsigned FileNumber,
48  StringRef Filename,
49  ArrayRef<uint8_t> ChecksumBytes,
50  uint8_t ChecksumKind) {
51  assert(FileNumber > 0);
52  auto FilenameOffset = addToStringTable(Filename);
53  Filename = FilenameOffset.first;
54  unsigned Idx = FileNumber - 1;
55  if (Idx >= Files.size())
56  Files.resize(Idx + 1);
57 
58  if (Filename.empty())
59  Filename = "<stdin>";
60 
61  if (Files[Idx].Assigned)
62  return false;
63 
64  FilenameOffset = addToStringTable(Filename);
65  Filename = FilenameOffset.first;
66  unsigned Offset = FilenameOffset.second;
67 
68  auto ChecksumOffsetSymbol =
69  OS.getContext().createTempSymbol("checksum_offset", false);
70  Files[Idx].StringTableOffset = Offset;
71  Files[Idx].ChecksumTableOffset = ChecksumOffsetSymbol;
72  Files[Idx].Assigned = true;
73  Files[Idx].Checksum = ChecksumBytes;
74  Files[Idx].ChecksumKind = ChecksumKind;
75 
76  return true;
77 }
78 
80  if (FuncId >= Functions.size())
81  return nullptr;
82  if (Functions[FuncId].isUnallocatedFunctionInfo())
83  return nullptr;
84  return &Functions[FuncId];
85 }
86 
88  if (FuncId >= Functions.size())
89  Functions.resize(FuncId + 1);
90 
91  // Return false if this function info was already allocated.
92  if (!Functions[FuncId].isUnallocatedFunctionInfo())
93  return false;
94 
95  // Mark this as an allocated normal function, and leave the rest alone.
96  Functions[FuncId].ParentFuncIdPlusOne = MCCVFunctionInfo::FunctionSentinel;
97  return true;
98 }
99 
100 bool CodeViewContext::recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc,
101  unsigned IAFile, unsigned IALine,
102  unsigned IACol) {
103  if (FuncId >= Functions.size())
104  Functions.resize(FuncId + 1);
105 
106  // Return false if this function info was already allocated.
107  if (!Functions[FuncId].isUnallocatedFunctionInfo())
108  return false;
109 
110  MCCVFunctionInfo::LineInfo InlinedAt;
111  InlinedAt.File = IAFile;
112  InlinedAt.Line = IALine;
113  InlinedAt.Col = IACol;
114 
115  // Mark this as an inlined call site and record call site line info.
116  MCCVFunctionInfo *Info = &Functions[FuncId];
117  Info->ParentFuncIdPlusOne = IAFunc + 1;
118  Info->InlinedAt = InlinedAt;
119 
120  // Walk up the call chain adding this function id to the InlinedAtMap of all
121  // transitive callers until we hit a real function.
122  while (Info->isInlinedCallSite()) {
123  InlinedAt = Info->InlinedAt;
124  Info = getCVFunctionInfo(Info->getParentFuncId());
125  Info->InlinedAtMap[FuncId] = InlinedAt;
126  }
127 
128  return true;
129 }
130 
132  unsigned FunctionId, unsigned FileNo,
133  unsigned Line, unsigned Column,
134  bool PrologueEnd, bool IsStmt) {
135  addLineEntry(MCCVLoc{
136  Label, FunctionId, FileNo, Line, Column, PrologueEnd, IsStmt});
137 }
138 
139 MCDataFragment *CodeViewContext::getStringTableFragment() {
140  if (!StrTabFragment) {
141  StrTabFragment = new MCDataFragment();
142  // Start a new string table out with a null byte.
143  StrTabFragment->getContents().push_back('\0');
144  }
145  return StrTabFragment;
146 }
147 
148 std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {
149  SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents();
150  auto Insertion =
151  StringTable.insert(std::make_pair(S, unsigned(Contents.size())));
152  // Return the string from the table, since it is stable.
153  std::pair<StringRef, unsigned> Ret =
154  std::make_pair(Insertion.first->first(), Insertion.first->second);
155  if (Insertion.second) {
156  // The string map key is always null terminated.
157  Contents.append(Ret.first.begin(), Ret.first.end() + 1);
158  }
159  return Ret;
160 }
161 
162 unsigned CodeViewContext::getStringTableOffset(StringRef S) {
163  // A string table offset of zero is always the empty string.
164  if (S.empty())
165  return 0;
166  auto I = StringTable.find(S);
167  assert(I != StringTable.end());
168  return I->second;
169 }
170 
172  MCContext &Ctx = OS.getContext();
173  MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false),
174  *StringEnd = Ctx.createTempSymbol("strtab_end", false);
175 
177  OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4);
178  OS.emitLabel(StringBegin);
179 
180  // Put the string table data fragment here, if we haven't already put it
181  // somewhere else. If somebody wants two string tables in their .s file, one
182  // will just be empty.
183  if (!InsertedStrTabFragment) {
184  OS.insert(getStringTableFragment());
185  InsertedStrTabFragment = true;
186  }
187 
188  OS.emitValueToAlignment(Align(4), 0);
189 
190  OS.emitLabel(StringEnd);
191 }
192 
194  // Do nothing if there are no file checksums. Microsoft's linker rejects empty
195  // CodeView substreams.
196  if (Files.empty())
197  return;
198 
199  MCContext &Ctx = OS.getContext();
200  MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false),
201  *FileEnd = Ctx.createTempSymbol("filechecksums_end", false);
202 
204  OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4);
205  OS.emitLabel(FileBegin);
206 
207  unsigned CurrentOffset = 0;
208 
209  // Emit an array of FileChecksum entries. We index into this table using the
210  // user-provided file number. Each entry may be a variable number of bytes
211  // determined by the checksum kind and size.
212  for (auto File : Files) {
213  OS.emitAssignment(File.ChecksumTableOffset,
214  MCConstantExpr::create(CurrentOffset, Ctx));
215  CurrentOffset += 4; // String table offset.
216  if (!File.ChecksumKind) {
217  CurrentOffset +=
218  4; // One byte each for checksum size and kind, then align to 4 bytes.
219  } else {
220  CurrentOffset += 2; // One byte each for checksum size and kind.
221  CurrentOffset += File.Checksum.size();
222  CurrentOffset = alignTo(CurrentOffset, 4);
223  }
224 
225  OS.emitInt32(File.StringTableOffset);
226 
227  if (!File.ChecksumKind) {
228  // There is no checksum. Therefore zero the next two fields and align
229  // back to 4 bytes.
230  OS.emitInt32(0);
231  continue;
232  }
233  OS.emitInt8(static_cast<uint8_t>(File.Checksum.size()));
234  OS.emitInt8(File.ChecksumKind);
235  OS.emitBytes(toStringRef(File.Checksum));
237  }
238 
239  OS.emitLabel(FileEnd);
240 
241  ChecksumOffsetsAssigned = true;
242 }
243 
244 // Output checksum table offset of the given file number. It is possible that
245 // not all files have been registered yet, and so the offset cannot be
246 // calculated. In this case a symbol representing the offset is emitted, and
247 // the value of this symbol will be fixed up at a later time.
249  unsigned FileNo) {
250  unsigned Idx = FileNo - 1;
251 
252  if (Idx >= Files.size())
253  Files.resize(Idx + 1);
254 
255  if (ChecksumOffsetsAssigned) {
256  OS.emitSymbolValue(Files[Idx].ChecksumTableOffset, 4);
257  return;
258  }
259 
260  const MCSymbolRefExpr *SRE =
261  MCSymbolRefExpr::create(Files[Idx].ChecksumTableOffset, OS.getContext());
262 
263  OS.emitValueImpl(SRE, 4);
264 }
265 
266 void CodeViewContext::addLineEntry(const MCCVLoc &LineEntry) {
267  size_t Offset = MCCVLines.size();
268  auto I = MCCVLineStartStop.insert(
269  {LineEntry.getFunctionId(), {Offset, Offset + 1}});
270  if (!I.second)
271  I.first->second.second = Offset + 1;
272  MCCVLines.push_back(LineEntry);
273 }
274 
275 std::vector<MCCVLoc>
277  std::vector<MCCVLoc> FilteredLines;
278  auto I = MCCVLineStartStop.find(FuncId);
279  if (I != MCCVLineStartStop.end()) {
280  MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(FuncId);
281  for (size_t Idx = I->second.first, End = I->second.second; Idx != End;
282  ++Idx) {
283  unsigned LocationFuncId = MCCVLines[Idx].getFunctionId();
284  if (LocationFuncId == FuncId) {
285  // This was a .cv_loc directly for FuncId, so record it.
286  FilteredLines.push_back(MCCVLines[Idx]);
287  } else {
288  // Check if the current location is inlined in this function. If it is,
289  // synthesize a statement .cv_loc at the original inlined call site.
290  auto I = SiteInfo->InlinedAtMap.find(LocationFuncId);
291  if (I != SiteInfo->InlinedAtMap.end()) {
292  MCCVFunctionInfo::LineInfo &IA = I->second;
293  // Only add the location if it differs from the previous location.
294  // Large inlined calls will have many .cv_loc entries and we only need
295  // one line table entry in the parent function.
296  if (FilteredLines.empty() ||
297  FilteredLines.back().getFileNum() != IA.File ||
298  FilteredLines.back().getLine() != IA.Line ||
299  FilteredLines.back().getColumn() != IA.Col) {
300  FilteredLines.push_back(MCCVLoc(
301  MCCVLines[Idx].getLabel(),
302  FuncId, IA.File, IA.Line, IA.Col, false, false));
303  }
304  }
305  }
306  }
307  }
308  return FilteredLines;
309 }
310 
311 std::pair<size_t, size_t> CodeViewContext::getLineExtent(unsigned FuncId) {
312  auto I = MCCVLineStartStop.find(FuncId);
313  // Return an empty extent if there are no cv_locs for this function id.
314  if (I == MCCVLineStartStop.end())
315  return {~0ULL, 0};
316  return I->second;
317 }
318 
320  if (R <= L)
321  return None;
322  if (L >= MCCVLines.size())
323  return None;
324  return makeArrayRef(&MCCVLines[L], R - L);
325 }
326 
328  unsigned FuncId,
329  const MCSymbol *FuncBegin,
330  const MCSymbol *FuncEnd) {
331  MCContext &Ctx = OS.getContext();
332  MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false),
333  *LineEnd = Ctx.createTempSymbol("linetable_end", false);
334 
336  OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4);
337  OS.emitLabel(LineBegin);
338  OS.emitCOFFSecRel32(FuncBegin, /*Offset=*/0);
339  OS.emitCOFFSectionIndex(FuncBegin);
340 
341  // Actual line info.
342  std::vector<MCCVLoc> Locs = getFunctionLineEntries(FuncId);
343  bool HaveColumns = any_of(Locs, [](const MCCVLoc &LineEntry) {
344  return LineEntry.getColumn() != 0;
345  });
346  OS.emitInt16(HaveColumns ? int(LF_HaveColumns) : 0);
347  OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4);
348 
349  for (auto I = Locs.begin(), E = Locs.end(); I != E;) {
350  // Emit a file segment for the run of locations that share a file id.
351  unsigned CurFileNum = I->getFileNum();
352  auto FileSegEnd =
353  std::find_if(I, E, [CurFileNum](const MCCVLoc &Loc) {
354  return Loc.getFileNum() != CurFileNum;
355  });
356  unsigned EntryCount = FileSegEnd - I;
357  OS.AddComment(
358  "Segment for file '" +
359  Twine(getStringTableFragment()
360  ->getContents()[Files[CurFileNum - 1].StringTableOffset]) +
361  "' begins");
362  OS.emitCVFileChecksumOffsetDirective(CurFileNum);
363  OS.emitInt32(EntryCount);
364  uint32_t SegmentSize = 12;
365  SegmentSize += 8 * EntryCount;
366  if (HaveColumns)
367  SegmentSize += 4 * EntryCount;
368  OS.emitInt32(SegmentSize);
369 
370  for (auto J = I; J != FileSegEnd; ++J) {
371  OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4);
372  unsigned LineData = J->getLine();
373  if (J->isStmt())
374  LineData |= LineInfo::StatementFlag;
375  OS.emitInt32(LineData);
376  }
377  if (HaveColumns) {
378  for (auto J = I; J != FileSegEnd; ++J) {
379  OS.emitInt16(J->getColumn());
380  OS.emitInt16(0);
381  }
382  }
383  I = FileSegEnd;
384  }
385  OS.emitLabel(LineEnd);
386 }
387 
389  if (isUInt<7>(Data)) {
390  Buffer.push_back(Data);
391  return true;
392  }
393 
394  if (isUInt<14>(Data)) {
395  Buffer.push_back((Data >> 8) | 0x80);
396  Buffer.push_back(Data & 0xff);
397  return true;
398  }
399 
400  if (isUInt<29>(Data)) {
401  Buffer.push_back((Data >> 24) | 0xC0);
402  Buffer.push_back((Data >> 16) & 0xff);
403  Buffer.push_back((Data >> 8) & 0xff);
404  Buffer.push_back(Data & 0xff);
405  return true;
406  }
407 
408  return false;
409 }
410 
412  SmallVectorImpl<char> &Buffer) {
413  return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer);
414 }
415 
417  if (Data >> 31)
418  return ((-Data) << 1) | 1;
419  return Data << 1;
420 }
421 
423  unsigned PrimaryFunctionId,
424  unsigned SourceFileId,
425  unsigned SourceLineNum,
426  const MCSymbol *FnStartSym,
427  const MCSymbol *FnEndSym) {
428  // Create and insert a fragment into the current section that will be encoded
429  // later.
430  new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId,
431  SourceLineNum, FnStartSym, FnEndSym,
432  OS.getCurrentSectionOnly());
433 }
434 
436  MCObjectStreamer &OS,
437  ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
438  StringRef FixedSizePortion) {
439  // Create and insert a fragment into the current section that will be encoded
440  // later.
441  return new MCCVDefRangeFragment(Ranges, FixedSizePortion,
442  OS.getCurrentSectionOnly());
443 }
444 
445 static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin,
446  const MCSymbol *End) {
447  MCContext &Ctx = Layout.getAssembler().getContext();
449  const MCExpr *BeginRef = MCSymbolRefExpr::create(Begin, Variant, Ctx),
450  *EndRef = MCSymbolRefExpr::create(End, Variant, Ctx);
451  const MCExpr *AddrDelta =
452  MCBinaryExpr::create(MCBinaryExpr::Sub, EndRef, BeginRef, Ctx);
453  int64_t Result;
454  bool Success = AddrDelta->evaluateKnownAbsolute(Result, Layout);
455  assert(Success && "failed to evaluate label difference as absolute");
456  (void)Success;
457  assert(Result >= 0 && "negative label difference requested");
458  assert(Result < UINT_MAX && "label difference greater than 2GB");
459  return unsigned(Result);
460 }
461 
464  size_t LocBegin;
465  size_t LocEnd;
466  std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId);
467 
468  // Include all child inline call sites in our .cv_loc extent.
469  MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(Frag.SiteFuncId);
470  for (auto &KV : SiteInfo->InlinedAtMap) {
471  unsigned ChildId = KV.first;
472  auto Extent = getLineExtent(ChildId);
473  LocBegin = std::min(LocBegin, Extent.first);
474  LocEnd = std::max(LocEnd, Extent.second);
475  }
476 
477  if (LocBegin >= LocEnd)
478  return;
479  ArrayRef<MCCVLoc> Locs = getLinesForExtent(LocBegin, LocEnd);
480  if (Locs.empty())
481  return;
482 
483  // Check that the locations are all in the same section.
484 #ifndef NDEBUG
485  const MCSection *FirstSec = &Locs.front().getLabel()->getSection();
486  for (const MCCVLoc &Loc : Locs) {
487  if (&Loc.getLabel()->getSection() != FirstSec) {
488  errs() << ".cv_loc " << Loc.getFunctionId() << ' ' << Loc.getFileNum()
489  << ' ' << Loc.getLine() << ' ' << Loc.getColumn()
490  << " is in the wrong section\n";
491  llvm_unreachable(".cv_loc crosses sections");
492  }
493  }
494 #endif
495 
496  // Make an artificial start location using the function start and the inlinee
497  // lines start location information. All deltas start relative to this
498  // location.
499  MCCVLoc StartLoc = Locs.front();
500  StartLoc.setLabel(Frag.getFnStartSym());
501  StartLoc.setFileNum(Frag.StartFileId);
502  StartLoc.setLine(Frag.StartLineNum);
503  bool HaveOpenRange = false;
504 
505  const MCSymbol *LastLabel = Frag.getFnStartSym();
506  MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc;
507  LastSourceLoc.File = Frag.StartFileId;
508  LastSourceLoc.Line = Frag.StartLineNum;
509 
510  SmallVectorImpl<char> &Buffer = Frag.getContents();
511  Buffer.clear(); // Clear old contents if we went through relaxation.
512  for (const MCCVLoc &Loc : Locs) {
513  // Exit early if our line table would produce an oversized InlineSiteSym
514  // record. Account for the ChangeCodeLength annotation emitted after the
515  // loop ends.
516  constexpr uint32_t InlineSiteSize = 12;
517  constexpr uint32_t AnnotationSize = 8;
518  size_t MaxBufferSize = MaxRecordLength - InlineSiteSize - AnnotationSize;
519  if (Buffer.size() >= MaxBufferSize)
520  break;
521 
522  if (Loc.getFunctionId() == Frag.SiteFuncId) {
523  CurSourceLoc.File = Loc.getFileNum();
524  CurSourceLoc.Line = Loc.getLine();
525  } else {
526  auto I = SiteInfo->InlinedAtMap.find(Loc.getFunctionId());
527  if (I != SiteInfo->InlinedAtMap.end()) {
528  // This .cv_loc is from a child inline call site. Use the source
529  // location of the inlined call site instead of the .cv_loc directive
530  // source location.
531  CurSourceLoc = I->second;
532  } else {
533  // We've hit a cv_loc not attributed to this inline call site. Use this
534  // label to end the PC range.
535  if (HaveOpenRange) {
536  unsigned Length = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
538  compressAnnotation(Length, Buffer);
539  LastLabel = Loc.getLabel();
540  }
541  HaveOpenRange = false;
542  continue;
543  }
544  }
545 
546  // Skip this .cv_loc if we have an open range and this isn't a meaningful
547  // source location update. The current table format does not support column
548  // info, so we can skip updates for those.
549  if (HaveOpenRange && CurSourceLoc.File == LastSourceLoc.File &&
550  CurSourceLoc.Line == LastSourceLoc.Line)
551  continue;
552 
553  HaveOpenRange = true;
554 
555  if (CurSourceLoc.File != LastSourceLoc.File) {
556  unsigned FileOffset = static_cast<const MCConstantExpr *>(
557  Files[CurSourceLoc.File - 1]
558  .ChecksumTableOffset->getVariableValue())
559  ->getValue();
561  compressAnnotation(FileOffset, Buffer);
562  }
563 
564  int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line;
565  unsigned EncodedLineDelta = encodeSignedNumber(LineDelta);
566  unsigned CodeDelta = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
567  if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) {
568  // The ChangeCodeOffsetAndLineOffset combination opcode is used when the
569  // encoded line delta uses 3 or fewer set bits and the code offset fits
570  // in one nibble.
571  unsigned Operand = (EncodedLineDelta << 4) | CodeDelta;
573  Buffer);
574  compressAnnotation(Operand, Buffer);
575  } else {
576  // Otherwise use the separate line and code deltas.
577  if (LineDelta != 0) {
579  compressAnnotation(EncodedLineDelta, Buffer);
580  }
582  compressAnnotation(CodeDelta, Buffer);
583  }
584 
585  LastLabel = Loc.getLabel();
586  LastSourceLoc = CurSourceLoc;
587  }
588 
589  assert(HaveOpenRange);
590 
591  unsigned EndSymLength =
592  computeLabelDiff(Layout, LastLabel, Frag.getFnEndSym());
593  unsigned LocAfterLength = ~0U;
594  ArrayRef<MCCVLoc> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1);
595  if (!LocAfter.empty()) {
596  // Only try to compute this difference if we're in the same section.
597  const MCCVLoc &Loc = LocAfter[0];
598  if (&Loc.getLabel()->getSection() == &LastLabel->getSection())
599  LocAfterLength = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
600  }
601 
603  compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer);
604 }
605 
607  MCCVDefRangeFragment &Frag) {
608  MCContext &Ctx = Layout.getAssembler().getContext();
609  SmallVectorImpl<char> &Contents = Frag.getContents();
610  Contents.clear();
612  Fixups.clear();
613  raw_svector_ostream OS(Contents);
614 
615  // Compute all the sizes up front.
616  SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes;
617  const MCSymbol *LastLabel = nullptr;
618  for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) {
619  unsigned GapSize =
620  LastLabel ? computeLabelDiff(Layout, LastLabel, Range.first) : 0;
621  unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second);
622  GapAndRangeSizes.push_back({GapSize, RangeSize});
623  LastLabel = Range.second;
624  }
625 
626  // Write down each range where the variable is defined.
627  for (size_t I = 0, E = Frag.getRanges().size(); I != E;) {
628  // If the range size of multiple consecutive ranges is under the max,
629  // combine the ranges and emit some gaps.
630  const MCSymbol *RangeBegin = Frag.getRanges()[I].first;
631  unsigned RangeSize = GapAndRangeSizes[I].second;
632  size_t J = I + 1;
633  for (; J != E; ++J) {
634  unsigned GapAndRangeSize = GapAndRangeSizes[J].first + GapAndRangeSizes[J].second;
635  if (RangeSize + GapAndRangeSize > MaxDefRange)
636  break;
637  RangeSize += GapAndRangeSize;
638  }
639  unsigned NumGaps = J - I - 1;
640 
642 
643  unsigned Bias = 0;
644  // We must split the range into chunks of MaxDefRange, this is a fundamental
645  // limitation of the file format.
646  do {
647  uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize);
648 
649  const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx);
650  const MCBinaryExpr *BE =
651  MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx);
652  MCValue Res;
653  BE->evaluateAsRelocatable(Res, &Layout, /*Fixup=*/nullptr);
654 
655  // Each record begins with a 2-byte number indicating how large the record
656  // is.
657  StringRef FixedSizePortion = Frag.getFixedSizePortion();
658  // Our record is a fixed sized prefix and a LocalVariableAddrRange that we
659  // are artificially constructing.
660  size_t RecordSize = FixedSizePortion.size() +
661  sizeof(LocalVariableAddrRange) + 4 * NumGaps;
662  // Write out the record size.
663  LEWriter.write<uint16_t>(RecordSize);
664  // Write out the fixed size prefix.
665  OS << FixedSizePortion;
666  // Make space for a fixup that will eventually have a section relative
667  // relocation pointing at the offset where the variable becomes live.
668  Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4));
669  LEWriter.write<uint32_t>(0); // Fixup for code start.
670  // Make space for a fixup that will record the section index for the code.
671  Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2));
672  LEWriter.write<uint16_t>(0); // Fixup for section index.
673  // Write down the range's extent.
674  LEWriter.write<uint16_t>(Chunk);
675 
676  // Move on to the next range.
677  Bias += Chunk;
678  RangeSize -= Chunk;
679  } while (RangeSize > 0);
680 
681  // Emit the gaps afterwards.
682  assert((NumGaps == 0 || Bias <= MaxDefRange) &&
683  "large ranges should not have gaps");
684  unsigned GapStartOffset = GapAndRangeSizes[I].second;
685  for (++I; I != J; ++I) {
686  unsigned GapSize, RangeSize;
687  assert(I < GapAndRangeSizes.size());
688  std::tie(GapSize, RangeSize) = GapAndRangeSizes[I];
689  LEWriter.write<uint16_t>(GapStartOffset);
690  LEWriter.write<uint16_t>(GapSize);
691  GapStartOffset += GapSize + RangeSize;
692  }
693  }
694 }
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:156
llvm::MCCVLoc::getColumn
unsigned getColumn() const
Get the Column of this MCCVLoc.
Definition: MCCodeView.h:69
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::CodeViewContext::addToStringTable
std::pair< StringRef, unsigned > addToStringTable(StringRef S)
Add something to the string table.
Definition: MCCodeView.cpp:148
llvm::MCCVInlineLineTableFragment
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition: MCFragment.h:515
llvm::MCObjectStreamer::emitLabel
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Definition: MCObjectStreamer.cpp:286
llvm::codeview::DebugSubsectionKind::Lines
@ Lines
llvm::CodeViewContext::getLineExtent
std::pair< size_t, size_t > getLineExtent(unsigned FuncId)
Definition: MCCodeView.cpp:311
llvm::MCCVInlineLineTableFragment::getFnEndSym
const MCSymbol * getFnEndSym() const
Definition: MCFragment.h:537
llvm::codeview::MaxDefRange
@ MaxDefRange
Definition: SymbolRecord.h:434
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::MCCVFunctionInfo::LineInfo::File
unsigned File
Definition: MCCodeView.h:109
llvm::CodeViewContext::recordCVLoc
void recordCVLoc(MCContext &Ctx, const MCSymbol *Label, unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt)
Saves the information from the currently parsed .cv_loc directive and sets CVLocSeen.
Definition: MCCodeView.cpp:131
llvm::MCCVFunctionInfo::FunctionSentinel
@ FunctionSentinel
Definition: MCCodeView.h:106
SymbolRecord.h
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
llvm::MCStreamer::emitInt8
void emitInt8(uint64_t Value)
Definition: MCStreamer.h:745
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::codeview::Line
Definition: Line.h:90
llvm::codeview::LF_HaveColumns
@ LF_HaveColumns
Definition: CodeView.h:570
llvm::MCFixup::create
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
llvm::codeview::BinaryAnnotationsOpCode::ChangeCodeOffset
@ ChangeCodeOffset
llvm::CodeViewContext::addFile
bool addFile(MCStreamer &OS, unsigned FileNumber, StringRef Filename, ArrayRef< uint8_t > ChecksumBytes, uint8_t ChecksumKind)
Definition: MCCodeView.cpp:47
llvm::MCCVDefRangeFragment::getRanges
ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > getRanges() const
Definition: MCFragment.h:564
MCAssembler.h
llvm::CodeViewContext::~CodeViewContext
~CodeViewContext()
Definition: MCCodeView.cpp:31
llvm::MCObjectStreamer::emitAbsoluteSymbolDiff
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) override
Emit the absolute difference between two symbols if possible.
Definition: MCObjectStreamer.cpp:162
llvm::MCStreamer::emitInt32
void emitInt32(uint64_t Value)
Definition: MCStreamer.h:747
llvm::MCObjectStreamer::emitCVFileChecksumOffsetDirective
void emitCVFileChecksumOffsetDirective(unsigned FileNo) override
This implements the CodeView '.cv_filechecksumoffset' assembler directive.
Definition: MCObjectStreamer.cpp:633
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:891
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::codeview::DebugSubsectionKind::StringTable
@ StringTable
STLExtras.h
llvm::MCEncodedFragmentWithFixups::getFixups
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:222
llvm::MCBinaryExpr
Binary assembler expressions.
Definition: MCExpr.h:481
llvm::support::endian::Writer
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52
llvm::CodeViewContext::emitLineTableForFunction
void emitLineTableForFunction(MCObjectStreamer &OS, unsigned FuncId, const MCSymbol *FuncBegin, const MCSymbol *FuncEnd)
Emits a line table substream.
Definition: MCCodeView.cpp:327
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MCCVFunctionInfo::LineInfo::Line
unsigned Line
Definition: MCCodeView.h:110
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
llvm::MCExpr::evaluateAsRelocatable
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:749
llvm::codeview::BinaryAnnotationsOpCode::ChangeLineOffset
@ ChangeLineOffset
MCObjectStreamer.h
llvm::CodeViewContext::getCVFunctionInfo
MCCVFunctionInfo * getCVFunctionInfo(unsigned FuncId)
Retreive the function info if this is a valid function id, or nullptr.
Definition: MCCodeView.cpp:79
llvm::MCFragment
Definition: MCFragment.h:30
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:212
llvm::MCSymbol::getSection
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:262
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::CodeViewContext::CodeViewContext
CodeViewContext()
llvm::FK_SecRel_4
@ FK_SecRel_4
A four-byte section relative fixup.
Definition: MCFixup.h:42
llvm::support::little
@ little
Definition: Endian.h:27
llvm::MCAssembler::getContext
MCContext & getContext() const
Definition: MCAssembler.h:321
llvm::CodeViewContext::encodeDefRange
void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F)
Definition: MCCodeView.cpp:606
llvm::CodeViewContext::addLineEntry
void addLineEntry(const MCCVLoc &LineEntry)
Add a line entry.
Definition: MCCodeView.cpp:266
llvm::MCObjectStreamer::emitValueImpl
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
Definition: MCObjectStreamer.cpp:244
MCContext.h
llvm::MCCVLoc::setLabel
void setLabel(const MCSymbol *L)
Definition: MCCodeView.h:74
llvm::MCCVLoc::getFileNum
unsigned getFileNum() const
Get the FileNum of this MCCVLoc.
Definition: MCCodeView.h:63
llvm::MCStreamer::emitSymbolValue
void emitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
Definition: MCStreamer.cpp:182
llvm::codeview::LocalVariableAddrRange
Definition: SymbolRecord.h:423
llvm::FK_SecRel_2
@ FK_SecRel_2
A two-byte section relative fixup.
Definition: MCFixup.h:41
llvm::MCObjectStreamer
Streaming object file generation interface.
Definition: MCObjectStreamer.h:42
llvm::dwarf::toStringRef
StringRef toStringRef(const Optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:193
llvm::AArch64::Fixups
Fixups
Definition: AArch64FixupKinds.h:17
llvm::MCObjectStreamer::emitValueToAlignment
void emitValueToAlignment(Align Alignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Definition: MCObjectStreamer.cpp:644
llvm::codeview::DebugSubsectionKind::FileChecksums
@ FileChecksums
llvm::MCCVFunctionInfo::LineInfo::Col
unsigned Col
Definition: MCCodeView.h:111
llvm::MCCVFunctionInfo::InlinedAtMap
DenseMap< unsigned, LineInfo > InlinedAtMap
Map from inlined call site id to the inlined at location to use for that call site.
Definition: MCCodeView.h:124
llvm::CodeViewContext::getLinesForExtent
ArrayRef< MCCVLoc > getLinesForExtent(size_t L, size_t R)
Definition: MCCodeView.cpp:319
llvm::AArch64PACKey::IA
@ IA
Definition: AArch64BaseInfo.h:819
llvm::MCExpr::evaluateKnownAbsolute
bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const
Definition: MCExpr.cpp:561
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:687
llvm::MCObjectStreamer::emitAssignment
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol.
Definition: MCObjectStreamer.cpp:387
llvm::MCCVLoc::setFileNum
void setFileNum(unsigned fileNum)
Set the FileNum of this MCCVLoc.
Definition: MCCodeView.h:79
Align
uint64_t Align
Definition: ELFObjHandler.cpp:81
CodeView.h
llvm::MCStreamer::getCurrentSectionOnly
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:389
llvm::CodeViewContext::emitInlineLineTableForFunction
void emitInlineLineTableForFunction(MCObjectStreamer &OS, unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
Definition: MCCodeView.cpp:422
Line.h
llvm::CodeViewContext::emitDefRange
MCFragment * emitDefRange(MCObjectStreamer &OS, ArrayRef< std::pair< const MCSymbol *, const MCSymbol * >> Ranges, StringRef FixedSizePortion)
Definition: MCCodeView.cpp:435
llvm::MCConstantExpr
Definition: MCExpr.h:144
llvm::MCCVLoc::getLabel
const MCSymbol * getLabel() const
Definition: MCCodeView.h:58
llvm::MCStreamer::emitInt16
void emitInt16(uint64_t Value)
Definition: MCStreamer.h:746
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
llvm::CodeViewContext::isValidFileNumber
bool isValidFileNumber(unsigned FileNumber) const
This is a valid number for use with .cv_loc if we've already seen a .cv_file for it.
Definition: MCCodeView.cpp:40
llvm::CodeViewContext::emitStringTable
void emitStringTable(MCObjectStreamer &OS)
Emits the string table substream.
Definition: MCCodeView.cpp:171
llvm::CodeViewContext::encodeInlineLineTable
void encodeInlineLineTable(MCAsmLayout &Layout, MCCVInlineLineTableFragment &F)
Encodes the binary annotations once we have a layout.
Definition: MCCodeView.cpp:462
llvm::CodeViewContext::emitFileChecksumOffset
void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo)
Emits the offset into the checksum table of the given file number.
Definition: MCCodeView.cpp:248
I
#define I(x, y, z)
Definition: MD5.cpp:58
StringExtras.h
FuncId
Profile::FuncID FuncId
Definition: Profile.cpp:321
llvm::MCBinaryExpr::create
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:183
compressAnnotation
static bool compressAnnotation(uint32_t Data, SmallVectorImpl< char > &Buffer)
Definition: MCCodeView.cpp:388
llvm::MCObjectStreamer::insert
void insert(MCFragment *F)
Definition: MCObjectStreamer.h:94
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCCVFunctionInfo
Information describing a function or inlined call site introduced by .cv_func_id or ....
Definition: MCCodeView.h:98
llvm::CodeViewContext::getFunctionLineEntries
std::vector< MCCVLoc > getFunctionLineEntries(unsigned FuncId)
Definition: MCCodeView.cpp:276
llvm::MCStreamer::emitCOFFSecRel32
virtual void emitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset)
Emits a COFF section relative relocation.
Definition: MCStreamer.cpp:977
llvm::MCCVFunctionInfo::LineInfo
Definition: MCCodeView.h:108
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::MCCVDefRangeFragment::getFixedSizePortion
StringRef getFixedSizePortion() const
Definition: MCFragment.h:568
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:525
llvm::ArrayRef< uint8_t >
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1716
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::MCCVInlineLineTableFragment::getFnStartSym
const MCSymbol * getFnStartSym() const
Definition: MCFragment.h:536
llvm::MCCVLoc::setLine
void setLine(unsigned line)
Set the Line of this MCCVLoc.
Definition: MCCodeView.h:82
llvm::support::endian::Writer::write
void write(ArrayRef< value_type > Val)
Definition: EndianStream.h:56
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
uint32_t
llvm::MCCVLoc
Instances of this class represent the information from a .cv_loc directive.
Definition: MCCodeView.h:38
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MCContext::createTempSymbol
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:318
llvm::ArrayRef::front
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:167
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
MCAsmLayout.h
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::CodeViewContext::recordFunctionId
bool recordFunctionId(unsigned FuncId)
Records the function id of a normal function.
Definition: MCCodeView.cpp:87
llvm::codeview::BinaryAnnotationsOpCode::ChangeFile
@ ChangeFile
MCCodeView.h
EndianStream.h
llvm::None
constexpr std::nullopt_t None
Definition: None.h:27
uint16_t
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::codeview::BinaryAnnotationsOpCode
BinaryAnnotationsOpCode
Definition: CodeView.h:493
llvm::MCBinaryExpr::Sub
@ Sub
Subtraction.
Definition: MCExpr.h:506
Success
#define Success
Definition: AArch64Disassembler.cpp:292
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:614
MCValue.h
llvm::codeview
Definition: AppendingTypeTableBuilder.h:22
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:475
llvm::codeview::FunctionId
Definition: FunctionId.h:17
llvm::MCObjectStreamer::emitBytes
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
Definition: MCObjectStreamer.cpp:637
llvm::MCStreamer::getContext
MCContext & getContext() const
Definition: MCStreamer.h:293
llvm::CodeViewContext::recordInlinedCallSiteId
bool recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc, unsigned IAFile, unsigned IALine, unsigned IACol)
Records the function id of an inlined call site.
Definition: MCCodeView.cpp:100
llvm::MCCVLoc::getFunctionId
unsigned getFunctionId() const
Definition: MCCodeView.h:60
llvm::MCStreamer::emitCOFFSectionIndex
virtual void emitCOFFSectionIndex(MCSymbol const *Symbol)
Emits a COFF section index.
Definition: MCStreamer.cpp:975
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:658
llvm::codeview::BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset
@ ChangeCodeOffsetAndLineOffset
llvm::SmallVectorImpl< char >
llvm::MCAsmLayout::getAssembler
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::codeview::LineInfo::StatementFlag
@ StatementFlag
Definition: Line.h:32
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::lltok::ChecksumKind
@ ChecksumKind
Definition: LLToken.h:454
llvm::MCDataFragment
Fragment for data and encoded instructions.
Definition: MCFragment.h:241
encodeSignedNumber
static uint32_t encodeSignedNumber(uint32_t Data)
Definition: MCCodeView.cpp:416
llvm::MCCVDefRangeFragment
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:548
llvm::codeview::MaxRecordLength
@ MaxRecordLength
Definition: RecordSerialization.h:30
computeLabelDiff
static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin, const MCSymbol *End)
Definition: MCCodeView.cpp:445
llvm::codeview::BinaryAnnotationsOpCode::ChangeCodeLength
@ ChangeCodeLength
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::CodeViewContext::emitFileChecksums
void emitFileChecksums(MCObjectStreamer &OS)
Emits the file checksum substream.
Definition: MCCodeView.cpp:193
llvm::MCStreamer::AddComment
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:355
llvm::MCEncodedFragmentWithContents::getContents
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:196
llvm::MCCVInlineLineTableFragment::getContents
SmallString< 8 > & getContents()
Definition: MCFragment.h:539