]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCMachObjectWriter.h
Merge openmp trunk r366426, resolve conflicts, and add FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / MC / MCMachObjectWriter.h
1 //===- llvm/MC/MCMachObjectWriter.h - Mach Object Writer --------*- 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_MCMACHOBJECTWRITER_H
10 #define LLVM_MC_MCMACHOBJECTWRITER_H
11
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/MachO.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/StringTableBuilder.h"
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <vector>
23
24 namespace llvm {
25
26 class MachObjectWriter;
27
28 class MCMachObjectTargetWriter : public MCObjectTargetWriter {
29   const unsigned Is64Bit : 1;
30   const uint32_t CPUType;
31   const uint32_t CPUSubtype;
32   unsigned LocalDifference_RIT;
33
34 protected:
35   MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
36                            uint32_t CPUSubtype_);
37
38   void setLocalDifferenceRelocationType(unsigned Type) {
39     LocalDifference_RIT = Type;
40   }
41
42 public:
43   virtual ~MCMachObjectTargetWriter();
44
45   virtual Triple::ObjectFormatType getFormat() const { return Triple::MachO; }
46   static bool classof(const MCObjectTargetWriter *W) {
47     return W->getFormat() == Triple::MachO;
48   }
49
50   /// \name Lifetime Management
51   /// @{
52
53   virtual void reset() {}
54
55   /// @}
56
57   /// \name Accessors
58   /// @{
59
60   bool is64Bit() const { return Is64Bit; }
61   uint32_t getCPUType() const { return CPUType; }
62   uint32_t getCPUSubtype() const { return CPUSubtype; }
63   unsigned getLocalDifferenceRelocationType() const {
64     return LocalDifference_RIT;
65   }
66
67   /// @}
68
69   /// \name API
70   /// @{
71
72   virtual void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
73                                 const MCAsmLayout &Layout,
74                                 const MCFragment *Fragment,
75                                 const MCFixup &Fixup, MCValue Target,
76                                 uint64_t &FixedValue) = 0;
77
78   /// @}
79 };
80
81 class MachObjectWriter : public MCObjectWriter {
82   /// Helper struct for containing some precomputed information on symbols.
83   struct MachSymbolData {
84     const MCSymbol *Symbol;
85     uint64_t StringIndex;
86     uint8_t SectionIndex;
87
88     // Support lexicographic sorting.
89     bool operator<(const MachSymbolData &RHS) const;
90   };
91
92   /// The target specific Mach-O writer instance.
93   std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
94
95   /// \name Relocation Data
96   /// @{
97
98   struct RelAndSymbol {
99     const MCSymbol *Sym;
100     MachO::any_relocation_info MRE;
101     RelAndSymbol(const MCSymbol *Sym, const MachO::any_relocation_info &MRE)
102         : Sym(Sym), MRE(MRE) {}
103   };
104
105   DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
106   DenseMap<const MCSection *, unsigned> IndirectSymBase;
107
108   SectionAddrMap SectionAddress;
109
110   /// @}
111   /// \name Symbol Table Data
112   /// @{
113
114   StringTableBuilder StringTable{StringTableBuilder::MachO};
115   std::vector<MachSymbolData> LocalSymbolData;
116   std::vector<MachSymbolData> ExternalSymbolData;
117   std::vector<MachSymbolData> UndefinedSymbolData;
118
119   /// @}
120
121   MachSymbolData *findSymbolData(const MCSymbol &Sym);
122
123   void writeWithPadding(StringRef Str, uint64_t Size);
124
125 public:
126   MachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
127                    raw_pwrite_stream &OS, bool IsLittleEndian)
128       : TargetObjectWriter(std::move(MOTW)),
129         W(OS, IsLittleEndian ? support::little : support::big) {}
130
131   support::endian::Writer W;
132
133   const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
134
135   /// \name Lifetime management Methods
136   /// @{
137
138   void reset() override;
139
140   /// @}
141
142   /// \name Utility Methods
143   /// @{
144
145   bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
146
147   SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
148
149   uint64_t getSectionAddress(const MCSection *Sec) const {
150     return SectionAddress.lookup(Sec);
151   }
152   uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
153
154   uint64_t getFragmentAddress(const MCFragment *Fragment,
155                               const MCAsmLayout &Layout) const;
156
157   uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
158
159   bool doesSymbolRequireExternRelocation(const MCSymbol &S);
160
161   /// @}
162
163   /// \name Target Writer Proxy Accessors
164   /// @{
165
166   bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
167   bool isX86_64() const {
168     uint32_t CPUType = TargetObjectWriter->getCPUType();
169     return CPUType == MachO::CPU_TYPE_X86_64;
170   }
171
172   /// @}
173
174   void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands,
175                    unsigned LoadCommandsSize, bool SubsectionsViaSymbols);
176
177   /// Write a segment load command.
178   ///
179   /// \param NumSections The number of sections in this segment.
180   /// \param SectionDataSize The total size of the sections.
181   void writeSegmentLoadCommand(StringRef Name, unsigned NumSections,
182                                uint64_t VMAddr, uint64_t VMSize,
183                                uint64_t SectionDataStartOffset,
184                                uint64_t SectionDataSize, uint32_t MaxProt,
185                                uint32_t InitProt);
186
187   void writeSection(const MCAsmLayout &Layout, const MCSection &Sec,
188                     uint64_t VMAddr, uint64_t FileOffset, unsigned Flags,
189                     uint64_t RelocationsStart, unsigned NumRelocations);
190
191   void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
192                               uint32_t StringTableOffset,
193                               uint32_t StringTableSize);
194
195   void writeDysymtabLoadCommand(
196       uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols,
197       uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols,
198       uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
199       uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
200
201   void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
202
203   void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
204                                 uint32_t DataSize);
205
206   void writeLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
207
208   // FIXME: We really need to improve the relocation validation. Basically, we
209   // want to implement a separate computation which evaluates the relocation
210   // entry as the linker would, and verifies that the resultant fixup value is
211   // exactly what the encoder wanted. This will catch several classes of
212   // problems:
213   //
214   //  - Relocation entry bugs, the two algorithms are unlikely to have the same
215   //    exact bug.
216   //
217   //  - Relaxation issues, where we forget to relax something.
218   //
219   //  - Input errors, where something cannot be correctly encoded. 'as' allows
220   //    these through in many cases.
221
222   // Add a relocation to be output in the object file. At the time this is
223   // called, the symbol indexes are not know, so if the relocation refers
224   // to a symbol it should be passed as \p RelSymbol so that it can be updated
225   // afterwards. If the relocation doesn't refer to a symbol, nullptr should be
226   // used.
227   void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec,
228                      MachO::any_relocation_info &MRE) {
229     RelAndSymbol P(RelSymbol, MRE);
230     Relocations[Sec].push_back(P);
231   }
232
233   void recordScatteredRelocation(const MCAssembler &Asm,
234                                  const MCAsmLayout &Layout,
235                                  const MCFragment *Fragment,
236                                  const MCFixup &Fixup, MCValue Target,
237                                  unsigned Log2Size, uint64_t &FixedValue);
238
239   void recordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
240                             const MCFragment *Fragment, const MCFixup &Fixup,
241                             MCValue Target, uint64_t &FixedValue);
242
243   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
244                         const MCFragment *Fragment, const MCFixup &Fixup,
245                         MCValue Target, uint64_t &FixedValue) override;
246
247   void bindIndirectSymbols(MCAssembler &Asm);
248
249   /// Compute the symbol table data.
250   void computeSymbolTable(MCAssembler &Asm,
251                           std::vector<MachSymbolData> &LocalSymbolData,
252                           std::vector<MachSymbolData> &ExternalSymbolData,
253                           std::vector<MachSymbolData> &UndefinedSymbolData);
254
255   void computeSectionAddresses(const MCAssembler &Asm,
256                                const MCAsmLayout &Layout);
257
258   void executePostLayoutBinding(MCAssembler &Asm,
259                                 const MCAsmLayout &Layout) override;
260
261   bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
262                                               const MCSymbol &A,
263                                               const MCSymbol &B,
264                                               bool InSet) const override;
265
266   bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
267                                               const MCSymbol &SymA,
268                                               const MCFragment &FB, bool InSet,
269                                               bool IsPCRel) const override;
270
271   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
272 };
273
274 /// Construct a new Mach-O writer instance.
275 ///
276 /// This routine takes ownership of the target writer subclass.
277 ///
278 /// \param MOTW - The target specific Mach-O writer subclass.
279 /// \param OS - The stream to write to.
280 /// \returns The constructed object writer.
281 std::unique_ptr<MCObjectWriter>
282 createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
283                        raw_pwrite_stream &OS, bool IsLittleEndian);
284
285 } // end namespace llvm
286
287 #endif // LLVM_MC_MCMACHOBJECTWRITER_H