MCMachObjectWriter.h revision 85e910fe5cc308956edc0c71165b6714a1654df9
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/OwningPtr.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCObjectWriter.h"
18#include "llvm/Object/MachOFormat.h"
19#include "llvm/Support/DataTypes.h"
20#include <vector>
21
22namespace llvm {
23
24class MCSectionData;
25class MachObjectWriter;
26
27class MCMachObjectTargetWriter {
28  const unsigned Is64Bit : 1;
29  const uint32_t CPUType;
30  const uint32_t CPUSubtype;
31  // FIXME: Remove this, we should just always use it once we no longer care
32  // about Darwin 'as' compatibility.
33  const unsigned UseAggressiveSymbolFolding : 1;
34  unsigned LocalDifference_RIT;
35
36protected:
37  MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
38                           uint32_t CPUSubtype_,
39                           bool UseAggressiveSymbolFolding_ = false);
40
41  void setLocalDifferenceRelocationType(unsigned Type) {
42    LocalDifference_RIT = Type;
43  }
44
45public:
46  virtual ~MCMachObjectTargetWriter();
47
48  /// @name Lifetime Management
49  /// @{
50
51  virtual void reset() {};
52
53  /// @}
54
55  /// @name Accessors
56  /// @{
57
58  bool is64Bit() const { return Is64Bit; }
59  bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; }
60  uint32_t getCPUType() const { return CPUType; }
61  uint32_t getCPUSubtype() const { return CPUSubtype; }
62  unsigned getLocalDifferenceRelocationType() const {
63    return LocalDifference_RIT;
64  }
65
66  /// @}
67
68  /// @name API
69  /// @{
70
71  virtual void RecordRelocation(MachObjectWriter *Writer,
72                                const MCAssembler &Asm,
73                                const MCAsmLayout &Layout,
74                                const MCFragment *Fragment,
75                                const MCFixup &Fixup,
76                                MCValue Target,
77                                uint64_t &FixedValue) = 0;
78
79  /// @}
80};
81
82class MachObjectWriter : public MCObjectWriter {
83  /// MachSymbolData - Helper struct for containing some precomputed information
84  /// on symbols.
85  struct MachSymbolData {
86    MCSymbolData *SymbolData;
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  llvm::OwningPtr<MCMachObjectTargetWriter> TargetObjectWriter;
96
97  /// @name Relocation Data
98  /// @{
99
100  llvm::DenseMap<const MCSectionData*,
101                 std::vector<object::macho::RelocationEntry> > Relocations;
102  llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
103
104  /// @}
105  /// @name Symbol Table Data
106  /// @{
107
108  SmallString<256> StringTable;
109  std::vector<MachSymbolData> LocalSymbolData;
110  std::vector<MachSymbolData> ExternalSymbolData;
111  std::vector<MachSymbolData> UndefinedSymbolData;
112
113  /// @}
114
115public:
116  MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
117                   bool _IsLittleEndian)
118    : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
119  }
120
121  /// @name Lifetime management Methods
122  /// @{
123
124  virtual void reset();
125
126  /// @}
127
128  /// @name Utility Methods
129  /// @{
130
131  bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
132
133  SectionAddrMap SectionAddress;
134
135  SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
136
137  uint64_t getSectionAddress(const MCSectionData* SD) const {
138    return SectionAddress.lookup(SD);
139  }
140  uint64_t getSymbolAddress(const MCSymbolData* SD,
141                            const MCAsmLayout &Layout) const;
142
143  uint64_t getFragmentAddress(const MCFragment *Fragment,
144                              const MCAsmLayout &Layout) const;
145
146  uint64_t getPaddingSize(const MCSectionData *SD,
147                          const MCAsmLayout &Layout) const;
148
149  bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
150
151  /// @}
152
153  /// @name Target Writer Proxy Accessors
154  /// @{
155
156  bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
157  bool isARM() const {
158    uint32_t CPUType = TargetObjectWriter->getCPUType() &
159      ~object::mach::CTFM_ArchMask;
160    return CPUType == object::mach::CTM_ARM;
161  }
162
163  /// @}
164
165  void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
166                   bool SubsectionsViaSymbols);
167
168  /// WriteSegmentLoadCommand - Write a segment load command.
169  ///
170  /// \param NumSections The number of sections in this segment.
171  /// \param SectionDataSize The total size of the sections.
172  void WriteSegmentLoadCommand(unsigned NumSections,
173                               uint64_t VMSize,
174                               uint64_t SectionDataStartOffset,
175                               uint64_t SectionDataSize);
176
177  void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
178                    const MCSectionData &SD, uint64_t FileOffset,
179                    uint64_t RelocationsStart, unsigned NumRelocations);
180
181  void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
182                              uint32_t StringTableOffset,
183                              uint32_t StringTableSize);
184
185  void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
186                                uint32_t NumLocalSymbols,
187                                uint32_t FirstExternalSymbol,
188                                uint32_t NumExternalSymbols,
189                                uint32_t FirstUndefinedSymbol,
190                                uint32_t NumUndefinedSymbols,
191                                uint32_t IndirectSymbolOffset,
192                                uint32_t NumIndirectSymbols);
193
194  void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
195
196  void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
197                                uint32_t DataSize);
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                     object::macho::RelocationEntry &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.
269MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
270                                       raw_ostream &OS, bool IsLittleEndian);
271
272} // End llvm namespace
273
274#endif
275