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   /// scanRelocation - When read in relocations, backend can do any modification
     69   /// to relocation and generate empty entries, such as GOT, dynamic relocation
     70   /// entries and other target dependent entries. These entries are generated
     71   /// for layout to adjust the ouput offset.
     72   /// @param pReloc - a read in relocation entry
     73   /// @param pInputSym - the input LDSymbol of relocation target symbol
     74   /// @param pSection - the section of relocation applying target
     75   virtual void scanRelocation(Relocation& pReloc,
     76                               IRBuilder& pBuilder,
     77                               Module& pModule,
     78                               LDSection& pSection) = 0;
     79 
     80   /// partialScanRelocation - When doing partial linking, backend can do any
     81   /// modification to relocation to fix the relocation offset after section
     82   /// merge
     83   /// @param pReloc - a read in relocation entry
     84   /// @param pInputSym - the input LDSymbol of relocation target symbol
     85   /// @param pSection - the section of relocation applying target
     86   virtual void partialScanRelocation(Relocation& pReloc,
     87                                      Module& pModule,
     88                                      const LDSection& pSection) = 0;
     89 
     90   // -----  format dependent  ----- //
     91   virtual ArchiveReader* createArchiveReader(Module&) = 0;
     92   virtual ObjectReader*  createObjectReader(IRBuilder&) = 0;
     93   virtual DynObjReader*  createDynObjReader(IRBuilder&) = 0;
     94   virtual BinaryReader*  createBinaryReader(IRBuilder&) = 0;
     95   virtual ObjectWriter*  createWriter() = 0;
     96 
     97   virtual bool initStdSections(ObjectBuilder& pBuilder) = 0;
     98 
     99   /// layout - layout method
    100   virtual void layout(Module& pModule) = 0;
    101 
    102   /// preLayout - Backend can do any needed modification before layout
    103   virtual void preLayout(Module& pModule, IRBuilder& pBuilder) = 0;
    104 
    105   /// postLayout - Backend can do any needed modification after layout
    106   virtual void postLayout(Module& pModule, IRBuilder& pBuilder) = 0;
    107 
    108   /// postProcessing - Backend can do any needed modification in the final stage
    109   virtual void postProcessing(MemoryArea& pOutput) = 0;
    110 
    111   /// section start offset in the output file
    112   virtual size_t sectionStartOffset() const = 0;
    113 
    114   /// computeSectionOrder - compute the layout order of the given section
    115   virtual unsigned int getSectionOrder(const LDSection& pSectHdr) const = 0;
    116 
    117   /// sizeNamePools - compute the size of regular name pools
    118   /// In ELF executable files, regular name pools are .symtab, .strtab.,
    119   /// .dynsym, .dynstr, and .hash
    120   virtual void sizeNamePools(Module& pModule, bool pIsStaticLink) = 0;
    121 
    122   /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
    123   /// then it will ask backend to finalize the symbol value.
    124   /// @return ture - if backend set the symbol value sucessfully
    125   /// @return false - if backend do not recognize the symbol
    126   virtual bool finalizeSymbols() = 0;
    127 
    128   /// finalizeTLSSymbol - Linker asks backend to set the symbol value when it
    129   /// meets a TLS symbol
    130   virtual bool finalizeTLSSymbol(LDSymbol& pSymbol) = 0;
    131 
    132   /// allocateCommonSymbols - allocate common symbols in the corresponding
    133   /// sections.
    134   virtual bool allocateCommonSymbols(Module& pModule) = 0;
    135 
    136   /// mergeSection - merge target dependent sections.
    137   virtual bool mergeSection(Module& pModule, LDSection& pInputSection)
    138   { return true; }
    139 
    140   /// updateSectionFlags - update pTo's flags when merging pFrom
    141   /// update the output section flags based on input section flags.
    142   /// FIXME: (Luba) I know ELF need to merge flags, but I'm not sure if
    143   /// MachO and COFF also need this.
    144   virtual bool updateSectionFlags(LDSection& pTo, const LDSection& pFrom)
    145   { return true; }
    146 
    147   /// readSection - read a target dependent section
    148   virtual bool readSection(Input& pInput, SectionData& pSD)
    149   { return true; }
    150 
    151   /// sizeInterp - compute the size of program interpreter's name
    152   /// In ELF executables, this is the length of dynamic linker's path name
    153   virtual void sizeInterp() = 0;
    154 
    155   // -----  relaxation  ----- //
    156   virtual bool initBRIslandFactory() = 0;
    157   virtual bool initStubFactory() = 0;
    158   virtual bool initTargetStubs() { return true; }
    159 
    160   virtual BranchIslandFactory* getBRIslandFactory() = 0;
    161   virtual StubFactory*         getStubFactory() = 0;
    162 
    163   /// relax - the relaxation pass
    164   virtual bool relax(Module& pModule, IRBuilder& pBuilder) = 0;
    165 
    166   /// mayRelax - return true if the backend needs to do relaxation
    167   virtual bool mayRelax() = 0;
    168 
    169 protected:
    170   const LinkerConfig& config() const { return m_Config; }
    171 
    172 private:
    173   const LinkerConfig& m_Config;
    174 };
    175 
    176 } // End mcld namespace
    177 
    178 #endif
    179