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