1aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar//===-- llvm/MC/MCMachObjectWriter.h - Mach Object Writer -------*- C++ -*-===//
2aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar//
3aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar//                     The LLVM Compiler Infrastructure
4aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar//
5aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar// This file is distributed under the University of Illinois Open Source
6aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar// License. See LICENSE.TXT for details.
7aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar//
8aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar//===----------------------------------------------------------------------===//
9aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar
10aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar#ifndef LLVM_MC_MCMACHOBJECTWRITER_H
11aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar#define LLVM_MC_MCMACHOBJECTWRITER_H
12aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar
13ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach#include "llvm/ADT/DenseMap.h"
14ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach#include "llvm/ADT/OwningPtr.h"
15ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach#include "llvm/ADT/SmallString.h"
16ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach#include "llvm/MC/MCExpr.h"
17aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar#include "llvm/MC/MCObjectWriter.h"
18ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach#include "llvm/Object/MachOFormat.h"
195d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar#include "llvm/Support/DataTypes.h"
20ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach#include <vector>
21aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar
22aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbarnamespace llvm {
23aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar
24ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbachclass MCSectionData;
25ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbachclass MachObjectWriter;
26ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
27ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbarclass MCMachObjectTargetWriter {
285d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  const unsigned Is64Bit : 1;
295d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  const uint32_t CPUType;
305d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  const uint32_t CPUSubtype;
31b87422778c4e7b828e6e846b81fd77509439c622Daniel Dunbar  // FIXME: Remove this, we should just always use it once we no longer care
32b87422778c4e7b828e6e846b81fd77509439c622Daniel Dunbar  // about Darwin 'as' compatibility.
33b87422778c4e7b828e6e846b81fd77509439c622Daniel Dunbar  const unsigned UseAggressiveSymbolFolding : 1;
34e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar  unsigned LocalDifference_RIT;
355d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar
36ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbarprotected:
375d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
38b87422778c4e7b828e6e846b81fd77509439c622Daniel Dunbar                           uint32_t CPUSubtype_,
39b87422778c4e7b828e6e846b81fd77509439c622Daniel Dunbar                           bool UseAggressiveSymbolFolding_ = false);
40ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar
41e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar  void setLocalDifferenceRelocationType(unsigned Type) {
42e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar    LocalDifference_RIT = Type;
43e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar  }
44e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar
45ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbarpublic:
46ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar  virtual ~MCMachObjectTargetWriter();
475d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar
4899cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  /// @name Lifetime Management
4999cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  /// @{
5099cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas
5199cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  virtual void reset() {};
5299cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas
5399cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  /// @}
5499cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas
555d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  /// @name Accessors
565d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  /// @{
575d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar
585d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  bool is64Bit() const { return Is64Bit; }
59b87422778c4e7b828e6e846b81fd77509439c622Daniel Dunbar  bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; }
605d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  uint32_t getCPUType() const { return CPUType; }
615d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  uint32_t getCPUSubtype() const { return CPUSubtype; }
62e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar  unsigned getLocalDifferenceRelocationType() const {
63e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar    return LocalDifference_RIT;
64e1feeb9da41fd48002e363c6dbb0a3d7bf0b7811Daniel Dunbar  }
655d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar
665d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar  /// @}
67ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
68ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @name API
69ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @{
70ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
71ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  virtual void RecordRelocation(MachObjectWriter *Writer,
72ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                const MCAssembler &Asm,
73ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                const MCAsmLayout &Layout,
74ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                const MCFragment *Fragment,
75ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                const MCFixup &Fixup,
76ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                MCValue Target,
77ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint64_t &FixedValue) = 0;
78ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
79ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @}
80ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar};
81ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar
82ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbachclass MachObjectWriter : public MCObjectWriter {
83ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// MachSymbolData - Helper struct for containing some precomputed information
84ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// on symbols.
85ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  struct MachSymbolData {
86ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    MCSymbolData *SymbolData;
87ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    uint64_t StringIndex;
88ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    uint8_t SectionIndex;
89ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
90ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    // Support lexicographic sorting.
91ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    bool operator<(const MachSymbolData &RHS) const;
92ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  };
93ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
94ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// The target specific Mach-O writer instance.
95ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  llvm::OwningPtr<MCMachObjectTargetWriter> TargetObjectWriter;
96ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
97ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @name Relocation Data
98ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @{
99ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
100ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  llvm::DenseMap<const MCSectionData*,
101ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                 std::vector<object::macho::RelocationEntry> > Relocations;
102ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
103ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
104ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @}
105ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @name Symbol Table Data
106ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @{
107ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
108ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  SmallString<256> StringTable;
109ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  std::vector<MachSymbolData> LocalSymbolData;
110ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  std::vector<MachSymbolData> ExternalSymbolData;
111ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  std::vector<MachSymbolData> UndefinedSymbolData;
112ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
113ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @}
114ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
115ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbachpublic:
116ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
117ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                   bool _IsLittleEndian)
118ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
119ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  }
120ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
12199cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  /// @name Lifetime management Methods
12299cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  /// @{
12399cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas
12499cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  virtual void reset();
12599cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas
12699cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas  /// @}
12799cbdde6198623ff014c776743caec2cf48f4840Pedro Artigas
128ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @name Utility Methods
129ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @{
130ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
131ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
132ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
133ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  SectionAddrMap SectionAddress;
134ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
135ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
136ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
137ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  uint64_t getSectionAddress(const MCSectionData* SD) const {
138ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    return SectionAddress.lookup(SD);
139ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  }
140ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  uint64_t getSymbolAddress(const MCSymbolData* SD,
141ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                            const MCAsmLayout &Layout) const;
142ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
143ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  uint64_t getFragmentAddress(const MCFragment *Fragment,
144ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                              const MCAsmLayout &Layout) const;
145ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
146ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  uint64_t getPaddingSize(const MCSectionData *SD,
147ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                          const MCAsmLayout &Layout) const;
148ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
149ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
150ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
151ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @}
152ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
153ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @name Target Writer Proxy Accessors
154ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @{
155ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
156ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
157ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  bool isARM() const {
158ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    uint32_t CPUType = TargetObjectWriter->getCPUType() &
159ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach      ~object::mach::CTFM_ArchMask;
160ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    return CPUType == object::mach::CTM_ARM;
161ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  }
162ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
163ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// @}
164ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
165ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
166ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                   bool SubsectionsViaSymbols);
167ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
168ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// WriteSegmentLoadCommand - Write a segment load command.
169ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  ///
170c5252da873d547a19069eaf9030fec203f128f66Dmitri Gribenko  /// \param NumSections The number of sections in this segment.
171c5252da873d547a19069eaf9030fec203f128f66Dmitri Gribenko  /// \param SectionDataSize The total size of the sections.
172ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void WriteSegmentLoadCommand(unsigned NumSections,
173ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                               uint64_t VMSize,
174ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                               uint64_t SectionDataStartOffset,
175ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                               uint64_t SectionDataSize);
176ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
177ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
178ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                    const MCSectionData &SD, uint64_t FileOffset,
179ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                    uint64_t RelocationsStart, unsigned NumRelocations);
180ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
181ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
182ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                              uint32_t StringTableOffset,
183ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                              uint32_t StringTableSize);
184ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
185ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
186ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint32_t NumLocalSymbols,
187ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint32_t FirstExternalSymbol,
188ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint32_t NumExternalSymbols,
189ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint32_t FirstUndefinedSymbol,
190ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint32_t NumUndefinedSymbols,
191ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint32_t IndirectSymbolOffset,
192ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                uint32_t NumIndirectSymbols);
193ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
194ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
195ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
1963e96531186ba574b0c25a4be62d24b8b7d752c9fJim Grosbach  void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
1973e96531186ba574b0c25a4be62d24b8b7d752c9fJim Grosbach                                uint32_t DataSize);
1983e96531186ba574b0c25a4be62d24b8b7d752c9fJim Grosbach
199a94c33942373cb504b6e64c95415165907a89d34Daniel Dunbar  void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
200a94c33942373cb504b6e64c95415165907a89d34Daniel Dunbar
201ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  // FIXME: We really need to improve the relocation validation. Basically, we
202ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  // want to implement a separate computation which evaluates the relocation
203ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  // entry as the linker would, and verifies that the resultant fixup value is
204ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  // exactly what the encoder wanted. This will catch several classes of
205ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  // problems:
206ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //
207ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //  - Relocation entry bugs, the two algorithms are unlikely to have the same
208ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //    exact bug.
209ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //
210ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //  - Relaxation issues, where we forget to relax something.
211ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //
212ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //  - Input errors, where something cannot be correctly encoded. 'as' allows
213ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  //    these through in many cases.
214ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
215ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void addRelocation(const MCSectionData *SD,
216ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                     object::macho::RelocationEntry &MRE) {
217ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach    Relocations[SD].push_back(MRE);
218ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  }
219ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
220ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void RecordScatteredRelocation(const MCAssembler &Asm,
221ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                 const MCAsmLayout &Layout,
222ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                 const MCFragment *Fragment,
223ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                 const MCFixup &Fixup, MCValue Target,
224ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                 unsigned Log2Size,
225ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                 uint64_t &FixedValue);
226ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
227ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void RecordTLVPRelocation(const MCAssembler &Asm,
228ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                            const MCAsmLayout &Layout,
229ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                            const MCFragment *Fragment,
230ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                            const MCFixup &Fixup, MCValue Target,
231ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                            uint64_t &FixedValue);
232ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
233ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
234ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                        const MCFragment *Fragment, const MCFixup &Fixup,
235ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                        MCValue Target, uint64_t &FixedValue);
236ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
237ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void BindIndirectSymbols(MCAssembler &Asm);
238ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
239ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// ComputeSymbolTable - Compute the symbol table data
240ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  ///
241ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  /// \param StringTable [out] - The string table data.
242ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
243ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                          std::vector<MachSymbolData> &LocalSymbolData,
244ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                          std::vector<MachSymbolData> &ExternalSymbolData,
245ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                          std::vector<MachSymbolData> &UndefinedSymbolData);
246ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
247ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void computeSectionAddresses(const MCAssembler &Asm,
248ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                               const MCAsmLayout &Layout);
249ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
25045d81bdde87a38c21facf2ec3b82b0589e9de7e9Jim Grosbach  void markAbsoluteVariableSymbols(MCAssembler &Asm,
25145d81bdde87a38c21facf2ec3b82b0589e9de7e9Jim Grosbach                                   const MCAsmLayout &Layout);
252ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout);
253ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
254ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  virtual bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
255ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                                      const MCSymbolData &DataA,
256ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                                      const MCFragment &FB,
257ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                                      bool InSet,
258ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach                                                      bool IsPCRel) const;
259ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
260ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach  void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
261ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach};
262ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
263ba8297ec08cdf7ae0c1e0c18ce07922e1f822643Jim Grosbach
264ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar/// \brief Construct a new Mach-O writer instance.
265ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar///
266ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar/// This routine takes ownership of the target writer subclass.
267ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar///
268ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar/// \param MOTW - The target specific Mach-O writer subclass.
269ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar/// \param OS - The stream to write to.
270ae5abd595f5442767313a4c8a24008ad19323cebDaniel Dunbar/// \returns The constructed object writer.
271ae5abd595f5442767313a4c8a24008ad19323cebDaniel DunbarMCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
2725d05d9769ec98cdee359fd934a56c9455e21232bDaniel Dunbar                                       raw_ostream &OS, bool IsLittleEndian);
273aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar
274aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar} // End llvm namespace
275aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar
276aa4b7dd13ba83152473950d7014a29686dc7eef6Daniel Dunbar#endif
277