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