xref: /aosp_15_r20/external/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file assembles .s files and emits AArch64 ELF .o object files. Different
11*9880d681SAndroid Build Coastguard Worker // from generic ELF streamer in emitting mapping symbols ($x and $d) to delimit
12*9880d681SAndroid Build Coastguard Worker // regions of data and code.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "AArch64TargetStreamer.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCELFStreamer.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Twine.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmBackend.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAssembler.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCCodeEmitter.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCELFStreamer.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCExpr.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCInst.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCObjectStreamer.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSection.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSectionELF.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCStreamer.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSymbolELF.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCValue.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ELF.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FormattedStream.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
40*9880d681SAndroid Build Coastguard Worker 
41*9880d681SAndroid Build Coastguard Worker using namespace llvm;
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker namespace {
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker class AArch64ELFStreamer;
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
48*9880d681SAndroid Build Coastguard Worker   formatted_raw_ostream &OS;
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker   void emitInst(uint32_t Inst) override;
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker public:
53*9880d681SAndroid Build Coastguard Worker   AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
54*9880d681SAndroid Build Coastguard Worker };
55*9880d681SAndroid Build Coastguard Worker 
AArch64TargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS)56*9880d681SAndroid Build Coastguard Worker AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
57*9880d681SAndroid Build Coastguard Worker                                                    formatted_raw_ostream &OS)
58*9880d681SAndroid Build Coastguard Worker   : AArch64TargetStreamer(S), OS(OS) {}
59*9880d681SAndroid Build Coastguard Worker 
emitInst(uint32_t Inst)60*9880d681SAndroid Build Coastguard Worker void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
61*9880d681SAndroid Build Coastguard Worker   OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker class AArch64TargetELFStreamer : public AArch64TargetStreamer {
65*9880d681SAndroid Build Coastguard Worker private:
66*9880d681SAndroid Build Coastguard Worker   AArch64ELFStreamer &getStreamer();
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   void emitInst(uint32_t Inst) override;
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker public:
AArch64TargetELFStreamer(MCStreamer & S)71*9880d681SAndroid Build Coastguard Worker   AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
72*9880d681SAndroid Build Coastguard Worker };
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
75*9880d681SAndroid Build Coastguard Worker /// the appropriate points in the object files. These symbols are defined in the
76*9880d681SAndroid Build Coastguard Worker /// AArch64 ELF ABI:
77*9880d681SAndroid Build Coastguard Worker ///    infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
78*9880d681SAndroid Build Coastguard Worker ///
79*9880d681SAndroid Build Coastguard Worker /// In brief: $x or $d should be emitted at the start of each contiguous region
80*9880d681SAndroid Build Coastguard Worker /// of A64 code or data in a section. In practice, this emission does not rely
81*9880d681SAndroid Build Coastguard Worker /// on explicit assembler directives but on inherent properties of the
82*9880d681SAndroid Build Coastguard Worker /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
83*9880d681SAndroid Build Coastguard Worker /// instruction).
84*9880d681SAndroid Build Coastguard Worker ///
85*9880d681SAndroid Build Coastguard Worker /// As a result this system is orthogonal to the DataRegion infrastructure used
86*9880d681SAndroid Build Coastguard Worker /// by MachO. Beware!
87*9880d681SAndroid Build Coastguard Worker class AArch64ELFStreamer : public MCELFStreamer {
88*9880d681SAndroid Build Coastguard Worker public:
89*9880d681SAndroid Build Coastguard Worker   friend class AArch64TargetELFStreamer;
90*9880d681SAndroid Build Coastguard Worker 
AArch64ELFStreamer(MCContext & Context,MCAsmBackend & TAB,raw_pwrite_stream & OS,MCCodeEmitter * Emitter)91*9880d681SAndroid Build Coastguard Worker   AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
92*9880d681SAndroid Build Coastguard Worker                      raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
93*9880d681SAndroid Build Coastguard Worker       : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
94*9880d681SAndroid Build Coastguard Worker         LastEMS(EMS_None) {}
95*9880d681SAndroid Build Coastguard Worker 
ChangeSection(MCSection * Section,const MCExpr * Subsection)96*9880d681SAndroid Build Coastguard Worker   void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
97*9880d681SAndroid Build Coastguard Worker     // We have to keep track of the mapping symbol state of any sections we
98*9880d681SAndroid Build Coastguard Worker     // use. Each one should start off as EMS_None, which is provided as the
99*9880d681SAndroid Build Coastguard Worker     // default constructor by DenseMap::lookup.
100*9880d681SAndroid Build Coastguard Worker     LastMappingSymbols[getPreviousSection().first] = LastEMS;
101*9880d681SAndroid Build Coastguard Worker     LastEMS = LastMappingSymbols.lookup(Section);
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker     MCELFStreamer::ChangeSection(Section, Subsection);
104*9880d681SAndroid Build Coastguard Worker   }
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   /// This function is the one used to emit instruction data into the ELF
107*9880d681SAndroid Build Coastguard Worker   /// streamer. We override it to add the appropriate mapping symbol if
108*9880d681SAndroid Build Coastguard Worker   /// necessary.
EmitInstruction(const MCInst & Inst,const MCSubtargetInfo & STI)109*9880d681SAndroid Build Coastguard Worker   void EmitInstruction(const MCInst &Inst,
110*9880d681SAndroid Build Coastguard Worker                        const MCSubtargetInfo &STI) override {
111*9880d681SAndroid Build Coastguard Worker     EmitA64MappingSymbol();
112*9880d681SAndroid Build Coastguard Worker     MCELFStreamer::EmitInstruction(Inst, STI);
113*9880d681SAndroid Build Coastguard Worker   }
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker   /// Emit a 32-bit value as an instruction. This is only used for the .inst
116*9880d681SAndroid Build Coastguard Worker   /// directive, EmitInstruction should be used in other cases.
emitInst(uint32_t Inst)117*9880d681SAndroid Build Coastguard Worker   void emitInst(uint32_t Inst) {
118*9880d681SAndroid Build Coastguard Worker     char Buffer[4];
119*9880d681SAndroid Build Coastguard Worker 
120*9880d681SAndroid Build Coastguard Worker     // We can't just use EmitIntValue here, as that will emit a data mapping
121*9880d681SAndroid Build Coastguard Worker     // symbol, and swap the endianness on big-endian systems (instructions are
122*9880d681SAndroid Build Coastguard Worker     // always little-endian).
123*9880d681SAndroid Build Coastguard Worker     for (unsigned I = 0; I < 4; ++I) {
124*9880d681SAndroid Build Coastguard Worker       Buffer[I] = uint8_t(Inst);
125*9880d681SAndroid Build Coastguard Worker       Inst >>= 8;
126*9880d681SAndroid Build Coastguard Worker     }
127*9880d681SAndroid Build Coastguard Worker 
128*9880d681SAndroid Build Coastguard Worker     EmitA64MappingSymbol();
129*9880d681SAndroid Build Coastguard Worker     MCELFStreamer::EmitBytes(StringRef(Buffer, 4));
130*9880d681SAndroid Build Coastguard Worker   }
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   /// This is one of the functions used to emit data into an ELF section, so the
133*9880d681SAndroid Build Coastguard Worker   /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
134*9880d681SAndroid Build Coastguard Worker   /// if necessary.
EmitBytes(StringRef Data)135*9880d681SAndroid Build Coastguard Worker   void EmitBytes(StringRef Data) override {
136*9880d681SAndroid Build Coastguard Worker     EmitDataMappingSymbol();
137*9880d681SAndroid Build Coastguard Worker     MCELFStreamer::EmitBytes(Data);
138*9880d681SAndroid Build Coastguard Worker   }
139*9880d681SAndroid Build Coastguard Worker 
140*9880d681SAndroid Build Coastguard Worker   /// This is one of the functions used to emit data into an ELF section, so the
141*9880d681SAndroid Build Coastguard Worker   /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
142*9880d681SAndroid Build Coastguard Worker   /// if necessary.
EmitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)143*9880d681SAndroid Build Coastguard Worker   void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
144*9880d681SAndroid Build Coastguard Worker     EmitDataMappingSymbol();
145*9880d681SAndroid Build Coastguard Worker     MCELFStreamer::EmitValueImpl(Value, Size, Loc);
146*9880d681SAndroid Build Coastguard Worker   }
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker private:
149*9880d681SAndroid Build Coastguard Worker   enum ElfMappingSymbol {
150*9880d681SAndroid Build Coastguard Worker     EMS_None,
151*9880d681SAndroid Build Coastguard Worker     EMS_A64,
152*9880d681SAndroid Build Coastguard Worker     EMS_Data
153*9880d681SAndroid Build Coastguard Worker   };
154*9880d681SAndroid Build Coastguard Worker 
EmitDataMappingSymbol()155*9880d681SAndroid Build Coastguard Worker   void EmitDataMappingSymbol() {
156*9880d681SAndroid Build Coastguard Worker     if (LastEMS == EMS_Data)
157*9880d681SAndroid Build Coastguard Worker       return;
158*9880d681SAndroid Build Coastguard Worker     EmitMappingSymbol("$d");
159*9880d681SAndroid Build Coastguard Worker     LastEMS = EMS_Data;
160*9880d681SAndroid Build Coastguard Worker   }
161*9880d681SAndroid Build Coastguard Worker 
EmitA64MappingSymbol()162*9880d681SAndroid Build Coastguard Worker   void EmitA64MappingSymbol() {
163*9880d681SAndroid Build Coastguard Worker     if (LastEMS == EMS_A64)
164*9880d681SAndroid Build Coastguard Worker       return;
165*9880d681SAndroid Build Coastguard Worker     EmitMappingSymbol("$x");
166*9880d681SAndroid Build Coastguard Worker     LastEMS = EMS_A64;
167*9880d681SAndroid Build Coastguard Worker   }
168*9880d681SAndroid Build Coastguard Worker 
EmitMappingSymbol(StringRef Name)169*9880d681SAndroid Build Coastguard Worker   void EmitMappingSymbol(StringRef Name) {
170*9880d681SAndroid Build Coastguard Worker     auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
171*9880d681SAndroid Build Coastguard Worker         Name + "." + Twine(MappingSymbolCounter++)));
172*9880d681SAndroid Build Coastguard Worker     EmitLabel(Symbol);
173*9880d681SAndroid Build Coastguard Worker     Symbol->setType(ELF::STT_NOTYPE);
174*9880d681SAndroid Build Coastguard Worker     Symbol->setBinding(ELF::STB_LOCAL);
175*9880d681SAndroid Build Coastguard Worker     Symbol->setExternal(false);
176*9880d681SAndroid Build Coastguard Worker   }
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker   int64_t MappingSymbolCounter;
179*9880d681SAndroid Build Coastguard Worker 
180*9880d681SAndroid Build Coastguard Worker   DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
181*9880d681SAndroid Build Coastguard Worker   ElfMappingSymbol LastEMS;
182*9880d681SAndroid Build Coastguard Worker };
183*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
184*9880d681SAndroid Build Coastguard Worker 
getStreamer()185*9880d681SAndroid Build Coastguard Worker AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
186*9880d681SAndroid Build Coastguard Worker   return static_cast<AArch64ELFStreamer &>(Streamer);
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker 
emitInst(uint32_t Inst)189*9880d681SAndroid Build Coastguard Worker void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
190*9880d681SAndroid Build Coastguard Worker   getStreamer().emitInst(Inst);
191*9880d681SAndroid Build Coastguard Worker }
192*9880d681SAndroid Build Coastguard Worker 
193*9880d681SAndroid Build Coastguard Worker namespace llvm {
createAArch64AsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter * InstPrint,bool isVerboseAsm)194*9880d681SAndroid Build Coastguard Worker MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
195*9880d681SAndroid Build Coastguard Worker                                                  formatted_raw_ostream &OS,
196*9880d681SAndroid Build Coastguard Worker                                                  MCInstPrinter *InstPrint,
197*9880d681SAndroid Build Coastguard Worker                                                  bool isVerboseAsm) {
198*9880d681SAndroid Build Coastguard Worker   return new AArch64TargetAsmStreamer(S, OS);
199*9880d681SAndroid Build Coastguard Worker }
200*9880d681SAndroid Build Coastguard Worker 
createAArch64ELFStreamer(MCContext & Context,MCAsmBackend & TAB,raw_pwrite_stream & OS,MCCodeEmitter * Emitter,bool RelaxAll)201*9880d681SAndroid Build Coastguard Worker MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
202*9880d681SAndroid Build Coastguard Worker                                         raw_pwrite_stream &OS,
203*9880d681SAndroid Build Coastguard Worker                                         MCCodeEmitter *Emitter, bool RelaxAll) {
204*9880d681SAndroid Build Coastguard Worker   AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
205*9880d681SAndroid Build Coastguard Worker   if (RelaxAll)
206*9880d681SAndroid Build Coastguard Worker     S->getAssembler().setRelaxAll(true);
207*9880d681SAndroid Build Coastguard Worker   return S;
208*9880d681SAndroid Build Coastguard Worker }
209*9880d681SAndroid Build Coastguard Worker 
210*9880d681SAndroid Build Coastguard Worker MCTargetStreamer *
createAArch64ObjectTargetStreamer(MCStreamer & S,const MCSubtargetInfo & STI)211*9880d681SAndroid Build Coastguard Worker createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
212*9880d681SAndroid Build Coastguard Worker   const Triple &TT = STI.getTargetTriple();
213*9880d681SAndroid Build Coastguard Worker   if (TT.isOSBinFormatELF())
214*9880d681SAndroid Build Coastguard Worker     return new AArch64TargetELFStreamer(S);
215*9880d681SAndroid Build Coastguard Worker   return nullptr;
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker }
218