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"
25 class MachObjectWriter;
27 class MCMachObjectTargetWriter {
28 const unsigned Is64Bit : 1;
29 const uint32_t CPUType;
30 const uint32_t CPUSubtype;
31 // FIXME: Remove this, we should just always use it once we no longer care
32 // about Darwin 'as' compatibility.
33 const unsigned UseAggressiveSymbolFolding : 1;
34 unsigned LocalDifference_RIT;
37 MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
39 bool UseAggressiveSymbolFolding_ = false);
41 void setLocalDifferenceRelocationType(unsigned Type) {
42 LocalDifference_RIT = Type;
46 virtual ~MCMachObjectTargetWriter();
48 /// @name Lifetime Management
51 virtual void reset() {};
58 bool is64Bit() const { return Is64Bit; }
59 bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; }
60 uint32_t getCPUType() const { return CPUType; }
61 uint32_t getCPUSubtype() const { return CPUSubtype; }
62 unsigned getLocalDifferenceRelocationType() const {
63 return LocalDifference_RIT;
71 virtual void RecordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
72 const MCAsmLayout &Layout,
73 const MCFragment *Fragment,
74 const MCFixup &Fixup, MCValue Target,
75 uint64_t &FixedValue) = 0;
80 class MachObjectWriter : public MCObjectWriter {
81 /// MachSymbolData - Helper struct for containing some precomputed information
83 struct MachSymbolData {
84 MCSymbolData *SymbolData;
88 // Support lexicographic sorting.
89 bool operator<(const MachSymbolData &RHS) const;
92 /// The target specific Mach-O writer instance.
93 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
95 /// @name Relocation Data
99 const MCSymbolData *Sym;
100 MachO::any_relocation_info MRE;
101 RelAndSymbol(const MCSymbolData *Sym, const MachO::any_relocation_info &MRE)
102 : Sym(Sym), MRE(MRE) {}
105 llvm::DenseMap<const MCSectionData *, std::vector<RelAndSymbol>> Relocations;
106 llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
109 /// @name Symbol Table Data
112 StringTableBuilder StringTable;
113 std::vector<MachSymbolData> LocalSymbolData;
114 std::vector<MachSymbolData> ExternalSymbolData;
115 std::vector<MachSymbolData> UndefinedSymbolData;
119 MachSymbolData *findSymbolData(const MCSymbol &Sym);
122 MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_pwrite_stream &OS,
124 : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
126 const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
128 /// @name Lifetime management Methods
131 void reset() override;
135 /// @name Utility Methods
138 bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
140 SectionAddrMap SectionAddress;
142 SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
144 uint64_t getSectionAddress(const MCSectionData* SD) const {
145 return SectionAddress.lookup(SD);
147 uint64_t getSymbolAddress(const MCSymbolData* SD,
148 const MCAsmLayout &Layout) const;
150 uint64_t getFragmentAddress(const MCFragment *Fragment,
151 const MCAsmLayout &Layout) const;
153 uint64_t getPaddingSize(const MCSectionData *SD,
154 const MCAsmLayout &Layout) const;
156 bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
160 /// @name Target Writer Proxy Accessors
163 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
164 bool isX86_64() const {
165 uint32_t CPUType = TargetObjectWriter->getCPUType();
166 return CPUType == MachO::CPU_TYPE_X86_64;
171 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
172 bool SubsectionsViaSymbols);
174 /// WriteSegmentLoadCommand - Write a segment load command.
176 /// \param NumSections The number of sections in this segment.
177 /// \param SectionDataSize The total size of the sections.
178 void WriteSegmentLoadCommand(unsigned NumSections,
180 uint64_t SectionDataStartOffset,
181 uint64_t SectionDataSize);
183 void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
184 const MCSectionData &SD, uint64_t FileOffset,
185 uint64_t RelocationsStart, unsigned NumRelocations);
187 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
188 uint32_t StringTableOffset,
189 uint32_t StringTableSize);
191 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
192 uint32_t NumLocalSymbols,
193 uint32_t FirstExternalSymbol,
194 uint32_t NumExternalSymbols,
195 uint32_t FirstUndefinedSymbol,
196 uint32_t NumUndefinedSymbols,
197 uint32_t IndirectSymbolOffset,
198 uint32_t NumIndirectSymbols);
200 void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
202 void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
205 void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
207 // FIXME: We really need to improve the relocation validation. Basically, we
208 // want to implement a separate computation which evaluates the relocation
209 // entry as the linker would, and verifies that the resultant fixup value is
210 // exactly what the encoder wanted. This will catch several classes of
213 // - Relocation entry bugs, the two algorithms are unlikely to have the same
216 // - Relaxation issues, where we forget to relax something.
218 // - Input errors, where something cannot be correctly encoded. 'as' allows
219 // these through in many cases.
221 // Add a relocation to be output in the object file. At the time this is
222 // called, the symbol indexes are not know, so if the relocation refers
223 // to a symbol it should be passed as \p RelSymbol so that it can be updated
224 // afterwards. If the relocation doesn't refer to a symbol, nullptr should be
226 void addRelocation(const MCSymbolData *RelSymbol, const MCSectionData *SD,
227 MachO::any_relocation_info &MRE) {
228 RelAndSymbol P(RelSymbol, MRE);
229 Relocations[SD].push_back(P);
232 void RecordScatteredRelocation(const MCAssembler &Asm,
233 const MCAsmLayout &Layout,
234 const MCFragment *Fragment,
235 const MCFixup &Fixup, MCValue Target,
237 uint64_t &FixedValue);
239 void RecordTLVPRelocation(const MCAssembler &Asm,
240 const MCAsmLayout &Layout,
241 const MCFragment *Fragment,
242 const MCFixup &Fixup, MCValue Target,
243 uint64_t &FixedValue);
245 void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
246 const MCFragment *Fragment, const MCFixup &Fixup,
247 MCValue Target, bool &IsPCRel,
248 uint64_t &FixedValue) override;
250 void BindIndirectSymbols(MCAssembler &Asm);
252 /// ComputeSymbolTable - Compute the symbol table data
254 void ComputeSymbolTable(MCAssembler &Asm,
255 std::vector<MachSymbolData> &LocalSymbolData,
256 std::vector<MachSymbolData> &ExternalSymbolData,
257 std::vector<MachSymbolData> &UndefinedSymbolData);
259 void computeSectionAddresses(const MCAssembler &Asm,
260 const MCAsmLayout &Layout);
262 void ExecutePostLayoutBinding(MCAssembler &Asm,
263 const MCAsmLayout &Layout) override;
265 bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
266 const MCSymbolData &DataA,
267 const MCFragment &FB,
269 bool IsPCRel) const override;
271 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
275 /// \brief Construct a new Mach-O writer instance.
277 /// This routine takes ownership of the target writer subclass.
279 /// \param MOTW - The target specific Mach-O writer subclass.
280 /// \param OS - The stream to write to.
281 /// \returns The constructed object writer.
282 MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
283 raw_pwrite_stream &OS,
284 bool IsLittleEndian);
286 } // End llvm namespace