1 //===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "AArch64TargetStreamer.h"
16 #include "AArch64WinCOFFStreamer.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/MC/MCAsmBackend.h"
23 #include "llvm/MC/MCAssembler.h"
24 #include "llvm/MC/MCCodeEmitter.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELFStreamer.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCObjectWriter.h"
30 #include "llvm/MC/MCSection.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSubtargetInfo.h"
33 #include "llvm/MC/MCSymbolELF.h"
34 #include "llvm/MC/MCWinCOFFStreamer.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/raw_ostream.h"
43 class AArch64ELFStreamer
;
45 class AArch64TargetAsmStreamer
: public AArch64TargetStreamer
{
46 formatted_raw_ostream
&OS
;
48 void emitInst(uint32_t Inst
) override
;
51 AArch64TargetAsmStreamer(MCStreamer
&S
, formatted_raw_ostream
&OS
);
54 AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer
&S
,
55 formatted_raw_ostream
&OS
)
56 : AArch64TargetStreamer(S
), OS(OS
) {}
58 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst
) {
59 OS
<< "\t.inst\t0x" << Twine::utohexstr(Inst
) << "\n";
62 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
63 /// the appropriate points in the object files. These symbols are defined in the
65 /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
67 /// In brief: $x or $d should be emitted at the start of each contiguous region
68 /// of A64 code or data in a section. In practice, this emission does not rely
69 /// on explicit assembler directives but on inherent properties of the
70 /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
73 /// As a result this system is orthogonal to the DataRegion infrastructure used
75 class AArch64ELFStreamer
: public MCELFStreamer
{
77 AArch64ELFStreamer(MCContext
&Context
, std::unique_ptr
<MCAsmBackend
> TAB
,
78 std::unique_ptr
<MCObjectWriter
> OW
,
79 std::unique_ptr
<MCCodeEmitter
> Emitter
)
80 : MCELFStreamer(Context
, std::move(TAB
), std::move(OW
),
82 MappingSymbolCounter(0), LastEMS(EMS_None
) {}
84 void ChangeSection(MCSection
*Section
, const MCExpr
*Subsection
) override
{
85 // We have to keep track of the mapping symbol state of any sections we
86 // use. Each one should start off as EMS_None, which is provided as the
87 // default constructor by DenseMap::lookup.
88 LastMappingSymbols
[getPreviousSection().first
] = LastEMS
;
89 LastEMS
= LastMappingSymbols
.lookup(Section
);
91 MCELFStreamer::ChangeSection(Section
, Subsection
);
94 // Reset state between object emissions
95 void reset() override
{
96 MappingSymbolCounter
= 0;
97 MCELFStreamer::reset();
98 LastMappingSymbols
.clear();
102 /// This function is the one used to emit instruction data into the ELF
103 /// streamer. We override it to add the appropriate mapping symbol if
105 void EmitInstruction(const MCInst
&Inst
,
106 const MCSubtargetInfo
&STI
) override
{
107 EmitA64MappingSymbol();
108 MCELFStreamer::EmitInstruction(Inst
, STI
);
111 /// Emit a 32-bit value as an instruction. This is only used for the .inst
112 /// directive, EmitInstruction should be used in other cases.
113 void emitInst(uint32_t Inst
) {
116 // We can't just use EmitIntValue here, as that will emit a data mapping
117 // symbol, and swap the endianness on big-endian systems (instructions are
118 // always little-endian).
119 for (unsigned I
= 0; I
< 4; ++I
) {
120 Buffer
[I
] = uint8_t(Inst
);
124 EmitA64MappingSymbol();
125 MCELFStreamer::EmitBytes(StringRef(Buffer
, 4));
128 /// This is one of the functions used to emit data into an ELF section, so the
129 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
131 void EmitBytes(StringRef Data
) override
{
132 EmitDataMappingSymbol();
133 MCELFStreamer::EmitBytes(Data
);
136 /// This is one of the functions used to emit data into an ELF section, so the
137 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
139 void EmitValueImpl(const MCExpr
*Value
, unsigned Size
, SMLoc Loc
) override
{
140 EmitDataMappingSymbol();
141 MCELFStreamer::EmitValueImpl(Value
, Size
, Loc
);
144 void emitFill(const MCExpr
&NumBytes
, uint64_t FillValue
,
145 SMLoc Loc
) override
{
146 EmitDataMappingSymbol();
147 MCObjectStreamer::emitFill(NumBytes
, FillValue
, Loc
);
150 enum ElfMappingSymbol
{
156 void EmitDataMappingSymbol() {
157 if (LastEMS
== EMS_Data
)
159 EmitMappingSymbol("$d");
163 void EmitA64MappingSymbol() {
164 if (LastEMS
== EMS_A64
)
166 EmitMappingSymbol("$x");
170 void EmitMappingSymbol(StringRef Name
) {
171 auto *Symbol
= cast
<MCSymbolELF
>(getContext().getOrCreateSymbol(
172 Name
+ "." + Twine(MappingSymbolCounter
++)));
174 Symbol
->setType(ELF::STT_NOTYPE
);
175 Symbol
->setBinding(ELF::STB_LOCAL
);
176 Symbol
->setExternal(false);
179 int64_t MappingSymbolCounter
;
181 DenseMap
<const MCSection
*, ElfMappingSymbol
> LastMappingSymbols
;
182 ElfMappingSymbol LastEMS
;
185 } // end anonymous namespace
189 AArch64ELFStreamer
&AArch64TargetELFStreamer::getStreamer() {
190 return static_cast<AArch64ELFStreamer
&>(Streamer
);
193 void AArch64TargetELFStreamer::emitInst(uint32_t Inst
) {
194 getStreamer().emitInst(Inst
);
197 MCTargetStreamer
*createAArch64AsmTargetStreamer(MCStreamer
&S
,
198 formatted_raw_ostream
&OS
,
199 MCInstPrinter
*InstPrint
,
201 return new AArch64TargetAsmStreamer(S
, OS
);
204 MCELFStreamer
*createAArch64ELFStreamer(MCContext
&Context
,
205 std::unique_ptr
<MCAsmBackend
> TAB
,
206 std::unique_ptr
<MCObjectWriter
> OW
,
207 std::unique_ptr
<MCCodeEmitter
> Emitter
,
209 AArch64ELFStreamer
*S
= new AArch64ELFStreamer(
210 Context
, std::move(TAB
), std::move(OW
), std::move(Emitter
));
212 S
->getAssembler().setRelaxAll(true);
216 } // end namespace llvm