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/MC/StringTableBuilder.h"
18 #include "llvm/Support/DataTypes.h"
19 #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 unsigned LocalDifference_RIT;
33 MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
34 uint32_t CPUSubtype_);
36 void setLocalDifferenceRelocationType(unsigned Type) {
37 LocalDifference_RIT = Type;
41 virtual ~MCMachObjectTargetWriter();
43 /// \name Lifetime Management
46 virtual void reset() {}
53 bool is64Bit() const { return Is64Bit; }
54 uint32_t getCPUType() const { return CPUType; }
55 uint32_t getCPUSubtype() const { return CPUSubtype; }
56 unsigned getLocalDifferenceRelocationType() const {
57 return LocalDifference_RIT;
65 virtual void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
66 const MCAsmLayout &Layout,
67 const MCFragment *Fragment,
68 const MCFixup &Fixup, MCValue Target,
69 uint64_t &FixedValue) = 0;
74 class MachObjectWriter : public MCObjectWriter {
75 /// Helper struct for containing some precomputed information on symbols.
76 struct MachSymbolData {
77 const MCSymbol *Symbol;
81 // Support lexicographic sorting.
82 bool operator<(const MachSymbolData &RHS) const;
85 /// The target specific Mach-O writer instance.
86 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
88 /// \name Relocation Data
93 MachO::any_relocation_info MRE;
94 RelAndSymbol(const MCSymbol *Sym, const MachO::any_relocation_info &MRE)
95 : Sym(Sym), MRE(MRE) {}
98 llvm::DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
99 llvm::DenseMap<const MCSection *, unsigned> IndirectSymBase;
101 SectionAddrMap SectionAddress;
104 /// \name Symbol Table Data
107 StringTableBuilder 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_pwrite_stream &OS,
119 : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
121 const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
123 /// \name Lifetime management Methods
126 void reset() override;
130 /// \name Utility Methods
133 bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
135 SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
137 uint64_t getSectionAddress(const MCSection *Sec) const {
138 return SectionAddress.lookup(Sec);
140 uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
142 uint64_t getFragmentAddress(const MCFragment *Fragment,
143 const MCAsmLayout &Layout) const;
145 uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
147 bool doesSymbolRequireExternRelocation(const MCSymbol &S);
151 /// \name Target Writer Proxy Accessors
154 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
155 bool isX86_64() const {
156 uint32_t CPUType = TargetObjectWriter->getCPUType();
157 return CPUType == MachO::CPU_TYPE_X86_64;
162 void writeHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
163 bool SubsectionsViaSymbols);
165 /// Write a segment load command.
167 /// \param NumSections The number of sections in this segment.
168 /// \param SectionDataSize The total size of the sections.
169 void writeSegmentLoadCommand(unsigned NumSections, uint64_t VMSize,
170 uint64_t SectionDataStartOffset,
171 uint64_t SectionDataSize);
173 void writeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
174 const MCSection &Sec, uint64_t FileOffset,
175 uint64_t RelocationsStart, unsigned NumRelocations);
177 void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
178 uint32_t StringTableOffset,
179 uint32_t StringTableSize);
181 void writeDysymtabLoadCommand(
182 uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols,
183 uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols,
184 uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
185 uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
187 void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
189 void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
192 void writeLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
194 // FIXME: We really need to improve the relocation validation. Basically, we
195 // want to implement a separate computation which evaluates the relocation
196 // entry as the linker would, and verifies that the resultant fixup value is
197 // exactly what the encoder wanted. This will catch several classes of
200 // - Relocation entry bugs, the two algorithms are unlikely to have the same
203 // - Relaxation issues, where we forget to relax something.
205 // - Input errors, where something cannot be correctly encoded. 'as' allows
206 // these through in many cases.
208 // Add a relocation to be output in the object file. At the time this is
209 // called, the symbol indexes are not know, so if the relocation refers
210 // to a symbol it should be passed as \p RelSymbol so that it can be updated
211 // afterwards. If the relocation doesn't refer to a symbol, nullptr should be
213 void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec,
214 MachO::any_relocation_info &MRE) {
215 RelAndSymbol P(RelSymbol, MRE);
216 Relocations[Sec].push_back(P);
219 void recordScatteredRelocation(const MCAssembler &Asm,
220 const MCAsmLayout &Layout,
221 const MCFragment *Fragment,
222 const MCFixup &Fixup, MCValue Target,
223 unsigned Log2Size, uint64_t &FixedValue);
225 void recordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
226 const MCFragment *Fragment, const MCFixup &Fixup,
227 MCValue Target, uint64_t &FixedValue);
229 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
230 const MCFragment *Fragment, const MCFixup &Fixup,
231 MCValue Target, bool &IsPCRel,
232 uint64_t &FixedValue) override;
234 void bindIndirectSymbols(MCAssembler &Asm);
236 /// Compute the symbol table data.
237 void computeSymbolTable(MCAssembler &Asm,
238 std::vector<MachSymbolData> &LocalSymbolData,
239 std::vector<MachSymbolData> &ExternalSymbolData,
240 std::vector<MachSymbolData> &UndefinedSymbolData);
242 void computeSectionAddresses(const MCAssembler &Asm,
243 const MCAsmLayout &Layout);
245 void executePostLayoutBinding(MCAssembler &Asm,
246 const MCAsmLayout &Layout) override;
248 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
249 const MCSymbol &SymA,
250 const MCFragment &FB, bool InSet,
251 bool IsPCRel) const override;
253 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
256 /// Construct a new Mach-O writer instance.
258 /// This routine takes ownership of the target writer subclass.
260 /// \param MOTW - The target specific Mach-O writer subclass.
261 /// \param OS - The stream to write to.
262 /// \returns The constructed object writer.
263 MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
264 raw_pwrite_stream &OS,
265 bool IsLittleEndian);
267 } // End llvm namespace