1 //===- llvm/MC/MCMachObjectWriter.h - Mach Object Writer --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_MC_MCMACHOBJECTWRITER_H
10 #define LLVM_MC_MCMACHOBJECTWRITER_H
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/MachO.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/StringTableBuilder.h"
19 #include "llvm/Support/EndianStream.h"
27 class MachObjectWriter;
29 class MCMachObjectTargetWriter : public MCObjectTargetWriter {
30 const unsigned Is64Bit : 1;
31 const uint32_t CPUType;
35 unsigned LocalDifference_RIT;
38 MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
39 uint32_t CPUSubtype_);
41 void setLocalDifferenceRelocationType(unsigned Type) {
42 LocalDifference_RIT = Type;
46 virtual ~MCMachObjectTargetWriter();
48 Triple::ObjectFormatType getFormat() const override { return Triple::MachO; }
49 static bool classof(const MCObjectTargetWriter *W) {
50 return W->getFormat() == Triple::MachO;
53 /// \name Lifetime Management
56 virtual void reset() {}
63 bool is64Bit() const { return Is64Bit; }
64 uint32_t getCPUType() const { return CPUType; }
65 uint32_t getCPUSubtype() const { return CPUSubtype; }
66 unsigned getLocalDifferenceRelocationType() const {
67 return LocalDifference_RIT;
75 virtual void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
76 const MCAsmLayout &Layout,
77 const MCFragment *Fragment,
78 const MCFixup &Fixup, MCValue Target,
79 uint64_t &FixedValue) = 0;
84 class MachObjectWriter : public MCObjectWriter {
85 /// Helper struct for containing some precomputed information on symbols.
86 struct MachSymbolData {
87 const MCSymbol *Symbol;
91 // Support lexicographic sorting.
92 bool operator<(const MachSymbolData &RHS) const;
95 /// The target specific Mach-O writer instance.
96 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
98 /// \name Relocation Data
101 struct RelAndSymbol {
103 MachO::any_relocation_info MRE;
104 RelAndSymbol(const MCSymbol *Sym, const MachO::any_relocation_info &MRE)
105 : Sym(Sym), MRE(MRE) {}
108 DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
109 DenseMap<const MCSection *, unsigned> IndirectSymBase;
111 SectionAddrMap SectionAddress;
114 /// \name Symbol Table Data
117 StringTableBuilder StringTable{StringTableBuilder::MachO};
118 std::vector<MachSymbolData> LocalSymbolData;
119 std::vector<MachSymbolData> ExternalSymbolData;
120 std::vector<MachSymbolData> UndefinedSymbolData;
124 MachSymbolData *findSymbolData(const MCSymbol &Sym);
126 void writeWithPadding(StringRef Str, uint64_t Size);
129 MachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
130 raw_pwrite_stream &OS, bool IsLittleEndian)
131 : TargetObjectWriter(std::move(MOTW)),
132 W(OS, IsLittleEndian ? support::little : support::big) {}
134 support::endian::Writer W;
136 const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
138 /// \name Lifetime management Methods
141 void reset() override;
145 /// \name Utility Methods
148 bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
150 SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
152 uint64_t getSectionAddress(const MCSection *Sec) const {
153 return SectionAddress.lookup(Sec);
155 uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
157 uint64_t getFragmentAddress(const MCFragment *Fragment,
158 const MCAsmLayout &Layout) const;
160 uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
162 bool doesSymbolRequireExternRelocation(const MCSymbol &S);
166 /// \name Target Writer Proxy Accessors
169 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
170 bool isX86_64() const {
171 uint32_t CPUType = TargetObjectWriter->getCPUType();
172 return CPUType == MachO::CPU_TYPE_X86_64;
177 void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands,
178 unsigned LoadCommandsSize, bool SubsectionsViaSymbols);
180 /// Write a segment load command.
182 /// \param NumSections The number of sections in this segment.
183 /// \param SectionDataSize The total size of the sections.
184 void writeSegmentLoadCommand(StringRef Name, unsigned NumSections,
185 uint64_t VMAddr, uint64_t VMSize,
186 uint64_t SectionDataStartOffset,
187 uint64_t SectionDataSize, uint32_t MaxProt,
190 void writeSection(const MCAsmLayout &Layout, const MCSection &Sec,
191 uint64_t VMAddr, uint64_t FileOffset, unsigned Flags,
192 uint64_t RelocationsStart, unsigned NumRelocations);
194 void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
195 uint32_t StringTableOffset,
196 uint32_t StringTableSize);
198 void writeDysymtabLoadCommand(
199 uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols,
200 uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols,
201 uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
202 uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
204 void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
206 void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
209 void writeLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
211 // FIXME: We really need to improve the relocation validation. Basically, we
212 // want to implement a separate computation which evaluates the relocation
213 // entry as the linker would, and verifies that the resultant fixup value is
214 // exactly what the encoder wanted. This will catch several classes of
217 // - Relocation entry bugs, the two algorithms are unlikely to have the same
220 // - Relaxation issues, where we forget to relax something.
222 // - Input errors, where something cannot be correctly encoded. 'as' allows
223 // these through in many cases.
225 // Add a relocation to be output in the object file. At the time this is
226 // called, the symbol indexes are not know, so if the relocation refers
227 // to a symbol it should be passed as \p RelSymbol so that it can be updated
228 // afterwards. If the relocation doesn't refer to a symbol, nullptr should be
230 void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec,
231 MachO::any_relocation_info &MRE) {
232 RelAndSymbol P(RelSymbol, MRE);
233 Relocations[Sec].push_back(P);
236 void recordScatteredRelocation(const MCAssembler &Asm,
237 const MCAsmLayout &Layout,
238 const MCFragment *Fragment,
239 const MCFixup &Fixup, MCValue Target,
240 unsigned Log2Size, uint64_t &FixedValue);
242 void recordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
243 const MCFragment *Fragment, const MCFixup &Fixup,
244 MCValue Target, uint64_t &FixedValue);
246 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
247 const MCFragment *Fragment, const MCFixup &Fixup,
248 MCValue Target, uint64_t &FixedValue) override;
250 void bindIndirectSymbols(MCAssembler &Asm);
252 /// Compute the symbol table data.
253 void computeSymbolTable(MCAssembler &Asm,
254 std::vector<MachSymbolData> &LocalSymbolData,
255 std::vector<MachSymbolData> &ExternalSymbolData,
256 std::vector<MachSymbolData> &UndefinedSymbolData);
258 void computeSectionAddresses(const MCAssembler &Asm,
259 const MCAsmLayout &Layout);
261 void executePostLayoutBinding(MCAssembler &Asm,
262 const MCAsmLayout &Layout) override;
264 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
267 bool InSet) const override;
269 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
270 const MCSymbol &SymA,
271 const MCFragment &FB, bool InSet,
272 bool IsPCRel) const override;
274 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
277 /// Construct a new Mach-O writer instance.
279 /// This routine takes ownership of the target writer subclass.
281 /// \param MOTW - The target specific Mach-O writer subclass.
282 /// \param OS - The stream to write to.
283 /// \returns The constructed object writer.
284 std::unique_ptr<MCObjectWriter>
285 createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
286 raw_pwrite_stream &OS, bool IsLittleEndian);
288 } // end namespace llvm
290 #endif // LLVM_MC_MCMACHOBJECTWRITER_H