1 //===- StaticResolver.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 #ifndef MCLD_STATIC_SYMBOL_RESOLVER_H 10 #define MCLD_STATIC_SYMBOL_RESOLVER_H 11 #ifdef ENABLE_UNITTEST 12 #include <gtest.h> 13 #endif 14 #include <string> 15 #include <mcld/LD/Resolver.h> 16 #include <mcld/LD/ResolveInfo.h> 17 18 namespace mcld 19 { 20 21 class StrSymPool; 22 23 /** \class StaticResolver 24 */ 25 class StaticResolver : public Resolver 26 { 27 public: 28 /** \enum LinkAction 29 * LinkAction follows BFD:linker.c (binary file descriptor). 30 * List all actions to take in the state table 31 */ 32 enum LinkAction 33 { 34 FAIL, /* abort. */ 35 NOACT, /* no action. */ 36 UND, /* override by symbol undefined symbol. */ 37 WEAK, /* override by symbol weak undefined. */ 38 DEF, /* override by symbol defined. */ 39 DEFW, /* override by symbol weak defined. */ 40 DEFD, /* override by symbol dynamic defined. */ 41 DEFWD, /* override by symbol dynamic weak defined. */ 42 MDEFD, /* mark symbol dynamic defined. */ 43 MDEFWD, /* mark symbol dynamic weak defined. */ 44 DUND, /* override dynamic defined symbol by undefined one. */ 45 DUNDW, /* oevrride dynamic defined symbol by weak undefined one. */ 46 COM, /* override by symbol common. */ 47 CREF, /* Possibly warn about common reference to defined symbol. */ 48 CDEF, /* redefine existing common symbol. */ 49 BIG, /* override by symbol common using largest size. */ 50 MBIG, /* mark common symbol by larger size. */ 51 IND, /* override by indirect symbol. */ 52 CIND, /* mark indirect symbol from existing common symbol. */ 53 MDEF, /* multiple definition error. */ 54 MIND, /* multiple indirect symbols. */ 55 REFC /* Mark indirect symbol referenced and then CYCLE. */ 56 }; 57 58 private: 59 // These are the values generated by the bit codes. 60 /** Encoding: 61 * D -> define 62 * U -> undefine 63 * d -> dynamic 64 * w -> weak 65 * C -> common 66 * I -> indirect 67 */ 68 enum 69 { 70 U = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::undefine_flag, 71 w_U = ResolveInfo::weak_flag | ResolveInfo::regular_flag | ResolveInfo::undefine_flag, 72 d_U = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::undefine_flag, 73 wd_U = ResolveInfo::weak_flag | ResolveInfo::dynamic_flag | ResolveInfo::undefine_flag, 74 D = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::define_flag, 75 w_D = ResolveInfo::weak_flag | ResolveInfo::regular_flag | ResolveInfo::define_flag, 76 d_D = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::define_flag, 77 wd_D = ResolveInfo::weak_flag | ResolveInfo::dynamic_flag | ResolveInfo::define_flag, 78 C = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::common_flag, 79 w_C = ResolveInfo::weak_flag | ResolveInfo::regular_flag | ResolveInfo::common_flag, 80 d_C = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::common_flag, 81 wd_C = ResolveInfo::weak_flag | ResolveInfo::dynamic_flag | ResolveInfo::common_flag, 82 I = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::indirect_flag, 83 w_I = ResolveInfo::weak_flag | ResolveInfo::regular_flag | ResolveInfo::indirect_flag, 84 d_I = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::indirect_flag, 85 wd_I = ResolveInfo::weak_flag | ResolveInfo::dynamic_flag | ResolveInfo::indirect_flag 86 }; 87 88 enum ORDINATE 89 { 90 U_ORD, 91 w_U_ORD, 92 d_U_ORD, 93 wd_U_ORD, 94 D_ORD, 95 w_D_ORD, 96 d_D_ORD, 97 wd_D_ORD, 98 C_ORD, 99 w_C_ORD, 100 Cs_ORD, 101 Is_ORD, 102 LAST_ORD 103 }; 104 105 public: 106 StaticResolver(); 107 108 StaticResolver(const StaticResolver& pCopy); 109 110 virtual ~StaticResolver(); 111 112 /// shouldOverride - Can resolver override the symbol pOld by the symbol pNew? 113 /// @return the action should be taken. 114 /// @param pOld the symbol which may be overridden. 115 /// @param pNew the symbol which is used to replace pOld 116 virtual unsigned int resolve(ResolveInfo & __restrict__ pOld, 117 const ResolveInfo & __restrict__ pNew, 118 bool &pOverride); 119 doClone()120 StaticResolver* doClone() const { 121 return new StaticResolver(*this); 122 } 123 124 private: getOrdinate(const ResolveInfo & pInfo)125 inline unsigned int getOrdinate(const ResolveInfo& pInfo) const { 126 if (pInfo.isAbsolute() && pInfo.isDyn()) 127 return d_D_ORD; 128 if (pInfo.isAbsolute()) 129 return D_ORD; 130 if (pInfo.isCommon() && pInfo.isDyn()) 131 return Cs_ORD; 132 if (pInfo.isCommon() && pInfo.isDefine()) 133 return C_ORD; 134 if (pInfo.isCommon() && pInfo.isWeak()) 135 return w_C_ORD; 136 if (pInfo.isIndirect()) 137 return Is_ORD; 138 return pInfo.info(); 139 } 140 }; 141 142 } // namespace of mcld 143 144 #endif 145