blob: 1434018fdbe57bb1fc6578eeaffc7db80c07ddfb [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
Benjamin Kramer1d1b9242015-05-23 16:15:10 +000016#include "AArch64TargetStreamer.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000017#include "llvm/MC/MCELFStreamer.h"
18#include "llvm/ADT/SmallPtrSet.h"
Chad Rosierdcd2a302014-10-22 20:35:57 +000019#include "llvm/ADT/StringExtras.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000020#include "llvm/ADT/Twine.h"
21#include "llvm/MC/MCAsmBackend.h"
Chad Rosierdcd2a302014-10-22 20:35:57 +000022#include "llvm/MC/MCAsmInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000023#include "llvm/MC/MCAssembler.h"
24#include "llvm/MC/MCCodeEmitter.h"
25#include "llvm/MC/MCContext.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000026#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"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000034#include "llvm/MC/MCSymbolELF.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000035#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) {
Benjamin Kramerbe48c402015-05-23 16:39:10 +000062 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
Chad Rosierdcd2a302014-10-22 20:35:57 +000063}
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
Rafael Espindola0709a7b2015-05-21 19:20:38 +000097 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover3b0846e2014-05-24 12:50:23 +000098 // We have to keep track of the mapping symbol state of any sections we
99 // use. Each one should start off as EMS_None, which is provided as the
100 // default constructor by DenseMap::lookup.
101 LastMappingSymbols[getPreviousSection().first] = LastEMS;
102 LastEMS = LastMappingSymbols.lookup(Section);
103
104 MCELFStreamer::ChangeSection(Section, Subsection);
105 }
106
107 /// This function is the one used to emit instruction data into the ELF
108 /// streamer. We override it to add the appropriate mapping symbol if
109 /// necessary.
110 void EmitInstruction(const MCInst &Inst,
111 const MCSubtargetInfo &STI) override {
112 EmitA64MappingSymbol();
113 MCELFStreamer::EmitInstruction(Inst, STI);
114 }
115
Chad Rosierdcd2a302014-10-22 20:35:57 +0000116 void emitInst(uint32_t Inst) {
Chad Rosierdcd2a302014-10-22 20:35:57 +0000117 EmitA64MappingSymbol();
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000118 MCELFStreamer::EmitIntValue(Inst, 4);
Chad Rosierdcd2a302014-10-22 20:35:57 +0000119 }
120
Tim Northover3b0846e2014-05-24 12:50:23 +0000121 /// This is one of the functions used to emit data into an ELF section, so the
122 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
123 /// if necessary.
124 void EmitBytes(StringRef Data) override {
125 EmitDataMappingSymbol();
126 MCELFStreamer::EmitBytes(Data);
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.
132 void EmitValueImpl(const MCExpr *Value, unsigned Size,
133 const SMLoc &Loc) override {
134 EmitDataMappingSymbol();
135 MCELFStreamer::EmitValueImpl(Value, Size);
136 }
137
138private:
139 enum ElfMappingSymbol {
140 EMS_None,
141 EMS_A64,
142 EMS_Data
143 };
144
145 void EmitDataMappingSymbol() {
146 if (LastEMS == EMS_Data)
147 return;
148 EmitMappingSymbol("$d");
149 LastEMS = EMS_Data;
150 }
151
152 void EmitA64MappingSymbol() {
153 if (LastEMS == EMS_A64)
154 return;
155 EmitMappingSymbol("$x");
156 LastEMS = EMS_A64;
157 }
158
159 void EmitMappingSymbol(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000160 MCSymbol *Start = getContext().createTempSymbol();
Tim Northover3b0846e2014-05-24 12:50:23 +0000161 EmitLabel(Start);
162
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000163 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
164 Name + "." + Twine(MappingSymbolCounter++)));
Tim Northover3b0846e2014-05-24 12:50:23 +0000165
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000166 getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000167 Symbol->setType(ELF::STT_NOTYPE);
168 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000169 Symbol->setExternal(false);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000170 auto Sec = getCurrentSection().first;
171 assert(Sec && "need a section");
172 Symbol->setSection(*Sec);
Tim Northover3b0846e2014-05-24 12:50:23 +0000173
Jim Grosbach13760bd2015-05-30 01:25:56 +0000174 const MCExpr *Value = MCSymbolRefExpr::create(Start, getContext());
Tim Northover3b0846e2014-05-24 12:50:23 +0000175 Symbol->setVariableValue(Value);
176 }
177
178 int64_t MappingSymbolCounter;
179
180 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
181 ElfMappingSymbol LastEMS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000182};
Chad Rosierdcd2a302014-10-22 20:35:57 +0000183} // end anonymous namespace
184
185AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
186 return static_cast<AArch64ELFStreamer &>(Streamer);
187}
188
189void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
190 getStreamer().emitInst(Inst);
Tim Northover3b0846e2014-05-24 12:50:23 +0000191}
192
193namespace llvm {
Rafael Espindola73870dd2015-03-16 21:43:42 +0000194MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
195 formatted_raw_ostream &OS,
196 MCInstPrinter *InstPrint,
197 bool isVerboseAsm) {
198 return new AArch64TargetAsmStreamer(S, OS);
Chad Rosierdcd2a302014-10-22 20:35:57 +0000199}
200
Tim Northover3b0846e2014-05-24 12:50:23 +0000201MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000202 raw_pwrite_stream &OS,
203 MCCodeEmitter *Emitter, bool RelaxAll) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000204 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
205 if (RelaxAll)
206 S->getAssembler().setRelaxAll(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000207 return S;
208}
Rafael Espindolacd584a82015-03-19 01:50:16 +0000209
210MCTargetStreamer *
211createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
212 Triple TT(STI.getTargetTriple());
213 if (TT.getObjectFormat() == Triple::ELF)
214 return new AArch64TargetELFStreamer(S);
215 return nullptr;
216}
Tim Northover3b0846e2014-05-24 12:50:23 +0000217}