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/TargetTuple.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/MC/MCAsmBackend.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCAssembler.h"
25 #include "llvm/MC/MCCodeEmitter.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCELFStreamer.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/MCSymbolELF.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"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/raw_ostream.h"
46 class AArch64ELFStreamer;
48 class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
49 formatted_raw_ostream &OS;
51 void emitInst(uint32_t Inst) override;
54 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
57 AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
58 formatted_raw_ostream &OS)
59 : AArch64TargetStreamer(S), OS(OS) {}
61 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
62 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
65 class AArch64TargetELFStreamer : public AArch64TargetStreamer {
67 AArch64ELFStreamer &getStreamer();
69 void emitInst(uint32_t Inst) override;
72 AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
75 /// 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
78 /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
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
86 /// As a result this system is orthogonal to the DataRegion infrastructure used
88 class AArch64ELFStreamer : public MCELFStreamer {
90 friend class AArch64TargetELFStreamer;
92 AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
93 raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
94 : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
97 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
98 // 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);
104 MCELFStreamer::ChangeSection(Section, Subsection);
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
110 void EmitInstruction(const MCInst &Inst,
111 const MCSubtargetInfo &STI) override {
112 EmitA64MappingSymbol();
113 MCELFStreamer::EmitInstruction(Inst, STI);
116 void emitInst(uint32_t Inst) {
117 EmitA64MappingSymbol();
118 MCELFStreamer::EmitIntValue(Inst, 4);
121 /// 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)
124 void EmitBytes(StringRef Data) override {
125 EmitDataMappingSymbol();
126 MCELFStreamer::EmitBytes(Data);
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)
132 void EmitValueImpl(const MCExpr *Value, unsigned Size,
133 const SMLoc &Loc) override {
134 EmitDataMappingSymbol();
135 MCELFStreamer::EmitValueImpl(Value, Size);
139 enum ElfMappingSymbol {
145 void EmitDataMappingSymbol() {
146 if (LastEMS == EMS_Data)
148 EmitMappingSymbol("$d");
152 void EmitA64MappingSymbol() {
153 if (LastEMS == EMS_A64)
155 EmitMappingSymbol("$x");
159 void EmitMappingSymbol(StringRef Name) {
160 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
161 Name + "." + Twine(MappingSymbolCounter++)));
163 Symbol->setType(ELF::STT_NOTYPE);
164 Symbol->setBinding(ELF::STB_LOCAL);
165 Symbol->setExternal(false);
168 int64_t MappingSymbolCounter;
170 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
171 ElfMappingSymbol LastEMS;
173 } // end anonymous namespace
175 AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
176 return static_cast<AArch64ELFStreamer &>(Streamer);
179 void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
180 getStreamer().emitInst(Inst);
184 MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
185 formatted_raw_ostream &OS,
186 MCInstPrinter *InstPrint,
188 return new AArch64TargetAsmStreamer(S, OS);
191 MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
192 raw_pwrite_stream &OS,
193 MCCodeEmitter *Emitter, bool RelaxAll) {
194 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
196 S->getAssembler().setRelaxAll(true);
201 createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
202 const TargetTuple &TT = STI.getTargetTuple();
203 if (TT.isOSBinFormatELF())
204 return new AArch64TargetELFStreamer(S);