LLVM  3.7.0
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/Support/Compiler.h"
15 #include "llvm/Support/DataTypes.h"
18 #include <cassert>
19 
20 namespace llvm {
21 class MCAsmLayout;
22 class MCAssembler;
23 class MCFixup;
24 class MCFragment;
25 class MCSymbolRefExpr;
26 class MCValue;
27 
28 /// Defines the object file and target independent interfaces used by the
29 /// assembler backend to write native file format object files.
30 ///
31 /// The object writer contains a few callbacks used by the assembler to allow
32 /// the object writer to modify the assembler data structures at appropriate
33 /// points. Once assembly is complete, the object writer is given the
34 /// MCAssembler instance, which contains all the symbol and section data which
35 /// should be emitted as part of writeObject().
36 ///
37 /// The object writer also contains a number of helper methods for writing
38 /// binary data to the output stream.
40  MCObjectWriter(const MCObjectWriter &) = delete;
41  void operator=(const MCObjectWriter &) = delete;
42 
43 protected:
45 
46  unsigned IsLittleEndian : 1;
47 
48 protected: // Can only create subclasses.
50  : OS(OS), IsLittleEndian(IsLittleEndian) {}
51 
52 public:
53  virtual ~MCObjectWriter();
54 
55  /// lifetime management
56  virtual void reset() {}
57 
58  bool isLittleEndian() const { return IsLittleEndian; }
59 
60  raw_ostream &getStream() { return OS; }
61 
62  /// \name High-Level API
63  /// @{
64 
65  /// Perform any late binding of symbols (for example, to assign symbol
66  /// indices for use when generating relocations).
67  ///
68  /// This routine is called by the assembler after layout and relaxation is
69  /// complete.
71  const MCAsmLayout &Layout) = 0;
72 
73  /// Record a relocation entry.
74  ///
75  /// This routine is called by the assembler after layout and relaxation, and
76  /// post layout binding. The implementation is responsible for storing
77  /// information about the relocation so that it can be emitted during
78  /// writeObject().
79  virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
80  const MCFragment *Fragment,
81  const MCFixup &Fixup, MCValue Target,
82  bool &IsPCRel, uint64_t &FixedValue) = 0;
83 
84  /// Check whether the difference (A - B) between two symbol references is
85  /// fully resolved.
86  ///
87  /// Clients are not required to answer precisely and may conservatively return
88  /// false, even when a difference is fully resolved.
90  const MCSymbolRefExpr *A,
91  const MCSymbolRefExpr *B,
92  bool InSet) const;
93 
95  const MCSymbol &SymA,
96  const MCFragment &FB,
97  bool InSet,
98  bool IsPCRel) const;
99 
100  /// True if this symbol (which is a variable) is weak. This is not
101  /// just STB_WEAK, but more generally whether or not we can evaluate
102  /// past it.
103  virtual bool isWeak(const MCSymbol &Sym) const;
104 
105  /// Write the object file.
106  ///
107  /// This routine is called by the assembler after layout and relaxation is
108  /// complete, fixups have been evaluated and applied, and relocations
109  /// generated.
110  virtual void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
111 
112  /// @}
113  /// \name Binary Output
114  /// @{
115 
116  void write8(uint8_t Value) { OS << char(Value); }
117 
118  void writeLE16(uint16_t Value) {
120  }
121 
122  void writeLE32(uint32_t Value) {
124  }
125 
126  void writeLE64(uint64_t Value) {
128  }
129 
130  void writeBE16(uint16_t Value) {
132  }
133 
134  void writeBE32(uint32_t Value) {
136  }
137 
138  void writeBE64(uint64_t Value) {
140  }
141 
142  void write16(uint16_t Value) {
143  if (IsLittleEndian)
144  writeLE16(Value);
145  else
146  writeBE16(Value);
147  }
148 
149  void write32(uint32_t Value) {
150  if (IsLittleEndian)
151  writeLE32(Value);
152  else
153  writeBE32(Value);
154  }
155 
156  void write64(uint64_t Value) {
157  if (IsLittleEndian)
158  writeLE64(Value);
159  else
160  writeBE64(Value);
161  }
162 
163  void WriteZeros(unsigned N) {
164  const char Zeros[16] = {0};
165 
166  for (unsigned i = 0, e = N / 16; i != e; ++i)
167  OS << StringRef(Zeros, 16);
168 
169  OS << StringRef(Zeros, N % 16);
170  }
171 
172  void writeBytes(const SmallVectorImpl<char> &ByteVec,
173  unsigned ZeroFillSize = 0) {
174  writeBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
175  }
176 
177  void writeBytes(StringRef Str, unsigned ZeroFillSize = 0) {
178  // TODO: this version may need to go away once all fragment contents are
179  // converted to SmallVector<char, N>
180  assert(
181  (ZeroFillSize == 0 || Str.size() <= ZeroFillSize) &&
182  "data size greater than fill size, unexpected large write will occur");
183  OS << Str;
184  if (ZeroFillSize)
185  WriteZeros(ZeroFillSize - Str.size());
186  }
187 
188  /// @}
189 };
190 
191 } // End llvm namespace
192 
193 #endif
virtual bool isWeak(const MCSymbol &Sym) const
True if this symbol (which is a variable) is weak.
void WriteZeros(unsigned N)
raw_ostream & getStream()
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
void write64(uint64_t Value)
This represents an "assembler immediate".
Definition: MCValue.h:44
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
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:62
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.
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:159
void writeBE64(uint64_t Value)
void write32(uint32_t Value)
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const
virtual void reset()
lifetime management
raw_pwrite_stream & OS
void write16(uint16_t Value)
PowerPC TLS Dynamic Call Fixup
Target - Wrapper for Target specific information.
void writeBE32(uint32_t Value)
void writeLE32(uint32_t Value)
virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue)=0
Record a relocation entry.
void write(void *memory, value_type value)
Write a value to memory with a particular endianness.
Definition: Endian.h:73
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:26
void writeLE16(uint16_t Value)
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:134
bool isLittleEndian() const
#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:321
LLVM Value Representation.
Definition: Value.h:69
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void writeBytes(StringRef Str, unsigned ZeroFillSize=0)