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