• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NamePool;
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   virtual ~StaticResolver();
107 
108   /// shouldOverride - Can resolver override the symbol pOld by the symbol pNew?
109   /// @return successfully resolved, return true; otherwise, return false.
110   /// @param pOld the symbol which may be overridden.
111   /// @param pNew the symbol which is used to replace pOld
112   virtual bool resolve(ResolveInfo & __restrict__ pOld,
113                        const ResolveInfo & __restrict__ pNew,
114                        bool &pOverride) const;
115 
116 private:
getOrdinate(const ResolveInfo & pInfo)117   inline unsigned int getOrdinate(const ResolveInfo& pInfo) const {
118     if (pInfo.isAbsolute() && pInfo.isDyn())
119       return d_D_ORD;
120     if (pInfo.isAbsolute())
121       return D_ORD;
122     if (pInfo.isCommon() && pInfo.isDyn())
123       return Cs_ORD;
124     if (pInfo.isCommon() && pInfo.isDefine())
125       return C_ORD;
126     if (pInfo.isCommon() && pInfo.isWeak())
127       return w_C_ORD;
128     if (pInfo.isIndirect())
129       return Is_ORD;
130     return pInfo.info();
131   }
132 };
133 
134 } // namespace of mcld
135 
136 #endif
137 
138