32 std::string TripleName;
39 return error(ErrorStr,
Context),
false;
45 return error(
Twine(
"no register info for target ") + TripleName,
Context),
51 return error(
"no asm info for target " + TripleName,
Context),
false;
55 return error(
"no subtarget info for target " + TripleName,
Context),
false;
57 MC.reset(
new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get(),
nullptr,
58 nullptr,
true, Swift5ReflectionSegmentName));
60 MC->setObjectFileInfo(MOFI.get());
64 return error(
"no asm backend for target " + TripleName,
Context),
false;
68 return error(
"no instr info info for target " + TripleName,
Context),
false;
72 return error(
"no code emitter for target " + TripleName,
Context),
false;
74 switch (OutFileType) {
79 *MC, std::make_unique<formatted_raw_ostream>(OutFile),
true,
true, MIP,
80 std::unique_ptr<MCCodeEmitter>(MCE), std::unique_ptr<MCAsmBackend>(MAB),
86 TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
95 return error(
"no object streamer for target " + TripleName,
Context),
false;
101 return error(
"no target machine for target " + TripleName,
Context),
false;
103 Asm.reset(TheTarget->
createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
105 return error(
"no asm printer for target " + TripleName,
Context),
false;
107 RangesSectionSize = 0;
110 FrameSectionSize = 0;
111 DebugInfoSectionSize = 0;
120 MC->setDwarfVersion(DwarfVersion);
140 unsigned DwarfVersion) {
144 Unit.setLabelBegin(Asm->createTempSymbol(
"cu_begin"));
145 Asm->OutStreamer->emitLabel(Unit.getLabelBegin());
150 Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
151 Asm->emitInt16(DwarfVersion);
153 if (DwarfVersion >= 5) {
154 Asm->emitInt8(dwarf::DW_UT_compile);
155 Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
159 DebugInfoSectionSize += 12;
164 Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
165 DebugInfoSectionSize += 11;
169 EmittedUnits.push_back({Unit.getUniqueID(), Unit.getLabelBegin()});
175 const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
176 unsigned DwarfVersion) {
178 MC->setDwarfVersion(DwarfVersion);
179 Asm->emitDwarfAbbrevs(Abbrevs);
185 Asm->emitDwarfDIE(Die);
186 DebugInfoSectionSize += Die.
getSize();
193 .
Case(
"debug_line", MC->getObjectFileInfo()->getDwarfLineSection())
194 .
Case(
"debug_loc", MC->getObjectFileInfo()->getDwarfLocSection())
195 .
Case(
"debug_ranges",
196 MC->getObjectFileInfo()->getDwarfRangesSection())
197 .
Case(
"debug_frame", MC->getObjectFileInfo()->getDwarfFrameSection())
198 .
Case(
"debug_aranges",
199 MC->getObjectFileInfo()->getDwarfARangesSection())
213 Asm.emitInt32(11 + Die.
getSize() - 4);
216 Asm.emitInt8(MC->getTargetTriple().isArch64Bit() ? 8 : 4);
217 DebugInfoSectionSize += 11;
223 Asm->OutStreamer->switchSection(MOFI->getDwarfStrSection());
225 for (
auto Entry : Entries) {
227 Asm->OutStreamer->emitBytes(Entry.getString());
233 if (DwarfVersion >= 5) {
235 Asm->OutStreamer->switchSection(MOFI->getDwarfStrOffSection());
236 Asm->emitDwarfUnitLength(4,
"Length of String Offsets Set");
237 Asm->emitInt16(DwarfVersion);
245 if (EmittedUnits.empty())
249 std::vector<MCSymbol *> CompUnits;
252 for (
auto &
CU : EmittedUnits) {
253 CompUnits.push_back(
CU.LabelBegin);
255 UniqueIdToCuMap[
CU.ID] =
Id++;
258 Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection());
260 Asm.get(), Table, CompUnits,
262 return UniqueIdToCuMap[Entry.getCUIndex()];
268 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelNamespaceSection());
269 auto *SectionBegin = Asm->createTempSymbol(
"namespac_begin");
270 Asm->OutStreamer->emitLabel(SectionBegin);
276 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelNamesSection());
277 auto *SectionBegin = Asm->createTempSymbol(
"names_begin");
278 Asm->OutStreamer->emitLabel(SectionBegin);
284 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelObjCSection());
285 auto *SectionBegin = Asm->createTempSymbol(
"objc_begin");
286 Asm->OutStreamer->emitLabel(SectionBegin);
292 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelTypesSection());
293 auto *SectionBegin = Asm->createTempSymbol(
"types_begin");
294 Asm->OutStreamer->emitLabel(SectionBegin);
300 MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection();
310 MOFI->getSwift5ReflectionSection(ReflSectionKind);
311 if (ReflectionSection ==
nullptr)
323 int64_t UnitPcOffset,
uint64_t OrigLowPc,
325 const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
326 unsigned AddressSize) {
327 MS->
switchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
330 int64_t PcOffset = Entries.empty() ? 0 : FuncRange.
value() + UnitPcOffset;
331 for (
const auto &Range : Entries) {
332 if (Range.isBaseAddressSelectionEntry(AddressSize)) {
333 warn(
"unsupported base address selection operation",
334 "emitting debug_ranges");
338 if (Range.StartAddress == Range.EndAddress)
342 if (!(Range.StartAddress + OrigLowPc >= FuncRange.
start() &&
343 Range.EndAddress + OrigLowPc <= FuncRange.
stop()))
344 warn(
"inconsistent range data.",
"emitting debug_ranges");
345 MS->
emitIntValue(Range.StartAddress + PcOffset, AddressSize);
346 MS->
emitIntValue(Range.EndAddress + PcOffset, AddressSize);
347 RangesSectionSize += 2 * AddressSize;
353 RangesSectionSize += 2 * AddressSize;
362 bool DoDebugRanges) {
363 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
367 std::vector<std::pair<uint64_t, uint64_t>> Ranges;
368 const auto &FunctionRanges = Unit.getFunctionRanges();
369 for (
auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
370 Range != End; ++Range)
371 Ranges.push_back(std::make_pair(Range.start() + Range.value(),
372 Range.stop() + Range.value()));
378 if (!Ranges.empty()) {
379 MS->
switchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
381 MCSymbol *BeginLabel = Asm->createTempSymbol(
"Barange");
382 MCSymbol *EndLabel = Asm->createTempSymbol(
"Earange");
391 unsigned TupleSize = AddressSize * 2;
394 Asm->emitLabelDifference(EndLabel, BeginLabel, 4);
395 Asm->OutStreamer->emitLabel(BeginLabel);
397 Asm->emitInt32(Unit.getStartOffset());
398 Asm->emitInt8(AddressSize);
401 Asm->OutStreamer->emitFill(Padding, 0x0);
403 for (
auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
407 while ((Range + 1) != End && Range->second == (Range + 1)->first)
409 MS->
emitIntValue(Range->second - RangeStart, AddressSize);
413 Asm->OutStreamer->emitIntValue(0, AddressSize);
414 Asm->OutStreamer->emitIntValue(0, AddressSize);
415 Asm->OutStreamer->emitLabel(EndLabel);
421 MS->
switchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
423 int64_t PcOffset = -Unit.getLowPc();
425 for (
auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
427 while (Range + 1 != End && Range->second == (Range + 1)->first)
429 MS->
emitIntValue(Range->second + PcOffset, AddressSize);
430 RangesSectionSize += 2 * AddressSize;
436 RangesSectionSize += 2 * AddressSize;
444 const auto &
Attributes = Unit.getLocationAttributes();
449 MS->
switchSection(MC->getObjectFileInfo()->getDwarfLocSection());
451 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
452 uint64_t BaseAddressMarker = (AddressSize == 8)
457 DWARFUnit &OrigUnit = Unit.getOrigUnit();
458 auto OrigUnitDie = OrigUnit.
getUnitDIE(
false);
459 int64_t UnitPcOffset = 0;
460 if (
auto OrigLowPc =
dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc)))
461 UnitPcOffset = int64_t(*OrigLowPc) - Unit.getLowPc();
466 Attr.first.set(LocSectionSize);
469 int64_t LocPcOffset = Attr.second + UnitPcOffset;
470 while (
Data.isValidOffset(Offset)) {
473 LocSectionSize += 2 * AddressSize;
476 Asm->OutStreamer->emitIntValue(0, AddressSize);
477 Asm->OutStreamer->emitIntValue(0, AddressSize);
481 if (
Low == BaseAddressMarker) {
482 Asm->OutStreamer->emitIntValue(BaseAddressMarker, AddressSize);
483 Asm->OutStreamer->emitIntValue(
High + Attr.second, AddressSize);
488 Asm->OutStreamer->emitIntValue(
Low + LocPcOffset, AddressSize);
489 Asm->OutStreamer->emitIntValue(
High + LocPcOffset, AddressSize);
491 Asm->OutStreamer->emitIntValue(Length, 2);
496 ProcessExpr(Input, Buffer);
497 Asm->OutStreamer->emitBytes(
498 StringRef((
const char *)Buffer.data(), Length));
500 LocSectionSize += Length + 2;
507 unsigned MinInstLength,
508 std::vector<DWARFDebugLine::Row> &Rows,
511 MS->
switchSection(MC->getObjectFileInfo()->getDwarfLineSection());
512 MCSymbol *LineStartSym = MC->createTempSymbol();
513 MCSymbol *LineEndSym = MC->createTempSymbol();
517 Asm->emitLabelDifference(LineEndSym, LineStartSym, 4);
518 Asm->OutStreamer->emitLabel(LineStartSym);
521 LineSectionSize += PrologueBytes.
size() + 4;
532 LineSectionSize += EncodingBuffer.size();
538 unsigned FileNum = 1;
539 unsigned LastLine = 1;
541 unsigned IsStatement = 1;
545 unsigned RowsSinceLastSequence = 0;
549 if (Address == -1ULL) {
557 AddressDelta = (Row.Address.Address - Address) / MinInstLength;
565 if (FileNum != Row.File) {
571 if (Column != Row.Column) {
581 if (Isa != Row.Isa) {
587 if (IsStatement != Row.IsStmt) {
588 IsStatement = Row.IsStmt;
590 LineSectionSize += 1;
592 if (Row.BasicBlock) {
594 LineSectionSize += 1;
597 if (Row.PrologueEnd) {
599 LineSectionSize += 1;
602 if (Row.EpilogueBegin) {
604 LineSectionSize += 1;
607 int64_t LineDelta = int64_t(Row.Line) - LastLine;
608 if (!Row.EndSequence) {
611 LineSectionSize += EncodingBuffer.size();
613 Address = Row.Address.Address;
615 RowsSinceLastSequence++;
630 LineSectionSize += EncodingBuffer.size();
633 LastLine = FileNum = IsStatement = 1;
634 RowsSinceLastSequence = Column = Isa = 0;
638 if (RowsSinceLastSequence) {
642 LineSectionSize += EncodingBuffer.size();
652 MS->
switchSection(MC->getObjectFileInfo()->getDwarfLineSection());
658 unsigned UnitLength =
Data.getU32(&Offset);
659 uint64_t UnitEnd = Offset + UnitLength;
660 MCSymbol *BeginLabel = MC->createTempSymbol();
661 MCSymbol *EndLabel = MC->createTempSymbol();
665 warn(
"Unsupported line table version: dropping contents and not "
666 "unobfsucating line table.");
670 Asm->emitLabelDifference(EndLabel, BeginLabel, 4);
671 Asm->OutStreamer->emitLabel(BeginLabel);
673 LineSectionSize += 6;
675 MCSymbol *HeaderBeginLabel = MC->createTempSymbol();
676 MCSymbol *HeaderEndLabel = MC->createTempSymbol();
677 Asm->emitLabelDifference(HeaderEndLabel, HeaderBeginLabel, 4);
678 Asm->OutStreamer->emitLabel(HeaderBeginLabel);
680 LineSectionSize += 4;
682 uint64_t AfterHeaderLengthOffset = Offset;
684 Offset += (
Version >= 4) ? 5 : 4;
685 unsigned OpcodeBase =
Data.getU8(&Offset);
686 Offset += OpcodeBase - 1;
687 Asm->OutStreamer->emitBytes(Contents.
slice(AfterHeaderLengthOffset, Offset));
688 LineSectionSize += Offset - AfterHeaderLengthOffset;
691 while (
const char *Dir =
Data.getCStr(&Offset)) {
696 Asm->OutStreamer->emitBytes(Translated);
698 LineSectionSize += Translated.
size() + 1;
701 LineSectionSize += 1;
703 while (
const char *File =
Data.getCStr(&Offset)) {
708 Asm->OutStreamer->emitBytes(Translated);
710 LineSectionSize += Translated.
size() + 1;
713 Asm->emitULEB128(
Data.getULEB128(&Offset));
714 Asm->emitULEB128(
Data.getULEB128(&Offset));
715 Asm->emitULEB128(
Data.getULEB128(&Offset));
716 LineSectionSize += Offset - OffsetBeforeLEBs;
719 LineSectionSize += 1;
721 Asm->OutStreamer->emitLabel(HeaderEndLabel);
724 Asm->OutStreamer->emitBytes(Contents.
slice(Offset, UnitEnd));
725 LineSectionSize += UnitEnd - Offset;
727 Asm->OutStreamer->emitLabel(EndLabel);
733 void DwarfStreamer::emitPubSectionForUnit(
735 const std::vector<CompileUnit::AccelInfo> &Names) {
740 Asm->OutStreamer->switchSection(Sec);
741 MCSymbol *BeginLabel = Asm->createTempSymbol(
"pub" + SecName +
"_begin");
742 MCSymbol *EndLabel = Asm->createTempSymbol(
"pub" + SecName +
"_end");
744 bool HeaderEmitted =
false;
746 for (
const auto &
Name : Names) {
747 if (
Name.SkipPubSection)
750 if (!HeaderEmitted) {
752 Asm->emitLabelDifference(EndLabel, BeginLabel, 4);
753 Asm->OutStreamer->emitLabel(BeginLabel);
755 Asm->emitInt32(Unit.getStartOffset());
756 Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset());
757 HeaderEmitted =
true;
759 Asm->emitInt32(
Name.Die->getOffset());
762 Asm->OutStreamer->emitBytes(
Name.Name.getString());
770 Asm->OutStreamer->emitLabel(EndLabel);
775 emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
776 "names", Unit, Unit.getPubnames());
781 emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
782 "types", Unit, Unit.getPubtypes());
787 MS->
switchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
790 FrameSectionSize += CIEBytes.
size();
798 MS->
switchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
804 FrameSectionSize += FDEBytes.
size() + 8 + AddrSize;