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