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/Twine.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCAssembler.h"
24 #include "llvm/MC/MCCodeEmitter.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELFStreamer.h"
27 #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"
33 #include "llvm/MC/MCSymbolELF.h"
34 #include "llvm/MC/MCValue.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ELF.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/raw_ostream.h"
45 class AArch64ELFStreamer;
47 class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
48 formatted_raw_ostream &OS;
50 void emitInst(uint32_t Inst) override;
53 AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
56 AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
57 formatted_raw_ostream &OS)
58 : AArch64TargetStreamer(S), OS(OS) {}
60 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
61 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
64 class AArch64TargetELFStreamer : public AArch64TargetStreamer {
66 AArch64ELFStreamer &getStreamer();
68 void emitInst(uint32_t Inst) override;
71 AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
74 /// 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
77 /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
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
85 /// As a result this system is orthogonal to the DataRegion infrastructure used
87 class AArch64ELFStreamer : public MCELFStreamer {
89 friend class AArch64TargetELFStreamer;
91 AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
92 raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
93 : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
96 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
97 // 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);
103 MCELFStreamer::ChangeSection(Section, Subsection);
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
109 void EmitInstruction(const MCInst &Inst,
110 const MCSubtargetInfo &STI) override {
111 EmitA64MappingSymbol();
112 MCELFStreamer::EmitInstruction(Inst, STI);
115 void emitInst(uint32_t Inst) {
116 EmitA64MappingSymbol();
117 MCELFStreamer::EmitIntValue(Inst, 4);
120 /// 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)
123 void EmitBytes(StringRef Data) override {
124 EmitDataMappingSymbol();
125 MCELFStreamer::EmitBytes(Data);
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)
131 void EmitValueImpl(const MCExpr *Value, unsigned Size,
132 const SMLoc &Loc) override {
133 EmitDataMappingSymbol();
134 MCELFStreamer::EmitValueImpl(Value, Size);
138 enum ElfMappingSymbol {
144 void EmitDataMappingSymbol() {
145 if (LastEMS == EMS_Data)
147 EmitMappingSymbol("$d");
151 void EmitA64MappingSymbol() {
152 if (LastEMS == EMS_A64)
154 EmitMappingSymbol("$x");
158 void EmitMappingSymbol(StringRef Name) {
159 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
160 Name + "." + Twine(MappingSymbolCounter++)));
162 Symbol->setType(ELF::STT_NOTYPE);
163 Symbol->setBinding(ELF::STB_LOCAL);
164 Symbol->setExternal(false);
167 int64_t MappingSymbolCounter;
169 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
170 ElfMappingSymbol LastEMS;
172 } // end anonymous namespace
174 AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
175 return static_cast<AArch64ELFStreamer &>(Streamer);
178 void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
179 getStreamer().emitInst(Inst);
183 MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
184 formatted_raw_ostream &OS,
185 MCInstPrinter *InstPrint,
187 return new AArch64TargetAsmStreamer(S, OS);
190 MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
191 raw_pwrite_stream &OS,
192 MCCodeEmitter *Emitter, bool RelaxAll) {
193 AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
195 S->getAssembler().setRelaxAll(true);
200 createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
201 const Triple &TT = STI.getTargetTriple();
202 if (TT.isOSBinFormatELF())
203 return new AArch64TargetELFStreamer(S);