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