1 //===- LDSymbol.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_LD_LDSYMBOL_H_
10 #define MCLD_LD_LDSYMBOL_H_
11 
12 #include "mcld/Config/Config.h"
13 #include "mcld/LD/ResolveInfo.h"
14 #include "mcld/Support/Allocators.h"
15 
16 #include <cassert>
17 
18 namespace llvm {
19 
20 // forware declaration
21 template <class T>
22 void* object_creator();
23 
24 }  // namespace llvm
25 
26 namespace mcld {
27 
28 class FragmentRef;
29 
30 /** \class LDSymbol
31  *  \brief LDSymbol provides a consistent abstraction for different formats
32  *  in different targets.
33  */
34 class LDSymbol {
35  public:
36   // FIXME: use SizeTrait<32> or SizeTrait<64> instead of big type
37   typedef ResolveInfo::SizeType SizeType;
38   typedef uint64_t ValueType;
39 
40  public:
41   ~LDSymbol();
42 
43   // -----  factory method ----- //
44   static LDSymbol* Create(ResolveInfo& pResolveInfo);
45 
46   static void Destroy(LDSymbol*& pSymbol);
47 
48   /// Clear - This function tells MCLinker to clear all created LDSymbols.
49   static void Clear();
50 
51   /// NullSymbol() - This returns a reference to a LDSymbol that represents Null
52   /// symbol.
53   static LDSymbol* Null();
54 
55   // -----  observers  ----- //
56   bool isNull() const;
57 
name()58   const char* name() const {
59     assert(m_pResolveInfo != NULL);
60     return m_pResolveInfo->name();
61   }
62 
nameSize()63   unsigned int nameSize() const {
64     assert(m_pResolveInfo != NULL);
65     return m_pResolveInfo->nameSize();
66   }
67 
str()68   llvm::StringRef str() const {
69     assert(m_pResolveInfo != NULL);
70     return llvm::StringRef(m_pResolveInfo->name(), m_pResolveInfo->nameSize());
71   }
72 
isDyn()73   bool isDyn() const {
74     assert(m_pResolveInfo != NULL);
75     return m_pResolveInfo->isDyn();
76   }
77 
type()78   unsigned int type() const {
79     assert(m_pResolveInfo != NULL);
80     return m_pResolveInfo->type();
81   }
desc()82   unsigned int desc() const {
83     assert(m_pResolveInfo != NULL);
84     return m_pResolveInfo->desc();
85   }
binding()86   unsigned int binding() const {
87     assert(m_pResolveInfo != NULL);
88     return m_pResolveInfo->binding();
89   }
90 
other()91   uint8_t other() const {
92     assert(m_pResolveInfo != NULL);
93     return m_pResolveInfo->other();
94   }
95 
visibility()96   uint8_t visibility() const {
97     assert(m_pResolveInfo != NULL);
98     return m_pResolveInfo->other();
99   }
100 
value()101   ValueType value() const { return m_Value; }
102 
fragRef()103   const FragmentRef* fragRef() const { return m_pFragRef; }
fragRef()104   FragmentRef* fragRef() { return m_pFragRef; }
105 
size()106   SizeType size() const { return m_pResolveInfo->size(); }
107 
resolveInfo()108   const ResolveInfo* resolveInfo() const { return m_pResolveInfo; }
resolveInfo()109   ResolveInfo* resolveInfo() { return m_pResolveInfo; }
110 
111   bool hasFragRef() const;
112 
113   // -----  modifiers  ----- //
setSize(SizeType pSize)114   void setSize(SizeType pSize) {
115     assert(m_pResolveInfo != NULL);
116     m_pResolveInfo->setSize(pSize);
117   }
118 
setValue(ValueType pValue)119   void setValue(ValueType pValue) { m_Value = pValue; }
120 
121   void setFragmentRef(FragmentRef* pFragmentRef);
122 
123   void setResolveInfo(const ResolveInfo& pInfo);
124 
125  private:
126   friend class Chunk<LDSymbol, MCLD_SYMBOLS_PER_INPUT>;
127   template <class T>
128   friend void* llvm::object_creator();
129 
130   LDSymbol();
131   LDSymbol(const LDSymbol& pCopy);
132   LDSymbol& operator=(const LDSymbol& pCopy);
133 
134  private:
135   // -----  Symbol's fields  ----- //
136   ResolveInfo* m_pResolveInfo;
137   FragmentRef* m_pFragRef;
138   ValueType m_Value;
139 };
140 
141 }  // namespace mcld
142 
143 #endif  // MCLD_LD_LDSYMBOL_H_
144