1 //===- lib/MC/MCELFStreamer.cpp - ELF Object 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 // This file assembles .s files and emits ELF .o object files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/MC/MCELFStreamer.h" 14 #include "llvm/ADT/SmallString.h" 15 #include "llvm/ADT/SmallVector.h" 16 #include "llvm/BinaryFormat/ELF.h" 17 #include "llvm/MC/MCAsmBackend.h" 18 #include "llvm/MC/MCAsmInfo.h" 19 #include "llvm/MC/MCAssembler.h" 20 #include "llvm/MC/MCCodeEmitter.h" 21 #include "llvm/MC/MCContext.h" 22 #include "llvm/MC/MCExpr.h" 23 #include "llvm/MC/MCFixup.h" 24 #include "llvm/MC/MCFragment.h" 25 #include "llvm/MC/MCObjectFileInfo.h" 26 #include "llvm/MC/MCObjectWriter.h" 27 #include "llvm/MC/MCSection.h" 28 #include "llvm/MC/MCSectionELF.h" 29 #include "llvm/MC/MCStreamer.h" 30 #include "llvm/MC/MCSymbol.h" 31 #include "llvm/MC/MCSymbolELF.h" 32 #include "llvm/Support/Casting.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/TargetRegistry.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include <cassert> 37 #include <cstdint> 38 39 using namespace llvm; 40 41 MCELFStreamer::MCELFStreamer(MCContext &Context, 42 std::unique_ptr<MCAsmBackend> TAB, 43 std::unique_ptr<MCObjectWriter> OW, 44 std::unique_ptr<MCCodeEmitter> Emitter) 45 : MCObjectStreamer(Context, std::move(TAB), std::move(OW), 46 std::move(Emitter)) {} 47 48 bool MCELFStreamer::isBundleLocked() const { 49 return getCurrentSectionOnly()->isBundleLocked(); 50 } 51 52 void MCELFStreamer::mergeFragment(MCDataFragment *DF, 53 MCDataFragment *EF) { 54 MCAssembler &Assembler = getAssembler(); 55 56 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) { 57 uint64_t FSize = EF->getContents().size(); 58 59 if (FSize > Assembler.getBundleAlignSize()) 60 report_fatal_error("Fragment can't be larger than a bundle size"); 61 62 uint64_t RequiredBundlePadding = computeBundlePadding( 63 Assembler, EF, DF->getContents().size(), FSize); 64 65 if (RequiredBundlePadding > UINT8_MAX) 66 report_fatal_error("Padding cannot exceed 255 bytes"); 67 68 if (RequiredBundlePadding > 0) { 69 SmallString<256> Code; 70 raw_svector_ostream VecOS(Code); 71 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding)); 72 Assembler.writeFragmentPadding(VecOS, *EF, FSize); 73 74 DF->getContents().append(Code.begin(), Code.end()); 75 } 76 } 77 78 flushPendingLabels(DF, DF->getContents().size()); 79 80 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) { 81 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() + 82 DF->getContents().size()); 83 DF->getFixups().push_back(EF->getFixups()[i]); 84 } 85 if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo()) 86 DF->setHasInstructions(*EF->getSubtargetInfo()); 87 DF->getContents().append(EF->getContents().begin(), EF->getContents().end()); 88 } 89 90 void MCELFStreamer::InitSections(bool NoExecStack) { 91 MCContext &Ctx = getContext(); 92 SwitchSection(Ctx.getObjectFileInfo()->getTextSection()); 93 EmitCodeAlignment(4); 94 95 if (NoExecStack) 96 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx)); 97 } 98 99 void MCELFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) { 100 auto *Symbol = cast<MCSymbolELF>(S); 101 MCObjectStreamer::EmitLabel(Symbol, Loc); 102 103 const MCSectionELF &Section = 104 static_cast<const MCSectionELF &>(*getCurrentSectionOnly()); 105 if (Section.getFlags() & ELF::SHF_TLS) 106 Symbol->setType(ELF::STT_TLS); 107 } 108 109 void MCELFStreamer::EmitLabelAtPos(MCSymbol *S, SMLoc Loc, MCFragment *F, 110 uint64_t Offset) { 111 auto *Symbol = cast<MCSymbolELF>(S); 112 MCObjectStreamer::EmitLabelAtPos(Symbol, Loc, F, Offset); 113 114 const MCSectionELF &Section = 115 static_cast<const MCSectionELF &>(*getCurrentSectionOnly()); 116 if (Section.getFlags() & ELF::SHF_TLS) 117 Symbol->setType(ELF::STT_TLS); 118 } 119 120 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { 121 // Let the target do whatever target specific stuff it needs to do. 122 getAssembler().getBackend().handleAssemblerFlag(Flag); 123 // Do any generic stuff we need to do. 124 switch (Flag) { 125 case MCAF_SyntaxUnified: return; // no-op here. 126 case MCAF_Code16: return; // Change parsing mode; no-op here. 127 case MCAF_Code32: return; // Change parsing mode; no-op here. 128 case MCAF_Code64: return; // Change parsing mode; no-op here. 129 case MCAF_SubsectionsViaSymbols: 130 getAssembler().setSubsectionsViaSymbols(true); 131 return; 132 } 133 134 llvm_unreachable("invalid assembler flag!"); 135 } 136 137 // If bundle alignment is used and there are any instructions in the section, it 138 // needs to be aligned to at least the bundle size. 139 static void setSectionAlignmentForBundling(const MCAssembler &Assembler, 140 MCSection *Section) { 141 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() && 142 Section->getAlignment() < Assembler.getBundleAlignSize()) 143 Section->setAlignment(Align(Assembler.getBundleAlignSize())); 144 } 145 146 void MCELFStreamer::ChangeSection(MCSection *Section, 147 const MCExpr *Subsection) { 148 MCSection *CurSection = getCurrentSectionOnly(); 149 if (CurSection && isBundleLocked()) 150 report_fatal_error("Unterminated .bundle_lock when changing a section"); 151 152 MCAssembler &Asm = getAssembler(); 153 // Ensure the previous section gets aligned if necessary. 154 setSectionAlignmentForBundling(Asm, CurSection); 155 auto *SectionELF = static_cast<const MCSectionELF *>(Section); 156 const MCSymbol *Grp = SectionELF->getGroup(); 157 if (Grp) 158 Asm.registerSymbol(*Grp); 159 160 changeSectionImpl(Section, Subsection); 161 Asm.registerSymbol(*Section->getBeginSymbol()); 162 } 163 164 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { 165 getAssembler().registerSymbol(*Symbol); 166 const MCExpr *Value = MCSymbolRefExpr::create( 167 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext()); 168 Alias->setVariableValue(Value); 169 } 170 171 // When GNU as encounters more than one .type declaration for an object it seems 172 // to use a mechanism similar to the one below to decide which type is actually 173 // used in the object file. The greater of T1 and T2 is selected based on the 174 // following ordering: 175 // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else 176 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user 177 // provided type). 178 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) { 179 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC, 180 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) { 181 if (T1 == Type) 182 return T2; 183 if (T2 == Type) 184 return T1; 185 } 186 187 return T2; 188 } 189 190 bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { 191 auto *Symbol = cast<MCSymbolELF>(S); 192 193 // Adding a symbol attribute always introduces the symbol, note that an 194 // important side effect of calling registerSymbol here is to register 195 // the symbol with the assembler. 196 getAssembler().registerSymbol(*Symbol); 197 198 // The implementation of symbol attributes is designed to match 'as', but it 199 // leaves much to desired. It doesn't really make sense to arbitrarily add and 200 // remove flags, but 'as' allows this (in particular, see .desc). 201 // 202 // In the future it might be worth trying to make these operations more well 203 // defined. 204 switch (Attribute) { 205 case MCSA_Cold: 206 case MCSA_LazyReference: 207 case MCSA_Reference: 208 case MCSA_SymbolResolver: 209 case MCSA_PrivateExtern: 210 case MCSA_WeakDefinition: 211 case MCSA_WeakDefAutoPrivate: 212 case MCSA_Invalid: 213 case MCSA_IndirectSymbol: 214 return false; 215 216 case MCSA_NoDeadStrip: 217 // Ignore for now. 218 break; 219 220 case MCSA_ELF_TypeGnuUniqueObject: 221 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT)); 222 Symbol->setBinding(ELF::STB_GNU_UNIQUE); 223 Symbol->setExternal(true); 224 break; 225 226 case MCSA_Global: 227 Symbol->setBinding(ELF::STB_GLOBAL); 228 Symbol->setExternal(true); 229 break; 230 231 case MCSA_WeakReference: 232 case MCSA_Weak: 233 Symbol->setBinding(ELF::STB_WEAK); 234 Symbol->setExternal(true); 235 break; 236 237 case MCSA_Local: 238 Symbol->setBinding(ELF::STB_LOCAL); 239 Symbol->setExternal(false); 240 break; 241 242 case MCSA_ELF_TypeFunction: 243 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC)); 244 break; 245 246 case MCSA_ELF_TypeIndFunction: 247 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC)); 248 break; 249 250 case MCSA_ELF_TypeObject: 251 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT)); 252 break; 253 254 case MCSA_ELF_TypeTLS: 255 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS)); 256 break; 257 258 case MCSA_ELF_TypeCommon: 259 // TODO: Emit these as a common symbol. 260 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT)); 261 break; 262 263 case MCSA_ELF_TypeNoType: 264 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE)); 265 break; 266 267 case MCSA_Protected: 268 Symbol->setVisibility(ELF::STV_PROTECTED); 269 break; 270 271 case MCSA_Hidden: 272 Symbol->setVisibility(ELF::STV_HIDDEN); 273 break; 274 275 case MCSA_Internal: 276 Symbol->setVisibility(ELF::STV_INTERNAL); 277 break; 278 279 case MCSA_AltEntry: 280 llvm_unreachable("ELF doesn't support the .alt_entry attribute"); 281 282 case MCSA_LGlobal: 283 llvm_unreachable("ELF doesn't support the .lglobl attribute"); 284 } 285 286 return true; 287 } 288 289 void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size, 290 unsigned ByteAlignment) { 291 auto *Symbol = cast<MCSymbolELF>(S); 292 getAssembler().registerSymbol(*Symbol); 293 294 if (!Symbol->isBindingSet()) { 295 Symbol->setBinding(ELF::STB_GLOBAL); 296 Symbol->setExternal(true); 297 } 298 299 Symbol->setType(ELF::STT_OBJECT); 300 301 if (Symbol->getBinding() == ELF::STB_LOCAL) { 302 MCSection &Section = *getAssembler().getContext().getELFSection( 303 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 304 MCSectionSubPair P = getCurrentSection(); 305 SwitchSection(&Section); 306 307 EmitValueToAlignment(ByteAlignment, 0, 1, 0); 308 EmitLabel(Symbol); 309 EmitZeros(Size); 310 311 SwitchSection(P.first, P.second); 312 } else { 313 if(Symbol->declareCommon(Size, ByteAlignment)) 314 report_fatal_error("Symbol: " + Symbol->getName() + 315 " redeclared as different type"); 316 } 317 318 cast<MCSymbolELF>(Symbol) 319 ->setSize(MCConstantExpr::create(Size, getContext())); 320 } 321 322 void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) { 323 cast<MCSymbolELF>(Symbol)->setSize(Value); 324 } 325 326 void MCELFStreamer::emitELFSymverDirective(StringRef AliasName, 327 const MCSymbol *Aliasee) { 328 getAssembler().Symvers.push_back({AliasName, Aliasee}); 329 } 330 331 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size, 332 unsigned ByteAlignment) { 333 auto *Symbol = cast<MCSymbolELF>(S); 334 // FIXME: Should this be caught and done earlier? 335 getAssembler().registerSymbol(*Symbol); 336 Symbol->setBinding(ELF::STB_LOCAL); 337 Symbol->setExternal(false); 338 EmitCommonSymbol(Symbol, Size, ByteAlignment); 339 } 340 341 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, 342 SMLoc Loc) { 343 if (isBundleLocked()) 344 report_fatal_error("Emitting values inside a locked bundle is forbidden"); 345 fixSymbolsInTLSFixups(Value); 346 MCObjectStreamer::EmitValueImpl(Value, Size, Loc); 347 } 348 349 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, 350 int64_t Value, 351 unsigned ValueSize, 352 unsigned MaxBytesToEmit) { 353 if (isBundleLocked()) 354 report_fatal_error("Emitting values inside a locked bundle is forbidden"); 355 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, 356 ValueSize, MaxBytesToEmit); 357 } 358 359 void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From, 360 const MCSymbolRefExpr *To, 361 uint64_t Count) { 362 getAssembler().CGProfile.push_back({From, To, Count}); 363 } 364 365 void MCELFStreamer::EmitIdent(StringRef IdentString) { 366 MCSection *Comment = getAssembler().getContext().getELFSection( 367 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); 368 PushSection(); 369 SwitchSection(Comment); 370 if (!SeenIdent) { 371 EmitIntValue(0, 1); 372 SeenIdent = true; 373 } 374 EmitBytes(IdentString); 375 EmitIntValue(0, 1); 376 PopSection(); 377 } 378 379 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { 380 switch (expr->getKind()) { 381 case MCExpr::Target: 382 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler()); 383 break; 384 case MCExpr::Constant: 385 break; 386 387 case MCExpr::Binary: { 388 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); 389 fixSymbolsInTLSFixups(be->getLHS()); 390 fixSymbolsInTLSFixups(be->getRHS()); 391 break; 392 } 393 394 case MCExpr::SymbolRef: { 395 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); 396 switch (symRef.getKind()) { 397 default: 398 return; 399 case MCSymbolRefExpr::VK_GOTTPOFF: 400 case MCSymbolRefExpr::VK_INDNTPOFF: 401 case MCSymbolRefExpr::VK_NTPOFF: 402 case MCSymbolRefExpr::VK_GOTNTPOFF: 403 case MCSymbolRefExpr::VK_TLSCALL: 404 case MCSymbolRefExpr::VK_TLSDESC: 405 case MCSymbolRefExpr::VK_TLSGD: 406 case MCSymbolRefExpr::VK_TLSLD: 407 case MCSymbolRefExpr::VK_TLSLDM: 408 case MCSymbolRefExpr::VK_TPOFF: 409 case MCSymbolRefExpr::VK_TPREL: 410 case MCSymbolRefExpr::VK_DTPOFF: 411 case MCSymbolRefExpr::VK_DTPREL: 412 case MCSymbolRefExpr::VK_PPC_DTPMOD: 413 case MCSymbolRefExpr::VK_PPC_TPREL_LO: 414 case MCSymbolRefExpr::VK_PPC_TPREL_HI: 415 case MCSymbolRefExpr::VK_PPC_TPREL_HA: 416 case MCSymbolRefExpr::VK_PPC_TPREL_HIGH: 417 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA: 418 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER: 419 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA: 420 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST: 421 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA: 422 case MCSymbolRefExpr::VK_PPC_DTPREL_LO: 423 case MCSymbolRefExpr::VK_PPC_DTPREL_HI: 424 case MCSymbolRefExpr::VK_PPC_DTPREL_HA: 425 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH: 426 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA: 427 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER: 428 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA: 429 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST: 430 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA: 431 case MCSymbolRefExpr::VK_PPC_GOT_TPREL: 432 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO: 433 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI: 434 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA: 435 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL: 436 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO: 437 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI: 438 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA: 439 case MCSymbolRefExpr::VK_PPC_TLS: 440 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD: 441 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO: 442 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI: 443 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA: 444 case MCSymbolRefExpr::VK_PPC_TLSGD: 445 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD: 446 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO: 447 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI: 448 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA: 449 case MCSymbolRefExpr::VK_PPC_TLSLD: 450 break; 451 } 452 getAssembler().registerSymbol(symRef.getSymbol()); 453 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS); 454 break; 455 } 456 457 case MCExpr::Unary: 458 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); 459 break; 460 } 461 } 462 463 void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) { 464 const MCSymbol *S = &SRE->getSymbol(); 465 if (S->isTemporary()) { 466 if (!S->isInSection()) { 467 getContext().reportError( 468 SRE->getLoc(), Twine("Reference to undefined temporary symbol ") + 469 "`" + S->getName() + "`"); 470 return; 471 } 472 S = S->getSection().getBeginSymbol(); 473 S->setUsedInReloc(); 474 SRE = 475 MCSymbolRefExpr::create(S, SRE->getKind(), getContext(), SRE->getLoc()); 476 return; 477 } 478 // Not a temporary, referece it as a weak undefined. 479 bool Created; 480 getAssembler().registerSymbol(*S, &Created); 481 if (Created) { 482 cast<MCSymbolELF>(S)->setBinding(ELF::STB_WEAK); 483 cast<MCSymbolELF>(S)->setExternal(true); 484 } 485 } 486 487 void MCELFStreamer::finalizeCGProfile() { 488 for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) { 489 finalizeCGProfileEntry(E.From); 490 finalizeCGProfileEntry(E.To); 491 } 492 } 493 494 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst, 495 const MCSubtargetInfo &STI) { 496 this->MCObjectStreamer::EmitInstToFragment(Inst, STI); 497 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment()); 498 499 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) 500 fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); 501 } 502 503 // A fragment can only have one Subtarget, and when bundling is enabled we 504 // sometimes need to use the same fragment. We give an error if there 505 // are conflicting Subtargets. 506 static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI, 507 const MCSubtargetInfo *NewSTI) { 508 if (OldSTI && NewSTI && OldSTI != NewSTI) 509 report_fatal_error("A Bundle can only have one Subtarget."); 510 } 511 512 void MCELFStreamer::EmitInstToData(const MCInst &Inst, 513 const MCSubtargetInfo &STI) { 514 MCAssembler &Assembler = getAssembler(); 515 SmallVector<MCFixup, 4> Fixups; 516 SmallString<256> Code; 517 raw_svector_ostream VecOS(Code); 518 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); 519 520 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) 521 fixSymbolsInTLSFixups(Fixups[i].getValue()); 522 523 // There are several possibilities here: 524 // 525 // If bundling is disabled, append the encoded instruction to the current data 526 // fragment (or create a new such fragment if the current fragment is not a 527 // data fragment, or the Subtarget has changed). 528 // 529 // If bundling is enabled: 530 // - If we're not in a bundle-locked group, emit the instruction into a 531 // fragment of its own. If there are no fixups registered for the 532 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a 533 // MCDataFragment. 534 // - If we're in a bundle-locked group, append the instruction to the current 535 // data fragment because we want all the instructions in a group to get into 536 // the same fragment. Be careful not to do that for the first instruction in 537 // the group, though. 538 MCDataFragment *DF; 539 540 if (Assembler.isBundlingEnabled()) { 541 MCSection &Sec = *getCurrentSectionOnly(); 542 if (Assembler.getRelaxAll() && isBundleLocked()) { 543 // If the -mc-relax-all flag is used and we are bundle-locked, we re-use 544 // the current bundle group. 545 DF = BundleGroups.back(); 546 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI); 547 } 548 else if (Assembler.getRelaxAll() && !isBundleLocked()) 549 // When not in a bundle-locked group and the -mc-relax-all flag is used, 550 // we create a new temporary fragment which will be later merged into 551 // the current fragment. 552 DF = new MCDataFragment(); 553 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) { 554 // If we are bundle-locked, we re-use the current fragment. 555 // The bundle-locking directive ensures this is a new data fragment. 556 DF = cast<MCDataFragment>(getCurrentFragment()); 557 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI); 558 } 559 else if (!isBundleLocked() && Fixups.size() == 0) { 560 // Optimize memory usage by emitting the instruction to a 561 // MCCompactEncodedInstFragment when not in a bundle-locked group and 562 // there are no fixups registered. 563 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment(); 564 insert(CEIF); 565 CEIF->getContents().append(Code.begin(), Code.end()); 566 CEIF->setHasInstructions(STI); 567 return; 568 } else { 569 DF = new MCDataFragment(); 570 insert(DF); 571 } 572 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) { 573 // If this fragment is for a group marked "align_to_end", set a flag 574 // in the fragment. This can happen after the fragment has already been 575 // created if there are nested bundle_align groups and an inner one 576 // is the one marked align_to_end. 577 DF->setAlignToBundleEnd(true); 578 } 579 580 // We're now emitting an instruction in a bundle group, so this flag has 581 // to be turned off. 582 Sec.setBundleGroupBeforeFirstInst(false); 583 } else { 584 DF = getOrCreateDataFragment(&STI); 585 } 586 587 // Add the fixups and data. 588 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { 589 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); 590 DF->getFixups().push_back(Fixups[i]); 591 } 592 DF->setHasInstructions(STI); 593 DF->getContents().append(Code.begin(), Code.end()); 594 595 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) { 596 if (!isBundleLocked()) { 597 mergeFragment(getOrCreateDataFragment(&STI), DF); 598 delete DF; 599 } 600 } 601 } 602 603 void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) { 604 assert(AlignPow2 <= 30 && "Invalid bundle alignment"); 605 MCAssembler &Assembler = getAssembler(); 606 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 || 607 Assembler.getBundleAlignSize() == 1U << AlignPow2)) 608 Assembler.setBundleAlignSize(1U << AlignPow2); 609 else 610 report_fatal_error(".bundle_align_mode cannot be changed once set"); 611 } 612 613 void MCELFStreamer::EmitBundleLock(bool AlignToEnd) { 614 MCSection &Sec = *getCurrentSectionOnly(); 615 616 // Sanity checks 617 // 618 if (!getAssembler().isBundlingEnabled()) 619 report_fatal_error(".bundle_lock forbidden when bundling is disabled"); 620 621 if (!isBundleLocked()) 622 Sec.setBundleGroupBeforeFirstInst(true); 623 624 if (getAssembler().getRelaxAll() && !isBundleLocked()) { 625 // TODO: drop the lock state and set directly in the fragment 626 MCDataFragment *DF = new MCDataFragment(); 627 BundleGroups.push_back(DF); 628 } 629 630 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd 631 : MCSection::BundleLocked); 632 } 633 634 void MCELFStreamer::EmitBundleUnlock() { 635 MCSection &Sec = *getCurrentSectionOnly(); 636 637 // Sanity checks 638 if (!getAssembler().isBundlingEnabled()) 639 report_fatal_error(".bundle_unlock forbidden when bundling is disabled"); 640 else if (!isBundleLocked()) 641 report_fatal_error(".bundle_unlock without matching lock"); 642 else if (Sec.isBundleGroupBeforeFirstInst()) 643 report_fatal_error("Empty bundle-locked group is forbidden"); 644 645 // When the -mc-relax-all flag is used, we emit instructions to fragments 646 // stored on a stack. When the bundle unlock is emitted, we pop a fragment 647 // from the stack a merge it to the one below. 648 if (getAssembler().getRelaxAll()) { 649 assert(!BundleGroups.empty() && "There are no bundle groups"); 650 MCDataFragment *DF = BundleGroups.back(); 651 652 // FIXME: Use BundleGroups to track the lock state instead. 653 Sec.setBundleLockState(MCSection::NotBundleLocked); 654 655 // FIXME: Use more separate fragments for nested groups. 656 if (!isBundleLocked()) { 657 mergeFragment(getOrCreateDataFragment(DF->getSubtargetInfo()), DF); 658 BundleGroups.pop_back(); 659 delete DF; 660 } 661 662 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd) 663 getOrCreateDataFragment()->setAlignToBundleEnd(false); 664 } else 665 Sec.setBundleLockState(MCSection::NotBundleLocked); 666 } 667 668 void MCELFStreamer::FinishImpl() { 669 // Ensure the last section gets aligned if necessary. 670 MCSection *CurSection = getCurrentSectionOnly(); 671 setSectionAlignmentForBundling(getAssembler(), CurSection); 672 673 finalizeCGProfile(); 674 EmitFrames(nullptr); 675 676 this->MCObjectStreamer::FinishImpl(); 677 } 678 679 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { 680 llvm_unreachable("Generic ELF doesn't support this directive"); 681 } 682 683 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 684 llvm_unreachable("ELF doesn't support this directive"); 685 } 686 687 void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, 688 uint64_t Size, unsigned ByteAlignment, 689 SMLoc Loc) { 690 llvm_unreachable("ELF doesn't support this directive"); 691 } 692 693 void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, 694 uint64_t Size, unsigned ByteAlignment) { 695 llvm_unreachable("ELF doesn't support this directive"); 696 } 697 698 MCStreamer *llvm::createELFStreamer(MCContext &Context, 699 std::unique_ptr<MCAsmBackend> &&MAB, 700 std::unique_ptr<MCObjectWriter> &&OW, 701 std::unique_ptr<MCCodeEmitter> &&CE, 702 bool RelaxAll) { 703 MCELFStreamer *S = 704 new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE)); 705 if (RelaxAll) 706 S->getAssembler().setRelaxAll(true); 707 return S; 708 } 709