1//===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
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 AArch64 ELF .o object files. Different
10// from generic ELF streamer in emitting mapping symbols ($x and $d) to delimit
11// regions of data and code.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AArch64ELFStreamer.h"
16#include "AArch64MCTargetDesc.h"
17#include "AArch64TargetStreamer.h"
18#include "AArch64WinCOFFStreamer.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/BinaryFormat/ELF.h"
23#include "llvm/MC/MCAsmBackend.h"
24#include "llvm/MC/MCAssembler.h"
25#include "llvm/MC/MCCodeEmitter.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCELFStreamer.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCObjectWriter.h"
31#include "llvm/MC/MCSectionELF.h"
32#include "llvm/MC/MCStreamer.h"
33#include "llvm/MC/MCSubtargetInfo.h"
34#include "llvm/MC/MCSymbolELF.h"
35#include "llvm/MC/MCWinCOFFStreamer.h"
36#include "llvm/Support/Casting.h"
37#include "llvm/Support/FormattedStream.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/TargetParser/Triple.h"
40
41using namespace llvm;
42
43namespace {
44
45class AArch64ELFStreamer;
46
47class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
48 formatted_raw_ostream &OS;
49
50 void emitInst(uint32_t Inst) override;
51
52 void emitDirectiveVariantPCS(MCSymbol *Symbol) override {
53 OS << "\t.variant_pcs\t" << Symbol->getName() << "\n";
54 }
55
56 void emitARM64WinCFIAllocStack(unsigned Size) override {
57 OS << "\t.seh_stackalloc\t" << Size << "\n";
58 }
59 void emitARM64WinCFISaveR19R20X(int Offset) override {
60 OS << "\t.seh_save_r19r20_x\t" << Offset << "\n";
61 }
62 void emitARM64WinCFISaveFPLR(int Offset) override {
63 OS << "\t.seh_save_fplr\t" << Offset << "\n";
64 }
65 void emitARM64WinCFISaveFPLRX(int Offset) override {
66 OS << "\t.seh_save_fplr_x\t" << Offset << "\n";
67 }
68 void emitARM64WinCFISaveReg(unsigned Reg, int Offset) override {
69 OS << "\t.seh_save_reg\tx" << Reg << ", " << Offset << "\n";
70 }
71 void emitARM64WinCFISaveRegX(unsigned Reg, int Offset) override {
72 OS << "\t.seh_save_reg_x\tx" << Reg << ", " << Offset << "\n";
73 }
74 void emitARM64WinCFISaveRegP(unsigned Reg, int Offset) override {
75 OS << "\t.seh_save_regp\tx" << Reg << ", " << Offset << "\n";
76 }
77 void emitARM64WinCFISaveRegPX(unsigned Reg, int Offset) override {
78 OS << "\t.seh_save_regp_x\tx" << Reg << ", " << Offset << "\n";
79 }
80 void emitARM64WinCFISaveLRPair(unsigned Reg, int Offset) override {
81 OS << "\t.seh_save_lrpair\tx" << Reg << ", " << Offset << "\n";
82 }
83 void emitARM64WinCFISaveFReg(unsigned Reg, int Offset) override {
84 OS << "\t.seh_save_freg\td" << Reg << ", " << Offset << "\n";
85 }
86 void emitARM64WinCFISaveFRegX(unsigned Reg, int Offset) override {
87 OS << "\t.seh_save_freg_x\td" << Reg << ", " << Offset << "\n";
88 }
89 void emitARM64WinCFISaveFRegP(unsigned Reg, int Offset) override {
90 OS << "\t.seh_save_fregp\td" << Reg << ", " << Offset << "\n";
91 }
92 void emitARM64WinCFISaveFRegPX(unsigned Reg, int Offset) override {
93 OS << "\t.seh_save_fregp_x\td" << Reg << ", " << Offset << "\n";
94 }
95 void emitARM64WinCFISetFP() override { OS << "\t.seh_set_fp\n"; }
96 void emitARM64WinCFIAddFP(unsigned Size) override {
97 OS << "\t.seh_add_fp\t" << Size << "\n";
98 }
99 void emitARM64WinCFINop() override { OS << "\t.seh_nop\n"; }
100 void emitARM64WinCFISaveNext() override { OS << "\t.seh_save_next\n"; }
101 void emitARM64WinCFIPrologEnd() override { OS << "\t.seh_endprologue\n"; }
102 void emitARM64WinCFIEpilogStart() override { OS << "\t.seh_startepilogue\n"; }
103 void emitARM64WinCFIEpilogEnd() override { OS << "\t.seh_endepilogue\n"; }
104 void emitARM64WinCFITrapFrame() override { OS << "\t.seh_trap_frame\n"; }
105 void emitARM64WinCFIMachineFrame() override { OS << "\t.seh_pushframe\n"; }
106 void emitARM64WinCFIContext() override { OS << "\t.seh_context\n"; }
107 void emitARM64WinCFIECContext() override { OS << "\t.seh_ec_context\n"; }
108 void emitARM64WinCFIClearUnwoundToCall() override {
109 OS << "\t.seh_clear_unwound_to_call\n";
110 }
111 void emitARM64WinCFIPACSignLR() override {
112 OS << "\t.seh_pac_sign_lr\n";
113 }
114
115 void emitARM64WinCFISaveAnyRegI(unsigned Reg, int Offset) override {
116 OS << "\t.seh_save_any_reg\tx" << Reg << ", " << Offset << "\n";
117 }
118 void emitARM64WinCFISaveAnyRegIP(unsigned Reg, int Offset) override {
119 OS << "\t.seh_save_any_reg_p\tx" << Reg << ", " << Offset << "\n";
120 }
121 void emitARM64WinCFISaveAnyRegD(unsigned Reg, int Offset) override {
122 OS << "\t.seh_save_any_reg\td" << Reg << ", " << Offset << "\n";
123 }
124 void emitARM64WinCFISaveAnyRegDP(unsigned Reg, int Offset) override {
125 OS << "\t.seh_save_any_reg_p\td" << Reg << ", " << Offset << "\n";
126 }
127 void emitARM64WinCFISaveAnyRegQ(unsigned Reg, int Offset) override {
128 OS << "\t.seh_save_any_reg\tq" << Reg << ", " << Offset << "\n";
129 }
130 void emitARM64WinCFISaveAnyRegQP(unsigned Reg, int Offset) override {
131 OS << "\t.seh_save_any_reg_p\tq" << Reg << ", " << Offset << "\n";
132 }
133 void emitARM64WinCFISaveAnyRegIX(unsigned Reg, int Offset) override {
134 OS << "\t.seh_save_any_reg_x\tx" << Reg << ", " << Offset << "\n";
135 }
136 void emitARM64WinCFISaveAnyRegIPX(unsigned Reg, int Offset) override {
137 OS << "\t.seh_save_any_reg_px\tx" << Reg << ", " << Offset << "\n";
138 }
139 void emitARM64WinCFISaveAnyRegDX(unsigned Reg, int Offset) override {
140 OS << "\t.seh_save_any_reg_x\td" << Reg << ", " << Offset << "\n";
141 }
142 void emitARM64WinCFISaveAnyRegDPX(unsigned Reg, int Offset) override {
143 OS << "\t.seh_save_any_reg_px\td" << Reg << ", " << Offset << "\n";
144 }
145 void emitARM64WinCFISaveAnyRegQX(unsigned Reg, int Offset) override {
146 OS << "\t.seh_save_any_reg_x\tq" << Reg << ", " << Offset << "\n";
147 }
148 void emitARM64WinCFISaveAnyRegQPX(unsigned Reg, int Offset) override {
149 OS << "\t.seh_save_any_reg_px\tq" << Reg << ", " << Offset << "\n";
150 }
151
152public:
153 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
154};
155
156AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
157 formatted_raw_ostream &OS)
158 : AArch64TargetStreamer(S), OS(OS) {}
159
160void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
161 OS << "\t.inst\t0x" << Twine::utohexstr(Val: Inst) << "\n";
162}
163
164/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
165/// the appropriate points in the object files. These symbols are defined in the
166/// AArch64 ELF ABI:
167/// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
168///
169/// In brief: $x or $d should be emitted at the start of each contiguous region
170/// of A64 code or data in a section. In practice, this emission does not rely
171/// on explicit assembler directives but on inherent properties of the
172/// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
173/// instruction).
174///
175/// As a result this system is orthogonal to the DataRegion infrastructure used
176/// by MachO. Beware!
177class AArch64ELFStreamer : public MCELFStreamer {
178public:
179 AArch64ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
180 std::unique_ptr<MCObjectWriter> OW,
181 std::unique_ptr<MCCodeEmitter> Emitter)
182 : MCELFStreamer(Context, std::move(TAB), std::move(OW),
183 std::move(Emitter)),
184 LastEMS(EMS_None) {}
185
186 void changeSection(MCSection *Section, uint32_t Subsection = 0) override {
187 // We have to keep track of the mapping symbol state of any sections we
188 // use. Each one should start off as EMS_None, which is provided as the
189 // default constructor by DenseMap::lookup.
190 LastMappingSymbols[getCurrentSection().first] = LastEMS;
191 LastEMS = LastMappingSymbols.lookup(Val: Section);
192
193 MCELFStreamer::changeSection(Section, Subsection);
194 }
195
196 // Reset state between object emissions
197 void reset() override {
198 MCELFStreamer::reset();
199 LastMappingSymbols.clear();
200 LastEMS = EMS_None;
201 }
202
203 /// This function is the one used to emit instruction data into the ELF
204 /// streamer. We override it to add the appropriate mapping symbol if
205 /// necessary.
206 void emitInstruction(const MCInst &Inst,
207 const MCSubtargetInfo &STI) override {
208 emitA64MappingSymbol();
209 MCELFStreamer::emitInstruction(Inst, STI);
210 }
211
212 /// Emit a 32-bit value as an instruction. This is only used for the .inst
213 /// directive, EmitInstruction should be used in other cases.
214 void emitInst(uint32_t Inst) {
215 char Buffer[4];
216
217 // We can't just use EmitIntValue here, as that will emit a data mapping
218 // symbol, and swap the endianness on big-endian systems (instructions are
219 // always little-endian).
220 for (char &C : Buffer) {
221 C = uint8_t(Inst);
222 Inst >>= 8;
223 }
224
225 emitA64MappingSymbol();
226 MCELFStreamer::emitBytes(Data: StringRef(Buffer, 4));
227 }
228
229 /// This is one of the functions used to emit data into an ELF section, so the
230 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
231 /// if necessary.
232 void emitBytes(StringRef Data) override {
233 emitDataMappingSymbol();
234 MCELFStreamer::emitBytes(Data);
235 }
236
237 /// This is one of the functions used to emit data into an ELF section, so the
238 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
239 /// if necessary.
240 void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
241 emitDataMappingSymbol();
242 MCELFStreamer::emitValueImpl(Value, Size, Loc);
243 }
244
245 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
246 SMLoc Loc) override {
247 emitDataMappingSymbol();
248 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
249 }
250
251private:
252 enum ElfMappingSymbol {
253 EMS_None,
254 EMS_A64,
255 EMS_Data
256 };
257
258 void emitDataMappingSymbol() {
259 if (LastEMS == EMS_Data)
260 return;
261 emitMappingSymbol(Name: "$d");
262 LastEMS = EMS_Data;
263 }
264
265 void emitA64MappingSymbol() {
266 if (LastEMS == EMS_A64)
267 return;
268 emitMappingSymbol(Name: "$x");
269 LastEMS = EMS_A64;
270 }
271
272 void emitMappingSymbol(StringRef Name) {
273 auto *Symbol = cast<MCSymbolELF>(Val: getContext().createLocalSymbol(Name));
274 emitLabel(Symbol);
275 Symbol->setType(ELF::STT_NOTYPE);
276 Symbol->setBinding(ELF::STB_LOCAL);
277 }
278
279 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
280 ElfMappingSymbol LastEMS;
281};
282} // end anonymous namespace
283
284AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
285 return static_cast<AArch64ELFStreamer &>(Streamer);
286}
287
288void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
289 getStreamer().emitInst(Inst);
290}
291
292void AArch64TargetELFStreamer::emitDirectiveVariantPCS(MCSymbol *Symbol) {
293 getStreamer().getAssembler().registerSymbol(Symbol: *Symbol);
294 cast<MCSymbolELF>(Val: Symbol)->setOther(ELF::STO_AARCH64_VARIANT_PCS);
295}
296
297void AArch64TargetELFStreamer::finish() {
298 AArch64TargetStreamer::finish();
299 AArch64ELFStreamer &S = getStreamer();
300 MCContext &Ctx = S.getContext();
301 auto &Asm = S.getAssembler();
302 MCSectionELF *MemtagSec = nullptr;
303 for (const MCSymbol &Symbol : Asm.symbols()) {
304 const auto &Sym = cast<MCSymbolELF>(Val: Symbol);
305 if (Sym.isMemtag()) {
306 MemtagSec = Ctx.getELFSection(Section: ".memtag.globals.static",
307 Type: ELF::SHT_AARCH64_MEMTAG_GLOBALS_STATIC, Flags: 0);
308 break;
309 }
310 }
311 if (!MemtagSec)
312 return;
313
314 // switchSection registers the section symbol and invalidates symbols(). We
315 // need a separate symbols() loop.
316 S.switchSection(Section: MemtagSec);
317 const auto *Zero = MCConstantExpr::create(Value: 0, Ctx);
318 for (const MCSymbol &Symbol : Asm.symbols()) {
319 const auto &Sym = cast<MCSymbolELF>(Val: Symbol);
320 if (!Sym.isMemtag())
321 continue;
322 auto *SRE = MCSymbolRefExpr::create(Symbol: &Sym, Kind: MCSymbolRefExpr::VK_None, Ctx);
323 (void)S.emitRelocDirective(Offset: *Zero, Name: "BFD_RELOC_NONE", Expr: SRE, Loc: SMLoc(),
324 STI: *Ctx.getSubtargetInfo());
325 }
326}
327
328MCTargetStreamer *
329llvm::createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
330 MCInstPrinter *InstPrint) {
331 return new AArch64TargetAsmStreamer(S, OS);
332}
333
334MCELFStreamer *
335llvm::createAArch64ELFStreamer(MCContext &Context,
336 std::unique_ptr<MCAsmBackend> TAB,
337 std::unique_ptr<MCObjectWriter> OW,
338 std::unique_ptr<MCCodeEmitter> Emitter) {
339 AArch64ELFStreamer *S = new AArch64ELFStreamer(
340 Context, std::move(TAB), std::move(OW), std::move(Emitter));
341 return S;
342}
343