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 /// MachSymbolData - Helper struct for containing some precomputed information
77 struct MachSymbolData {
78 const MCSymbol *Symbol;
82 // Support lexicographic sorting.
83 bool operator<(const MachSymbolData &RHS) const;
86 /// The target specific Mach-O writer instance.
87 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
89 /// \name Relocation Data
94 MachO::any_relocation_info MRE;
95 RelAndSymbol(const MCSymbol *Sym, const MachO::any_relocation_info &MRE)
96 : Sym(Sym), MRE(MRE) {}
99 llvm::DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
100 llvm::DenseMap<const MCSection *, unsigned> IndirectSymBase;
103 /// \name Symbol Table Data
106 StringTableBuilder StringTable;
107 std::vector<MachSymbolData> LocalSymbolData;
108 std::vector<MachSymbolData> ExternalSymbolData;
109 std::vector<MachSymbolData> UndefinedSymbolData;
113 MachSymbolData *findSymbolData(const MCSymbol &Sym);
116 MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_pwrite_stream &OS,
118 : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
120 const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
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 MCSection *Sec) const {
139 return SectionAddress.lookup(Sec);
141 uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
143 uint64_t getFragmentAddress(const MCFragment *Fragment,
144 const MCAsmLayout &Layout) const;
146 uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
148 bool doesSymbolRequireExternRelocation(const MCSymbol &S);
152 /// \name Target Writer Proxy Accessors
155 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
156 bool isX86_64() const {
157 uint32_t CPUType = TargetObjectWriter->getCPUType();
158 return CPUType == MachO::CPU_TYPE_X86_64;
163 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
164 bool SubsectionsViaSymbols);
166 /// WriteSegmentLoadCommand - Write a segment load command.
168 /// \param NumSections The number of sections in this segment.
169 /// \param SectionDataSize The total size of the sections.
170 void WriteSegmentLoadCommand(unsigned NumSections,
172 uint64_t SectionDataStartOffset,
173 uint64_t SectionDataSize);
175 void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
176 const MCSection &Sec, uint64_t FileOffset,
177 uint64_t RelocationsStart, unsigned NumRelocations);
179 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
180 uint32_t StringTableOffset,
181 uint32_t StringTableSize);
183 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
184 uint32_t NumLocalSymbols,
185 uint32_t FirstExternalSymbol,
186 uint32_t NumExternalSymbols,
187 uint32_t FirstUndefinedSymbol,
188 uint32_t NumUndefinedSymbols,
189 uint32_t IndirectSymbolOffset,
190 uint32_t NumIndirectSymbols);
192 void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
194 void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
197 void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
199 // FIXME: We really need to improve the relocation validation. Basically, we
200 // want to implement a separate computation which evaluates the relocation
201 // entry as the linker would, and verifies that the resultant fixup value is
202 // exactly what the encoder wanted. This will catch several classes of
205 // - Relocation entry bugs, the two algorithms are unlikely to have the same
208 // - Relaxation issues, where we forget to relax something.
210 // - Input errors, where something cannot be correctly encoded. 'as' allows
211 // these through in many cases.
213 // Add a relocation to be output in the object file. At the time this is
214 // called, the symbol indexes are not know, so if the relocation refers
215 // to a symbol it should be passed as \p RelSymbol so that it can be updated
216 // afterwards. If the relocation doesn't refer to a symbol, nullptr should be
218 void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec,
219 MachO::any_relocation_info &MRE) {
220 RelAndSymbol P(RelSymbol, MRE);
221 Relocations[Sec].push_back(P);
224 void RecordScatteredRelocation(const MCAssembler &Asm,
225 const MCAsmLayout &Layout,
226 const MCFragment *Fragment,
227 const MCFixup &Fixup, MCValue Target,
229 uint64_t &FixedValue);
231 void RecordTLVPRelocation(const MCAssembler &Asm,
232 const MCAsmLayout &Layout,
233 const MCFragment *Fragment,
234 const MCFixup &Fixup, MCValue Target,
235 uint64_t &FixedValue);
237 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
238 const MCFragment *Fragment, const MCFixup &Fixup,
239 MCValue Target, bool &IsPCRel,
240 uint64_t &FixedValue) override;
242 void BindIndirectSymbols(MCAssembler &Asm);
244 /// ComputeSymbolTable - Compute the symbol table data
246 void ComputeSymbolTable(MCAssembler &Asm,
247 std::vector<MachSymbolData> &LocalSymbolData,
248 std::vector<MachSymbolData> &ExternalSymbolData,
249 std::vector<MachSymbolData> &UndefinedSymbolData);
251 void computeSectionAddresses(const MCAssembler &Asm,
252 const MCAsmLayout &Layout);
254 void ExecutePostLayoutBinding(MCAssembler &Asm,
255 const MCAsmLayout &Layout) override;
257 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
258 const MCSymbol &SymA,
259 const MCFragment &FB, bool InSet,
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_pwrite_stream &OS,
275 bool IsLittleEndian);
277 } // End llvm namespace