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