MCMachObjectWriter.h revision cd81d94322a39503e4a3e87b6ee03d4fcb3465fb
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
21namespace llvm {
22
23class MCSectionData;
24class MachObjectWriter;
25
26class 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
35protected:
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
44public:
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
81class 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  MachSymbolData *findSymbolData(const MCSymbol &Sym);
115
116public:
117  MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
118                   bool _IsLittleEndian)
119    : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
120  }
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 MCSectionData* SD) const {
139    return SectionAddress.lookup(SD);
140  }
141  uint64_t getSymbolAddress(const MCSymbolData* SD,
142                            const MCAsmLayout &Layout) const;
143
144  uint64_t getFragmentAddress(const MCFragment *Fragment,
145                              const MCAsmLayout &Layout) const;
146
147  uint64_t getPaddingSize(const MCSectionData *SD,
148                          const MCAsmLayout &Layout) const;
149
150  bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
151
152  /// @}
153
154  /// @name Target Writer Proxy Accessors
155  /// @{
156
157  bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
158  bool isX86_64() const {
159    uint32_t CPUType = TargetObjectWriter->getCPUType();
160    return CPUType == MachO::CPU_TYPE_X86_64;
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  void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
200
201  // FIXME: We really need to improve the relocation validation. Basically, we
202  // want to implement a separate computation which evaluates the relocation
203  // entry as the linker would, and verifies that the resultant fixup value is
204  // exactly what the encoder wanted. This will catch several classes of
205  // problems:
206  //
207  //  - Relocation entry bugs, the two algorithms are unlikely to have the same
208  //    exact bug.
209  //
210  //  - Relaxation issues, where we forget to relax something.
211  //
212  //  - Input errors, where something cannot be correctly encoded. 'as' allows
213  //    these through in many cases.
214
215  void addRelocation(const MCSectionData *SD,
216                     MachO::any_relocation_info &MRE) {
217    Relocations[SD].push_back(MRE);
218  }
219
220  void RecordScatteredRelocation(const MCAssembler &Asm,
221                                 const MCAsmLayout &Layout,
222                                 const MCFragment *Fragment,
223                                 const MCFixup &Fixup, MCValue Target,
224                                 unsigned Log2Size,
225                                 uint64_t &FixedValue);
226
227  void RecordTLVPRelocation(const MCAssembler &Asm,
228                            const MCAsmLayout &Layout,
229                            const MCFragment *Fragment,
230                            const MCFixup &Fixup, MCValue Target,
231                            uint64_t &FixedValue);
232
233  void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
234                        const MCFragment *Fragment, const MCFixup &Fixup,
235                        MCValue Target, bool &IsPCRel,
236                        uint64_t &FixedValue) override;
237
238  void BindIndirectSymbols(MCAssembler &Asm);
239
240  /// ComputeSymbolTable - Compute the symbol table data
241  ///
242  /// \param StringTable [out] - The string table data.
243  void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
244                          std::vector<MachSymbolData> &LocalSymbolData,
245                          std::vector<MachSymbolData> &ExternalSymbolData,
246                          std::vector<MachSymbolData> &UndefinedSymbolData);
247
248  void computeSectionAddresses(const MCAssembler &Asm,
249                               const MCAsmLayout &Layout);
250
251  void markAbsoluteVariableSymbols(MCAssembler &Asm,
252                                   const MCAsmLayout &Layout);
253  void ExecutePostLayoutBinding(MCAssembler &Asm,
254                                const MCAsmLayout &Layout) override;
255
256  bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
257                                              const MCSymbolData &DataA,
258                                              const MCFragment &FB,
259                                              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.
273MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
274                                       raw_ostream &OS, bool IsLittleEndian);
275
276} // End llvm namespace
277
278#endif
279