blob: 8bae2bf200832f8132f011ba58495d559381bd6e [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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 Scullcdfcccc2018-10-05 20:58:37 +010015#include "llvm/ADT/Triple.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010016#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
22namespace llvm {
23
24class MCAsmLayout;
25class MCAssembler;
26class MCFixup;
27class MCFragment;
28class MCSymbol;
29class MCSymbolRefExpr;
30class 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 Scull5e1ddfa2018-08-14 10:06:54 +010040class MCObjectWriter {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041protected:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010042 MCObjectWriter() = default;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043
44public:
45 MCObjectWriter(const MCObjectWriter &) = delete;
46 MCObjectWriter &operator=(const MCObjectWriter &) = delete;
47 virtual ~MCObjectWriter();
48
49 /// lifetime management
50 virtual void reset() {}
51
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052 /// \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 Scullcdfcccc2018-10-05 20:58:37 +010095 /// 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 Scull5e1ddfa2018-08-14 10:06:54 +0100105 ///
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 Scullcdfcccc2018-10-05 20:58:37 +0100109 virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100110
111 /// @}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112};
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100114/// Base class for classes that define behaviour that is specific to both the
115/// target and the object format.
116class MCObjectTargetWriter {
117public:
118 virtual ~MCObjectTargetWriter() = default;
119 virtual Triple::ObjectFormatType getFormat() const = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120};
121
122} // end namespace llvm
123
124#endif // LLVM_MC_MCOBJECTWRITER_H