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/StringRef.h"
15 #include "llvm/BinaryFormat/MachO.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/StringTableBuilder.h"
27 class MachObjectWriter;
29 class MCMachObjectTargetWriter {
30 const unsigned Is64Bit : 1;
31 const uint32_t CPUType;
32 const uint32_t CPUSubtype;
33 unsigned LocalDifference_RIT;
36 MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
37 uint32_t CPUSubtype_);
39 void setLocalDifferenceRelocationType(unsigned Type) {
40 LocalDifference_RIT = Type;
44 virtual ~MCMachObjectTargetWriter();
46 /// \name Lifetime Management
49 virtual void reset() {}
56 bool is64Bit() const { return Is64Bit; }
57 uint32_t getCPUType() const { return CPUType; }
58 uint32_t getCPUSubtype() const { return CPUSubtype; }
59 unsigned getLocalDifferenceRelocationType() const {
60 return LocalDifference_RIT;
68 virtual void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
69 const MCAsmLayout &Layout,
70 const MCFragment *Fragment,
71 const MCFixup &Fixup, MCValue Target,
72 uint64_t &FixedValue) = 0;
77 class MachObjectWriter : public MCObjectWriter {
78 /// Helper struct for containing some precomputed information on symbols.
79 struct MachSymbolData {
80 const MCSymbol *Symbol;
84 // Support lexicographic sorting.
85 bool operator<(const MachSymbolData &RHS) const;
88 /// The target specific Mach-O writer instance.
89 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
91 /// \name Relocation Data
96 MachO::any_relocation_info MRE;
97 RelAndSymbol(const MCSymbol *Sym, const MachO::any_relocation_info &MRE)
98 : Sym(Sym), MRE(MRE) {}
101 DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
102 DenseMap<const MCSection *, unsigned> IndirectSymBase;
104 SectionAddrMap SectionAddress;
107 /// \name Symbol Table Data
110 StringTableBuilder StringTable{StringTableBuilder::MachO};
111 std::vector<MachSymbolData> LocalSymbolData;
112 std::vector<MachSymbolData> ExternalSymbolData;
113 std::vector<MachSymbolData> UndefinedSymbolData;
117 MachSymbolData *findSymbolData(const MCSymbol &Sym);
120 MachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
121 raw_pwrite_stream &OS, bool IsLittleEndian)
122 : MCObjectWriter(OS, IsLittleEndian),
123 TargetObjectWriter(std::move(MOTW)) {}
125 const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
127 /// \name Lifetime management Methods
130 void reset() override;
134 /// \name Utility Methods
137 bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
139 SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
141 uint64_t getSectionAddress(const MCSection *Sec) const {
142 return SectionAddress.lookup(Sec);
144 uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
146 uint64_t getFragmentAddress(const MCFragment *Fragment,
147 const MCAsmLayout &Layout) const;
149 uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
151 bool doesSymbolRequireExternRelocation(const MCSymbol &S);
155 /// \name Target Writer Proxy Accessors
158 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
159 bool isX86_64() const {
160 uint32_t CPUType = TargetObjectWriter->getCPUType();
161 return CPUType == MachO::CPU_TYPE_X86_64;
166 void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands,
167 unsigned LoadCommandsSize, bool SubsectionsViaSymbols);
169 /// Write a segment load command.
171 /// \param NumSections The number of sections in this segment.
172 /// \param SectionDataSize The total size of the sections.
173 void writeSegmentLoadCommand(StringRef Name, unsigned NumSections,
174 uint64_t VMAddr, uint64_t VMSize,
175 uint64_t SectionDataStartOffset,
176 uint64_t SectionDataSize, uint32_t MaxProt,
179 void writeSection(const MCAsmLayout &Layout, const MCSection &Sec,
180 uint64_t VMAddr, uint64_t FileOffset, unsigned Flags,
181 uint64_t RelocationsStart, unsigned NumRelocations);
183 void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
184 uint32_t StringTableOffset,
185 uint32_t StringTableSize);
187 void writeDysymtabLoadCommand(
188 uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols,
189 uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols,
190 uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
191 uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
193 void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
195 void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
198 void writeLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
200 // FIXME: We really need to improve the relocation validation. Basically, we
201 // want to implement a separate computation which evaluates the relocation
202 // entry as the linker would, and verifies that the resultant fixup value is
203 // exactly what the encoder wanted. This will catch several classes of
206 // - Relocation entry bugs, the two algorithms are unlikely to have the same
209 // - Relaxation issues, where we forget to relax something.
211 // - Input errors, where something cannot be correctly encoded. 'as' allows
212 // these through in many cases.
214 // Add a relocation to be output in the object file. At the time this is
215 // called, the symbol indexes are not know, so if the relocation refers
216 // to a symbol it should be passed as \p RelSymbol so that it can be updated
217 // afterwards. If the relocation doesn't refer to a symbol, nullptr should be
219 void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec,
220 MachO::any_relocation_info &MRE) {
221 RelAndSymbol P(RelSymbol, MRE);
222 Relocations[Sec].push_back(P);
225 void recordScatteredRelocation(const MCAssembler &Asm,
226 const MCAsmLayout &Layout,
227 const MCFragment *Fragment,
228 const MCFixup &Fixup, MCValue Target,
229 unsigned Log2Size, uint64_t &FixedValue);
231 void recordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
232 const MCFragment *Fragment, const MCFixup &Fixup,
233 MCValue Target, uint64_t &FixedValue);
235 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
236 const MCFragment *Fragment, const MCFixup &Fixup,
237 MCValue Target, uint64_t &FixedValue) override;
239 void bindIndirectSymbols(MCAssembler &Asm);
241 /// Compute the symbol table data.
242 void computeSymbolTable(MCAssembler &Asm,
243 std::vector<MachSymbolData> &LocalSymbolData,
244 std::vector<MachSymbolData> &ExternalSymbolData,
245 std::vector<MachSymbolData> &UndefinedSymbolData);
247 void computeSectionAddresses(const MCAssembler &Asm,
248 const MCAsmLayout &Layout);
250 void executePostLayoutBinding(MCAssembler &Asm,
251 const MCAsmLayout &Layout) override;
253 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
256 bool InSet) const override;
258 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
259 const MCSymbol &SymA,
260 const MCFragment &FB, bool InSet,
261 bool IsPCRel) const override;
263 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
266 /// 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 std::unique_ptr<MCObjectWriter>
274 createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
275 raw_pwrite_stream &OS, bool IsLittleEndian);
277 } // end namespace llvm
279 #endif // LLVM_MC_MCMACHOBJECTWRITER_H