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/Support/Compiler.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/raw_ostream.h"
24 class MCSymbolRefExpr;
27 /// MCObjectWriter - Defines the object file and target independent interfaces
28 /// used by the assembler backend to write native file format object files.
30 /// The object writer contains a few callbacks used by the assembler to allow
31 /// the object writer to modify the assembler data structures at appropriate
32 /// points. Once assembly is complete, the object writer is given the
33 /// MCAssembler instance, which contains all the symbol and section data which
34 /// should be emitted as part of WriteObject().
36 /// The object writer also contains a number of helper methods for writing
37 /// binary data to the output stream.
38 class MCObjectWriter {
39 MCObjectWriter(const MCObjectWriter &) LLVM_DELETED_FUNCTION;
40 void operator=(const MCObjectWriter &) LLVM_DELETED_FUNCTION;
45 unsigned IsLittleEndian : 1;
47 protected: // Can only create subclasses.
48 MCObjectWriter(raw_ostream &_OS, bool _IsLittleEndian)
49 : OS(_OS), IsLittleEndian(_IsLittleEndian) {}
52 virtual ~MCObjectWriter();
54 /// lifetime management
55 virtual void reset() { }
57 bool isLittleEndian() const { return IsLittleEndian; }
59 raw_ostream &getStream() { return OS; }
61 /// @name High-Level API
64 /// Perform any late binding of symbols (for example, to assign symbol indices
65 /// for use when generating relocations).
67 /// This routine is called by the assembler after layout and relaxation is
69 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
70 const MCAsmLayout &Layout) = 0;
72 /// Record a relocation entry.
74 /// This routine is called by the assembler after layout and relaxation, and
75 /// post layout binding. The implementation is responsible for storing
76 /// information about the relocation so that it can be emitted during
78 virtual void RecordRelocation(const MCAssembler &Asm,
79 const MCAsmLayout &Layout,
80 const MCFragment *Fragment,
81 const MCFixup &Fixup, MCValue Target,
82 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.
90 IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
91 const MCSymbolRefExpr *A,
92 const MCSymbolRefExpr *B,
96 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
97 const MCSymbolData &DataA,
103 /// Write the object file.
105 /// This routine is called by the assembler after layout and relaxation is
106 /// complete, fixups have been evaluated and applied, and relocations
108 virtual void WriteObject(MCAssembler &Asm,
109 const MCAsmLayout &Layout) = 0;
112 /// @name Binary Output
115 void Write8(uint8_t Value) {
119 void WriteLE16(uint16_t Value) {
120 Write8(uint8_t(Value >> 0));
121 Write8(uint8_t(Value >> 8));
124 void WriteLE32(uint32_t Value) {
125 WriteLE16(uint16_t(Value >> 0));
126 WriteLE16(uint16_t(Value >> 16));
129 void WriteLE64(uint64_t Value) {
130 WriteLE32(uint32_t(Value >> 0));
131 WriteLE32(uint32_t(Value >> 32));
134 void WriteBE16(uint16_t Value) {
135 Write8(uint8_t(Value >> 8));
136 Write8(uint8_t(Value >> 0));
139 void WriteBE32(uint32_t Value) {
140 WriteBE16(uint16_t(Value >> 16));
141 WriteBE16(uint16_t(Value >> 0));
144 void WriteBE64(uint64_t Value) {
145 WriteBE32(uint32_t(Value >> 32));
146 WriteBE32(uint32_t(Value >> 0));
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(SmallVectorImpl<char> &ByteVec, unsigned ZeroFillSize = 0) {
180 WriteBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
183 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
184 // TODO: this version may need to go away once all fragment contents are
185 // converted to SmallVector<char, N>
186 assert((ZeroFillSize == 0 || Str.size () <= ZeroFillSize) &&
187 "data size greater than fill size, unexpected large write will occur");
190 WriteZeros(ZeroFillSize - Str.size());
197 } // End llvm namespace