LLVM  6.0.0svn
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/None.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"
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 static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
49  unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
50  if (MinInsnLength == 1)
51  return AddrDelta;
52  if (AddrDelta % MinInsnLength != 0) {
53  // TODO: report this error, but really only once.
54  ;
55  }
56  return AddrDelta / MinInsnLength;
57 }
58 
59 //
60 // This is called when an instruction is assembled into the specified section
61 // and if there is information from the last .loc directive that has yet to have
62 // a line entry made for it is made.
63 //
65  if (!MCOS->getContext().getDwarfLocSeen())
66  return;
67 
68  // Create a symbol at in the current section for use in the line entry.
69  MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
70  // Set the value of the symbol to use for the MCDwarfLineEntry.
71  MCOS->EmitLabel(LineSym);
72 
73  // Get the current .loc info saved in the context.
74  const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
75 
76  // Create a (local) line entry with the symbol and the current .loc info.
77  MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
78 
79  // clear DwarfLocSeen saying the current .loc info is now used.
80  MCOS->getContext().clearDwarfLocSeen();
81 
82  // Add the line entry to this section's entries.
83  MCOS->getContext()
85  .getMCLineSections()
86  .addLineEntry(LineEntry, Section);
87 }
88 
89 //
90 // This helper routine returns an expression of End - Start + IntVal .
91 //
92 static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
93  const MCSymbol &Start,
94  const MCSymbol &End,
95  int IntVal) {
97  const MCExpr *Res =
98  MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
99  const MCExpr *RHS =
100  MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
101  const MCExpr *Res1 =
103  const MCExpr *Res2 =
104  MCConstantExpr::create(IntVal, MCOS.getContext());
105  const MCExpr *Res3 =
107  return Res3;
108 }
109 
110 //
111 // This emits the Dwarf line table for the specified section from the entries
112 // in the LineSection.
113 //
114 static inline void
116  const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
117  unsigned FileNum = 1;
118  unsigned LastLine = 1;
119  unsigned Column = 0;
120  unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
121  unsigned Isa = 0;
122  unsigned Discriminator = 0;
123  MCSymbol *LastLabel = nullptr;
124 
125  // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
126  for (const MCDwarfLineEntry &LineEntry : LineEntries) {
127  int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
128 
129  if (FileNum != LineEntry.getFileNum()) {
130  FileNum = LineEntry.getFileNum();
131  MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
132  MCOS->EmitULEB128IntValue(FileNum);
133  }
134  if (Column != LineEntry.getColumn()) {
135  Column = LineEntry.getColumn();
136  MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
137  MCOS->EmitULEB128IntValue(Column);
138  }
139  if (Discriminator != LineEntry.getDiscriminator() &&
140  MCOS->getContext().getDwarfVersion() >= 4) {
141  Discriminator = LineEntry.getDiscriminator();
142  unsigned Size = getULEB128Size(Discriminator);
143  MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
144  MCOS->EmitULEB128IntValue(Size + 1);
145  MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
146  MCOS->EmitULEB128IntValue(Discriminator);
147  }
148  if (Isa != LineEntry.getIsa()) {
149  Isa = LineEntry.getIsa();
150  MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
151  MCOS->EmitULEB128IntValue(Isa);
152  }
153  if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
154  Flags = LineEntry.getFlags();
155  MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
156  }
157  if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
158  MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
159  if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
160  MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
161  if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
162  MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
163 
164  MCSymbol *Label = LineEntry.getLabel();
165 
166  // At this point we want to emit/create the sequence to encode the delta in
167  // line numbers and the increment of the address from the previous Label
168  // and the current Label.
169  const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
170  MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
171  asmInfo->getCodePointerSize());
172 
173  Discriminator = 0;
174  LastLine = LineEntry.getLine();
175  LastLabel = Label;
176  }
177 
178  // Emit a DW_LNE_end_sequence for the end of the section.
179  // Use the section end label to compute the address delta and use INT64_MAX
180  // as the line delta which is the signal that this is actually a
181  // DW_LNE_end_sequence.
182  MCSymbol *SectionEnd = MCOS->endSection(Section);
183 
184  // Switch back the dwarf line section, in case endSection had to switch the
185  // section.
186  MCContext &Ctx = MCOS->getContext();
188 
189  const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
190  MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
191  AsmInfo->getCodePointerSize());
192 }
193 
194 //
195 // This emits the Dwarf file and the line tables.
196 //
198  MCDwarfLineTableParams Params) {
199  MCContext &context = MCOS->getContext();
200 
201  auto &LineTables = context.getMCDwarfLineTables();
202 
203  // Bail out early so we don't switch to the debug_line section needlessly and
204  // in doing so create an unnecessary (if empty) section.
205  if (LineTables.empty())
206  return;
207 
208  // Switch to the section where the table will be emitted into.
210 
211  // Handle the rest of the Compile Units.
212  for (const auto &CUIDTablePair : LineTables)
213  CUIDTablePair.second.EmitCU(MCOS, Params);
214 }
215 
217  MCDwarfLineTableParams Params) const {
218  MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second);
219 }
220 
221 std::pair<MCSymbol *, MCSymbol *>
223  MCDwarfLineTableParams Params) const {
224  static const char StandardOpcodeLengths[] = {
225  0, // length of DW_LNS_copy
226  1, // length of DW_LNS_advance_pc
227  1, // length of DW_LNS_advance_line
228  1, // length of DW_LNS_set_file
229  1, // length of DW_LNS_set_column
230  0, // length of DW_LNS_negate_stmt
231  0, // length of DW_LNS_set_basic_block
232  0, // length of DW_LNS_const_add_pc
233  1, // length of DW_LNS_fixed_advance_pc
234  0, // length of DW_LNS_set_prologue_end
235  0, // length of DW_LNS_set_epilogue_begin
236  1 // DW_LNS_set_isa
237  };
238  assert(array_lengthof(StandardOpcodeLengths) >=
239  (Params.DWARF2LineOpcodeBase - 1U));
240  return Emit(MCOS, Params, makeArrayRef(StandardOpcodeLengths,
241  Params.DWARF2LineOpcodeBase - 1));
242 }
243 
244 static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
245  MCContext &Context = OS.getContext();
246  assert(!isa<MCSymbolRefExpr>(Expr));
247  if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
248  return Expr;
249 
250  MCSymbol *ABS = Context.createTempSymbol();
251  OS.EmitAssignment(ABS, Expr);
252  return MCSymbolRefExpr::create(ABS, Context);
253 }
254 
255 static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
256  const MCExpr *ABS = forceExpAbs(OS, Value);
257  OS.EmitValue(ABS, Size);
258 }
259 
260 std::pair<MCSymbol *, MCSymbol *>
262  ArrayRef<char> StandardOpcodeLengths) const {
263  MCContext &context = MCOS->getContext();
264 
265  // Create a symbol at the beginning of the line table.
266  MCSymbol *LineStartSym = Label;
267  if (!LineStartSym)
268  LineStartSym = context.createTempSymbol();
269  // Set the value of the symbol, as we are at the start of the line table.
270  MCOS->EmitLabel(LineStartSym);
271 
272  // Create a symbol for the end of the section (to be set when we get there).
273  MCSymbol *LineEndSym = context.createTempSymbol();
274 
275  // The first 4 bytes is the total length of the information for this
276  // compilation unit (not including these 4 bytes for the length).
277  emitAbsValue(*MCOS,
278  MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
279 
280  // Next 2 bytes is the Version, which is Dwarf 2.
281  MCOS->EmitIntValue(2, 2);
282 
283  // Create a symbol for the end of the prologue (to be set when we get there).
284  MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
285 
286  // Length of the prologue, is the next 4 bytes. Which is the start of the
287  // section to the end of the prologue. Not including the 4 bytes for the
288  // total length, the 2 bytes for the version, and these 4 bytes for the
289  // length of the prologue.
290  emitAbsValue(
291  *MCOS,
292  MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, (4 + 2 + 4)), 4);
293 
294  // Parameters of the state machine, are next.
295  MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
297  MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
298  MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
299  MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
300 
301  // Standard opcode lengths
302  for (char Length : StandardOpcodeLengths)
303  MCOS->EmitIntValue(Length, 1);
304 
305  // Put out the directory and file tables.
306 
307  // First the directory table.
308  for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
309  MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
310  MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
311  }
312  MCOS->EmitIntValue(0, 1); // Terminate the directory list
313 
314  // Second the file table.
315  for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
316  assert(!MCDwarfFiles[i].Name.empty());
317  MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
318  MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
319  // the Directory num
320  MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
321  MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
322  MCOS->EmitIntValue(0, 1); // filesize (always 0)
323  }
324  MCOS->EmitIntValue(0, 1); // Terminate the file list
325 
326  // This is the end of the prologue, so set the value of the symbol at the
327  // end of the prologue (that was used in a previous expression).
328  MCOS->EmitLabel(ProEndSym);
329 
330  return std::make_pair(LineStartSym, LineEndSym);
331 }
332 
334  MCDwarfLineTableParams Params) const {
335  MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second;
336 
337  // Put out the line tables.
338  for (const auto &LineSec : MCLineSections.getMCLineEntries())
339  EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
340 
341  // This is the end of the section, so set the value of the symbol at the end
342  // of this section (that was used in a previous expression).
343  MCOS->EmitLabel(LineEndSym);
344 }
345 
346 unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
347  unsigned FileNumber) {
348  return Header.getFile(Directory, FileName, FileNumber);
349 }
350 
352  StringRef &FileName,
353  unsigned FileNumber) {
354  if (Directory == CompilationDir)
355  Directory = "";
356  if (FileName.empty()) {
357  FileName = "<stdin>";
358  Directory = "";
359  }
360  assert(!FileName.empty());
361  if (FileNumber == 0) {
362  // File numbers start with 1 and/or after any file numbers
363  // allocated by inline-assembler .file directives.
364  FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
365  SmallString<256> Buffer;
366  auto IterBool = SourceIdMap.insert(
367  std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
368  FileNumber));
369  if (!IterBool.second)
370  return IterBool.first->second;
371  }
372  // Make space for this FileNumber in the MCDwarfFiles vector if needed.
373  MCDwarfFiles.resize(FileNumber + 1);
374 
375  // Get the new MCDwarfFile slot for this FileNumber.
376  MCDwarfFile &File = MCDwarfFiles[FileNumber];
377 
378  // It is an error to use see the same number more than once.
379  if (!File.Name.empty())
380  return 0;
381 
382  if (Directory.empty()) {
383  // Separate the directory part from the basename of the FileName.
384  StringRef tFileName = sys::path::filename(FileName);
385  if (!tFileName.empty()) {
386  Directory = sys::path::parent_path(FileName);
387  if (!Directory.empty())
388  FileName = tFileName;
389  }
390  }
391 
392  // Find or make an entry in the MCDwarfDirs vector for this Directory.
393  // Capture directory name.
394  unsigned DirIndex;
395  if (Directory.empty()) {
396  // For FileNames with no directories a DirIndex of 0 is used.
397  DirIndex = 0;
398  } else {
399  DirIndex = 0;
400  for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
401  if (Directory == MCDwarfDirs[DirIndex])
402  break;
403  }
404  if (DirIndex >= MCDwarfDirs.size())
405  MCDwarfDirs.push_back(Directory);
406  // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
407  // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
408  // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
409  // are stored at MCDwarfFiles[FileNumber].Name .
410  DirIndex++;
411  }
412 
413  File.Name = FileName;
414  File.DirIndex = DirIndex;
415 
416  // return the allocated FileNumber.
417  return FileNumber;
418 }
419 
420 /// Utility function to emit the encoding to a streamer.
422  int64_t LineDelta, uint64_t AddrDelta) {
423  MCContext &Context = MCOS->getContext();
424  SmallString<256> Tmp;
425  raw_svector_ostream OS(Tmp);
426  MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
427  MCOS->EmitBytes(OS.str());
428 }
429 
430 /// Given a special op, return the address skip amount (in units of
431 /// DWARF2_LINE_MIN_INSN_LENGTH).
432 static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
433  return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
434 }
435 
436 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
438  int64_t LineDelta, uint64_t AddrDelta,
439  raw_ostream &OS) {
440  uint64_t Temp, Opcode;
441  bool NeedCopy = false;
442 
443  // The maximum address skip amount that can be encoded with a special op.
444  uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
445 
446  // Scale the address delta by the minimum instruction length.
447  AddrDelta = ScaleAddrDelta(Context, AddrDelta);
448 
449  // A LineDelta of INT64_MAX is a signal that this is actually a
450  // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
451  // end_sequence to emit the matrix entry.
452  if (LineDelta == INT64_MAX) {
453  if (AddrDelta == MaxSpecialAddrDelta)
454  OS << char(dwarf::DW_LNS_const_add_pc);
455  else if (AddrDelta) {
456  OS << char(dwarf::DW_LNS_advance_pc);
457  encodeULEB128(AddrDelta, OS);
458  }
459  OS << char(dwarf::DW_LNS_extended_op);
460  OS << char(1);
461  OS << char(dwarf::DW_LNE_end_sequence);
462  return;
463  }
464 
465  // Bias the line delta by the base.
466  Temp = LineDelta - Params.DWARF2LineBase;
467 
468  // If the line increment is out of range of a special opcode, we must encode
469  // it with DW_LNS_advance_line.
470  if (Temp >= Params.DWARF2LineRange ||
471  Temp + Params.DWARF2LineOpcodeBase > 255) {
472  OS << char(dwarf::DW_LNS_advance_line);
473  encodeSLEB128(LineDelta, OS);
474 
475  LineDelta = 0;
476  Temp = 0 - Params.DWARF2LineBase;
477  NeedCopy = true;
478  }
479 
480  // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
481  if (LineDelta == 0 && AddrDelta == 0) {
482  OS << char(dwarf::DW_LNS_copy);
483  return;
484  }
485 
486  // Bias the opcode by the special opcode base.
487  Temp += Params.DWARF2LineOpcodeBase;
488 
489  // Avoid overflow when addr_delta is large.
490  if (AddrDelta < 256 + MaxSpecialAddrDelta) {
491  // Try using a special opcode.
492  Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
493  if (Opcode <= 255) {
494  OS << char(Opcode);
495  return;
496  }
497 
498  // Try using DW_LNS_const_add_pc followed by special op.
499  Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
500  if (Opcode <= 255) {
501  OS << char(dwarf::DW_LNS_const_add_pc);
502  OS << char(Opcode);
503  return;
504  }
505  }
506 
507  // Otherwise use DW_LNS_advance_pc.
508  OS << char(dwarf::DW_LNS_advance_pc);
509  encodeULEB128(AddrDelta, OS);
510 
511  if (NeedCopy)
512  OS << char(dwarf::DW_LNS_copy);
513  else {
514  assert(Temp <= 255 && "Buggy special opcode encoding.");
515  OS << char(Temp);
516  }
517 }
518 
519 // Utility function to write a tuple for .debug_abbrev.
520 static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
521  MCOS->EmitULEB128IntValue(Name);
522  MCOS->EmitULEB128IntValue(Form);
523 }
524 
525 // When generating dwarf for assembly source files this emits
526 // the data for .debug_abbrev section which contains three DIEs.
527 static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
528  MCContext &context = MCOS->getContext();
530 
531  // DW_TAG_compile_unit DIE abbrev (1).
532  MCOS->EmitULEB128IntValue(1);
533  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
535  EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
536  ? dwarf::DW_FORM_sec_offset
537  : dwarf::DW_FORM_data4);
538  if (context.getGenDwarfSectionSyms().size() > 1 &&
539  context.getDwarfVersion() >= 3) {
540  EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
541  ? dwarf::DW_FORM_sec_offset
542  : dwarf::DW_FORM_data4);
543  } else {
544  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
545  EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
546  }
547  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
548  if (!context.getCompilationDir().empty())
549  EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
550  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
551  if (!DwarfDebugFlags.empty())
552  EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
553  EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
554  EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
555  EmitAbbrev(MCOS, 0, 0);
556 
557  // DW_TAG_label DIE abbrev (2).
558  MCOS->EmitULEB128IntValue(2);
559  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
561  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
562  EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
563  EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
564  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
565  EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
566  EmitAbbrev(MCOS, 0, 0);
567 
568  // DW_TAG_unspecified_parameters DIE abbrev (3).
569  MCOS->EmitULEB128IntValue(3);
570  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
572  EmitAbbrev(MCOS, 0, 0);
573 
574  // Terminate the abbreviations for this compilation unit.
575  MCOS->EmitIntValue(0, 1);
576 }
577 
578 // When generating dwarf for assembly source files this emits the data for
579 // .debug_aranges section. This section contains a header and a table of pairs
580 // of PointerSize'ed values for the address and size of section(s) with line
581 // table entries.
582 static void EmitGenDwarfAranges(MCStreamer *MCOS,
583  const MCSymbol *InfoSectionSymbol) {
584  MCContext &context = MCOS->getContext();
585 
586  auto &Sections = context.getGenDwarfSectionSyms();
587 
589 
590  // This will be the length of the .debug_aranges section, first account for
591  // the size of each item in the header (see below where we emit these items).
592  int Length = 4 + 2 + 4 + 1 + 1;
593 
594  // Figure the padding after the header before the table of address and size
595  // pairs who's values are PointerSize'ed.
596  const MCAsmInfo *asmInfo = context.getAsmInfo();
597  int AddrSize = asmInfo->getCodePointerSize();
598  int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
599  if (Pad == 2 * AddrSize)
600  Pad = 0;
601  Length += Pad;
602 
603  // Add the size of the pair of PointerSize'ed values for the address and size
604  // of each section we have in the table.
605  Length += 2 * AddrSize * Sections.size();
606  // And the pair of terminating zeros.
607  Length += 2 * AddrSize;
608 
609  // Emit the header for this section.
610  // The 4 byte length not including the 4 byte value for the length.
611  MCOS->EmitIntValue(Length - 4, 4);
612  // The 2 byte version, which is 2.
613  MCOS->EmitIntValue(2, 2);
614  // The 4 byte offset to the compile unit in the .debug_info from the start
615  // of the .debug_info.
616  if (InfoSectionSymbol)
617  MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
619  else
620  MCOS->EmitIntValue(0, 4);
621  // The 1 byte size of an address.
622  MCOS->EmitIntValue(AddrSize, 1);
623  // The 1 byte size of a segment descriptor, we use a value of zero.
624  MCOS->EmitIntValue(0, 1);
625  // Align the header with the padding if needed, before we put out the table.
626  for(int i = 0; i < Pad; i++)
627  MCOS->EmitIntValue(0, 1);
628 
629  // Now emit the table of pairs of PointerSize'ed values for the section
630  // addresses and sizes.
631  for (MCSection *Sec : Sections) {
632  const MCSymbol *StartSymbol = Sec->getBeginSymbol();
633  MCSymbol *EndSymbol = Sec->getEndSymbol(context);
634  assert(StartSymbol && "StartSymbol must not be NULL");
635  assert(EndSymbol && "EndSymbol must not be NULL");
636 
637  const MCExpr *Addr = MCSymbolRefExpr::create(
638  StartSymbol, MCSymbolRefExpr::VK_None, context);
639  const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
640  *StartSymbol, *EndSymbol, 0);
641  MCOS->EmitValue(Addr, AddrSize);
642  emitAbsValue(*MCOS, Size, AddrSize);
643  }
644 
645  // And finally the pair of terminating zeros.
646  MCOS->EmitIntValue(0, AddrSize);
647  MCOS->EmitIntValue(0, AddrSize);
648 }
649 
650 // When generating dwarf for assembly source files this emits the data for
651 // .debug_info section which contains three parts. The header, the compile_unit
652 // DIE and a list of label DIEs.
653 static void EmitGenDwarfInfo(MCStreamer *MCOS,
654  const MCSymbol *AbbrevSectionSymbol,
655  const MCSymbol *LineSectionSymbol,
656  const MCSymbol *RangesSectionSymbol) {
657  MCContext &context = MCOS->getContext();
658 
660 
661  // Create a symbol at the start and end of this section used in here for the
662  // expression to calculate the length in the header.
663  MCSymbol *InfoStart = context.createTempSymbol();
664  MCOS->EmitLabel(InfoStart);
665  MCSymbol *InfoEnd = context.createTempSymbol();
666 
667  // First part: the header.
668 
669  // The 4 byte total length of the information for this compilation unit, not
670  // including these 4 bytes.
671  const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
672  emitAbsValue(*MCOS, Length, 4);
673 
674  // The 2 byte DWARF version.
675  MCOS->EmitIntValue(context.getDwarfVersion(), 2);
676 
677  // The DWARF v5 header has unit type, address size, abbrev offset.
678  // Earlier versions have abbrev offset, address size.
679  const MCAsmInfo &AsmInfo = *context.getAsmInfo();
680  int AddrSize = AsmInfo.getCodePointerSize();
681  if (context.getDwarfVersion() >= 5) {
682  MCOS->EmitIntValue(dwarf::DW_UT_compile, 1);
683  MCOS->EmitIntValue(AddrSize, 1);
684  }
685  // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
686  // it is at the start of that section so this is zero.
687  if (AbbrevSectionSymbol == nullptr)
688  MCOS->EmitIntValue(0, 4);
689  else
690  MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
692  if (context.getDwarfVersion() <= 4)
693  MCOS->EmitIntValue(AddrSize, 1);
694 
695  // Second part: the compile_unit DIE.
696 
697  // The DW_TAG_compile_unit DIE abbrev (1).
698  MCOS->EmitULEB128IntValue(1);
699 
700  // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
701  // which is at the start of that section so this is zero.
702  if (LineSectionSymbol)
703  MCOS->EmitSymbolValue(LineSectionSymbol, 4,
705  else
706  MCOS->EmitIntValue(0, 4);
707 
708  if (RangesSectionSymbol) {
709  // There are multiple sections containing code, so we must use the
710  // .debug_ranges sections.
711 
712  // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
713  // to the address range list for this compilation unit.
714  MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
715  } else {
716  // If we only have one non-empty code section, we can use the simpler
717  // AT_low_pc and AT_high_pc attributes.
718 
719  // Find the first (and only) non-empty text section
720  auto &Sections = context.getGenDwarfSectionSyms();
721  const auto TextSection = Sections.begin();
722  assert(TextSection != Sections.end() && "No text section found");
723 
724  MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
725  MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
726  assert(StartSymbol && "StartSymbol must not be NULL");
727  assert(EndSymbol && "EndSymbol must not be NULL");
728 
729  // AT_low_pc, the first address of the default .text section.
730  const MCExpr *Start = MCSymbolRefExpr::create(
731  StartSymbol, MCSymbolRefExpr::VK_None, context);
732  MCOS->EmitValue(Start, AddrSize);
733 
734  // AT_high_pc, the last address of the default .text section.
736  EndSymbol, MCSymbolRefExpr::VK_None, context);
737  MCOS->EmitValue(End, AddrSize);
738  }
739 
740  // AT_name, the name of the source file. Reconstruct from the first directory
741  // and file table entries.
742  const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
743  if (MCDwarfDirs.size() > 0) {
744  MCOS->EmitBytes(MCDwarfDirs[0]);
746  }
747  const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
748  MCOS->getContext().getMCDwarfFiles();
749  MCOS->EmitBytes(MCDwarfFiles[1].Name);
750  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
751 
752  // AT_comp_dir, the working directory the assembly was done in.
753  if (!context.getCompilationDir().empty()) {
754  MCOS->EmitBytes(context.getCompilationDir());
755  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
756  }
757 
758  // AT_APPLE_flags, the command line arguments of the assembler tool.
759  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
760  if (!DwarfDebugFlags.empty()){
761  MCOS->EmitBytes(DwarfDebugFlags);
762  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
763  }
764 
765  // AT_producer, the version of the assembler tool.
766  StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
767  if (!DwarfDebugProducer.empty())
768  MCOS->EmitBytes(DwarfDebugProducer);
769  else
770  MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
771  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
772 
773  // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
774  // draft has no standard code for assembler.
775  MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
776 
777  // Third part: the list of label DIEs.
778 
779  // Loop on saved info for dwarf labels and create the DIEs for them.
780  const std::vector<MCGenDwarfLabelEntry> &Entries =
782  for (const auto &Entry : Entries) {
783  // The DW_TAG_label DIE abbrev (2).
784  MCOS->EmitULEB128IntValue(2);
785 
786  // AT_name, of the label without any leading underbar.
787  MCOS->EmitBytes(Entry.getName());
788  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
789 
790  // AT_decl_file, index into the file table.
791  MCOS->EmitIntValue(Entry.getFileNumber(), 4);
792 
793  // AT_decl_line, source line number.
794  MCOS->EmitIntValue(Entry.getLineNumber(), 4);
795 
796  // AT_low_pc, start address of the label.
797  const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
798  MCSymbolRefExpr::VK_None, context);
799  MCOS->EmitValue(AT_low_pc, AddrSize);
800 
801  // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
802  MCOS->EmitIntValue(0, 1);
803 
804  // The DW_TAG_unspecified_parameters DIE abbrev (3).
805  MCOS->EmitULEB128IntValue(3);
806 
807  // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
808  MCOS->EmitIntValue(0, 1);
809  }
810 
811  // Add the NULL DIE terminating the Compile Unit DIE's.
812  MCOS->EmitIntValue(0, 1);
813 
814  // Now set the value of the symbol at the end of the info section.
815  MCOS->EmitLabel(InfoEnd);
816 }
817 
818 // When generating dwarf for assembly source files this emits the data for
819 // .debug_ranges section. We only emit one range list, which spans all of the
820 // executable sections of this file.
821 static void EmitGenDwarfRanges(MCStreamer *MCOS) {
822  MCContext &context = MCOS->getContext();
823  auto &Sections = context.getGenDwarfSectionSyms();
824 
825  const MCAsmInfo *AsmInfo = context.getAsmInfo();
826  int AddrSize = AsmInfo->getCodePointerSize();
827 
829 
830  for (MCSection *Sec : Sections) {
831  const MCSymbol *StartSymbol = Sec->getBeginSymbol();
832  MCSymbol *EndSymbol = Sec->getEndSymbol(context);
833  assert(StartSymbol && "StartSymbol must not be NULL");
834  assert(EndSymbol && "EndSymbol must not be NULL");
835 
836  // Emit a base address selection entry for the start of this section
837  const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
838  StartSymbol, MCSymbolRefExpr::VK_None, context);
839  MCOS->emitFill(AddrSize, 0xFF);
840  MCOS->EmitValue(SectionStartAddr, AddrSize);
841 
842  // Emit a range list entry spanning this section
844  *StartSymbol, *EndSymbol, 0);
845  MCOS->EmitIntValue(0, AddrSize);
846  emitAbsValue(*MCOS, SectionSize, AddrSize);
847  }
848 
849  // Emit end of list entry
850  MCOS->EmitIntValue(0, AddrSize);
851  MCOS->EmitIntValue(0, AddrSize);
852 }
853 
854 //
855 // When generating dwarf for assembly source files this emits the Dwarf
856 // sections.
857 //
859  MCContext &context = MCOS->getContext();
860 
861  // Create the dwarf sections in this order (.debug_line already created).
862  const MCAsmInfo *AsmInfo = context.getAsmInfo();
863  bool CreateDwarfSectionSymbols =
865  MCSymbol *LineSectionSymbol = nullptr;
866  if (CreateDwarfSectionSymbols)
867  LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
868  MCSymbol *AbbrevSectionSymbol = nullptr;
869  MCSymbol *InfoSectionSymbol = nullptr;
870  MCSymbol *RangesSectionSymbol = nullptr;
871 
872  // Create end symbols for each section, and remove empty sections
873  MCOS->getContext().finalizeDwarfSections(*MCOS);
874 
875  // If there are no sections to generate debug info for, we don't need
876  // to do anything
877  if (MCOS->getContext().getGenDwarfSectionSyms().empty())
878  return;
879 
880  // We only use the .debug_ranges section if we have multiple code sections,
881  // and we are emitting a DWARF version which supports it.
882  const bool UseRangesSection =
883  MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
884  MCOS->getContext().getDwarfVersion() >= 3;
885  CreateDwarfSectionSymbols |= UseRangesSection;
886 
888  if (CreateDwarfSectionSymbols) {
889  InfoSectionSymbol = context.createTempSymbol();
890  MCOS->EmitLabel(InfoSectionSymbol);
891  }
893  if (CreateDwarfSectionSymbols) {
894  AbbrevSectionSymbol = context.createTempSymbol();
895  MCOS->EmitLabel(AbbrevSectionSymbol);
896  }
897  if (UseRangesSection) {
899  if (CreateDwarfSectionSymbols) {
900  RangesSectionSymbol = context.createTempSymbol();
901  MCOS->EmitLabel(RangesSectionSymbol);
902  }
903  }
904 
905  assert((RangesSectionSymbol != nullptr) || !UseRangesSection);
906 
908 
909  // Output the data for .debug_aranges section.
910  EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
911 
912  if (UseRangesSection)
913  EmitGenDwarfRanges(MCOS);
914 
915  // Output the data for .debug_abbrev section.
916  EmitGenDwarfAbbrev(MCOS);
917 
918  // Output the data for .debug_info section.
919  EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
920  RangesSectionSymbol);
921 }
922 
923 //
924 // When generating dwarf for assembly source files this is called when symbol
925 // for a label is created. If this symbol is not a temporary and is in the
926 // section that dwarf is being generated for, save the needed info to create
927 // a dwarf label.
928 //
930  SourceMgr &SrcMgr, SMLoc &Loc) {
931  // We won't create dwarf labels for temporary symbols.
932  if (Symbol->isTemporary())
933  return;
934  MCContext &context = MCOS->getContext();
935  // We won't create dwarf labels for symbols in sections that we are not
936  // generating debug info for.
937  if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
938  return;
939 
940  // The dwarf label's name does not have the symbol name's leading
941  // underbar if any.
942  StringRef Name = Symbol->getName();
943  if (Name.startswith("_"))
944  Name = Name.substr(1, Name.size()-1);
945 
946  // Get the dwarf file number to be used for the dwarf label.
947  unsigned FileNumber = context.getGenDwarfFileNumber();
948 
949  // Finding the line number is the expensive part which is why we just don't
950  // pass it in as for some symbols we won't create a dwarf label.
951  unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
952  unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
953 
954  // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
955  // values so that they don't have things like an ARM thumb bit from the
956  // original symbol. So when used they won't get a low bit set after
957  // relocation.
958  MCSymbol *Label = context.createTempSymbol();
959  MCOS->EmitLabel(Label);
960 
961  // Create and entry for the info and add it to the other entries.
963  MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
964 }
965 
966 static int getDataAlignmentFactor(MCStreamer &streamer) {
967  MCContext &context = streamer.getContext();
968  const MCAsmInfo *asmInfo = context.getAsmInfo();
969  int size = asmInfo->getCalleeSaveStackSlotSize();
970  if (asmInfo->isStackGrowthDirectionUp())
971  return size;
972  else
973  return -size;
974 }
975 
976 static unsigned getSizeForEncoding(MCStreamer &streamer,
977  unsigned symbolEncoding) {
978  MCContext &context = streamer.getContext();
979  unsigned format = symbolEncoding & 0x0f;
980  switch (format) {
981  default: llvm_unreachable("Unknown Encoding");
984  return context.getAsmInfo()->getCodePointerSize();
987  return 2;
990  return 4;
993  return 8;
994  }
995 }
996 
997 static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
998  unsigned symbolEncoding, bool isEH) {
999  MCContext &context = streamer.getContext();
1000  const MCAsmInfo *asmInfo = context.getAsmInfo();
1001  const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
1002  symbolEncoding,
1003  streamer);
1004  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
1005  if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
1006  emitAbsValue(streamer, v, size);
1007  else
1008  streamer.EmitValue(v, size);
1009 }
1010 
1011 static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
1012  unsigned symbolEncoding) {
1013  MCContext &context = streamer.getContext();
1014  const MCAsmInfo *asmInfo = context.getAsmInfo();
1015  const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1016  symbolEncoding,
1017  streamer);
1018  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
1019  streamer.EmitValue(v, size);
1020 }
1021 
1022 namespace {
1023 
1024 class FrameEmitterImpl {
1025  int CFAOffset = 0;
1026  int InitialCFAOffset = 0;
1027  bool IsEH;
1028  MCObjectStreamer &Streamer;
1029 
1030 public:
1031  FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1032  : IsEH(IsEH), Streamer(Streamer) {}
1033 
1034  /// Emit the unwind information in a compact way.
1035  void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
1036 
1037  const MCSymbol &EmitCIE(const MCSymbol *personality,
1038  unsigned personalityEncoding, const MCSymbol *lsda,
1039  bool IsSignalFrame, unsigned lsdaEncoding,
1040  bool IsSimple, unsigned RAReg);
1041  void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
1042  bool LastInSection, const MCSymbol &SectionStart);
1043  void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
1044  MCSymbol *BaseLabel);
1045  void EmitCFIInstruction(const MCCFIInstruction &Instr);
1046 };
1047 
1048 } // end anonymous namespace
1049 
1050 static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
1051  Streamer.EmitIntValue(Encoding, 1);
1052 }
1053 
1054 void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
1055  int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
1056  auto *MRI = Streamer.getContext().getRegisterInfo();
1057 
1058  switch (Instr.getOperation()) {
1060  unsigned Reg1 = Instr.getRegister();
1061  unsigned Reg2 = Instr.getRegister2();
1062  if (!IsEH) {
1063  Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1064  Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1065  }
1066  Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1067  Streamer.EmitULEB128IntValue(Reg1);
1068  Streamer.EmitULEB128IntValue(Reg2);
1069  return;
1070  }
1072  Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1073  return;
1074 
1076  unsigned Reg = Instr.getRegister();
1077  Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1078  Streamer.EmitULEB128IntValue(Reg);
1079  return;
1080  }
1083  const bool IsRelative =
1085 
1086  Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1087 
1088  if (IsRelative)
1089  CFAOffset += Instr.getOffset();
1090  else
1091  CFAOffset = -Instr.getOffset();
1092 
1093  Streamer.EmitULEB128IntValue(CFAOffset);
1094 
1095  return;
1096  }
1098  unsigned Reg = Instr.getRegister();
1099  if (!IsEH)
1100  Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1101  Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1102  Streamer.EmitULEB128IntValue(Reg);
1103  CFAOffset = -Instr.getOffset();
1104  Streamer.EmitULEB128IntValue(CFAOffset);
1105 
1106  return;
1107  }
1109  unsigned Reg = Instr.getRegister();
1110  if (!IsEH)
1111  Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1112  Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1113  Streamer.EmitULEB128IntValue(Reg);
1114 
1115  return;
1116  }
1119  const bool IsRelative =
1121 
1122  unsigned Reg = Instr.getRegister();
1123  if (!IsEH)
1124  Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1125 
1126  int Offset = Instr.getOffset();
1127  if (IsRelative)
1128  Offset -= CFAOffset;
1129  Offset = Offset / dataAlignmentFactor;
1130 
1131  if (Offset < 0) {
1132  Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1133  Streamer.EmitULEB128IntValue(Reg);
1134  Streamer.EmitSLEB128IntValue(Offset);
1135  } else if (Reg < 64) {
1136  Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
1137  Streamer.EmitULEB128IntValue(Offset);
1138  } else {
1139  Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
1140  Streamer.EmitULEB128IntValue(Reg);
1141  Streamer.EmitULEB128IntValue(Offset);
1142  }
1143  return;
1144  }
1146  Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1147  return;
1149  Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1150  return;
1152  unsigned Reg = Instr.getRegister();
1153  Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
1154  Streamer.EmitULEB128IntValue(Reg);
1155  return;
1156  }
1158  unsigned Reg = Instr.getRegister();
1159  if (!IsEH)
1160  Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1161  Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1162  return;
1163  }
1165  Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1166  Streamer.EmitULEB128IntValue(Instr.getOffset());
1167  return;
1168 
1170  Streamer.EmitBytes(Instr.getValues());
1171  return;
1172  }
1173  llvm_unreachable("Unhandled case in switch");
1174 }
1175 
1176 /// Emit frame instructions to describe the layout of the frame.
1177 void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
1178  MCSymbol *BaseLabel) {
1179  for (const MCCFIInstruction &Instr : Instrs) {
1180  MCSymbol *Label = Instr.getLabel();
1181  // Throw out move if the label is invalid.
1182  if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1183 
1184  // Advance row if new location.
1185  if (BaseLabel && Label) {
1186  MCSymbol *ThisSym = Label;
1187  if (ThisSym != BaseLabel) {
1188  Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1189  BaseLabel = ThisSym;
1190  }
1191  }
1192 
1193  EmitCFIInstruction(Instr);
1194  }
1195 }
1196 
1197 /// Emit the unwind information in a compact way.
1198 void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
1199  MCContext &Context = Streamer.getContext();
1200  const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
1201 
1202  // range-start range-length compact-unwind-enc personality-func lsda
1203  // _foo LfooEnd-_foo 0x00000023 0 0
1204  // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1205  //
1206  // .section __LD,__compact_unwind,regular,debug
1207  //
1208  // # compact unwind for _foo
1209  // .quad _foo
1210  // .set L1,LfooEnd-_foo
1211  // .long L1
1212  // .long 0x01010001
1213  // .quad 0
1214  // .quad 0
1215  //
1216  // # compact unwind for _bar
1217  // .quad _bar
1218  // .set L2,LbarEnd-_bar
1219  // .long L2
1220  // .long 0x01020011
1221  // .quad __gxx_personality
1222  // .quad except_tab1
1223 
1224  uint32_t Encoding = Frame.CompactUnwindEncoding;
1225  if (!Encoding) return;
1226  bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
1227 
1228  // The encoding needs to know we have an LSDA.
1229  if (!DwarfEHFrameOnly && Frame.Lsda)
1230  Encoding |= 0x40000000;
1231 
1232  // Range Start
1233  unsigned FDEEncoding = MOFI->getFDEEncoding();
1234  unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
1235  Streamer.EmitSymbolValue(Frame.Begin, Size);
1236 
1237  // Range Length
1238  const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1239  *Frame.End, 0);
1240  emitAbsValue(Streamer, Range, 4);
1241 
1242  // Compact Encoding
1243  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1244  Streamer.EmitIntValue(Encoding, Size);
1245 
1246  // Personality Function
1247  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
1248  if (!DwarfEHFrameOnly && Frame.Personality)
1249  Streamer.EmitSymbolValue(Frame.Personality, Size);
1250  else
1251  Streamer.EmitIntValue(0, Size); // No personality fn
1252 
1253  // LSDA
1254  Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
1255  if (!DwarfEHFrameOnly && Frame.Lsda)
1256  Streamer.EmitSymbolValue(Frame.Lsda, Size);
1257  else
1258  Streamer.EmitIntValue(0, Size); // No LSDA
1259 }
1260 
1261 static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1262  if (IsEH)
1263  return 1;
1264  switch (DwarfVersion) {
1265  case 2:
1266  return 1;
1267  case 3:
1268  return 3;
1269  case 4:
1270  case 5:
1271  return 4;
1272  }
1273  llvm_unreachable("Unknown version");
1274 }
1275 
1276 const MCSymbol &FrameEmitterImpl::EmitCIE(const MCSymbol *personality,
1277  unsigned personalityEncoding,
1278  const MCSymbol *lsda,
1279  bool IsSignalFrame,
1280  unsigned lsdaEncoding, bool IsSimple,
1281  unsigned RAReg) {
1282  MCContext &context = Streamer.getContext();
1283  const MCRegisterInfo *MRI = context.getRegisterInfo();
1284  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
1285 
1286  MCSymbol *sectionStart = context.createTempSymbol();
1287  Streamer.EmitLabel(sectionStart);
1288 
1289  MCSymbol *sectionEnd = context.createTempSymbol();
1290 
1291  // Length
1292  const MCExpr *Length =
1293  MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1294  emitAbsValue(Streamer, Length, 4);
1295 
1296  // CIE ID
1297  unsigned CIE_ID = IsEH ? 0 : -1;
1298  Streamer.EmitIntValue(CIE_ID, 4);
1299 
1300  // Version
1301  uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
1302  Streamer.EmitIntValue(CIEVersion, 1);
1303 
1304  // Augmentation String
1305  SmallString<8> Augmentation;
1306  if (IsEH) {
1307  Augmentation += "z";
1308  if (personality)
1309  Augmentation += "P";
1310  if (lsda)
1311  Augmentation += "L";
1312  Augmentation += "R";
1313  if (IsSignalFrame)
1314  Augmentation += "S";
1315  Streamer.EmitBytes(Augmentation);
1316  }
1317  Streamer.EmitIntValue(0, 1);
1318 
1319  if (CIEVersion >= 4) {
1320  // Address Size
1321  Streamer.EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
1322 
1323  // Segment Descriptor Size
1324  Streamer.EmitIntValue(0, 1);
1325  }
1326 
1327  // Code Alignment Factor
1328  Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
1329 
1330  // Data Alignment Factor
1331  Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
1332 
1333  // Return Address Register
1334  if (RAReg == static_cast<unsigned>(INT_MAX))
1335  RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
1336 
1337  if (CIEVersion == 1) {
1338  assert(RAReg <= 255 &&
1339  "DWARF 2 encodes return_address_register in one byte");
1340  Streamer.EmitIntValue(RAReg, 1);
1341  } else {
1342  Streamer.EmitULEB128IntValue(RAReg);
1343  }
1344 
1345  // Augmentation Data Length (optional)
1346 
1347  unsigned augmentationLength = 0;
1348  if (IsEH) {
1349  if (personality) {
1350  // Personality Encoding
1351  augmentationLength += 1;
1352  // Personality
1353  augmentationLength += getSizeForEncoding(Streamer, personalityEncoding);
1354  }
1355  if (lsda)
1356  augmentationLength += 1;
1357  // Encoding of the FDE pointers
1358  augmentationLength += 1;
1359 
1360  Streamer.EmitULEB128IntValue(augmentationLength);
1361 
1362  // Augmentation Data (optional)
1363  if (personality) {
1364  // Personality Encoding
1365  emitEncodingByte(Streamer, personalityEncoding);
1366  // Personality
1367  EmitPersonality(Streamer, *personality, personalityEncoding);
1368  }
1369 
1370  if (lsda)
1371  emitEncodingByte(Streamer, lsdaEncoding);
1372 
1373  // Encoding of the FDE pointers
1374  emitEncodingByte(Streamer, MOFI->getFDEEncoding());
1375  }
1376 
1377  // Initial Instructions
1378 
1379  const MCAsmInfo *MAI = context.getAsmInfo();
1380  if (!IsSimple) {
1381  const std::vector<MCCFIInstruction> &Instructions =
1382  MAI->getInitialFrameState();
1383  EmitCFIInstructions(Instructions, nullptr);
1384  }
1385 
1386  InitialCFAOffset = CFAOffset;
1387 
1388  // Padding
1389  Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getCodePointerSize());
1390 
1391  Streamer.EmitLabel(sectionEnd);
1392  return *sectionStart;
1393 }
1394 
1395 void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1396  const MCDwarfFrameInfo &frame,
1397  bool LastInSection,
1398  const MCSymbol &SectionStart) {
1399  MCContext &context = Streamer.getContext();
1400  MCSymbol *fdeStart = context.createTempSymbol();
1401  MCSymbol *fdeEnd = context.createTempSymbol();
1402  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
1403 
1404  CFAOffset = InitialCFAOffset;
1405 
1406  // Length
1407  const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1408  emitAbsValue(Streamer, Length, 4);
1409 
1410  Streamer.EmitLabel(fdeStart);
1411 
1412  // CIE Pointer
1413  const MCAsmInfo *asmInfo = context.getAsmInfo();
1414  if (IsEH) {
1415  const MCExpr *offset =
1416  MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1417  emitAbsValue(Streamer, offset, 4);
1418  } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
1419  const MCExpr *offset =
1420  MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
1421  emitAbsValue(Streamer, offset, 4);
1422  } else {
1423  Streamer.EmitSymbolValue(&cieStart, 4);
1424  }
1425 
1426  // PC Begin
1427  unsigned PCEncoding =
1429  unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1430  emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
1431 
1432  // PC Range
1433  const MCExpr *Range =
1434  MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1435  emitAbsValue(Streamer, Range, PCSize);
1436 
1437  if (IsEH) {
1438  // Augmentation Data Length
1439  unsigned augmentationLength = 0;
1440 
1441  if (frame.Lsda)
1442  augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
1443 
1444  Streamer.EmitULEB128IntValue(augmentationLength);
1445 
1446  // Augmentation Data
1447  if (frame.Lsda)
1448  emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
1449  }
1450 
1451  // Call Frame Instructions
1452  EmitCFIInstructions(frame.Instructions, frame.Begin);
1453 
1454  // Padding
1455  // The size of a .eh_frame section has to be a multiple of the alignment
1456  // since a null CIE is interpreted as the end. Old systems overaligned
1457  // .eh_frame, so we do too and account for it in the last FDE.
1458  unsigned Align = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
1459  Streamer.EmitValueToAlignment(Align);
1460 
1461  Streamer.EmitLabel(fdeEnd);
1462 }
1463 
1464 namespace {
1465 
1466 struct CIEKey {
1467  static const CIEKey getEmptyKey() {
1468  return CIEKey(nullptr, 0, -1, false, false);
1469  }
1470 
1471  static const CIEKey getTombstoneKey() {
1472  return CIEKey(nullptr, -1, 0, false, false);
1473  }
1474 
1475  CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
1476  unsigned LsdaEncoding, bool IsSignalFrame, bool IsSimple)
1477  : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
1478  LsdaEncoding(LsdaEncoding), IsSignalFrame(IsSignalFrame),
1479  IsSimple(IsSimple) {}
1480 
1481  const MCSymbol *Personality;
1482  unsigned PersonalityEncoding;
1483  unsigned LsdaEncoding;
1484  bool IsSignalFrame;
1485  bool IsSimple;
1486 };
1487 
1488 } // end anonymous namespace
1489 
1490 namespace llvm {
1491 
1492 template <> struct DenseMapInfo<CIEKey> {
1493  static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1494  static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
1495 
1496  static unsigned getHashValue(const CIEKey &Key) {
1497  return static_cast<unsigned>(
1498  hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
1499  Key.IsSignalFrame, Key.IsSimple));
1500  }
1501 
1502  static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1503  return LHS.Personality == RHS.Personality &&
1504  LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1505  LHS.LsdaEncoding == RHS.LsdaEncoding &&
1506  LHS.IsSignalFrame == RHS.IsSignalFrame &&
1507  LHS.IsSimple == RHS.IsSimple;
1508  }
1509 };
1510 
1511 } // end namespace llvm
1512 
1514  bool IsEH) {
1515  Streamer.generateCompactUnwindEncodings(MAB);
1516 
1517  MCContext &Context = Streamer.getContext();
1518  const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
1519  const MCAsmInfo *AsmInfo = Context.getAsmInfo();
1520  FrameEmitterImpl Emitter(IsEH, Streamer);
1521  ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
1522 
1523  // Emit the compact unwind info if available.
1524  bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
1525  if (IsEH && MOFI->getCompactUnwindSection()) {
1526  bool SectionEmitted = false;
1527  for (const MCDwarfFrameInfo &Frame : FrameArray) {
1528  if (Frame.CompactUnwindEncoding == 0) continue;
1529  if (!SectionEmitted) {
1530  Streamer.SwitchSection(MOFI->getCompactUnwindSection());
1531  Streamer.EmitValueToAlignment(AsmInfo->getCodePointerSize());
1532  SectionEmitted = true;
1533  }
1534  NeedsEHFrameSection |=
1535  Frame.CompactUnwindEncoding ==
1537  Emitter.EmitCompactUnwind(Frame);
1538  }
1539  }
1540 
1541  if (!NeedsEHFrameSection) return;
1542 
1543  MCSection &Section =
1544  IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1545  : *MOFI->getDwarfFrameSection();
1546 
1547  Streamer.SwitchSection(&Section);
1548  MCSymbol *SectionStart = Context.createTempSymbol();
1549  Streamer.EmitLabel(SectionStart);
1550 
1552 
1553  const MCSymbol *DummyDebugKey = nullptr;
1554  bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
1555  for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1556  const MCDwarfFrameInfo &Frame = *I;
1557  ++I;
1558  if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
1560  // Don't generate an EH frame if we don't need one. I.e., it's taken care
1561  // of by the compact unwind encoding.
1562  continue;
1563 
1564  CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
1565  Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
1566  const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1567  if (!CIEStart)
1568  CIEStart = &Emitter.EmitCIE(
1569  Frame.Personality, Frame.PersonalityEncoding, Frame.Lsda,
1570  Frame.IsSignalFrame, Frame.LsdaEncoding, Frame.IsSimple, Frame.RAReg);
1571 
1572  Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
1573  }
1574 }
1575 
1577  uint64_t AddrDelta) {
1578  MCContext &Context = Streamer.getContext();
1579  SmallString<256> Tmp;
1580  raw_svector_ostream OS(Tmp);
1581  MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
1582  Streamer.EmitBytes(OS.str());
1583 }
1584 
1586  uint64_t AddrDelta,
1587  raw_ostream &OS) {
1588  // Scale the address delta by the minimum instruction length.
1589  AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1590 
1591  if (AddrDelta == 0) {
1592  } else if (isUIntN(6, AddrDelta)) {
1593  uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1594  OS << Opcode;
1595  } else if (isUInt<8>(AddrDelta)) {
1596  OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1597  OS << uint8_t(AddrDelta);
1598  } else if (isUInt<16>(AddrDelta)) {
1599  OS << uint8_t(dwarf::DW_CFA_advance_loc2);
1600  if (Context.getAsmInfo()->isLittleEndian())
1601  support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1602  else
1603  support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
1604  } else {
1605  assert(isUInt<32>(AddrDelta));
1606  OS << uint8_t(dwarf::DW_CFA_advance_loc4);
1607  if (Context.getAsmInfo()->isLittleEndian())
1609  else
1610  support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
1611  }
1612 }
static const MCExpr * forceExpAbs(MCStreamer &OS, const MCExpr *Expr)
Definition: MCDwarf.cpp:244
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:283
void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry &E)
Definition: MCContext.h:604
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:341
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void EmitCU(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params) const
Definition: MCDwarf.cpp:333
static int getDataAlignmentFactor(MCStreamer &streamer)
Definition: MCDwarf.cpp:966
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:78
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
LLVMContext & Context
static CIEKey getTombstoneKey()
Definition: MCDwarf.cpp:1494
#define DWARF2_FLAG_PROLOGUE_END
Definition: MCDwarf.h:71
static void Make(MCObjectStreamer *MCOS, MCSection *Section)
Definition: MCDwarf.cpp:64
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
Compute iterated dominance frontiers using a linear time algorithm.
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:128
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
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
const SmallVectorImpl< MCDwarfFile > & getMCDwarfFiles(unsigned CUID=0)
Definition: MCContext.h:536
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
SourceMgr SrcMgr
Definition: Error.cpp:23
virtual void EmitBytes(StringRef Data)
Emit the bytes in Data into the output.
Definition: MCStreamer.cpp:838
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:453
static void EmitAdvanceLoc(MCObjectStreamer &Streamer, uint64_t AddrDelta)
Definition: MCDwarf.cpp:1576
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
void Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params) const
Definition: MCDwarf.cpp:216
MCSection * getDwarfARangesSection() const
uint16_t getDwarfVersion() const
Definition: MCContext.h:620
#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:421
virtual MCSymbol * getDwarfLineTableSymbol(unsigned CUID)
Definition: MCStreamer.cpp:200
bool getSupportsCompactUnwindWithoutEHFrame() const
static void EmitGenDwarfInfo(MCStreamer *MCOS, const MCSymbol *AbbrevSectionSymbol, const MCSymbol *LineSectionSymbol, const MCSymbol *RangesSectionSymbol)
Definition: MCDwarf.cpp:653
unsigned LsdaEncoding
Definition: MCDwarf.h:508
StringRef getDwarfDebugFlags()
Definition: MCContext.h:609
std::vector< MCDwarfLineEntry > MCDwarfLineEntryCollection
Definition: MCDwarf.h:171
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:437
void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize)
std::vector< MCCFIInstruction > Instructions
Definition: MCDwarf.h:505
const std::vector< MCGenDwarfLabelEntry > & getMCGenDwarfLabelEntries() const
Definition: MCContext.h:600
MCContext & getContext() const
Definition: MCStreamer.h:227
#define DWARF2_FLAG_IS_STMT
Definition: MCDwarf.h:69
StringRef getDwarfDebugProducer()
Definition: MCContext.h:612
std::string Name
Definition: MCDwarf.h:49
virtual const MCExpr * getExprForPersonalitySymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition: MCAsmInfo.cpp:68
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:367
void finalizeDwarfSections(MCStreamer &MCOS)
Remove empty sections from SectionStartEndSyms, to avoid generating useless debug info for them...
Definition: MCContext.cpp:593
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
MCSymbol * Begin
Definition: MCDwarf.h:501
uint8_t DWARF2LineRange
Range of line offsets in a special line info. opcode.
Definition: MCDwarf.h:197
Reg
All possible values of the reg field in the ModR/M byte.
const MCSymbol * Lsda
Definition: MCDwarf.h:504
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
unsigned getCompactUnwindDwarfEHFrameOnly() const
static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol, unsigned symbolEncoding, bool isEH)
Definition: MCDwarf.cpp:997
MCDwarfLineTable & getMCDwarfLineTable(unsigned CUID)
Definition: MCContext.h:526
MCSection * getDwarfFrameSection() const
const SmallVectorImpl< std::string > & getMCDwarfDirs(unsigned CUID=0)
Definition: MCContext.h:540
bool hasAggressiveSymbolFolding() const
Definition: MCAsmInfo.h:512
bool getOmitDwarfIfHaveCompactUnwind() const
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Emit an assignment of Value to Symbol.
Definition: MCStreamer.cpp:743
Context object for machine code objects.
Definition: MCContext.h:59
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:527
unsigned getDwarfCompileUnitID()
Definition: MCContext.h:551
const std::map< unsigned, MCDwarfLineTable > & getMCDwarfLineTables() const
Definition: MCContext.h:522
Streaming object file generation interface.
bool doesDwarfUseRelocationsAcrossSections() const
Definition: MCAsmInfo.h:574
Instances of this class represent the information from a dwarf .loc directive.
Definition: MCDwarf.h:57
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
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:584
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:93
MCSymbol * getLabel() const
Definition: MCDwarf.h:470
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:124
const char * Name
MCSection * getDwarfAbbrevSection() const
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:148
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:432
static unsigned getSizeForEncoding(MCStreamer &streamer, unsigned symbolEncoding)
Definition: MCDwarf.cpp:976
Streaming machine code generation interface.
Definition: MCStreamer.h:168
static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion)
Definition: MCDwarf.cpp:1261
static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form)
Definition: MCDwarf.cpp:520
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:215
uint8_t DWARF2LineOpcodeBase
First special line opcode - leave room for the standard opcodes.
Definition: MCDwarf.h:192
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
constexpr bool isUInt< 8 >(uint64_t x)
Definition: MathExtras.h:335
unsigned const MachineRegisterInfo * MRI
void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void write(ArrayRef< value_type > Vals)
Definition: EndianStream.h:30
unsigned getRegister2() const
Definition: MCDwarf.h:480
#define DWARF2_FLAG_EPILOGUE_BEGIN
Definition: MCDwarf.h:72
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:860
static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size)
Definition: MCDwarf.cpp:255
unsigned getFDEEncoding() const
static void EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section, const MCLineSection::MCDwarfLineEntryCollection &LineEntries)
Definition: MCDwarf.cpp:115
int getOffset() const
Definition: MCDwarf.h:485
static void EmitGenDwarfAranges(MCStreamer *MCOS, const MCSymbol *InfoSectionSymbol)
Definition: MCDwarf.cpp:582
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:220
cl::opt< int > DwarfVersion("dwarf-version", cl::desc("Dwarf version"), cl::init(0))
OpType getOperation() const
Definition: MCDwarf.h:469
unsigned getFile(StringRef &Directory, StringRef &FileName, unsigned FileNumber=0)
Definition: MCDwarf.cpp:351
static const unsigned End
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:287
#define DWARF2_FLAG_BASIC_BLOCK
Definition: MCDwarf.h:70
bool doDwarfFDESymbolsUseAbsDiff() const
Definition: MCAsmInfo.h:578
const MCDwarfLoc & getCurrentDwarfLoc()
Definition: MCContext.h:580
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:476
unsigned PersonalityEncoding
Definition: MCDwarf.h:507
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
StringRef getValues() const
Definition: MCDwarf.h:492
int8_t DWARF2LineBase
Minimum line offset in a special line info.
Definition: MCDwarf.h:195
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition: Path.cpp:611
void generateCompactUnwindEncodings(MCAsmBackend *MAB)
Definition: MCStreamer.cpp:85
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void clearDwarfLocSeen()
Definition: MCContext.h:577
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.
StringRef getCompilationDir() const
Get the compilation directory for DW_AT_comp_dir The compilation directory should be set with setComp...
Definition: MCContext.h:503
unsigned getRegister() const
Definition: MCDwarf.h:472
const SetVector< MCSection * > & getGenDwarfSectionSyms()
Definition: MCContext.h:590
const std::vector< MCCFIInstruction > & getInitialFrameState() const
Definition: MCAsmInfo.h:586
#define E
Definition: LargeTest.cpp:27
static unsigned getHashValue(const CIEKey &Key)
Definition: MCDwarf.cpp:1496
unsigned getMinInstAlignment() const
Definition: MCAsmInfo.h:462
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:731
static bool isEqual(const CIEKey &LHS, const CIEKey &RHS)
Definition: MCDwarf.cpp:1502
iterator end() const
Definition: ArrayRef.h:138
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:515
static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, raw_ostream &OS)
Definition: MCDwarf.cpp:1585
MCSection * getDwarfInfoSection() const
uint32_t CompactUnwindEncoding
Definition: MCDwarf.h:509
static void EmitGenDwarfRanges(MCStreamer *MCOS)
Definition: MCDwarf.cpp:821
bool getDwarfLocSeen()
Definition: MCContext.h:579
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:309
static CIEKey getEmptyKey()
Definition: MCDwarf.cpp:1493
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:602
static void Emit(MCStreamer *MCOS)
Definition: MCDwarf.cpp:858
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:482
#define DWARF2_LINE_DEFAULT_IS_STMT
Definition: MCDwarf.h:67
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
Definition: SourceMgr.h:149
static StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:42
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
bool isStackGrowthDirectionUp() const
True if target stack grow up.
Definition: MCAsmInfo.h:394
static uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta)
Definition: MCDwarf.cpp:48
Instances of this class represent the line information for the dwarf line table entries.
Definition: MCDwarf.h:140
static void Emit(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params)
Definition: MCDwarf.cpp:197
static const MCExpr * MakeStartMinusEndExpr(const MCStreamer &MCOS, const MCSymbol &Start, const MCSymbol &End, int IntVal)
Definition: MCDwarf.cpp:92
static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol, unsigned symbolEncoding)
Definition: MCDwarf.cpp:1011
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:575
static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr, SMLoc &Loc)
Definition: MCDwarf.cpp:929
const MCSymbol * Personality
Definition: MCDwarf.h:503
#define I(x, y, z)
Definition: MD5.cpp:58
bool isDefined(bool SetUsed=true) const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:249
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned getCalleeSaveStackSlotSize() const
Get the callee-saved register stack slot size in bytes.
Definition: MCAsmInfo.h:386
bool isLittleEndian() const
True if the target is little endian.
Definition: MCAsmInfo.h:391
constexpr bool isUInt< 16 >(uint64_t x)
Definition: MathExtras.h:338
virtual void emitFill(uint64_t NumBytes, uint8_t FillValue)
Emit NumBytes bytes worth of the value specified by FillValue.
Definition: MCStreamer.cpp:165
unsigned getRARegister() const
This method should return the register where the return address can be found.
void EmitULEB128IntValue(uint64_t Value, unsigned Padding=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:108
MCSymbol * endSection(MCSection *Section)
Definition: MCStreamer.cpp:874
unsigned DirIndex
Definition: MCDwarf.h:52
void encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
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:382
std::pair< MCSymbol *, MCSymbol * > Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params) const
Definition: MCDwarf.cpp:222
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:285
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
ArrayRef< MCDwarfFrameInfo > getDwarfFrameInfos() const
Definition: MCStreamer.h:234
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:301
static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding)
Definition: MCDwarf.cpp:1050
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:73
Subtraction.
Definition: MCExpr.h:423
static void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH)
Definition: MCDwarf.cpp:1513
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:46
MCSection * getDwarfRangesSection() const
Represents a location in source code.
Definition: SMLoc.h:24
unsigned getFile(StringRef &Directory, StringRef &FileName, unsigned FileNumber=0)
Definition: MCDwarf.cpp:346
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:385
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159
virtual const MCExpr * getExprForFDESymbol(const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const
Definition: MCAsmInfo.cpp:75