Replace OwningPtr<T> with std::unique_ptr<T>.
[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/Support/DataTypes.h"
18 #include "llvm/Support/MachO.h"
19 #include <vector>
20
21 namespace llvm {
22
23 class MCSectionData;
24 class MachObjectWriter;
25
26 class MCMachObjectTargetWriter {
27   const unsigned Is64Bit : 1;
28   const uint32_t CPUType;
29   const uint32_t CPUSubtype;
30   // FIXME: Remove this, we should just always use it once we no longer care
31   // about Darwin 'as' compatibility.
32   const unsigned UseAggressiveSymbolFolding : 1;
33   unsigned LocalDifference_RIT;
34
35 protected:
36   MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
37                            uint32_t CPUSubtype_,
38                            bool UseAggressiveSymbolFolding_ = false);
39
40   void setLocalDifferenceRelocationType(unsigned Type) {
41     LocalDifference_RIT = Type;
42   }
43
44 public:
45   virtual ~MCMachObjectTargetWriter();
46
47   /// @name Lifetime Management
48   /// @{
49
50   virtual void reset() {};
51
52   /// @}
53
54   /// @name Accessors
55   /// @{
56
57   bool is64Bit() const { return Is64Bit; }
58   bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; }
59   uint32_t getCPUType() const { return CPUType; }
60   uint32_t getCPUSubtype() const { return CPUSubtype; }
61   unsigned getLocalDifferenceRelocationType() const {
62     return LocalDifference_RIT;
63   }
64
65   /// @}
66
67   /// @name API
68   /// @{
69
70   virtual void RecordRelocation(MachObjectWriter *Writer,
71                                 const MCAssembler &Asm,
72                                 const MCAsmLayout &Layout,
73                                 const MCFragment *Fragment,
74                                 const MCFixup &Fixup,
75                                 MCValue Target,
76                                 uint64_t &FixedValue) = 0;
77
78   /// @}
79 };
80
81 class MachObjectWriter : public MCObjectWriter {
82   /// MachSymbolData - Helper struct for containing some precomputed information
83   /// on symbols.
84   struct MachSymbolData {
85     MCSymbolData *SymbolData;
86     uint64_t StringIndex;
87     uint8_t SectionIndex;
88
89     // Support lexicographic sorting.
90     bool operator<(const MachSymbolData &RHS) const;
91   };
92
93   /// The target specific Mach-O writer instance.
94   std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
95
96   /// @name Relocation Data
97   /// @{
98
99   llvm::DenseMap<const MCSectionData*,
100                  std::vector<MachO::any_relocation_info> > Relocations;
101   llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
102
103   /// @}
104   /// @name Symbol Table Data
105   /// @{
106
107   SmallString<256> StringTable;
108   std::vector<MachSymbolData> LocalSymbolData;
109   std::vector<MachSymbolData> ExternalSymbolData;
110   std::vector<MachSymbolData> UndefinedSymbolData;
111
112   /// @}
113
114 public:
115   MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
116                    bool _IsLittleEndian)
117     : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
118   }
119
120   /// @name Lifetime management Methods
121   /// @{
122
123   virtual void reset();
124
125   /// @}
126
127   /// @name Utility Methods
128   /// @{
129
130   bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
131
132   SectionAddrMap SectionAddress;
133
134   SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
135
136   uint64_t getSectionAddress(const MCSectionData* SD) const {
137     return SectionAddress.lookup(SD);
138   }
139   uint64_t getSymbolAddress(const MCSymbolData* SD,
140                             const MCAsmLayout &Layout) const;
141
142   uint64_t getFragmentAddress(const MCFragment *Fragment,
143                               const MCAsmLayout &Layout) const;
144
145   uint64_t getPaddingSize(const MCSectionData *SD,
146                           const MCAsmLayout &Layout) const;
147
148   bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
149
150   /// @}
151
152   /// @name Target Writer Proxy Accessors
153   /// @{
154
155   bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
156   bool isARM() const {
157     uint32_t CPUType = TargetObjectWriter->getCPUType() & ~MachO::CPU_ARCH_MASK;
158     return CPUType == MachO::CPU_TYPE_ARM;
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 MCSectionData &SD, 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   void addRelocation(const MCSectionData *SD,
214                      MachO::any_relocation_info &MRE) {
215     Relocations[SD].push_back(MRE);
216   }
217
218   void RecordScatteredRelocation(const MCAssembler &Asm,
219                                  const MCAsmLayout &Layout,
220                                  const MCFragment *Fragment,
221                                  const MCFixup &Fixup, MCValue Target,
222                                  unsigned Log2Size,
223                                  uint64_t &FixedValue);
224
225   void RecordTLVPRelocation(const MCAssembler &Asm,
226                             const MCAsmLayout &Layout,
227                             const MCFragment *Fragment,
228                             const MCFixup &Fixup, MCValue Target,
229                             uint64_t &FixedValue);
230
231   void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
232                         const MCFragment *Fragment, const MCFixup &Fixup,
233                         MCValue Target, uint64_t &FixedValue);
234
235   void BindIndirectSymbols(MCAssembler &Asm);
236
237   /// ComputeSymbolTable - Compute the symbol table data
238   ///
239   /// \param StringTable [out] - The string table data.
240   void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
241                           std::vector<MachSymbolData> &LocalSymbolData,
242                           std::vector<MachSymbolData> &ExternalSymbolData,
243                           std::vector<MachSymbolData> &UndefinedSymbolData);
244
245   void computeSectionAddresses(const MCAssembler &Asm,
246                                const MCAsmLayout &Layout);
247
248   void markAbsoluteVariableSymbols(MCAssembler &Asm,
249                                    const MCAsmLayout &Layout);
250   void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout);
251
252   virtual bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
253                                                       const MCSymbolData &DataA,
254                                                       const MCFragment &FB,
255                                                       bool InSet,
256                                                       bool IsPCRel) const;
257
258   void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
259 };
260
261
262 /// \brief Construct a new Mach-O writer instance.
263 ///
264 /// This routine takes ownership of the target writer subclass.
265 ///
266 /// \param MOTW - The target specific Mach-O writer subclass.
267 /// \param OS - The stream to write to.
268 /// \returns The constructed object writer.
269 MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
270                                        raw_ostream &OS, bool IsLittleEndian);
271
272 } // End llvm namespace
273
274 #endif