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