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/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"
18 #include <cassert>
19 
20 namespace llvm {
21 class MCAsmLayout;
22 class MCAssembler;
23 class MCFixup;
24 class MCFragment;
25 class MCSymbolRefExpr;
26 class MCValue;
27 
28 /// Defines the object file and target independent interfaces used by the
29 /// 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 &) = delete;
41   void operator=(const MCObjectWriter &) = delete;
42 
43   raw_pwrite_stream *OS;
44 
45 protected:
46   unsigned IsLittleEndian : 1;
47 
48 protected: // Can only create subclasses.
MCObjectWriter(raw_pwrite_stream & OS,bool IsLittleEndian)49   MCObjectWriter(raw_pwrite_stream &OS, bool IsLittleEndian)
50       : OS(&OS), IsLittleEndian(IsLittleEndian) {}
51 
getInitialOffset()52   unsigned getInitialOffset() {
53     return OS->tell();
54   }
55 
56 public:
57   virtual ~MCObjectWriter();
58 
59   /// lifetime management
reset()60   virtual void reset() {}
61 
isLittleEndian()62   bool isLittleEndian() const { return IsLittleEndian; }
63 
getStream()64   raw_pwrite_stream &getStream() { return *OS; }
setStream(raw_pwrite_stream & NewOS)65   void setStream(raw_pwrite_stream &NewOS) { OS = &NewOS; }
66 
67   /// \name High-Level API
68   /// @{
69 
70   /// Perform any late binding of symbols (for example, to assign symbol
71   /// indices for use when generating relocations).
72   ///
73   /// This routine is called by the assembler after layout and relaxation is
74   /// complete.
75   virtual void executePostLayoutBinding(MCAssembler &Asm,
76                                         const MCAsmLayout &Layout) = 0;
77 
78   /// Record a relocation entry.
79   ///
80   /// This routine is called by the assembler after layout and relaxation, and
81   /// post layout binding. The implementation is responsible for storing
82   /// information about the relocation so that it can be emitted during
83   /// writeObject().
84   virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
85                                 const MCFragment *Fragment,
86                                 const MCFixup &Fixup, MCValue Target,
87                                 bool &IsPCRel, uint64_t &FixedValue) = 0;
88 
89   /// Check whether the difference (A - B) between two symbol references is
90   /// fully resolved.
91   ///
92   /// Clients are not required to answer precisely and may conservatively return
93   /// false, even when a difference is fully resolved.
94   bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
95                                           const MCSymbolRefExpr *A,
96                                           const MCSymbolRefExpr *B,
97                                           bool InSet) const;
98 
99   virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
100                                                       const MCSymbol &A,
101                                                       const MCSymbol &B,
102                                                       bool InSet) const;
103 
104   virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
105                                                       const MCSymbol &SymA,
106                                                       const MCFragment &FB,
107                                                       bool InSet,
108                                                       bool IsPCRel) const;
109 
110   /// True if this symbol (which is a variable) is weak. This is not
111   /// just STB_WEAK, but more generally whether or not we can evaluate
112   /// past it.
113   virtual bool isWeak(const MCSymbol &Sym) const;
114 
115   /// Write the object file.
116   ///
117   /// This routine is called by the assembler after layout and relaxation is
118   /// complete, fixups have been evaluated and applied, and relocations
119   /// generated.
120   virtual void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
121 
122   /// @}
123   /// \name Binary Output
124   /// @{
125 
write8(uint8_t Value)126   void write8(uint8_t Value) { *OS << char(Value); }
127 
writeLE16(uint16_t Value)128   void writeLE16(uint16_t Value) {
129     support::endian::Writer<support::little>(*OS).write(Value);
130   }
131 
writeLE32(uint32_t Value)132   void writeLE32(uint32_t Value) {
133     support::endian::Writer<support::little>(*OS).write(Value);
134   }
135 
writeLE64(uint64_t Value)136   void writeLE64(uint64_t Value) {
137     support::endian::Writer<support::little>(*OS).write(Value);
138   }
139 
writeBE16(uint16_t Value)140   void writeBE16(uint16_t Value) {
141     support::endian::Writer<support::big>(*OS).write(Value);
142   }
143 
writeBE32(uint32_t Value)144   void writeBE32(uint32_t Value) {
145     support::endian::Writer<support::big>(*OS).write(Value);
146   }
147 
writeBE64(uint64_t Value)148   void writeBE64(uint64_t Value) {
149     support::endian::Writer<support::big>(*OS).write(Value);
150   }
151 
write16(uint16_t Value)152   void write16(uint16_t Value) {
153     if (IsLittleEndian)
154       writeLE16(Value);
155     else
156       writeBE16(Value);
157   }
158 
write32(uint32_t Value)159   void write32(uint32_t Value) {
160     if (IsLittleEndian)
161       writeLE32(Value);
162     else
163       writeBE32(Value);
164   }
165 
write64(uint64_t Value)166   void write64(uint64_t Value) {
167     if (IsLittleEndian)
168       writeLE64(Value);
169     else
170       writeBE64(Value);
171   }
172 
WriteZeros(unsigned N)173   void WriteZeros(unsigned N) {
174     const char Zeros[16] = {0};
175 
176     for (unsigned i = 0, e = N / 16; i != e; ++i)
177       *OS << StringRef(Zeros, 16);
178 
179     *OS << StringRef(Zeros, N % 16);
180   }
181 
182   void writeBytes(const SmallVectorImpl<char> &ByteVec,
183                   unsigned ZeroFillSize = 0) {
184     writeBytes(StringRef(ByteVec.data(), ByteVec.size()), ZeroFillSize);
185   }
186 
187   void writeBytes(StringRef Str, unsigned ZeroFillSize = 0) {
188     // TODO: this version may need to go away once all fragment contents are
189     // converted to SmallVector<char, N>
190     assert(
191         (ZeroFillSize == 0 || Str.size() <= ZeroFillSize) &&
192         "data size greater than fill size, unexpected large write will occur");
193     *OS << Str;
194     if (ZeroFillSize)
195       WriteZeros(ZeroFillSize - Str.size());
196   }
197 
198   /// @}
199 };
200 
201 } // End llvm namespace
202 
203 #endif
204