summaryrefslogtreecommitdiff
path: root/include/mcld/LD/ResolveInfo.h
blob: 70e4f27b7e57c288224b867c7a286bf3d2d52a18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//===- ResolveInfo.h ------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef MCLD_RESOLVE_INFO_H
#define MCLD_RESOLVE_INFO_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif

#include <llvm/Support/DataTypes.h>
#include <llvm/ADT/StringRef.h>

namespace mcld
{

class LDSymbol;

/** \class ResolveInfo
 *  \brief ResolveInfo records the information about how to resolve a symbol.
 *
 *  A symbol must have some `attributes':
 *  - Desc - Defined, Reference, Common or Indirect
 *  - Binding - Global, Local, Weak
 *  - IsDyn - appear in dynamic objects or regular objects
 *  - Type - what the symbol points to
 *  - Size  - the size of the symbol point to
 *  - Value - the pointer to another LDSymbol
 *  In order to save the memory and speed up the performance, MCLinker uses
 *  a bit field to store all attributes.
 *
 *  The maximum string length is (2^16 - 1)
 */
class ResolveInfo
{
friend class ResolveInfoFactory;
friend class MCLinker;
public:
  typedef uint64_t SizeType;

  /** \enum Type
   *  \brief What the symbol stand for
   *
   *  It is like ELF32_ST_TYPE
   *  MachO does not need this, and can not jump between Thumb and ARM code.
   */
  enum Type {
    NoType        = 0,
    Object        = 1,
    Function      = 2,
    Section       = 3,
    File          = 4,
    CommonBlock   = 5,
    ThreadLocal    = 6,
    LoProc        = 13,
    HiProc        = 15
  };

  /** \enum Desc
   *  \brief Description of the symbols.
   *
   *   Follow the naming in MachO. Like MachO nlist::n_desc
   *   In ELF, is a part of st_shndx
   */
  enum Desc {
    Undefined    = 0,
    Define       = 1,
    Common       = 2,
    Indirect     = 3,
    NoneDesc
  };

  enum Binding {
    Global       = 0,
    Weak         = 1,
    Local        = 2,
    Absolute     = 3,
    NoneBinding
  };

  enum Visibility {
    Default      = 0,
    Internal     = 1,
    Hidden       = 2,
    Protected    = 3
  };

  // -----  For HashTable  ----- //
  typedef llvm::StringRef key_type;

public:
  // -----  modifiers  ----- //
  /// setRegular - set the source of the file is a regular object
  void setRegular();

  /// setDynamic - set the source of the file is a dynamic object
  void setDynamic();

  /// setSource - set the source of the file
  /// @param pIsDyn is the source from a dynamic object?
  void setSource(bool pIsDyn);

  void setType(uint32_t pType);

  void setDesc(uint32_t pDesc);

  void setBinding(uint32_t pBinding);

  void setOther(uint32_t pOther);

  void setVisibility(Visibility pVisibility);

  void setIsSymbol(bool pIsSymbol);

  void setReserved(uint32_t pReserved);

  void setSize(SizeType pSize)
  { m_Size = pSize; }

  void override(const ResolveInfo& pForm);

  void overrideAttributes(const ResolveInfo& pFrom);

  void overrideVisibility(const ResolveInfo& pFrom);

  void setSymPtr(const LDSymbol* pSymPtr)
  { m_Ptr.sym_ptr = const_cast<LDSymbol*>(pSymPtr); }

  void setLink(const ResolveInfo* pTarget) {
    m_Ptr.info_ptr = const_cast<ResolveInfo*>(pTarget);
    m_BitField |= indirect_flag;
  }


  // -----  observers  ----- //
  bool isSymbol() const;

  bool isString() const;

  bool isGlobal() const;

  bool isWeak() const;

  bool isLocal() const;

  bool isAbsolute() const;

  bool isDefine() const;

  bool isUndef() const;

  bool isDyn() const;

  bool isCommon() const;

  bool isIndirect() const;

  uint32_t type() const;

  uint32_t desc() const;

  uint32_t binding() const;

  uint32_t reserved() const;

  uint8_t other() const
  { return (uint8_t)visibility(); }

  Visibility visibility() const;

  LDSymbol* outSymbol()
  { return m_Ptr.sym_ptr; }

  const LDSymbol* outSymbol() const
  { return m_Ptr.sym_ptr; }

  ResolveInfo* link()
  { return m_Ptr.info_ptr; }

  const ResolveInfo* link() const
  { return m_Ptr.info_ptr; }

  SizeType size() const
  { return m_Size; }

  const char* name() const
  { return m_Name; }

  unsigned int nameSize() const
  { return (m_BitField >> NAME_LENGTH_OFFSET); }

  uint32_t info() const
  { return (m_BitField & INFO_MASK); }

  uint32_t bitfield() const
  { return m_BitField; }

  // -----  For HashTable  ----- //
  bool compare(const key_type& pKey);

private:
  static const uint32_t GLOBAL_OFFSET      = 0;
  static const uint32_t GLOBAL_MASK        = 1;

  static const uint32_t DYN_OFFSET         = 1;
  static const uint32_t DYN_MASK           = 1   << DYN_OFFSET;

  static const uint32_t DESC_OFFSET        = 2;
  static const uint32_t DESC_MASK          = 0x3 << DESC_OFFSET;

  static const uint32_t LOCAL_OFFSET       = 4;
  static const uint32_t LOCAL_MASK         = 1   << LOCAL_OFFSET;

  static const uint32_t BINDING_MASK       = GLOBAL_MASK | LOCAL_MASK;

  static const uint32_t VISIBILITY_OFFSET  = 5;
  static const uint32_t VISIBILITY_MASK    = 0x3 << VISIBILITY_OFFSET;

  static const uint32_t TYPE_OFFSET        = 7;
  static const uint32_t TYPE_MASK          = 0xF << TYPE_OFFSET;

  static const uint32_t SYMBOL_OFFSET      = 11;
  static const uint32_t SYMBOL_MASK        = 1   << SYMBOL_OFFSET;

  static const uint32_t RESERVED_OFFSET    = 12;
  static const uint32_t RESERVED_MASK      = 0xF << RESERVED_OFFSET;
  static const uint32_t NAME_LENGTH_OFFSET = 16;
  static const uint32_t INFO_MASK          = 0xF;
  static const uint32_t RESOLVE_MASK       = 0xFFFF;

  union SymOrInfo {
    LDSymbol*    sym_ptr;
    ResolveInfo* info_ptr;
  };

public:
  static const uint32_t global_flag    = 0        << GLOBAL_OFFSET;
  static const uint32_t weak_flag      = 1        << GLOBAL_OFFSET;
  static const uint32_t regular_flag   = 0        << DYN_OFFSET;
  static const uint32_t dynamic_flag   = 1        << DYN_OFFSET;
  static const uint32_t undefine_flag  = 0        << DESC_OFFSET;
  static const uint32_t define_flag    = 1        << DESC_OFFSET;
  static const uint32_t common_flag    = 2        << DESC_OFFSET;
  static const uint32_t indirect_flag  = 3        << DESC_OFFSET;
  static const uint32_t local_flag     = 1        << LOCAL_OFFSET;
  static const uint32_t absolute_flag  = BINDING_MASK;
  static const uint32_t object_flag    = Object   << TYPE_OFFSET;
  static const uint32_t function_flag  = Function << TYPE_OFFSET;
  static const uint32_t section_flag   = Section  << TYPE_OFFSET;
  static const uint32_t file_flag      = File     << TYPE_OFFSET;
  static const uint32_t string_flag    = 0        << SYMBOL_OFFSET;
  static const uint32_t symbol_flag    = 1        << SYMBOL_OFFSET;

private:
  ResolveInfo();
  ResolveInfo(const ResolveInfo& pCopy);
  ResolveInfo& operator=(const ResolveInfo& pCopy);
  ~ResolveInfo();

private:
  SizeType m_Size;
  SymOrInfo m_Ptr;

  /** m_BitField
   *  31     ...    16 15    12 11     10..7 6      ..    5 4     3   2   1   0
   * |length of m_Name|reserved|Symbol|Type |ELF visibility|Local|Com|Def|Dyn|Weak|
   */
  uint32_t m_BitField;
  char m_Name[0];
};

} // namespace of mcld

#endif