10b57cec5SDimitry Andric //===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file assembles .s files and emits AArch64 ELF .o object files. Different
100b57cec5SDimitry Andric // from generic ELF streamer in emitting mapping symbols ($x and $d) to delimit
110b57cec5SDimitry Andric // regions of data and code.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric
15fe6060f1SDimitry Andric #include "AArch64ELFStreamer.h"
16fe6060f1SDimitry Andric #include "AArch64MCTargetDesc.h"
170b57cec5SDimitry Andric #include "AArch64TargetStreamer.h"
180b57cec5SDimitry Andric #include "AArch64WinCOFFStreamer.h"
190b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
200b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
210b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
220b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCAsmBackend.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCAssembler.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCELFStreamer.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
300b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
31*0fca6ea1SDimitry Andric #include "llvm/MC/MCSectionELF.h"
320b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
340b57cec5SDimitry Andric #include "llvm/MC/MCSymbolELF.h"
350b57cec5SDimitry Andric #include "llvm/MC/MCWinCOFFStreamer.h"
360b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
370b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
380b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
3906c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric using namespace llvm;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric namespace {
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric class AArch64ELFStreamer;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
480b57cec5SDimitry Andric formatted_raw_ostream &OS;
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric void emitInst(uint32_t Inst) override;
510b57cec5SDimitry Andric
emitDirectiveVariantPCS(MCSymbol * Symbol)52eaeb601bSDimitry Andric void emitDirectiveVariantPCS(MCSymbol *Symbol) override {
53fe6060f1SDimitry Andric OS << "\t.variant_pcs\t" << Symbol->getName() << "\n";
54eaeb601bSDimitry Andric }
55eaeb601bSDimitry Andric
emitARM64WinCFIAllocStack(unsigned Size)56fe6060f1SDimitry Andric void emitARM64WinCFIAllocStack(unsigned Size) override {
57fe6060f1SDimitry Andric OS << "\t.seh_stackalloc\t" << Size << "\n";
58e8d8bef9SDimitry Andric }
emitARM64WinCFISaveR19R20X(int Offset)59fe6060f1SDimitry Andric void emitARM64WinCFISaveR19R20X(int Offset) override {
60fe6060f1SDimitry Andric OS << "\t.seh_save_r19r20_x\t" << Offset << "\n";
61e8d8bef9SDimitry Andric }
emitARM64WinCFISaveFPLR(int Offset)62fe6060f1SDimitry Andric void emitARM64WinCFISaveFPLR(int Offset) override {
63fe6060f1SDimitry Andric OS << "\t.seh_save_fplr\t" << Offset << "\n";
64e8d8bef9SDimitry Andric }
emitARM64WinCFISaveFPLRX(int Offset)65fe6060f1SDimitry Andric void emitARM64WinCFISaveFPLRX(int Offset) override {
66fe6060f1SDimitry Andric OS << "\t.seh_save_fplr_x\t" << Offset << "\n";
67e8d8bef9SDimitry Andric }
emitARM64WinCFISaveReg(unsigned Reg,int Offset)68fe6060f1SDimitry Andric void emitARM64WinCFISaveReg(unsigned Reg, int Offset) override {
69fe6060f1SDimitry Andric OS << "\t.seh_save_reg\tx" << Reg << ", " << Offset << "\n";
70e8d8bef9SDimitry Andric }
emitARM64WinCFISaveRegX(unsigned Reg,int Offset)71fe6060f1SDimitry Andric void emitARM64WinCFISaveRegX(unsigned Reg, int Offset) override {
72fe6060f1SDimitry Andric OS << "\t.seh_save_reg_x\tx" << Reg << ", " << Offset << "\n";
73e8d8bef9SDimitry Andric }
emitARM64WinCFISaveRegP(unsigned Reg,int Offset)74fe6060f1SDimitry Andric void emitARM64WinCFISaveRegP(unsigned Reg, int Offset) override {
75fe6060f1SDimitry Andric OS << "\t.seh_save_regp\tx" << Reg << ", " << Offset << "\n";
76e8d8bef9SDimitry Andric }
emitARM64WinCFISaveRegPX(unsigned Reg,int Offset)77fe6060f1SDimitry Andric void emitARM64WinCFISaveRegPX(unsigned Reg, int Offset) override {
78fe6060f1SDimitry Andric OS << "\t.seh_save_regp_x\tx" << Reg << ", " << Offset << "\n";
79e8d8bef9SDimitry Andric }
emitARM64WinCFISaveLRPair(unsigned Reg,int Offset)80fe6060f1SDimitry Andric void emitARM64WinCFISaveLRPair(unsigned Reg, int Offset) override {
81fe6060f1SDimitry Andric OS << "\t.seh_save_lrpair\tx" << Reg << ", " << Offset << "\n";
82e8d8bef9SDimitry Andric }
emitARM64WinCFISaveFReg(unsigned Reg,int Offset)83fe6060f1SDimitry Andric void emitARM64WinCFISaveFReg(unsigned Reg, int Offset) override {
84fe6060f1SDimitry Andric OS << "\t.seh_save_freg\td" << Reg << ", " << Offset << "\n";
85e8d8bef9SDimitry Andric }
emitARM64WinCFISaveFRegX(unsigned Reg,int Offset)86fe6060f1SDimitry Andric void emitARM64WinCFISaveFRegX(unsigned Reg, int Offset) override {
87fe6060f1SDimitry Andric OS << "\t.seh_save_freg_x\td" << Reg << ", " << Offset << "\n";
88e8d8bef9SDimitry Andric }
emitARM64WinCFISaveFRegP(unsigned Reg,int Offset)89fe6060f1SDimitry Andric void emitARM64WinCFISaveFRegP(unsigned Reg, int Offset) override {
90fe6060f1SDimitry Andric OS << "\t.seh_save_fregp\td" << Reg << ", " << Offset << "\n";
91e8d8bef9SDimitry Andric }
emitARM64WinCFISaveFRegPX(unsigned Reg,int Offset)92fe6060f1SDimitry Andric void emitARM64WinCFISaveFRegPX(unsigned Reg, int Offset) override {
93fe6060f1SDimitry Andric OS << "\t.seh_save_fregp_x\td" << Reg << ", " << Offset << "\n";
94e8d8bef9SDimitry Andric }
emitARM64WinCFISetFP()95fe6060f1SDimitry Andric void emitARM64WinCFISetFP() override { OS << "\t.seh_set_fp\n"; }
emitARM64WinCFIAddFP(unsigned Size)96fe6060f1SDimitry Andric void emitARM64WinCFIAddFP(unsigned Size) override {
97fe6060f1SDimitry Andric OS << "\t.seh_add_fp\t" << Size << "\n";
98e8d8bef9SDimitry Andric }
emitARM64WinCFINop()99fe6060f1SDimitry Andric void emitARM64WinCFINop() override { OS << "\t.seh_nop\n"; }
emitARM64WinCFISaveNext()100fe6060f1SDimitry Andric void emitARM64WinCFISaveNext() override { OS << "\t.seh_save_next\n"; }
emitARM64WinCFIPrologEnd()101fe6060f1SDimitry Andric void emitARM64WinCFIPrologEnd() override { OS << "\t.seh_endprologue\n"; }
emitARM64WinCFIEpilogStart()102fe6060f1SDimitry Andric void emitARM64WinCFIEpilogStart() override { OS << "\t.seh_startepilogue\n"; }
emitARM64WinCFIEpilogEnd()103fe6060f1SDimitry Andric void emitARM64WinCFIEpilogEnd() override { OS << "\t.seh_endepilogue\n"; }
emitARM64WinCFITrapFrame()104fe6060f1SDimitry Andric void emitARM64WinCFITrapFrame() override { OS << "\t.seh_trap_frame\n"; }
emitARM64WinCFIMachineFrame()105fe6060f1SDimitry Andric void emitARM64WinCFIMachineFrame() override { OS << "\t.seh_pushframe\n"; }
emitARM64WinCFIContext()106fe6060f1SDimitry Andric void emitARM64WinCFIContext() override { OS << "\t.seh_context\n"; }
emitARM64WinCFIECContext()1075f757f3fSDimitry Andric void emitARM64WinCFIECContext() override { OS << "\t.seh_ec_context\n"; }
emitARM64WinCFIClearUnwoundToCall()108fe6060f1SDimitry Andric void emitARM64WinCFIClearUnwoundToCall() override {
109e8d8bef9SDimitry Andric OS << "\t.seh_clear_unwound_to_call\n";
110e8d8bef9SDimitry Andric }
emitARM64WinCFIPACSignLR()111bdd1243dSDimitry Andric void emitARM64WinCFIPACSignLR() override {
112bdd1243dSDimitry Andric OS << "\t.seh_pac_sign_lr\n";
113bdd1243dSDimitry Andric }
114bdd1243dSDimitry Andric
emitARM64WinCFISaveAnyRegI(unsigned Reg,int Offset)115bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegI(unsigned Reg, int Offset) override {
116bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg\tx" << Reg << ", " << Offset << "\n";
117bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegIP(unsigned Reg,int Offset)118bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegIP(unsigned Reg, int Offset) override {
119bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_p\tx" << Reg << ", " << Offset << "\n";
120bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegD(unsigned Reg,int Offset)121bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegD(unsigned Reg, int Offset) override {
122bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg\td" << Reg << ", " << Offset << "\n";
123bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegDP(unsigned Reg,int Offset)124bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegDP(unsigned Reg, int Offset) override {
125bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_p\td" << Reg << ", " << Offset << "\n";
126bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegQ(unsigned Reg,int Offset)127bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegQ(unsigned Reg, int Offset) override {
128bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg\tq" << Reg << ", " << Offset << "\n";
129bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegQP(unsigned Reg,int Offset)130bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegQP(unsigned Reg, int Offset) override {
131bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_p\tq" << Reg << ", " << Offset << "\n";
132bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegIX(unsigned Reg,int Offset)133bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegIX(unsigned Reg, int Offset) override {
134bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_x\tx" << Reg << ", " << Offset << "\n";
135bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegIPX(unsigned Reg,int Offset)136bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegIPX(unsigned Reg, int Offset) override {
137bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_px\tx" << Reg << ", " << Offset << "\n";
138bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegDX(unsigned Reg,int Offset)139bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegDX(unsigned Reg, int Offset) override {
140bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_x\td" << Reg << ", " << Offset << "\n";
141bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegDPX(unsigned Reg,int Offset)142bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegDPX(unsigned Reg, int Offset) override {
143bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_px\td" << Reg << ", " << Offset << "\n";
144bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegQX(unsigned Reg,int Offset)145bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegQX(unsigned Reg, int Offset) override {
146bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_x\tq" << Reg << ", " << Offset << "\n";
147bdd1243dSDimitry Andric }
emitARM64WinCFISaveAnyRegQPX(unsigned Reg,int Offset)148bdd1243dSDimitry Andric void emitARM64WinCFISaveAnyRegQPX(unsigned Reg, int Offset) override {
149bdd1243dSDimitry Andric OS << "\t.seh_save_any_reg_px\tq" << Reg << ", " << Offset << "\n";
150bdd1243dSDimitry Andric }
151e8d8bef9SDimitry Andric
1520b57cec5SDimitry Andric public:
1530b57cec5SDimitry Andric AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
1540b57cec5SDimitry Andric };
1550b57cec5SDimitry Andric
AArch64TargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS)1560b57cec5SDimitry Andric AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
1570b57cec5SDimitry Andric formatted_raw_ostream &OS)
1580b57cec5SDimitry Andric : AArch64TargetStreamer(S), OS(OS) {}
1590b57cec5SDimitry Andric
emitInst(uint32_t Inst)1600b57cec5SDimitry Andric void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
1610b57cec5SDimitry Andric OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
1650b57cec5SDimitry Andric /// the appropriate points in the object files. These symbols are defined in the
1660b57cec5SDimitry Andric /// AArch64 ELF ABI:
1670b57cec5SDimitry Andric /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
1680b57cec5SDimitry Andric ///
1690b57cec5SDimitry Andric /// In brief: $x or $d should be emitted at the start of each contiguous region
1700b57cec5SDimitry Andric /// of A64 code or data in a section. In practice, this emission does not rely
1710b57cec5SDimitry Andric /// on explicit assembler directives but on inherent properties of the
1720b57cec5SDimitry Andric /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
1730b57cec5SDimitry Andric /// instruction).
1740b57cec5SDimitry Andric ///
1750b57cec5SDimitry Andric /// As a result this system is orthogonal to the DataRegion infrastructure used
1760b57cec5SDimitry Andric /// by MachO. Beware!
1770b57cec5SDimitry Andric class AArch64ELFStreamer : public MCELFStreamer {
1780b57cec5SDimitry Andric public:
AArch64ELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)1790b57cec5SDimitry Andric AArch64ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
1800b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> OW,
1810b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> Emitter)
1820b57cec5SDimitry Andric : MCELFStreamer(Context, std::move(TAB), std::move(OW),
1830b57cec5SDimitry Andric std::move(Emitter)),
184*0fca6ea1SDimitry Andric LastEMS(EMS_None) {}
1850b57cec5SDimitry Andric
changeSection(MCSection * Section,uint32_t Subsection=0)186*0fca6ea1SDimitry Andric void changeSection(MCSection *Section, uint32_t Subsection = 0) override {
1870b57cec5SDimitry Andric // We have to keep track of the mapping symbol state of any sections we
1880b57cec5SDimitry Andric // use. Each one should start off as EMS_None, which is provided as the
1890b57cec5SDimitry Andric // default constructor by DenseMap::lookup.
190*0fca6ea1SDimitry Andric LastMappingSymbols[getCurrentSection().first] = LastEMS;
1910b57cec5SDimitry Andric LastEMS = LastMappingSymbols.lookup(Section);
1920b57cec5SDimitry Andric
1935ffd83dbSDimitry Andric MCELFStreamer::changeSection(Section, Subsection);
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric // Reset state between object emissions
reset()1970b57cec5SDimitry Andric void reset() override {
1980b57cec5SDimitry Andric MCELFStreamer::reset();
1990b57cec5SDimitry Andric LastMappingSymbols.clear();
2000b57cec5SDimitry Andric LastEMS = EMS_None;
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric /// This function is the one used to emit instruction data into the ELF
2040b57cec5SDimitry Andric /// streamer. We override it to add the appropriate mapping symbol if
2050b57cec5SDimitry Andric /// necessary.
emitInstruction(const MCInst & Inst,const MCSubtargetInfo & STI)2065ffd83dbSDimitry Andric void emitInstruction(const MCInst &Inst,
2070b57cec5SDimitry Andric const MCSubtargetInfo &STI) override {
208fe6060f1SDimitry Andric emitA64MappingSymbol();
2095ffd83dbSDimitry Andric MCELFStreamer::emitInstruction(Inst, STI);
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric
2120b57cec5SDimitry Andric /// Emit a 32-bit value as an instruction. This is only used for the .inst
2130b57cec5SDimitry Andric /// directive, EmitInstruction should be used in other cases.
emitInst(uint32_t Inst)2140b57cec5SDimitry Andric void emitInst(uint32_t Inst) {
2150b57cec5SDimitry Andric char Buffer[4];
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric // We can't just use EmitIntValue here, as that will emit a data mapping
2180b57cec5SDimitry Andric // symbol, and swap the endianness on big-endian systems (instructions are
2190b57cec5SDimitry Andric // always little-endian).
2204824e7fdSDimitry Andric for (char &C : Buffer) {
2214824e7fdSDimitry Andric C = uint8_t(Inst);
2220b57cec5SDimitry Andric Inst >>= 8;
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric
225fe6060f1SDimitry Andric emitA64MappingSymbol();
2265ffd83dbSDimitry Andric MCELFStreamer::emitBytes(StringRef(Buffer, 4));
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric
2290b57cec5SDimitry Andric /// This is one of the functions used to emit data into an ELF section, so the
2300b57cec5SDimitry Andric /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
2310b57cec5SDimitry Andric /// if necessary.
emitBytes(StringRef Data)2325ffd83dbSDimitry Andric void emitBytes(StringRef Data) override {
2335ffd83dbSDimitry Andric emitDataMappingSymbol();
2345ffd83dbSDimitry Andric MCELFStreamer::emitBytes(Data);
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric /// This is one of the functions used to emit data into an ELF section, so the
2380b57cec5SDimitry Andric /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
2390b57cec5SDimitry Andric /// if necessary.
emitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)2405ffd83dbSDimitry Andric void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
2415ffd83dbSDimitry Andric emitDataMappingSymbol();
2425ffd83dbSDimitry Andric MCELFStreamer::emitValueImpl(Value, Size, Loc);
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric
emitFill(const MCExpr & NumBytes,uint64_t FillValue,SMLoc Loc)2450b57cec5SDimitry Andric void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
2460b57cec5SDimitry Andric SMLoc Loc) override {
2475ffd83dbSDimitry Andric emitDataMappingSymbol();
2480b57cec5SDimitry Andric MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
2490b57cec5SDimitry Andric }
250*0fca6ea1SDimitry Andric
2510b57cec5SDimitry Andric private:
2520b57cec5SDimitry Andric enum ElfMappingSymbol {
2530b57cec5SDimitry Andric EMS_None,
2540b57cec5SDimitry Andric EMS_A64,
2550b57cec5SDimitry Andric EMS_Data
2560b57cec5SDimitry Andric };
2570b57cec5SDimitry Andric
emitDataMappingSymbol()2585ffd83dbSDimitry Andric void emitDataMappingSymbol() {
2590b57cec5SDimitry Andric if (LastEMS == EMS_Data)
2600b57cec5SDimitry Andric return;
261fe6060f1SDimitry Andric emitMappingSymbol("$d");
2620b57cec5SDimitry Andric LastEMS = EMS_Data;
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric
emitA64MappingSymbol()265fe6060f1SDimitry Andric void emitA64MappingSymbol() {
2660b57cec5SDimitry Andric if (LastEMS == EMS_A64)
2670b57cec5SDimitry Andric return;
268fe6060f1SDimitry Andric emitMappingSymbol("$x");
2690b57cec5SDimitry Andric LastEMS = EMS_A64;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric
emitMappingSymbol(StringRef Name)272fe6060f1SDimitry Andric void emitMappingSymbol(StringRef Name) {
273*0fca6ea1SDimitry Andric auto *Symbol = cast<MCSymbolELF>(getContext().createLocalSymbol(Name));
2745ffd83dbSDimitry Andric emitLabel(Symbol);
2750b57cec5SDimitry Andric Symbol->setType(ELF::STT_NOTYPE);
2760b57cec5SDimitry Andric Symbol->setBinding(ELF::STB_LOCAL);
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
2800b57cec5SDimitry Andric ElfMappingSymbol LastEMS;
2810b57cec5SDimitry Andric };
2820b57cec5SDimitry Andric } // end anonymous namespace
2830b57cec5SDimitry Andric
getStreamer()2840b57cec5SDimitry Andric AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
2850b57cec5SDimitry Andric return static_cast<AArch64ELFStreamer &>(Streamer);
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric
emitInst(uint32_t Inst)2880b57cec5SDimitry Andric void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
2890b57cec5SDimitry Andric getStreamer().emitInst(Inst);
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric
emitDirectiveVariantPCS(MCSymbol * Symbol)292eaeb601bSDimitry Andric void AArch64TargetELFStreamer::emitDirectiveVariantPCS(MCSymbol *Symbol) {
2933a9a9c0cSDimitry Andric getStreamer().getAssembler().registerSymbol(*Symbol);
294eaeb601bSDimitry Andric cast<MCSymbolELF>(Symbol)->setOther(ELF::STO_AARCH64_VARIANT_PCS);
295eaeb601bSDimitry Andric }
296eaeb601bSDimitry Andric
finish()297*0fca6ea1SDimitry Andric void AArch64TargetELFStreamer::finish() {
298*0fca6ea1SDimitry Andric AArch64TargetStreamer::finish();
299*0fca6ea1SDimitry Andric AArch64ELFStreamer &S = getStreamer();
300*0fca6ea1SDimitry Andric MCContext &Ctx = S.getContext();
301*0fca6ea1SDimitry Andric auto &Asm = S.getAssembler();
302*0fca6ea1SDimitry Andric MCSectionELF *MemtagSec = nullptr;
303*0fca6ea1SDimitry Andric for (const MCSymbol &Symbol : Asm.symbols()) {
304*0fca6ea1SDimitry Andric const auto &Sym = cast<MCSymbolELF>(Symbol);
305*0fca6ea1SDimitry Andric if (Sym.isMemtag()) {
306*0fca6ea1SDimitry Andric MemtagSec = Ctx.getELFSection(".memtag.globals.static",
307*0fca6ea1SDimitry Andric ELF::SHT_AARCH64_MEMTAG_GLOBALS_STATIC, 0);
308*0fca6ea1SDimitry Andric break;
309*0fca6ea1SDimitry Andric }
310*0fca6ea1SDimitry Andric }
311*0fca6ea1SDimitry Andric if (!MemtagSec)
312*0fca6ea1SDimitry Andric return;
313*0fca6ea1SDimitry Andric
314*0fca6ea1SDimitry Andric // switchSection registers the section symbol and invalidates symbols(). We
315*0fca6ea1SDimitry Andric // need a separate symbols() loop.
316*0fca6ea1SDimitry Andric S.switchSection(MemtagSec);
317*0fca6ea1SDimitry Andric const auto *Zero = MCConstantExpr::create(0, Ctx);
318*0fca6ea1SDimitry Andric for (const MCSymbol &Symbol : Asm.symbols()) {
319*0fca6ea1SDimitry Andric const auto &Sym = cast<MCSymbolELF>(Symbol);
320*0fca6ea1SDimitry Andric if (!Sym.isMemtag())
321*0fca6ea1SDimitry Andric continue;
322*0fca6ea1SDimitry Andric auto *SRE = MCSymbolRefExpr::create(&Sym, MCSymbolRefExpr::VK_None, Ctx);
323*0fca6ea1SDimitry Andric (void)S.emitRelocDirective(*Zero, "BFD_RELOC_NONE", SRE, SMLoc(),
324*0fca6ea1SDimitry Andric *Ctx.getSubtargetInfo());
325*0fca6ea1SDimitry Andric }
326*0fca6ea1SDimitry Andric }
327*0fca6ea1SDimitry Andric
328fe6060f1SDimitry Andric MCTargetStreamer *
createAArch64AsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter * InstPrint)329fe6060f1SDimitry Andric llvm::createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
330*0fca6ea1SDimitry Andric MCInstPrinter *InstPrint) {
3310b57cec5SDimitry Andric return new AArch64TargetAsmStreamer(S, OS);
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
334*0fca6ea1SDimitry Andric MCELFStreamer *
createAArch64ELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)335*0fca6ea1SDimitry Andric llvm::createAArch64ELFStreamer(MCContext &Context,
336*0fca6ea1SDimitry Andric std::unique_ptr<MCAsmBackend> TAB,
337*0fca6ea1SDimitry Andric std::unique_ptr<MCObjectWriter> OW,
338*0fca6ea1SDimitry Andric std::unique_ptr<MCCodeEmitter> Emitter) {
3390b57cec5SDimitry Andric AArch64ELFStreamer *S = new AArch64ELFStreamer(
3400b57cec5SDimitry Andric Context, std::move(TAB), std::move(OW), std::move(Emitter));
3410b57cec5SDimitry Andric return S;
3420b57cec5SDimitry Andric }
343