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/Support/raw_ostream.h"
14 #include "llvm/System/DataTypes.h"
24 /// MCObjectWriter - Defines the object file and target independent interfaces
25 /// used by the assembler backend to write native file format object files.
27 /// The object writer contains a few callbacks used by the assembler to allow
28 /// the object writer to modify the assembler data structures at appropriate
29 /// points. Once assembly is complete, the object writer is given the
30 /// MCAssembler instance, which contains all the symbol and section data which
31 /// should be emitted as part of WriteObject().
33 /// The object writer also contains a number of helper methods for writing
34 /// binary data to the output stream.
35 class MCObjectWriter {
36 MCObjectWriter(const MCObjectWriter &); // DO NOT IMPLEMENT
37 void operator=(const MCObjectWriter &); // DO NOT IMPLEMENT
42 unsigned IsLittleEndian : 1;
44 protected: // Can only create subclasses.
45 MCObjectWriter(raw_ostream &_OS, bool _IsLittleEndian)
46 : OS(_OS), IsLittleEndian(_IsLittleEndian) {}
49 virtual ~MCObjectWriter();
51 bool isLittleEndian() { return IsLittleEndian; }
53 raw_ostream &getStream() { return OS; }
55 /// @name High-Level API
58 /// Perform any late binding of symbols (for example, to assign symbol indices
59 /// for use when generating relocations).
61 /// This routine is called by the assembler after layout and relaxation is
63 virtual void ExecutePostLayoutBinding(MCAssembler &Asm) = 0;
65 /// Record a relocation entry.
67 /// This routine is called by the assembler after layout and relaxation, and
68 /// post layout binding. The implementation is responsible for storing
69 /// information about the relocation so that it can be emitted during
71 virtual void RecordRelocation(const MCAssembler &Asm,
72 const MCDataFragment &Fragment,
73 const MCAsmFixup &Fixup, MCValue Target,
74 uint64_t &FixedValue) = 0;
76 /// Write the object file.
78 /// This routine is called by the assembler after layout and relaxation is
79 /// complete, fixups have been evaluate and applied, and relocations
81 virtual void WriteObject(const MCAssembler &Asm) = 0;
84 /// @name Binary Output
87 void Write8(uint8_t Value) {
91 void WriteLE16(uint16_t Value) {
92 Write8(uint8_t(Value >> 0));
93 Write8(uint8_t(Value >> 8));
96 void WriteLE32(uint32_t Value) {
97 WriteLE16(uint16_t(Value >> 0));
98 WriteLE16(uint16_t(Value >> 16));
101 void WriteLE64(uint64_t Value) {
102 WriteLE32(uint32_t(Value >> 0));
103 WriteLE32(uint32_t(Value >> 32));
106 void WriteBE16(uint16_t Value) {
107 Write8(uint8_t(Value >> 8));
108 Write8(uint8_t(Value >> 0));
111 void WriteBE32(uint32_t Value) {
112 WriteBE16(uint16_t(Value >> 16));
113 WriteBE16(uint16_t(Value >> 0));
116 void WriteBE64(uint64_t Value) {
117 WriteBE32(uint32_t(Value >> 32));
118 WriteBE32(uint32_t(Value >> 0));
121 void Write16(uint16_t Value) {
128 void Write32(uint32_t Value) {
135 void Write64(uint64_t Value) {
142 void WriteZeros(unsigned N) {
143 const char Zeros[16] = { 0 };
145 for (unsigned i = 0, e = N / 16; i != e; ++i)
146 OS << StringRef(Zeros, 16);
148 OS << StringRef(Zeros, N % 16);
151 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
154 WriteZeros(ZeroFillSize - Str.size());
160 } // End llvm namespace