blob: caa1cfcb79bdba948e9ace1a4ce539a747a9e802 [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"
Tim Northover3b0846e2014-05-24 12:50:23 +000027#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCObjectStreamer.h"
30#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCSectionELF.h"
32#include "llvm/MC/MCStreamer.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000033#include "llvm/MC/MCSymbolELF.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000034#include "llvm/MC/MCValue.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/ELF.h"
37#include "llvm/Support/ErrorHandling.h"
Chad Rosierdcd2a302014-10-22 20:35:57 +000038#include "llvm/Support/FormattedStream.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000039#include "llvm/Support/raw_ostream.h"
40
41using namespace llvm;
42
43namespace {
44
Chad Rosierdcd2a302014-10-22 20:35:57 +000045class AArch64ELFStreamer;
46
47class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
48 formatted_raw_ostream &OS;
49
50 void emitInst(uint32_t Inst) override;
51
52public:
53 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
54};
55
56AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
57 formatted_raw_ostream &OS)
58 : AArch64TargetStreamer(S), OS(OS) {}
59
60void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
Benjamin Kramerbe48c402015-05-23 16:39:10 +000061 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
Chad Rosierdcd2a302014-10-22 20:35:57 +000062}
63
64class AArch64TargetELFStreamer : public AArch64TargetStreamer {
65private:
66 AArch64ELFStreamer &getStreamer();
67
68 void emitInst(uint32_t Inst) override;
69
70public:
71 AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
72};
73
Tim Northover3b0846e2014-05-24 12:50:23 +000074/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
75/// the appropriate points in the object files. These symbols are defined in the
76/// AArch64 ELF ABI:
77/// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
78///
79/// In brief: $x or $d should be emitted at the start of each contiguous region
80/// of A64 code or data in a section. In practice, this emission does not rely
81/// on explicit assembler directives but on inherent properties of the
82/// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
83/// instruction).
84///
85/// As a result this system is orthogonal to the DataRegion infrastructure used
86/// by MachO. Beware!
87class AArch64ELFStreamer : public MCELFStreamer {
88public:
Chad Rosierdcd2a302014-10-22 20:35:57 +000089 friend class AArch64TargetELFStreamer;
90
Rafael Espindola5560a4c2015-04-14 22:14:34 +000091 AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
92 raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
Tim Northover3b0846e2014-05-24 12:50:23 +000093 : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
94 LastEMS(EMS_None) {}
95
Rafael Espindola0709a7b2015-05-21 19:20:38 +000096 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover3b0846e2014-05-24 12:50:23 +000097 // We have to keep track of the mapping symbol state of any sections we
98 // use. Each one should start off as EMS_None, which is provided as the
99 // default constructor by DenseMap::lookup.
100 LastMappingSymbols[getPreviousSection().first] = LastEMS;
101 LastEMS = LastMappingSymbols.lookup(Section);
102
103 MCELFStreamer::ChangeSection(Section, Subsection);
104 }
105
106 /// This function is the one used to emit instruction data into the ELF
107 /// streamer. We override it to add the appropriate mapping symbol if
108 /// necessary.
109 void EmitInstruction(const MCInst &Inst,
110 const MCSubtargetInfo &STI) override {
111 EmitA64MappingSymbol();
112 MCELFStreamer::EmitInstruction(Inst, STI);
113 }
114
Chad Rosierdcd2a302014-10-22 20:35:57 +0000115 void emitInst(uint32_t Inst) {
Chad Rosierdcd2a302014-10-22 20:35:57 +0000116 EmitA64MappingSymbol();
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000117 MCELFStreamer::EmitIntValue(Inst, 4);
Chad Rosierdcd2a302014-10-22 20:35:57 +0000118 }
119
Tim Northover3b0846e2014-05-24 12:50:23 +0000120 /// This is one of the functions used to emit data into an ELF section, so the
121 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
122 /// if necessary.
123 void EmitBytes(StringRef Data) override {
124 EmitDataMappingSymbol();
125 MCELFStreamer::EmitBytes(Data);
126 }
127
128 /// This is one of the functions used to emit data into an ELF section, so the
129 /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
130 /// if necessary.
Craig Topper3c76c522015-09-20 23:35:59 +0000131 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
Tim Northover3b0846e2014-05-24 12:50:23 +0000132 EmitDataMappingSymbol();
133 MCELFStreamer::EmitValueImpl(Value, Size);
134 }
135
136private:
137 enum ElfMappingSymbol {
138 EMS_None,
139 EMS_A64,
140 EMS_Data
141 };
142
143 void EmitDataMappingSymbol() {
144 if (LastEMS == EMS_Data)
145 return;
146 EmitMappingSymbol("$d");
147 LastEMS = EMS_Data;
148 }
149
150 void EmitA64MappingSymbol() {
151 if (LastEMS == EMS_A64)
152 return;
153 EmitMappingSymbol("$x");
154 LastEMS = EMS_A64;
155 }
156
157 void EmitMappingSymbol(StringRef Name) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000158 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
159 Name + "." + Twine(MappingSymbolCounter++)));
Rafael Espindolac5fb5082015-06-26 11:31:13 +0000160 EmitLabel(Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000161 Symbol->setType(ELF::STT_NOTYPE);
162 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000163 Symbol->setExternal(false);
Tim Northover3b0846e2014-05-24 12:50:23 +0000164 }
165
166 int64_t MappingSymbolCounter;
167
168 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
169 ElfMappingSymbol LastEMS;
Tim Northover3b0846e2014-05-24 12:50:23 +0000170};
Chad Rosierdcd2a302014-10-22 20:35:57 +0000171} // end anonymous namespace
172
173AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
174 return static_cast<AArch64ELFStreamer &>(Streamer);
175}
176
177void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
178 getStreamer().emitInst(Inst);
Tim Northover3b0846e2014-05-24 12:50:23 +0000179}
180
181namespace llvm {
Rafael Espindola73870dd2015-03-16 21:43:42 +0000182MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
183 formatted_raw_ostream &OS,
184 MCInstPrinter *InstPrint,
185 bool isVerboseAsm) {
186 return new AArch64TargetAsmStreamer(S, OS);
Chad Rosierdcd2a302014-10-22 20:35:57 +0000187}
188
Tim Northover3b0846e2014-05-24 12:50:23 +0000189MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000190 raw_pwrite_stream &OS,
191 MCCodeEmitter *Emitter, bool RelaxAll) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000192 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
193 if (RelaxAll)
194 S->getAssembler().setRelaxAll(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000195 return S;
196}
Rafael Espindolacd584a82015-03-19 01:50:16 +0000197
198MCTargetStreamer *
199createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
Daniel Sanders50f17232015-09-15 16:17:27 +0000200 const Triple &TT = STI.getTargetTriple();
Daniel Sandersfbdab432015-07-06 16:33:18 +0000201 if (TT.isOSBinFormatELF())
Rafael Espindolacd584a82015-03-19 01:50:16 +0000202 return new AArch64TargetELFStreamer(S);
203 return nullptr;
204}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000205}