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