LLVM API Documentation

MCMachObjectWriter.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCMachObjectWriter.h - Mach Object Writer -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_MC_MCMACHOBJECTWRITER_H
00011 #define LLVM_MC_MCMACHOBJECTWRITER_H
00012 
00013 #include "llvm/ADT/DenseMap.h"
00014 #include "llvm/ADT/SmallString.h"
00015 #include "llvm/MC/MCExpr.h"
00016 #include "llvm/MC/MCObjectWriter.h"
00017 #include "llvm/Support/DataTypes.h"
00018 #include "llvm/Support/MachO.h"
00019 #include <vector>
00020 
00021 namespace llvm {
00022 
00023 class MCSectionData;
00024 class MachObjectWriter;
00025 
00026 class MCMachObjectTargetWriter {
00027   const unsigned Is64Bit : 1;
00028   const uint32_t CPUType;
00029   const uint32_t CPUSubtype;
00030   // FIXME: Remove this, we should just always use it once we no longer care
00031   // about Darwin 'as' compatibility.
00032   const unsigned UseAggressiveSymbolFolding : 1;
00033   unsigned LocalDifference_RIT;
00034 
00035 protected:
00036   MCMachObjectTargetWriter(bool Is64Bit_, uint32_t CPUType_,
00037                            uint32_t CPUSubtype_,
00038                            bool UseAggressiveSymbolFolding_ = false);
00039 
00040   void setLocalDifferenceRelocationType(unsigned Type) {
00041     LocalDifference_RIT = Type;
00042   }
00043 
00044 public:
00045   virtual ~MCMachObjectTargetWriter();
00046 
00047   /// @name Lifetime Management
00048   /// @{
00049 
00050   virtual void reset() {};
00051 
00052   /// @}
00053 
00054   /// @name Accessors
00055   /// @{
00056 
00057   bool is64Bit() const { return Is64Bit; }
00058   bool useAggressiveSymbolFolding() const { return UseAggressiveSymbolFolding; }
00059   uint32_t getCPUType() const { return CPUType; }
00060   uint32_t getCPUSubtype() const { return CPUSubtype; }
00061   unsigned getLocalDifferenceRelocationType() const {
00062     return LocalDifference_RIT;
00063   }
00064 
00065   /// @}
00066 
00067   /// @name API
00068   /// @{
00069 
00070   virtual void RecordRelocation(MachObjectWriter *Writer,
00071                                 const MCAssembler &Asm,
00072                                 const MCAsmLayout &Layout,
00073                                 const MCFragment *Fragment,
00074                                 const MCFixup &Fixup,
00075                                 MCValue Target,
00076                                 uint64_t &FixedValue) = 0;
00077 
00078   /// @}
00079 };
00080 
00081 class MachObjectWriter : public MCObjectWriter {
00082   /// MachSymbolData - Helper struct for containing some precomputed information
00083   /// on symbols.
00084   struct MachSymbolData {
00085     MCSymbolData *SymbolData;
00086     uint64_t StringIndex;
00087     uint8_t SectionIndex;
00088 
00089     // Support lexicographic sorting.
00090     bool operator<(const MachSymbolData &RHS) const;
00091   };
00092 
00093   /// The target specific Mach-O writer instance.
00094   std::unique_ptr<MCMachObjectTargetWriter> TargetObjectWriter;
00095 
00096   /// @name Relocation Data
00097   /// @{
00098 
00099   llvm::DenseMap<const MCSectionData*,
00100                  std::vector<MachO::any_relocation_info> > Relocations;
00101   llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
00102 
00103   /// @}
00104   /// @name Symbol Table Data
00105   /// @{
00106 
00107   SmallString<256> StringTable;
00108   std::vector<MachSymbolData> LocalSymbolData;
00109   std::vector<MachSymbolData> ExternalSymbolData;
00110   std::vector<MachSymbolData> UndefinedSymbolData;
00111 
00112   /// @}
00113 
00114   MachSymbolData *findSymbolData(const MCSymbol &Sym);
00115 
00116 public:
00117   MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
00118                    bool _IsLittleEndian)
00119     : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
00120   }
00121 
00122   /// @name Lifetime management Methods
00123   /// @{
00124 
00125   void reset() override;
00126 
00127   /// @}
00128 
00129   /// @name Utility Methods
00130   /// @{
00131 
00132   bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
00133 
00134   SectionAddrMap SectionAddress;
00135 
00136   SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
00137 
00138   uint64_t getSectionAddress(const MCSectionData* SD) const {
00139     return SectionAddress.lookup(SD);
00140   }
00141   uint64_t getSymbolAddress(const MCSymbolData* SD,
00142                             const MCAsmLayout &Layout) const;
00143 
00144   uint64_t getFragmentAddress(const MCFragment *Fragment,
00145                               const MCAsmLayout &Layout) const;
00146 
00147   uint64_t getPaddingSize(const MCSectionData *SD,
00148                           const MCAsmLayout &Layout) const;
00149 
00150   bool doesSymbolRequireExternRelocation(const MCSymbolData *SD);
00151 
00152   /// @}
00153 
00154   /// @name Target Writer Proxy Accessors
00155   /// @{
00156 
00157   bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
00158   bool isX86_64() const {
00159     uint32_t CPUType = TargetObjectWriter->getCPUType();
00160     return CPUType == MachO::CPU_TYPE_X86_64;
00161   }
00162 
00163   /// @}
00164 
00165   void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
00166                    bool SubsectionsViaSymbols);
00167 
00168   /// WriteSegmentLoadCommand - Write a segment load command.
00169   ///
00170   /// \param NumSections The number of sections in this segment.
00171   /// \param SectionDataSize The total size of the sections.
00172   void WriteSegmentLoadCommand(unsigned NumSections,
00173                                uint64_t VMSize,
00174                                uint64_t SectionDataStartOffset,
00175                                uint64_t SectionDataSize);
00176 
00177   void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
00178                     const MCSectionData &SD, uint64_t FileOffset,
00179                     uint64_t RelocationsStart, unsigned NumRelocations);
00180 
00181   void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
00182                               uint32_t StringTableOffset,
00183                               uint32_t StringTableSize);
00184 
00185   void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
00186                                 uint32_t NumLocalSymbols,
00187                                 uint32_t FirstExternalSymbol,
00188                                 uint32_t NumExternalSymbols,
00189                                 uint32_t FirstUndefinedSymbol,
00190                                 uint32_t NumUndefinedSymbols,
00191                                 uint32_t IndirectSymbolOffset,
00192                                 uint32_t NumIndirectSymbols);
00193 
00194   void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
00195 
00196   void WriteLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
00197                                 uint32_t DataSize);
00198 
00199   void WriteLinkerOptionsLoadCommand(const std::vector<std::string> &Options);
00200 
00201   // FIXME: We really need to improve the relocation validation. Basically, we
00202   // want to implement a separate computation which evaluates the relocation
00203   // entry as the linker would, and verifies that the resultant fixup value is
00204   // exactly what the encoder wanted. This will catch several classes of
00205   // problems:
00206   //
00207   //  - Relocation entry bugs, the two algorithms are unlikely to have the same
00208   //    exact bug.
00209   //
00210   //  - Relaxation issues, where we forget to relax something.
00211   //
00212   //  - Input errors, where something cannot be correctly encoded. 'as' allows
00213   //    these through in many cases.
00214 
00215   void addRelocation(const MCSectionData *SD,
00216                      MachO::any_relocation_info &MRE) {
00217     Relocations[SD].push_back(MRE);
00218   }
00219 
00220   void RecordScatteredRelocation(const MCAssembler &Asm,
00221                                  const MCAsmLayout &Layout,
00222                                  const MCFragment *Fragment,
00223                                  const MCFixup &Fixup, MCValue Target,
00224                                  unsigned Log2Size,
00225                                  uint64_t &FixedValue);
00226 
00227   void RecordTLVPRelocation(const MCAssembler &Asm,
00228                             const MCAsmLayout &Layout,
00229                             const MCFragment *Fragment,
00230                             const MCFixup &Fixup, MCValue Target,
00231                             uint64_t &FixedValue);
00232 
00233   void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
00234                         const MCFragment *Fragment, const MCFixup &Fixup,
00235                         MCValue Target, bool &IsPCRel,
00236                         uint64_t &FixedValue) override;
00237 
00238   void BindIndirectSymbols(MCAssembler &Asm);
00239 
00240   /// ComputeSymbolTable - Compute the symbol table data
00241   ///
00242   /// \param StringTable [out] - The string table data.
00243   void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
00244                           std::vector<MachSymbolData> &LocalSymbolData,
00245                           std::vector<MachSymbolData> &ExternalSymbolData,
00246                           std::vector<MachSymbolData> &UndefinedSymbolData);
00247 
00248   void computeSectionAddresses(const MCAssembler &Asm,
00249                                const MCAsmLayout &Layout);
00250 
00251   void markAbsoluteVariableSymbols(MCAssembler &Asm,
00252                                    const MCAsmLayout &Layout);
00253   void ExecutePostLayoutBinding(MCAssembler &Asm,
00254                                 const MCAsmLayout &Layout) override;
00255 
00256   bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
00257                                               const MCSymbolData &DataA,
00258                                               const MCFragment &FB,
00259                                               bool InSet,
00260                                               bool IsPCRel) const override;
00261 
00262   void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
00263 };
00264 
00265 
00266 /// \brief Construct a new Mach-O writer instance.
00267 ///
00268 /// This routine takes ownership of the target writer subclass.
00269 ///
00270 /// \param MOTW - The target specific Mach-O writer subclass.
00271 /// \param OS - The stream to write to.
00272 /// \returns The constructed object writer.
00273 MCObjectWriter *createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
00274                                        raw_ostream &OS, bool IsLittleEndian);
00275 
00276 } // End llvm namespace
00277 
00278 #endif