Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
9.3 KiB

  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. #ifndef LLVM_MC_MCMACHOBJECTWRITER_H
  10. #define LLVM_MC_MCMACHOBJECTWRITER_H
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/OwningPtr.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/MC/MCExpr.h"
  15. #include "llvm/MC/MCObjectWriter.h"
  16. #include "llvm/Object/MachOFormat.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include <vector>
  19. namespace llvm {
  20. class MCSectionData;
  21. class MachObjectWriter;
  22. class MCMachObjectTargetWriter {
  23. const unsigned Is64Bit : 1;
  24. const uint32_t CPUType;
  25. const uint32_t CPUSubtype;
  26. // FIXME: Remove this, we should just always use it once we no longer care
  27. // about Darwin 'as' compatibility.
  28. const unsigned UseAggressiveSymbolFolding : 1;
  29. unsigned LocalDifference_RIT;
  30. protected:
  31. MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
  32. uint32_t CPUSubtype_,
  33. bool UseAggressiveSymbolFolding_ = false);
  34. void setLocalDifferenceRelocationType(unsigned Type) {
  35. LocalDifference_RIT = Type;
  36. }
  37. public:
  38. virtual ~MCMachObjectTargetWriter();
  39. /// @name Lifetime Management
  40. /// @{
  41. virtual void reset() {};
  42. /// @}
  43. /// @name Accessors
  44. /// @{
  45. bool is64Bit() const { return Is64Bit; }
  46. bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; }
  47. uint32_t getCPUType() const { return CPUType; }
  48. uint32_t getCPUSubtype() const { return CPUSubtype; }
  49. unsigned getLocalDifferenceRelocationType() const {
  50. return LocalDifference_RIT;
  51. }
  52. /// @}
  53. /// @name API
  54. /// @{
  55. virtual void RecordRelocation(MachObjectWriter *Writer,
  56. const MCAssembler &Asm,
  57. const MCAsmLayout &Layout,
  58. const MCFragment *Fragment,
  59. const MCFixup &Fixup,
  60. MCValue Target,
  61. uint64_t &FixedValue) = 0;
  62. /// @}
  63. };
  64. class MachObjectWriter : public MCObjectWriter {
  65. /// MachSymbolData - Helper struct for containing some precomputed information
  66. /// on symbols.
  67. struct MachSymbolData {
  68. MCSymbolData *SymbolData;
  69. uint64_t StringIndex;
  70. uint8_t SectionIndex;
  71. // Support lexicographic sorting.
  72. bool operator<(const MachSymbolData &RHS) const;
  73. };
  74. /// The target specific Mach-O writer instance.
  75. llvm::OwningPtr<MCMachObjectTargetWriter> TargetObjectWriter;
  76. /// @name Relocation Data
  77. /// @{
  78. llvm::DenseMap<const MCSectionData*,
  79. std::vector<object::macho::RelocationEntry> > Relocations;
  80. llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
  81. /// @}
  82. /// @name Symbol Table Data
  83. /// @{
  84. SmallString<256> StringTable;
  85. std::vector<MachSymbolData> LocalSymbolData;
  86. std::vector<MachSymbolData> ExternalSymbolData;
  87. std::vector<MachSymbolData> UndefinedSymbolData;
  88. /// @}
  89. public:
  90. MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
  91. bool _IsLittleEndian)
  92. : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
  93. }
  94. /// @name Lifetime management Methods
  95. /// @{
  96. virtual void reset();
  97. /// @}
  98. /// @name Utility Methods
  99. /// @{
  100. bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
  101. SectionAddrMap SectionAddress;
  102. SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
  103. uint64_t getSectionAddress(const MCSectionData* SD) const {
  104. return SectionAddress.lookup(SD);
  105. }
  106. uint64_t getSymbolAddress(const MCSymbolData* SD,
  107. const MCAsmLayout &Layout) const;
  108. uint64_t getFragmentAddress(const MCFragment *Fragment,
  109. const MCAsmLayout &Layout) const;
  110. uint64_t getPaddingSize(const MCSectionData *SD,
  111. const MCAsmLayout &Layout) const;
  112. bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
  113. /// @}
  114. /// @name Target Writer Proxy Accessors
  115. /// @{
  116. bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
  117. bool isARM() const {
  118. uint32_t CPUType = TargetObjectWriter->getCPUType() &
  119. ~object::mach::CTFM_ArchMask;
  120. return CPUType == object::mach::CTM_ARM;
  121. }
  122. /// @}
  123. void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
  124. bool SubsectionsViaSymbols);
  125. /// WriteSegmentLoadCommand - Write a segment load command.
  126. ///
  127. /// \param NumSections The number of sections in this segment.
  128. /// \param SectionDataSize The total size of the sections.
  129. void WriteSegmentLoadCommand(unsigned NumSections,
  130. uint64_t VMSize,
  131. uint64_t SectionDataStartOffset,
  132. uint64_t SectionDataSize);
  133. void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
  134. const MCSectionData &SD, uint64_t FileOffset,
  135. uint64_t RelocationsStart, unsigned NumRelocations);
  136. void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
  137. uint32_t StringTableOffset,
  138. uint32_t StringTableSize);
  139. void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
  140. uint32_t NumLocalSymbols,
  141. uint32_t FirstExternalSymbol,
  142. uint32_t NumExternalSymbols,
  143. uint32_t FirstUndefinedSymbol,
  144. uint32_t NumUndefinedSymbols,
  145. uint32_t IndirectSymbolOffset,
  146. uint32_t NumIndirectSymbols);
  147. void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
  148. void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
  149. uint32_t DataSize);
  150. void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
  151. // FIXME: We really need to improve the relocation validation. Basically, we
  152. // want to implement a separate computation which evaluates the relocation
  153. // entry as the linker would, and verifies that the resultant fixup value is
  154. // exactly what the encoder wanted. This will catch several classes of
  155. // problems:
  156. //
  157. // - Relocation entry bugs, the two algorithms are unlikely to have the same
  158. // exact bug.
  159. //
  160. // - Relaxation issues, where we forget to relax something.
  161. //
  162. // - Input errors, where something cannot be correctly encoded. 'as' allows
  163. // these through in many cases.
  164. void addRelocation(const MCSectionData *SD,
  165. object::macho::RelocationEntry &MRE) {
  166. Relocations[SD].push_back(MRE);
  167. }
  168. void RecordScatteredRelocation(const MCAssembler &Asm,
  169. const MCAsmLayout &Layout,
  170. const MCFragment *Fragment,
  171. const MCFixup &Fixup, MCValue Target,
  172. unsigned Log2Size,
  173. uint64_t &FixedValue);
  174. void RecordTLVPRelocation(const MCAssembler &Asm,
  175. const MCAsmLayout &Layout,
  176. const MCFragment *Fragment,
  177. const MCFixup &Fixup, MCValue Target,
  178. uint64_t &FixedValue);
  179. void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
  180. const MCFragment *Fragment, const MCFixup &Fixup,
  181. MCValue Target, uint64_t &FixedValue);
  182. void BindIndirectSymbols(MCAssembler &Asm);
  183. /// ComputeSymbolTable - Compute the symbol table data
  184. ///
  185. /// \param StringTable [out] - The string table data.
  186. void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
  187. std::vector<MachSymbolData> &LocalSymbolData,
  188. std::vector<MachSymbolData> &ExternalSymbolData,
  189. std::vector<MachSymbolData> &UndefinedSymbolData);
  190. void computeSectionAddresses(const MCAssembler &Asm,
  191. const MCAsmLayout &Layout);
  192. void markAbsoluteVariableSymbols(MCAssembler &Asm,
  193. const MCAsmLayout &Layout);
  194. void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout);
  195. virtual bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
  196. const MCSymbolData &DataA,
  197. const MCFragment &FB,
  198. bool InSet,
  199. bool IsPCRel) const;
  200. void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
  201. };
  202. /// \brief Construct a new Mach-O writer instance.
  203. ///
  204. /// This routine takes ownership of the target writer subclass.
  205. ///
  206. /// \param MOTW - The target specific Mach-O writer subclass.
  207. /// \param OS - The stream to write to.
  208. /// \returns The constructed object writer.
  209. MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
  210. raw_ostream &OS, bool IsLittleEndian);
  211. } // End llvm namespace
  212. #endif