1 //===- lib/MC/MCStreamer.cpp - Streaming Machine Code Output --------------===// 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/MCStreamer.h" 10 #include "llvm/ADT/Optional.h" 11 #include "llvm/ADT/SmallString.h" 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/ADT/Twine.h" 14 #include "llvm/BinaryFormat/COFF.h" 15 #include "llvm/BinaryFormat/MachO.h" 16 #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 17 #include "llvm/MC/MCAsmBackend.h" 18 #include "llvm/MC/MCAsmInfo.h" 19 #include "llvm/MC/MCCodeView.h" 20 #include "llvm/MC/MCContext.h" 21 #include "llvm/MC/MCDwarf.h" 22 #include "llvm/MC/MCExpr.h" 23 #include "llvm/MC/MCInst.h" 24 #include "llvm/MC/MCInstPrinter.h" 25 #include "llvm/MC/MCObjectFileInfo.h" 26 #include "llvm/MC/MCPseudoProbe.h" 27 #include "llvm/MC/MCRegister.h" 28 #include "llvm/MC/MCRegisterInfo.h" 29 #include "llvm/MC/MCSection.h" 30 #include "llvm/MC/MCSectionCOFF.h" 31 #include "llvm/MC/MCSymbol.h" 32 #include "llvm/MC/MCWin64EH.h" 33 #include "llvm/MC/MCWinEH.h" 34 #include "llvm/Support/Casting.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/LEB128.h" 37 #include "llvm/Support/MathExtras.h" 38 #include "llvm/Support/raw_ostream.h" 39 #include <cassert> 40 #include <cstdint> 41 #include <cstdlib> 42 #include <utility> 43 44 using namespace llvm; 45 46 MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) { 47 S.setTargetStreamer(this); 48 } 49 50 // Pin the vtables to this file. 51 MCTargetStreamer::~MCTargetStreamer() = default; 52 53 void MCTargetStreamer::emitLabel(MCSymbol *Symbol) {} 54 55 void MCTargetStreamer::finish() {} 56 57 void MCTargetStreamer::emitConstantPools() {} 58 59 void MCTargetStreamer::changeSection(const MCSection *CurSection, 60 MCSection *Section, 61 const MCExpr *Subsection, 62 raw_ostream &OS) { 63 Section->printSwitchToSection(*Streamer.getContext().getAsmInfo(), 64 Streamer.getContext().getTargetTriple(), OS, 65 Subsection); 66 } 67 68 void MCTargetStreamer::emitDwarfFileDirective(StringRef Directive) { 69 Streamer.emitRawText(Directive); 70 } 71 72 void MCTargetStreamer::emitValue(const MCExpr *Value) { 73 SmallString<128> Str; 74 raw_svector_ostream OS(Str); 75 76 Value->print(OS, Streamer.getContext().getAsmInfo()); 77 Streamer.emitRawText(OS.str()); 78 } 79 80 void MCTargetStreamer::emitRawBytes(StringRef Data) { 81 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo(); 82 const char *Directive = MAI->getData8bitsDirective(); 83 for (const unsigned char C : Data.bytes()) { 84 SmallString<128> Str; 85 raw_svector_ostream OS(Str); 86 87 OS << Directive << (unsigned)C; 88 Streamer.emitRawText(OS.str()); 89 } 90 } 91 92 void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {} 93 94 MCStreamer::MCStreamer(MCContext &Ctx) 95 : Context(Ctx), CurrentWinFrameInfo(nullptr), 96 CurrentProcWinFrameInfoStartIndex(0), UseAssemblerInfoForParsing(false) { 97 SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>()); 98 } 99 100 MCStreamer::~MCStreamer() = default; 101 102 void MCStreamer::reset() { 103 DwarfFrameInfos.clear(); 104 CurrentWinFrameInfo = nullptr; 105 WinFrameInfos.clear(); 106 SymbolOrdering.clear(); 107 SectionStack.clear(); 108 SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>()); 109 } 110 111 raw_ostream &MCStreamer::getCommentOS() { 112 // By default, discard comments. 113 return nulls(); 114 } 115 116 unsigned MCStreamer::getNumFrameInfos() { return DwarfFrameInfos.size(); } 117 ArrayRef<MCDwarfFrameInfo> MCStreamer::getDwarfFrameInfos() const { 118 return DwarfFrameInfos; 119 } 120 121 void MCStreamer::emitRawComment(const Twine &T, bool TabPrefix) {} 122 123 void MCStreamer::addExplicitComment(const Twine &T) {} 124 void MCStreamer::emitExplicitComments() {} 125 126 void MCStreamer::generateCompactUnwindEncodings(MCAsmBackend *MAB) { 127 for (auto &FI : DwarfFrameInfos) 128 FI.CompactUnwindEncoding = 129 (MAB ? MAB->generateCompactUnwindEncoding(FI.Instructions) : 0); 130 } 131 132 /// EmitIntValue - Special case of EmitValue that avoids the client having to 133 /// pass in a MCExpr for constant integers. 134 void MCStreamer::emitIntValue(uint64_t Value, unsigned Size) { 135 assert(1 <= Size && Size <= 8 && "Invalid size"); 136 assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) && 137 "Invalid size"); 138 const bool IsLittleEndian = Context.getAsmInfo()->isLittleEndian(); 139 uint64_t Swapped = support::endian::byte_swap( 140 Value, IsLittleEndian ? support::little : support::big); 141 unsigned Index = IsLittleEndian ? 0 : 8 - Size; 142 emitBytes(StringRef(reinterpret_cast<char *>(&Swapped) + Index, Size)); 143 } 144 void MCStreamer::emitIntValue(APInt Value) { 145 if (Value.getNumWords() == 1) { 146 emitIntValue(Value.getLimitedValue(), Value.getBitWidth() / 8); 147 return; 148 } 149 150 const bool IsLittleEndianTarget = Context.getAsmInfo()->isLittleEndian(); 151 const bool ShouldSwap = sys::IsLittleEndianHost != IsLittleEndianTarget; 152 const APInt Swapped = ShouldSwap ? Value.byteSwap() : Value; 153 const unsigned Size = Value.getBitWidth() / 8; 154 SmallString<10> Tmp; 155 Tmp.resize(Size); 156 StoreIntToMemory(Swapped, reinterpret_cast<uint8_t *>(Tmp.data()), Size); 157 emitBytes(Tmp.str()); 158 } 159 160 /// EmitULEB128IntValue - Special case of EmitULEB128Value that avoids the 161 /// client having to pass in a MCExpr for constant integers. 162 void MCStreamer::emitULEB128IntValue(uint64_t Value, unsigned PadTo) { 163 SmallString<128> Tmp; 164 raw_svector_ostream OSE(Tmp); 165 encodeULEB128(Value, OSE, PadTo); 166 emitBytes(OSE.str()); 167 } 168 169 /// EmitSLEB128IntValue - Special case of EmitSLEB128Value that avoids the 170 /// client having to pass in a MCExpr for constant integers. 171 void MCStreamer::emitSLEB128IntValue(int64_t Value) { 172 SmallString<128> Tmp; 173 raw_svector_ostream OSE(Tmp); 174 encodeSLEB128(Value, OSE); 175 emitBytes(OSE.str()); 176 } 177 178 void MCStreamer::emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc) { 179 emitValueImpl(Value, Size, Loc); 180 } 181 182 void MCStreamer::emitSymbolValue(const MCSymbol *Sym, unsigned Size, 183 bool IsSectionRelative) { 184 assert((!IsSectionRelative || Size == 4) && 185 "SectionRelative value requires 4-bytes"); 186 187 if (!IsSectionRelative) 188 emitValueImpl(MCSymbolRefExpr::create(Sym, getContext()), Size); 189 else 190 emitCOFFSecRel32(Sym, /*Offset=*/0); 191 } 192 193 void MCStreamer::emitDTPRel64Value(const MCExpr *Value) { 194 report_fatal_error("unsupported directive in streamer"); 195 } 196 197 void MCStreamer::emitDTPRel32Value(const MCExpr *Value) { 198 report_fatal_error("unsupported directive in streamer"); 199 } 200 201 void MCStreamer::emitTPRel64Value(const MCExpr *Value) { 202 report_fatal_error("unsupported directive in streamer"); 203 } 204 205 void MCStreamer::emitTPRel32Value(const MCExpr *Value) { 206 report_fatal_error("unsupported directive in streamer"); 207 } 208 209 void MCStreamer::emitGPRel64Value(const MCExpr *Value) { 210 report_fatal_error("unsupported directive in streamer"); 211 } 212 213 void MCStreamer::emitGPRel32Value(const MCExpr *Value) { 214 report_fatal_error("unsupported directive in streamer"); 215 } 216 217 /// Emit NumBytes bytes worth of the value specified by FillValue. 218 /// This implements directives such as '.space'. 219 void MCStreamer::emitFill(uint64_t NumBytes, uint8_t FillValue) { 220 emitFill(*MCConstantExpr::create(NumBytes, getContext()), FillValue); 221 } 222 223 void llvm::MCStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLen, 224 llvm::SMLoc, const MCSubtargetInfo& STI) {} 225 226 /// The implementation in this class just redirects to emitFill. 227 void MCStreamer::emitZeros(uint64_t NumBytes) { emitFill(NumBytes, 0); } 228 229 Expected<unsigned> 230 MCStreamer::tryEmitDwarfFileDirective(unsigned FileNo, StringRef Directory, 231 StringRef Filename, 232 Optional<MD5::MD5Result> Checksum, 233 Optional<StringRef> Source, 234 unsigned CUID) { 235 return getContext().getDwarfFile(Directory, Filename, FileNo, Checksum, 236 Source, CUID); 237 } 238 239 void MCStreamer::emitDwarfFile0Directive(StringRef Directory, 240 StringRef Filename, 241 Optional<MD5::MD5Result> Checksum, 242 Optional<StringRef> Source, 243 unsigned CUID) { 244 getContext().setMCLineTableRootFile(CUID, Directory, Filename, Checksum, 245 Source); 246 } 247 248 void MCStreamer::emitCFIBKeyFrame() { 249 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 250 if (!CurFrame) 251 return; 252 CurFrame->IsBKeyFrame = true; 253 } 254 255 void MCStreamer::emitCFIMTETaggedFrame() { 256 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 257 if (!CurFrame) 258 return; 259 CurFrame->IsMTETaggedFrame = true; 260 } 261 262 void MCStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line, 263 unsigned Column, unsigned Flags, 264 unsigned Isa, unsigned Discriminator, 265 StringRef FileName) { 266 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa, 267 Discriminator); 268 } 269 270 MCSymbol *MCStreamer::getDwarfLineTableSymbol(unsigned CUID) { 271 MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID); 272 if (!Table.getLabel()) { 273 StringRef Prefix = Context.getAsmInfo()->getPrivateGlobalPrefix(); 274 Table.setLabel( 275 Context.getOrCreateSymbol(Prefix + "line_table_start" + Twine(CUID))); 276 } 277 return Table.getLabel(); 278 } 279 280 bool MCStreamer::hasUnfinishedDwarfFrameInfo() { 281 return !DwarfFrameInfos.empty() && !DwarfFrameInfos.back().End; 282 } 283 284 MCDwarfFrameInfo *MCStreamer::getCurrentDwarfFrameInfo() { 285 if (!hasUnfinishedDwarfFrameInfo()) { 286 getContext().reportError(getStartTokLoc(), 287 "this directive must appear between " 288 ".cfi_startproc and .cfi_endproc directives"); 289 return nullptr; 290 } 291 return &DwarfFrameInfos.back(); 292 } 293 294 bool MCStreamer::emitCVFileDirective(unsigned FileNo, StringRef Filename, 295 ArrayRef<uint8_t> Checksum, 296 unsigned ChecksumKind) { 297 return getContext().getCVContext().addFile(*this, FileNo, Filename, Checksum, 298 ChecksumKind); 299 } 300 301 bool MCStreamer::emitCVFuncIdDirective(unsigned FunctionId) { 302 return getContext().getCVContext().recordFunctionId(FunctionId); 303 } 304 305 bool MCStreamer::emitCVInlineSiteIdDirective(unsigned FunctionId, 306 unsigned IAFunc, unsigned IAFile, 307 unsigned IALine, unsigned IACol, 308 SMLoc Loc) { 309 if (getContext().getCVContext().getCVFunctionInfo(IAFunc) == nullptr) { 310 getContext().reportError(Loc, "parent function id not introduced by " 311 ".cv_func_id or .cv_inline_site_id"); 312 return true; 313 } 314 315 return getContext().getCVContext().recordInlinedCallSiteId( 316 FunctionId, IAFunc, IAFile, IALine, IACol); 317 } 318 319 void MCStreamer::emitCVLocDirective(unsigned FunctionId, unsigned FileNo, 320 unsigned Line, unsigned Column, 321 bool PrologueEnd, bool IsStmt, 322 StringRef FileName, SMLoc Loc) {} 323 324 bool MCStreamer::checkCVLocSection(unsigned FuncId, unsigned FileNo, 325 SMLoc Loc) { 326 CodeViewContext &CVC = getContext().getCVContext(); 327 MCCVFunctionInfo *FI = CVC.getCVFunctionInfo(FuncId); 328 if (!FI) { 329 getContext().reportError( 330 Loc, "function id not introduced by .cv_func_id or .cv_inline_site_id"); 331 return false; 332 } 333 334 // Track the section 335 if (FI->Section == nullptr) 336 FI->Section = getCurrentSectionOnly(); 337 else if (FI->Section != getCurrentSectionOnly()) { 338 getContext().reportError( 339 Loc, 340 "all .cv_loc directives for a function must be in the same section"); 341 return false; 342 } 343 return true; 344 } 345 346 void MCStreamer::emitCVLinetableDirective(unsigned FunctionId, 347 const MCSymbol *Begin, 348 const MCSymbol *End) {} 349 350 void MCStreamer::emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, 351 unsigned SourceFileId, 352 unsigned SourceLineNum, 353 const MCSymbol *FnStartSym, 354 const MCSymbol *FnEndSym) {} 355 356 /// Only call this on endian-specific types like ulittle16_t and little32_t, or 357 /// structs composed of them. 358 template <typename T> 359 static void copyBytesForDefRange(SmallString<20> &BytePrefix, 360 codeview::SymbolKind SymKind, 361 const T &DefRangeHeader) { 362 BytePrefix.resize(2 + sizeof(T)); 363 codeview::ulittle16_t SymKindLE = codeview::ulittle16_t(SymKind); 364 memcpy(&BytePrefix[0], &SymKindLE, 2); 365 memcpy(&BytePrefix[2], &DefRangeHeader, sizeof(T)); 366 } 367 368 void MCStreamer::emitCVDefRangeDirective( 369 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 370 StringRef FixedSizePortion) {} 371 372 void MCStreamer::emitCVDefRangeDirective( 373 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 374 codeview::DefRangeRegisterRelHeader DRHdr) { 375 SmallString<20> BytePrefix; 376 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_REGISTER_REL, DRHdr); 377 emitCVDefRangeDirective(Ranges, BytePrefix); 378 } 379 380 void MCStreamer::emitCVDefRangeDirective( 381 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 382 codeview::DefRangeSubfieldRegisterHeader DRHdr) { 383 SmallString<20> BytePrefix; 384 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_SUBFIELD_REGISTER, 385 DRHdr); 386 emitCVDefRangeDirective(Ranges, BytePrefix); 387 } 388 389 void MCStreamer::emitCVDefRangeDirective( 390 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 391 codeview::DefRangeRegisterHeader DRHdr) { 392 SmallString<20> BytePrefix; 393 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_REGISTER, DRHdr); 394 emitCVDefRangeDirective(Ranges, BytePrefix); 395 } 396 397 void MCStreamer::emitCVDefRangeDirective( 398 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 399 codeview::DefRangeFramePointerRelHeader DRHdr) { 400 SmallString<20> BytePrefix; 401 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_FRAMEPOINTER_REL, 402 DRHdr); 403 emitCVDefRangeDirective(Ranges, BytePrefix); 404 } 405 406 void MCStreamer::emitEHSymAttributes(const MCSymbol *Symbol, 407 MCSymbol *EHSymbol) { 408 } 409 410 void MCStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) { 411 switchSection(getContext().getObjectFileInfo()->getTextSection()); 412 } 413 414 void MCStreamer::assignFragment(MCSymbol *Symbol, MCFragment *Fragment) { 415 assert(Fragment); 416 Symbol->setFragment(Fragment); 417 418 // As we emit symbols into a section, track the order so that they can 419 // be sorted upon later. Zero is reserved to mean 'unemitted'. 420 SymbolOrdering[Symbol] = 1 + SymbolOrdering.size(); 421 } 422 423 void MCStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { 424 Symbol->redefineIfPossible(); 425 426 if (!Symbol->isUndefined() || Symbol->isVariable()) 427 return getContext().reportError(Loc, "symbol '" + Twine(Symbol->getName()) + 428 "' is already defined"); 429 430 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); 431 assert(getCurrentSectionOnly() && "Cannot emit before setting section!"); 432 assert(!Symbol->getFragment() && "Unexpected fragment on symbol data!"); 433 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 434 435 Symbol->setFragment(&getCurrentSectionOnly()->getDummyFragment()); 436 437 MCTargetStreamer *TS = getTargetStreamer(); 438 if (TS) 439 TS->emitLabel(Symbol); 440 } 441 442 void MCStreamer::emitConditionalAssignment(MCSymbol *Symbol, 443 const MCExpr *Value) {} 444 445 void MCStreamer::emitCFISections(bool EH, bool Debug) {} 446 447 void MCStreamer::emitCFIStartProc(bool IsSimple, SMLoc Loc) { 448 if (hasUnfinishedDwarfFrameInfo()) 449 return getContext().reportError( 450 Loc, "starting new .cfi frame before finishing the previous one"); 451 452 MCDwarfFrameInfo Frame; 453 Frame.IsSimple = IsSimple; 454 emitCFIStartProcImpl(Frame); 455 456 const MCAsmInfo* MAI = Context.getAsmInfo(); 457 if (MAI) { 458 for (const MCCFIInstruction& Inst : MAI->getInitialFrameState()) { 459 if (Inst.getOperation() == MCCFIInstruction::OpDefCfa || 460 Inst.getOperation() == MCCFIInstruction::OpDefCfaRegister || 461 Inst.getOperation() == MCCFIInstruction::OpLLVMDefAspaceCfa) { 462 Frame.CurrentCfaRegister = Inst.getRegister(); 463 } 464 } 465 } 466 467 DwarfFrameInfos.push_back(Frame); 468 } 469 470 void MCStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { 471 } 472 473 void MCStreamer::emitCFIEndProc() { 474 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 475 if (!CurFrame) 476 return; 477 emitCFIEndProcImpl(*CurFrame); 478 } 479 480 void MCStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { 481 // Put a dummy non-null value in Frame.End to mark that this frame has been 482 // closed. 483 Frame.End = (MCSymbol *)1; 484 } 485 486 MCSymbol *MCStreamer::emitCFILabel() { 487 // Return a dummy non-null value so that label fields appear filled in when 488 // generating textual assembly. 489 return (MCSymbol *)1; 490 } 491 492 void MCStreamer::emitCFIDefCfa(int64_t Register, int64_t Offset) { 493 MCSymbol *Label = emitCFILabel(); 494 MCCFIInstruction Instruction = 495 MCCFIInstruction::cfiDefCfa(Label, Register, Offset); 496 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 497 if (!CurFrame) 498 return; 499 CurFrame->Instructions.push_back(Instruction); 500 CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register); 501 } 502 503 void MCStreamer::emitCFIDefCfaOffset(int64_t Offset) { 504 MCSymbol *Label = emitCFILabel(); 505 MCCFIInstruction Instruction = 506 MCCFIInstruction::cfiDefCfaOffset(Label, Offset); 507 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 508 if (!CurFrame) 509 return; 510 CurFrame->Instructions.push_back(Instruction); 511 } 512 513 void MCStreamer::emitCFIAdjustCfaOffset(int64_t Adjustment) { 514 MCSymbol *Label = emitCFILabel(); 515 MCCFIInstruction Instruction = 516 MCCFIInstruction::createAdjustCfaOffset(Label, Adjustment); 517 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 518 if (!CurFrame) 519 return; 520 CurFrame->Instructions.push_back(Instruction); 521 } 522 523 void MCStreamer::emitCFIDefCfaRegister(int64_t Register) { 524 MCSymbol *Label = emitCFILabel(); 525 MCCFIInstruction Instruction = 526 MCCFIInstruction::createDefCfaRegister(Label, Register); 527 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 528 if (!CurFrame) 529 return; 530 CurFrame->Instructions.push_back(Instruction); 531 CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register); 532 } 533 534 void MCStreamer::emitCFILLVMDefAspaceCfa(int64_t Register, int64_t Offset, 535 int64_t AddressSpace) { 536 MCSymbol *Label = emitCFILabel(); 537 MCCFIInstruction Instruction = MCCFIInstruction::createLLVMDefAspaceCfa( 538 Label, Register, Offset, AddressSpace); 539 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 540 if (!CurFrame) 541 return; 542 CurFrame->Instructions.push_back(Instruction); 543 CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register); 544 } 545 546 void MCStreamer::emitCFIOffset(int64_t Register, int64_t Offset) { 547 MCSymbol *Label = emitCFILabel(); 548 MCCFIInstruction Instruction = 549 MCCFIInstruction::createOffset(Label, Register, Offset); 550 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 551 if (!CurFrame) 552 return; 553 CurFrame->Instructions.push_back(Instruction); 554 } 555 556 void MCStreamer::emitCFIRelOffset(int64_t Register, int64_t Offset) { 557 MCSymbol *Label = emitCFILabel(); 558 MCCFIInstruction Instruction = 559 MCCFIInstruction::createRelOffset(Label, Register, Offset); 560 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 561 if (!CurFrame) 562 return; 563 CurFrame->Instructions.push_back(Instruction); 564 } 565 566 void MCStreamer::emitCFIPersonality(const MCSymbol *Sym, 567 unsigned Encoding) { 568 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 569 if (!CurFrame) 570 return; 571 CurFrame->Personality = Sym; 572 CurFrame->PersonalityEncoding = Encoding; 573 } 574 575 void MCStreamer::emitCFILsda(const MCSymbol *Sym, unsigned Encoding) { 576 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 577 if (!CurFrame) 578 return; 579 CurFrame->Lsda = Sym; 580 CurFrame->LsdaEncoding = Encoding; 581 } 582 583 void MCStreamer::emitCFIRememberState() { 584 MCSymbol *Label = emitCFILabel(); 585 MCCFIInstruction Instruction = MCCFIInstruction::createRememberState(Label); 586 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 587 if (!CurFrame) 588 return; 589 CurFrame->Instructions.push_back(Instruction); 590 } 591 592 void MCStreamer::emitCFIRestoreState() { 593 // FIXME: Error if there is no matching cfi_remember_state. 594 MCSymbol *Label = emitCFILabel(); 595 MCCFIInstruction Instruction = MCCFIInstruction::createRestoreState(Label); 596 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 597 if (!CurFrame) 598 return; 599 CurFrame->Instructions.push_back(Instruction); 600 } 601 602 void MCStreamer::emitCFISameValue(int64_t Register) { 603 MCSymbol *Label = emitCFILabel(); 604 MCCFIInstruction Instruction = 605 MCCFIInstruction::createSameValue(Label, Register); 606 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 607 if (!CurFrame) 608 return; 609 CurFrame->Instructions.push_back(Instruction); 610 } 611 612 void MCStreamer::emitCFIRestore(int64_t Register) { 613 MCSymbol *Label = emitCFILabel(); 614 MCCFIInstruction Instruction = 615 MCCFIInstruction::createRestore(Label, Register); 616 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 617 if (!CurFrame) 618 return; 619 CurFrame->Instructions.push_back(Instruction); 620 } 621 622 void MCStreamer::emitCFIEscape(StringRef Values) { 623 MCSymbol *Label = emitCFILabel(); 624 MCCFIInstruction Instruction = MCCFIInstruction::createEscape(Label, Values); 625 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 626 if (!CurFrame) 627 return; 628 CurFrame->Instructions.push_back(Instruction); 629 } 630 631 void MCStreamer::emitCFIGnuArgsSize(int64_t Size) { 632 MCSymbol *Label = emitCFILabel(); 633 MCCFIInstruction Instruction = 634 MCCFIInstruction::createGnuArgsSize(Label, Size); 635 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 636 if (!CurFrame) 637 return; 638 CurFrame->Instructions.push_back(Instruction); 639 } 640 641 void MCStreamer::emitCFISignalFrame() { 642 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 643 if (!CurFrame) 644 return; 645 CurFrame->IsSignalFrame = true; 646 } 647 648 void MCStreamer::emitCFIUndefined(int64_t Register) { 649 MCSymbol *Label = emitCFILabel(); 650 MCCFIInstruction Instruction = 651 MCCFIInstruction::createUndefined(Label, Register); 652 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 653 if (!CurFrame) 654 return; 655 CurFrame->Instructions.push_back(Instruction); 656 } 657 658 void MCStreamer::emitCFIRegister(int64_t Register1, int64_t Register2) { 659 MCSymbol *Label = emitCFILabel(); 660 MCCFIInstruction Instruction = 661 MCCFIInstruction::createRegister(Label, Register1, Register2); 662 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 663 if (!CurFrame) 664 return; 665 CurFrame->Instructions.push_back(Instruction); 666 } 667 668 void MCStreamer::emitCFIWindowSave() { 669 MCSymbol *Label = emitCFILabel(); 670 MCCFIInstruction Instruction = 671 MCCFIInstruction::createWindowSave(Label); 672 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 673 if (!CurFrame) 674 return; 675 CurFrame->Instructions.push_back(Instruction); 676 } 677 678 void MCStreamer::emitCFINegateRAState() { 679 MCSymbol *Label = emitCFILabel(); 680 MCCFIInstruction Instruction = MCCFIInstruction::createNegateRAState(Label); 681 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 682 if (!CurFrame) 683 return; 684 CurFrame->Instructions.push_back(Instruction); 685 } 686 687 void MCStreamer::emitCFIReturnColumn(int64_t Register) { 688 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); 689 if (!CurFrame) 690 return; 691 CurFrame->RAReg = Register; 692 } 693 694 WinEH::FrameInfo *MCStreamer::EnsureValidWinFrameInfo(SMLoc Loc) { 695 const MCAsmInfo *MAI = Context.getAsmInfo(); 696 if (!MAI->usesWindowsCFI()) { 697 getContext().reportError( 698 Loc, ".seh_* directives are not supported on this target"); 699 return nullptr; 700 } 701 if (!CurrentWinFrameInfo || CurrentWinFrameInfo->End) { 702 getContext().reportError( 703 Loc, ".seh_ directive must appear within an active frame"); 704 return nullptr; 705 } 706 return CurrentWinFrameInfo; 707 } 708 709 void MCStreamer::emitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc) { 710 const MCAsmInfo *MAI = Context.getAsmInfo(); 711 if (!MAI->usesWindowsCFI()) 712 return getContext().reportError( 713 Loc, ".seh_* directives are not supported on this target"); 714 if (CurrentWinFrameInfo && !CurrentWinFrameInfo->End) 715 getContext().reportError( 716 Loc, "Starting a function before ending the previous one!"); 717 718 MCSymbol *StartProc = emitCFILabel(); 719 720 CurrentProcWinFrameInfoStartIndex = WinFrameInfos.size(); 721 WinFrameInfos.emplace_back( 722 std::make_unique<WinEH::FrameInfo>(Symbol, StartProc)); 723 CurrentWinFrameInfo = WinFrameInfos.back().get(); 724 CurrentWinFrameInfo->TextSection = getCurrentSectionOnly(); 725 } 726 727 void MCStreamer::emitWinCFIEndProc(SMLoc Loc) { 728 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 729 if (!CurFrame) 730 return; 731 if (CurFrame->ChainedParent) 732 getContext().reportError(Loc, "Not all chained regions terminated!"); 733 734 MCSymbol *Label = emitCFILabel(); 735 CurFrame->End = Label; 736 if (!CurFrame->FuncletOrFuncEnd) 737 CurFrame->FuncletOrFuncEnd = CurFrame->End; 738 739 for (size_t I = CurrentProcWinFrameInfoStartIndex, E = WinFrameInfos.size(); 740 I != E; ++I) 741 emitWindowsUnwindTables(WinFrameInfos[I].get()); 742 switchSection(CurFrame->TextSection); 743 } 744 745 void MCStreamer::emitWinCFIFuncletOrFuncEnd(SMLoc Loc) { 746 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 747 if (!CurFrame) 748 return; 749 if (CurFrame->ChainedParent) 750 getContext().reportError(Loc, "Not all chained regions terminated!"); 751 752 MCSymbol *Label = emitCFILabel(); 753 CurFrame->FuncletOrFuncEnd = Label; 754 } 755 756 void MCStreamer::emitWinCFIStartChained(SMLoc Loc) { 757 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 758 if (!CurFrame) 759 return; 760 761 MCSymbol *StartProc = emitCFILabel(); 762 763 WinFrameInfos.emplace_back(std::make_unique<WinEH::FrameInfo>( 764 CurFrame->Function, StartProc, CurFrame)); 765 CurrentWinFrameInfo = WinFrameInfos.back().get(); 766 CurrentWinFrameInfo->TextSection = getCurrentSectionOnly(); 767 } 768 769 void MCStreamer::emitWinCFIEndChained(SMLoc Loc) { 770 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 771 if (!CurFrame) 772 return; 773 if (!CurFrame->ChainedParent) 774 return getContext().reportError( 775 Loc, "End of a chained region outside a chained region!"); 776 777 MCSymbol *Label = emitCFILabel(); 778 779 CurFrame->End = Label; 780 CurrentWinFrameInfo = const_cast<WinEH::FrameInfo *>(CurFrame->ChainedParent); 781 } 782 783 void MCStreamer::emitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except, 784 SMLoc Loc) { 785 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 786 if (!CurFrame) 787 return; 788 if (CurFrame->ChainedParent) 789 return getContext().reportError( 790 Loc, "Chained unwind areas can't have handlers!"); 791 CurFrame->ExceptionHandler = Sym; 792 if (!Except && !Unwind) 793 getContext().reportError(Loc, "Don't know what kind of handler this is!"); 794 if (Unwind) 795 CurFrame->HandlesUnwind = true; 796 if (Except) 797 CurFrame->HandlesExceptions = true; 798 } 799 800 void MCStreamer::emitWinEHHandlerData(SMLoc Loc) { 801 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 802 if (!CurFrame) 803 return; 804 if (CurFrame->ChainedParent) 805 getContext().reportError(Loc, "Chained unwind areas can't have handlers!"); 806 } 807 808 void MCStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From, 809 const MCSymbolRefExpr *To, uint64_t Count) { 810 } 811 812 static MCSection *getWinCFISection(MCContext &Context, unsigned *NextWinCFIID, 813 MCSection *MainCFISec, 814 const MCSection *TextSec) { 815 // If this is the main .text section, use the main unwind info section. 816 if (TextSec == Context.getObjectFileInfo()->getTextSection()) 817 return MainCFISec; 818 819 const auto *TextSecCOFF = cast<MCSectionCOFF>(TextSec); 820 auto *MainCFISecCOFF = cast<MCSectionCOFF>(MainCFISec); 821 unsigned UniqueID = TextSecCOFF->getOrAssignWinCFISectionID(NextWinCFIID); 822 823 // If this section is COMDAT, this unwind section should be COMDAT associative 824 // with its group. 825 const MCSymbol *KeySym = nullptr; 826 if (TextSecCOFF->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { 827 KeySym = TextSecCOFF->getCOMDATSymbol(); 828 829 // In a GNU environment, we can't use associative comdats. Instead, do what 830 // GCC does, which is to make plain comdat selectany section named like 831 // ".[px]data$_Z3foov". 832 if (!Context.getAsmInfo()->hasCOFFAssociativeComdats()) { 833 std::string SectionName = (MainCFISecCOFF->getName() + "$" + 834 TextSecCOFF->getName().split('$').second) 835 .str(); 836 return Context.getCOFFSection( 837 SectionName, 838 MainCFISecCOFF->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT, 839 MainCFISecCOFF->getKind(), "", COFF::IMAGE_COMDAT_SELECT_ANY); 840 } 841 } 842 843 return Context.getAssociativeCOFFSection(MainCFISecCOFF, KeySym, UniqueID); 844 } 845 846 MCSection *MCStreamer::getAssociatedPDataSection(const MCSection *TextSec) { 847 return getWinCFISection(getContext(), &NextWinCFIID, 848 getContext().getObjectFileInfo()->getPDataSection(), 849 TextSec); 850 } 851 852 MCSection *MCStreamer::getAssociatedXDataSection(const MCSection *TextSec) { 853 return getWinCFISection(getContext(), &NextWinCFIID, 854 getContext().getObjectFileInfo()->getXDataSection(), 855 TextSec); 856 } 857 858 void MCStreamer::emitSyntaxDirective() {} 859 860 static unsigned encodeSEHRegNum(MCContext &Ctx, MCRegister Reg) { 861 return Ctx.getRegisterInfo()->getSEHRegNum(Reg); 862 } 863 864 void MCStreamer::emitWinCFIPushReg(MCRegister Register, SMLoc Loc) { 865 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 866 if (!CurFrame) 867 return; 868 869 MCSymbol *Label = emitCFILabel(); 870 871 WinEH::Instruction Inst = Win64EH::Instruction::PushNonVol( 872 Label, encodeSEHRegNum(Context, Register)); 873 CurFrame->Instructions.push_back(Inst); 874 } 875 876 void MCStreamer::emitWinCFISetFrame(MCRegister Register, unsigned Offset, 877 SMLoc Loc) { 878 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 879 if (!CurFrame) 880 return; 881 if (CurFrame->LastFrameInst >= 0) 882 return getContext().reportError( 883 Loc, "frame register and offset can be set at most once"); 884 if (Offset & 0x0F) 885 return getContext().reportError(Loc, "offset is not a multiple of 16"); 886 if (Offset > 240) 887 return getContext().reportError( 888 Loc, "frame offset must be less than or equal to 240"); 889 890 MCSymbol *Label = emitCFILabel(); 891 892 WinEH::Instruction Inst = Win64EH::Instruction::SetFPReg( 893 Label, encodeSEHRegNum(getContext(), Register), Offset); 894 CurFrame->LastFrameInst = CurFrame->Instructions.size(); 895 CurFrame->Instructions.push_back(Inst); 896 } 897 898 void MCStreamer::emitWinCFIAllocStack(unsigned Size, SMLoc Loc) { 899 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 900 if (!CurFrame) 901 return; 902 if (Size == 0) 903 return getContext().reportError(Loc, 904 "stack allocation size must be non-zero"); 905 if (Size & 7) 906 return getContext().reportError( 907 Loc, "stack allocation size is not a multiple of 8"); 908 909 MCSymbol *Label = emitCFILabel(); 910 911 WinEH::Instruction Inst = Win64EH::Instruction::Alloc(Label, Size); 912 CurFrame->Instructions.push_back(Inst); 913 } 914 915 void MCStreamer::emitWinCFISaveReg(MCRegister Register, unsigned Offset, 916 SMLoc Loc) { 917 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 918 if (!CurFrame) 919 return; 920 921 if (Offset & 7) 922 return getContext().reportError( 923 Loc, "register save offset is not 8 byte aligned"); 924 925 MCSymbol *Label = emitCFILabel(); 926 927 WinEH::Instruction Inst = Win64EH::Instruction::SaveNonVol( 928 Label, encodeSEHRegNum(Context, Register), Offset); 929 CurFrame->Instructions.push_back(Inst); 930 } 931 932 void MCStreamer::emitWinCFISaveXMM(MCRegister Register, unsigned Offset, 933 SMLoc Loc) { 934 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 935 if (!CurFrame) 936 return; 937 if (Offset & 0x0F) 938 return getContext().reportError(Loc, "offset is not a multiple of 16"); 939 940 MCSymbol *Label = emitCFILabel(); 941 942 WinEH::Instruction Inst = Win64EH::Instruction::SaveXMM( 943 Label, encodeSEHRegNum(Context, Register), Offset); 944 CurFrame->Instructions.push_back(Inst); 945 } 946 947 void MCStreamer::emitWinCFIPushFrame(bool Code, SMLoc Loc) { 948 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 949 if (!CurFrame) 950 return; 951 if (!CurFrame->Instructions.empty()) 952 return getContext().reportError( 953 Loc, "If present, PushMachFrame must be the first UOP"); 954 955 MCSymbol *Label = emitCFILabel(); 956 957 WinEH::Instruction Inst = Win64EH::Instruction::PushMachFrame(Label, Code); 958 CurFrame->Instructions.push_back(Inst); 959 } 960 961 void MCStreamer::emitWinCFIEndProlog(SMLoc Loc) { 962 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc); 963 if (!CurFrame) 964 return; 965 966 MCSymbol *Label = emitCFILabel(); 967 968 CurFrame->PrologEnd = Label; 969 } 970 971 void MCStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {} 972 973 void MCStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {} 974 975 void MCStreamer::emitCOFFSectionIndex(MCSymbol const *Symbol) {} 976 977 void MCStreamer::emitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset) {} 978 979 void MCStreamer::emitCOFFImgRel32(MCSymbol const *Symbol, int64_t Offset) {} 980 981 /// EmitRawText - If this file is backed by an assembly streamer, this dumps 982 /// the specified string in the output .s file. This capability is 983 /// indicated by the hasRawTextSupport() predicate. 984 void MCStreamer::emitRawTextImpl(StringRef String) { 985 // This is not llvm_unreachable for the sake of out of tree backend 986 // developers who may not have assembly streamers and should serve as a 987 // reminder to not accidentally call EmitRawText in the absence of such. 988 report_fatal_error("EmitRawText called on an MCStreamer that doesn't support " 989 "it (target backend is likely missing an AsmStreamer " 990 "implementation)"); 991 } 992 993 void MCStreamer::emitRawText(const Twine &T) { 994 SmallString<128> Str; 995 emitRawTextImpl(T.toStringRef(Str)); 996 } 997 998 void MCStreamer::emitWindowsUnwindTables() {} 999 1000 void MCStreamer::emitWindowsUnwindTables(WinEH::FrameInfo *Frame) {} 1001 1002 void MCStreamer::finish(SMLoc EndLoc) { 1003 if ((!DwarfFrameInfos.empty() && !DwarfFrameInfos.back().End) || 1004 (!WinFrameInfos.empty() && !WinFrameInfos.back()->End)) { 1005 getContext().reportError(EndLoc, "Unfinished frame!"); 1006 return; 1007 } 1008 1009 MCTargetStreamer *TS = getTargetStreamer(); 1010 if (TS) 1011 TS->finish(); 1012 1013 finishImpl(); 1014 } 1015 1016 void MCStreamer::maybeEmitDwarf64Mark() { 1017 if (Context.getDwarfFormat() != dwarf::DWARF64) 1018 return; 1019 AddComment("DWARF64 Mark"); 1020 emitInt32(dwarf::DW_LENGTH_DWARF64); 1021 } 1022 1023 void MCStreamer::emitDwarfUnitLength(uint64_t Length, const Twine &Comment) { 1024 assert(Context.getDwarfFormat() == dwarf::DWARF64 || 1025 Length <= dwarf::DW_LENGTH_lo_reserved); 1026 maybeEmitDwarf64Mark(); 1027 AddComment(Comment); 1028 emitIntValue(Length, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat())); 1029 } 1030 1031 MCSymbol *MCStreamer::emitDwarfUnitLength(const Twine &Prefix, 1032 const Twine &Comment) { 1033 maybeEmitDwarf64Mark(); 1034 AddComment(Comment); 1035 MCSymbol *Lo = Context.createTempSymbol(Prefix + "_start"); 1036 MCSymbol *Hi = Context.createTempSymbol(Prefix + "_end"); 1037 1038 emitAbsoluteSymbolDiff( 1039 Hi, Lo, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat())); 1040 // emit the begin symbol after we generate the length field. 1041 emitLabel(Lo); 1042 // Return the Hi symbol to the caller. 1043 return Hi; 1044 } 1045 1046 void MCStreamer::emitDwarfLineStartLabel(MCSymbol *StartSym) { 1047 // Set the value of the symbol, as we are at the start of the line table. 1048 emitLabel(StartSym); 1049 } 1050 1051 void MCStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 1052 visitUsedExpr(*Value); 1053 Symbol->setVariableValue(Value); 1054 1055 MCTargetStreamer *TS = getTargetStreamer(); 1056 if (TS) 1057 TS->emitAssignment(Symbol, Value); 1058 } 1059 1060 void MCTargetStreamer::prettyPrintAsm(MCInstPrinter &InstPrinter, 1061 uint64_t Address, const MCInst &Inst, 1062 const MCSubtargetInfo &STI, 1063 raw_ostream &OS) { 1064 InstPrinter.printInst(&Inst, Address, "", STI, OS); 1065 } 1066 1067 void MCStreamer::visitUsedSymbol(const MCSymbol &Sym) { 1068 } 1069 1070 void MCStreamer::visitUsedExpr(const MCExpr &Expr) { 1071 switch (Expr.getKind()) { 1072 case MCExpr::Target: 1073 cast<MCTargetExpr>(Expr).visitUsedExpr(*this); 1074 break; 1075 1076 case MCExpr::Constant: 1077 break; 1078 1079 case MCExpr::Binary: { 1080 const MCBinaryExpr &BE = cast<MCBinaryExpr>(Expr); 1081 visitUsedExpr(*BE.getLHS()); 1082 visitUsedExpr(*BE.getRHS()); 1083 break; 1084 } 1085 1086 case MCExpr::SymbolRef: 1087 visitUsedSymbol(cast<MCSymbolRefExpr>(Expr).getSymbol()); 1088 break; 1089 1090 case MCExpr::Unary: 1091 visitUsedExpr(*cast<MCUnaryExpr>(Expr).getSubExpr()); 1092 break; 1093 } 1094 } 1095 1096 void MCStreamer::emitInstruction(const MCInst &Inst, const MCSubtargetInfo &) { 1097 // Scan for values. 1098 for (unsigned i = Inst.getNumOperands(); i--;) 1099 if (Inst.getOperand(i).isExpr()) 1100 visitUsedExpr(*Inst.getOperand(i).getExpr()); 1101 } 1102 1103 void MCStreamer::emitPseudoProbe(uint64_t Guid, uint64_t Index, uint64_t Type, 1104 uint64_t Attr, 1105 const MCPseudoProbeInlineStack &InlineStack) { 1106 auto &Context = getContext(); 1107 1108 // Create a symbol at in the current section for use in the probe. 1109 MCSymbol *ProbeSym = Context.createTempSymbol(); 1110 1111 // Set the value of the symbol to use for the MCPseudoProbe. 1112 emitLabel(ProbeSym); 1113 1114 // Create a (local) probe entry with the symbol. 1115 MCPseudoProbe Probe(ProbeSym, Guid, Index, Type, Attr); 1116 1117 // Add the probe entry to this section's entries. 1118 Context.getMCPseudoProbeTable().getProbeSections().addPseudoProbe( 1119 getCurrentSectionOnly(), Probe, InlineStack); 1120 } 1121 1122 void MCStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, 1123 unsigned Size) { 1124 // Get the Hi-Lo expression. 1125 const MCExpr *Diff = 1126 MCBinaryExpr::createSub(MCSymbolRefExpr::create(Hi, Context), 1127 MCSymbolRefExpr::create(Lo, Context), Context); 1128 1129 const MCAsmInfo *MAI = Context.getAsmInfo(); 1130 if (!MAI->doesSetDirectiveSuppressReloc()) { 1131 emitValue(Diff, Size); 1132 return; 1133 } 1134 1135 // Otherwise, emit with .set (aka assignment). 1136 MCSymbol *SetLabel = Context.createTempSymbol("set"); 1137 emitAssignment(SetLabel, Diff); 1138 emitSymbolValue(SetLabel, Size); 1139 } 1140 1141 void MCStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, 1142 const MCSymbol *Lo) { 1143 // Get the Hi-Lo expression. 1144 const MCExpr *Diff = 1145 MCBinaryExpr::createSub(MCSymbolRefExpr::create(Hi, Context), 1146 MCSymbolRefExpr::create(Lo, Context), Context); 1147 1148 emitULEB128Value(Diff); 1149 } 1150 1151 void MCStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {} 1152 void MCStreamer::emitThumbFunc(MCSymbol *Func) {} 1153 void MCStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {} 1154 void MCStreamer::beginCOFFSymbolDef(const MCSymbol *Symbol) { 1155 llvm_unreachable("this directive only supported on COFF targets"); 1156 } 1157 void MCStreamer::endCOFFSymbolDef() { 1158 llvm_unreachable("this directive only supported on COFF targets"); 1159 } 1160 void MCStreamer::emitFileDirective(StringRef Filename) {} 1161 void MCStreamer::emitFileDirective(StringRef Filename, StringRef CompilerVerion, 1162 StringRef TimeStamp, StringRef Description) { 1163 } 1164 void MCStreamer::emitCOFFSymbolStorageClass(int StorageClass) { 1165 llvm_unreachable("this directive only supported on COFF targets"); 1166 } 1167 void MCStreamer::emitCOFFSymbolType(int Type) { 1168 llvm_unreachable("this directive only supported on COFF targets"); 1169 } 1170 void MCStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size, 1171 MCSymbol *CsectSym, 1172 unsigned ByteAlign) { 1173 llvm_unreachable("this directive only supported on XCOFF targets"); 1174 } 1175 1176 void MCStreamer::emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol, 1177 MCSymbolAttr Linkage, 1178 MCSymbolAttr Visibility) { 1179 llvm_unreachable("emitXCOFFSymbolLinkageWithVisibility is only supported on " 1180 "XCOFF targets"); 1181 } 1182 1183 void MCStreamer::emitXCOFFRenameDirective(const MCSymbol *Name, 1184 StringRef Rename) { 1185 llvm_unreachable("emitXCOFFRenameDirective is only supported on " 1186 "XCOFF targets"); 1187 } 1188 1189 void MCStreamer::emitXCOFFRefDirective(StringRef Name) { 1190 llvm_unreachable("emitXCOFFRefDirective is only supported on XCOFF targets"); 1191 } 1192 1193 void MCStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {} 1194 void MCStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym, 1195 StringRef Name, bool KeepOriginalSym) {} 1196 void MCStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, 1197 unsigned ByteAlignment) {} 1198 void MCStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, 1199 uint64_t Size, unsigned ByteAlignment) {} 1200 void MCStreamer::changeSection(MCSection *, const MCExpr *) {} 1201 void MCStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {} 1202 void MCStreamer::emitBytes(StringRef Data) {} 1203 void MCStreamer::emitBinaryData(StringRef Data) { emitBytes(Data); } 1204 void MCStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) { 1205 visitUsedExpr(*Value); 1206 } 1207 void MCStreamer::emitULEB128Value(const MCExpr *Value) {} 1208 void MCStreamer::emitSLEB128Value(const MCExpr *Value) {} 1209 void MCStreamer::emitFill(const MCExpr &NumBytes, uint64_t Value, SMLoc Loc) {} 1210 void MCStreamer::emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr, 1211 SMLoc Loc) {} 1212 void MCStreamer::emitValueToAlignment(unsigned ByteAlignment, int64_t Value, 1213 unsigned ValueSize, 1214 unsigned MaxBytesToEmit) {} 1215 void MCStreamer::emitCodeAlignment(unsigned ByteAlignment, 1216 const MCSubtargetInfo *STI, 1217 unsigned MaxBytesToEmit) {} 1218 void MCStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value, 1219 SMLoc Loc) {} 1220 void MCStreamer::emitBundleAlignMode(unsigned AlignPow2) {} 1221 void MCStreamer::emitBundleLock(bool AlignToEnd) {} 1222 void MCStreamer::finishImpl() {} 1223 void MCStreamer::emitBundleUnlock() {} 1224 1225 void MCStreamer::switchSection(MCSection *Section, const MCExpr *Subsection) { 1226 assert(Section && "Cannot switch to a null section!"); 1227 MCSectionSubPair curSection = SectionStack.back().first; 1228 SectionStack.back().second = curSection; 1229 if (MCSectionSubPair(Section, Subsection) != curSection) { 1230 changeSection(Section, Subsection); 1231 SectionStack.back().first = MCSectionSubPair(Section, Subsection); 1232 assert(!Section->hasEnded() && "Section already ended"); 1233 MCSymbol *Sym = Section->getBeginSymbol(); 1234 if (Sym && !Sym->isInSection()) 1235 emitLabel(Sym); 1236 } 1237 } 1238 1239 MCSymbol *MCStreamer::endSection(MCSection *Section) { 1240 // TODO: keep track of the last subsection so that this symbol appears in the 1241 // correct place. 1242 MCSymbol *Sym = Section->getEndSymbol(Context); 1243 if (Sym->isInSection()) 1244 return Sym; 1245 1246 switchSection(Section); 1247 emitLabel(Sym); 1248 return Sym; 1249 } 1250 1251 static VersionTuple 1252 targetVersionOrMinimumSupportedOSVersion(const Triple &Target, 1253 VersionTuple TargetVersion) { 1254 VersionTuple Min = Target.getMinimumSupportedOSVersion(); 1255 return !Min.empty() && Min > TargetVersion ? Min : TargetVersion; 1256 } 1257 1258 static MCVersionMinType 1259 getMachoVersionMinLoadCommandType(const Triple &Target) { 1260 assert(Target.isOSDarwin() && "expected a darwin OS"); 1261 switch (Target.getOS()) { 1262 case Triple::MacOSX: 1263 case Triple::Darwin: 1264 return MCVM_OSXVersionMin; 1265 case Triple::IOS: 1266 assert(!Target.isMacCatalystEnvironment() && 1267 "mac Catalyst should use LC_BUILD_VERSION"); 1268 return MCVM_IOSVersionMin; 1269 case Triple::TvOS: 1270 return MCVM_TvOSVersionMin; 1271 case Triple::WatchOS: 1272 return MCVM_WatchOSVersionMin; 1273 default: 1274 break; 1275 } 1276 llvm_unreachable("unexpected OS type"); 1277 } 1278 1279 static VersionTuple getMachoBuildVersionSupportedOS(const Triple &Target) { 1280 assert(Target.isOSDarwin() && "expected a darwin OS"); 1281 switch (Target.getOS()) { 1282 case Triple::MacOSX: 1283 case Triple::Darwin: 1284 return VersionTuple(10, 14); 1285 case Triple::IOS: 1286 // Mac Catalyst always uses the build version load command. 1287 if (Target.isMacCatalystEnvironment()) 1288 return VersionTuple(); 1289 LLVM_FALLTHROUGH; 1290 case Triple::TvOS: 1291 return VersionTuple(12); 1292 case Triple::WatchOS: 1293 return VersionTuple(5); 1294 case Triple::DriverKit: 1295 // DriverKit always uses the build version load command. 1296 return VersionTuple(); 1297 default: 1298 break; 1299 } 1300 llvm_unreachable("unexpected OS type"); 1301 } 1302 1303 static MachO::PlatformType 1304 getMachoBuildVersionPlatformType(const Triple &Target) { 1305 assert(Target.isOSDarwin() && "expected a darwin OS"); 1306 switch (Target.getOS()) { 1307 case Triple::MacOSX: 1308 case Triple::Darwin: 1309 return MachO::PLATFORM_MACOS; 1310 case Triple::IOS: 1311 if (Target.isMacCatalystEnvironment()) 1312 return MachO::PLATFORM_MACCATALYST; 1313 return Target.isSimulatorEnvironment() ? MachO::PLATFORM_IOSSIMULATOR 1314 : MachO::PLATFORM_IOS; 1315 case Triple::TvOS: 1316 return Target.isSimulatorEnvironment() ? MachO::PLATFORM_TVOSSIMULATOR 1317 : MachO::PLATFORM_TVOS; 1318 case Triple::WatchOS: 1319 return Target.isSimulatorEnvironment() ? MachO::PLATFORM_WATCHOSSIMULATOR 1320 : MachO::PLATFORM_WATCHOS; 1321 case Triple::DriverKit: 1322 return MachO::PLATFORM_DRIVERKIT; 1323 default: 1324 break; 1325 } 1326 llvm_unreachable("unexpected OS type"); 1327 } 1328 1329 void MCStreamer::emitVersionForTarget( 1330 const Triple &Target, const VersionTuple &SDKVersion, 1331 const Triple *DarwinTargetVariantTriple, 1332 const VersionTuple &DarwinTargetVariantSDKVersion) { 1333 if (!Target.isOSBinFormatMachO() || !Target.isOSDarwin()) 1334 return; 1335 // Do we even know the version? 1336 if (Target.getOSMajorVersion() == 0) 1337 return; 1338 1339 VersionTuple Version; 1340 switch (Target.getOS()) { 1341 case Triple::MacOSX: 1342 case Triple::Darwin: 1343 Target.getMacOSXVersion(Version); 1344 break; 1345 case Triple::IOS: 1346 case Triple::TvOS: 1347 Version = Target.getiOSVersion(); 1348 break; 1349 case Triple::WatchOS: 1350 Version = Target.getWatchOSVersion(); 1351 break; 1352 case Triple::DriverKit: 1353 Version = Target.getDriverKitVersion(); 1354 break; 1355 default: 1356 llvm_unreachable("unexpected OS type"); 1357 } 1358 assert(Version.getMajor() != 0 && "A non-zero major version is expected"); 1359 auto LinkedTargetVersion = 1360 targetVersionOrMinimumSupportedOSVersion(Target, Version); 1361 auto BuildVersionOSVersion = getMachoBuildVersionSupportedOS(Target); 1362 bool ShouldEmitBuildVersion = false; 1363 if (BuildVersionOSVersion.empty() || 1364 LinkedTargetVersion >= BuildVersionOSVersion) { 1365 if (Target.isMacCatalystEnvironment() && DarwinTargetVariantTriple && 1366 DarwinTargetVariantTriple->isMacOSX()) { 1367 emitVersionForTarget(*DarwinTargetVariantTriple, 1368 DarwinTargetVariantSDKVersion, 1369 /*DarwinTargetVariantTriple=*/nullptr, 1370 /*DarwinTargetVariantSDKVersion=*/VersionTuple()); 1371 emitDarwinTargetVariantBuildVersion( 1372 getMachoBuildVersionPlatformType(Target), 1373 LinkedTargetVersion.getMajor(), 1374 LinkedTargetVersion.getMinor().value_or(0), 1375 LinkedTargetVersion.getSubminor().value_or(0), SDKVersion); 1376 return; 1377 } 1378 emitBuildVersion(getMachoBuildVersionPlatformType(Target), 1379 LinkedTargetVersion.getMajor(), 1380 LinkedTargetVersion.getMinor().value_or(0), 1381 LinkedTargetVersion.getSubminor().value_or(0), SDKVersion); 1382 ShouldEmitBuildVersion = true; 1383 } 1384 1385 if (const Triple *TVT = DarwinTargetVariantTriple) { 1386 if (Target.isMacOSX() && TVT->isMacCatalystEnvironment()) { 1387 auto TVLinkedTargetVersion = 1388 targetVersionOrMinimumSupportedOSVersion(*TVT, TVT->getiOSVersion()); 1389 emitDarwinTargetVariantBuildVersion( 1390 getMachoBuildVersionPlatformType(*TVT), 1391 TVLinkedTargetVersion.getMajor(), 1392 TVLinkedTargetVersion.getMinor().value_or(0), 1393 TVLinkedTargetVersion.getSubminor().value_or(0), 1394 DarwinTargetVariantSDKVersion); 1395 } 1396 } 1397 1398 if (ShouldEmitBuildVersion) 1399 return; 1400 1401 emitVersionMin(getMachoVersionMinLoadCommandType(Target), 1402 LinkedTargetVersion.getMajor(), 1403 LinkedTargetVersion.getMinor().value_or(0), 1404 LinkedTargetVersion.getSubminor().value_or(0), SDKVersion); 1405 } 1406