977b504a6ef99a68f0a202ae9daea970108e7c5a
[oota-llvm.git] / include / llvm / MC / MCObjectWriter.h
1 //===-- llvm/MC/MCObjectWriter.h - Object File Writer Interface -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_MC_MCOBJECTWRITER_H
11 #define LLVM_MC_MCOBJECTWRITER_H
12
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Support/DataTypes.h"
16 #include <cassert>
17
18 namespace llvm {
19 class MCAsmLayout;
20 class MCAssembler;
21 class MCFixup;
22 class MCFragment;
23 class MCSymbol;
24 class MCSymbolRefExpr;
25 class MCValue;
26 class raw_ostream;
27
28 /// MCObjectWriter - Defines the object file and target independent interfaces
29 /// used by the assembler backend to write native file format object files.
30 ///
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().
36 ///
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 &); // DO NOT IMPLEMENT
41   void operator=(const MCObjectWriter &); // DO NOT IMPLEMENT
42
43 protected:
44   raw_ostream &OS;
45
46   unsigned IsLittleEndian : 1;
47
48 protected: // Can only create subclasses.
49   MCObjectWriter(raw_ostream &_OS, bool _IsLittleEndian)
50     : OS(_OS), IsLittleEndian(_IsLittleEndian) {}
51
52 public:
53   virtual ~MCObjectWriter();
54
55   bool isLittleEndian() const { return IsLittleEndian; }
56
57   raw_ostream &getStream() { return OS; }
58
59   /// @name High-Level API
60   /// @{
61
62   /// Perform any late binding of symbols (for example, to assign symbol indices
63   /// for use when generating relocations).
64   ///
65   /// This routine is called by the assembler after layout and relaxation is
66   /// complete.
67   virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
68                                         const MCAsmLayout &Layout) = 0;
69
70   /// Record a relocation entry.
71   ///
72   /// This routine is called by the assembler after layout and relaxation, and
73   /// post layout binding. The implementation is responsible for storing
74   /// information about the relocation so that it can be emitted during
75   /// WriteObject().
76   virtual void RecordRelocation(const MCAssembler &Asm,
77                                 const MCAsmLayout &Layout,
78                                 const MCFragment *Fragment,
79                                 const MCFixup &Fixup, MCValue Target,
80                                 uint64_t &FixedValue) = 0;
81
82   /// \brief Check whether the difference (A - B) between two symbol
83   /// references is fully resolved.
84   ///
85   /// Clients are not required to answer precisely and may conservatively return
86   /// false, even when a difference is fully resolved.
87   virtual bool
88   IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
89                                      const MCSymbolRefExpr *A,
90                                      const MCSymbolRefExpr *B,
91                                      bool InSet) const;
92
93   /// Check if a fixup is fully resolved.
94   ///
95   /// This routine is used by the assembler to let the file format decide
96   /// if a fixup is not fully resolved. For example, one that crosses
97   /// two sections on ELF.
98   virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
99                                     const MCValue Target,
100                                     bool IsPCRel,
101                                     const MCFragment *DF) const = 0;
102
103   /// Write the object file.
104   ///
105   /// This routine is called by the assembler after layout and relaxation is
106   /// complete, fixups have been evaluated and applied, and relocations
107   /// generated.
108   virtual void WriteObject(MCAssembler &Asm,
109                            const MCAsmLayout &Layout) = 0;
110
111   /// @}
112   /// @name Binary Output
113   /// @{
114
115   void Write8(uint8_t Value) {
116     OS << char(Value);
117   }
118
119   void WriteLE16(uint16_t Value) {
120     Write8(uint8_t(Value >> 0));
121     Write8(uint8_t(Value >> 8));
122   }
123
124   void WriteLE32(uint32_t Value) {
125     WriteLE16(uint16_t(Value >> 0));
126     WriteLE16(uint16_t(Value >> 16));
127   }
128
129   void WriteLE64(uint64_t Value) {
130     WriteLE32(uint32_t(Value >> 0));
131     WriteLE32(uint32_t(Value >> 32));
132   }
133
134   void WriteBE16(uint16_t Value) {
135     Write8(uint8_t(Value >> 8));
136     Write8(uint8_t(Value >> 0));
137   }
138
139   void WriteBE32(uint32_t Value) {
140     WriteBE16(uint16_t(Value >> 16));
141     WriteBE16(uint16_t(Value >> 0));
142   }
143
144   void WriteBE64(uint64_t Value) {
145     WriteBE32(uint32_t(Value >> 32));
146     WriteBE32(uint32_t(Value >> 0));
147   }
148
149   void Write16(uint16_t Value) {
150     if (IsLittleEndian)
151       WriteLE16(Value);
152     else
153       WriteBE16(Value);
154   }
155
156   void Write32(uint32_t Value) {
157     if (IsLittleEndian)
158       WriteLE32(Value);
159     else
160       WriteBE32(Value);
161   }
162
163   void Write64(uint64_t Value) {
164     if (IsLittleEndian)
165       WriteLE64(Value);
166     else
167       WriteBE64(Value);
168   }
169
170   void WriteZeros(unsigned N) {
171     const char Zeros[16] = { 0 };
172
173     for (unsigned i = 0, e = N / 16; i != e; ++i)
174       OS << StringRef(Zeros, 16);
175
176     OS << StringRef(Zeros, N % 16);
177   }
178
179   void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
180     assert((ZeroFillSize == 0 || Str.size () <= ZeroFillSize) &&
181       "data size greater than fill size, unexpected large write will occur");
182     OS << Str;
183     if (ZeroFillSize)
184       WriteZeros(ZeroFillSize - Str.size());
185   }
186
187   /// @}
188
189   /// Utility function to encode a SLEB128 value.
190   static void EncodeSLEB128(int64_t Value, raw_ostream &OS);
191   /// Utility function to encode a ULEB128 value.
192   static void EncodeULEB128(uint64_t Value, raw_ostream &OS);
193 };
194
195 MCObjectWriter *createWinCOFFObjectWriter(raw_ostream &OS, bool is64Bit);
196
197 } // End llvm namespace
198
199 #endif