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"
25 class MCSymbolRefExpr;
28 /// MCObjectWriter - Defines the object file and target independent interfaces
29 /// used by the assembler backend to write native file format object files.
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().
37 /// The object writer also contains a number of helper methods for writing
38 /// binary data to the output stream.
39 class MCObjectWriter {
40 MCObjectWriter(const MCObjectWriter &) = delete;
41 void operator=(const MCObjectWriter &) = delete;
44 raw_pwrite_stream &OS;
46 unsigned IsLittleEndian : 1;
48 protected: // Can only create subclasses.
49 MCObjectWriter(raw_pwrite_stream &OS, bool IsLittleEndian)
50 : OS(OS), IsLittleEndian(IsLittleEndian) {}
53 virtual ~MCObjectWriter();
55 /// lifetime management
56 virtual void reset() { }
58 bool isLittleEndian() const { return IsLittleEndian; }
60 raw_ostream &getStream() { return OS; }
62 /// \name High-Level API
65 /// \brief Perform any late binding of symbols (for example, to assign symbol
66 /// indices for use when generating relocations).
68 /// This routine is called by the assembler after layout and relaxation is
70 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
71 const MCAsmLayout &Layout) = 0;
73 /// \brief Record a relocation entry.
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
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;
84 /// \brief Check whether the difference (A - B) between two symbol
85 /// references is fully resolved.
87 /// Clients are not required to answer precisely and may conservatively return
88 /// false, even when a difference is fully resolved.
89 bool IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
90 const MCSymbolRefExpr *A,
91 const MCSymbolRefExpr *B,
94 virtual bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
100 /// \brief 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
103 virtual bool isWeak(const MCSymbol &Sym) const;
105 /// \brief Write the object file.
107 /// This routine is called by the assembler after layout and relaxation is
108 /// complete, fixups have been evaluated and applied, and relocations
110 virtual void WriteObject(MCAssembler &Asm,
111 const MCAsmLayout &Layout) = 0;
114 /// \name Binary Output
117 void Write8(uint8_t Value) {
121 void WriteLE16(uint16_t Value) {
122 support::endian::Writer<support::little>(OS).write(Value);
125 void WriteLE32(uint32_t Value) {
126 support::endian::Writer<support::little>(OS).write(Value);
129 void WriteLE64(uint64_t Value) {
130 support::endian::Writer<support::little>(OS).write(Value);
133 void WriteBE16(uint16_t Value) {
134 support::endian::Writer<support::big>(OS).write(Value);
137 void WriteBE32(uint32_t Value) {
138 support::endian::Writer<support::big>(OS).write(Value);
141 void WriteBE64(uint64_t Value) {
142 support::endian::Writer<support::big>(OS).write(Value);
145 void Write16(uint16_t Value) {
152 void Write32(uint32_t Value) {
159 void Write64(uint64_t Value) {
166 void WriteZeros(unsigned N) {
167 const char Zeros[16] = { 0 };
169 for (unsigned i = 0, e = N / 16; i != e; ++i)
170 OS << StringRef(Zeros, 16);
172 OS << StringRef(Zeros, N % 16);
175 void WriteBytes(const SmallVectorImpl<char> &ByteVec, unsigned ZeroFillSize = 0) {
176 WriteBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
179 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
180 // TODO: this version may need to go away once all fragment contents are
181 // converted to SmallVector<char, N>
182 assert((ZeroFillSize == 0 || Str.size () <= ZeroFillSize) &&
183 "data size greater than fill size, unexpected large write will occur");
186 WriteZeros(ZeroFillSize - Str.size());
193 } // End llvm namespace