LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/OwningPtr.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/MachO.h"
20 #include <vector>
21 
22 namespace llvm {
23 
24 class MCSectionData;
25 class MachObjectWriter;
26 
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;
35 
36 protected:
37  MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
38  uint32_t CPUSubtype_,
39  bool UseAggressiveSymbolFolding_ = false);
40 
42  LocalDifference_RIT = Type;
43  }
44 
45 public:
46  virtual ~MCMachObjectTargetWriter();
47 
48  /// @name Lifetime Management
49  /// @{
50 
51  virtual void reset() {};
52 
53  /// @}
54 
55  /// @name Accessors
56  /// @{
57 
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; }
63  return LocalDifference_RIT;
64  }
65 
66  /// @}
67 
68  /// @name API
69  /// @{
70 
71  virtual void RecordRelocation(MachObjectWriter *Writer,
72  const MCAssembler &Asm,
73  const MCAsmLayout &Layout,
74  const MCFragment *Fragment,
75  const MCFixup &Fixup,
77  uint64_t &FixedValue) = 0;
78 
79  /// @}
80 };
81 
83  /// MachSymbolData - Helper struct for containing some precomputed information
84  /// on symbols.
85  struct MachSymbolData {
86  MCSymbolData *SymbolData;
87  uint64_t StringIndex;
88  uint8_t SectionIndex;
89 
90  // Support lexicographic sorting.
91  bool operator<(const MachSymbolData &RHS) const;
92  };
93 
94  /// The target specific Mach-O writer instance.
96 
97  /// @name Relocation Data
98  /// @{
99 
101  std::vector<MachO::any_relocation_info> > Relocations;
103 
104  /// @}
105  /// @name Symbol Table Data
106  /// @{
107 
108  SmallString<256> StringTable;
109  std::vector<MachSymbolData> LocalSymbolData;
110  std::vector<MachSymbolData> ExternalSymbolData;
111  std::vector<MachSymbolData> UndefinedSymbolData;
112 
113  /// @}
114 
115 public:
117  bool _IsLittleEndian)
118  : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
119  }
120 
121  /// @name Lifetime management Methods
122  /// @{
123 
124  virtual void reset();
125 
126  /// @}
127 
128  /// @name Utility Methods
129  /// @{
130 
131  bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
132 
134 
136 
137  uint64_t getSectionAddress(const MCSectionData* SD) const {
138  return SectionAddress.lookup(SD);
139  }
140  uint64_t getSymbolAddress(const MCSymbolData* SD,
141  const MCAsmLayout &Layout) const;
142 
143  uint64_t getFragmentAddress(const MCFragment *Fragment,
144  const MCAsmLayout &Layout) const;
145 
146  uint64_t getPaddingSize(const MCSectionData *SD,
147  const MCAsmLayout &Layout) const;
148 
150 
151  /// @}
152 
153  /// @name Target Writer Proxy Accessors
154  /// @{
155 
156  bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
157  bool isARM() const {
158  uint32_t CPUType = TargetObjectWriter->getCPUType() & ~MachO::CPU_ARCH_MASK;
159  return CPUType == MachO::CPU_TYPE_ARM;
160  }
161 
162  /// @}
163 
164  void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
165  bool SubsectionsViaSymbols);
166 
167  /// WriteSegmentLoadCommand - Write a segment load command.
168  ///
169  /// \param NumSections The number of sections in this segment.
170  /// \param SectionDataSize The total size of the sections.
171  void WriteSegmentLoadCommand(unsigned NumSections,
172  uint64_t VMSize,
173  uint64_t SectionDataStartOffset,
174  uint64_t SectionDataSize);
175 
176  void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
177  const MCSectionData &SD, uint64_t FileOffset,
178  uint64_t RelocationsStart, unsigned NumRelocations);
179 
180  void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
181  uint32_t StringTableOffset,
182  uint32_t StringTableSize);
183 
184  void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
185  uint32_t NumLocalSymbols,
186  uint32_t FirstExternalSymbol,
187  uint32_t NumExternalSymbols,
188  uint32_t FirstUndefinedSymbol,
189  uint32_t NumUndefinedSymbols,
190  uint32_t IndirectSymbolOffset,
191  uint32_t NumIndirectSymbols);
192 
193  void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
194 
195  void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
196  uint32_t DataSize);
197 
198  void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
199 
200  // FIXME: We really need to improve the relocation validation. Basically, we
201  // want to implement a separate computation which evaluates the relocation
202  // entry as the linker would, and verifies that the resultant fixup value is
203  // exactly what the encoder wanted. This will catch several classes of
204  // problems:
205  //
206  // - Relocation entry bugs, the two algorithms are unlikely to have the same
207  // exact bug.
208  //
209  // - Relaxation issues, where we forget to relax something.
210  //
211  // - Input errors, where something cannot be correctly encoded. 'as' allows
212  // these through in many cases.
213 
214  void addRelocation(const MCSectionData *SD,
216  Relocations[SD].push_back(MRE);
217  }
218 
220  const MCAsmLayout &Layout,
221  const MCFragment *Fragment,
222  const MCFixup &Fixup, MCValue Target,
223  unsigned Log2Size,
224  uint64_t &FixedValue);
225 
227  const MCAsmLayout &Layout,
228  const MCFragment *Fragment,
229  const MCFixup &Fixup, MCValue Target,
230  uint64_t &FixedValue);
231 
232  void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
233  const MCFragment *Fragment, const MCFixup &Fixup,
234  MCValue Target, uint64_t &FixedValue);
235 
237 
238  /// ComputeSymbolTable - Compute the symbol table data
239  ///
240  /// \param StringTable [out] - The string 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);
251  void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout);
252 
254  const MCSymbolData &DataA,
255  const MCFragment &FB,
256  bool InSet,
257  bool IsPCRel) const;
258 
259  void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
260 };
261 
262 
263 /// \brief Construct a new Mach-O writer instance.
264 ///
265 /// This routine takes ownership of the target writer subclass.
266 ///
267 /// \param MOTW - The target specific Mach-O writer subclass.
268 /// \param OS - The stream to write to.
269 /// \returns The constructed object writer.
270 MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
271  raw_ostream &OS, bool IsLittleEndian);
272 
273 } // End llvm namespace
274 
275 #endif
void markAbsoluteVariableSymbols(MCAssembler &Asm, const MCAsmLayout &Layout)
COFF::RelocationTypeX86 Type
Definition: COFFYAML.cpp:227
virtual void reset()
lifetime management
void computeSectionAddresses(const MCAssembler &Asm, const MCAsmLayout &Layout)
void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout)
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
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 RecordScatteredRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, unsigned Log2Size, uint64_t &FixedValue)
void operator<(const Optional< T > &X, const Optional< U > &Y)
Poison comparison between two Optional objects. Clients needs to explicitly compare the underlying va...
MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_, uint32_t CPUSubtype_, bool UseAggressiveSymbolFolding_=false)
void RecordTLVPRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)
void WriteSegmentLoadCommand(unsigned NumSections, uint64_t VMSize, uint64_t SectionDataStartOffset, uint64_t SectionDataSize)
void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset, uint32_t DataSize)
void WriteLinkerOptionsLoadCommand(const std::vector< std::string > &Options)
uint64_t getSymbolAddress(const MCSymbolData *SD, const MCAsmLayout &Layout) const
void setLocalDifferenceRelocationType(unsigned Type)
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind)
uint64_t getSectionAddress(const MCSectionData *SD) const
void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCSectionData &SD, uint64_t FileOffset, uint64_t RelocationsStart, unsigned NumRelocations)
virtual void RecordRelocation(MachObjectWriter *Writer, const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
SectionAddrMap & getSectionAddressMap()
void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout)
Write the object file.
void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout)
MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS, bool _IsLittleEndian)
unsigned getLocalDifferenceRelocationType() const
virtual bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbolData &DataA, const MCFragment &FB, bool InSet, bool IsPCRel) const
uint64_t getPaddingSize(const MCSectionData *SD, const MCAsmLayout &Layout) const
uint64_t getFragmentAddress(const MCFragment *Fragment, const MCAsmLayout &Layout) const
bool doesSymbolRequireExternRelocation(const MCSymbolData *SD)
void BindIndirectSymbols(MCAssembler &Asm)
ValueT lookup(const KeyT &Val) const
Definition: DenseMap.h:143
void addRelocation(const MCSectionData *SD, MachO::any_relocation_info &MRE)
void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)
Record a relocation entry.
void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, uint32_t StringTableOffset, uint32_t StringTableSize)
void ComputeSymbolTable(MCAssembler &Asm, SmallString< 256 > &StringTable, std::vector< MachSymbolData > &LocalSymbolData, std::vector< MachSymbolData > &ExternalSymbolData, std::vector< MachSymbolData > &UndefinedSymbolData)
void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize, bool SubsectionsViaSymbols)
MCObjectWriter * createMachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.