blob: ddc2301c04c13d1958470e246d0b8d9d30b4932d [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/MC/MCObjectWriter.h - Object File Writer Interface --*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_MC_MCOBJECTWRITER_H
10#define LLVM_MC_MCOBJECTWRITER_H
11
Andrew Scullcdfcccc2018-10-05 20:58:37 +010012#include "llvm/ADT/Triple.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010013#include <cstdint>
14
15namespace llvm {
16
17class MCAsmLayout;
18class MCAssembler;
19class MCFixup;
20class MCFragment;
21class MCSymbol;
22class MCSymbolRefExpr;
23class MCValue;
24
25/// Defines the object file and target independent interfaces used by the
26/// assembler backend to write native file format object files.
27///
28/// The object writer contains a few callbacks used by the assembler to allow
29/// the object writer to modify the assembler data structures at appropriate
30/// points. Once assembly is complete, the object writer is given the
31/// MCAssembler instance, which contains all the symbol and section data which
32/// should be emitted as part of writeObject().
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033class MCObjectWriter {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034protected:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010035 MCObjectWriter() = default;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010036
37public:
38 MCObjectWriter(const MCObjectWriter &) = delete;
39 MCObjectWriter &operator=(const MCObjectWriter &) = delete;
40 virtual ~MCObjectWriter();
41
42 /// lifetime management
43 virtual void reset() {}
44
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045 /// \name High-Level API
46 /// @{
47
48 /// Perform any late binding of symbols (for example, to assign symbol
49 /// indices for use when generating relocations).
50 ///
51 /// This routine is called by the assembler after layout and relaxation is
52 /// complete.
53 virtual void executePostLayoutBinding(MCAssembler &Asm,
54 const MCAsmLayout &Layout) = 0;
55
56 /// Record a relocation entry.
57 ///
58 /// This routine is called by the assembler after layout and relaxation, and
59 /// post layout binding. The implementation is responsible for storing
60 /// information about the relocation so that it can be emitted during
61 /// writeObject().
62 virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
63 const MCFragment *Fragment,
64 const MCFixup &Fixup, MCValue Target,
65 uint64_t &FixedValue) = 0;
66
67 /// Check whether the difference (A - B) between two symbol references is
68 /// fully resolved.
69 ///
70 /// Clients are not required to answer precisely and may conservatively return
71 /// false, even when a difference is fully resolved.
72 bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
73 const MCSymbolRefExpr *A,
74 const MCSymbolRefExpr *B,
75 bool InSet) const;
76
77 virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
78 const MCSymbol &A,
79 const MCSymbol &B,
80 bool InSet) const;
81
82 virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
83 const MCSymbol &SymA,
84 const MCFragment &FB,
85 bool InSet,
86 bool IsPCRel) const;
87
Andrew Scullcdfcccc2018-10-05 20:58:37 +010088 /// Tell the object writer to emit an address-significance table during
89 /// writeObject(). If this function is not called, all symbols are treated as
90 /// address-significant.
91 virtual void emitAddrsigSection() {}
92
93 /// Record the given symbol in the address-significance table to be written
94 /// diring writeObject().
95 virtual void addAddrsigSymbol(const MCSymbol *Sym) {}
96
97 /// Write the object file and returns the number of bytes written.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098 ///
99 /// This routine is called by the assembler after layout and relaxation is
100 /// complete, fixups have been evaluated and applied, and relocations
101 /// generated.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100102 virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100103
104 /// @}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100105};
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100107/// Base class for classes that define behaviour that is specific to both the
108/// target and the object format.
109class MCObjectTargetWriter {
110public:
111 virtual ~MCObjectTargetWriter() = default;
112 virtual Triple::ObjectFormatType getFormat() const = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113};
114
115} // end namespace llvm
116
117#endif // LLVM_MC_MCOBJECTWRITER_H