blob: 473ffbe6bce232dfa96e948082ffd45927b17599 [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===- 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 "llvm/MC/MCELFStreamer.h"
17#include "llvm/ADT/SmallPtrSet.h"
Chad Rosierdcd2a302014-10-22 20:35:57 +000018#include "llvm/ADT/StringExtras.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000019#include "llvm/ADT/Twine.h"
20#include "llvm/MC/MCAsmBackend.h"
Chad Rosierdcd2a302014-10-22 20:35:57 +000021#include "llvm/MC/MCAsmInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000022#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCCodeEmitter.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCELF.h"
26#include "llvm/MC/MCELFStreamer.h"
27#include "llvm/MC/MCELFSymbolFlags.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCObjectStreamer.h"
31#include "llvm/MC/MCSection.h"
32#include "llvm/MC/MCSectionELF.h"
33#include "llvm/MC/MCStreamer.h"
34#include "llvm/MC/MCSymbol.h"
35#include "llvm/MC/MCValue.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/ELF.h"
38#include "llvm/Support/ErrorHandling.h"
Chad Rosierdcd2a302014-10-22 20:35:57 +000039#include "llvm/Support/FormattedStream.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000040#include "llvm/Support/raw_ostream.h"
41
42using namespace llvm;
43
44namespace {
45
Chad Rosierdcd2a302014-10-22 20:35:57 +000046class AArch64ELFStreamer;
47
48class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
49 formatted_raw_ostream &OS;
50
51 void emitInst(uint32_t Inst) override;
52
53public:
54 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
55};
56
57AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
58 formatted_raw_ostream &OS)
59 : AArch64TargetStreamer(S), OS(OS) {}
60
61void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
62 OS << "\t.inst\t0x" << utohexstr(Inst) << "\n";
63}
64
65class AArch64TargetELFStreamer : public AArch64TargetStreamer {
66private:
67 AArch64ELFStreamer &getStreamer();
68
69 void emitInst(uint32_t Inst) override;
70
71public:
72 AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
73};
74
Tim Northover3b0846e2014-05-24 12:50:23 +000075/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
76/// the appropriate points in the object files. These symbols are defined in the
77/// AArch64 ELF ABI:
78/// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
79///
80/// In brief: $x or $d should be emitted at the start of each contiguous region
81/// of A64 code or data in a section. In practice, this emission does not rely
82/// on explicit assembler directives but on inherent properties of the
83/// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
84/// instruction).
85///
86/// As a result this system is orthogonal to the DataRegion infrastructure used
87/// by MachO. Beware!
88class AArch64ELFStreamer : public MCELFStreamer {
89public:
Chad Rosierdcd2a302014-10-22 20:35:57 +000090 friend class AArch64TargetELFStreamer;
91
Rafael Espindola5560a4c2015-04-14 22:14:34 +000092 AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
93 raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
Tim Northover3b0846e2014-05-24 12:50:23 +000094 : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
95 LastEMS(EMS_None) {}
96
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000097 ~AArch64ELFStreamer() override {}
Tim Northover3b0846e2014-05-24 12:50:23 +000098
Rafael Espindola0709a7b2015-05-21 19:20:38 +000099 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover3b0846e2014-05-24 12:50:23 +0000100 // We have to keep track of the mapping symbol state of any sections we
101 // use. Each one should start off as EMS_None, which is provided as the
102 // default constructor by DenseMap::lookup.
103 LastMappingSymbols[getPreviousSection().first] = LastEMS;
104 LastEMS = LastMappingSymbols.lookup(Section);
105
106 MCELFStreamer::ChangeSection(Section, Subsection);
107 }
108
109 /// This function is the one used to emit instruction data into the ELF
110 /// streamer. We override it to add the appropriate mapping symbol if
111 /// necessary.
112 void EmitInstruction(const MCInst &Inst,
113 const MCSubtargetInfo &STI) override {
114 EmitA64MappingSymbol();
115 MCELFStreamer::EmitInstruction(Inst, STI);
116 }
117
Chad Rosierdcd2a302014-10-22 20:35:57 +0000118 void emitInst(uint32_t Inst) {
119 char Buffer[4];
120 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
121
122 EmitA64MappingSymbol();
123 for (unsigned II = 0; II != 4; ++II) {
124 const unsigned I = LittleEndian ? (4 - II - 1) : II;
125 Buffer[4 - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
126 }
127 MCELFStreamer::EmitBytes(StringRef(Buffer, 4));
128 }
129
Tim Northover3b0846e2014-05-24 12:50:23 +0000130 /// 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)
132 /// if necessary.
133 void EmitBytes(StringRef Data) override {
134 EmitDataMappingSymbol();
135 MCELFStreamer::EmitBytes(Data);
136 }
137
138 /// This is one of the functions used to emit data into an ELF section, so the
139 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
140 /// if necessary.
141 void EmitValueImpl(const MCExpr *Value, unsigned Size,
142 const SMLoc &Loc) override {
143 EmitDataMappingSymbol();
144 MCELFStreamer::EmitValueImpl(Value, Size);
145 }
146
147private:
148 enum ElfMappingSymbol {
149 EMS_None,
150 EMS_A64,
151 EMS_Data
152 };
153
154 void EmitDataMappingSymbol() {
155 if (LastEMS == EMS_Data)
156 return;
157 EmitMappingSymbol("$d");
158 LastEMS = EMS_Data;
159 }
160
161 void EmitA64MappingSymbol() {
162 if (LastEMS == EMS_A64)
163 return;
164 EmitMappingSymbol("$x");
165 LastEMS = EMS_A64;
166 }
167
168 void EmitMappingSymbol(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000169 MCSymbol *Start = getContext().createTempSymbol();
Tim Northover3b0846e2014-05-24 12:50:23 +0000170 EmitLabel(Start);
171
Jim Grosbach6f482002015-05-18 18:43:14 +0000172 MCSymbol *Symbol = getContext().getOrCreateSymbol(
Tim Northover3b0846e2014-05-24 12:50:23 +0000173 Name + "." + Twine(MappingSymbolCounter++));
174
175 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
176 MCELF::SetType(SD, ELF::STT_NOTYPE);
177 MCELF::SetBinding(SD, ELF::STB_LOCAL);
178 SD.setExternal(false);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000179 auto Sec = getCurrentSection().first;
180 assert(Sec && "need a section");
181 Symbol->setSection(*Sec);
Tim Northover3b0846e2014-05-24 12:50:23 +0000182
183 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
184 Symbol->setVariableValue(Value);
185 }
186
187 int64_t MappingSymbolCounter;
188
189 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
190 ElfMappingSymbol LastEMS;
191
192 /// @}
193};
Chad Rosierdcd2a302014-10-22 20:35:57 +0000194} // end anonymous namespace
195
196AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
197 return static_cast<AArch64ELFStreamer &>(Streamer);
198}
199
200void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
201 getStreamer().emitInst(Inst);
Tim Northover3b0846e2014-05-24 12:50:23 +0000202}
203
204namespace llvm {
Rafael Espindola73870dd2015-03-16 21:43:42 +0000205MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
206 formatted_raw_ostream &OS,
207 MCInstPrinter *InstPrint,
208 bool isVerboseAsm) {
209 return new AArch64TargetAsmStreamer(S, OS);
Chad Rosierdcd2a302014-10-22 20:35:57 +0000210}
211
Tim Northover3b0846e2014-05-24 12:50:23 +0000212MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000213 raw_pwrite_stream &OS,
214 MCCodeEmitter *Emitter, bool RelaxAll) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000215 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
216 if (RelaxAll)
217 S->getAssembler().setRelaxAll(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000218 return S;
219}
Rafael Espindolacd584a82015-03-19 01:50:16 +0000220
221MCTargetStreamer *
222createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
223 Triple TT(STI.getTargetTriple());
224 if (TT.getObjectFormat() == Triple::ELF)
225 return new AArch64TargetELFStreamer(S);
226 return nullptr;
227}
Tim Northover3b0846e2014-05-24 12:50:23 +0000228}