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"
26 class MachObjectWriter;
28 class MCMachObjectTargetWriter : public MCObjectTargetWriter {
29 const unsigned Is64Bit : 1;
30 const uint32_t CPUType;
34 unsigned LocalDifference_RIT;
37 MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
38 uint32_t CPUSubtype_);
40 void setLocalDifferenceRelocationType(unsigned Type) {
41 LocalDifference_RIT = Type;
45 virtual ~MCMachObjectTargetWriter();
47 virtual Triple::ObjectFormatType getFormat() const { return Triple::MachO; }
48 static bool classof(const MCObjectTargetWriter *W) {
49 return W->getFormat() == Triple::MachO;
52 /// \name Lifetime Management
55 virtual void reset() {}
62 bool is64Bit() const { return Is64Bit; }
63 uint32_t getCPUType() const { return CPUType; }
64 uint32_t getCPUSubtype() const { return CPUSubtype; }
65 unsigned getLocalDifferenceRelocationType() const {
66 return LocalDifference_RIT;
74 virtual void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm,
75 const MCAsmLayout &Layout,
76 const MCFragment *Fragment,
77 const MCFixup &Fixup, MCValue Target,
78 uint64_t &FixedValue) = 0;
83 class MachObjectWriter : public MCObjectWriter {
84 /// Helper struct for containing some precomputed information on symbols.
85 struct MachSymbolData {
86 const MCSymbol *Symbol;
90 // Support lexicographic sorting.
91 bool operator<(const MachSymbolData &RHS) const;
94 /// The target specific Mach-O writer instance.
95 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
97 /// \name Relocation Data
100 struct RelAndSymbol {
102 MachO::any_relocation_info MRE;
103 RelAndSymbol(const MCSymbol *Sym, const MachO::any_relocation_info &MRE)
104 : Sym(Sym), MRE(MRE) {}
107 DenseMap<const MCSection *, std::vector<RelAndSymbol>> Relocations;
108 DenseMap<const MCSection *, unsigned> IndirectSymBase;
110 SectionAddrMap SectionAddress;
113 /// \name Symbol Table Data
116 StringTableBuilder StringTable{StringTableBuilder::MachO};
117 std::vector<MachSymbolData> LocalSymbolData;
118 std::vector<MachSymbolData> ExternalSymbolData;
119 std::vector<MachSymbolData> UndefinedSymbolData;
123 MachSymbolData *findSymbolData(const MCSymbol &Sym);
125 void writeWithPadding(StringRef Str, uint64_t Size);
128 MachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
129 raw_pwrite_stream &OS, bool IsLittleEndian)
130 : TargetObjectWriter(std::move(MOTW)),
131 W(OS, IsLittleEndian ? support::little : support::big) {}
133 support::endian::Writer W;
135 const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
137 /// \name Lifetime management Methods
140 void reset() override;
144 /// \name Utility Methods
147 bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
149 SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
151 uint64_t getSectionAddress(const MCSection *Sec) const {
152 return SectionAddress.lookup(Sec);
154 uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
156 uint64_t getFragmentAddress(const MCFragment *Fragment,
157 const MCAsmLayout &Layout) const;
159 uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
161 bool doesSymbolRequireExternRelocation(const MCSymbol &S);
165 /// \name Target Writer Proxy Accessors
168 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
169 bool isX86_64() const {
170 uint32_t CPUType = TargetObjectWriter->getCPUType();
171 return CPUType == MachO::CPU_TYPE_X86_64;
176 void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands,
177 unsigned LoadCommandsSize, bool SubsectionsViaSymbols);
179 /// Write a segment load command.
181 /// \param NumSections The number of sections in this segment.
182 /// \param SectionDataSize The total size of the sections.
183 void writeSegmentLoadCommand(StringRef Name, unsigned NumSections,
184 uint64_t VMAddr, uint64_t VMSize,
185 uint64_t SectionDataStartOffset,
186 uint64_t SectionDataSize, uint32_t MaxProt,
189 void writeSection(const MCAsmLayout &Layout, const MCSection &Sec,
190 uint64_t VMAddr, uint64_t FileOffset, unsigned Flags,
191 uint64_t RelocationsStart, unsigned NumRelocations);
193 void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
194 uint32_t StringTableOffset,
195 uint32_t StringTableSize);
197 void writeDysymtabLoadCommand(
198 uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols,
199 uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols,
200 uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
201 uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
203 void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
205 void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
208 void writeLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
210 // FIXME: We really need to improve the relocation validation. Basically, we
211 // want to implement a separate computation which evaluates the relocation
212 // entry as the linker would, and verifies that the resultant fixup value is
213 // exactly what the encoder wanted. This will catch several classes of
216 // - Relocation entry bugs, the two algorithms are unlikely to have the same
219 // - Relaxation issues, where we forget to relax something.
221 // - Input errors, where something cannot be correctly encoded. 'as' allows
222 // these through in many cases.
224 // Add a relocation to be output in the object file. At the time this is
225 // called, the symbol indexes are not know, so if the relocation refers
226 // to a symbol it should be passed as \p RelSymbol so that it can be updated
227 // afterwards. If the relocation doesn't refer to a symbol, nullptr should be
229 void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec,
230 MachO::any_relocation_info &MRE) {
231 RelAndSymbol P(RelSymbol, MRE);
232 Relocations[Sec].push_back(P);
235 void recordScatteredRelocation(const MCAssembler &Asm,
236 const MCAsmLayout &Layout,
237 const MCFragment *Fragment,
238 const MCFixup &Fixup, MCValue Target,
239 unsigned Log2Size, uint64_t &FixedValue);
241 void recordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
242 const MCFragment *Fragment, const MCFixup &Fixup,
243 MCValue Target, uint64_t &FixedValue);
245 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
246 const MCFragment *Fragment, const MCFixup &Fixup,
247 MCValue Target, uint64_t &FixedValue) override;
249 void bindIndirectSymbols(MCAssembler &Asm);
251 /// Compute the symbol table data.
252 void computeSymbolTable(MCAssembler &Asm,
253 std::vector<MachSymbolData> &LocalSymbolData,
254 std::vector<MachSymbolData> &ExternalSymbolData,
255 std::vector<MachSymbolData> &UndefinedSymbolData);
257 void computeSectionAddresses(const MCAssembler &Asm,
258 const MCAsmLayout &Layout);
260 void executePostLayoutBinding(MCAssembler &Asm,
261 const MCAsmLayout &Layout) override;
263 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
266 bool InSet) const override;
268 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
269 const MCSymbol &SymA,
270 const MCFragment &FB, bool InSet,
271 bool IsPCRel) const override;
273 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
276 /// Construct a new Mach-O writer instance.
278 /// This routine takes ownership of the target writer subclass.
280 /// \param MOTW - The target specific Mach-O writer subclass.
281 /// \param OS - The stream to write to.
282 /// \returns The constructed object writer.
283 std::unique_ptr<MCObjectWriter>
284 createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
285 raw_pwrite_stream &OS, bool IsLittleEndian);
287 } // end namespace llvm
289 #endif // LLVM_MC_MCMACHOBJECTWRITER_H