Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/ADT/StringRef.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 15 | #include "llvm/ADT/Triple.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
| 17 | #include "llvm/Support/EndianStream.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | #include <cassert> |
| 20 | #include <cstdint> |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | class MCAsmLayout; |
| 25 | class MCAssembler; |
| 26 | class MCFixup; |
| 27 | class MCFragment; |
| 28 | class MCSymbol; |
| 29 | class MCSymbolRefExpr; |
| 30 | class MCValue; |
| 31 | |
| 32 | /// Defines the object file and target independent interfaces used by the |
| 33 | /// assembler backend to write native file format object files. |
| 34 | /// |
| 35 | /// The object writer contains a few callbacks used by the assembler to allow |
| 36 | /// the object writer to modify the assembler data structures at appropriate |
| 37 | /// points. Once assembly is complete, the object writer is given the |
| 38 | /// MCAssembler instance, which contains all the symbol and section data which |
| 39 | /// should be emitted as part of writeObject(). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 40 | class MCObjectWriter { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | protected: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 42 | MCObjectWriter() = default; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 43 | |
| 44 | public: |
| 45 | MCObjectWriter(const MCObjectWriter &) = delete; |
| 46 | MCObjectWriter &operator=(const MCObjectWriter &) = delete; |
| 47 | virtual ~MCObjectWriter(); |
| 48 | |
| 49 | /// lifetime management |
| 50 | virtual void reset() {} |
| 51 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | /// \name High-Level API |
| 53 | /// @{ |
| 54 | |
| 55 | /// Perform any late binding of symbols (for example, to assign symbol |
| 56 | /// indices for use when generating relocations). |
| 57 | /// |
| 58 | /// This routine is called by the assembler after layout and relaxation is |
| 59 | /// complete. |
| 60 | virtual void executePostLayoutBinding(MCAssembler &Asm, |
| 61 | const MCAsmLayout &Layout) = 0; |
| 62 | |
| 63 | /// Record a relocation entry. |
| 64 | /// |
| 65 | /// This routine is called by the assembler after layout and relaxation, and |
| 66 | /// post layout binding. The implementation is responsible for storing |
| 67 | /// information about the relocation so that it can be emitted during |
| 68 | /// writeObject(). |
| 69 | virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, |
| 70 | const MCFragment *Fragment, |
| 71 | const MCFixup &Fixup, MCValue Target, |
| 72 | uint64_t &FixedValue) = 0; |
| 73 | |
| 74 | /// Check whether the difference (A - B) between two symbol references is |
| 75 | /// fully resolved. |
| 76 | /// |
| 77 | /// Clients are not required to answer precisely and may conservatively return |
| 78 | /// false, even when a difference is fully resolved. |
| 79 | bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm, |
| 80 | const MCSymbolRefExpr *A, |
| 81 | const MCSymbolRefExpr *B, |
| 82 | bool InSet) const; |
| 83 | |
| 84 | virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
| 85 | const MCSymbol &A, |
| 86 | const MCSymbol &B, |
| 87 | bool InSet) const; |
| 88 | |
| 89 | virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
| 90 | const MCSymbol &SymA, |
| 91 | const MCFragment &FB, |
| 92 | bool InSet, |
| 93 | bool IsPCRel) const; |
| 94 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 95 | /// Tell the object writer to emit an address-significance table during |
| 96 | /// writeObject(). If this function is not called, all symbols are treated as |
| 97 | /// address-significant. |
| 98 | virtual void emitAddrsigSection() {} |
| 99 | |
| 100 | /// Record the given symbol in the address-significance table to be written |
| 101 | /// diring writeObject(). |
| 102 | virtual void addAddrsigSymbol(const MCSymbol *Sym) {} |
| 103 | |
| 104 | /// Write the object file and returns the number of bytes written. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 105 | /// |
| 106 | /// This routine is called by the assembler after layout and relaxation is |
| 107 | /// complete, fixups have been evaluated and applied, and relocations |
| 108 | /// generated. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 109 | virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 110 | |
| 111 | /// @} |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 112 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 113 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 114 | /// Base class for classes that define behaviour that is specific to both the |
| 115 | /// target and the object format. |
| 116 | class MCObjectTargetWriter { |
| 117 | public: |
| 118 | virtual ~MCObjectTargetWriter() = default; |
| 119 | virtual Triple::ObjectFormatType getFormat() const = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // end namespace llvm |
| 123 | |
| 124 | #endif // LLVM_MC_MCOBJECTWRITER_H |