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" |
13 | #include "llvm/Support/Compiler.h" |
14 | #include "llvm/TargetParser/Triple.h" |
15 | #include <cstdint> |
16 | |
17 | namespace llvm { |
18 | |
19 | class MCAssembler; |
20 | class MCFixup; |
21 | class MCFragment; |
22 | class MCSymbol; |
23 | class MCSymbolRefExpr; |
24 | class 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(). |
34 | class LLVM_ABI MCObjectWriter { |
35 | protected: |
36 | MCAssembler *Asm = nullptr; |
37 | /// List of declared file names |
38 | SmallVector<std::pair<std::string, size_t>, 0> FileNames; |
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 | |
45 | struct CGProfileEntry { |
46 | const MCSymbolRefExpr *From; |
47 | const MCSymbolRefExpr *To; |
48 | uint64_t Count; |
49 | }; |
50 | SmallVector<CGProfileEntry, 0> CGProfile; |
51 | |
52 | MCObjectWriter() = default; |
53 | |
54 | public: |
55 | MCObjectWriter(const MCObjectWriter &) = delete; |
56 | MCObjectWriter &operator=(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 | |
98 | MutableArrayRef<std::pair<std::string, size_t>> getFileNames() { |
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(x: Sym); } |
116 | |
117 | std::vector<const MCSymbol *> &getAddrsigSyms() { return AddrsigSyms; } |
118 | SmallVector<CGProfileEntry, 0> &getCGProfile() { return CGProfile; } |
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. |
136 | class MCObjectTargetWriter { |
137 | public: |
138 | virtual ~MCObjectTargetWriter() = default; |
139 | void setAssembler(MCAssembler *A) { Asm = A; } |
140 | virtual Triple::ObjectFormatType getFormat() const = 0; |
141 | |
142 | protected: |
143 | LLVM_ABI MCContext &getContext() const; |
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 |
152 | |