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"
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,
66 const MCAsmLayout &Layout) = 0;
68 /// Record a relocation entry.
70 /// This routine is called by the assembler after layout and relaxation, and
71 /// post layout binding. The implementation is responsible for storing
72 /// information about the relocation so that it can be emitted during
74 virtual void RecordRelocation(const MCAssembler &Asm,
75 const MCAsmLayout &Layout,
76 const MCFragment *Fragment,
77 const MCFixup &Fixup, MCValue Target,
78 uint64_t &FixedValue) = 0;
80 /// Check if a fixup is fully resolved.
82 /// This routine is used by the assembler to let the file format decide
83 /// if a fixup is not fully resolved. For example, one that crosses
84 /// two sections on ELF.
85 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
88 const MCFragment *DF) const = 0;
90 /// Write the object file.
92 /// This routine is called by the assembler after layout and relaxation is
93 /// complete, fixups have been evaluated and applied, and relocations
95 virtual void WriteObject(MCAssembler &Asm,
96 const MCAsmLayout &Layout) = 0;
99 /// @name Binary Output
102 void Write8(uint8_t Value) {
106 void WriteLE16(uint16_t Value) {
107 Write8(uint8_t(Value >> 0));
108 Write8(uint8_t(Value >> 8));
111 void WriteLE32(uint32_t Value) {
112 WriteLE16(uint16_t(Value >> 0));
113 WriteLE16(uint16_t(Value >> 16));
116 void WriteLE64(uint64_t Value) {
117 WriteLE32(uint32_t(Value >> 0));
118 WriteLE32(uint32_t(Value >> 32));
121 void WriteBE16(uint16_t Value) {
122 Write8(uint8_t(Value >> 8));
123 Write8(uint8_t(Value >> 0));
126 void WriteBE32(uint32_t Value) {
127 WriteBE16(uint16_t(Value >> 16));
128 WriteBE16(uint16_t(Value >> 0));
131 void WriteBE64(uint64_t Value) {
132 WriteBE32(uint32_t(Value >> 32));
133 WriteBE32(uint32_t(Value >> 0));
136 void Write16(uint16_t Value) {
143 void Write32(uint32_t Value) {
150 void Write64(uint64_t Value) {
157 void WriteZeros(unsigned N) {
158 const char Zeros[16] = { 0 };
160 for (unsigned i = 0, e = N / 16; i != e; ++i)
161 OS << StringRef(Zeros, 16);
163 OS << StringRef(Zeros, N % 16);
166 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
167 assert((ZeroFillSize == 0 || Str.size () <= ZeroFillSize) &&
168 "data size greater than fill size, unexpected large write will occur");
171 WriteZeros(ZeroFillSize - Str.size());
176 /// Utility function to encode a SLEB128 value.
177 static void EncodeSLEB128(int64_t Value, raw_ostream &OS);
178 /// Utility function to encode a ULEB128 value.
179 static void EncodeULEB128(uint64_t Value, raw_ostream &OS);
182 MCObjectWriter *createMachObjectWriter(raw_ostream &OS, bool is64Bit,
183 uint32_t CPUType, uint32_t CPUSubtype,
184 bool IsLittleEndian);
185 MCObjectWriter *createELFObjectWriter(raw_ostream &OS, bool is64Bit,
186 Triple::OSType OSType, uint16_t EMachine,
188 bool HasRelocationAddend);
189 MCObjectWriter *createWinCOFFObjectWriter(raw_ostream &OS, bool is64Bit);
191 } // End llvm namespace