1 //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===// 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 ARM ELF .o object files. Different 10 // from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to 11 // delimit regions of data and code. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ARMRegisterInfo.h" 16 #include "ARMUnwindOpAsm.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/Triple.h" 22 #include "llvm/ADT/Twine.h" 23 #include "llvm/BinaryFormat/ELF.h" 24 #include "llvm/MC/MCAsmBackend.h" 25 #include "llvm/MC/MCAsmInfo.h" 26 #include "llvm/MC/MCAssembler.h" 27 #include "llvm/MC/MCCodeEmitter.h" 28 #include "llvm/MC/MCContext.h" 29 #include "llvm/MC/MCELFStreamer.h" 30 #include "llvm/MC/MCExpr.h" 31 #include "llvm/MC/MCFixup.h" 32 #include "llvm/MC/MCFragment.h" 33 #include "llvm/MC/MCInst.h" 34 #include "llvm/MC/MCInstPrinter.h" 35 #include "llvm/MC/MCObjectWriter.h" 36 #include "llvm/MC/MCRegisterInfo.h" 37 #include "llvm/MC/MCSection.h" 38 #include "llvm/MC/MCSectionELF.h" 39 #include "llvm/MC/MCStreamer.h" 40 #include "llvm/MC/MCSubtargetInfo.h" 41 #include "llvm/MC/MCSymbol.h" 42 #include "llvm/MC/MCSymbolELF.h" 43 #include "llvm/MC/SectionKind.h" 44 #include "llvm/Support/ARMBuildAttributes.h" 45 #include "llvm/Support/ARMEHABI.h" 46 #include "llvm/Support/Casting.h" 47 #include "llvm/Support/ErrorHandling.h" 48 #include "llvm/Support/FormattedStream.h" 49 #include "llvm/Support/LEB128.h" 50 #include "llvm/Support/TargetParser.h" 51 #include "llvm/Support/raw_ostream.h" 52 #include <algorithm> 53 #include <cassert> 54 #include <climits> 55 #include <cstddef> 56 #include <cstdint> 57 #include <string> 58 59 using namespace llvm; 60 61 static std::string GetAEABIUnwindPersonalityName(unsigned Index) { 62 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && 63 "Invalid personality index"); 64 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str(); 65 } 66 67 namespace { 68 69 class ARMELFStreamer; 70 71 class ARMTargetAsmStreamer : public ARMTargetStreamer { 72 formatted_raw_ostream &OS; 73 MCInstPrinter &InstPrinter; 74 bool IsVerboseAsm; 75 76 void emitFnStart() override; 77 void emitFnEnd() override; 78 void emitCantUnwind() override; 79 void emitPersonality(const MCSymbol *Personality) override; 80 void emitPersonalityIndex(unsigned Index) override; 81 void emitHandlerData() override; 82 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override; 83 void emitMovSP(unsigned Reg, int64_t Offset = 0) override; 84 void emitPad(int64_t Offset) override; 85 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, 86 bool isVector) override; 87 void emitUnwindRaw(int64_t Offset, 88 const SmallVectorImpl<uint8_t> &Opcodes) override; 89 90 void switchVendor(StringRef Vendor) override; 91 void emitAttribute(unsigned Attribute, unsigned Value) override; 92 void emitTextAttribute(unsigned Attribute, StringRef String) override; 93 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, 94 StringRef StringValue) override; 95 void emitArch(ARM::ArchKind Arch) override; 96 void emitArchExtension(uint64_t ArchExt) override; 97 void emitObjectArch(ARM::ArchKind Arch) override; 98 void emitFPU(unsigned FPU) override; 99 void emitInst(uint32_t Inst, char Suffix = '\0') override; 100 void finishAttributeSection() override; 101 102 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override; 103 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override; 104 105 public: 106 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS, 107 MCInstPrinter &InstPrinter, bool VerboseAsm); 108 }; 109 110 ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S, 111 formatted_raw_ostream &OS, 112 MCInstPrinter &InstPrinter, 113 bool VerboseAsm) 114 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter), 115 IsVerboseAsm(VerboseAsm) {} 116 117 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; } 118 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; } 119 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; } 120 121 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) { 122 OS << "\t.personality " << Personality->getName() << '\n'; 123 } 124 125 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) { 126 OS << "\t.personalityindex " << Index << '\n'; 127 } 128 129 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; } 130 131 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg, 132 int64_t Offset) { 133 OS << "\t.setfp\t"; 134 InstPrinter.printRegName(OS, FpReg); 135 OS << ", "; 136 InstPrinter.printRegName(OS, SpReg); 137 if (Offset) 138 OS << ", #" << Offset; 139 OS << '\n'; 140 } 141 142 void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) { 143 assert((Reg != ARM::SP && Reg != ARM::PC) && 144 "the operand of .movsp cannot be either sp or pc"); 145 146 OS << "\t.movsp\t"; 147 InstPrinter.printRegName(OS, Reg); 148 if (Offset) 149 OS << ", #" << Offset; 150 OS << '\n'; 151 } 152 153 void ARMTargetAsmStreamer::emitPad(int64_t Offset) { 154 OS << "\t.pad\t#" << Offset << '\n'; 155 } 156 157 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList, 158 bool isVector) { 159 assert(RegList.size() && "RegList should not be empty"); 160 if (isVector) 161 OS << "\t.vsave\t{"; 162 else 163 OS << "\t.save\t{"; 164 165 InstPrinter.printRegName(OS, RegList[0]); 166 167 for (unsigned i = 1, e = RegList.size(); i != e; ++i) { 168 OS << ", "; 169 InstPrinter.printRegName(OS, RegList[i]); 170 } 171 172 OS << "}\n"; 173 } 174 175 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {} 176 177 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) { 178 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value); 179 if (IsVerboseAsm) { 180 StringRef Name = 181 ELFAttrs::attrTypeAsString(Attribute, ARMBuildAttrs::ARMAttributeTags); 182 if (!Name.empty()) 183 OS << "\t@ " << Name; 184 } 185 OS << "\n"; 186 } 187 188 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute, 189 StringRef String) { 190 switch (Attribute) { 191 case ARMBuildAttrs::CPU_name: 192 OS << "\t.cpu\t" << String.lower(); 193 break; 194 default: 195 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\""; 196 if (IsVerboseAsm) { 197 StringRef Name = ELFAttrs::attrTypeAsString( 198 Attribute, ARMBuildAttrs::ARMAttributeTags); 199 if (!Name.empty()) 200 OS << "\t@ " << Name; 201 } 202 break; 203 } 204 OS << "\n"; 205 } 206 207 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute, 208 unsigned IntValue, 209 StringRef StringValue) { 210 switch (Attribute) { 211 default: llvm_unreachable("unsupported multi-value attribute in asm mode"); 212 case ARMBuildAttrs::compatibility: 213 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue; 214 if (!StringValue.empty()) 215 OS << ", \"" << StringValue << "\""; 216 if (IsVerboseAsm) 217 OS << "\t@ " 218 << ELFAttrs::attrTypeAsString(Attribute, 219 ARMBuildAttrs::ARMAttributeTags); 220 break; 221 } 222 OS << "\n"; 223 } 224 225 void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) { 226 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n"; 227 } 228 229 void ARMTargetAsmStreamer::emitArchExtension(uint64_t ArchExt) { 230 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n"; 231 } 232 233 void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) { 234 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n'; 235 } 236 237 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) { 238 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n"; 239 } 240 241 void ARMTargetAsmStreamer::finishAttributeSection() {} 242 243 void 244 ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) { 245 OS << "\t.tlsdescseq\t" << S->getSymbol().getName() << "\n"; 246 } 247 248 void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) { 249 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo(); 250 251 OS << "\t.thumb_set\t"; 252 Symbol->print(OS, MAI); 253 OS << ", "; 254 Value->print(OS, MAI); 255 OS << '\n'; 256 } 257 258 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) { 259 OS << "\t.inst"; 260 if (Suffix) 261 OS << "." << Suffix; 262 OS << "\t0x" << Twine::utohexstr(Inst) << "\n"; 263 } 264 265 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset, 266 const SmallVectorImpl<uint8_t> &Opcodes) { 267 OS << "\t.unwind_raw " << Offset; 268 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(), 269 OCE = Opcodes.end(); 270 OCI != OCE; ++OCI) 271 OS << ", 0x" << Twine::utohexstr(*OCI); 272 OS << '\n'; 273 } 274 275 class ARMTargetELFStreamer : public ARMTargetStreamer { 276 private: 277 // This structure holds all attributes, accounting for 278 // their string/numeric value, so we can later emit them 279 // in declaration order, keeping all in the same vector 280 struct AttributeItem { 281 enum { 282 HiddenAttribute = 0, 283 NumericAttribute, 284 TextAttribute, 285 NumericAndTextAttributes 286 } Type; 287 unsigned Tag; 288 unsigned IntValue; 289 std::string StringValue; 290 291 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) { 292 // The conformance tag must be emitted first when serialised 293 // into an object file. Specifically, the addenda to the ARM ABI 294 // states that (2.3.7.4): 295 // 296 // "To simplify recognition by consumers in the common case of 297 // claiming conformity for the whole file, this tag should be 298 // emitted first in a file-scope sub-subsection of the first 299 // public subsection of the attributes section." 300 // 301 // So it is special-cased in this comparison predicate when the 302 // attributes are sorted in finishAttributeSection(). 303 return (RHS.Tag != ARMBuildAttrs::conformance) && 304 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag)); 305 } 306 }; 307 308 StringRef CurrentVendor; 309 unsigned FPU = ARM::FK_INVALID; 310 ARM::ArchKind Arch = ARM::ArchKind::INVALID; 311 ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID; 312 SmallVector<AttributeItem, 64> Contents; 313 314 MCSection *AttributeSection = nullptr; 315 316 AttributeItem *getAttributeItem(unsigned Attribute) { 317 for (size_t i = 0; i < Contents.size(); ++i) 318 if (Contents[i].Tag == Attribute) 319 return &Contents[i]; 320 return nullptr; 321 } 322 323 void setAttributeItem(unsigned Attribute, unsigned Value, 324 bool OverwriteExisting) { 325 // Look for existing attribute item 326 if (AttributeItem *Item = getAttributeItem(Attribute)) { 327 if (!OverwriteExisting) 328 return; 329 Item->Type = AttributeItem::NumericAttribute; 330 Item->IntValue = Value; 331 return; 332 } 333 334 // Create new attribute item 335 AttributeItem Item = {AttributeItem::NumericAttribute, Attribute, Value, 336 std::string(StringRef(""))}; 337 Contents.push_back(Item); 338 } 339 340 void setAttributeItem(unsigned Attribute, StringRef Value, 341 bool OverwriteExisting) { 342 // Look for existing attribute item 343 if (AttributeItem *Item = getAttributeItem(Attribute)) { 344 if (!OverwriteExisting) 345 return; 346 Item->Type = AttributeItem::TextAttribute; 347 Item->StringValue = std::string(Value); 348 return; 349 } 350 351 // Create new attribute item 352 AttributeItem Item = {AttributeItem::TextAttribute, Attribute, 0, 353 std::string(Value)}; 354 Contents.push_back(Item); 355 } 356 357 void setAttributeItems(unsigned Attribute, unsigned IntValue, 358 StringRef StringValue, bool OverwriteExisting) { 359 // Look for existing attribute item 360 if (AttributeItem *Item = getAttributeItem(Attribute)) { 361 if (!OverwriteExisting) 362 return; 363 Item->Type = AttributeItem::NumericAndTextAttributes; 364 Item->IntValue = IntValue; 365 Item->StringValue = std::string(StringValue); 366 return; 367 } 368 369 // Create new attribute item 370 AttributeItem Item = {AttributeItem::NumericAndTextAttributes, Attribute, 371 IntValue, std::string(StringValue)}; 372 Contents.push_back(Item); 373 } 374 375 void emitArchDefaultAttributes(); 376 void emitFPUDefaultAttributes(); 377 378 ARMELFStreamer &getStreamer(); 379 380 void emitFnStart() override; 381 void emitFnEnd() override; 382 void emitCantUnwind() override; 383 void emitPersonality(const MCSymbol *Personality) override; 384 void emitPersonalityIndex(unsigned Index) override; 385 void emitHandlerData() override; 386 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override; 387 void emitMovSP(unsigned Reg, int64_t Offset = 0) override; 388 void emitPad(int64_t Offset) override; 389 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, 390 bool isVector) override; 391 void emitUnwindRaw(int64_t Offset, 392 const SmallVectorImpl<uint8_t> &Opcodes) override; 393 394 void switchVendor(StringRef Vendor) override; 395 void emitAttribute(unsigned Attribute, unsigned Value) override; 396 void emitTextAttribute(unsigned Attribute, StringRef String) override; 397 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, 398 StringRef StringValue) override; 399 void emitArch(ARM::ArchKind Arch) override; 400 void emitObjectArch(ARM::ArchKind Arch) override; 401 void emitFPU(unsigned FPU) override; 402 void emitInst(uint32_t Inst, char Suffix = '\0') override; 403 void finishAttributeSection() override; 404 void emitLabel(MCSymbol *Symbol) override; 405 406 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override; 407 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override; 408 409 size_t calculateContentSize() const; 410 411 // Reset state between object emissions 412 void reset() override; 413 414 public: 415 ARMTargetELFStreamer(MCStreamer &S) 416 : ARMTargetStreamer(S), CurrentVendor("aeabi") {} 417 }; 418 419 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at 420 /// the appropriate points in the object files. These symbols are defined in the 421 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf. 422 /// 423 /// In brief: $a, $t or $d should be emitted at the start of each contiguous 424 /// region of ARM code, Thumb code or data in a section. In practice, this 425 /// emission does not rely on explicit assembler directives but on inherent 426 /// properties of the directives doing the emission (e.g. ".byte" is data, "add 427 /// r0, r0, r0" an instruction). 428 /// 429 /// As a result this system is orthogonal to the DataRegion infrastructure used 430 /// by MachO. Beware! 431 class ARMELFStreamer : public MCELFStreamer { 432 public: 433 friend class ARMTargetELFStreamer; 434 435 ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB, 436 std::unique_ptr<MCObjectWriter> OW, 437 std::unique_ptr<MCCodeEmitter> Emitter, bool IsThumb, 438 bool IsAndroid) 439 : MCELFStreamer(Context, std::move(TAB), std::move(OW), 440 std::move(Emitter)), 441 IsThumb(IsThumb), IsAndroid(IsAndroid) { 442 EHReset(); 443 } 444 445 ~ARMELFStreamer() override = default; 446 447 void finishImpl() override; 448 449 // ARM exception handling directives 450 void emitFnStart(); 451 void emitFnEnd(); 452 void emitCantUnwind(); 453 void emitPersonality(const MCSymbol *Per); 454 void emitPersonalityIndex(unsigned index); 455 void emitHandlerData(); 456 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0); 457 void emitMovSP(unsigned Reg, int64_t Offset = 0); 458 void emitPad(int64_t Offset); 459 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector); 460 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes); 461 void emitFill(const MCExpr &NumBytes, uint64_t FillValue, 462 SMLoc Loc) override { 463 emitDataMappingSymbol(); 464 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc); 465 } 466 467 void changeSection(MCSection *Section, const MCExpr *Subsection) override { 468 LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo); 469 MCELFStreamer::changeSection(Section, Subsection); 470 auto LastMappingSymbol = LastMappingSymbols.find(Section); 471 if (LastMappingSymbol != LastMappingSymbols.end()) { 472 LastEMSInfo = std::move(LastMappingSymbol->second); 473 return; 474 } 475 LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0)); 476 } 477 478 /// This function is the one used to emit instruction data into the ELF 479 /// streamer. We override it to add the appropriate mapping symbol if 480 /// necessary. 481 void emitInstruction(const MCInst &Inst, 482 const MCSubtargetInfo &STI) override { 483 if (IsThumb) 484 EmitThumbMappingSymbol(); 485 else 486 EmitARMMappingSymbol(); 487 488 MCELFStreamer::emitInstruction(Inst, STI); 489 } 490 491 void emitInst(uint32_t Inst, char Suffix) { 492 unsigned Size; 493 char Buffer[4]; 494 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian(); 495 496 switch (Suffix) { 497 case '\0': 498 Size = 4; 499 500 assert(!IsThumb); 501 EmitARMMappingSymbol(); 502 for (unsigned II = 0, IE = Size; II != IE; II++) { 503 const unsigned I = LittleEndian ? (Size - II - 1) : II; 504 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT); 505 } 506 507 break; 508 case 'n': 509 case 'w': 510 Size = (Suffix == 'n' ? 2 : 4); 511 512 assert(IsThumb); 513 EmitThumbMappingSymbol(); 514 // Thumb wide instructions are emitted as a pair of 16-bit words of the 515 // appropriate endianness. 516 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) { 517 const unsigned I0 = LittleEndian ? II + 0 : II + 1; 518 const unsigned I1 = LittleEndian ? II + 1 : II + 0; 519 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT); 520 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT); 521 } 522 523 break; 524 default: 525 llvm_unreachable("Invalid Suffix"); 526 } 527 528 MCELFStreamer::emitBytes(StringRef(Buffer, Size)); 529 } 530 531 /// This is one of the functions used to emit data into an ELF section, so the 532 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if 533 /// necessary. 534 void emitBytes(StringRef Data) override { 535 emitDataMappingSymbol(); 536 MCELFStreamer::emitBytes(Data); 537 } 538 539 void FlushPendingMappingSymbol() { 540 if (!LastEMSInfo->hasInfo()) 541 return; 542 ElfMappingSymbolInfo *EMS = LastEMSInfo.get(); 543 EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset); 544 EMS->resetInfo(); 545 } 546 547 /// This is one of the functions used to emit data into an ELF section, so the 548 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if 549 /// necessary. 550 void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override { 551 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) { 552 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) { 553 getContext().reportError(Loc, "relocated expression must be 32-bit"); 554 return; 555 } 556 getOrCreateDataFragment(); 557 } 558 559 emitDataMappingSymbol(); 560 MCELFStreamer::emitValueImpl(Value, Size, Loc); 561 } 562 563 void emitAssemblerFlag(MCAssemblerFlag Flag) override { 564 MCELFStreamer::emitAssemblerFlag(Flag); 565 566 switch (Flag) { 567 case MCAF_SyntaxUnified: 568 return; // no-op here. 569 case MCAF_Code16: 570 IsThumb = true; 571 return; // Change to Thumb mode 572 case MCAF_Code32: 573 IsThumb = false; 574 return; // Change to ARM mode 575 case MCAF_Code64: 576 return; 577 case MCAF_SubsectionsViaSymbols: 578 return; 579 } 580 } 581 582 /// If a label is defined before the .type directive sets the label's type 583 /// then the label can't be recorded as thumb function when the label is 584 /// defined. We override emitSymbolAttribute() which is called as part of the 585 /// parsing of .type so that if the symbol has already been defined we can 586 /// record the label as Thumb. FIXME: there is a corner case where the state 587 /// is changed in between the label definition and the .type directive, this 588 /// is not expected to occur in practice and handling it would require the 589 /// backend to track IsThumb for every label. 590 bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override { 591 bool Val = MCELFStreamer::emitSymbolAttribute(Symbol, Attribute); 592 593 if (!IsThumb) 594 return Val; 595 596 unsigned Type = cast<MCSymbolELF>(Symbol)->getType(); 597 if ((Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC) && 598 Symbol->isDefined()) 599 getAssembler().setIsThumbFunc(Symbol); 600 601 return Val; 602 }; 603 604 private: 605 enum ElfMappingSymbol { 606 EMS_None, 607 EMS_ARM, 608 EMS_Thumb, 609 EMS_Data 610 }; 611 612 struct ElfMappingSymbolInfo { 613 explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O) 614 : Loc(Loc), F(F), Offset(O), State(EMS_None) {} 615 void resetInfo() { 616 F = nullptr; 617 Offset = 0; 618 } 619 bool hasInfo() { return F != nullptr; } 620 SMLoc Loc; 621 MCFragment *F; 622 uint64_t Offset; 623 ElfMappingSymbol State; 624 }; 625 626 void emitDataMappingSymbol() { 627 if (LastEMSInfo->State == EMS_Data) 628 return; 629 else if (LastEMSInfo->State == EMS_None) { 630 // This is a tentative symbol, it won't really be emitted until it's 631 // actually needed. 632 ElfMappingSymbolInfo *EMS = LastEMSInfo.get(); 633 auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); 634 if (!DF) 635 return; 636 EMS->Loc = SMLoc(); 637 EMS->F = getCurrentFragment(); 638 EMS->Offset = DF->getContents().size(); 639 LastEMSInfo->State = EMS_Data; 640 return; 641 } 642 EmitMappingSymbol("$d"); 643 LastEMSInfo->State = EMS_Data; 644 } 645 646 void EmitThumbMappingSymbol() { 647 if (LastEMSInfo->State == EMS_Thumb) 648 return; 649 FlushPendingMappingSymbol(); 650 EmitMappingSymbol("$t"); 651 LastEMSInfo->State = EMS_Thumb; 652 } 653 654 void EmitARMMappingSymbol() { 655 if (LastEMSInfo->State == EMS_ARM) 656 return; 657 FlushPendingMappingSymbol(); 658 EmitMappingSymbol("$a"); 659 LastEMSInfo->State = EMS_ARM; 660 } 661 662 void EmitMappingSymbol(StringRef Name) { 663 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol( 664 Name + "." + Twine(MappingSymbolCounter++))); 665 emitLabel(Symbol); 666 667 Symbol->setType(ELF::STT_NOTYPE); 668 Symbol->setBinding(ELF::STB_LOCAL); 669 } 670 671 void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F, 672 uint64_t Offset) { 673 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol( 674 Name + "." + Twine(MappingSymbolCounter++))); 675 emitLabelAtPos(Symbol, Loc, F, Offset); 676 Symbol->setType(ELF::STT_NOTYPE); 677 Symbol->setBinding(ELF::STB_LOCAL); 678 } 679 680 void emitThumbFunc(MCSymbol *Func) override { 681 getAssembler().setIsThumbFunc(Func); 682 emitSymbolAttribute(Func, MCSA_ELF_TypeFunction); 683 } 684 685 // Helper functions for ARM exception handling directives 686 void EHReset(); 687 688 // Reset state between object emissions 689 void reset() override; 690 691 void EmitPersonalityFixup(StringRef Name); 692 void FlushPendingOffset(); 693 void FlushUnwindOpcodes(bool NoHandlerData); 694 695 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags, 696 SectionKind Kind, const MCSymbol &Fn); 697 void SwitchToExTabSection(const MCSymbol &FnStart); 698 void SwitchToExIdxSection(const MCSymbol &FnStart); 699 700 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind); 701 702 bool IsThumb; 703 bool IsAndroid; 704 int64_t MappingSymbolCounter = 0; 705 706 DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>> 707 LastMappingSymbols; 708 709 std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo; 710 711 // ARM Exception Handling Frame Information 712 MCSymbol *ExTab; 713 MCSymbol *FnStart; 714 const MCSymbol *Personality; 715 unsigned PersonalityIndex; 716 unsigned FPReg; // Frame pointer register 717 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp) 718 int64_t SPOffset; // Offset: (final $sp) - (initial $sp) 719 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp) 720 bool UsedFP; 721 bool CantUnwind; 722 SmallVector<uint8_t, 64> Opcodes; 723 UnwindOpcodeAssembler UnwindOpAsm; 724 }; 725 726 } // end anonymous namespace 727 728 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() { 729 return static_cast<ARMELFStreamer &>(Streamer); 730 } 731 732 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); } 733 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); } 734 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); } 735 736 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) { 737 getStreamer().emitPersonality(Personality); 738 } 739 740 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) { 741 getStreamer().emitPersonalityIndex(Index); 742 } 743 744 void ARMTargetELFStreamer::emitHandlerData() { 745 getStreamer().emitHandlerData(); 746 } 747 748 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg, 749 int64_t Offset) { 750 getStreamer().emitSetFP(FpReg, SpReg, Offset); 751 } 752 753 void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) { 754 getStreamer().emitMovSP(Reg, Offset); 755 } 756 757 void ARMTargetELFStreamer::emitPad(int64_t Offset) { 758 getStreamer().emitPad(Offset); 759 } 760 761 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList, 762 bool isVector) { 763 getStreamer().emitRegSave(RegList, isVector); 764 } 765 766 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset, 767 const SmallVectorImpl<uint8_t> &Opcodes) { 768 getStreamer().emitUnwindRaw(Offset, Opcodes); 769 } 770 771 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) { 772 assert(!Vendor.empty() && "Vendor cannot be empty."); 773 774 if (CurrentVendor == Vendor) 775 return; 776 777 if (!CurrentVendor.empty()) 778 finishAttributeSection(); 779 780 assert(Contents.empty() && 781 ".ARM.attributes should be flushed before changing vendor"); 782 CurrentVendor = Vendor; 783 784 } 785 786 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) { 787 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true); 788 } 789 790 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute, 791 StringRef Value) { 792 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true); 793 } 794 795 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute, 796 unsigned IntValue, 797 StringRef StringValue) { 798 setAttributeItems(Attribute, IntValue, StringValue, 799 /* OverwriteExisting= */ true); 800 } 801 802 void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) { 803 Arch = Value; 804 } 805 806 void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) { 807 EmittedArch = Value; 808 } 809 810 void ARMTargetELFStreamer::emitArchDefaultAttributes() { 811 using namespace ARMBuildAttrs; 812 813 setAttributeItem(CPU_name, 814 ARM::getCPUAttr(Arch), 815 false); 816 817 if (EmittedArch == ARM::ArchKind::INVALID) 818 setAttributeItem(CPU_arch, 819 ARM::getArchAttr(Arch), 820 false); 821 else 822 setAttributeItem(CPU_arch, 823 ARM::getArchAttr(EmittedArch), 824 false); 825 826 switch (Arch) { 827 case ARM::ArchKind::ARMV2: 828 case ARM::ArchKind::ARMV2A: 829 case ARM::ArchKind::ARMV3: 830 case ARM::ArchKind::ARMV3M: 831 case ARM::ArchKind::ARMV4: 832 setAttributeItem(ARM_ISA_use, Allowed, false); 833 break; 834 835 case ARM::ArchKind::ARMV4T: 836 case ARM::ArchKind::ARMV5T: 837 case ARM::ArchKind::XSCALE: 838 case ARM::ArchKind::ARMV5TE: 839 case ARM::ArchKind::ARMV6: 840 setAttributeItem(ARM_ISA_use, Allowed, false); 841 setAttributeItem(THUMB_ISA_use, Allowed, false); 842 break; 843 844 case ARM::ArchKind::ARMV6T2: 845 setAttributeItem(ARM_ISA_use, Allowed, false); 846 setAttributeItem(THUMB_ISA_use, AllowThumb32, false); 847 break; 848 849 case ARM::ArchKind::ARMV6K: 850 case ARM::ArchKind::ARMV6KZ: 851 setAttributeItem(ARM_ISA_use, Allowed, false); 852 setAttributeItem(THUMB_ISA_use, Allowed, false); 853 setAttributeItem(Virtualization_use, AllowTZ, false); 854 break; 855 856 case ARM::ArchKind::ARMV6M: 857 setAttributeItem(THUMB_ISA_use, Allowed, false); 858 break; 859 860 case ARM::ArchKind::ARMV7A: 861 setAttributeItem(CPU_arch_profile, ApplicationProfile, false); 862 setAttributeItem(ARM_ISA_use, Allowed, false); 863 setAttributeItem(THUMB_ISA_use, AllowThumb32, false); 864 break; 865 866 case ARM::ArchKind::ARMV7R: 867 setAttributeItem(CPU_arch_profile, RealTimeProfile, false); 868 setAttributeItem(ARM_ISA_use, Allowed, false); 869 setAttributeItem(THUMB_ISA_use, AllowThumb32, false); 870 break; 871 872 case ARM::ArchKind::ARMV7EM: 873 case ARM::ArchKind::ARMV7M: 874 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false); 875 setAttributeItem(THUMB_ISA_use, AllowThumb32, false); 876 break; 877 878 case ARM::ArchKind::ARMV8A: 879 case ARM::ArchKind::ARMV8_1A: 880 case ARM::ArchKind::ARMV8_2A: 881 case ARM::ArchKind::ARMV8_3A: 882 case ARM::ArchKind::ARMV8_4A: 883 case ARM::ArchKind::ARMV8_5A: 884 case ARM::ArchKind::ARMV8_6A: 885 setAttributeItem(CPU_arch_profile, ApplicationProfile, false); 886 setAttributeItem(ARM_ISA_use, Allowed, false); 887 setAttributeItem(THUMB_ISA_use, AllowThumb32, false); 888 setAttributeItem(MPextension_use, Allowed, false); 889 setAttributeItem(Virtualization_use, AllowTZVirtualization, false); 890 break; 891 892 case ARM::ArchKind::ARMV8MBaseline: 893 case ARM::ArchKind::ARMV8MMainline: 894 setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false); 895 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false); 896 break; 897 898 case ARM::ArchKind::IWMMXT: 899 setAttributeItem(ARM_ISA_use, Allowed, false); 900 setAttributeItem(THUMB_ISA_use, Allowed, false); 901 setAttributeItem(WMMX_arch, AllowWMMXv1, false); 902 break; 903 904 case ARM::ArchKind::IWMMXT2: 905 setAttributeItem(ARM_ISA_use, Allowed, false); 906 setAttributeItem(THUMB_ISA_use, Allowed, false); 907 setAttributeItem(WMMX_arch, AllowWMMXv2, false); 908 break; 909 910 default: 911 report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch))); 912 break; 913 } 914 } 915 916 void ARMTargetELFStreamer::emitFPU(unsigned Value) { 917 FPU = Value; 918 } 919 920 void ARMTargetELFStreamer::emitFPUDefaultAttributes() { 921 switch (FPU) { 922 case ARM::FK_VFP: 923 case ARM::FK_VFPV2: 924 setAttributeItem(ARMBuildAttrs::FP_arch, 925 ARMBuildAttrs::AllowFPv2, 926 /* OverwriteExisting= */ false); 927 break; 928 929 case ARM::FK_VFPV3: 930 setAttributeItem(ARMBuildAttrs::FP_arch, 931 ARMBuildAttrs::AllowFPv3A, 932 /* OverwriteExisting= */ false); 933 break; 934 935 case ARM::FK_VFPV3_FP16: 936 setAttributeItem(ARMBuildAttrs::FP_arch, 937 ARMBuildAttrs::AllowFPv3A, 938 /* OverwriteExisting= */ false); 939 setAttributeItem(ARMBuildAttrs::FP_HP_extension, 940 ARMBuildAttrs::AllowHPFP, 941 /* OverwriteExisting= */ false); 942 break; 943 944 case ARM::FK_VFPV3_D16: 945 setAttributeItem(ARMBuildAttrs::FP_arch, 946 ARMBuildAttrs::AllowFPv3B, 947 /* OverwriteExisting= */ false); 948 break; 949 950 case ARM::FK_VFPV3_D16_FP16: 951 setAttributeItem(ARMBuildAttrs::FP_arch, 952 ARMBuildAttrs::AllowFPv3B, 953 /* OverwriteExisting= */ false); 954 setAttributeItem(ARMBuildAttrs::FP_HP_extension, 955 ARMBuildAttrs::AllowHPFP, 956 /* OverwriteExisting= */ false); 957 break; 958 959 case ARM::FK_VFPV3XD: 960 setAttributeItem(ARMBuildAttrs::FP_arch, 961 ARMBuildAttrs::AllowFPv3B, 962 /* OverwriteExisting= */ false); 963 break; 964 case ARM::FK_VFPV3XD_FP16: 965 setAttributeItem(ARMBuildAttrs::FP_arch, 966 ARMBuildAttrs::AllowFPv3B, 967 /* OverwriteExisting= */ false); 968 setAttributeItem(ARMBuildAttrs::FP_HP_extension, 969 ARMBuildAttrs::AllowHPFP, 970 /* OverwriteExisting= */ false); 971 break; 972 973 case ARM::FK_VFPV4: 974 setAttributeItem(ARMBuildAttrs::FP_arch, 975 ARMBuildAttrs::AllowFPv4A, 976 /* OverwriteExisting= */ false); 977 break; 978 979 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same 980 // as _D16 here. 981 case ARM::FK_FPV4_SP_D16: 982 case ARM::FK_VFPV4_D16: 983 setAttributeItem(ARMBuildAttrs::FP_arch, 984 ARMBuildAttrs::AllowFPv4B, 985 /* OverwriteExisting= */ false); 986 break; 987 988 case ARM::FK_FP_ARMV8: 989 setAttributeItem(ARMBuildAttrs::FP_arch, 990 ARMBuildAttrs::AllowFPARMv8A, 991 /* OverwriteExisting= */ false); 992 break; 993 994 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so 995 // uses the FP_ARMV8_D16 build attribute. 996 case ARM::FK_FPV5_SP_D16: 997 case ARM::FK_FPV5_D16: 998 setAttributeItem(ARMBuildAttrs::FP_arch, 999 ARMBuildAttrs::AllowFPARMv8B, 1000 /* OverwriteExisting= */ false); 1001 break; 1002 1003 case ARM::FK_NEON: 1004 setAttributeItem(ARMBuildAttrs::FP_arch, 1005 ARMBuildAttrs::AllowFPv3A, 1006 /* OverwriteExisting= */ false); 1007 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch, 1008 ARMBuildAttrs::AllowNeon, 1009 /* OverwriteExisting= */ false); 1010 break; 1011 1012 case ARM::FK_NEON_FP16: 1013 setAttributeItem(ARMBuildAttrs::FP_arch, 1014 ARMBuildAttrs::AllowFPv3A, 1015 /* OverwriteExisting= */ false); 1016 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch, 1017 ARMBuildAttrs::AllowNeon, 1018 /* OverwriteExisting= */ false); 1019 setAttributeItem(ARMBuildAttrs::FP_HP_extension, 1020 ARMBuildAttrs::AllowHPFP, 1021 /* OverwriteExisting= */ false); 1022 break; 1023 1024 case ARM::FK_NEON_VFPV4: 1025 setAttributeItem(ARMBuildAttrs::FP_arch, 1026 ARMBuildAttrs::AllowFPv4A, 1027 /* OverwriteExisting= */ false); 1028 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch, 1029 ARMBuildAttrs::AllowNeon2, 1030 /* OverwriteExisting= */ false); 1031 break; 1032 1033 case ARM::FK_NEON_FP_ARMV8: 1034 case ARM::FK_CRYPTO_NEON_FP_ARMV8: 1035 setAttributeItem(ARMBuildAttrs::FP_arch, 1036 ARMBuildAttrs::AllowFPARMv8A, 1037 /* OverwriteExisting= */ false); 1038 // 'Advanced_SIMD_arch' must be emitted not here, but within 1039 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a() 1040 break; 1041 1042 case ARM::FK_SOFTVFP: 1043 case ARM::FK_NONE: 1044 break; 1045 1046 default: 1047 report_fatal_error("Unknown FPU: " + Twine(FPU)); 1048 break; 1049 } 1050 } 1051 1052 size_t ARMTargetELFStreamer::calculateContentSize() const { 1053 size_t Result = 0; 1054 for (size_t i = 0; i < Contents.size(); ++i) { 1055 AttributeItem item = Contents[i]; 1056 switch (item.Type) { 1057 case AttributeItem::HiddenAttribute: 1058 break; 1059 case AttributeItem::NumericAttribute: 1060 Result += getULEB128Size(item.Tag); 1061 Result += getULEB128Size(item.IntValue); 1062 break; 1063 case AttributeItem::TextAttribute: 1064 Result += getULEB128Size(item.Tag); 1065 Result += item.StringValue.size() + 1; // string + '\0' 1066 break; 1067 case AttributeItem::NumericAndTextAttributes: 1068 Result += getULEB128Size(item.Tag); 1069 Result += getULEB128Size(item.IntValue); 1070 Result += item.StringValue.size() + 1; // string + '\0'; 1071 break; 1072 } 1073 } 1074 return Result; 1075 } 1076 1077 void ARMTargetELFStreamer::finishAttributeSection() { 1078 // <format-version> 1079 // [ <section-length> "vendor-name" 1080 // [ <file-tag> <size> <attribute>* 1081 // | <section-tag> <size> <section-number>* 0 <attribute>* 1082 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>* 1083 // ]+ 1084 // ]* 1085 1086 if (FPU != ARM::FK_INVALID) 1087 emitFPUDefaultAttributes(); 1088 1089 if (Arch != ARM::ArchKind::INVALID) 1090 emitArchDefaultAttributes(); 1091 1092 if (Contents.empty()) 1093 return; 1094 1095 llvm::sort(Contents, AttributeItem::LessTag); 1096 1097 ARMELFStreamer &Streamer = getStreamer(); 1098 1099 // Switch to .ARM.attributes section 1100 if (AttributeSection) { 1101 Streamer.SwitchSection(AttributeSection); 1102 } else { 1103 AttributeSection = Streamer.getContext().getELFSection( 1104 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0); 1105 Streamer.SwitchSection(AttributeSection); 1106 1107 // Format version 1108 Streamer.emitInt8(0x41); 1109 } 1110 1111 // Vendor size + Vendor name + '\0' 1112 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1; 1113 1114 // Tag + Tag Size 1115 const size_t TagHeaderSize = 1 + 4; 1116 1117 const size_t ContentsSize = calculateContentSize(); 1118 1119 Streamer.emitInt32(VendorHeaderSize + TagHeaderSize + ContentsSize); 1120 Streamer.emitBytes(CurrentVendor); 1121 Streamer.emitInt8(0); // '\0' 1122 1123 Streamer.emitInt8(ARMBuildAttrs::File); 1124 Streamer.emitInt32(TagHeaderSize + ContentsSize); 1125 1126 // Size should have been accounted for already, now 1127 // emit each field as its type (ULEB or String) 1128 for (size_t i = 0; i < Contents.size(); ++i) { 1129 AttributeItem item = Contents[i]; 1130 Streamer.emitULEB128IntValue(item.Tag); 1131 switch (item.Type) { 1132 default: llvm_unreachable("Invalid attribute type"); 1133 case AttributeItem::NumericAttribute: 1134 Streamer.emitULEB128IntValue(item.IntValue); 1135 break; 1136 case AttributeItem::TextAttribute: 1137 Streamer.emitBytes(item.StringValue); 1138 Streamer.emitInt8(0); // '\0' 1139 break; 1140 case AttributeItem::NumericAndTextAttributes: 1141 Streamer.emitULEB128IntValue(item.IntValue); 1142 Streamer.emitBytes(item.StringValue); 1143 Streamer.emitInt8(0); // '\0' 1144 break; 1145 } 1146 } 1147 1148 Contents.clear(); 1149 FPU = ARM::FK_INVALID; 1150 } 1151 1152 void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) { 1153 ARMELFStreamer &Streamer = getStreamer(); 1154 if (!Streamer.IsThumb) 1155 return; 1156 1157 Streamer.getAssembler().registerSymbol(*Symbol); 1158 unsigned Type = cast<MCSymbolELF>(Symbol)->getType(); 1159 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC) 1160 Streamer.emitThumbFunc(Symbol); 1161 } 1162 1163 void 1164 ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) { 1165 getStreamer().EmitFixup(S, FK_Data_4); 1166 } 1167 1168 void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) { 1169 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) { 1170 const MCSymbol &Sym = SRE->getSymbol(); 1171 if (!Sym.isDefined()) { 1172 getStreamer().emitAssignment(Symbol, Value); 1173 return; 1174 } 1175 } 1176 1177 getStreamer().emitThumbFunc(Symbol); 1178 getStreamer().emitAssignment(Symbol, Value); 1179 } 1180 1181 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) { 1182 getStreamer().emitInst(Inst, Suffix); 1183 } 1184 1185 void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; } 1186 1187 void ARMELFStreamer::finishImpl() { 1188 MCTargetStreamer &TS = *getTargetStreamer(); 1189 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS); 1190 ATS.finishAttributeSection(); 1191 1192 MCELFStreamer::finishImpl(); 1193 } 1194 1195 void ARMELFStreamer::reset() { 1196 MCTargetStreamer &TS = *getTargetStreamer(); 1197 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS); 1198 ATS.reset(); 1199 MappingSymbolCounter = 0; 1200 MCELFStreamer::reset(); 1201 LastMappingSymbols.clear(); 1202 LastEMSInfo.reset(); 1203 // MCELFStreamer clear's the assembler's e_flags. However, for 1204 // arm we manually set the ABI version on streamer creation, so 1205 // do the same here 1206 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5); 1207 } 1208 1209 inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix, 1210 unsigned Type, 1211 unsigned Flags, 1212 SectionKind Kind, 1213 const MCSymbol &Fn) { 1214 const MCSectionELF &FnSection = 1215 static_cast<const MCSectionELF &>(Fn.getSection()); 1216 1217 // Create the name for new section 1218 StringRef FnSecName(FnSection.getName()); 1219 SmallString<128> EHSecName(Prefix); 1220 if (FnSecName != ".text") { 1221 EHSecName += FnSecName; 1222 } 1223 1224 // Get .ARM.extab or .ARM.exidx section 1225 const MCSymbolELF *Group = FnSection.getGroup(); 1226 if (Group) 1227 Flags |= ELF::SHF_GROUP; 1228 MCSectionELF *EHSection = getContext().getELFSection( 1229 EHSecName, Type, Flags, 0, Group, /*IsComdat=*/true, 1230 FnSection.getUniqueID(), 1231 static_cast<const MCSymbolELF *>(FnSection.getBeginSymbol())); 1232 1233 assert(EHSection && "Failed to get the required EH section"); 1234 1235 // Switch to .ARM.extab or .ARM.exidx section 1236 SwitchSection(EHSection); 1237 emitCodeAlignment(4); 1238 } 1239 1240 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) { 1241 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC, 1242 SectionKind::getData(), FnStart); 1243 } 1244 1245 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) { 1246 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX, 1247 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER, 1248 SectionKind::getData(), FnStart); 1249 } 1250 1251 void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) { 1252 MCDataFragment *Frag = getOrCreateDataFragment(); 1253 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr, 1254 Kind)); 1255 } 1256 1257 void ARMELFStreamer::EHReset() { 1258 ExTab = nullptr; 1259 FnStart = nullptr; 1260 Personality = nullptr; 1261 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX; 1262 FPReg = ARM::SP; 1263 FPOffset = 0; 1264 SPOffset = 0; 1265 PendingOffset = 0; 1266 UsedFP = false; 1267 CantUnwind = false; 1268 1269 Opcodes.clear(); 1270 UnwindOpAsm.Reset(); 1271 } 1272 1273 void ARMELFStreamer::emitFnStart() { 1274 assert(FnStart == nullptr); 1275 FnStart = getContext().createTempSymbol(); 1276 emitLabel(FnStart); 1277 } 1278 1279 void ARMELFStreamer::emitFnEnd() { 1280 assert(FnStart && ".fnstart must precedes .fnend"); 1281 1282 // Emit unwind opcodes if there is no .handlerdata directive 1283 if (!ExTab && !CantUnwind) 1284 FlushUnwindOpcodes(true); 1285 1286 // Emit the exception index table entry 1287 SwitchToExIdxSection(*FnStart); 1288 1289 // The EHABI requires a dependency preserving R_ARM_NONE relocation to the 1290 // personality routine to protect it from an arbitrary platform's static 1291 // linker garbage collection. We disable this for Android where the unwinder 1292 // is either dynamically linked or directly references the personality 1293 // routine. 1294 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX && !IsAndroid) 1295 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex)); 1296 1297 const MCSymbolRefExpr *FnStartRef = 1298 MCSymbolRefExpr::create(FnStart, 1299 MCSymbolRefExpr::VK_ARM_PREL31, 1300 getContext()); 1301 1302 emitValue(FnStartRef, 4); 1303 1304 if (CantUnwind) { 1305 emitInt32(ARM::EHABI::EXIDX_CANTUNWIND); 1306 } else if (ExTab) { 1307 // Emit a reference to the unwind opcodes in the ".ARM.extab" section. 1308 const MCSymbolRefExpr *ExTabEntryRef = 1309 MCSymbolRefExpr::create(ExTab, 1310 MCSymbolRefExpr::VK_ARM_PREL31, 1311 getContext()); 1312 emitValue(ExTabEntryRef, 4); 1313 } else { 1314 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in 1315 // the second word of exception index table entry. The size of the unwind 1316 // opcodes should always be 4 bytes. 1317 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 && 1318 "Compact model must use __aeabi_unwind_cpp_pr0 as personality"); 1319 assert(Opcodes.size() == 4u && 1320 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4"); 1321 uint64_t Intval = Opcodes[0] | 1322 Opcodes[1] << 8 | 1323 Opcodes[2] << 16 | 1324 Opcodes[3] << 24; 1325 emitIntValue(Intval, Opcodes.size()); 1326 } 1327 1328 // Switch to the section containing FnStart 1329 SwitchSection(&FnStart->getSection()); 1330 1331 // Clean exception handling frame information 1332 EHReset(); 1333 } 1334 1335 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; } 1336 1337 // Add the R_ARM_NONE fixup at the same position 1338 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) { 1339 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name); 1340 1341 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create( 1342 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext()); 1343 1344 visitUsedExpr(*PersonalityRef); 1345 MCDataFragment *DF = getOrCreateDataFragment(); 1346 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), 1347 PersonalityRef, 1348 MCFixup::getKindForSize(4, false))); 1349 } 1350 1351 void ARMELFStreamer::FlushPendingOffset() { 1352 if (PendingOffset != 0) { 1353 UnwindOpAsm.EmitSPOffset(-PendingOffset); 1354 PendingOffset = 0; 1355 } 1356 } 1357 1358 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) { 1359 // Emit the unwind opcode to restore $sp. 1360 if (UsedFP) { 1361 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 1362 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset; 1363 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset); 1364 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg)); 1365 } else { 1366 FlushPendingOffset(); 1367 } 1368 1369 // Finalize the unwind opcode sequence 1370 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes); 1371 1372 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx 1373 // section. Thus, we don't have to create an entry in the .ARM.extab 1374 // section. 1375 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0) 1376 return; 1377 1378 // Switch to .ARM.extab section. 1379 SwitchToExTabSection(*FnStart); 1380 1381 // Create .ARM.extab label for offset in .ARM.exidx 1382 assert(!ExTab); 1383 ExTab = getContext().createTempSymbol(); 1384 emitLabel(ExTab); 1385 1386 // Emit personality 1387 if (Personality) { 1388 const MCSymbolRefExpr *PersonalityRef = 1389 MCSymbolRefExpr::create(Personality, 1390 MCSymbolRefExpr::VK_ARM_PREL31, 1391 getContext()); 1392 1393 emitValue(PersonalityRef, 4); 1394 } 1395 1396 // Emit unwind opcodes 1397 assert((Opcodes.size() % 4) == 0 && 1398 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4"); 1399 for (unsigned I = 0; I != Opcodes.size(); I += 4) { 1400 uint64_t Intval = Opcodes[I] | 1401 Opcodes[I + 1] << 8 | 1402 Opcodes[I + 2] << 16 | 1403 Opcodes[I + 3] << 24; 1404 emitInt32(Intval); 1405 } 1406 1407 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or 1408 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted 1409 // after the unwind opcodes. The handler data consists of several 32-bit 1410 // words, and should be terminated by zero. 1411 // 1412 // In case that the .handlerdata directive is not specified by the 1413 // programmer, we should emit zero to terminate the handler data. 1414 if (NoHandlerData && !Personality) 1415 emitInt32(0); 1416 } 1417 1418 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); } 1419 1420 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) { 1421 Personality = Per; 1422 UnwindOpAsm.setPersonality(Per); 1423 } 1424 1425 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) { 1426 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index"); 1427 PersonalityIndex = Index; 1428 } 1429 1430 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg, 1431 int64_t Offset) { 1432 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) && 1433 "the operand of .setfp directive should be either $sp or $fp"); 1434 1435 UsedFP = true; 1436 FPReg = NewFPReg; 1437 1438 if (NewSPReg == ARM::SP) 1439 FPOffset = SPOffset + Offset; 1440 else 1441 FPOffset += Offset; 1442 } 1443 1444 void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) { 1445 assert((Reg != ARM::SP && Reg != ARM::PC) && 1446 "the operand of .movsp cannot be either sp or pc"); 1447 assert(FPReg == ARM::SP && "current FP must be SP"); 1448 1449 FlushPendingOffset(); 1450 1451 FPReg = Reg; 1452 FPOffset = SPOffset + Offset; 1453 1454 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 1455 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg)); 1456 } 1457 1458 void ARMELFStreamer::emitPad(int64_t Offset) { 1459 // Track the change of the $sp offset 1460 SPOffset -= Offset; 1461 1462 // To squash multiple .pad directives, we should delay the unwind opcode 1463 // until the .save, .vsave, .handlerdata, or .fnend directives. 1464 PendingOffset -= Offset; 1465 } 1466 1467 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList, 1468 bool IsVector) { 1469 // Collect the registers in the register list 1470 unsigned Count = 0; 1471 uint32_t Mask = 0; 1472 const MCRegisterInfo *MRI = getContext().getRegisterInfo(); 1473 for (size_t i = 0; i < RegList.size(); ++i) { 1474 unsigned Reg = MRI->getEncodingValue(RegList[i]); 1475 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range"); 1476 unsigned Bit = (1u << Reg); 1477 if ((Mask & Bit) == 0) { 1478 Mask |= Bit; 1479 ++Count; 1480 } 1481 } 1482 1483 // Track the change the $sp offset: For the .save directive, the 1484 // corresponding push instruction will decrease the $sp by (4 * Count). 1485 // For the .vsave directive, the corresponding vpush instruction will 1486 // decrease $sp by (8 * Count). 1487 SPOffset -= Count * (IsVector ? 8 : 4); 1488 1489 // Emit the opcode 1490 FlushPendingOffset(); 1491 if (IsVector) 1492 UnwindOpAsm.EmitVFPRegSave(Mask); 1493 else 1494 UnwindOpAsm.EmitRegSave(Mask); 1495 } 1496 1497 void ARMELFStreamer::emitUnwindRaw(int64_t Offset, 1498 const SmallVectorImpl<uint8_t> &Opcodes) { 1499 FlushPendingOffset(); 1500 SPOffset = SPOffset - Offset; 1501 UnwindOpAsm.EmitRaw(Opcodes); 1502 } 1503 1504 namespace llvm { 1505 1506 MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S, 1507 formatted_raw_ostream &OS, 1508 MCInstPrinter *InstPrint, 1509 bool isVerboseAsm) { 1510 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm); 1511 } 1512 1513 MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) { 1514 return new ARMTargetStreamer(S); 1515 } 1516 1517 MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S, 1518 const MCSubtargetInfo &STI) { 1519 const Triple &TT = STI.getTargetTriple(); 1520 if (TT.isOSBinFormatELF()) 1521 return new ARMTargetELFStreamer(S); 1522 return new ARMTargetStreamer(S); 1523 } 1524 1525 MCELFStreamer *createARMELFStreamer(MCContext &Context, 1526 std::unique_ptr<MCAsmBackend> TAB, 1527 std::unique_ptr<MCObjectWriter> OW, 1528 std::unique_ptr<MCCodeEmitter> Emitter, 1529 bool RelaxAll, bool IsThumb, 1530 bool IsAndroid) { 1531 ARMELFStreamer *S = 1532 new ARMELFStreamer(Context, std::move(TAB), std::move(OW), 1533 std::move(Emitter), IsThumb, IsAndroid); 1534 // FIXME: This should eventually end up somewhere else where more 1535 // intelligent flag decisions can be made. For now we are just maintaining 1536 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default. 1537 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5); 1538 1539 if (RelaxAll) 1540 S->getAssembler().setRelaxAll(true); 1541 return S; 1542 } 1543 1544 } // end namespace llvm 1545