blob: 094f90d0fec4956b911c02d9aaff95a868606bbd [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"
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"
Chad Rosierdcd2a302014-10-22 20:35:57 +000040#include "llvm/Support/FormattedStream.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000041#include "llvm/Support/raw_ostream.h"
42
43using namespace llvm;
44
45namespace {
46
Chad Rosierdcd2a302014-10-22 20:35:57 +000047class AArch64ELFStreamer;
48
49class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
50 formatted_raw_ostream &OS;
51
52 void emitInst(uint32_t Inst) override;
53
54public:
55 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
56};
57
58AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
59 formatted_raw_ostream &OS)
60 : AArch64TargetStreamer(S), OS(OS) {}
61
62void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
Benjamin Kramerbe48c402015-05-23 16:39:10 +000063 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
Chad Rosierdcd2a302014-10-22 20:35:57 +000064}
65
66class AArch64TargetELFStreamer : public AArch64TargetStreamer {
67private:
68 AArch64ELFStreamer &getStreamer();
69
70 void emitInst(uint32_t Inst) override;
71
72public:
73 AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
74};
75
Tim Northover3b0846e2014-05-24 12:50:23 +000076/// 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
78/// AArch64 ELF ABI:
79/// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
80///
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
85/// instruction).
86///
87/// As a result this system is orthogonal to the DataRegion infrastructure used
88/// by MachO. Beware!
89class AArch64ELFStreamer : public MCELFStreamer {
90public:
Chad Rosierdcd2a302014-10-22 20:35:57 +000091 friend class AArch64TargetELFStreamer;
92
Rafael Espindola5560a4c2015-04-14 22:14:34 +000093 AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
94 raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
Tim Northover3b0846e2014-05-24 12:50:23 +000095 : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
96 LastEMS(EMS_None) {}
97
Rafael Espindola0709a7b2015-05-21 19:20:38 +000098 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover3b0846e2014-05-24 12:50:23 +000099 // 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);
104
105 MCELFStreamer::ChangeSection(Section, Subsection);
106 }
107
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
110 /// necessary.
111 void EmitInstruction(const MCInst &Inst,
112 const MCSubtargetInfo &STI) override {
113 EmitA64MappingSymbol();
114 MCELFStreamer::EmitInstruction(Inst, STI);
115 }
116
Chad Rosierdcd2a302014-10-22 20:35:57 +0000117 void emitInst(uint32_t Inst) {
Chad Rosierdcd2a302014-10-22 20:35:57 +0000118 EmitA64MappingSymbol();
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000119 MCELFStreamer::EmitIntValue(Inst, 4);
Chad Rosierdcd2a302014-10-22 20:35:57 +0000120 }
121
Tim Northover3b0846e2014-05-24 12:50:23 +0000122 /// 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)
124 /// if necessary.
125 void EmitBytes(StringRef Data) override {
126 EmitDataMappingSymbol();
127 MCELFStreamer::EmitBytes(Data);
128 }
129
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)
132 /// if necessary.
133 void EmitValueImpl(const MCExpr *Value, unsigned Size,
134 const SMLoc &Loc) override {
135 EmitDataMappingSymbol();
136 MCELFStreamer::EmitValueImpl(Value, Size);
137 }
138
139private:
140 enum ElfMappingSymbol {
141 EMS_None,
142 EMS_A64,
143 EMS_Data
144 };
145
146 void EmitDataMappingSymbol() {
147 if (LastEMS == EMS_Data)
148 return;
149 EmitMappingSymbol("$d");
150 LastEMS = EMS_Data;
151 }
152
153 void EmitA64MappingSymbol() {
154 if (LastEMS == EMS_A64)
155 return;
156 EmitMappingSymbol("$x");
157 LastEMS = EMS_A64;
158 }
159
160 void EmitMappingSymbol(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000161 MCSymbol *Start = getContext().createTempSymbol();
Tim Northover3b0846e2014-05-24 12:50:23 +0000162 EmitLabel(Start);
163
Jim Grosbach6f482002015-05-18 18:43:14 +0000164 MCSymbol *Symbol = getContext().getOrCreateSymbol(
Tim Northover3b0846e2014-05-24 12:50:23 +0000165 Name + "." + Twine(MappingSymbolCounter++));
166
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000167 getAssembler().registerSymbol(*Symbol);
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000168 MCELF::SetType(*Symbol, ELF::STT_NOTYPE);
169 MCELF::SetBinding(*Symbol, ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000170 Symbol->setExternal(false);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000171 auto Sec = getCurrentSection().first;
172 assert(Sec && "need a section");
173 Symbol->setSection(*Sec);
Tim Northover3b0846e2014-05-24 12:50:23 +0000174
Jim Grosbach13760bd2015-05-30 01:25:56 +0000175 const MCExpr *Value = MCSymbolRefExpr::create(Start, getContext());
Tim Northover3b0846e2014-05-24 12:50:23 +0000176 Symbol->setVariableValue(Value);
177 }
178
179 int64_t MappingSymbolCounter;
180
181 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
182 ElfMappingSymbol LastEMS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000183};
Chad Rosierdcd2a302014-10-22 20:35:57 +0000184} // end anonymous namespace
185
186AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
187 return static_cast<AArch64ELFStreamer &>(Streamer);
188}
189
190void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
191 getStreamer().emitInst(Inst);
Tim Northover3b0846e2014-05-24 12:50:23 +0000192}
193
194namespace llvm {
Rafael Espindola73870dd2015-03-16 21:43:42 +0000195MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
196 formatted_raw_ostream &OS,
197 MCInstPrinter *InstPrint,
198 bool isVerboseAsm) {
199 return new AArch64TargetAsmStreamer(S, OS);
Chad Rosierdcd2a302014-10-22 20:35:57 +0000200}
201
Tim Northover3b0846e2014-05-24 12:50:23 +0000202MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000203 raw_pwrite_stream &OS,
204 MCCodeEmitter *Emitter, bool RelaxAll) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000205 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
206 if (RelaxAll)
207 S->getAssembler().setRelaxAll(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000208 return S;
209}
Rafael Espindolacd584a82015-03-19 01:50:16 +0000210
211MCTargetStreamer *
212createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
213 Triple TT(STI.getTargetTriple());
214 if (TT.getObjectFormat() == Triple::ELF)
215 return new AArch64TargetELFStreamer(S);
216 return nullptr;
217}
Tim Northover3b0846e2014-05-24 12:50:23 +0000218}