1 //===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "AArch64TargetStreamer.h"
17 #include "llvm/MC/MCELFStreamer.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCAssembler.h"
24 #include "llvm/MC/MCCodeEmitter.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELF.h"
27 #include "llvm/MC/MCELFStreamer.h"
28 #include "llvm/MC/MCELFSymbolFlags.h"
29 #include "llvm/MC/MCExpr.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCObjectStreamer.h"
32 #include "llvm/MC/MCSection.h"
33 #include "llvm/MC/MCSectionELF.h"
34 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MCSymbol.h"
36 #include "llvm/MC/MCValue.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ELF.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/raw_ostream.h"
47 class AArch64ELFStreamer;
49 class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
50 formatted_raw_ostream &OS;
52 void emitInst(uint32_t Inst) override;
55 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
58 AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
59 formatted_raw_ostream &OS)
60 : AArch64TargetStreamer(S), OS(OS) {}
62 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
63 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
66 class AArch64TargetELFStreamer : public AArch64TargetStreamer {
68 AArch64ELFStreamer &getStreamer();
70 void emitInst(uint32_t Inst) override;
73 AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
76 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
77 /// the appropriate points in the object files. These symbols are defined in the
79 /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
81 /// In brief: $x or $d should be emitted at the start of each contiguous region
82 /// of A64 code or data in a section. In practice, this emission does not rely
83 /// on explicit assembler directives but on inherent properties of the
84 /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
87 /// As a result this system is orthogonal to the DataRegion infrastructure used
89 class AArch64ELFStreamer : public MCELFStreamer {
91 friend class AArch64TargetELFStreamer;
93 AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
94 raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
95 : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
98 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
99 // We have to keep track of the mapping symbol state of any sections we
100 // use. Each one should start off as EMS_None, which is provided as the
101 // default constructor by DenseMap::lookup.
102 LastMappingSymbols[getPreviousSection().first] = LastEMS;
103 LastEMS = LastMappingSymbols.lookup(Section);
105 MCELFStreamer::ChangeSection(Section, Subsection);
108 /// This function is the one used to emit instruction data into the ELF
109 /// streamer. We override it to add the appropriate mapping symbol if
111 void EmitInstruction(const MCInst &Inst,
112 const MCSubtargetInfo &STI) override {
113 EmitA64MappingSymbol();
114 MCELFStreamer::EmitInstruction(Inst, STI);
117 void emitInst(uint32_t Inst) {
118 EmitA64MappingSymbol();
119 MCELFStreamer::EmitIntValue(Inst, 4);
122 /// This is one of the functions used to emit data into an ELF section, so the
123 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
125 void EmitBytes(StringRef Data) override {
126 EmitDataMappingSymbol();
127 MCELFStreamer::EmitBytes(Data);
130 /// This is one of the functions used to emit data into an ELF section, so the
131 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
133 void EmitValueImpl(const MCExpr *Value, unsigned Size,
134 const SMLoc &Loc) override {
135 EmitDataMappingSymbol();
136 MCELFStreamer::EmitValueImpl(Value, Size);
140 enum ElfMappingSymbol {
146 void EmitDataMappingSymbol() {
147 if (LastEMS == EMS_Data)
149 EmitMappingSymbol("$d");
153 void EmitA64MappingSymbol() {
154 if (LastEMS == EMS_A64)
156 EmitMappingSymbol("$x");
160 void EmitMappingSymbol(StringRef Name) {
161 MCSymbol *Start = getContext().createTempSymbol();
164 MCSymbol *Symbol = getContext().getOrCreateSymbol(
165 Name + "." + Twine(MappingSymbolCounter++));
167 getAssembler().registerSymbol(*Symbol);
168 MCSymbolData &SD = Symbol->getData();
169 MCELF::SetType(*Symbol, ELF::STT_NOTYPE);
170 MCELF::SetBinding(*Symbol, ELF::STB_LOCAL);
171 SD.setExternal(false);
172 auto Sec = getCurrentSection().first;
173 assert(Sec && "need a section");
174 Symbol->setSection(*Sec);
176 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
177 Symbol->setVariableValue(Value);
180 int64_t MappingSymbolCounter;
182 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
183 ElfMappingSymbol LastEMS;
185 } // end anonymous namespace
187 AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
188 return static_cast<AArch64ELFStreamer &>(Streamer);
191 void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
192 getStreamer().emitInst(Inst);
196 MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
197 formatted_raw_ostream &OS,
198 MCInstPrinter *InstPrint,
200 return new AArch64TargetAsmStreamer(S, OS);
203 MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
204 raw_pwrite_stream &OS,
205 MCCodeEmitter *Emitter, bool RelaxAll) {
206 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
208 S->getAssembler().setRelaxAll(true);
213 createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
214 Triple TT(STI.getTargetTriple());
215 if (TT.getObjectFormat() == Triple::ELF)
216 return new AArch64TargetELFStreamer(S);