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