1 //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===// 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/MCObjectStreamer.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/MC/MCAsmBackend.h" 12 #include "llvm/MC/MCAssembler.h" 13 #include "llvm/MC/MCCodeEmitter.h" 14 #include "llvm/MC/MCCodeView.h" 15 #include "llvm/MC/MCContext.h" 16 #include "llvm/MC/MCDwarf.h" 17 #include "llvm/MC/MCExpr.h" 18 #include "llvm/MC/MCObjectWriter.h" 19 #include "llvm/MC/MCSection.h" 20 #include "llvm/MC/MCSymbol.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/SourceMgr.h" 23 using namespace llvm; 24 25 MCObjectStreamer::MCObjectStreamer(MCContext &Context, 26 std::unique_ptr<MCAsmBackend> TAB, 27 std::unique_ptr<MCObjectWriter> OW, 28 std::unique_ptr<MCCodeEmitter> Emitter) 29 : MCStreamer(Context), 30 Assembler(std::make_unique<MCAssembler>( 31 Context, std::move(TAB), std::move(Emitter), std::move(OW))), 32 EmitEHFrame(true), EmitDebugFrame(false) { 33 if (Assembler->getBackendPtr()) 34 setAllowAutoPadding(Assembler->getBackend().allowAutoPadding()); 35 } 36 37 MCObjectStreamer::~MCObjectStreamer() {} 38 39 // AssemblerPtr is used for evaluation of expressions and causes 40 // difference between asm and object outputs. Return nullptr to in 41 // inline asm mode to limit divergence to assembly inputs. 42 MCAssembler *MCObjectStreamer::getAssemblerPtr() { 43 if (getUseAssemblerInfoForParsing()) 44 return Assembler.get(); 45 return nullptr; 46 } 47 48 void MCObjectStreamer::addPendingLabel(MCSymbol* S) { 49 MCSection *CurSection = getCurrentSectionOnly(); 50 if (CurSection) { 51 // Register labels that have not yet been assigned to a Section. 52 if (!PendingLabels.empty()) { 53 for (MCSymbol* Sym : PendingLabels) 54 CurSection->addPendingLabel(Sym); 55 PendingLabels.clear(); 56 } 57 58 // Add this label to the current Section / Subsection. 59 CurSection->addPendingLabel(S, CurSubsectionIdx); 60 61 // Add this Section to the list of PendingLabelSections. 62 auto SecIt = std::find(PendingLabelSections.begin(), 63 PendingLabelSections.end(), CurSection); 64 if (SecIt == PendingLabelSections.end()) 65 PendingLabelSections.push_back(CurSection); 66 } 67 else 68 // There is no Section / Subsection for this label yet. 69 PendingLabels.push_back(S); 70 } 71 72 void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) { 73 MCSection *CurSection = getCurrentSectionOnly(); 74 if (!CurSection) { 75 assert(PendingLabels.empty()); 76 return; 77 } 78 // Register labels that have not yet been assigned to a Section. 79 if (!PendingLabels.empty()) { 80 for (MCSymbol* Sym : PendingLabels) 81 CurSection->addPendingLabel(Sym, CurSubsectionIdx); 82 PendingLabels.clear(); 83 } 84 85 // Associate a fragment with this label, either the supplied fragment 86 // or an empty data fragment. 87 if (F) 88 CurSection->flushPendingLabels(F, FOffset, CurSubsectionIdx); 89 else 90 CurSection->flushPendingLabels(nullptr, 0, CurSubsectionIdx); 91 } 92 93 void MCObjectStreamer::flushPendingLabels() { 94 // Register labels that have not yet been assigned to a Section. 95 if (!PendingLabels.empty()) { 96 MCSection *CurSection = getCurrentSectionOnly(); 97 assert(CurSection); 98 for (MCSymbol* Sym : PendingLabels) 99 CurSection->addPendingLabel(Sym, CurSubsectionIdx); 100 PendingLabels.clear(); 101 } 102 103 // Assign an empty data fragment to all remaining pending labels. 104 for (MCSection* Section : PendingLabelSections) 105 Section->flushPendingLabels(); 106 } 107 108 // When fixup's offset is a forward declared label, e.g.: 109 // 110 // .reloc 1f, R_MIPS_JALR, foo 111 // 1: nop 112 // 113 // postpone adding it to Fixups vector until the label is defined and its offset 114 // is known. 115 void MCObjectStreamer::resolvePendingFixups() { 116 for (PendingMCFixup &PendingFixup : PendingFixups) { 117 if (!PendingFixup.Sym || PendingFixup.Sym->isUndefined ()) { 118 getContext().reportError(PendingFixup.Fixup.getLoc(), 119 "unresolved relocation offset"); 120 continue; 121 } 122 flushPendingLabels(PendingFixup.DF, PendingFixup.DF->getContents().size()); 123 PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset()); 124 PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup); 125 } 126 PendingFixups.clear(); 127 } 128 129 // As a compile-time optimization, avoid allocating and evaluating an MCExpr 130 // tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment. 131 static Optional<uint64_t> 132 absoluteSymbolDiff(MCAssembler &Asm, const MCSymbol *Hi, const MCSymbol *Lo) { 133 assert(Hi && Lo); 134 if (Asm.getBackendPtr()->requiresDiffExpressionRelocations()) 135 return None; 136 137 if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() || 138 Hi->isVariable() || Lo->isVariable()) 139 return None; 140 141 return Hi->getOffset() - Lo->getOffset(); 142 } 143 144 void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, 145 const MCSymbol *Lo, 146 unsigned Size) { 147 if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) { 148 emitIntValue(*Diff, Size); 149 return; 150 } 151 MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size); 152 } 153 154 void MCObjectStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, 155 const MCSymbol *Lo) { 156 if (Optional<uint64_t> Diff = absoluteSymbolDiff(getAssembler(), Hi, Lo)) { 157 emitULEB128IntValue(*Diff); 158 return; 159 } 160 MCStreamer::emitAbsoluteSymbolDiffAsULEB128(Hi, Lo); 161 } 162 163 void MCObjectStreamer::reset() { 164 if (Assembler) 165 Assembler->reset(); 166 CurInsertionPoint = MCSection::iterator(); 167 EmitEHFrame = true; 168 EmitDebugFrame = false; 169 PendingLabels.clear(); 170 PendingLabelSections.clear(); 171 MCStreamer::reset(); 172 } 173 174 void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) { 175 if (!getNumFrameInfos()) 176 return; 177 178 if (EmitEHFrame) 179 MCDwarfFrameEmitter::Emit(*this, MAB, true); 180 181 if (EmitDebugFrame) 182 MCDwarfFrameEmitter::Emit(*this, MAB, false); 183 } 184 185 MCFragment *MCObjectStreamer::getCurrentFragment() const { 186 assert(getCurrentSectionOnly() && "No current section!"); 187 188 if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin()) 189 return &*std::prev(CurInsertionPoint); 190 191 return nullptr; 192 } 193 194 static bool CanReuseDataFragment(const MCDataFragment &F, 195 const MCAssembler &Assembler, 196 const MCSubtargetInfo *STI) { 197 if (!F.hasInstructions()) 198 return true; 199 // When bundling is enabled, we don't want to add data to a fragment that 200 // already has instructions (see MCELFStreamer::EmitInstToData for details) 201 if (Assembler.isBundlingEnabled()) 202 return Assembler.getRelaxAll(); 203 // If the subtarget is changed mid fragment we start a new fragment to record 204 // the new STI. 205 return !STI || F.getSubtargetInfo() == STI; 206 } 207 208 MCDataFragment * 209 MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) { 210 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); 211 if (!F || !CanReuseDataFragment(*F, *Assembler, STI)) { 212 F = new MCDataFragment(); 213 insert(F); 214 } 215 return F; 216 } 217 218 void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) { 219 Assembler->registerSymbol(Sym); 220 } 221 222 void MCObjectStreamer::emitCFISections(bool EH, bool Debug) { 223 MCStreamer::emitCFISections(EH, Debug); 224 EmitEHFrame = EH; 225 EmitDebugFrame = Debug; 226 } 227 228 void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, 229 SMLoc Loc) { 230 MCStreamer::emitValueImpl(Value, Size, Loc); 231 MCDataFragment *DF = getOrCreateDataFragment(); 232 flushPendingLabels(DF, DF->getContents().size()); 233 234 MCDwarfLineEntry::Make(this, getCurrentSectionOnly()); 235 236 // Avoid fixups when possible. 237 int64_t AbsValue; 238 if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) { 239 if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) { 240 getContext().reportError( 241 Loc, "value evaluated as " + Twine(AbsValue) + " is out of range."); 242 return; 243 } 244 emitIntValue(AbsValue, Size); 245 return; 246 } 247 DF->getFixups().push_back( 248 MCFixup::create(DF->getContents().size(), Value, 249 MCFixup::getKindForSize(Size, false), Loc)); 250 DF->getContents().resize(DF->getContents().size() + Size, 0); 251 } 252 253 MCSymbol *MCObjectStreamer::emitCFILabel() { 254 MCSymbol *Label = getContext().createTempSymbol("cfi", true); 255 emitLabel(Label); 256 return Label; 257 } 258 259 void MCObjectStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { 260 // We need to create a local symbol to avoid relocations. 261 Frame.Begin = getContext().createTempSymbol(); 262 emitLabel(Frame.Begin); 263 } 264 265 void MCObjectStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { 266 Frame.End = getContext().createTempSymbol(); 267 emitLabel(Frame.End); 268 } 269 270 void MCObjectStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { 271 MCStreamer::emitLabel(Symbol, Loc); 272 273 getAssembler().registerSymbol(*Symbol); 274 275 // If there is a current fragment, mark the symbol as pointing into it. 276 // Otherwise queue the label and set its fragment pointer when we emit the 277 // next fragment. 278 auto *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); 279 if (F && !(getAssembler().isBundlingEnabled() && 280 getAssembler().getRelaxAll())) { 281 Symbol->setFragment(F); 282 Symbol->setOffset(F->getContents().size()); 283 } else { 284 // Assign all pending labels to offset 0 within the dummy "pending" 285 // fragment. (They will all be reassigned to a real fragment in 286 // flushPendingLabels()) 287 Symbol->setOffset(0); 288 addPendingLabel(Symbol); 289 } 290 } 291 292 // Emit a label at a previously emitted fragment/offset position. This must be 293 // within the currently-active section. 294 void MCObjectStreamer::emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, 295 MCFragment *F, uint64_t Offset) { 296 assert(F->getParent() == getCurrentSectionOnly()); 297 298 MCStreamer::emitLabel(Symbol, Loc); 299 getAssembler().registerSymbol(*Symbol); 300 auto *DF = dyn_cast_or_null<MCDataFragment>(F); 301 Symbol->setOffset(Offset); 302 if (DF) { 303 Symbol->setFragment(F); 304 } else { 305 assert(isa<MCDummyFragment>(F) && 306 "F must either be an MCDataFragment or the pending MCDummyFragment"); 307 assert(Offset == 0); 308 addPendingLabel(Symbol); 309 } 310 } 311 312 void MCObjectStreamer::emitULEB128Value(const MCExpr *Value) { 313 int64_t IntValue; 314 if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) { 315 emitULEB128IntValue(IntValue); 316 return; 317 } 318 insert(new MCLEBFragment(*Value, false)); 319 } 320 321 void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) { 322 int64_t IntValue; 323 if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) { 324 emitSLEB128IntValue(IntValue); 325 return; 326 } 327 insert(new MCLEBFragment(*Value, true)); 328 } 329 330 void MCObjectStreamer::emitWeakReference(MCSymbol *Alias, 331 const MCSymbol *Symbol) { 332 report_fatal_error("This file format doesn't support weak aliases."); 333 } 334 335 void MCObjectStreamer::ChangeSection(MCSection *Section, 336 const MCExpr *Subsection) { 337 changeSectionImpl(Section, Subsection); 338 } 339 340 bool MCObjectStreamer::changeSectionImpl(MCSection *Section, 341 const MCExpr *Subsection) { 342 assert(Section && "Cannot switch to a null section!"); 343 getContext().clearDwarfLocSeen(); 344 345 bool Created = getAssembler().registerSection(*Section); 346 347 int64_t IntSubsection = 0; 348 if (Subsection && 349 !Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr())) 350 report_fatal_error("Cannot evaluate subsection number"); 351 if (IntSubsection < 0 || IntSubsection > 8192) 352 report_fatal_error("Subsection number out of range"); 353 CurSubsectionIdx = unsigned(IntSubsection); 354 CurInsertionPoint = 355 Section->getSubsectionInsertionPoint(CurSubsectionIdx); 356 return Created; 357 } 358 359 void MCObjectStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 360 getAssembler().registerSymbol(*Symbol); 361 MCStreamer::emitAssignment(Symbol, Value); 362 } 363 364 bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const { 365 return Sec.hasInstructions(); 366 } 367 368 void MCObjectStreamer::emitInstruction(const MCInst &Inst, 369 const MCSubtargetInfo &STI) { 370 const MCSection &Sec = *getCurrentSectionOnly(); 371 if (Sec.isVirtualSection()) { 372 getContext().reportError(Inst.getLoc(), Twine(Sec.getVirtualSectionKind()) + 373 " section '" + Sec.getName() + 374 "' cannot have instructions"); 375 return; 376 } 377 getAssembler().getBackend().emitInstructionBegin(*this, Inst); 378 emitInstructionImpl(Inst, STI); 379 getAssembler().getBackend().emitInstructionEnd(*this, Inst); 380 } 381 382 void MCObjectStreamer::emitInstructionImpl(const MCInst &Inst, 383 const MCSubtargetInfo &STI) { 384 MCStreamer::emitInstruction(Inst, STI); 385 386 MCSection *Sec = getCurrentSectionOnly(); 387 Sec->setHasInstructions(true); 388 389 // Now that a machine instruction has been assembled into this section, make 390 // a line entry for any .loc directive that has been seen. 391 MCDwarfLineEntry::Make(this, getCurrentSectionOnly()); 392 393 // If this instruction doesn't need relaxation, just emit it as data. 394 MCAssembler &Assembler = getAssembler(); 395 MCAsmBackend &Backend = Assembler.getBackend(); 396 if (!(Backend.mayNeedRelaxation(Inst, STI) || 397 Backend.allowEnhancedRelaxation())) { 398 EmitInstToData(Inst, STI); 399 return; 400 } 401 402 // Otherwise, relax and emit it as data if either: 403 // - The RelaxAll flag was passed 404 // - Bundling is enabled and this instruction is inside a bundle-locked 405 // group. We want to emit all such instructions into the same data 406 // fragment. 407 if (Assembler.getRelaxAll() || 408 (Assembler.isBundlingEnabled() && Sec->isBundleLocked())) { 409 MCInst Relaxed = Inst; 410 while (getAssembler().getBackend().mayNeedRelaxation(Relaxed, STI)) 411 getAssembler().getBackend().relaxInstruction(Relaxed, STI, Relaxed); 412 EmitInstToData(Relaxed, STI); 413 return; 414 } 415 416 // Otherwise emit to a separate fragment. 417 EmitInstToFragment(Inst, STI); 418 } 419 420 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst, 421 const MCSubtargetInfo &STI) { 422 if (getAssembler().getRelaxAll() && getAssembler().isBundlingEnabled()) 423 llvm_unreachable("All instructions should have already been relaxed"); 424 425 // Always create a new, separate fragment here, because its size can change 426 // during relaxation. 427 MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI); 428 insert(IF); 429 430 SmallString<128> Code; 431 raw_svector_ostream VecOS(Code); 432 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(), 433 STI); 434 IF->getContents().append(Code.begin(), Code.end()); 435 } 436 437 #ifndef NDEBUG 438 static const char *const BundlingNotImplementedMsg = 439 "Aligned bundling is not implemented for this object format"; 440 #endif 441 442 void MCObjectStreamer::emitBundleAlignMode(unsigned AlignPow2) { 443 llvm_unreachable(BundlingNotImplementedMsg); 444 } 445 446 void MCObjectStreamer::emitBundleLock(bool AlignToEnd) { 447 llvm_unreachable(BundlingNotImplementedMsg); 448 } 449 450 void MCObjectStreamer::emitBundleUnlock() { 451 llvm_unreachable(BundlingNotImplementedMsg); 452 } 453 454 void MCObjectStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line, 455 unsigned Column, unsigned Flags, 456 unsigned Isa, 457 unsigned Discriminator, 458 StringRef FileName) { 459 // In case we see two .loc directives in a row, make sure the 460 // first one gets a line entry. 461 MCDwarfLineEntry::Make(this, getCurrentSectionOnly()); 462 463 this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa, 464 Discriminator, FileName); 465 } 466 467 static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, 468 const MCSymbol *B) { 469 MCContext &Context = OS.getContext(); 470 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 471 const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context); 472 const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context); 473 const MCExpr *AddrDelta = 474 MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context); 475 return AddrDelta; 476 } 477 478 static void emitDwarfSetLineAddr(MCObjectStreamer &OS, 479 MCDwarfLineTableParams Params, 480 int64_t LineDelta, const MCSymbol *Label, 481 int PointerSize) { 482 // emit the sequence to set the address 483 OS.emitIntValue(dwarf::DW_LNS_extended_op, 1); 484 OS.emitULEB128IntValue(PointerSize + 1); 485 OS.emitIntValue(dwarf::DW_LNE_set_address, 1); 486 OS.emitSymbolValue(Label, PointerSize); 487 488 // emit the sequence for the LineDelta (from 1) and a zero address delta. 489 MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0); 490 } 491 492 void MCObjectStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta, 493 const MCSymbol *LastLabel, 494 const MCSymbol *Label, 495 unsigned PointerSize) { 496 if (!LastLabel) { 497 emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta, 498 Label, PointerSize); 499 return; 500 } 501 const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); 502 int64_t Res; 503 if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) { 504 MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta, 505 Res); 506 return; 507 } 508 insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); 509 } 510 511 void MCObjectStreamer::emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, 512 const MCSymbol *Label) { 513 const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); 514 int64_t Res; 515 if (AddrDelta->evaluateAsAbsolute(Res, getAssemblerPtr())) { 516 MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res); 517 return; 518 } 519 insert(new MCDwarfCallFrameFragment(*AddrDelta)); 520 } 521 522 void MCObjectStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, 523 unsigned Line, unsigned Column, 524 bool PrologueEnd, bool IsStmt, 525 StringRef FileName, SMLoc Loc) { 526 // Validate the directive. 527 if (!checkCVLocSection(FunctionId, FileNo, Loc)) 528 return; 529 530 // Emit a label at the current position and record it in the CodeViewContext. 531 MCSymbol *LineSym = getContext().createTempSymbol(); 532 emitLabel(LineSym); 533 getContext().getCVContext().recordCVLoc(getContext(), LineSym, FunctionId, 534 FileNo, Line, Column, PrologueEnd, 535 IsStmt); 536 } 537 538 void MCObjectStreamer::EmitCVLinetableDirective(unsigned FunctionId, 539 const MCSymbol *Begin, 540 const MCSymbol *End) { 541 getContext().getCVContext().emitLineTableForFunction(*this, FunctionId, Begin, 542 End); 543 this->MCStreamer::EmitCVLinetableDirective(FunctionId, Begin, End); 544 } 545 546 void MCObjectStreamer::EmitCVInlineLinetableDirective( 547 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, 548 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) { 549 getContext().getCVContext().emitInlineLineTableForFunction( 550 *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, 551 FnEndSym); 552 this->MCStreamer::EmitCVInlineLinetableDirective( 553 PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym); 554 } 555 556 void MCObjectStreamer::EmitCVDefRangeDirective( 557 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 558 StringRef FixedSizePortion) { 559 MCFragment *Frag = 560 getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion); 561 // Attach labels that were pending before we created the defrange fragment to 562 // the beginning of the new fragment. 563 flushPendingLabels(Frag, 0); 564 this->MCStreamer::EmitCVDefRangeDirective(Ranges, FixedSizePortion); 565 } 566 567 void MCObjectStreamer::EmitCVStringTableDirective() { 568 getContext().getCVContext().emitStringTable(*this); 569 } 570 void MCObjectStreamer::EmitCVFileChecksumsDirective() { 571 getContext().getCVContext().emitFileChecksums(*this); 572 } 573 574 void MCObjectStreamer::EmitCVFileChecksumOffsetDirective(unsigned FileNo) { 575 getContext().getCVContext().emitFileChecksumOffset(*this, FileNo); 576 } 577 578 void MCObjectStreamer::emitBytes(StringRef Data) { 579 MCDwarfLineEntry::Make(this, getCurrentSectionOnly()); 580 MCDataFragment *DF = getOrCreateDataFragment(); 581 flushPendingLabels(DF, DF->getContents().size()); 582 DF->getContents().append(Data.begin(), Data.end()); 583 } 584 585 void MCObjectStreamer::emitValueToAlignment(unsigned ByteAlignment, 586 int64_t Value, 587 unsigned ValueSize, 588 unsigned MaxBytesToEmit) { 589 if (MaxBytesToEmit == 0) 590 MaxBytesToEmit = ByteAlignment; 591 insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit)); 592 593 // Update the maximum alignment on the current section if necessary. 594 MCSection *CurSec = getCurrentSectionOnly(); 595 if (ByteAlignment > CurSec->getAlignment()) 596 CurSec->setAlignment(Align(ByteAlignment)); 597 } 598 599 void MCObjectStreamer::emitCodeAlignment(unsigned ByteAlignment, 600 unsigned MaxBytesToEmit) { 601 emitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit); 602 cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true); 603 } 604 605 void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset, 606 unsigned char Value, 607 SMLoc Loc) { 608 insert(new MCOrgFragment(*Offset, Value, Loc)); 609 } 610 611 // Associate DTPRel32 fixup with data and resize data area 612 void MCObjectStreamer::emitDTPRel32Value(const MCExpr *Value) { 613 MCDataFragment *DF = getOrCreateDataFragment(); 614 flushPendingLabels(DF, DF->getContents().size()); 615 616 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), 617 Value, FK_DTPRel_4)); 618 DF->getContents().resize(DF->getContents().size() + 4, 0); 619 } 620 621 // Associate DTPRel64 fixup with data and resize data area 622 void MCObjectStreamer::emitDTPRel64Value(const MCExpr *Value) { 623 MCDataFragment *DF = getOrCreateDataFragment(); 624 flushPendingLabels(DF, DF->getContents().size()); 625 626 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), 627 Value, FK_DTPRel_8)); 628 DF->getContents().resize(DF->getContents().size() + 8, 0); 629 } 630 631 // Associate TPRel32 fixup with data and resize data area 632 void MCObjectStreamer::emitTPRel32Value(const MCExpr *Value) { 633 MCDataFragment *DF = getOrCreateDataFragment(); 634 flushPendingLabels(DF, DF->getContents().size()); 635 636 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), 637 Value, FK_TPRel_4)); 638 DF->getContents().resize(DF->getContents().size() + 4, 0); 639 } 640 641 // Associate TPRel64 fixup with data and resize data area 642 void MCObjectStreamer::emitTPRel64Value(const MCExpr *Value) { 643 MCDataFragment *DF = getOrCreateDataFragment(); 644 flushPendingLabels(DF, DF->getContents().size()); 645 646 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), 647 Value, FK_TPRel_8)); 648 DF->getContents().resize(DF->getContents().size() + 8, 0); 649 } 650 651 // Associate GPRel32 fixup with data and resize data area 652 void MCObjectStreamer::emitGPRel32Value(const MCExpr *Value) { 653 MCDataFragment *DF = getOrCreateDataFragment(); 654 flushPendingLabels(DF, DF->getContents().size()); 655 656 DF->getFixups().push_back( 657 MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4)); 658 DF->getContents().resize(DF->getContents().size() + 4, 0); 659 } 660 661 // Associate GPRel64 fixup with data and resize data area 662 void MCObjectStreamer::emitGPRel64Value(const MCExpr *Value) { 663 MCDataFragment *DF = getOrCreateDataFragment(); 664 flushPendingLabels(DF, DF->getContents().size()); 665 666 DF->getFixups().push_back( 667 MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4)); 668 DF->getContents().resize(DF->getContents().size() + 8, 0); 669 } 670 671 bool MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name, 672 const MCExpr *Expr, SMLoc Loc, 673 const MCSubtargetInfo &STI) { 674 Optional<MCFixupKind> MaybeKind = Assembler->getBackend().getFixupKind(Name); 675 if (!MaybeKind.hasValue()) 676 return true; 677 678 MCFixupKind Kind = *MaybeKind; 679 680 if (Expr == nullptr) 681 Expr = 682 MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext()); 683 684 MCDataFragment *DF = getOrCreateDataFragment(&STI); 685 flushPendingLabels(DF, DF->getContents().size()); 686 687 int64_t OffsetValue; 688 if (Offset.evaluateAsAbsolute(OffsetValue)) { 689 if (OffsetValue < 0) 690 llvm_unreachable(".reloc offset is negative"); 691 DF->getFixups().push_back(MCFixup::create(OffsetValue, Expr, Kind, Loc)); 692 return false; 693 } 694 695 if (Offset.getKind() != llvm::MCExpr::SymbolRef) 696 llvm_unreachable(".reloc offset is not absolute nor a label"); 697 698 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Offset); 699 if (SRE.getSymbol().isDefined()) { 700 DF->getFixups().push_back(MCFixup::create(SRE.getSymbol().getOffset(), 701 Expr, Kind, Loc)); 702 return false; 703 } 704 705 PendingFixups.emplace_back(&SRE.getSymbol(), DF, 706 MCFixup::create(-1, Expr, Kind, Loc)); 707 return false; 708 } 709 710 void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue, 711 SMLoc Loc) { 712 MCDataFragment *DF = getOrCreateDataFragment(); 713 flushPendingLabels(DF, DF->getContents().size()); 714 715 assert(getCurrentSectionOnly() && "need a section"); 716 insert(new MCFillFragment(FillValue, 1, NumBytes, Loc)); 717 } 718 719 void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size, 720 int64_t Expr, SMLoc Loc) { 721 int64_t IntNumValues; 722 // Do additional checking now if we can resolve the value. 723 if (NumValues.evaluateAsAbsolute(IntNumValues, getAssemblerPtr())) { 724 if (IntNumValues < 0) { 725 getContext().getSourceManager()->PrintMessage( 726 Loc, SourceMgr::DK_Warning, 727 "'.fill' directive with negative repeat count has no effect"); 728 return; 729 } 730 // Emit now if we can for better errors. 731 int64_t NonZeroSize = Size > 4 ? 4 : Size; 732 Expr &= ~0ULL >> (64 - NonZeroSize * 8); 733 for (uint64_t i = 0, e = IntNumValues; i != e; ++i) { 734 emitIntValue(Expr, NonZeroSize); 735 if (NonZeroSize < Size) 736 emitIntValue(0, Size - NonZeroSize); 737 } 738 return; 739 } 740 741 // Otherwise emit as fragment. 742 MCDataFragment *DF = getOrCreateDataFragment(); 743 flushPendingLabels(DF, DF->getContents().size()); 744 745 assert(getCurrentSectionOnly() && "need a section"); 746 insert(new MCFillFragment(Expr, Size, NumValues, Loc)); 747 } 748 749 void MCObjectStreamer::emitFileDirective(StringRef Filename) { 750 getAssembler().addFileName(Filename); 751 } 752 753 void MCObjectStreamer::emitAddrsig() { 754 getAssembler().getWriter().emitAddrsigSection(); 755 } 756 757 void MCObjectStreamer::emitAddrsigSym(const MCSymbol *Sym) { 758 getAssembler().registerSymbol(*Sym); 759 getAssembler().getWriter().addAddrsigSymbol(Sym); 760 } 761 762 void MCObjectStreamer::FinishImpl() { 763 getContext().RemapDebugPaths(); 764 765 // If we are generating dwarf for assembly source files dump out the sections. 766 if (getContext().getGenDwarfForAssembly()) 767 MCGenDwarfInfo::Emit(this); 768 769 // Dump out the dwarf file & directory tables and line tables. 770 MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams()); 771 772 // Update any remaining pending labels with empty data fragments. 773 flushPendingLabels(); 774 775 resolvePendingFixups(); 776 getAssembler().Finish(); 777 } 778