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/SmallVector.h" 15 #include "llvm/BinaryFormat/ELF.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/MCELFObjectWriter.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/MC/TargetRegistry.h" 33 #include "llvm/Support/Casting.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/LEB128.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 ELFObjectWriter &MCELFStreamer::getWriter() { 49 return static_cast<ELFObjectWriter &>(getAssembler().getWriter()); 50 } 51 52 bool MCELFStreamer::isBundleLocked() const { 53 return getCurrentSectionOnly()->isBundleLocked(); 54 } 55 56 void MCELFStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) { 57 MCContext &Ctx = getContext(); 58 switchSection(Ctx.getObjectFileInfo()->getTextSection()); 59 emitCodeAlignment(Align(Ctx.getObjectFileInfo()->getTextSectionAlignment()), 60 &STI); 61 62 if (NoExecStack) 63 switchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx)); 64 } 65 66 void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) { 67 auto *Symbol = cast<MCSymbolELF>(S); 68 MCObjectStreamer::emitLabel(Symbol, Loc); 69 70 const MCSectionELF &Section = 71 static_cast<const MCSectionELF &>(*getCurrentSectionOnly()); 72 if (Section.getFlags() & ELF::SHF_TLS) 73 Symbol->setType(ELF::STT_TLS); 74 } 75 76 void MCELFStreamer::emitLabelAtPos(MCSymbol *S, SMLoc Loc, MCDataFragment &F, 77 uint64_t Offset) { 78 auto *Symbol = cast<MCSymbolELF>(S); 79 MCObjectStreamer::emitLabelAtPos(Symbol, Loc, F, Offset); 80 81 const MCSectionELF &Section = 82 static_cast<const MCSectionELF &>(*getCurrentSectionOnly()); 83 if (Section.getFlags() & ELF::SHF_TLS) 84 Symbol->setType(ELF::STT_TLS); 85 } 86 87 void MCELFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) { 88 // Let the target do whatever target specific stuff it needs to do. 89 getAssembler().getBackend().handleAssemblerFlag(Flag); 90 } 91 92 // If bundle alignment is used and there are any instructions in the section, it 93 // needs to be aligned to at least the bundle size. 94 static void setSectionAlignmentForBundling(const MCAssembler &Assembler, 95 MCSection *Section) { 96 if (Assembler.isBundlingEnabled() && Section->hasInstructions()) 97 Section->ensureMinAlignment(Align(Assembler.getBundleAlignSize())); 98 } 99 100 void MCELFStreamer::changeSection(MCSection *Section, uint32_t Subsection) { 101 MCAssembler &Asm = getAssembler(); 102 if (auto *F = getCurrentFragment()) { 103 if (isBundleLocked()) 104 report_fatal_error("Unterminated .bundle_lock when changing a section"); 105 106 // Ensure the previous section gets aligned if necessary. 107 setSectionAlignmentForBundling(Asm, F->getParent()); 108 } 109 auto *SectionELF = static_cast<const MCSectionELF *>(Section); 110 const MCSymbol *Grp = SectionELF->getGroup(); 111 if (Grp) 112 Asm.registerSymbol(*Grp); 113 if (SectionELF->getFlags() & ELF::SHF_GNU_RETAIN) 114 getWriter().markGnuAbi(); 115 116 changeSectionImpl(Section, Subsection); 117 Asm.registerSymbol(*Section->getBeginSymbol()); 118 } 119 120 void MCELFStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { 121 getAssembler().registerSymbol(*Symbol); 122 const MCExpr *Value = MCSymbolRefExpr::create( 123 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext()); 124 Alias->setVariableValue(Value); 125 } 126 127 // When GNU as encounters more than one .type declaration for an object it seems 128 // to use a mechanism similar to the one below to decide which type is actually 129 // used in the object file. The greater of T1 and T2 is selected based on the 130 // following ordering: 131 // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else 132 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user 133 // provided type). 134 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) { 135 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC, 136 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) { 137 if (T1 == Type) 138 return T2; 139 if (T2 == Type) 140 return T1; 141 } 142 143 return T2; 144 } 145 146 bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { 147 auto *Symbol = cast<MCSymbolELF>(S); 148 149 // Adding a symbol attribute always introduces the symbol, note that an 150 // important side effect of calling registerSymbol here is to register 151 // the symbol with the assembler. 152 getAssembler().registerSymbol(*Symbol); 153 154 // The implementation of symbol attributes is designed to match 'as', but it 155 // leaves much to desired. It doesn't really make sense to arbitrarily add and 156 // remove flags, but 'as' allows this (in particular, see .desc). 157 // 158 // In the future it might be worth trying to make these operations more well 159 // defined. 160 switch (Attribute) { 161 case MCSA_Cold: 162 case MCSA_Extern: 163 case MCSA_LazyReference: 164 case MCSA_Reference: 165 case MCSA_SymbolResolver: 166 case MCSA_PrivateExtern: 167 case MCSA_WeakDefinition: 168 case MCSA_WeakDefAutoPrivate: 169 case MCSA_Invalid: 170 case MCSA_IndirectSymbol: 171 case MCSA_Exported: 172 case MCSA_WeakAntiDep: 173 return false; 174 175 case MCSA_NoDeadStrip: 176 // Ignore for now. 177 break; 178 179 case MCSA_ELF_TypeGnuUniqueObject: 180 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT)); 181 Symbol->setBinding(ELF::STB_GNU_UNIQUE); 182 getWriter().markGnuAbi(); 183 break; 184 185 case MCSA_Global: 186 // For `.weak x; .global x`, GNU as sets the binding to STB_WEAK while we 187 // traditionally set the binding to STB_GLOBAL. This is error-prone, so we 188 // error on such cases. Note, we also disallow changed binding from .local. 189 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_GLOBAL) 190 getContext().reportError(getStartTokLoc(), 191 Symbol->getName() + 192 " changed binding to STB_GLOBAL"); 193 Symbol->setBinding(ELF::STB_GLOBAL); 194 break; 195 196 case MCSA_WeakReference: 197 case MCSA_Weak: 198 // For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK. 199 // We emit a warning for now but may switch to an error in the future. 200 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK) 201 getContext().reportWarning( 202 getStartTokLoc(), Symbol->getName() + " changed binding to STB_WEAK"); 203 Symbol->setBinding(ELF::STB_WEAK); 204 break; 205 206 case MCSA_Local: 207 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_LOCAL) 208 getContext().reportError(getStartTokLoc(), 209 Symbol->getName() + 210 " changed binding to STB_LOCAL"); 211 Symbol->setBinding(ELF::STB_LOCAL); 212 break; 213 214 case MCSA_ELF_TypeFunction: 215 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC)); 216 break; 217 218 case MCSA_ELF_TypeIndFunction: 219 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC)); 220 getWriter().markGnuAbi(); 221 break; 222 223 case MCSA_ELF_TypeObject: 224 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT)); 225 break; 226 227 case MCSA_ELF_TypeTLS: 228 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS)); 229 break; 230 231 case MCSA_ELF_TypeCommon: 232 // TODO: Emit these as a common symbol. 233 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT)); 234 break; 235 236 case MCSA_ELF_TypeNoType: 237 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE)); 238 break; 239 240 case MCSA_Protected: 241 Symbol->setVisibility(ELF::STV_PROTECTED); 242 break; 243 244 case MCSA_Memtag: 245 Symbol->setMemtag(true); 246 break; 247 248 case MCSA_Hidden: 249 Symbol->setVisibility(ELF::STV_HIDDEN); 250 break; 251 252 case MCSA_Internal: 253 Symbol->setVisibility(ELF::STV_INTERNAL); 254 break; 255 256 case MCSA_AltEntry: 257 llvm_unreachable("ELF doesn't support the .alt_entry attribute"); 258 259 case MCSA_LGlobal: 260 llvm_unreachable("ELF doesn't support the .lglobl attribute"); 261 } 262 263 return true; 264 } 265 266 void MCELFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, 267 Align ByteAlignment) { 268 auto *Symbol = cast<MCSymbolELF>(S); 269 getAssembler().registerSymbol(*Symbol); 270 271 if (!Symbol->isBindingSet()) 272 Symbol->setBinding(ELF::STB_GLOBAL); 273 274 Symbol->setType(ELF::STT_OBJECT); 275 276 if (Symbol->getBinding() == ELF::STB_LOCAL) { 277 MCSection &Section = *getAssembler().getContext().getELFSection( 278 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 279 MCSectionSubPair P = getCurrentSection(); 280 switchSection(&Section); 281 282 emitValueToAlignment(ByteAlignment, 0, 1, 0); 283 emitLabel(Symbol); 284 emitZeros(Size); 285 286 switchSection(P.first, P.second); 287 } else { 288 if (Symbol->declareCommon(Size, ByteAlignment)) 289 report_fatal_error(Twine("Symbol: ") + Symbol->getName() + 290 " redeclared as different type"); 291 } 292 293 cast<MCSymbolELF>(Symbol) 294 ->setSize(MCConstantExpr::create(Size, getContext())); 295 } 296 297 void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) { 298 cast<MCSymbolELF>(Symbol)->setSize(Value); 299 } 300 301 void MCELFStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym, 302 StringRef Name, 303 bool KeepOriginalSym) { 304 getWriter().Symvers.push_back(ELFObjectWriter::Symver{ 305 getStartTokLoc(), OriginalSym, Name, KeepOriginalSym}); 306 } 307 308 void MCELFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size, 309 Align ByteAlignment) { 310 auto *Symbol = cast<MCSymbolELF>(S); 311 // FIXME: Should this be caught and done earlier? 312 getAssembler().registerSymbol(*Symbol); 313 Symbol->setBinding(ELF::STB_LOCAL); 314 emitCommonSymbol(Symbol, Size, ByteAlignment); 315 } 316 317 void MCELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, 318 SMLoc Loc) { 319 if (isBundleLocked()) 320 report_fatal_error("Emitting values inside a locked bundle is forbidden"); 321 fixSymbolsInTLSFixups(Value); 322 MCObjectStreamer::emitValueImpl(Value, Size, Loc); 323 } 324 325 void MCELFStreamer::emitValueToAlignment(Align Alignment, int64_t Value, 326 unsigned ValueSize, 327 unsigned MaxBytesToEmit) { 328 if (isBundleLocked()) 329 report_fatal_error("Emitting values inside a locked bundle is forbidden"); 330 MCObjectStreamer::emitValueToAlignment(Alignment, Value, ValueSize, 331 MaxBytesToEmit); 332 } 333 334 void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From, 335 const MCSymbolRefExpr *To, 336 uint64_t Count) { 337 getWriter().getCGProfile().push_back({From, To, Count}); 338 } 339 340 void MCELFStreamer::emitIdent(StringRef IdentString) { 341 MCSection *Comment = getAssembler().getContext().getELFSection( 342 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1); 343 pushSection(); 344 switchSection(Comment); 345 if (!SeenIdent) { 346 emitInt8(0); 347 SeenIdent = true; 348 } 349 emitBytes(IdentString); 350 emitInt8(0); 351 popSection(); 352 } 353 354 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { 355 switch (expr->getKind()) { 356 case MCExpr::Target: 357 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler()); 358 break; 359 case MCExpr::Constant: 360 break; 361 362 case MCExpr::Binary: { 363 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); 364 fixSymbolsInTLSFixups(be->getLHS()); 365 fixSymbolsInTLSFixups(be->getRHS()); 366 break; 367 } 368 369 case MCExpr::SymbolRef: { 370 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); 371 switch (symRef.getKind()) { 372 default: 373 return; 374 case MCSymbolRefExpr::VK_GOTTPOFF: 375 case MCSymbolRefExpr::VK_INDNTPOFF: 376 case MCSymbolRefExpr::VK_NTPOFF: 377 case MCSymbolRefExpr::VK_GOTNTPOFF: 378 case MCSymbolRefExpr::VK_TLSCALL: 379 case MCSymbolRefExpr::VK_TLSDESC: 380 case MCSymbolRefExpr::VK_TLSGD: 381 case MCSymbolRefExpr::VK_TLSLD: 382 case MCSymbolRefExpr::VK_TLSLDM: 383 case MCSymbolRefExpr::VK_TPOFF: 384 case MCSymbolRefExpr::VK_TPREL: 385 case MCSymbolRefExpr::VK_DTPOFF: 386 case MCSymbolRefExpr::VK_DTPREL: 387 case MCSymbolRefExpr::VK_PPC_DTPMOD: 388 case MCSymbolRefExpr::VK_PPC_TPREL_LO: 389 case MCSymbolRefExpr::VK_PPC_TPREL_HI: 390 case MCSymbolRefExpr::VK_PPC_TPREL_HA: 391 case MCSymbolRefExpr::VK_PPC_TPREL_HIGH: 392 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA: 393 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER: 394 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA: 395 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST: 396 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA: 397 case MCSymbolRefExpr::VK_PPC_DTPREL_LO: 398 case MCSymbolRefExpr::VK_PPC_DTPREL_HI: 399 case MCSymbolRefExpr::VK_PPC_DTPREL_HA: 400 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH: 401 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA: 402 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER: 403 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA: 404 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST: 405 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA: 406 case MCSymbolRefExpr::VK_PPC_GOT_TPREL: 407 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO: 408 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI: 409 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA: 410 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_PCREL: 411 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL: 412 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO: 413 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI: 414 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA: 415 case MCSymbolRefExpr::VK_PPC_TLS: 416 case MCSymbolRefExpr::VK_PPC_TLS_PCREL: 417 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD: 418 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO: 419 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI: 420 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA: 421 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_PCREL: 422 case MCSymbolRefExpr::VK_PPC_TLSGD: 423 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD: 424 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO: 425 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI: 426 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA: 427 case MCSymbolRefExpr::VK_PPC_TLSLD: 428 break; 429 } 430 getAssembler().registerSymbol(symRef.getSymbol()); 431 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS); 432 break; 433 } 434 435 case MCExpr::Unary: 436 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); 437 break; 438 } 439 } 440 441 void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE, 442 uint64_t Offset) { 443 const MCSymbol *S = &SRE->getSymbol(); 444 if (S->isTemporary()) { 445 if (!S->isInSection()) { 446 getContext().reportError( 447 SRE->getLoc(), Twine("Reference to undefined temporary symbol ") + 448 "`" + S->getName() + "`"); 449 return; 450 } 451 S = S->getSection().getBeginSymbol(); 452 S->setUsedInReloc(); 453 SRE = MCSymbolRefExpr::create(S, MCSymbolRefExpr::VK_None, getContext(), 454 SRE->getLoc()); 455 } 456 const MCConstantExpr *MCOffset = MCConstantExpr::create(Offset, getContext()); 457 if (std::optional<std::pair<bool, std::string>> Err = 458 MCObjectStreamer::emitRelocDirective( 459 *MCOffset, "BFD_RELOC_NONE", SRE, SRE->getLoc(), 460 *getContext().getSubtargetInfo())) 461 report_fatal_error("Relocation for CG Profile could not be created: " + 462 Twine(Err->second)); 463 } 464 465 void MCELFStreamer::finalizeCGProfile() { 466 ELFObjectWriter &W = getWriter(); 467 if (W.getCGProfile().empty()) 468 return; 469 MCSection *CGProfile = getAssembler().getContext().getELFSection( 470 ".llvm.call-graph-profile", ELF::SHT_LLVM_CALL_GRAPH_PROFILE, 471 ELF::SHF_EXCLUDE, /*sizeof(Elf_CGProfile_Impl<>)=*/8); 472 pushSection(); 473 switchSection(CGProfile); 474 uint64_t Offset = 0; 475 for (auto &E : W.getCGProfile()) { 476 finalizeCGProfileEntry(E.From, Offset); 477 finalizeCGProfileEntry(E.To, Offset); 478 emitIntValue(E.Count, sizeof(uint64_t)); 479 Offset += sizeof(uint64_t); 480 } 481 popSection(); 482 } 483 484 void MCELFStreamer::emitInstToFragment(const MCInst &Inst, 485 const MCSubtargetInfo &STI) { 486 this->MCObjectStreamer::emitInstToFragment(Inst, STI); 487 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment()); 488 489 for (auto &Fixup : F.getFixups()) 490 fixSymbolsInTLSFixups(Fixup.getValue()); 491 } 492 493 // A fragment can only have one Subtarget, and when bundling is enabled we 494 // sometimes need to use the same fragment. We give an error if there 495 // are conflicting Subtargets. 496 static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI, 497 const MCSubtargetInfo *NewSTI) { 498 if (OldSTI && NewSTI && OldSTI != NewSTI) 499 report_fatal_error("A Bundle can only have one Subtarget."); 500 } 501 502 void MCELFStreamer::emitInstToData(const MCInst &Inst, 503 const MCSubtargetInfo &STI) { 504 MCAssembler &Assembler = getAssembler(); 505 506 // There are several possibilities here: 507 // 508 // If bundling is disabled, append the encoded instruction to the current data 509 // fragment (or create a new such fragment if the current fragment is not a 510 // data fragment, or the Subtarget has changed). 511 // 512 // If bundling is enabled: 513 // - If we're not in a bundle-locked group, emit the instruction into a 514 // fragment of its own. 515 // - If we're in a bundle-locked group, append the instruction to the current 516 // data fragment because we want all the instructions in a group to get into 517 // the same fragment. Be careful not to do that for the first instruction in 518 // the group, though. 519 MCDataFragment *DF; 520 521 if (Assembler.isBundlingEnabled()) { 522 MCSection &Sec = *getCurrentSectionOnly(); 523 if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) { 524 // If we are bundle-locked, we re-use the current fragment. 525 // The bundle-locking directive ensures this is a new data fragment. 526 DF = cast<MCDataFragment>(getCurrentFragment()); 527 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI); 528 } else { 529 DF = getContext().allocFragment<MCDataFragment>(); 530 insert(DF); 531 } 532 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) { 533 // If this fragment is for a group marked "align_to_end", set a flag 534 // in the fragment. This can happen after the fragment has already been 535 // created if there are nested bundle_align groups and an inner one 536 // is the one marked align_to_end. 537 DF->setAlignToBundleEnd(true); 538 } 539 540 // We're now emitting an instruction in a bundle group, so this flag has 541 // to be turned off. 542 Sec.setBundleGroupBeforeFirstInst(false); 543 } else { 544 DF = getOrCreateDataFragment(&STI); 545 } 546 547 // Emit instruction directly into data fragment. 548 size_t FixupStartIndex = DF->getFixups().size(); 549 size_t CodeOffset = DF->getContents().size(); 550 Assembler.getEmitter().encodeInstruction(Inst, DF->getContents(), 551 DF->getFixups(), STI); 552 553 auto Fixups = MutableArrayRef(DF->getFixups()).slice(FixupStartIndex); 554 for (auto &Fixup : Fixups) { 555 Fixup.setOffset(Fixup.getOffset() + CodeOffset); 556 fixSymbolsInTLSFixups(Fixup.getValue()); 557 } 558 559 DF->setHasInstructions(STI); 560 if (!Fixups.empty() && Fixups.back().getTargetKind() == 561 getAssembler().getBackend().RelaxFixupKind) 562 DF->setLinkerRelaxable(); 563 } 564 565 void MCELFStreamer::emitBundleAlignMode(Align Alignment) { 566 assert(Log2(Alignment) <= 30 && "Invalid bundle alignment"); 567 MCAssembler &Assembler = getAssembler(); 568 if (Alignment > 1 && (Assembler.getBundleAlignSize() == 0 || 569 Assembler.getBundleAlignSize() == Alignment.value())) 570 Assembler.setBundleAlignSize(Alignment.value()); 571 else 572 report_fatal_error(".bundle_align_mode cannot be changed once set"); 573 } 574 575 void MCELFStreamer::emitBundleLock(bool AlignToEnd) { 576 MCSection &Sec = *getCurrentSectionOnly(); 577 578 if (!getAssembler().isBundlingEnabled()) 579 report_fatal_error(".bundle_lock forbidden when bundling is disabled"); 580 581 if (!isBundleLocked()) 582 Sec.setBundleGroupBeforeFirstInst(true); 583 584 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd 585 : MCSection::BundleLocked); 586 } 587 588 void MCELFStreamer::emitBundleUnlock() { 589 MCSection &Sec = *getCurrentSectionOnly(); 590 591 if (!getAssembler().isBundlingEnabled()) 592 report_fatal_error(".bundle_unlock forbidden when bundling is disabled"); 593 else if (!isBundleLocked()) 594 report_fatal_error(".bundle_unlock without matching lock"); 595 else if (Sec.isBundleGroupBeforeFirstInst()) 596 report_fatal_error("Empty bundle-locked group is forbidden"); 597 598 Sec.setBundleLockState(MCSection::NotBundleLocked); 599 } 600 601 void MCELFStreamer::finishImpl() { 602 // Emit the .gnu attributes section if any attributes have been added. 603 if (!GNUAttributes.empty()) { 604 MCSection *DummyAttributeSection = nullptr; 605 createAttributesSection("gnu", ".gnu.attributes", ELF::SHT_GNU_ATTRIBUTES, 606 DummyAttributeSection, GNUAttributes); 607 } 608 609 // Ensure the last section gets aligned if necessary. 610 if (MCFragment *F = getCurrentFragment()) 611 setSectionAlignmentForBundling(getAssembler(), F->getParent()); 612 613 finalizeCGProfile(); 614 emitFrames(nullptr); 615 616 this->MCObjectStreamer::finishImpl(); 617 } 618 619 void MCELFStreamer::emitThumbFunc(MCSymbol *Func) { 620 llvm_unreachable("Generic ELF doesn't support this directive"); 621 } 622 623 void MCELFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 624 llvm_unreachable("ELF doesn't support this directive"); 625 } 626 627 void MCELFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol, 628 uint64_t Size, Align ByteAlignment, 629 SMLoc Loc) { 630 llvm_unreachable("ELF doesn't support this directive"); 631 } 632 633 void MCELFStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, 634 uint64_t Size, Align ByteAlignment) { 635 llvm_unreachable("ELF doesn't support this directive"); 636 } 637 638 void MCELFStreamer::setAttributeItem(unsigned Attribute, unsigned Value, 639 bool OverwriteExisting) { 640 // Look for existing attribute item 641 if (AttributeItem *Item = getAttributeItem(Attribute)) { 642 if (!OverwriteExisting) 643 return; 644 Item->Type = AttributeItem::NumericAttribute; 645 Item->IntValue = Value; 646 return; 647 } 648 649 // Create new attribute item 650 AttributeItem Item = {AttributeItem::NumericAttribute, Attribute, Value, 651 std::string(StringRef(""))}; 652 Contents.push_back(Item); 653 } 654 655 void MCELFStreamer::setAttributeItem(unsigned Attribute, StringRef Value, 656 bool OverwriteExisting) { 657 // Look for existing attribute item 658 if (AttributeItem *Item = getAttributeItem(Attribute)) { 659 if (!OverwriteExisting) 660 return; 661 Item->Type = AttributeItem::TextAttribute; 662 Item->StringValue = std::string(Value); 663 return; 664 } 665 666 // Create new attribute item 667 AttributeItem Item = {AttributeItem::TextAttribute, Attribute, 0, 668 std::string(Value)}; 669 Contents.push_back(Item); 670 } 671 672 void MCELFStreamer::setAttributeItems(unsigned Attribute, unsigned IntValue, 673 StringRef StringValue, 674 bool OverwriteExisting) { 675 // Look for existing attribute item 676 if (AttributeItem *Item = getAttributeItem(Attribute)) { 677 if (!OverwriteExisting) 678 return; 679 Item->Type = AttributeItem::NumericAndTextAttributes; 680 Item->IntValue = IntValue; 681 Item->StringValue = std::string(StringValue); 682 return; 683 } 684 685 // Create new attribute item 686 AttributeItem Item = {AttributeItem::NumericAndTextAttributes, Attribute, 687 IntValue, std::string(StringValue)}; 688 Contents.push_back(Item); 689 } 690 691 MCELFStreamer::AttributeItem * 692 MCELFStreamer::getAttributeItem(unsigned Attribute) { 693 for (AttributeItem &Item : Contents) 694 if (Item.Tag == Attribute) 695 return &Item; 696 return nullptr; 697 } 698 699 size_t MCELFStreamer::calculateContentSize( 700 SmallVector<AttributeItem, 64> &AttrsVec) const { 701 size_t Result = 0; 702 for (const AttributeItem &Item : AttrsVec) { 703 switch (Item.Type) { 704 case AttributeItem::HiddenAttribute: 705 break; 706 case AttributeItem::NumericAttribute: 707 Result += getULEB128Size(Item.Tag); 708 Result += getULEB128Size(Item.IntValue); 709 break; 710 case AttributeItem::TextAttribute: 711 Result += getULEB128Size(Item.Tag); 712 Result += Item.StringValue.size() + 1; // string + '\0' 713 break; 714 case AttributeItem::NumericAndTextAttributes: 715 Result += getULEB128Size(Item.Tag); 716 Result += getULEB128Size(Item.IntValue); 717 Result += Item.StringValue.size() + 1; // string + '\0'; 718 break; 719 } 720 } 721 return Result; 722 } 723 724 void MCELFStreamer::createAttributesSection( 725 StringRef Vendor, const Twine &Section, unsigned Type, 726 MCSection *&AttributeSection, SmallVector<AttributeItem, 64> &AttrsVec) { 727 // <format-version> 728 // [ <section-length> "vendor-name" 729 // [ <file-tag> <size> <attribute>* 730 // | <section-tag> <size> <section-number>* 0 <attribute>* 731 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>* 732 // ]+ 733 // ]* 734 735 // Switch section to AttributeSection or get/create the section. 736 if (AttributeSection) { 737 switchSection(AttributeSection); 738 } else { 739 AttributeSection = getContext().getELFSection(Section, Type, 0); 740 switchSection(AttributeSection); 741 742 // Format version 743 emitInt8(0x41); 744 } 745 746 // Vendor size + Vendor name + '\0' 747 const size_t VendorHeaderSize = 4 + Vendor.size() + 1; 748 749 // Tag + Tag Size 750 const size_t TagHeaderSize = 1 + 4; 751 752 const size_t ContentsSize = calculateContentSize(AttrsVec); 753 754 emitInt32(VendorHeaderSize + TagHeaderSize + ContentsSize); 755 emitBytes(Vendor); 756 emitInt8(0); // '\0' 757 758 emitInt8(ARMBuildAttrs::File); 759 emitInt32(TagHeaderSize + ContentsSize); 760 761 // Size should have been accounted for already, now 762 // emit each field as its type (ULEB or String) 763 for (const AttributeItem &Item : AttrsVec) { 764 emitULEB128IntValue(Item.Tag); 765 switch (Item.Type) { 766 default: 767 llvm_unreachable("Invalid attribute type"); 768 case AttributeItem::NumericAttribute: 769 emitULEB128IntValue(Item.IntValue); 770 break; 771 case AttributeItem::TextAttribute: 772 emitBytes(Item.StringValue); 773 emitInt8(0); // '\0' 774 break; 775 case AttributeItem::NumericAndTextAttributes: 776 emitULEB128IntValue(Item.IntValue); 777 emitBytes(Item.StringValue); 778 emitInt8(0); // '\0' 779 break; 780 } 781 } 782 783 AttrsVec.clear(); 784 } 785 786 void MCELFStreamer::createAttributesWithSubsection( 787 MCSection *&AttributeSection, const Twine &Section, unsigned Type, 788 SmallVector<AttributeSubSection, 64> &SubSectionVec) { 789 // <format-version: 'A'> 790 // [ <uint32: subsection-length> NTBS: vendor-name 791 // <bytes: vendor-data> 792 // ]* 793 // vendor-data expends to: 794 // <uint8: optional> <uint8: parameter type> <attribute>* 795 if (0 == SubSectionVec.size()) { 796 return; 797 } 798 799 // Switch section to AttributeSection or get/create the section. 800 if (AttributeSection) { 801 switchSection(AttributeSection); 802 } else { 803 AttributeSection = getContext().getELFSection(Section, Type, 0); 804 switchSection(AttributeSection); 805 806 // Format version 807 emitInt8(0x41); 808 } 809 810 for (AttributeSubSection &SubSection : SubSectionVec) { 811 // subsection-length + vendor-name + '\0' 812 const size_t VendorHeaderSize = 4 + SubSection.VendorName.size() + 1; 813 // optional + parameter-type 814 const size_t VendorParameters = 1 + 1; 815 const size_t ContentsSize = calculateContentSize(SubSection.Content); 816 817 emitInt32(VendorHeaderSize + VendorParameters + ContentsSize); 818 emitBytes(SubSection.VendorName); 819 emitInt8(0); // '\0' 820 emitInt8(SubSection.IsOptional); 821 emitInt8(SubSection.ParameterType); 822 823 for (AttributeItem &Item : SubSection.Content) { 824 emitULEB128IntValue(Item.Tag); 825 switch (Item.Type) { 826 default: 827 assert(0 && "Invalid attribute type"); 828 break; 829 case AttributeItem::NumericAttribute: 830 emitULEB128IntValue(Item.IntValue); 831 break; 832 case AttributeItem::TextAttribute: 833 emitBytes(Item.StringValue); 834 emitInt8(0); // '\0' 835 break; 836 case AttributeItem::NumericAndTextAttributes: 837 emitULEB128IntValue(Item.IntValue); 838 emitBytes(Item.StringValue); 839 emitInt8(0); // '\0' 840 break; 841 } 842 } 843 } 844 SubSectionVec.clear(); 845 } 846 847 MCStreamer *llvm::createELFStreamer(MCContext &Context, 848 std::unique_ptr<MCAsmBackend> &&MAB, 849 std::unique_ptr<MCObjectWriter> &&OW, 850 std::unique_ptr<MCCodeEmitter> &&CE) { 851 MCELFStreamer *S = 852 new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE)); 853 return S; 854 } 855