LLVM  5.0.0
MCMachObjectWriter.h
Go to the documentation of this file.
1 //===- llvm/MC/MCMachObjectWriter.h - Mach Object Writer --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCMACHOBJECTWRITER_H
11 #define LLVM_MC_MCMACHOBJECTWRITER_H
12 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/StringRef.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSection.h"
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 namespace llvm {
26 
27 class MachObjectWriter;
28 
30  const unsigned Is64Bit : 1;
31  const uint32_t CPUType;
32  const uint32_t CPUSubtype;
33  unsigned LocalDifference_RIT;
34 
35 protected:
36  MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
37  uint32_t CPUSubtype_);
38 
40  LocalDifference_RIT = Type;
41  }
42 
43 public:
44  virtual ~MCMachObjectTargetWriter();
45 
46  /// \name Lifetime Management
47  /// @{
48 
49  virtual void reset() {}
50 
51  /// @}
52 
53  /// \name Accessors
54  /// @{
55 
56  bool is64Bit() const { return Is64Bit; }
57  uint32_t getCPUType() const { return CPUType; }
58  uint32_t getCPUSubtype() const { return CPUSubtype; }
60  return LocalDifference_RIT;
61  }
62 
63  /// @}
64 
65  /// \name API
66  /// @{
67 
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;
73 
74  /// @}
75 };
76 
78  /// Helper struct for containing some precomputed information on symbols.
79  struct MachSymbolData {
80  const MCSymbol *Symbol;
81  uint64_t StringIndex;
82  uint8_t SectionIndex;
83 
84  // Support lexicographic sorting.
85  bool operator<(const MachSymbolData &RHS) const;
86  };
87 
88  /// The target specific Mach-O writer instance.
89  std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
90 
91  /// \name Relocation Data
92  /// @{
93 
94  struct RelAndSymbol {
95  const MCSymbol *Sym;
97  RelAndSymbol(const MCSymbol *Sym, const MachO::any_relocation_info &MRE)
98  : Sym(Sym), MRE(MRE) {}
99  };
100 
103 
104  SectionAddrMap SectionAddress;
105 
106  /// @}
107  /// \name Symbol Table Data
108  /// @{
109 
111  std::vector<MachSymbolData> LocalSymbolData;
112  std::vector<MachSymbolData> ExternalSymbolData;
113  std::vector<MachSymbolData> UndefinedSymbolData;
114 
115  /// @}
116 
117  MachSymbolData *findSymbolData(const MCSymbol &Sym);
118 
119 public:
121  bool IsLittleEndian)
122  : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
123 
124  const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
125 
126  /// \name Lifetime management Methods
127  /// @{
128 
129  void reset() override;
130 
131  /// @}
132 
133  /// \name Utility Methods
134  /// @{
135 
136  bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
137 
138  SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
139 
140  uint64_t getSectionAddress(const MCSection *Sec) const {
141  return SectionAddress.lookup(Sec);
142  }
143  uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
144 
145  uint64_t getFragmentAddress(const MCFragment *Fragment,
146  const MCAsmLayout &Layout) const;
147 
148  uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
149 
151 
152  /// @}
153 
154  /// \name Target Writer Proxy Accessors
155  /// @{
156 
157  bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
158  bool isX86_64() const {
159  uint32_t CPUType = TargetObjectWriter->getCPUType();
160  return CPUType == MachO::CPU_TYPE_X86_64;
161  }
162 
163  /// @}
164 
165  void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands,
166  unsigned LoadCommandsSize, bool SubsectionsViaSymbols);
167 
168  /// Write a segment load command.
169  ///
170  /// \param NumSections The number of sections in this segment.
171  /// \param SectionDataSize The total size of the sections.
172  void writeSegmentLoadCommand(StringRef Name, unsigned NumSections,
173  uint64_t VMAddr, uint64_t VMSize,
174  uint64_t SectionDataStartOffset,
175  uint64_t SectionDataSize, uint32_t MaxProt,
176  uint32_t InitProt);
177 
178  void writeSection(const MCAsmLayout &Layout, const MCSection &Sec,
179  uint64_t VMAddr, uint64_t FileOffset, unsigned Flags,
180  uint64_t RelocationsStart, unsigned NumRelocations);
181 
182  void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
183  uint32_t StringTableOffset,
184  uint32_t StringTableSize);
185 
187  uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols,
188  uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols,
189  uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
190  uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
191 
192  void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
193 
195  uint32_t DataSize);
196 
197  void writeLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
198 
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
203  // problems:
204  //
205  // - Relocation entry bugs, the two algorithms are unlikely to have the same
206  // exact bug.
207  //
208  // - Relaxation issues, where we forget to relax something.
209  //
210  // - Input errors, where something cannot be correctly encoded. 'as' allows
211  // these through in many cases.
212 
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
217  // used.
218  void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec,
220  RelAndSymbol P(RelSymbol, MRE);
221  Relocations[Sec].push_back(P);
222  }
223 
225  const MCAsmLayout &Layout,
226  const MCFragment *Fragment,
227  const MCFixup &Fixup, MCValue Target,
228  unsigned Log2Size, uint64_t &FixedValue);
229 
230  void recordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
231  const MCFragment *Fragment, const MCFixup &Fixup,
232  MCValue Target, uint64_t &FixedValue);
233 
234  void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
235  const MCFragment *Fragment, const MCFixup &Fixup,
236  MCValue Target, uint64_t &FixedValue) override;
237 
239 
240  /// Compute the symbol table data.
242  std::vector<MachSymbolData> &LocalSymbolData,
243  std::vector<MachSymbolData> &ExternalSymbolData,
244  std::vector<MachSymbolData> &UndefinedSymbolData);
245 
247  const MCAsmLayout &Layout);
248 
250  const MCAsmLayout &Layout) override;
251 
253  const MCSymbol &A,
254  const MCSymbol &B,
255  bool InSet) const override;
256 
258  const MCSymbol &SymA,
259  const MCFragment &FB, bool InSet,
260  bool IsPCRel) const override;
261 
262  void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
263 };
264 
265 /// Construct a new Mach-O writer instance.
266 ///
267 /// This routine takes ownership of the target writer subclass.
268 ///
269 /// \param MOTW - The target specific Mach-O writer subclass.
270 /// \param OS - The stream to write to.
271 /// \returns The constructed object writer.
272 MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
273  raw_pwrite_stream &OS,
274  bool IsLittleEndian);
275 
276 } // end namespace llvm
277 
278 #endif // LLVM_MC_MCMACHOBJECTWRITER_H
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void recordScatteredRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, unsigned Log2Size, uint64_t &FixedValue)
MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:166
void computeSectionAddresses(const MCAssembler &Asm, const MCAsmLayout &Layout)
bool doesSymbolRequireExternRelocation(const MCSymbol &S)
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
uint64_t getSectionAddress(const MCSection *Sec) const
void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout) override
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
void writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols, uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols, uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols, uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols)
void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override
Write the object file.
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, bool InSet) const override
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
HeaderFileType
Definition: MachO.h:37
void setLocalDifferenceRelocationType(unsigned Type)
void writeSegmentLoadCommand(StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize, uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt, uint32_t InitProt)
Write a segment load command.
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind)
Utility for building string tables with deduplicated suffixes.
void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec, MachO::any_relocation_info &MRE)
void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, uint32_t StringTableOffset, uint32_t StringTableSize)
SectionAddrMap & getSectionAddressMap()
void writeLinkerOptionsLoadCommand(const std::vector< std::string > &Options)
const char * Name
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
#define P(N)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
#define A
Definition: LargeTest.cpp:12
void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands, unsigned LoadCommandsSize, bool SubsectionsViaSymbols)
unsigned getLocalDifferenceRelocationType() const
#define B
Definition: LargeTest.cpp:24
void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset, uint32_t DataSize)
void writeSection(const MCAsmLayout &Layout, const MCSection &Sec, uint64_t VMAddr, uint64_t FileOffset, unsigned Flags, uint64_t RelocationsStart, unsigned NumRelocations)
uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const
uint64_t getFragmentAddress(const MCFragment *Fragment, const MCAsmLayout &Layout) const
Target - Wrapper for Target specific information.
MCObjectWriter * createMachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.
void computeSymbolTable(MCAssembler &Asm, std::vector< MachSymbolData > &LocalSymbolData, std::vector< MachSymbolData > &ExternalSymbolData, std::vector< MachSymbolData > &UndefinedSymbolData)
Compute the symbol table data.
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
virtual void recordRelocation(MachObjectWriter *Writer, MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
void recordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
const unsigned Kind
void reset() override
lifetime management
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:326
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void bindIndirectSymbols(MCAssembler &Asm)
uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const
const MCSymbol & findAliasedSymbol(const MCSymbol &Sym) const
void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout)
MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_, uint32_t CPUSubtype_)