Home | History | Annotate | Download | only in Target
      1 //===-- 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/ADT/StringRef.h>
     13 #include <llvm/Support/DataTypes.h>
     14 #include <mcld/LD/GarbageCollection.h>
     15 
     16 namespace mcld {
     17 
     18 class ArchiveReader;
     19 class BinaryReader;
     20 class BinaryWriter;
     21 class BranchIslandFactory;
     22 class DynObjReader;
     23 class DynObjWriter;
     24 class ExecWriter;
     25 class FileOutputBuffer;
     26 class SectionReachedListMap;
     27 class IRBuilder;
     28 class Input;
     29 class LDSection;
     30 class LDSymbol;
     31 class Layout;
     32 class LinkerConfig;
     33 class Module;
     34 class ObjectBuilder;
     35 class ObjectReader;
     36 class ObjectWriter;
     37 class Relocator;
     38 class ResolveInfo;
     39 class SectionData;
     40 class StubFactory;
     41 
     42 //===----------------------------------------------------------------------===//
     43 /// TargetLDBackend - Generic interface to target specific assembler backends.
     44 //===----------------------------------------------------------------------===//
     45 class TargetLDBackend
     46 {
     47   TargetLDBackend(const TargetLDBackend &);   // DO NOT IMPLEMENT
     48   void operator=(const TargetLDBackend &);  // DO NOT IMPLEMENT
     49 
     50 protected:
     51   TargetLDBackend(const LinkerConfig& pConfig);
     52 
     53 public:
     54   virtual ~TargetLDBackend();
     55 
     56   // -----  target dependent  ----- //
     57   virtual void initTargetSegments(IRBuilder& pBuilder) { }
     58   virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) { }
     59   virtual void initTargetSymbols(IRBuilder& pBuilder, Module& pModule) { }
     60   virtual void initTargetRelocation(IRBuilder& pBuilder) { }
     61   virtual bool initStandardSymbols(IRBuilder& pBuilder, Module& pModule) = 0;
     62 
     63   virtual bool initRelocator() = 0;
     64 
     65   virtual Relocator* getRelocator() = 0;
     66   virtual const Relocator* getRelocator() const = 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(FileOutputBuffer& 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,
    116                             const Input& pInputFile,
    117                             LDSection& pInputSection)
    118   { return true; }
    119 
    120   /// setUpReachedSectionsForGC - set the reference between two sections for
    121   /// some special target sections. GC will set up the reference for the Regular
    122   /// and BSS sections. Backends can also set up the reference if need.
    123   virtual void setUpReachedSectionsForGC(const Module& pModule,
    124         GarbageCollection::SectionReachedListMap& pSectReachedListMap) const { }
    125 
    126   /// updateSectionFlags - update pTo's flags when merging pFrom
    127   /// update the output section flags based on input section flags.
    128   /// FIXME: (Luba) I know ELF need to merge flags, but I'm not sure if
    129   /// MachO and COFF also need this.
    130   virtual bool updateSectionFlags(LDSection& pTo, const LDSection& pFrom)
    131   { return true; }
    132 
    133   /// readSection - read a target dependent section
    134   virtual bool readSection(Input& pInput, SectionData& pSD)
    135   { return true; }
    136 
    137   /// sizeInterp - compute the size of program interpreter's name
    138   /// In ELF executables, this is the length of dynamic linker's path name
    139   virtual void sizeInterp() = 0;
    140 
    141   /// getEntry - get the entry point name
    142   virtual llvm::StringRef getEntry(const Module& pModule) const = 0;
    143 
    144   // -----  relaxation  ----- //
    145   virtual bool initBRIslandFactory() = 0;
    146   virtual bool initStubFactory() = 0;
    147   virtual bool initTargetStubs() { return true; }
    148 
    149   virtual BranchIslandFactory* getBRIslandFactory() = 0;
    150   virtual StubFactory*         getStubFactory() = 0;
    151 
    152   /// relax - the relaxation pass
    153   virtual bool relax(Module& pModule, IRBuilder& pBuilder) = 0;
    154 
    155   /// mayRelax - return true if the backend needs to do relaxation
    156   virtual bool mayRelax() = 0;
    157 
    158   /// commonPageSize - the common page size of the target machine
    159   virtual uint64_t commonPageSize() const = 0;
    160 
    161   /// abiPageSize - the abi page size of the target machine
    162   virtual uint64_t abiPageSize() const = 0;
    163 
    164   /// sortRelocation - sort the dynamic relocations to let dynamic linker
    165   /// process relocations more efficiently
    166   virtual void sortRelocation(LDSection& pSection) = 0;
    167 
    168   /// createAndSizeEhFrameHdr - This is seperated since we may add eh_frame
    169   /// entry in the middle
    170   virtual void createAndSizeEhFrameHdr(Module& pModule) = 0;
    171 
    172   /// isSymbolPreemptible - whether the symbol can be preemted by other link
    173   /// units
    174   virtual bool isSymbolPreemptible(const ResolveInfo& pSym) const = 0;
    175 
    176   /// mayHaveUnsafeFunctionPointerAccess - check if the section may have unsafe
    177   /// function pointer access
    178   virtual bool mayHaveUnsafeFunctionPointerAccess(const LDSection& pSection)
    179       const = 0;
    180 
    181 protected:
    182   const LinkerConfig& config() const { return m_Config; }
    183 
    184 private:
    185   const LinkerConfig& m_Config;
    186 };
    187 
    188 } // End mcld namespace
    189 
    190 #endif
    191