1 //===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file assembles .s files and emits AArch64 ELF .o object files. Different
11 // from generic ELF streamer in emitting mapping symbols ($x and $d) to delimit
12 // regions of data and code.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "AArch64TargetStreamer.h"
17 #include "AArch64WinCOFFStreamer.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.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/MCSection.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
40 using namespace llvm;
41
42 namespace {
43
44 class AArch64ELFStreamer;
45
46 class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
47 formatted_raw_ostream &OS;
48
49 void emitInst(uint32_t Inst) override;
50
51 public:
52 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
53 };
54
AArch64TargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS)55 AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
56 formatted_raw_ostream &OS)
57 : AArch64TargetStreamer(S), OS(OS) {}
58
emitInst(uint32_t Inst)59 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
60 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
61 }
62
63 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
64 /// the appropriate points in the object files. These symbols are defined in the
65 /// AArch64 ELF ABI:
66 /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
67 ///
68 /// In brief: $x or $d should be emitted at the start of each contiguous region
69 /// of A64 code or data in a section. In practice, this emission does not rely
70 /// on explicit assembler directives but on inherent properties of the
71 /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
72 /// instruction).
73 ///
74 /// As a result this system is orthogonal to the DataRegion infrastructure used
75 /// by MachO. Beware!
76 class AArch64ELFStreamer : public MCELFStreamer {
77 public:
AArch64ELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)78 AArch64ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
79 std::unique_ptr<MCObjectWriter> OW,
80 std::unique_ptr<MCCodeEmitter> Emitter)
81 : MCELFStreamer(Context, std::move(TAB), std::move(OW),
82 std::move(Emitter)),
83 MappingSymbolCounter(0), LastEMS(EMS_None) {}
84
ChangeSection(MCSection * Section,const MCExpr * Subsection)85 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
86 // We have to keep track of the mapping symbol state of any sections we
87 // use. Each one should start off as EMS_None, which is provided as the
88 // default constructor by DenseMap::lookup.
89 LastMappingSymbols[getPreviousSection().first] = LastEMS;
90 LastEMS = LastMappingSymbols.lookup(Section);
91
92 MCELFStreamer::ChangeSection(Section, Subsection);
93 }
94
95 // Reset state between object emissions
reset()96 void reset() override {
97 MappingSymbolCounter = 0;
98 MCELFStreamer::reset();
99 LastMappingSymbols.clear();
100 LastEMS = EMS_None;
101 }
102
103 /// This function is the one used to emit instruction data into the ELF
104 /// streamer. We override it to add the appropriate mapping symbol if
105 /// necessary.
EmitInstruction(const MCInst & Inst,const MCSubtargetInfo & STI,bool)106 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
107 bool) override {
108 EmitA64MappingSymbol();
109 MCELFStreamer::EmitInstruction(Inst, STI);
110 }
111
112 /// Emit a 32-bit value as an instruction. This is only used for the .inst
113 /// directive, EmitInstruction should be used in other cases.
emitInst(uint32_t Inst)114 void emitInst(uint32_t Inst) {
115 char Buffer[4];
116
117 // We can't just use EmitIntValue here, as that will emit a data mapping
118 // symbol, and swap the endianness on big-endian systems (instructions are
119 // always little-endian).
120 for (unsigned I = 0; I < 4; ++I) {
121 Buffer[I] = uint8_t(Inst);
122 Inst >>= 8;
123 }
124
125 EmitA64MappingSymbol();
126 MCELFStreamer::EmitBytes(StringRef(Buffer, 4));
127 }
128
129 /// This is one of the functions used to emit data into an ELF section, so the
130 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
131 /// if necessary.
EmitBytes(StringRef Data)132 void EmitBytes(StringRef Data) override {
133 EmitDataMappingSymbol();
134 MCELFStreamer::EmitBytes(Data);
135 }
136
137 /// This is one of the functions used to emit data into an ELF section, so the
138 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
139 /// if necessary.
EmitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)140 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
141 EmitDataMappingSymbol();
142 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
143 }
144
emitFill(const MCExpr & NumBytes,uint64_t FillValue,SMLoc Loc)145 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
146 SMLoc Loc) override {
147 EmitDataMappingSymbol();
148 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
149 }
150 private:
151 enum ElfMappingSymbol {
152 EMS_None,
153 EMS_A64,
154 EMS_Data
155 };
156
EmitDataMappingSymbol()157 void EmitDataMappingSymbol() {
158 if (LastEMS == EMS_Data)
159 return;
160 EmitMappingSymbol("$d");
161 LastEMS = EMS_Data;
162 }
163
EmitA64MappingSymbol()164 void EmitA64MappingSymbol() {
165 if (LastEMS == EMS_A64)
166 return;
167 EmitMappingSymbol("$x");
168 LastEMS = EMS_A64;
169 }
170
EmitMappingSymbol(StringRef Name)171 void EmitMappingSymbol(StringRef Name) {
172 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
173 Name + "." + Twine(MappingSymbolCounter++)));
174 EmitLabel(Symbol);
175 Symbol->setType(ELF::STT_NOTYPE);
176 Symbol->setBinding(ELF::STB_LOCAL);
177 Symbol->setExternal(false);
178 }
179
180 int64_t MappingSymbolCounter;
181
182 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
183 ElfMappingSymbol LastEMS;
184 };
185
186 } // end anonymous namespace
187
188 namespace llvm {
189
getStreamer()190 AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
191 return static_cast<AArch64ELFStreamer &>(Streamer);
192 }
193
emitInst(uint32_t Inst)194 void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
195 getStreamer().emitInst(Inst);
196 }
197
createAArch64AsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter * InstPrint,bool isVerboseAsm)198 MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
199 formatted_raw_ostream &OS,
200 MCInstPrinter *InstPrint,
201 bool isVerboseAsm) {
202 return new AArch64TargetAsmStreamer(S, OS);
203 }
204
createAArch64ELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter,bool RelaxAll)205 MCELFStreamer *createAArch64ELFStreamer(MCContext &Context,
206 std::unique_ptr<MCAsmBackend> TAB,
207 std::unique_ptr<MCObjectWriter> OW,
208 std::unique_ptr<MCCodeEmitter> Emitter,
209 bool RelaxAll) {
210 AArch64ELFStreamer *S = new AArch64ELFStreamer(
211 Context, std::move(TAB), std::move(OW), std::move(Emitter));
212 if (RelaxAll)
213 S->getAssembler().setRelaxAll(true);
214 return S;
215 }
216
217 } // end namespace llvm
218