LLVM  9.0.0svn
MCDwarf.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
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 #include "llvm/MC/MCDwarf.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/Hashing.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/LEB128.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/SourceMgr.h"
40 #include <cassert>
41 #include <cstdint>
42 #include <string>
43 #include <utility>
44 #include <vector>
45 
46 using namespace llvm;
47 
48 /// Manage the .debug_line_str section contents, if we use it.
50  MCSymbol *LineStrLabel = nullptr;
52  bool UseRelocs = false;
53 
54 public:
55  /// Construct an instance that can emit .debug_line_str (for use in a normal
56  /// v5 line table).
57  explicit MCDwarfLineStr(MCContext &Ctx) {
59  if (UseRelocs)
60  LineStrLabel =
62  }
63 
64  /// Emit a reference to the string.
65  void emitRef(MCStreamer *MCOS, StringRef Path);
66 
67  /// Emit the .debug_line_str section if appropriate.
68  void emitSection(MCStreamer *MCOS);
69 };
70 
71 static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
72  unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
73  if (MinInsnLength == 1)
74  return AddrDelta;
75  if (AddrDelta % MinInsnLength != 0) {
76  // TODO: report this error, but really only once.
77  ;
78  }
79  return AddrDelta / MinInsnLength;
80 }
81 
82 //
83 // This is called when an instruction is assembled into the specified section
84 // and if there is information from the last .loc directive that has yet to have
85 // a line entry made for it is made.
86 //
88  if (!MCOS->getContext().getDwarfLocSeen())
89  return;
90 
91  // Create a symbol at in the current section for use in the line entry.
92  MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
93  // Set the value of the symbol to use for the MCDwarfLineEntry.
94  MCOS->EmitLabel(LineSym);
95 
96  // Get the current .loc info saved in the context.
97  const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
98 
99  // Create a (local) line entry with the symbol and the current .loc info.
100  MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
101 
102  // clear DwarfLocSeen saying the current .loc info is now used.
103  MCOS->getContext().clearDwarfLocSeen();
104 
105  // Add the line entry to this section's entries.
106  MCOS->getContext()
108  .getMCLineSections()
109  .addLineEntry(LineEntry, Section);
110 }
111 
112 //
113 // This helper routine returns an expression of End - Start + IntVal .
114 //
115 static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
116  const MCSymbol &Start,
117  const MCSymbol &End,
118  int IntVal) {
120  const MCExpr *Res =
121  MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
122  const MCExpr *RHS =
123  MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
124  const MCExpr *Res1 =
126  const MCExpr *Res2 =
127  MCConstantExpr::create(IntVal, MCOS.getContext());
128  const MCExpr *Res3 =
130  return Res3;
131 }
132 
133 //
134 // This helper routine returns an expression of Start + IntVal .
135 //
136 static inline const MCExpr *
137 makeStartPlusIntExpr(MCContext &Ctx, const MCSymbol &Start, int IntVal) {
139  const MCExpr *LHS = MCSymbolRefExpr::create(&Start, Variant, Ctx);
140  const MCExpr *RHS = MCConstantExpr::create(IntVal, Ctx);
141  const MCExpr *Res = MCBinaryExpr::create(MCBinaryExpr::Add, LHS, RHS, Ctx);
142  return Res;
143 }
144 
145 //
146 // This emits the Dwarf line table for the specified section from the entries
147 // in the LineSection.
148 //
149 static inline void
151  const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
152  unsigned FileNum = 1;
153  unsigned LastLine = 1;
154  unsigned Column = 0;
155  unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
156  unsigned Isa = 0;
157  unsigned Discriminator = 0;
158  MCSymbol *LastLabel = nullptr;
159 
160  // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
161  for (const MCDwarfLineEntry &LineEntry : LineEntries) {
162  int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
163 
164  if (FileNum != LineEntry.getFileNum()) {
165  FileNum = LineEntry.getFileNum();
166  MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
167  MCOS->EmitULEB128IntValue(FileNum);
168  }
169  if (Column != LineEntry.getColumn()) {
170  Column = LineEntry.getColumn();
171  MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
172  MCOS->EmitULEB128IntValue(Column);
173  }
174  if (Discriminator != LineEntry.getDiscriminator() &&
175  MCOS->getContext().getDwarfVersion() >= 4) {
176  Discriminator = LineEntry.getDiscriminator();
177  unsigned Size = getULEB128Size(Discriminator);
178  MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
179  MCOS->EmitULEB128IntValue(Size + 1);
180  MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
181  MCOS->EmitULEB128IntValue(Discriminator);
182  }
183  if (Isa != LineEntry.getIsa()) {
184  Isa = LineEntry.getIsa();
185  MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
186  MCOS->EmitULEB128IntValue(Isa);
187  }
188  if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
189  Flags = LineEntry.getFlags();
190  MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
191  }
192  if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
193  MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
194  if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
195  MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
196  if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
197  MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
198 
199  MCSymbol *Label = LineEntry.getLabel();
200 
201  // At this point we want to emit/create the sequence to encode the delta in
202  // line numbers and the increment of the address from the previous Label
203  // and the current Label.
204  const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
205  MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
206  asmInfo->getCodePointerSize());
207 
208  Discriminator = 0;
209  LastLine = LineEntry.getLine();
210  LastLabel = Label;
211  }
212 
213  // Emit a DW_LNE_end_sequence for the end of the section.
214  // Use the section end label to compute the address delta and use INT64_MAX
215  // as the line delta which is the signal that this is actually a
216  // DW_LNE_end_sequence.
217  MCSymbol *SectionEnd = MCOS->endSection(Section);
218 
219  // Switch back the dwarf line section, in case endSection had to switch the
220  // section.
221  MCContext &Ctx = MCOS->getContext();
223 
224  const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
225  MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
226  AsmInfo->getCodePointerSize());
227 }
228 
229 //
230 // This emits the Dwarf file and the line tables.
231 //
233  MCDwarfLineTableParams Params) {
234  MCContext &context = MCOS->getContext();
235 
236  auto &LineTables = context.getMCDwarfLineTables();
237 
238  // Bail out early so we don't switch to the debug_line section needlessly and
239  // in doing so create an unnecessary (if empty) section.
240  if (LineTables.empty())
241  return;
242 
243  // In a v5 non-split line table, put the strings in a separate section.
244  Optional<MCDwarfLineStr> LineStr;
245  if (context.getDwarfVersion() >= 5)
246  LineStr = MCDwarfLineStr(context);
247 
248  // Switch to the section where the table will be emitted into.
250 
251  // Handle the rest of the Compile Units.
252  for (const auto &CUIDTablePair : LineTables) {
253  CUIDTablePair.second.EmitCU(MCOS, Params, LineStr);
254  }
255 
256  if (LineStr)
257  LineStr->emitSection(MCOS);
258 }
259 
261  MCSection *Section) const {
262  if (Header.MCDwarfFiles.empty())
263  return;
264  Optional<MCDwarfLineStr> NoLineStr(None);
265  MCOS.SwitchSection(Section);
266  MCOS.EmitLabel(Header.Emit(&MCOS, Params, None, NoLineStr).second);
267 }
268 
269 std::pair<MCSymbol *, MCSymbol *>
271  Optional<MCDwarfLineStr> &LineStr) const {
272  static const char StandardOpcodeLengths[] = {
273  0, // length of DW_LNS_copy
274  1, // length of DW_LNS_advance_pc
275  1, // length of DW_LNS_advance_line
276  1, // length of DW_LNS_set_file
277  1, // length of DW_LNS_set_column
278  0, // length of DW_LNS_negate_stmt
279  0, // length of DW_LNS_set_basic_block
280  0, // length of DW_LNS_const_add_pc
281  1, // length of DW_LNS_fixed_advance_pc
282  0, // length of DW_LNS_set_prologue_end
283  0, // length of DW_LNS_set_epilogue_begin
284  1 // DW_LNS_set_isa
285  };
286  assert(array_lengthof(StandardOpcodeLengths) >=
287  (Params.DWARF2LineOpcodeBase - 1U));
288  return Emit(
289  MCOS, Params,
290  makeArrayRef(StandardOpcodeLengths, Params.DWARF2LineOpcodeBase - 1),
291  LineStr);
292 }
293 
294 static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
295  MCContext &Context = OS.getContext();
296  assert(!isa<MCSymbolRefExpr>(Expr));
297  if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
298  return Expr;
299 
300  MCSymbol *ABS = Context.createTempSymbol();
301  OS.EmitAssignment(ABS, Expr);
302  return MCSymbolRefExpr::create(ABS, Context);
303 }
304 
305 static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
306  const MCExpr *ABS = forceExpAbs(OS, Value);
307  OS.EmitValue(ABS, Size);
308 }
309 
311  // Switch to the .debug_line_str section.
312  MCOS->SwitchSection(
314  // Emit the strings without perturbing the offsets we used.
315  LineStrings.finalizeInOrder();
317  Data.resize(LineStrings.getSize());
318  LineStrings.write((uint8_t *)Data.data());
319  MCOS->EmitBinaryData(Data.str());
320 }
321 
323  int RefSize = 4; // FIXME: Support DWARF-64
324  size_t Offset = LineStrings.add(Path);
325  if (UseRelocs) {
326  MCContext &Ctx = MCOS->getContext();
327  MCOS->EmitValue(makeStartPlusIntExpr(Ctx, *LineStrLabel, Offset), RefSize);
328  } else
329  MCOS->EmitIntValue(Offset, RefSize);
330 }
331 
332 void MCDwarfLineTableHeader::emitV2FileDirTables(MCStreamer *MCOS) const {
333  // First the directory table.
334  for (auto &Dir : MCDwarfDirs) {
335  MCOS->EmitBytes(Dir); // The DirectoryName, and...
336  MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
337  }
338  MCOS->EmitIntValue(0, 1); // Terminate the directory list.
339 
340  // Second the file table.
341  for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
342  assert(!MCDwarfFiles[i].Name.empty());
343  MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName and...
344  MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
345  MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex); // Directory number.
346  MCOS->EmitIntValue(0, 1); // Last modification timestamp (always 0).
347  MCOS->EmitIntValue(0, 1); // File size (always 0).
348  }
349  MCOS->EmitIntValue(0, 1); // Terminate the file list.
350 }
351 
353  bool EmitMD5, bool HasSource,
354  Optional<MCDwarfLineStr> &LineStr) {
355  assert(!DwarfFile.Name.empty());
356  if (LineStr)
357  LineStr->emitRef(MCOS, DwarfFile.Name);
358  else {
359  MCOS->EmitBytes(DwarfFile.Name); // FileName and...
360  MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
361  }
362  MCOS->EmitULEB128IntValue(DwarfFile.DirIndex); // Directory number.
363  if (EmitMD5) {
364  MD5::MD5Result *Cksum = DwarfFile.Checksum;
365  MCOS->EmitBinaryData(
366  StringRef(reinterpret_cast<const char *>(Cksum->Bytes.data()),
367  Cksum->Bytes.size()));
368  }
369  if (HasSource) {
370  if (LineStr)
371  LineStr->emitRef(MCOS, DwarfFile.Source.getValueOr(StringRef()));
372  else {
373  MCOS->EmitBytes(
374  DwarfFile.Source.getValueOr(StringRef())); // Source and...
375  MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
376  }
377  }
378 }
379 
380 void MCDwarfLineTableHeader::emitV5FileDirTables(
381  MCStreamer *MCOS, Optional<MCDwarfLineStr> &LineStr,
382  StringRef CtxCompilationDir) const {
383  // The directory format, which is just a list of the directory paths. In a
384  // non-split object, these are references to .debug_line_str; in a split
385  // object, they are inline strings.
386  MCOS->EmitIntValue(1, 1);
387  MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
388  MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
389  : dwarf::DW_FORM_string);
390  MCOS->EmitULEB128IntValue(MCDwarfDirs.size() + 1);
391  // Try not to emit an empty compilation directory.
392  const StringRef CompDir =
393  CompilationDir.empty() ? CtxCompilationDir : StringRef(CompilationDir);
394  if (LineStr) {
395  // Record path strings, emit references here.
396  LineStr->emitRef(MCOS, CompDir);
397  for (const auto &Dir : MCDwarfDirs)
398  LineStr->emitRef(MCOS, Dir);
399  } else {
400  // The list of directory paths. Compilation directory comes first.
401  MCOS->EmitBytes(CompDir);
402  MCOS->EmitBytes(StringRef("\0", 1));
403  for (const auto &Dir : MCDwarfDirs) {
404  MCOS->EmitBytes(Dir); // The DirectoryName, and...
405  MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
406  }
407  }
408 
409  // The file format, which is the inline null-terminated filename and a
410  // directory index. We don't track file size/timestamp so don't emit them
411  // in the v5 table. Emit MD5 checksums and source if we have them.
412  uint64_t Entries = 2;
413  if (HasAllMD5)
414  Entries += 1;
415  if (HasSource)
416  Entries += 1;
417  MCOS->EmitIntValue(Entries, 1);
418  MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
419  MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
420  : dwarf::DW_FORM_string);
421  MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_directory_index);
422  MCOS->EmitULEB128IntValue(dwarf::DW_FORM_udata);
423  if (HasAllMD5) {
424  MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_MD5);
425  MCOS->EmitULEB128IntValue(dwarf::DW_FORM_data16);
426  }
427  if (HasSource) {
428  MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
429  MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
430  : dwarf::DW_FORM_string);
431  }
432  // Then the counted list of files. The root file is file #0, then emit the
433  // files as provide by .file directives. To accommodate assembler source
434  // written for DWARF v4 but trying to emit v5, if we didn't see a root file
435  // explicitly, replicate file #1.
436  MCOS->EmitULEB128IntValue(MCDwarfFiles.size());
437  emitOneV5FileEntry(MCOS, RootFile.Name.empty() ? MCDwarfFiles[1] : RootFile,
438  HasAllMD5, HasSource, LineStr);
439  for (unsigned i = 1; i < MCDwarfFiles.size(); ++i)
440  emitOneV5FileEntry(MCOS, MCDwarfFiles[i], HasAllMD5, HasSource, LineStr);
441 }
442 
443 std::pair<MCSymbol *, MCSymbol *>
445  ArrayRef<char> StandardOpcodeLengths,
446  Optional<MCDwarfLineStr> &LineStr) const {
447  MCContext &context = MCOS->getContext();
448 
449  // Create a symbol at the beginning of the line table.
450  MCSymbol *LineStartSym = Label;
451  if (!LineStartSym)
452  LineStartSym = context.createTempSymbol();
453  // Set the value of the symbol, as we are at the start of the line table.
454  MCOS->EmitLabel(LineStartSym);
455 
456  // Create a symbol for the end of the section (to be set when we get there).
457  MCSymbol *LineEndSym = context.createTempSymbol();
458 
459  // The first 4 bytes is the total length of the information for this
460  // compilation unit (not including these 4 bytes for the length).
461  emitAbsValue(*MCOS,
462  MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
463 
464  // Next 2 bytes is the Version.
465  unsigned LineTableVersion = context.getDwarfVersion();
466  MCOS->EmitIntValue(LineTableVersion, 2);
467 
468  // Keep track of the bytes between the very start and where the header length
469  // comes out.
470  unsigned PreHeaderLengthBytes = 4 + 2;
471 
472  // In v5, we get address info next.
473  if (LineTableVersion >= 5) {
474  MCOS->EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
475  MCOS->EmitIntValue(0, 1); // Segment selector; same as EmitGenDwarfAranges.
476  PreHeaderLengthBytes += 2;
477  }
478 
479  // Create a symbol for the end of the prologue (to be set when we get there).
480  MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
481 
482  // Length of the prologue, is the next 4 bytes. This is actually the length
483  // from after the length word, to the end of the prologue.
484  emitAbsValue(*MCOS,
485  MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
486  (PreHeaderLengthBytes + 4)),
487  4);
488 
489  // Parameters of the state machine, are next.
490  MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
491  // maximum_operations_per_instruction
492  // For non-VLIW architectures this field is always 1.
493  // FIXME: VLIW architectures need to update this field accordingly.
494  if (LineTableVersion >= 4)
495  MCOS->EmitIntValue(1, 1);
497  MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
498  MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
499  MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
500 
501  // Standard opcode lengths
502  for (char Length : StandardOpcodeLengths)
503  MCOS->EmitIntValue(Length, 1);
504 
505  // Put out the directory and file tables. The formats vary depending on
506  // the version.
507  if (LineTableVersion >= 5)
508  emitV5FileDirTables(MCOS, LineStr, context.getCompilationDir());
509  else
510  emitV2FileDirTables(MCOS);
511 
512  // This is the end of the prologue, so set the value of the symbol at the
513  // end of the prologue (that was used in a previous expression).
514  MCOS->EmitLabel(ProEndSym);
515 
516  return std::make_pair(LineStartSym, LineEndSym);
517 }
518 
520  MCDwarfLineTableParams Params,
521  Optional<MCDwarfLineStr> &LineStr) const {
522  MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
523 
524  // Put out the line tables.
525  for (const auto &LineSec : MCLineSections.getMCLineEntries())
526  EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
527 
528  // This is the end of the section, so set the value of the symbol at the end
529  // of this section (that was used in a previous expression).
530  MCOS->EmitLabel(LineEndSym);
531 }
532 
534  StringRef &FileName,
535  MD5::MD5Result *Checksum,
537  unsigned FileNumber) {
538  return Header.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
539 }
540 
543  StringRef &FileName,
544  MD5::MD5Result *Checksum,
546  unsigned FileNumber) {
547  if (Directory == CompilationDir)
548  Directory = "";
549  if (FileName.empty()) {
550  FileName = "<stdin>";
551  Directory = "";
552  }
553  assert(!FileName.empty());
554  // Keep track of whether any or all files have an MD5 checksum.
555  // If any files have embedded source, they all must.
556  if (MCDwarfFiles.empty()) {
557  trackMD5Usage(Checksum);
558  HasSource = (Source != None);
559  }
560  if (FileNumber == 0) {
561  // File numbers start with 1 and/or after any file numbers
562  // allocated by inline-assembler .file directives.
563  FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
564  SmallString<256> Buffer;
565  auto IterBool = SourceIdMap.insert(
566  std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
567  FileNumber));
568  if (!IterBool.second)
569  return IterBool.first->second;
570  }
571  // Make space for this FileNumber in the MCDwarfFiles vector if needed.
572  if (FileNumber >= MCDwarfFiles.size())
573  MCDwarfFiles.resize(FileNumber + 1);
574 
575  // Get the new MCDwarfFile slot for this FileNumber.
576  MCDwarfFile &File = MCDwarfFiles[FileNumber];
577 
578  // It is an error to see the same number more than once.
579  if (!File.Name.empty())
580  return make_error<StringError>("file number already allocated",
582 
583  // If any files have embedded source, they all must.
584  if (HasSource != (Source != None))
585  return make_error<StringError>("inconsistent use of embedded source",
587 
588  if (Directory.empty()) {
589  // Separate the directory part from the basename of the FileName.
590  StringRef tFileName = sys::path::filename(FileName);
591  if (!tFileName.empty()) {
592  Directory = sys::path::parent_path(FileName);
593  if (!Directory.empty())
594  FileName = tFileName;
595  }
596  }
597 
598  // Find or make an entry in the MCDwarfDirs vector for this Directory.
599  // Capture directory name.
600  unsigned DirIndex;
601  if (Directory.empty()) {
602  // For FileNames with no directories a DirIndex of 0 is used.
603  DirIndex = 0;
604  } else {
605  DirIndex = 0;
606  for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
607  if (Directory == MCDwarfDirs[DirIndex])
608  break;
609  }
610  if (DirIndex >= MCDwarfDirs.size())
611  MCDwarfDirs.push_back(Directory);
612  // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
613  // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
614  // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
615  // are stored at MCDwarfFiles[FileNumber].Name .
616  DirIndex++;
617  }
618 
619  File.Name = FileName;
620  File.DirIndex = DirIndex;
621  File.Checksum = Checksum;
622  trackMD5Usage(Checksum);
623  File.Source = Source;
624  if (Source)
625  HasSource = true;
626 
627  // return the allocated FileNumber.
628  return FileNumber;
629 }
630 
631 /// Utility function to emit the encoding to a streamer.
633  int64_t LineDelta, uint64_t AddrDelta) {
634  MCContext &Context = MCOS->getContext();
635  SmallString<256> Tmp;
636  raw_svector_ostream OS(Tmp);
637  MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
638  MCOS->EmitBytes(OS.str());
639 }
640 
641 /// Given a special op, return the address skip amount (in units of
642 /// DWARF2_LINE_MIN_INSN_LENGTH).
643 static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
644  return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
645 }
646 
647 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
649  int64_t LineDelta, uint64_t AddrDelta,
650  raw_ostream &OS) {
651  uint64_t Temp, Opcode;
652  bool NeedCopy = false;
653 
654  // The maximum address skip amount that can be encoded with a special op.
655  uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
656 
657  // Scale the address delta by the minimum instruction length.
658  AddrDelta = ScaleAddrDelta(Context, AddrDelta);
659 
660  // A LineDelta of INT64_MAX is a signal that this is actually a
661  // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
662  // end_sequence to emit the matrix entry.
663  if (LineDelta == INT64_MAX) {
664  if (AddrDelta == MaxSpecialAddrDelta)
665  OS << char(dwarf::DW_LNS_const_add_pc);
666  else if (AddrDelta) {
667  OS << char(dwarf::DW_LNS_advance_pc);
668  encodeULEB128(AddrDelta, OS);
669  }
670  OS << char(dwarf::DW_LNS_extended_op);
671  OS << char(1);
672  OS << char(dwarf::DW_LNE_end_sequence);
673  return;
674  }
675 
676  // Bias the line delta by the base.
677  Temp = LineDelta - Params.DWARF2LineBase;
678 
679  // If the line increment is out of range of a special opcode, we must encode
680  // it with DW_LNS_advance_line.
681  if (Temp >= Params.DWARF2LineRange ||
682  Temp + Params.DWARF2LineOpcodeBase > 255) {
683  OS << char(dwarf::DW_LNS_advance_line);
684  encodeSLEB128(LineDelta, OS);
685 
686  LineDelta = 0;
687  Temp = 0 - Params.DWARF2LineBase;
688  NeedCopy = true;
689  }
690 
691  // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
692  if (LineDelta == 0 && AddrDelta == 0) {
693  OS << char(dwarf::DW_LNS_copy);
694  return;
695  }
696 
697  // Bias the opcode by the special opcode base.
698  Temp += Params.DWARF2LineOpcodeBase;
699 
700  // Avoid overflow when addr_delta is large.
701  if (AddrDelta < 256 + MaxSpecialAddrDelta) {
702  // Try using a special opcode.
703  Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
704  if (Opcode <= 255) {
705  OS << char(Opcode);
706  return;
707  }
708 
709  // Try using DW_LNS_const_add_pc followed by special op.
710  Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
711  if (Opcode <= 255) {
712  OS << char(dwarf::DW_LNS_const_add_pc);
713  OS << char(Opcode);
714  return;
715  }
716  }
717 
718  // Otherwise use DW_LNS_advance_pc.
719  OS << char(dwarf::DW_LNS_advance_pc);
720  encodeULEB128(AddrDelta, OS);
721 
722  if (NeedCopy)
723  OS << char(dwarf::DW_LNS_copy);
724  else {
725  assert(Temp <= 255 && "Buggy special opcode encoding.");
726  OS << char(Temp);
727  }
728 }
729 
731  MCDwarfLineTableParams Params,
732  int64_t LineDelta, uint64_t AddrDelta,
733  raw_ostream &OS,
735  if (LineDelta != INT64_MAX) {
736  OS << char(dwarf::DW_LNS_advance_line);
737  encodeSLEB128(LineDelta, OS);
738  }
739 
740  // Use address delta to adjust address or use absolute address to adjust
741  // address.
742  bool SetDelta;
743  // According to DWARF spec., the DW_LNS_fixed_advance_pc opcode takes a
744  // single uhalf (unencoded) operand. So, the maximum value of AddrDelta
745  // is 65535. We set a conservative upper bound for it for relaxation.
746  if (AddrDelta > 60000) {
747  const MCAsmInfo *asmInfo = Context.getAsmInfo();
748  unsigned AddrSize = asmInfo->getCodePointerSize();
749 
750  OS << char(dwarf::DW_LNS_extended_op);
751  encodeULEB128(1 + AddrSize, OS);
752  OS << char(dwarf::DW_LNE_set_address);
753  // Generate fixup for the address.
754  *Offset = OS.tell();
755  *Size = AddrSize;
756  SetDelta = false;
757  std::vector<uint8_t> FillData;
758  FillData.insert(FillData.begin(), AddrSize, 0);
759  OS.write(reinterpret_cast<char *>(FillData.data()), AddrSize);
760  } else {
761  OS << char(dwarf::DW_LNS_fixed_advance_pc);
762  // Generate fixup for 2-bytes address delta.
763  *Offset = OS.tell();
764  *Size = 2;
765  SetDelta = true;
766  OS << char(0);
767  OS << char(0);
768  }
769 
770  if (LineDelta == INT64_MAX) {
771  OS << char(dwarf::DW_LNS_extended_op);
772  OS << char(1);
773  OS << char(dwarf::DW_LNE_end_sequence);
774  } else {
775  OS << char(dwarf::DW_LNS_copy);
776  }
777 
778  return SetDelta;
779 }
780 
781 // Utility function to write a tuple for .debug_abbrev.
782 static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
783  MCOS->EmitULEB128IntValue(Name);
784  MCOS->EmitULEB128IntValue(Form);
785 }
786 
787 // When generating dwarf for assembly source files this emits
788 // the data for .debug_abbrev section which contains three DIEs.
789 static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
790  MCContext &context = MCOS->getContext();
792 
793  // DW_TAG_compile_unit DIE abbrev (1).
794  MCOS->EmitULEB128IntValue(1);
795  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
797  EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
798  ? dwarf::DW_FORM_sec_offset
799  : dwarf::DW_FORM_data4);
800  if (context.getGenDwarfSectionSyms().size() > 1 &&
801  context.getDwarfVersion() >= 3) {
802  EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
803  ? dwarf::DW_FORM_sec_offset
804  : dwarf::DW_FORM_data4);
805  } else {
806  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
807  EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
808  }
809  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
810  if (!context.getCompilationDir().empty())
811  EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
812  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
813  if (!DwarfDebugFlags.empty())
814  EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
815  EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
816  EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
817  EmitAbbrev(MCOS, 0, 0);
818 
819  // DW_TAG_label DIE abbrev (2).
820  MCOS->EmitULEB128IntValue(2);
821  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
823  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
824  EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
825  EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
826  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
827  EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
828  EmitAbbrev(MCOS, 0, 0);
829 
830  // DW_TAG_unspecified_parameters DIE abbrev (3).
831  MCOS->EmitULEB128IntValue(3);
832  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
834  EmitAbbrev(MCOS, 0, 0);
835 
836  // Terminate the abbreviations for this compilation unit.
837  MCOS->EmitIntValue(0, 1);
838 }
839 
840 // When generating dwarf for assembly source files this emits the data for
841 // .debug_aranges section. This section contains a header and a table of pairs
842 // of PointerSize'ed values for the address and size of section(s) with line
843 // table entries.
844 static void EmitGenDwarfAranges(MCStreamer *MCOS,
845  const MCSymbol *InfoSectionSymbol) {
846  MCContext &context = MCOS->getContext();
847 
848  auto &Sections = context.getGenDwarfSectionSyms();
849 
851 
852  // This will be the length of the .debug_aranges section, first account for
853  // the size of each item in the header (see below where we emit these items).
854  int Length = 4 + 2 + 4 + 1 + 1;
855 
856  // Figure the padding after the header before the table of address and size
857  // pairs who's values are PointerSize'ed.
858  const MCAsmInfo *asmInfo = context.getAsmInfo();
859  int AddrSize = asmInfo->getCodePointerSize();
860  int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
861  if (Pad == 2 * AddrSize)
862  Pad = 0;
863  Length += Pad;
864 
865  // Add the size of the pair of PointerSize'ed values for the address and size
866  // of each section we have in the table.
867  Length += 2 * AddrSize * Sections.size();
868  // And the pair of terminating zeros.
869  Length += 2 * AddrSize;
870 
871  // Emit the header for this section.
872  // The 4 byte length not including the 4 byte value for the length.
873  MCOS->EmitIntValue(Length - 4, 4);
874  // The 2 byte version, which is 2.
875  MCOS->EmitIntValue(2, 2);
876  // The 4 byte offset to the compile unit in the .debug_info from the start
877  // of the .debug_info.
878  if (InfoSectionSymbol)
879  MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
881  else
882  MCOS->EmitIntValue(0, 4);
883  // The 1 byte size of an address.
884  MCOS->EmitIntValue(AddrSize, 1);
885  // The 1 byte size of a segment descriptor, we use a value of zero.
886  MCOS->EmitIntValue(0, 1);
887  // Align the header with the padding if needed, before we put out the table.
888  for(int i = 0; i < Pad; i++)
889  MCOS->EmitIntValue(0, 1);
890 
891  // Now emit the table of pairs of PointerSize'ed values for the section
892  // addresses and sizes.
893  for (MCSection *Sec : Sections) {
894  const MCSymbol *StartSymbol = Sec->getBeginSymbol();
895  MCSymbol *EndSymbol = Sec->getEndSymbol(context);
896  assert(StartSymbol && "StartSymbol must not be NULL");
897  assert(EndSymbol && "EndSymbol must not be NULL");
898 
899  const MCExpr *Addr = MCSymbolRefExpr::create(
900  StartSymbol, MCSymbolRefExpr::VK_None, context);
901  const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
902  *StartSymbol, *EndSymbol, 0);
903  MCOS->EmitValue(Addr, AddrSize);
904  emitAbsValue(*MCOS, Size, AddrSize);
905  }
906 
907  // And finally the pair of terminating zeros.
908  MCOS->EmitIntValue(0, AddrSize);
909  MCOS->EmitIntValue(0, AddrSize);
910 }
911 
912 // When generating dwarf for assembly source files this emits the data for
913 // .debug_info section which contains three parts. The header, the compile_unit
914 // DIE and a list of label DIEs.
915 static void EmitGenDwarfInfo(MCStreamer *MCOS,
916  const MCSymbol *AbbrevSectionSymbol,
917  const MCSymbol *LineSectionSymbol,
918  const MCSymbol *RangesSectionSymbol) {
919  MCContext &context = MCOS->getContext();
920 
922 
923  // Create a symbol at the start and end of this section used in here for the
924  // expression to calculate the length in the header.
925  MCSymbol *InfoStart = context.createTempSymbol();
926  MCOS->EmitLabel(InfoStart);
927  MCSymbol *InfoEnd = context.createTempSymbol();
928 
929  // First part: the header.
930 
931  // The 4 byte total length of the information for this compilation unit, not
932  // including these 4 bytes.
933  const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
934  emitAbsValue(*MCOS, Length, 4);
935 
936  // The 2 byte DWARF version.
937  MCOS->EmitIntValue(context.getDwarfVersion(), 2);
938 
939  // The DWARF v5 header has unit type, address size, abbrev offset.
940  // Earlier versions have abbrev offset, address size.
941  const MCAsmInfo &AsmInfo = *context.getAsmInfo();
942  int AddrSize = AsmInfo.getCodePointerSize();
943  if (context.getDwarfVersion() >= 5) {
944  MCOS->EmitIntValue(dwarf::DW_UT_compile, 1);
945  MCOS->EmitIntValue(AddrSize, 1);
946  }
947  // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
948  // it is at the start of that section so this is zero.
949  if (AbbrevSectionSymbol == nullptr)
950  MCOS->EmitIntValue(0, 4);
951  else
952  MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
954  if (context.getDwarfVersion() <= 4)
955  MCOS->EmitIntValue(AddrSize, 1);
956 
957  // Second part: the compile_unit DIE.
958 
959  // The DW_TAG_compile_unit DIE abbrev (1).
960  MCOS->EmitULEB128IntValue(1);
961 
962  // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
963  // which is at the start of that section so this is zero.
964  if (LineSectionSymbol)
965  MCOS->EmitSymbolValue(LineSectionSymbol, 4,
967  else
968  MCOS->EmitIntValue(0, 4);
969 
970  if (RangesSectionSymbol) {
971  // There are multiple sections containing code, so we must use the
972  // .debug_ranges sections.
973 
974  // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
975  // to the address range list for this compilation unit.
976  MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
977  } else {
978  // If we only have one non-empty code section, we can use the simpler
979  // AT_low_pc and AT_high_pc attributes.
980 
981  // Find the first (and only) non-empty text section
982  auto &Sections = context.getGenDwarfSectionSyms();
983  const auto TextSection = Sections.begin();
984  assert(TextSection != Sections.end() && "No text section found");
985 
986  MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
987  MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
988  assert(StartSymbol && "StartSymbol must not be NULL");
989  assert(EndSymbol && "EndSymbol must not be NULL");
990 
991  // AT_low_pc, the first address of the default .text section.
992  const MCExpr *Start = MCSymbolRefExpr::create(
993  StartSymbol, MCSymbolRefExpr::VK_None, context);
994  MCOS->EmitValue(Start, AddrSize);
995 
996  // AT_high_pc, the last address of the default .text section.
997  const MCExpr *End = MCSymbolRefExpr::create(
998  EndSymbol, MCSymbolRefExpr::VK_None, context);
999  MCOS->EmitValue(End, AddrSize);
1000  }
1001 
1002  // AT_name, the name of the source file. Reconstruct from the first directory
1003  // and file table entries.
1004  const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
1005  if (MCDwarfDirs.size() > 0) {
1006  MCOS->EmitBytes(MCDwarfDirs[0]);
1008  }
1009  const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
1010  MCOS->getContext().getMCDwarfFiles();
1011  MCOS->EmitBytes(MCDwarfFiles[1].Name);
1012  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1013 
1014  // AT_comp_dir, the working directory the assembly was done in.
1015  if (!context.getCompilationDir().empty()) {
1016  MCOS->EmitBytes(context.getCompilationDir());
1017  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1018  }
1019 
1020  // AT_APPLE_flags, the command line arguments of the assembler tool.
1021  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
1022  if (!DwarfDebugFlags.empty()){
1023  MCOS->EmitBytes(DwarfDebugFlags);
1024  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1025  }
1026 
1027  // AT_producer, the version of the assembler tool.
1028  StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
1029  if (!DwarfDebugProducer.empty())
1030  MCOS->EmitBytes(DwarfDebugProducer);
1031  else
1032  MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
1033  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1034 
1035  // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
1036  // draft has no standard code for assembler.
1037  MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
1038 
1039  // Third part: the list of label DIEs.
1040 
1041  // Loop on saved info for dwarf labels and create the DIEs for them.
1042  const std::vector<MCGenDwarfLabelEntry> &Entries =
1044  for (const auto &Entry : Entries) {
1045  // The DW_TAG_label DIE abbrev (2).
1046  MCOS->EmitULEB128IntValue(2);
1047 
1048  // AT_name, of the label without any leading underbar.
1049  MCOS->EmitBytes(Entry.getName());
1050  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1051 
1052  // AT_decl_file, index into the file table.
1053  MCOS->EmitIntValue(Entry.getFileNumber(), 4);
1054 
1055  // AT_decl_line, source line number.
1056  MCOS->EmitIntValue(Entry.getLineNumber(), 4);
1057 
1058  // AT_low_pc, start address of the label.
1059  const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
1060  MCSymbolRefExpr::VK_None, context);
1061  MCOS->EmitValue(AT_low_pc, AddrSize);
1062 
1063  // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
1064  MCOS->EmitIntValue(0, 1);
1065 
1066  // The DW_TAG_unspecified_parameters DIE abbrev (3).
1067  MCOS->EmitULEB128IntValue(3);
1068 
1069  // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
1070  MCOS->EmitIntValue(0, 1);
1071  }
1072 
1073  // Add the NULL DIE terminating the Compile Unit DIE's.
1074  MCOS->EmitIntValue(0, 1);
1075 
1076  // Now set the value of the symbol at the end of the info section.
1077  MCOS->EmitLabel(InfoEnd);
1078 }
1079 
1080 // When generating dwarf for assembly source files this emits the data for
1081 // .debug_ranges section. We only emit one range list, which spans all of the
1082 // executable sections of this file.
1083 static void EmitGenDwarfRanges(MCStreamer *MCOS) {
1084  MCContext &context = MCOS->getContext();
1085  auto &Sections = context.getGenDwarfSectionSyms();
1086 
1087  const MCAsmInfo *AsmInfo = context.getAsmInfo();
1088  int AddrSize = AsmInfo->getCodePointerSize();
1089 
1091 
1092  for (MCSection *Sec : Sections) {
1093  const MCSymbol *StartSymbol = Sec->getBeginSymbol();
1094  MCSymbol *EndSymbol = Sec->getEndSymbol(context);
1095  assert(StartSymbol && "StartSymbol must not be NULL");
1096  assert(EndSymbol && "EndSymbol must not be NULL");
1097 
1098  // Emit a base address selection entry for the start of this section
1099  const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
1100  StartSymbol, MCSymbolRefExpr::VK_None, context);
1101  MCOS->emitFill(AddrSize, 0xFF);
1102  MCOS->EmitValue(SectionStartAddr, AddrSize);
1103 
1104  // Emit a range list entry spanning this section
1105  const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
1106  *StartSymbol, *EndSymbol, 0);
1107  MCOS->EmitIntValue(0, AddrSize);
1108  emitAbsValue(*MCOS, SectionSize, AddrSize);
1109  }
1110 
1111  // Emit end of list entry
1112  MCOS->EmitIntValue(0, AddrSize);
1113  MCOS->EmitIntValue(0, AddrSize);
1114 }
1115 
1116 //
1117 // When generating dwarf for assembly source files this emits the Dwarf
1118 // sections.
1119 //
1121  MCContext &context = MCOS->getContext();
1122 
1123  // Create the dwarf sections in this order (.debug_line already created).
1124  const MCAsmInfo *AsmInfo = context.getAsmInfo();
1125  bool CreateDwarfSectionSymbols =
1127  MCSymbol *LineSectionSymbol = nullptr;
1128  if (CreateDwarfSectionSymbols)
1129  LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
1130  MCSymbol *AbbrevSectionSymbol = nullptr;
1131  MCSymbol *InfoSectionSymbol = nullptr;
1132  MCSymbol *RangesSectionSymbol = nullptr;
1133 
1134  // Create end symbols for each section, and remove empty sections
1135  MCOS->getContext().finalizeDwarfSections(*MCOS);
1136 
1137  // If there are no sections to generate debug info for, we don't need
1138  // to do anything
1139  if (MCOS->getContext().getGenDwarfSectionSyms().empty())
1140  return;
1141 
1142  // We only use the .debug_ranges section if we have multiple code sections,
1143  // and we are emitting a DWARF version which supports it.
1144  const bool UseRangesSection =
1145  MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
1146  MCOS->getContext().getDwarfVersion() >= 3;
1147  CreateDwarfSectionSymbols |= UseRangesSection;
1148 
1150  if (CreateDwarfSectionSymbols) {
1151  InfoSectionSymbol = context.createTempSymbol();
1152  MCOS->EmitLabel(InfoSectionSymbol);
1153  }
1155  if (CreateDwarfSectionSymbols) {
1156  AbbrevSectionSymbol = context.createTempSymbol();
1157  MCOS->EmitLabel(AbbrevSectionSymbol);
1158  }
1159  if (UseRangesSection) {
1161  if (CreateDwarfSectionSymbols) {
1162  RangesSectionSymbol = context.createTempSymbol();
1163  MCOS->EmitLabel(RangesSectionSymbol);
1164  }
1165  }
1166 
1167  assert((RangesSectionSymbol != nullptr) || !UseRangesSection);
1168 
1170 
1171  // Output the data for .debug_aranges section.
1172  EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
1173 
1174  if (UseRangesSection)
1175  EmitGenDwarfRanges(MCOS);
1176 
1177  // Output the data for .debug_abbrev section.
1178  EmitGenDwarfAbbrev(MCOS);
1179 
1180  // Output the data for .debug_info section.
1181  EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
1182  RangesSectionSymbol);
1183 }
1184 
1185 //
1186 // When generating dwarf for assembly source files this is called when symbol
1187 // for a label is created. If this symbol is not a temporary and is in the
1188 // section that dwarf is being generated for, save the needed info to create
1189 // a dwarf label.
1190 //
1192  SourceMgr &SrcMgr, SMLoc &Loc) {
1193  // We won't create dwarf labels for temporary symbols.
1194  if (Symbol->isTemporary())
1195  return;
1196  MCContext &context = MCOS->getContext();
1197  // We won't create dwarf labels for symbols in sections that we are not
1198  // generating debug info for.
1199  if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
1200  return;
1201 
1202  // The dwarf label's name does not have the symbol name's leading
1203  // underbar if any.
1204  StringRef Name = Symbol->getName();
1205  if (Name.startswith("_"))
1206  Name = Name.substr(1, Name.size()-1);
1207 
1208  // Get the dwarf file number to be used for the dwarf label.
1209  unsigned FileNumber = context.getGenDwarfFileNumber();
1210 
1211  // Finding the line number is the expensive part which is why we just don't
1212  // pass it in as for some symbols we won't create a dwarf label.
1213  unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
1214  unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
1215 
1216  // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
1217  // values so that they don't have things like an ARM thumb bit from the
1218  // original symbol. So when used they won't get a low bit set after
1219  // relocation.
1220  MCSymbol *Label = context.createTempSymbol();
1221  MCOS->EmitLabel(Label);
1222 
1223  // Create and entry for the info and add it to the other entries.
1225  MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
1226 }
1227 
1228 static int getDataAlignmentFactor(MCStreamer &streamer) {
1229  MCContext &context = streamer.getContext();
1230  const MCAsmInfo *asmInfo = context.getAsmInfo();
1231  int size = asmInfo->getCalleeSaveStackSlotSize();
1232  if (asmInfo->isStackGrowthDirectionUp())
1233  return size;
1234  else
1235  return -size;
1236 }
1237 
1238 static unsigned getSizeForEncoding(MCStreamer &streamer,
1239  unsigned symbolEncoding) {
1240  MCContext &context = streamer.getContext();
1241  unsigned format = symbolEncoding & 0x0f;
1242  switch (format) {
1243  default: llvm_unreachable("Unknown Encoding");
1246  return context.getAsmInfo()->getCodePointerSize();
1249  return 2;
1252  return 4;
1255  return 8;
1256  }
1257 }
1258 
1259 static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
1260  unsigned symbolEncoding, bool isEH) {
1261  MCContext &context = streamer.getContext();
1262  const MCAsmInfo *asmInfo = context.getAsmInfo();
1263  const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
1264  symbolEncoding,
1265  streamer);
1266  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
1267  if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
1268  emitAbsValue(streamer, v, size);
1269  else
1270  streamer.EmitValue(v, size);
1271 }
1272 
1273 static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
1274  unsigned symbolEncoding) {
1275  MCContext &context = streamer.getContext();
1276  const MCAsmInfo *asmInfo = context.getAsmInfo();
1277  const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1278  symbolEncoding,
1279  streamer);
1280  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
1281  streamer.EmitValue(v, size);
1282 }
1283 
1284 namespace {
1285 
1286 class FrameEmitterImpl {
1287  int CFAOffset = 0;
1288  int InitialCFAOffset = 0;
1289  bool IsEH;
1290  MCObjectStreamer &Streamer;
1291 
1292 public:
1293  FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1294  : IsEH(IsEH), Streamer(Streamer) {}
1295 
1296  /// Emit the unwind information in a compact way.
1297  void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
1298 
1299  const MCSymbol &EmitCIE(const MCDwarfFrameInfo &F);
1300  void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
1301  bool LastInSection, const MCSymbol &SectionStart);
1302  void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
1303  MCSymbol *BaseLabel);
1304  void EmitCFIInstruction(const MCCFIInstruction &Instr);
1305 };
1306 
1307 } // end anonymous namespace
1308 
1309 static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
1310  Streamer.EmitIntValue(Encoding, 1);
1311 }
1312 
1313 void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
1314  int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
1315  auto *MRI = Streamer.getContext().getRegisterInfo();
1316 
1317  switch (Instr.getOperation()) {
1319  unsigned Reg1 = Instr.getRegister();
1320  unsigned Reg2 = Instr.getRegister2();
1321  if (!IsEH) {
1322  Reg1 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg1);
1323  Reg2 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg2);
1324  }
1325  Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1326  Streamer.EmitULEB128IntValue(Reg1);
1327  Streamer.EmitULEB128IntValue(Reg2);
1328  return;
1329  }
1331  Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1332  return;
1333 
1335  Streamer.EmitIntValue(dwarf::DW_CFA_AARCH64_negate_ra_state, 1);
1336  return;
1337 
1339  unsigned Reg = Instr.getRegister();
1340  Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1341  Streamer.EmitULEB128IntValue(Reg);
1342  return;
1343  }
1346  const bool IsRelative =
1348 
1349  Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1350 
1351  if (IsRelative)
1352  CFAOffset += Instr.getOffset();
1353  else
1354  CFAOffset = -Instr.getOffset();
1355 
1356  Streamer.EmitULEB128IntValue(CFAOffset);
1357 
1358  return;
1359  }
1361  unsigned Reg = Instr.getRegister();
1362  if (!IsEH)
1363  Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
1364  Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1365  Streamer.EmitULEB128IntValue(Reg);
1366  CFAOffset = -Instr.getOffset();
1367  Streamer.EmitULEB128IntValue(CFAOffset);
1368 
1369  return;
1370  }
1372  unsigned Reg = Instr.getRegister();
1373  if (!IsEH)
1374  Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
1375  Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1376  Streamer.EmitULEB128IntValue(Reg);
1377 
1378  return;
1379  }
1382  const bool IsRelative =
1384 
1385  unsigned Reg = Instr.getRegister();
1386  if (!IsEH)
1387  Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
1388 
1389  int Offset = Instr.getOffset();
1390  if (IsRelative)
1391  Offset -= CFAOffset;
1392  Offset = Offset / dataAlignmentFactor;
1393 
1394  if (Offset < 0) {
1395  Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1396  Streamer.EmitULEB128IntValue(Reg);
1397  Streamer.EmitSLEB128IntValue(Offset);
1398  } else if (Reg < 64) {
1399  Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
1400  Streamer.EmitULEB128IntValue(Offset);
1401  } else {
1402  Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
1403  Streamer.EmitULEB128IntValue(Reg);
1404  Streamer.EmitULEB128IntValue(Offset);
1405  }
1406  return;
1407  }
1409  Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1410  return;
1412  Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1413  return;
1415  unsigned Reg = Instr.getRegister();
1416  Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
1417  Streamer.EmitULEB128IntValue(Reg);
1418  return;
1419  }
1421  unsigned Reg = Instr.getRegister();
1422  if (!IsEH)
1423  Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
1424  if (Reg < 64) {
1425  Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1426  } else {
1427  Streamer.EmitIntValue(dwarf::DW_CFA_restore_extended, 1);
1428  Streamer.EmitULEB128IntValue(Reg);
1429  }
1430  return;
1431  }
1433  Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1434  Streamer.EmitULEB128IntValue(Instr.getOffset());
1435  return;
1436 
1438  Streamer.EmitBytes(Instr.getValues());
1439  return;
1440  }
1441  llvm_unreachable("Unhandled case in switch");
1442 }
1443 
1444 /// Emit frame instructions to describe the layout of the frame.
1445 void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
1446  MCSymbol *BaseLabel) {
1447  for (const MCCFIInstruction &Instr : Instrs) {
1448  MCSymbol *Label = Instr.getLabel();
1449  // Throw out move if the label is invalid.
1450  if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1451 
1452  // Advance row if new location.
1453  if (BaseLabel && Label) {
1454  MCSymbol *ThisSym = Label;
1455  if (ThisSym != BaseLabel) {
1456  Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1457  BaseLabel = ThisSym;
1458  }
1459  }
1460 
1461  EmitCFIInstruction(Instr);
1462  }
1463 }
1464 
1465 /// Emit the unwind information in a compact way.
1466 void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
1467  MCContext &Context = Streamer.getContext();
1468  const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
1469 
1470  // range-start range-length compact-unwind-enc personality-func lsda
1471  // _foo LfooEnd-_foo 0x00000023 0 0
1472  // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1473  //
1474  // .section __LD,__compact_unwind,regular,debug
1475  //
1476  // # compact unwind for _foo
1477  // .quad _foo
1478  // .set L1,LfooEnd-_foo
1479  // .long L1
1480  // .long 0x01010001
1481  // .quad 0
1482  // .quad 0
1483  //
1484  // # compact unwind for _bar
1485  // .quad _bar
1486  // .set L2,LbarEnd-_bar
1487  // .long L2
1488  // .long 0x01020011
1489  // .quad __gxx_personality
1490  // .quad except_tab1
1491 
1492  uint32_t Encoding = Frame.CompactUnwindEncoding;
1493  if (!Encoding) return;
1494  bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
1495 
1496  // The encoding needs to know we have an LSDA.
1497  if (!DwarfEHFrameOnly && Frame.Lsda)
1498  Encoding |= 0x40000000;
1499 
1500  // Range Start
1501  unsigned FDEEncoding = MOFI->getFDEEncoding();
1502  unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
1503  Streamer.EmitSymbolValue(Frame.Begin, Size);
1504 
1505  // Range Length
1506  const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1507  *Frame.End, 0);
1508  emitAbsValue(Streamer, Range, 4);
1509 
1510  // Compact Encoding
1511  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1512  Streamer.EmitIntValue(Encoding, Size);
1513 
1514  // Personality Function
1515  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
1516  if (!DwarfEHFrameOnly && Frame.Personality)
1517  Streamer.EmitSymbolValue(Frame.Personality, Size);
1518  else
1519  Streamer.EmitIntValue(0, Size); // No personality fn
1520 
1521  // LSDA
1522  Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
1523  if (!DwarfEHFrameOnly && Frame.Lsda)
1524  Streamer.EmitSymbolValue(Frame.Lsda, Size);
1525  else
1526  Streamer.EmitIntValue(0, Size); // No LSDA
1527 }
1528 
1529 static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1530  if (IsEH)
1531  return 1;
1532  switch (DwarfVersion) {
1533  case 2:
1534  return 1;
1535  case 3:
1536  return 3;
1537  case 4:
1538  case 5:
1539  return 4;
1540  }
1541  llvm_unreachable("Unknown version");
1542 }
1543 
1544 const MCSymbol &FrameEmitterImpl::EmitCIE(const MCDwarfFrameInfo &Frame) {
1545  MCContext &context = Streamer.getContext();
1546  const MCRegisterInfo *MRI = context.getRegisterInfo();
1547  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
1548 
1549  MCSymbol *sectionStart = context.createTempSymbol();
1550  Streamer.EmitLabel(sectionStart);
1551 
1552  MCSymbol *sectionEnd = context.createTempSymbol();
1553 
1554  // Length
1555  const MCExpr *Length =
1556  MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1557  emitAbsValue(Streamer, Length, 4);
1558 
1559  // CIE ID
1560  unsigned CIE_ID = IsEH ? 0 : -1;
1561  Streamer.EmitIntValue(CIE_ID, 4);
1562 
1563  // Version
1564  uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
1565  Streamer.EmitIntValue(CIEVersion, 1);
1566 
1567  if (IsEH) {
1568  SmallString<8> Augmentation;
1569  Augmentation += "z";
1570  if (Frame.Personality)
1571  Augmentation += "P";
1572  if (Frame.Lsda)
1573  Augmentation += "L";
1574  Augmentation += "R";
1575  if (Frame.IsSignalFrame)
1576  Augmentation += "S";
1577  if (Frame.IsBKeyFrame)
1578  Augmentation += "B";
1579  Streamer.EmitBytes(Augmentation);
1580  }
1581  Streamer.EmitIntValue(0, 1);
1582 
1583  if (CIEVersion >= 4) {
1584  // Address Size
1585  Streamer.EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
1586 
1587  // Segment Descriptor Size
1588  Streamer.EmitIntValue(0, 1);
1589  }
1590 
1591  // Code Alignment Factor
1592  Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
1593 
1594  // Data Alignment Factor
1595  Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
1596 
1597  // Return Address Register
1598  unsigned RAReg = Frame.RAReg;
1599  if (RAReg == static_cast<unsigned>(INT_MAX))
1600  RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
1601 
1602  if (CIEVersion == 1) {
1603  assert(RAReg <= 255 &&
1604  "DWARF 2 encodes return_address_register in one byte");
1605  Streamer.EmitIntValue(RAReg, 1);
1606  } else {
1607  Streamer.EmitULEB128IntValue(RAReg);
1608  }
1609 
1610  // Augmentation Data Length (optional)
1611  unsigned augmentationLength = 0;
1612  if (IsEH) {
1613  if (Frame.Personality) {
1614  // Personality Encoding
1615  augmentationLength += 1;
1616  // Personality
1617  augmentationLength +=
1618  getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
1619  }
1620  if (Frame.Lsda)
1621  augmentationLength += 1;
1622  // Encoding of the FDE pointers
1623  augmentationLength += 1;
1624 
1625  Streamer.EmitULEB128IntValue(augmentationLength);
1626 
1627  // Augmentation Data (optional)
1628  if (Frame.Personality) {
1629  // Personality Encoding
1630  emitEncodingByte(Streamer, Frame.PersonalityEncoding);
1631  // Personality
1632  EmitPersonality(Streamer, *Frame.Personality, Frame.PersonalityEncoding);
1633  }
1634 
1635  if (Frame.Lsda)
1636  emitEncodingByte(Streamer, Frame.LsdaEncoding);
1637 
1638  // Encoding of the FDE pointers
1639  emitEncodingByte(Streamer, MOFI->getFDEEncoding());
1640  }
1641 
1642  // Initial Instructions
1643 
1644  const MCAsmInfo *MAI = context.getAsmInfo();
1645  if (!Frame.IsSimple) {
1646  const std::vector<MCCFIInstruction> &Instructions =
1647  MAI->getInitialFrameState();
1648  EmitCFIInstructions(Instructions, nullptr);
1649  }
1650 
1651  InitialCFAOffset = CFAOffset;
1652 
1653  // Padding
1654  Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getCodePointerSize());
1655 
1656  Streamer.EmitLabel(sectionEnd);
1657  return *sectionStart;
1658 }
1659 
1660 void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1661  const MCDwarfFrameInfo &frame,
1662  bool LastInSection,
1663  const MCSymbol &SectionStart) {
1664  MCContext &context = Streamer.getContext();
1665  MCSymbol *fdeStart = context.createTempSymbol();
1666  MCSymbol *fdeEnd = context.createTempSymbol();
1667  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
1668 
1669  CFAOffset = InitialCFAOffset;
1670 
1671  // Length
1672  const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1673  emitAbsValue(Streamer, Length, 4);
1674 
1675  Streamer.EmitLabel(fdeStart);
1676 
1677  // CIE Pointer
1678  const MCAsmInfo *asmInfo = context.getAsmInfo();
1679  if (IsEH) {
1680  const MCExpr *offset =
1681  MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1682  emitAbsValue(Streamer, offset, 4);
1683  } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
1684  const MCExpr *offset =
1685  MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
1686  emitAbsValue(Streamer, offset, 4);
1687  } else {
1688  Streamer.EmitSymbolValue(&cieStart, 4);
1689  }
1690 
1691  // PC Begin
1692  unsigned PCEncoding =
1694  unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1695  emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
1696 
1697  // PC Range
1698  const MCExpr *Range =
1699  MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1700  emitAbsValue(Streamer, Range, PCSize);
1701 
1702  if (IsEH) {
1703  // Augmentation Data Length
1704  unsigned augmentationLength = 0;
1705 
1706  if (frame.Lsda)
1707  augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
1708 
1709  Streamer.EmitULEB128IntValue(augmentationLength);
1710 
1711  // Augmentation Data
1712  if (frame.Lsda)
1713  emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
1714  }
1715 
1716  // Call Frame Instructions
1717  EmitCFIInstructions(frame.Instructions, frame.Begin);
1718 
1719  // Padding
1720  // The size of a .eh_frame section has to be a multiple of the alignment
1721  // since a null CIE is interpreted as the end. Old systems overaligned
1722  // .eh_frame, so we do too and account for it in the last FDE.
1723  unsigned Align = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
1724  Streamer.EmitValueToAlignment(Align);
1725 
1726  Streamer.EmitLabel(fdeEnd);
1727 }
1728 
1729 namespace {
1730 
1731 struct CIEKey {
1732  static const CIEKey getEmptyKey() {
1733  return CIEKey(nullptr, 0, -1, false, false, static_cast<unsigned>(INT_MAX),
1734  false);
1735  }
1736 
1737  static const CIEKey getTombstoneKey() {
1738  return CIEKey(nullptr, -1, 0, false, false, static_cast<unsigned>(INT_MAX),
1739  false);
1740  }
1741 
1742  CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
1743  unsigned LSDAEncoding, bool IsSignalFrame, bool IsSimple,
1744  unsigned RAReg, bool IsBKeyFrame)
1745  : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
1746  LsdaEncoding(LSDAEncoding), IsSignalFrame(IsSignalFrame),
1747  IsSimple(IsSimple), RAReg(RAReg), IsBKeyFrame(IsBKeyFrame) {}
1748 
1749  explicit CIEKey(const MCDwarfFrameInfo &Frame)
1750  : Personality(Frame.Personality),
1751  PersonalityEncoding(Frame.PersonalityEncoding),
1752  LsdaEncoding(Frame.LsdaEncoding), IsSignalFrame(Frame.IsSignalFrame),
1753  IsSimple(Frame.IsSimple), RAReg(Frame.RAReg),
1754  IsBKeyFrame(Frame.IsBKeyFrame) {}
1755 
1756  const MCSymbol *Personality;
1757  unsigned PersonalityEncoding;
1758  unsigned LsdaEncoding;
1759  bool IsSignalFrame;
1760  bool IsSimple;
1761  unsigned RAReg;
1762  bool IsBKeyFrame;
1763 };
1764 
1765 } // end anonymous namespace
1766 
1767 namespace llvm {
1768 
1769 template <> struct DenseMapInfo<CIEKey> {
1770  static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1771  static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
1772 
1773  static unsigned getHashValue(const CIEKey &Key) {
1774  return static_cast<unsigned>(hash_combine(
1775  Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
1776  Key.IsSignalFrame, Key.IsSimple, Key.RAReg, Key.IsBKeyFrame));
1777  }
1778 
1779  static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1780  return LHS.Personality == RHS.Personality &&
1781  LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1782  LHS.LsdaEncoding == RHS.LsdaEncoding &&
1783  LHS.IsSignalFrame == RHS.IsSignalFrame &&
1784  LHS.IsSimple == RHS.IsSimple && LHS.RAReg == RHS.RAReg &&
1785  LHS.IsBKeyFrame == RHS.IsBKeyFrame;
1786  }
1787 };
1788 
1789 } // end namespace llvm
1790 
1792  bool IsEH) {
1793  Streamer.generateCompactUnwindEncodings(MAB);
1794 
1795  MCContext &Context = Streamer.getContext();
1796  const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
1797  const MCAsmInfo *AsmInfo = Context.getAsmInfo();
1798  FrameEmitterImpl Emitter(IsEH, Streamer);
1799  ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
1800 
1801  // Emit the compact unwind info if available.
1802  bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
1803  if (IsEH && MOFI->getCompactUnwindSection()) {
1804  bool SectionEmitted = false;
1805  for (const MCDwarfFrameInfo &Frame : FrameArray) {
1806  if (Frame.CompactUnwindEncoding == 0) continue;
1807  if (!SectionEmitted) {
1808  Streamer.SwitchSection(MOFI->getCompactUnwindSection());
1809  Streamer.EmitValueToAlignment(AsmInfo->getCodePointerSize());
1810  SectionEmitted = true;
1811  }
1812  NeedsEHFrameSection |=
1813  Frame.CompactUnwindEncoding ==
1815  Emitter.EmitCompactUnwind(Frame);
1816  }
1817  }
1818 
1819  if (!NeedsEHFrameSection) return;
1820 
1821  MCSection &Section =
1822  IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1823  : *MOFI->getDwarfFrameSection();
1824 
1825  Streamer.SwitchSection(&Section);
1826  MCSymbol *SectionStart = Context.createTempSymbol();
1827  Streamer.EmitLabel(SectionStart);
1828 
1830 
1831  const MCSymbol *DummyDebugKey = nullptr;
1832  bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
1833  for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1834  const MCDwarfFrameInfo &Frame = *I;
1835  ++I;
1836  if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
1838  // Don't generate an EH frame if we don't need one. I.e., it's taken care
1839  // of by the compact unwind encoding.
1840  continue;
1841 
1842  CIEKey Key(Frame);
1843  const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1844  if (!CIEStart)
1845  CIEStart = &Emitter.EmitCIE(Frame);
1846 
1847  Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
1848  }
1849 }
1850 
1852  uint64_t AddrDelta) {
1853  MCContext &Context = Streamer.getContext();
1854  SmallString<256> Tmp;
1855  raw_svector_ostream OS(Tmp);
1856  MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
1857  Streamer.EmitBytes(OS.str());
1858 }
1859 
1861  uint64_t AddrDelta,
1862  raw_ostream &OS) {
1863  // Scale the address delta by the minimum instruction length.
1864  AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1865 
1868  if (AddrDelta == 0) {
1869  } else if (isUIntN(6, AddrDelta)) {
1870  uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1871  OS << Opcode;
1872  } else if (isUInt<8>(AddrDelta)) {
1873  OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1874  OS << uint8_t(AddrDelta);
1875  } else if (isUInt<16>(AddrDelta)) {
1876  OS << uint8_t(dwarf::DW_CFA_advance_loc2);
1877  support::endian::write<uint16_t>(OS, AddrDelta, E);
1878  } else {
1879  assert(isUInt<32>(AddrDelta));
1880  OS << uint8_t(dwarf::DW_CFA_advance_loc4);
1881  support::endian::write<uint32_t>(OS, AddrDelta, E);
1882  }
1883 }
static const MCExpr * forceExpAbs(MCStreamer &OS, const MCExpr *Expr)
Definition: MCDwarf.cpp:294
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:292
void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry &E)
Definition: MCContext.h:611
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:348
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static int getDataAlignmentFactor(MCStreamer &streamer)
Definition: MCDwarf.cpp:1228
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:61
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
LLVMContext & Context
static CIEKey getTombstoneKey()
Definition: MCDwarf.cpp:1771
#define DWARF2_FLAG_PROLOGUE_END
Definition: MCDwarf.h:81
static void Make(MCObjectStreamer *MCOS, MCSection *Section)
Definition: MCDwarf.cpp:87
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:322
Optional< StringRef > Source
The source code of the file.
Definition: MCDwarf.h:62
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:158
MCSection * getDwarfLineSection() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
iterator begin() const
Definition: ArrayRef.h:136
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
static bool FixedEncode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS, uint32_t *Offset, uint32_t *Size)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas using fixed length operands...
Definition: MCDwarf.cpp:730
const SmallVectorImpl< MCDwarfFile > & getMCDwarfFiles(unsigned CUID=0)
Definition: MCContext.h:533
SourceMgr SrcMgr
Definition: Error.cpp:23
virtual void EmitBytes(StringRef Data)
Emit the bytes in Data into the output.
unsigned Reg
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:123
bool needsDwarfSectionOffsetDirective() const
Definition: MCAsmInfo.h:466
static void EmitAdvanceLoc(MCObjectStreamer &Streamer, uint64_t AddrDelta)
Definition: MCDwarf.cpp:1851
MCSection * getDwarfLineStrSection() const
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
F(f)
MCSection * getDwarfARangesSection() const
MCDwarfLineStr(MCContext &Ctx)
Construct an instance that can emit .debug_line_str (for use in a normal v5 line table).
Definition: MCDwarf.cpp:57
uint16_t getDwarfVersion() const
Definition: MCContext.h:627
#define op(i)
static void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta)
Utility function to emit the encoding to a streamer.
Definition: MCDwarf.cpp:632
virtual MCSymbol * getDwarfLineTableSymbol(unsigned CUID)
Definition: MCStreamer.cpp:239
bool getSupportsCompactUnwindWithoutEHFrame() const
static void EmitGenDwarfInfo(MCStreamer *MCOS, const MCSymbol *AbbrevSectionSymbol, const MCSymbol *LineSectionSymbol, const MCSymbol *RangesSectionSymbol)
Definition: MCDwarf.cpp:915
unsigned LsdaEncoding
Definition: MCDwarf.h:596
StringRef getDwarfDebugFlags()
Definition: MCContext.h:616
std::vector< MCDwarfLineEntry > MCDwarfLineEntryCollection
Definition: MCDwarf.h:181
static void Encode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Definition: MCDwarf.cpp:648
void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize)
std::vector< MCCFIInstruction > Instructions
Definition: MCDwarf.h:593
const std::vector< MCGenDwarfLabelEntry > & getMCGenDwarfLabelEntries() const
Definition: MCContext.h:607
MCContext & getContext() const
Definition: MCStreamer.h:250
#define DWARF2_FLAG_IS_STMT
Definition: MCDwarf.h:79
amdgpu Simplify well known AMD library false Value Value const Twine & Name
StringRef getDwarfDebugProducer()
Definition: MCContext.h:619
std::string Name
Definition: MCDwarf.h:51
virtual const MCExpr * getExprForPersonalitySymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition: MCAsmInfo.cpp:78
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth...
Definition: ISDOpcodes.h:392
void finalizeDwarfSections(MCStreamer &MCOS)
Remove empty sections from SectionsForRanges, to avoid generating useless debug info for them...
Definition: MCContext.cpp:596
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
#define INT64_MAX
Definition: DataTypes.h:77
MCSymbol * Begin
Definition: MCDwarf.h:589
uint8_t DWARF2LineRange
Range of line offsets in a special line info. opcode.
Definition: MCDwarf.h:207
const MCSymbol * Lsda
Definition: MCDwarf.h:592
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
unsigned getCompactUnwindDwarfEHFrameOnly() const
static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol, unsigned symbolEncoding, bool isEH)
Definition: MCDwarf.cpp:1259
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:578
MCDwarfLineTable & getMCDwarfLineTable(unsigned CUID)
Definition: MCContext.h:523
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
MCSection * getDwarfFrameSection() const
const SmallVectorImpl< std::string > & getMCDwarfDirs(unsigned CUID=0)
Definition: MCContext.h:537
bool hasAggressiveSymbolFolding() const
Definition: MCAsmInfo.h:527
bool getOmitDwarfIfHaveCompactUnwind() const
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Emit an assignment of Value to Symbol.
Definition: MCStreamer.cpp:911
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
Context object for machine code objects.
Definition: MCContext.h:62
Key
PAL metadata keys.
static void EmitGenDwarfAbbrev(MCStreamer *MCOS)
Definition: MCDwarf.cpp:789
void write(raw_ostream &OS) const
Utility for building string tables with deduplicated suffixes.
unsigned getDwarfCompileUnitID()
Definition: MCContext.h:548
size_t add(CachedHashStringRef S)
Add a string to the builder.
const std::map< unsigned, MCDwarfLineTable > & getMCDwarfLineTables() const
Definition: MCContext.h:519
Streaming object file generation interface.
bool doesDwarfUseRelocationsAcrossSections() const
Definition: MCAsmInfo.h:589
Instances of this class represent the information from a dwarf .loc directive.
Definition: MCDwarf.h:67
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
MCSection * getCompactUnwindSection() const
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
virtual void EmitBinaryData(StringRef Data)
Functionally identical to EmitBytes.
unsigned getGenDwarfFileNumber()
Definition: MCContext.h:591
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:123
MCSymbol * getLabel() const
Definition: MCDwarf.h:558
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:154
MCSection * getDwarfAbbrevSection() const
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:152
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:55
static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op)
Given a special op, return the address skip amount (in units of DWARF2_LINE_MIN_INSN_LENGTH).
Definition: MCDwarf.cpp:643
static unsigned getSizeForEncoding(MCStreamer &streamer, unsigned symbolEncoding)
Definition: MCDwarf.cpp:1238
Streaming machine code generation interface.
Definition: MCStreamer.h:188
static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion)
Definition: MCDwarf.cpp:1529
static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form)
Definition: MCDwarf.cpp:782
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:216
uint8_t DWARF2LineOpcodeBase
First special line opcode - leave room for the standard opcodes.
Definition: MCDwarf.h:202
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
constexpr bool isUInt< 8 >(uint64_t x)
Definition: MathExtras.h:342
unsigned const MachineRegisterInfo * MRI
void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
unsigned getRegister2() const
Definition: MCDwarf.h:568
void finalizeInOrder()
Finalize the string table without reording it.
#define DWARF2_FLAG_EPILOGUE_BEGIN
Definition: MCDwarf.h:82
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size)
Definition: MCDwarf.cpp:305
unsigned getFDEEncoding() const
static void EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section, const MCLineSection::MCDwarfLineEntryCollection &LineEntries)
Definition: MCDwarf.cpp:150
int getOffset() const
Definition: MCDwarf.h:573
static void EmitGenDwarfAranges(MCStreamer *MCOS, const MCSymbol *InfoSectionSymbol)
Definition: MCDwarf.cpp:844
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:219
OpType getOperation() const
Definition: MCDwarf.h:557
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:296
#define DWARF2_FLAG_BASIC_BLOCK
Definition: MCDwarf.h:80
bool doDwarfFDESymbolsUseAbsDiff() const
Definition: MCAsmInfo.h:593
const MCDwarfLoc & getCurrentDwarfLoc()
Definition: MCContext.h:587
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:490
unsigned PersonalityEncoding
Definition: MCDwarf.h:595
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
static void emitOneV5FileEntry(MCStreamer *MCOS, const MCDwarfFile &DwarfFile, bool EmitMD5, bool HasSource, Optional< MCDwarfLineStr > &LineStr)
Definition: MCDwarf.cpp:352
StringRef getValues() const
Definition: MCDwarf.h:580
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:52
size_t size() const
Definition: SmallVector.h:52
int8_t DWARF2LineBase
Minimum line offset in a special line info.
Definition: MCDwarf.h:205
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition: Path.cpp:625
void generateCompactUnwindEncodings(MCAsmBackend *MAB)
Definition: MCStreamer.cpp:115
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void clearDwarfLocSeen()
Definition: MCContext.h:584
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:19
void EmitValueToAlignment(unsigned ByteAlignment, 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.
raw_ostream & write(unsigned char C)
StringRef getCompilationDir() const
Get the compilation directory for DW_AT_comp_dir The compilation directory should be set with setComp...
Definition: MCContext.h:486
unsigned getRegister() const
Definition: MCDwarf.h:560
const SetVector< MCSection * > & getGenDwarfSectionSyms()
Definition: MCContext.h:597
const std::vector< MCCFIInstruction > & getInitialFrameState() const
Definition: MCAsmInfo.h:604
void Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params, MCSection *Section) const
Definition: MCDwarf.cpp:260
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1166
static unsigned getHashValue(const CIEKey &Key)
Definition: MCDwarf.cpp:1773
void EmitCU(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params, Optional< MCDwarfLineStr > &LineStr) const
Definition: MCDwarf.cpp:519
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
Expected< unsigned > tryGetFile(StringRef &Directory, StringRef &FileName, MD5::MD5Result *Checksum, Optional< StringRef > Source, unsigned FileNumber=0)
Definition: MCDwarf.cpp:533
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
unsigned getMinInstAlignment() const
Definition: MCAsmInfo.h:477
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:1043
static bool isEqual(const CIEKey &LHS, const CIEKey &RHS)
Definition: MCDwarf.cpp:1779
iterator end() const
Definition: ArrayRef.h:137
MCSymbol * getBeginSymbol()
Definition: MCSection.h:109
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:534
static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, raw_ostream &OS)
Definition: MCDwarf.cpp:1860
MCSection * getDwarfInfoSection() const
uint32_t CompactUnwindEncoding
Definition: MCDwarf.h:597
static void EmitGenDwarfRanges(MCStreamer *MCOS)
Definition: MCDwarf.cpp:1083
bool getDwarfLocSeen()
Definition: MCContext.h:586
This file contains constants used for implementing Dwarf debug support.
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:247
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:345
static CIEKey getEmptyKey()
Definition: MCDwarf.cpp:1770
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:600
static void Emit(MCStreamer *MCOS)
Definition: MCDwarf.cpp:1120
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:470
#define DWARF2_LINE_DEFAULT_IS_STMT
Definition: MCDwarf.h:77
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
Definition: SourceMgr.h:176
bool isStackGrowthDirectionUp() const
True if target stack grow up.
Definition: MCAsmInfo.h:407
static uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta)
Definition: MCDwarf.cpp:71
Instances of this class represent the line information for the dwarf line table entries.
Definition: MCDwarf.h:150
static void Emit(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params)
Definition: MCDwarf.cpp:232
static const MCExpr * MakeStartMinusEndExpr(const MCStreamer &MCOS, const MCSymbol &Start, const MCSymbol &End, int IntVal)
Definition: MCDwarf.cpp:115
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol, unsigned symbolEncoding)
Definition: MCDwarf.cpp:1273
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:589
static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr, SMLoc &Loc)
Definition: MCDwarf.cpp:1191
const MCSymbol * Personality
Definition: MCDwarf.h:591
#define I(x, y, z)
Definition: MD5.cpp:58
static const MCExpr * makeStartPlusIntExpr(MCContext &Ctx, const MCSymbol &Start, int IntVal)
Definition: MCDwarf.cpp:137
unsigned getCalleeSaveStackSlotSize() const
Get the callee-saved register stack slot size in bytes.
Definition: MCAsmInfo.h:399
uint32_t Size
Definition: Profile.cpp:46
bool isLittleEndian() const
True if the target is little endian.
Definition: MCAsmInfo.h:404
Manage the .debug_line_str section contents, if we use it.
Definition: MCDwarf.cpp:49
constexpr bool isUInt< 16 >(uint64_t x)
Definition: MathExtras.h:345
void emitFill(uint64_t NumBytes, uint8_t FillValue)
Emit NumBytes bytes worth of the value specified by FillValue.
Definition: MCStreamer.cpp:195
MD5::MD5Result * Checksum
The MD5 checksum, if there is one.
Definition: MCDwarf.h:58
unsigned getRARegister() const
This method should return the register where the return address can be found.
MCSymbol * endSection(MCSection *Section)
unsigned DirIndex
Definition: MCDwarf.h:54
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition: MCAsmInfo.h:395
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void emitRef(MCStreamer *MCOS, StringRef Path)
Emit a reference to the string.
Definition: MCDwarf.cpp:322
std::array< uint8_t, 16 > Bytes
Definition: MD5.h:56
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:294
LLVM Value Representation.
Definition: Value.h:72
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
ArrayRef< MCDwarfFrameInfo > getDwarfFrameInfos() const
Definition: MCStreamer.h:270
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:346
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:99
static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding)
Definition: MCDwarf.cpp:1309
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Subtraction.
Definition: MCExpr.h:440
void emitSection(MCStreamer *MCOS)
Emit the .debug_line_str section if appropriate.
Definition: MCDwarf.cpp:310
static void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH)
Definition: MCDwarf.cpp:1791
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Instances of this class represent the name of the dwarf .file directive and its associated dwarf file...
Definition: MCDwarf.h:49
MCSection * getDwarfRangesSection() const
void EmitULEB128IntValue(uint64_t Value)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:138
Represents a location in source code.
Definition: SMLoc.h:23
Expected< unsigned > tryGetFile(StringRef &Directory, StringRef &FileName, MD5::MD5Result *Checksum, Optional< StringRef > &Source, unsigned FileNumber=0)
Definition: MCDwarf.cpp:542
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:392
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
virtual const MCExpr * getExprForFDESymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition: MCAsmInfo.cpp:85
std::pair< MCSymbol *, MCSymbol * > Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, Optional< MCDwarfLineStr > &LineStr) const
Definition: MCDwarf.cpp:270
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
void resize(size_type N)
Definition: SmallVector.h:343