Home | History | Annotate | Download | only in Target
      1 //===-- llvm/Target/TargetLDBackend.h - Target LD Backend -----*- C++ -*-===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 #ifndef MCLD_TARGET_TARGETLDBACKEND_H
     10 #define MCLD_TARGET_TARGETLDBACKEND_H
     11 
     12 #include <llvm/Support/DataTypes.h>
     13 
     14 namespace mcld {
     15 
     16 class Module;
     17 class LinkerConfig;
     18 class IRBuilder;
     19 class Relocation;
     20 class RelocationFactory;
     21 class Relocator;
     22 class Layout;
     23 class ArchiveReader;
     24 class ObjectReader;
     25 class DynObjReader;
     26 class BinaryReader;
     27 class ObjectWriter;
     28 class DynObjWriter;
     29 class ExecWriter;
     30 class BinaryWriter;
     31 class LDFileFormat;
     32 class LDSymbol;
     33 class LDSection;
     34 class SectionData;
     35 class Input;
     36 class GOT;
     37 class MemoryArea;
     38 class MemoryAreaFactory;
     39 class BranchIslandFactory;
     40 class StubFactory;
     41 class ObjectBuilder;
     42 
     43 //===----------------------------------------------------------------------===//
     44 /// TargetLDBackend - Generic interface to target specific assembler backends.
     45 //===----------------------------------------------------------------------===//
     46 class TargetLDBackend
     47 {
     48   TargetLDBackend(const TargetLDBackend &);   // DO NOT IMPLEMENT
     49   void operator=(const TargetLDBackend &);  // DO NOT IMPLEMENT
     50 
     51 protected:
     52   TargetLDBackend(const LinkerConfig& pConfig);
     53 
     54 public:
     55   virtual ~TargetLDBackend();
     56 
     57   // -----  target dependent  ----- //
     58   virtual void initTargetSegments(IRBuilder& pBuilder) { }
     59   virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) { }
     60   virtual void initTargetSymbols(IRBuilder& pBuilder, Module& pModule) { }
     61   virtual void initTargetRelocation(IRBuilder& pBuilder) { }
     62   virtual bool initStandardSymbols(IRBuilder& pBuilder, Module& pModule) = 0;
     63 
     64   virtual bool initRelocator() = 0;
     65 
     66   virtual Relocator* getRelocator() = 0;
     67 
     68   // -----  format dependent  ----- //
     69   virtual ArchiveReader* createArchiveReader(Module&) = 0;
     70   virtual ObjectReader*  createObjectReader(IRBuilder&) = 0;
     71   virtual DynObjReader*  createDynObjReader(IRBuilder&) = 0;
     72   virtual BinaryReader*  createBinaryReader(IRBuilder&) = 0;
     73   virtual ObjectWriter*  createWriter() = 0;
     74 
     75   virtual bool initStdSections(ObjectBuilder& pBuilder) = 0;
     76 
     77   /// layout - layout method
     78   virtual void layout(Module& pModule) = 0;
     79 
     80   /// preLayout - Backend can do any needed modification before layout
     81   virtual void preLayout(Module& pModule, IRBuilder& pBuilder) = 0;
     82 
     83   /// postLayout - Backend can do any needed modification after layout
     84   virtual void postLayout(Module& pModule, IRBuilder& pBuilder) = 0;
     85 
     86   /// postProcessing - Backend can do any needed modification in the final stage
     87   virtual void postProcessing(MemoryArea& pOutput) = 0;
     88 
     89   /// section start offset in the output file
     90   virtual size_t sectionStartOffset() const = 0;
     91 
     92   /// computeSectionOrder - compute the layout order of the given section
     93   virtual unsigned int getSectionOrder(const LDSection& pSectHdr) const = 0;
     94 
     95   /// sizeNamePools - compute the size of regular name pools
     96   /// In ELF executable files, regular name pools are .symtab, .strtab.,
     97   /// .dynsym, .dynstr, and .hash
     98   virtual void sizeNamePools(Module& pModule) = 0;
     99 
    100   /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
    101   /// then it will ask backend to finalize the symbol value.
    102   /// @return ture - if backend set the symbol value sucessfully
    103   /// @return false - if backend do not recognize the symbol
    104   virtual bool finalizeSymbols() = 0;
    105 
    106   /// finalizeTLSSymbol - Linker asks backend to set the symbol value when it
    107   /// meets a TLS symbol
    108   virtual bool finalizeTLSSymbol(LDSymbol& pSymbol) = 0;
    109 
    110   /// allocateCommonSymbols - allocate common symbols in the corresponding
    111   /// sections.
    112   virtual bool allocateCommonSymbols(Module& pModule) = 0;
    113 
    114   /// mergeSection - merge target dependent sections.
    115   virtual bool mergeSection(Module& pModule, LDSection& pInputSection)
    116   { return true; }
    117 
    118   /// updateSectionFlags - update pTo's flags when merging pFrom
    119   /// update the output section flags based on input section flags.
    120   /// FIXME: (Luba) I know ELF need to merge flags, but I'm not sure if
    121   /// MachO and COFF also need this.
    122   virtual bool updateSectionFlags(LDSection& pTo, const LDSection& pFrom)
    123   { return true; }
    124 
    125   /// readSection - read a target dependent section
    126   virtual bool readSection(Input& pInput, SectionData& pSD)
    127   { return true; }
    128 
    129   /// sizeInterp - compute the size of program interpreter's name
    130   /// In ELF executables, this is the length of dynamic linker's path name
    131   virtual void sizeInterp() = 0;
    132 
    133   // -----  relaxation  ----- //
    134   virtual bool initBRIslandFactory() = 0;
    135   virtual bool initStubFactory() = 0;
    136   virtual bool initTargetStubs() { return true; }
    137 
    138   virtual BranchIslandFactory* getBRIslandFactory() = 0;
    139   virtual StubFactory*         getStubFactory() = 0;
    140 
    141   /// relax - the relaxation pass
    142   virtual bool relax(Module& pModule, IRBuilder& pBuilder) = 0;
    143 
    144   /// mayRelax - return true if the backend needs to do relaxation
    145   virtual bool mayRelax() = 0;
    146 
    147 protected:
    148   const LinkerConfig& config() const { return m_Config; }
    149 
    150 private:
    151   const LinkerConfig& m_Config;
    152 };
    153 
    154 } // End mcld namespace
    155 
    156 #endif
    157