1 //===-- llvm/MC/MCMachObjectWriter.h - Mach Object Writer -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCMACHOBJECTWRITER_H
11 #define LLVM_MC_MCMACHOBJECTWRITER_H
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"
24 class MachObjectWriter;
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;
36 MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
38 bool UseAggressiveSymbolFolding_ = false);
40 void setLocalDifferenceRelocationType(unsigned Type) {
41 LocalDifference_RIT = Type;
45 virtual ~MCMachObjectTargetWriter();
47 /// @name Lifetime Management
50 virtual void reset() {};
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;
70 virtual void RecordRelocation(MachObjectWriter *Writer,
71 const MCAssembler &Asm,
72 const MCAsmLayout &Layout,
73 const MCFragment *Fragment,
76 uint64_t &FixedValue) = 0;
81 class MachObjectWriter : public MCObjectWriter {
82 /// MachSymbolData - Helper struct for containing some precomputed information
84 struct MachSymbolData {
85 MCSymbolData *SymbolData;
89 // Support lexicographic sorting.
90 bool operator<(const MachSymbolData &RHS) const;
93 /// The target specific Mach-O writer instance.
94 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
96 /// @name Relocation Data
99 llvm::DenseMap<const MCSectionData*,
100 std::vector<MachO::any_relocation_info> > Relocations;
101 llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
104 /// @name Symbol Table Data
107 SmallString<256> StringTable;
108 std::vector<MachSymbolData> LocalSymbolData;
109 std::vector<MachSymbolData> ExternalSymbolData;
110 std::vector<MachSymbolData> UndefinedSymbolData;
114 MachSymbolData *findSymbolData(const MCSymbol &Sym);
117 MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
118 bool _IsLittleEndian)
119 : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
122 /// @name Lifetime management Methods
125 void reset() override;
129 /// @name Utility Methods
132 bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
134 SectionAddrMap SectionAddress;
136 SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
138 uint64_t getSectionAddress(const MCSectionData* SD) const {
139 return SectionAddress.lookup(SD);
141 uint64_t getSymbolAddress(const MCSymbolData* SD,
142 const MCAsmLayout &Layout) const;
144 uint64_t getFragmentAddress(const MCFragment *Fragment,
145 const MCAsmLayout &Layout) const;
147 uint64_t getPaddingSize(const MCSectionData *SD,
148 const MCAsmLayout &Layout) const;
150 bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
154 /// @name Target Writer Proxy Accessors
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;
165 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
166 bool SubsectionsViaSymbols);
168 /// WriteSegmentLoadCommand - Write a segment load command.
170 /// \param NumSections The number of sections in this segment.
171 /// \param SectionDataSize The total size of the sections.
172 void WriteSegmentLoadCommand(unsigned NumSections,
174 uint64_t SectionDataStartOffset,
175 uint64_t SectionDataSize);
177 void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
178 const MCSectionData &SD, uint64_t FileOffset,
179 uint64_t RelocationsStart, unsigned NumRelocations);
181 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
182 uint32_t StringTableOffset,
183 uint32_t StringTableSize);
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);
194 void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
196 void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
199 void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
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
207 // - Relocation entry bugs, the two algorithms are unlikely to have the same
210 // - Relaxation issues, where we forget to relax something.
212 // - Input errors, where something cannot be correctly encoded. 'as' allows
213 // these through in many cases.
215 void addRelocation(const MCSectionData *SD,
216 MachO::any_relocation_info &MRE) {
217 Relocations[SD].push_back(MRE);
220 void RecordScatteredRelocation(const MCAssembler &Asm,
221 const MCAsmLayout &Layout,
222 const MCFragment *Fragment,
223 const MCFixup &Fixup, MCValue Target,
225 uint64_t &FixedValue);
227 void RecordTLVPRelocation(const MCAssembler &Asm,
228 const MCAsmLayout &Layout,
229 const MCFragment *Fragment,
230 const MCFixup &Fixup, MCValue Target,
231 uint64_t &FixedValue);
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;
238 void BindIndirectSymbols(MCAssembler &Asm);
240 /// ComputeSymbolTable - Compute the symbol table data
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);
248 void computeSectionAddresses(const MCAssembler &Asm,
249 const MCAsmLayout &Layout);
251 void markAbsoluteVariableSymbols(MCAssembler &Asm,
252 const MCAsmLayout &Layout);
253 void ExecutePostLayoutBinding(MCAssembler &Asm,
254 const MCAsmLayout &Layout) override;
256 bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
257 const MCSymbolData &DataA,
258 const MCFragment &FB,
260 bool IsPCRel) const override;
262 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
266 /// \brief Construct a new Mach-O writer instance.
268 /// This routine takes ownership of the target writer subclass.
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_ostream &OS, bool IsLittleEndian);
276 } // End llvm namespace