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/SmallString.h" 15 #include "llvm/MC/MCExpr.h" 16 #include "llvm/MC/MCObjectWriter.h" 17 #include "llvm/MC/StringTableBuilder.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 27 class MCMachObjectTargetWriter { 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 setLocalDifferenceRelocationType(unsigned Type)41 void setLocalDifferenceRelocationType(unsigned Type) { 42 LocalDifference_RIT = Type; 43 } 44 45 public: 46 virtual ~MCMachObjectTargetWriter(); 47 48 /// @name Lifetime Management 49 /// @{ 50 reset()51 virtual void reset() {}; 52 53 /// @} 54 55 /// @name Accessors 56 /// @{ 57 is64Bit()58 bool is64Bit() const { return Is64Bit; } useAggressiveSymbolFolding()59 bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; } getCPUType()60 uint32_t getCPUType() const { return CPUType; } getCPUSubtype()61 uint32_t getCPUSubtype() const { return CPUSubtype; } getLocalDifferenceRelocationType()62 unsigned getLocalDifferenceRelocationType() const { 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, 76 MCValue Target, 77 uint64_t &FixedValue) = 0; 78 79 /// @} 80 }; 81 82 class MachObjectWriter : public MCObjectWriter { 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. 95 std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter; 96 97 /// @name Relocation Data 98 /// @{ 99 100 llvm::DenseMap<const MCSectionData*, 101 std::vector<MachO::any_relocation_info> > Relocations; 102 llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase; 103 104 /// @} 105 /// @name Symbol Table Data 106 /// @{ 107 108 StringTableBuilder StringTable; 109 std::vector<MachSymbolData> LocalSymbolData; 110 std::vector<MachSymbolData> ExternalSymbolData; 111 std::vector<MachSymbolData> UndefinedSymbolData; 112 113 /// @} 114 115 MachSymbolData *findSymbolData(const MCSymbol &Sym); 116 117 public: MachObjectWriter(MCMachObjectTargetWriter * MOTW,raw_ostream & _OS,bool _IsLittleEndian)118 MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS, 119 bool _IsLittleEndian) 120 : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) { 121 } 122 123 /// @name Lifetime management Methods 124 /// @{ 125 126 void reset() override; 127 128 /// @} 129 130 /// @name Utility Methods 131 /// @{ 132 133 bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind); 134 135 SectionAddrMap SectionAddress; 136 getSectionAddressMap()137 SectionAddrMap &getSectionAddressMap() { return SectionAddress; } 138 getSectionAddress(const MCSectionData * SD)139 uint64_t getSectionAddress(const MCSectionData* SD) const { 140 return SectionAddress.lookup(SD); 141 } 142 uint64_t getSymbolAddress(const MCSymbolData* SD, 143 const MCAsmLayout &Layout) const; 144 145 uint64_t getFragmentAddress(const MCFragment *Fragment, 146 const MCAsmLayout &Layout) const; 147 148 uint64_t getPaddingSize(const MCSectionData *SD, 149 const MCAsmLayout &Layout) const; 150 151 bool doesSymbolRequireExternRelocation(const MCSymbolData *SD); 152 153 /// @} 154 155 /// @name Target Writer Proxy Accessors 156 /// @{ 157 is64Bit()158 bool is64Bit() const { return TargetObjectWriter->is64Bit(); } isX86_64()159 bool isX86_64() const { 160 uint32_t CPUType = TargetObjectWriter->getCPUType(); 161 return CPUType == MachO::CPU_TYPE_X86_64; 162 } 163 164 /// @} 165 166 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize, 167 bool SubsectionsViaSymbols); 168 169 /// WriteSegmentLoadCommand - Write a segment load command. 170 /// 171 /// \param NumSections The number of sections in this segment. 172 /// \param SectionDataSize The total size of the sections. 173 void WriteSegmentLoadCommand(unsigned NumSections, 174 uint64_t VMSize, 175 uint64_t SectionDataStartOffset, 176 uint64_t SectionDataSize); 177 178 void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout, 179 const MCSectionData &SD, uint64_t FileOffset, 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 186 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol, 187 uint32_t NumLocalSymbols, 188 uint32_t FirstExternalSymbol, 189 uint32_t NumExternalSymbols, 190 uint32_t FirstUndefinedSymbol, 191 uint32_t NumUndefinedSymbols, 192 uint32_t IndirectSymbolOffset, 193 uint32_t NumIndirectSymbols); 194 195 void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout); 196 197 void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset, 198 uint32_t DataSize); 199 200 void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options); 201 202 // FIXME: We really need to improve the relocation validation. Basically, we 203 // want to implement a separate computation which evaluates the relocation 204 // entry as the linker would, and verifies that the resultant fixup value is 205 // exactly what the encoder wanted. This will catch several classes of 206 // problems: 207 // 208 // - Relocation entry bugs, the two algorithms are unlikely to have the same 209 // exact bug. 210 // 211 // - Relaxation issues, where we forget to relax something. 212 // 213 // - Input errors, where something cannot be correctly encoded. 'as' allows 214 // these through in many cases. 215 addRelocation(const MCSectionData * SD,MachO::any_relocation_info & MRE)216 void addRelocation(const MCSectionData *SD, 217 MachO::any_relocation_info &MRE) { 218 Relocations[SD].push_back(MRE); 219 } 220 221 void RecordScatteredRelocation(const MCAssembler &Asm, 222 const MCAsmLayout &Layout, 223 const MCFragment *Fragment, 224 const MCFixup &Fixup, MCValue Target, 225 unsigned Log2Size, 226 uint64_t &FixedValue); 227 228 void RecordTLVPRelocation(const MCAssembler &Asm, 229 const MCAsmLayout &Layout, 230 const MCFragment *Fragment, 231 const MCFixup &Fixup, MCValue Target, 232 uint64_t &FixedValue); 233 234 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, 235 const MCFragment *Fragment, const MCFixup &Fixup, 236 MCValue Target, bool &IsPCRel, 237 uint64_t &FixedValue) override; 238 239 void BindIndirectSymbols(MCAssembler &Asm); 240 241 /// ComputeSymbolTable - Compute the symbol table data 242 /// 243 void ComputeSymbolTable(MCAssembler &Asm, 244 std::vector<MachSymbolData> &LocalSymbolData, 245 std::vector<MachSymbolData> &ExternalSymbolData, 246 std::vector<MachSymbolData> &UndefinedSymbolData); 247 248 void computeSectionAddresses(const MCAssembler &Asm, 249 const MCAsmLayout &Layout); 250 251 void markAbsoluteVariableSymbols(MCAssembler &Asm, 252 const MCAsmLayout &Layout); 253 void ExecutePostLayoutBinding(MCAssembler &Asm, 254 const MCAsmLayout &Layout) override; 255 256 bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, 257 const MCSymbolData &DataA, 258 const MCFragment &FB, 259 bool InSet, 260 bool IsPCRel) const override; 261 262 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; 263 }; 264 265 266 /// \brief Construct a new Mach-O writer instance. 267 /// 268 /// This routine takes ownership of the target writer subclass. 269 /// 270 /// \param MOTW - The target specific Mach-O writer subclass. 271 /// \param OS - The stream to write to. 272 /// \returns The constructed object writer. 273 MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW, 274 raw_ostream &OS, bool IsLittleEndian); 275 276 } // End llvm namespace 277 278 #endif 279