LLVM  4.0.0
AArch64ELFStreamer.cpp
Go to the documentation of this file.
1 //===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
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"
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"
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 public:
53  AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
54 };
55 
56 AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
58  : AArch64TargetStreamer(S), OS(OS) {}
59 
60 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
61  OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
62 }
63 
64 class AArch64TargetELFStreamer : public AArch64TargetStreamer {
65 private:
66  AArch64ELFStreamer &getStreamer();
67 
68  void emitInst(uint32_t Inst) override;
69 
70 public:
71  AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
72 };
73 
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
76 /// AArch64 ELF ABI:
77 /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
78 ///
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
83 /// instruction).
84 ///
85 /// As a result this system is orthogonal to the DataRegion infrastructure used
86 /// by MachO. Beware!
87 class AArch64ELFStreamer : public MCELFStreamer {
88 public:
89  friend class AArch64TargetELFStreamer;
90 
91  AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
92  raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
93  : MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
94  LastEMS(EMS_None) {}
95 
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);
102 
103  MCELFStreamer::ChangeSection(Section, Subsection);
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();
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();
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();
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 private:
149  enum ElfMappingSymbol {
150  EMS_None,
151  EMS_A64,
152  EMS_Data
153  };
154 
155  void EmitDataMappingSymbol() {
156  if (LastEMS == EMS_Data)
157  return;
158  EmitMappingSymbol("$d");
159  LastEMS = EMS_Data;
160  }
161 
162  void EmitA64MappingSymbol() {
163  if (LastEMS == EMS_A64)
164  return;
165  EmitMappingSymbol("$x");
166  LastEMS = EMS_A64;
167  }
168 
169  void EmitMappingSymbol(StringRef Name) {
170  auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
171  Name + "." + Twine(MappingSymbolCounter++)));
172  EmitLabel(Symbol);
173  Symbol->setType(ELF::STT_NOTYPE);
174  Symbol->setBinding(ELF::STB_LOCAL);
175  Symbol->setExternal(false);
176  }
177 
178  int64_t MappingSymbolCounter;
179 
181  ElfMappingSymbol LastEMS;
182 };
183 } // end anonymous namespace
184 
185 AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
186  return static_cast<AArch64ELFStreamer &>(Streamer);
187 }
188 
189 void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
190  getStreamer().emitInst(Inst);
191 }
192 
193 namespace llvm {
196  MCInstPrinter *InstPrint,
197  bool isVerboseAsm) {
198  return new AArch64TargetAsmStreamer(S, OS);
199 }
200 
202  raw_pwrite_stream &OS,
203  MCCodeEmitter *Emitter, bool RelaxAll) {
204  AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
205  if (RelaxAll)
206  S->getAssembler().setRelaxAll(true);
207  return S;
208 }
209 
212  const Triple &TT = STI.getTargetTriple();
213  if (TT.isOSBinFormatELF())
214  return new AArch64TargetELFStreamer(S);
215  return nullptr;
216 }
217 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
MCSectionSubPair getPreviousSection() const
Return the previous section that the streamer is emitting code to.
Definition: MCStreamer.h:305
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
LLVMContext & Context
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Target specific streamer interface.
Definition: MCStreamer.h:73
virtual void emitInst(uint32_t Inst)
Callback used to implement the .inst directive.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
void EmitLabel(MCSymbol *Symbol) override
Emit a label for Symbol into the current section.
MCContext & getContext() const
Definition: MCStreamer.h:221
Context object for machine code objects.
Definition: MCContext.h:51
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.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
void ChangeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
Streaming machine code generation interface.
Definition: MCStreamer.h:161
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
MCELFStreamer * createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS, MCCodeEmitter *Emitter, bool RelaxAll)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCTargetStreamer * createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
MCStreamer & getStreamer()
Definition: MCStreamer.h:81
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:565
MCTargetStreamer * createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:114
#define I(x, y, z)
Definition: MD5.cpp:54
MCSubtargetInfo - Generic base class for all target subtargets.
const Triple & getTargetTriple() const
getTargetTriple - Return the target triple string.
static std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:48
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, ""relax all fixups in the emitted object file"))
LLVM Value Representation.
Definition: Value.h:71
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
Represents a location in source code.
Definition: SMLoc.h:24