1 //===- llvm/MC/MCObjectWriter.h - Object File Writer Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCOBJECTWRITER_H
11 #define LLVM_MC_MCOBJECTWRITER_H
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/EndianStream.h"
17 #include "llvm/Support/raw_ostream.h"
28 class MCSymbolRefExpr;
31 /// Defines the object file and target independent interfaces used by the
32 /// assembler backend to write native file format object files.
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().
40 /// The object writer also contains a number of helper methods for writing
41 /// binary data to the output stream.
42 class MCObjectWriter {
43 raw_pwrite_stream *OS;
46 unsigned IsLittleEndian : 1;
48 // Can only create subclasses.
49 MCObjectWriter(raw_pwrite_stream &OS, bool IsLittleEndian)
50 : OS(&OS), IsLittleEndian(IsLittleEndian) {}
52 unsigned getInitialOffset() {
57 MCObjectWriter(const MCObjectWriter &) = delete;
58 MCObjectWriter &operator=(const MCObjectWriter &) = delete;
59 virtual ~MCObjectWriter();
61 /// lifetime management
62 virtual void reset() {}
64 bool isLittleEndian() const { return IsLittleEndian; }
66 raw_pwrite_stream &getStream() { return *OS; }
67 void setStream(raw_pwrite_stream &NewOS) { OS = &NewOS; }
69 /// \name High-Level API
72 /// Perform any late binding of symbols (for example, to assign symbol
73 /// indices for use when generating relocations).
75 /// This routine is called by the assembler after layout and relaxation is
77 virtual void executePostLayoutBinding(MCAssembler &Asm,
78 const MCAsmLayout &Layout) = 0;
80 /// Record a relocation entry.
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
86 virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
87 const MCFragment *Fragment,
88 const MCFixup &Fixup, MCValue Target,
89 uint64_t &FixedValue) = 0;
91 /// Check whether the difference (A - B) between two symbol references is
94 /// Clients are not required to answer precisely and may conservatively return
95 /// false, even when a difference is fully resolved.
96 bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
97 const MCSymbolRefExpr *A,
98 const MCSymbolRefExpr *B,
101 virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
106 virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
107 const MCSymbol &SymA,
108 const MCFragment &FB,
112 /// Write the object file.
114 /// This routine is called by the assembler after layout and relaxation is
115 /// complete, fixups have been evaluated and applied, and relocations
117 virtual void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
120 /// \name Binary Output
123 void write8(uint8_t Value) { *OS << char(Value); }
125 void writeLE16(uint16_t Value) {
126 support::endian::Writer<support::little>(*OS).write(Value);
129 void writeLE32(uint32_t Value) {
130 support::endian::Writer<support::little>(*OS).write(Value);
133 void writeLE64(uint64_t Value) {
134 support::endian::Writer<support::little>(*OS).write(Value);
137 void writeBE16(uint16_t Value) {
138 support::endian::Writer<support::big>(*OS).write(Value);
141 void writeBE32(uint32_t Value) {
142 support::endian::Writer<support::big>(*OS).write(Value);
145 void writeBE64(uint64_t Value) {
146 support::endian::Writer<support::big>(*OS).write(Value);
149 void write16(uint16_t Value) {
156 void write32(uint32_t Value) {
163 void write64(uint64_t Value) {
170 void WriteZeros(unsigned N) {
171 const char Zeros[16] = {0};
173 for (unsigned i = 0, e = N / 16; i != e; ++i)
174 *OS << StringRef(Zeros, 16);
176 *OS << StringRef(Zeros, N % 16);
179 void writeBytes(const SmallVectorImpl<char> &ByteVec,
180 unsigned ZeroFillSize = 0) {
181 writeBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
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>
188 (ZeroFillSize == 0 || Str.size() <= ZeroFillSize) &&
189 "data size greater than fill size, unexpected large write will occur");
192 WriteZeros(ZeroFillSize - Str.size());
198 } // end namespace llvm
200 #endif // LLVM_MC_MCOBJECTWRITER_H