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