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/Support/Compiler.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/EndianStream.h"
17 #include "llvm/Support/raw_ostream.h"
26 class MCSymbolRefExpr;
29 /// Defines the object file and target independent interfaces used by the
30 /// assembler backend to write native file format object files.
32 /// The object writer contains a few callbacks used by the assembler to allow
33 /// the object writer to modify the assembler data structures at appropriate
34 /// points. Once assembly is complete, the object writer is given the
35 /// MCAssembler instance, which contains all the symbol and section data which
36 /// should be emitted as part of writeObject().
38 /// The object writer also contains a number of helper methods for writing
39 /// binary data to the output stream.
40 class MCObjectWriter {
41 MCObjectWriter(const MCObjectWriter &) = delete;
42 void operator=(const MCObjectWriter &) = delete;
44 raw_pwrite_stream *OS;
47 unsigned IsLittleEndian : 1;
49 protected: // Can only create subclasses.
50 MCObjectWriter(raw_pwrite_stream &OS, bool IsLittleEndian)
51 : OS(&OS), IsLittleEndian(IsLittleEndian) {}
53 unsigned getInitialOffset() {
58 virtual ~MCObjectWriter();
60 /// lifetime management
61 virtual void reset() {}
63 bool isLittleEndian() const { return IsLittleEndian; }
65 raw_pwrite_stream &getStream() { return *OS; }
66 void setStream(raw_pwrite_stream &NewOS) { OS = &NewOS; }
68 /// \name High-Level API
71 /// Perform any late binding of symbols (for example, to assign symbol
72 /// indices for use when generating relocations).
74 /// This routine is called by the assembler after layout and relaxation is
76 virtual void executePostLayoutBinding(MCAssembler &Asm,
77 const MCAsmLayout &Layout) = 0;
79 /// Record a relocation entry.
81 /// This routine is called by the assembler after layout and relaxation, and
82 /// post layout binding. The implementation is responsible for storing
83 /// information about the relocation so that it can be emitted during
85 virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
86 const MCFragment *Fragment,
87 const MCFixup &Fixup, MCValue Target,
88 bool &IsPCRel, uint64_t &FixedValue) = 0;
90 /// Check whether the difference (A - B) between two symbol references is
93 /// Clients are not required to answer precisely and may conservatively return
94 /// false, even when a difference is fully resolved.
95 bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
96 const MCSymbolRefExpr *A,
97 const MCSymbolRefExpr *B,
100 virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
105 virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
106 const MCSymbol &SymA,
107 const MCFragment &FB,
111 /// True if this symbol (which is a variable) is weak. This is not
112 /// just STB_WEAK, but more generally whether or not we can evaluate
114 virtual bool isWeak(const MCSymbol &Sym) const;
116 /// Write the object file.
118 /// This routine is called by the assembler after layout and relaxation is
119 /// complete, fixups have been evaluated and applied, and relocations
121 virtual void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
124 /// \name Binary Output
127 void write8(uint8_t Value) { *OS << char(Value); }
129 void writeLE16(uint16_t Value) {
130 support::endian::Writer<support::little>(*OS).write(Value);
133 void writeLE32(uint32_t Value) {
134 support::endian::Writer<support::little>(*OS).write(Value);
137 void writeLE64(uint64_t Value) {
138 support::endian::Writer<support::little>(*OS).write(Value);
141 void writeBE16(uint16_t Value) {
142 support::endian::Writer<support::big>(*OS).write(Value);
145 void writeBE32(uint32_t Value) {
146 support::endian::Writer<support::big>(*OS).write(Value);
149 void writeBE64(uint64_t Value) {
150 support::endian::Writer<support::big>(*OS).write(Value);
153 void write16(uint16_t Value) {
160 void write32(uint32_t Value) {
167 void write64(uint64_t Value) {
174 void WriteZeros(unsigned N) {
175 const char Zeros[16] = {0};
177 for (unsigned i = 0, e = N / 16; i != e; ++i)
178 *OS << StringRef(Zeros, 16);
180 *OS << StringRef(Zeros, N % 16);
183 void writeBytes(const SmallVectorImpl<char> &ByteVec,
184 unsigned ZeroFillSize = 0) {
185 writeBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
188 void writeBytes(StringRef Str, unsigned ZeroFillSize = 0) {
189 // TODO: this version may need to go away once all fragment contents are
190 // converted to SmallVector<char, N>
192 (ZeroFillSize == 0 || Str.size() <= ZeroFillSize) &&
193 "data size greater than fill size, unexpected large write will occur");
196 WriteZeros(ZeroFillSize - Str.size());
202 } // End llvm namespace