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