LLVM  13.0.0git
AArch64ELFStreamer.cpp
Go to the documentation of this file.
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 "AArch64ELFStreamer.h"
16 #include "AArch64MCTargetDesc.h"
17 #include "AArch64TargetStreamer.h"
18 #include "AArch64WinCOFFStreamer.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/MC/MCAsmBackend.h"
25 #include "llvm/MC/MCAssembler.h"
26 #include "llvm/MC/MCCodeEmitter.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCELFStreamer.h"
29 #include "llvm/MC/MCExpr.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCObjectWriter.h"
32 #include "llvm/MC/MCSection.h"
33 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MCSymbolELF.h"
37 #include "llvm/Support/Casting.h"
40 
41 using namespace llvm;
42 
43 namespace {
44 
45 class AArch64ELFStreamer;
46 
47 class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
49 
50  void emitInst(uint32_t Inst) override;
51 
52  void emitDirectiveVariantPCS(MCSymbol *Symbol) override {
53  OS << "\t.variant_pcs\t" << Symbol->getName() << "\n";
54  }
55 
56  void EmitARM64WinCFIAllocStack(unsigned Size) override {
57  OS << "\t.seh_stackalloc\t" << Size << "\n";
58  }
59  void EmitARM64WinCFISaveR19R20X(int Offset) override {
60  OS << "\t.seh_save_r19r20_x\t" << Offset << "\n";
61  }
62  void EmitARM64WinCFISaveFPLR(int Offset) override {
63  OS << "\t.seh_save_fplr\t" << Offset << "\n";
64  }
65  void EmitARM64WinCFISaveFPLRX(int Offset) override {
66  OS << "\t.seh_save_fplr_x\t" << Offset << "\n";
67  }
68  void EmitARM64WinCFISaveReg(unsigned Reg, int Offset) override {
69  OS << "\t.seh_save_reg\tx" << Reg << ", " << Offset << "\n";
70  }
71  void EmitARM64WinCFISaveRegX(unsigned Reg, int Offset) override {
72  OS << "\t.seh_save_reg_x\tx" << Reg << ", " << Offset << "\n";
73  }
74  void EmitARM64WinCFISaveRegP(unsigned Reg, int Offset) override {
75  OS << "\t.seh_save_regp\tx" << Reg << ", " << Offset << "\n";
76  }
77  void EmitARM64WinCFISaveRegPX(unsigned Reg, int Offset) override {
78  OS << "\t.seh_save_regp_x\tx" << Reg << ", " << Offset << "\n";
79  }
80  void EmitARM64WinCFISaveLRPair(unsigned Reg, int Offset) override {
81  OS << "\t.seh_save_lrpair\tx" << Reg << ", " << Offset << "\n";
82  }
83  void EmitARM64WinCFISaveFReg(unsigned Reg, int Offset) override {
84  OS << "\t.seh_save_freg\td" << Reg << ", " << Offset << "\n";
85  }
86  void EmitARM64WinCFISaveFRegX(unsigned Reg, int Offset) override {
87  OS << "\t.seh_save_freg_x\td" << Reg << ", " << Offset << "\n";
88  }
89  void EmitARM64WinCFISaveFRegP(unsigned Reg, int Offset) override {
90  OS << "\t.seh_save_fregp\td" << Reg << ", " << Offset << "\n";
91  }
92  void EmitARM64WinCFISaveFRegPX(unsigned Reg, int Offset) override {
93  OS << "\t.seh_save_fregp_x\td" << Reg << ", " << Offset << "\n";
94  }
95  void EmitARM64WinCFISetFP() override { OS << "\t.seh_set_fp\n"; }
96  void EmitARM64WinCFIAddFP(unsigned Size) override {
97  OS << "\t.seh_add_fp\t" << Size << "\n";
98  }
99  void EmitARM64WinCFINop() override { OS << "\t.seh_nop\n"; }
100  void EmitARM64WinCFISaveNext() override { OS << "\t.seh_save_next\n"; }
101  void EmitARM64WinCFIPrologEnd() override { OS << "\t.seh_endprologue\n"; }
102  void EmitARM64WinCFIEpilogStart() override { OS << "\t.seh_startepilogue\n"; }
103  void EmitARM64WinCFIEpilogEnd() override { OS << "\t.seh_endepilogue\n"; }
104  void EmitARM64WinCFITrapFrame() override { OS << "\t.seh_trap_frame\n"; }
105  void EmitARM64WinCFIMachineFrame() override { OS << "\t.seh_pushframe\n"; }
106  void EmitARM64WinCFIContext() override { OS << "\t.seh_context\n"; }
107  void EmitARM64WinCFIClearUnwoundToCall() override {
108  OS << "\t.seh_clear_unwound_to_call\n";
109  }
110 
111 public:
112  AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
113 };
114 
115 AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
117  : AArch64TargetStreamer(S), OS(OS) {}
118 
119 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
120  OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
121 }
122 
123 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
124 /// the appropriate points in the object files. These symbols are defined in the
125 /// AArch64 ELF ABI:
126 /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
127 ///
128 /// In brief: $x or $d should be emitted at the start of each contiguous region
129 /// of A64 code or data in a section. In practice, this emission does not rely
130 /// on explicit assembler directives but on inherent properties of the
131 /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
132 /// instruction).
133 ///
134 /// As a result this system is orthogonal to the DataRegion infrastructure used
135 /// by MachO. Beware!
136 class AArch64ELFStreamer : public MCELFStreamer {
137 public:
138  AArch64ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
139  std::unique_ptr<MCObjectWriter> OW,
140  std::unique_ptr<MCCodeEmitter> Emitter)
141  : MCELFStreamer(Context, std::move(TAB), std::move(OW),
142  std::move(Emitter)),
143  MappingSymbolCounter(0), LastEMS(EMS_None) {}
144 
145  void changeSection(MCSection *Section, const MCExpr *Subsection) override {
146  // We have to keep track of the mapping symbol state of any sections we
147  // use. Each one should start off as EMS_None, which is provided as the
148  // default constructor by DenseMap::lookup.
149  LastMappingSymbols[getPreviousSection().first] = LastEMS;
150  LastEMS = LastMappingSymbols.lookup(Section);
151 
153  }
154 
155  // Reset state between object emissions
156  void reset() override {
157  MappingSymbolCounter = 0;
159  LastMappingSymbols.clear();
160  LastEMS = EMS_None;
161  }
162 
163  /// This function is the one used to emit instruction data into the ELF
164  /// streamer. We override it to add the appropriate mapping symbol if
165  /// necessary.
166  void emitInstruction(const MCInst &Inst,
167  const MCSubtargetInfo &STI) override {
168  EmitA64MappingSymbol();
170  }
171 
172  /// Emit a 32-bit value as an instruction. This is only used for the .inst
173  /// directive, EmitInstruction should be used in other cases.
174  void emitInst(uint32_t Inst) {
175  char Buffer[4];
176 
177  // We can't just use EmitIntValue here, as that will emit a data mapping
178  // symbol, and swap the endianness on big-endian systems (instructions are
179  // always little-endian).
180  for (unsigned I = 0; I < 4; ++I) {
181  Buffer[I] = uint8_t(Inst);
182  Inst >>= 8;
183  }
184 
185  EmitA64MappingSymbol();
187  }
188 
189  /// This is one of the functions used to emit data into an ELF section, so the
190  /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
191  /// if necessary.
192  void emitBytes(StringRef Data) override {
193  emitDataMappingSymbol();
195  }
196 
197  /// This is one of the functions used to emit data into an ELF section, so the
198  /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
199  /// if necessary.
200  void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
201  emitDataMappingSymbol();
203  }
204 
205  void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
206  SMLoc Loc) override {
207  emitDataMappingSymbol();
208  MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
209  }
210 private:
211  enum ElfMappingSymbol {
212  EMS_None,
213  EMS_A64,
214  EMS_Data
215  };
216 
217  void emitDataMappingSymbol() {
218  if (LastEMS == EMS_Data)
219  return;
220  EmitMappingSymbol("$d");
221  LastEMS = EMS_Data;
222  }
223 
224  void EmitA64MappingSymbol() {
225  if (LastEMS == EMS_A64)
226  return;
227  EmitMappingSymbol("$x");
228  LastEMS = EMS_A64;
229  }
230 
231  void EmitMappingSymbol(StringRef Name) {
232  auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
233  Name + "." + Twine(MappingSymbolCounter++)));
234  emitLabel(Symbol);
235  Symbol->setType(ELF::STT_NOTYPE);
236  Symbol->setBinding(ELF::STB_LOCAL);
237  Symbol->setExternal(false);
238  }
239 
240  int64_t MappingSymbolCounter;
241 
243  ElfMappingSymbol LastEMS;
244 };
245 
246 } // end anonymous namespace
247 
248 AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
249  return static_cast<AArch64ELFStreamer &>(Streamer);
250 }
251 
252 void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
253  getStreamer().emitInst(Inst);
254 }
255 
256 void AArch64TargetELFStreamer::emitDirectiveVariantPCS(MCSymbol *Symbol) {
257  cast<MCSymbolELF>(Symbol)->setOther(ELF::STO_AARCH64_VARIANT_PCS);
258 }
259 
262  MCInstPrinter *InstPrint,
263  bool isVerboseAsm) {
264  return new AArch64TargetAsmStreamer(S, OS);
265 }
266 
268  MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
269  std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
270  bool RelaxAll) {
271  AArch64ELFStreamer *S = new AArch64ELFStreamer(
272  Context, std::move(TAB), std::move(OW), std::move(Emitter));
273  if (RelaxAll)
274  S->getAssembler().setRelaxAll(true);
275  return S;
276 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
Definition: AllocatorList.h:23
llvm::ELF::STO_AARCH64_VARIANT_PCS
@ STO_AARCH64_VARIANT_PCS
Definition: ELF.h:426
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCELFStreamer::emitLabel
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Definition: MCELFStreamer.cpp:99
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:71
StringRef.h
MCCodeEmitter.h
llvm::MCELFStreamer::changeSection
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
Definition: MCELFStreamer.cpp:146
llvm::ELF::STB_LOCAL
@ STB_LOCAL
Definition: ELF.h:1132
MCAssembler.h
DenseMap.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::createAArch64AsmTargetStreamer
MCTargetStreamer * createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
Definition: AArch64ELFStreamer.cpp:261
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MCELFStreamer::reset
void reset() override
state management
Definition: MCELFStreamer.h:32
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
MCELFStreamer.h
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
FormattedStream.h
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:197
llvm::MCStreamer::getPreviousSection
MCSectionSubPair getPreviousSection() const
Return the previous section that the streamer is emitting code to.
Definition: MCStreamer.h:377
ELF.h
llvm::MCELFStreamer
Definition: MCELFStreamer.h:23
MCAsmBackend.h
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
AArch64ELFStreamer.h
Twine.h
MCSymbolELF.h
MCContext.h
llvm::MCObjectStreamer::emitInstruction
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
Definition: MCObjectStreamer.cpp:367
MCInst.h
MCSubtargetInfo.h
llvm::MCTargetStreamer
Target specific streamer interface.
Definition: MCStreamer.h:91
llvm::MCTargetStreamer::Streamer
MCStreamer & Streamer
Definition: MCStreamer.h:93
llvm::MCInstPrinter
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:43
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::ELF::STT_NOTYPE
@ STT_NOTYPE
Definition: ELF.h:1144
llvm::formatted_raw_ostream
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Definition: FormattedStream.h:30
llvm::AArch64TargetStreamer
Definition: AArch64TargetStreamer.h:20
MCSection.h
Triple.h
llvm::MCELFStreamer::emitValueImpl
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
Definition: MCELFStreamer.cpp:355
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
MCWinCOFFStreamer.h
llvm::MCObjectStreamer::emitFill
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
Definition: MCObjectStreamer.cpp:803
MCObjectWriter.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
std
Definition: BitVector.h:838
Casting.h
AArch64MCTargetDesc.h
AArch64TargetStreamer.h
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::MCObjectStreamer::emitBytes
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
Definition: MCObjectStreamer.cpp:595
llvm::MCStreamer::getContext
MCContext & getContext() const
Definition: MCStreamer.h:278
MCStreamer.h
AArch64WinCOFFStreamer.h
raw_ostream.h
MCExpr.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::createAArch64ELFStreamer
MCELFStreamer * createAArch64ELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter, bool RelaxAll)
Definition: AArch64ELFStreamer.cpp:267