LLVM  6.0.0svn
MCObjectWriter.h
Go to the documentation of this file.
1 //===- llvm/MC/MCObjectWriter.h - Object File Writer Interface --*- C++ -*-===//
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 #ifndef LLVM_MC_MCOBJECTWRITER_H
11 #define LLVM_MC_MCOBJECTWRITER_H
12 
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Endian.h"
18 #include <cassert>
19 #include <cstdint>
20 
21 namespace llvm {
22 
23 class MCAsmLayout;
24 class MCAssembler;
25 class MCFixup;
26 class MCFragment;
27 class MCSymbol;
28 class MCSymbolRefExpr;
29 class MCValue;
30 
31 /// Defines the object file and target independent interfaces used by the
32 /// assembler backend to write native file format object files.
33 ///
34 /// The object writer contains a few callbacks used by the assembler to allow
35 /// the object writer to modify the assembler data structures at appropriate
36 /// points. Once assembly is complete, the object writer is given the
37 /// MCAssembler instance, which contains all the symbol and section data which
38 /// should be emitted as part of writeObject().
39 ///
40 /// The object writer also contains a number of helper methods for writing
41 /// binary data to the output stream.
44 
45 protected:
46  unsigned IsLittleEndian : 1;
47 
48  // Can only create subclasses.
50  : OS(&OS), IsLittleEndian(IsLittleEndian) {}
51 
52  unsigned getInitialOffset() {
53  return OS->tell();
54  }
55 
56 public:
57  MCObjectWriter(const MCObjectWriter &) = delete;
58  MCObjectWriter &operator=(const MCObjectWriter &) = delete;
59  virtual ~MCObjectWriter();
60 
61  /// lifetime management
62  virtual void reset() {}
63 
64  bool isLittleEndian() const { return IsLittleEndian; }
65 
66  raw_pwrite_stream &getStream() { return *OS; }
67  void setStream(raw_pwrite_stream &NewOS) { OS = &NewOS; }
68 
69  /// \name High-Level API
70  /// @{
71 
72  /// Perform any late binding of symbols (for example, to assign symbol
73  /// indices for use when generating relocations).
74  ///
75  /// This routine is called by the assembler after layout and relaxation is
76  /// complete.
78  const MCAsmLayout &Layout) = 0;
79 
80  /// Record a relocation entry.
81  ///
82  /// This routine is called by the assembler after layout and relaxation, and
83  /// post layout binding. The implementation is responsible for storing
84  /// information about the relocation so that it can be emitted during
85  /// writeObject().
86  virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
87  const MCFragment *Fragment,
88  const MCFixup &Fixup, MCValue Target,
89  uint64_t &FixedValue) = 0;
90 
91  /// Check whether the difference (A - B) between two symbol references is
92  /// fully resolved.
93  ///
94  /// Clients are not required to answer precisely and may conservatively return
95  /// false, even when a difference is fully resolved.
97  const MCSymbolRefExpr *A,
98  const MCSymbolRefExpr *B,
99  bool InSet) const;
100 
101  virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
102  const MCSymbol &A,
103  const MCSymbol &B,
104  bool InSet) const;
105 
106  virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
107  const MCSymbol &SymA,
108  const MCFragment &FB,
109  bool InSet,
110  bool IsPCRel) const;
111 
112  /// Write the object file.
113  ///
114  /// This routine is called by the assembler after layout and relaxation is
115  /// complete, fixups have been evaluated and applied, and relocations
116  /// generated.
117  virtual void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
118 
119  /// @}
120  /// \name Binary Output
121  /// @{
122 
123  void write8(uint8_t Value) { *OS << char(Value); }
124 
125  void writeLE16(uint16_t Value) {
127  }
128 
131  }
132 
133  void writeLE64(uint64_t Value) {
135  }
136 
137  void writeBE16(uint16_t Value) {
139  }
140 
143  }
144 
145  void writeBE64(uint64_t Value) {
147  }
148 
149  void write16(uint16_t Value) {
150  if (IsLittleEndian)
151  writeLE16(Value);
152  else
153  writeBE16(Value);
154  }
155 
157  if (IsLittleEndian)
158  writeLE32(Value);
159  else
160  writeBE32(Value);
161  }
162 
163  void write64(uint64_t Value) {
164  if (IsLittleEndian)
165  writeLE64(Value);
166  else
167  writeBE64(Value);
168  }
169 
170  void WriteZeros(unsigned N) {
171  const char Zeros[16] = {0};
172 
173  for (unsigned i = 0, e = N / 16; i != e; ++i)
174  *OS << StringRef(Zeros, 16);
175 
176  *OS << StringRef(Zeros, N % 16);
177  }
178 
179  void writeBytes(const SmallVectorImpl<char> &ByteVec,
180  unsigned ZeroFillSize = 0) {
181  writeBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
182  }
183 
184  void writeBytes(StringRef Str, unsigned ZeroFillSize = 0) {
185  // TODO: this version may need to go away once all fragment contents are
186  // converted to SmallVector<char, N>
187  assert(
188  (ZeroFillSize == 0 || Str.size() <= ZeroFillSize) &&
189  "data size greater than fill size, unexpected large write will occur");
190  *OS << Str;
191  if (ZeroFillSize)
192  WriteZeros(ZeroFillSize - Str.size());
193  }
194 
195  /// @}
196 };
197 
198 } // end namespace llvm
199 
200 #endif // LLVM_MC_MCOBJECTWRITER_H
bool isLittleEndian() const
void WriteZeros(unsigned N)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
raw_pwrite_stream & getStream()
void write64(uint64_t Value)
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
virtual void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
virtual void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Write the object file.
void writeLE64(uint64_t Value)
MCObjectWriter(raw_pwrite_stream &OS, bool IsLittleEndian)
Defines the object file and target independent interfaces used by the assembler backend to write nati...
void write8(uint8_t Value)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
void writeBE16(uint16_t Value)
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
void writeBE64(uint64_t Value)
void write32(uint32_t Value)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
virtual ~MCObjectWriter()
void write(ArrayRef< value_type > Vals)
Definition: EndianStream.h:30
virtual void reset()
lifetime management
unsigned getInitialOffset()
void setStream(raw_pwrite_stream &NewOS)
void write16(uint16_t Value)
PowerPC TLS Dynamic Call Fixup
MCObjectWriter & operator=(const MCObjectWriter &)=delete
virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
Record a relocation entry.
Target - Wrapper for Target specific information.
void writeBE32(uint32_t Value)
void writeLE32(uint32_t Value)
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, bool InSet) const
bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm, const MCSymbolRefExpr *A, const MCSymbolRefExpr *B, bool InSet) const
Check whether the difference (A - B) between two symbol references is fully resolved.
void writeLE16(uint16_t Value)
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:143
#define N
void writeBytes(const SmallVectorImpl< char > &ByteVec, unsigned ZeroFillSize=0)
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:337
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:98
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void writeBytes(StringRef Str, unsigned ZeroFillSize=0)