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/System/DataTypes.h"
26 /// MCObjectWriter - Defines the object file and target independent interfaces
27 /// used by the assembler backend to write native file format object files.
29 /// The object writer contains a few callbacks used by the assembler to allow
30 /// the object writer to modify the assembler data structures at appropriate
31 /// points. Once assembly is complete, the object writer is given the
32 /// MCAssembler instance, which contains all the symbol and section data which
33 /// should be emitted as part of WriteObject().
35 /// The object writer also contains a number of helper methods for writing
36 /// binary data to the output stream.
37 class MCObjectWriter {
38 MCObjectWriter(const MCObjectWriter &); // DO NOT IMPLEMENT
39 void operator=(const MCObjectWriter &); // DO NOT IMPLEMENT
44 unsigned IsLittleEndian : 1;
46 protected: // Can only create subclasses.
47 MCObjectWriter(raw_ostream &_OS, bool _IsLittleEndian)
48 : OS(_OS), IsLittleEndian(_IsLittleEndian) {}
51 virtual ~MCObjectWriter();
53 bool isLittleEndian() const { return IsLittleEndian; }
55 raw_ostream &getStream() { return OS; }
57 /// @name High-Level API
60 /// Perform any late binding of symbols (for example, to assign symbol indices
61 /// for use when generating relocations).
63 /// This routine is called by the assembler after layout and relaxation is
65 virtual void ExecutePostLayoutBinding(MCAssembler &Asm) = 0;
67 /// Record a relocation entry.
69 /// This routine is called by the assembler after layout and relaxation, and
70 /// post layout binding. The implementation is responsible for storing
71 /// information about the relocation so that it can be emitted during
73 virtual void RecordRelocation(const MCAssembler &Asm,
74 const MCAsmLayout &Layout,
75 const MCFragment *Fragment,
76 const MCFixup &Fixup, MCValue Target,
77 uint64_t &FixedValue) = 0;
79 /// Check if a fixup is fully resolved.
81 /// This routine is used by the assembler to let the file format decide
82 /// if a fixup is not fully resolved. For example, one that crosses
83 /// two sections on ELF.
84 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
87 const MCFragment *DF) const = 0;
89 /// Write the object file.
91 /// This routine is called by the assembler after layout and relaxation is
92 /// complete, fixups have been evaluated and applied, and relocations
94 virtual void WriteObject(MCAssembler &Asm,
95 const MCAsmLayout &Layout) = 0;
98 /// @name Binary Output
101 void Write8(uint8_t Value) {
105 void WriteLE16(uint16_t Value) {
106 Write8(uint8_t(Value >> 0));
107 Write8(uint8_t(Value >> 8));
110 void WriteLE32(uint32_t Value) {
111 WriteLE16(uint16_t(Value >> 0));
112 WriteLE16(uint16_t(Value >> 16));
115 void WriteLE64(uint64_t Value) {
116 WriteLE32(uint32_t(Value >> 0));
117 WriteLE32(uint32_t(Value >> 32));
120 void WriteBE16(uint16_t Value) {
121 Write8(uint8_t(Value >> 8));
122 Write8(uint8_t(Value >> 0));
125 void WriteBE32(uint32_t Value) {
126 WriteBE16(uint16_t(Value >> 16));
127 WriteBE16(uint16_t(Value >> 0));
130 void WriteBE64(uint64_t Value) {
131 WriteBE32(uint32_t(Value >> 32));
132 WriteBE32(uint32_t(Value >> 0));
135 void Write16(uint16_t Value) {
142 void Write32(uint32_t Value) {
149 void Write64(uint64_t Value) {
156 void WriteZeros(unsigned N) {
157 const char Zeros[16] = { 0 };
159 for (unsigned i = 0, e = N / 16; i != e; ++i)
160 OS << StringRef(Zeros, 16);
162 OS << StringRef(Zeros, N % 16);
165 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
166 assert((ZeroFillSize == 0 || Str.size () <= ZeroFillSize) &&
167 "data size greater than fill size, unexpected large write will occur");
170 WriteZeros(ZeroFillSize - Str.size());
175 /// Utility function to encode a SLEB128 value.
176 static void EncodeSLEB128(int64_t Value, raw_ostream &OS);
177 /// Utility function to encode a ULEB128 value.
178 static void EncodeULEB128(uint64_t Value, raw_ostream &OS);
181 MCObjectWriter *createMachObjectWriter(raw_ostream &OS, bool is64Bit,
182 uint32_t CPUType, uint32_t CPUSubtype,
183 bool IsLittleEndian);
184 MCObjectWriter *createELFObjectWriter(raw_ostream &OS, bool is64Bit,
185 Triple::OSType OSType, uint16_t EMachine,
187 bool HasRelocationAddend);
188 MCObjectWriter *createWinCOFFObjectWriter(raw_ostream &OS, bool is64Bit);
190 } // End llvm namespace