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