1 //===- SectLinker.h -------------------------------------------------------===// 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 // 10 //SectLinker is a base class inherited by target specific linker. 11 //This class primarily handles common functionality used by all linkers. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef SECTION_LINKER_H 16 #define SECTION_LINKER_H 17 #ifdef ENABLE_UNITTEST 18 #include <gtest.h> 19 #endif 20 #include <llvm/CodeGen/MachineFunctionPass.h> 21 #include <mcld/Support/PositionDependentOption.h> 22 #include <vector> 23 24 namespace llvm 25 { 26 class Module; 27 class MachineFunction; 28 } // namespace of llvm 29 30 namespace mcld 31 { 32 class MCLDFile; 33 class MCLDDriver; 34 class TargetLDBackend; 35 class AttributeFactory; 36 class SectLinkerOption; 37 38 /** \class SectLinker 39 * \brief SectLinker provides a linking pass for standard compilation flow 40 * 41 * SectLinker is responded for 42 * - provide an interface for target-specific SectLinekr 43 * - set up environment for MCLDDriver 44 * - control AsmPrinter, make sure AsmPrinter has already prepared 45 * all MCSectionDatas for linking 46 * 47 * SectLinker resolves the absolue paths of input arguments. 48 * 49 * @see MachineFunctionPass MCLDDriver 50 */ 51 class SectLinker : public llvm::MachineFunctionPass 52 { 53 protected: 54 // Constructor. Although SectLinker has only two arguments, 55 // TargetSectLinker should handle 56 // - enabled attributes 57 // - the default attribute 58 // - the default link script 59 // - the standard symbols 60 SectLinker(SectLinkerOption &pOption, 61 TargetLDBackend &pLDBackend); 62 63 public: 64 virtual ~SectLinker(); 65 66 /// addTargetOptions - target SectLinker can hook this function to add 67 /// target-specific inputs addTargetOptions(llvm::Module & pM,SectLinkerOption & pOption)68 virtual void addTargetOptions(llvm::Module &pM, 69 SectLinkerOption &pOption) 70 { } 71 72 /// doInitialization - Read all parameters and set up the AsmPrinter. 73 /// If your pass overrides this, it must make sure to explicitly call 74 /// this implementation. 75 virtual bool doInitialization(llvm::Module &pM); 76 77 /// doFinalization - Shut down the AsmPrinter, and do really linking. 78 /// If you override this in your pass, you must make sure to call it 79 /// explicitly. 80 virtual bool doFinalization(llvm::Module &pM); 81 82 /// runOnMachineFunction 83 /// redirect to AsmPrinter 84 virtual bool runOnMachineFunction(llvm::MachineFunction& pMFn); 85 86 protected: 87 void initializeInputTree(const PositionDependentOptions &pOptions) const; 88 attrFactory()89 AttributeFactory* attrFactory() 90 { return m_pAttrFactory; } 91 92 private: 93 SectLinkerOption *m_pOption; 94 95 protected: 96 TargetLDBackend *m_pLDBackend; 97 MCLDDriver *m_pLDDriver; 98 AttributeFactory *m_pAttrFactory; 99 100 private: 101 static char m_ID; 102 }; 103 104 } // namespace of MC Linker 105 106 #endif 107