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