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