LLVM 22.0.0git
MCObjectWriter.h
Go to the documentation of this file.
1//===- llvm/MC/MCObjectWriter.h - Object File Writer Interface --*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_MC_MCOBJECTWRITER_H
10#define LLVM_MC_MCOBJECTWRITER_H
11
12#include "llvm/MC/MCSymbol.h"
15#include <cstdint>
16
17namespace llvm {
18
19class MCAssembler;
20class MCFixup;
21class MCFragment;
22class MCSymbol;
23class MCSymbolRefExpr;
24class MCValue;
25
26/// Defines the object file and target independent interfaces used by the
27/// assembler backend to write native file format object files.
28///
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().
35protected:
36 MCAssembler *Asm = nullptr;
37 /// List of declared file names
39 // XCOFF specific: Optional compiler version.
40 std::string CompilerVersion;
41 std::vector<const MCSymbol *> AddrsigSyms;
42 bool EmitAddrsigSection = false;
43 bool SubsectionsViaSymbols = false;
44
49 };
51
52 MCObjectWriter() = default;
53
54public:
55 MCObjectWriter(const MCObjectWriter &) = delete;
57 virtual ~MCObjectWriter();
58
59 virtual void setAssembler(MCAssembler *A) { Asm = A; }
60
61 MCContext &getContext() const;
62
63 /// lifetime management
64 virtual void reset();
65
66 /// \name High-Level API
67 /// @{
68
69 /// Perform any late binding of symbols (for example, to assign symbol
70 /// indices for use when generating relocations).
71 ///
72 /// This routine is called by the assembler after layout and relaxation is
73 /// complete.
74 virtual void executePostLayoutBinding() {}
75
76 /// Record a relocation entry.
77 ///
78 /// This routine is called by the assembler after layout and relaxation, and
79 /// post layout binding. The implementation is responsible for storing
80 /// information about the relocation so that it can be emitted during
81 /// writeObject().
82 virtual void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
83 MCValue Target, uint64_t &FixedValue);
84
85 /// Check whether the difference (A - B) between two symbol references is
86 /// fully resolved.
87 ///
88 /// Clients are not required to answer precisely and may conservatively return
89 /// false, even when a difference is fully resolved.
90 bool isSymbolRefDifferenceFullyResolved(const MCSymbol &A, const MCSymbol &B,
91 bool InSet) const;
92
93 virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
94 const MCFragment &FB,
95 bool InSet,
96 bool IsPCRel) const;
97
99 return FileNames;
100 }
101 void addFileName(StringRef FileName);
102 void setCompilerVersion(StringRef CompilerVers) {
103 CompilerVersion = CompilerVers;
104 }
105
106 /// Tell the object writer to emit an address-significance table during
107 /// writeObject(). If this function is not called, all symbols are treated as
108 /// address-significant.
109 void emitAddrsigSection() { EmitAddrsigSection = true; }
110
111 bool getEmitAddrsigSection() { return EmitAddrsigSection; }
112
113 /// Record the given symbol in the address-significance table to be written
114 /// diring writeObject().
115 void addAddrsigSymbol(const MCSymbol *Sym) { AddrsigSyms.push_back(Sym); }
116
117 std::vector<const MCSymbol *> &getAddrsigSyms() { return AddrsigSyms; }
119
120 // Mach-O specific: Whether .subsections_via_symbols is enabled.
121 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
122 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
123
124 /// Write the object file and returns the number of bytes written.
125 ///
126 /// This routine is called by the assembler after layout and relaxation is
127 /// complete, fixups have been evaluated and applied, and relocations
128 /// generated.
129 virtual uint64_t writeObject() = 0;
130
131 /// @}
132};
133
134/// Base class for classes that define behaviour that is specific to both the
135/// target and the object format.
137public:
138 virtual ~MCObjectTargetWriter() = default;
141
142protected:
144 LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const;
145
146 MCAssembler *Asm = nullptr;
147};
148
149} // end namespace llvm
150
151#endif // LLVM_MC_MCOBJECTWRITER_H
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition: Compiler.h:213
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
PowerPC TLS Dynamic Call Fixup
Context object for machine code objects.
Definition: MCContext.h:83
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:61
Base class for classes that define behaviour that is specific to both the target and the object forma...
LLVM_ABI MCContext & getContext() const
LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const
virtual Triple::ObjectFormatType getFormat() const =0
void setAssembler(MCAssembler *A)
virtual ~MCObjectTargetWriter()=default
Defines the object file and target independent interfaces used by the assembler backend to write nati...
MCObjectWriter()=default
MutableArrayRef< std::pair< std::string, size_t > > getFileNames()
MCObjectWriter & operator=(const MCObjectWriter &)=delete
SmallVector< CGProfileEntry, 0 > CGProfile
MCObjectWriter(const MCObjectWriter &)=delete
void setCompilerVersion(StringRef CompilerVers)
virtual ~MCObjectWriter()
bool getSubsectionsViaSymbols() const
virtual void setAssembler(MCAssembler *A)
void emitAddrsigSection()
Tell the object writer to emit an address-significance table during writeObject().
virtual void executePostLayoutBinding()
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
std::vector< const MCSymbol * > & getAddrsigSyms()
void addAddrsigSymbol(const MCSymbol *Sym)
Record the given symbol in the address-significance table to be written diring writeObject().
void setSubsectionsViaSymbols(bool Value)
std::string CompilerVersion
SmallVector< CGProfileEntry, 0 > & getCGProfile()
std::vector< const MCSymbol * > AddrsigSyms
virtual uint64_t writeObject()=0
Write the object file and returns the number of bytes written.
SmallVector< std::pair< std::string, size_t >, 0 > FileNames
List of declared file names.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:190
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
Represents a location in source code.
Definition: SMLoc.h:23
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Target - Wrapper for Target specific information.
ObjectFormatType
Definition: Triple.h:314
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
LLVM Value Representation.
Definition: Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18