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/Triple.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Support/DataTypes.h"
25 class MCSymbolRefExpr;
29 /// MCObjectWriter - Defines the object file and target independent interfaces
30 /// used by the 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 &); // DO NOT IMPLEMENT
42 void operator=(const MCObjectWriter &); // DO NOT IMPLEMENT
47 unsigned IsLittleEndian : 1;
49 protected: // Can only create subclasses.
50 MCObjectWriter(raw_ostream &_OS, bool _IsLittleEndian)
51 : OS(_OS), IsLittleEndian(_IsLittleEndian) {}
54 virtual ~MCObjectWriter();
56 bool isLittleEndian() const { return IsLittleEndian; }
58 raw_ostream &getStream() { return OS; }
60 /// @name High-Level API
63 /// Perform any late binding of symbols (for example, to assign symbol indices
64 /// for use when generating relocations).
66 /// This routine is called by the assembler after layout and relaxation is
68 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
69 const MCAsmLayout &Layout) = 0;
71 /// Record a relocation entry.
73 /// This routine is called by the assembler after layout and relaxation, and
74 /// post layout binding. The implementation is responsible for storing
75 /// information about the relocation so that it can be emitted during
77 virtual void RecordRelocation(const MCAssembler &Asm,
78 const MCAsmLayout &Layout,
79 const MCFragment *Fragment,
80 const MCFixup &Fixup, MCValue Target,
81 uint64_t &FixedValue) = 0;
83 /// \brief Check whether the difference (A - B) between two symbol
84 /// references is fully resolved.
86 /// Clients are not required to answer precisely and may conservatively return
87 /// false, even when a difference is fully resolved.
89 IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
90 const MCSymbolRefExpr *A,
91 const MCSymbolRefExpr *B,
95 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
96 const MCSymbolData &DataA,
102 /// Write the object file.
104 /// This routine is called by the assembler after layout and relaxation is
105 /// complete, fixups have been evaluated and applied, and relocations
107 virtual void WriteObject(MCAssembler &Asm,
108 const MCAsmLayout &Layout) = 0;
111 /// @name Binary Output
114 void Write8(uint8_t Value) {
118 void WriteLE16(uint16_t Value) {
119 Write8(uint8_t(Value >> 0));
120 Write8(uint8_t(Value >> 8));
123 void WriteLE32(uint32_t Value) {
124 WriteLE16(uint16_t(Value >> 0));
125 WriteLE16(uint16_t(Value >> 16));
128 void WriteLE64(uint64_t Value) {
129 WriteLE32(uint32_t(Value >> 0));
130 WriteLE32(uint32_t(Value >> 32));
133 void WriteBE16(uint16_t Value) {
134 Write8(uint8_t(Value >> 8));
135 Write8(uint8_t(Value >> 0));
138 void WriteBE32(uint32_t Value) {
139 WriteBE16(uint16_t(Value >> 16));
140 WriteBE16(uint16_t(Value >> 0));
143 void WriteBE64(uint64_t Value) {
144 WriteBE32(uint32_t(Value >> 32));
145 WriteBE32(uint32_t(Value >> 0));
148 void Write16(uint16_t Value) {
155 void Write32(uint32_t Value) {
162 void Write64(uint64_t Value) {
169 void WriteZeros(unsigned N) {
170 const char Zeros[16] = { 0 };
172 for (unsigned i = 0, e = N / 16; i != e; ++i)
173 OS << StringRef(Zeros, 16);
175 OS << StringRef(Zeros, N % 16);
178 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
179 assert((ZeroFillSize == 0 || Str.size () <= ZeroFillSize) &&
180 "data size greater than fill size, unexpected large write will occur");
183 WriteZeros(ZeroFillSize - Str.size());
188 /// Utility function to encode a SLEB128 value.
189 static void EncodeSLEB128(int64_t Value, raw_ostream &OS);
190 /// Utility function to encode a ULEB128 value.
191 static void EncodeULEB128(uint64_t Value, raw_ostream &OS);
194 MCObjectWriter *createWinCOFFObjectWriter(raw_ostream &OS, bool is64Bit);
196 } // End llvm namespace