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