1 //===- ResolveInfo.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_RESOLVE_INFO_H 10 #define MCLD_RESOLVE_INFO_H 11 #ifdef ENABLE_UNITTEST 12 #include <gtest.h> 13 #endif 14 15 #include <llvm/Support/DataTypes.h> 16 #include <llvm/ADT/StringRef.h> 17 18 namespace mcld 19 { 20 21 class LDSymbol; 22 23 /** \class ResolveInfo 24 * \brief ResolveInfo records the information about how to resolve a symbol. 25 * 26 * A symbol must have some `attributes': 27 * - Desc - Defined, Reference, Common or Indirect 28 * - Binding - Global, Local, Weak 29 * - IsDyn - appear in dynamic objects or regular objects 30 * - Type - what the symbol points to 31 * - Size - the size of the symbol point to 32 * - Value - the pointer to another LDSymbol 33 * In order to save the memory and speed up the performance, MCLinker uses 34 * a bit field to store all attributes. 35 * 36 * The maximum string length is (2^16 - 1) 37 */ 38 class ResolveInfo 39 { 40 friend class ResolveInfoFactory; 41 friend class MCLinker; 42 public: 43 typedef uint64_t SizeType; 44 45 /** \enum Type 46 * \brief What the symbol stand for 47 * 48 * It is like ELF32_ST_TYPE 49 * MachO does not need this, and can not jump between Thumb and ARM code. 50 */ 51 enum Type { 52 NoType = 0, 53 Object = 1, 54 Function = 2, 55 Section = 3, 56 File = 4, 57 CommonBlock = 5, 58 ThreadLocal = 6, 59 LoProc = 13, 60 HiProc = 15 61 }; 62 63 /** \enum Desc 64 * \brief Description of the symbols. 65 * 66 * Follow the naming in MachO. Like MachO nlist::n_desc 67 * In ELF, is a part of st_shndx 68 */ 69 enum Desc { 70 Undefined = 0, 71 Define = 1, 72 Common = 2, 73 Indirect = 3, 74 NoneDesc 75 }; 76 77 enum Binding { 78 Global = 0, 79 Weak = 1, 80 Local = 2, 81 Absolute = 3, 82 NoneBinding 83 }; 84 85 enum Visibility { 86 Default = 0, 87 Internal = 1, 88 Hidden = 2, 89 Protected = 3 90 }; 91 92 // ----- For HashTable ----- // 93 typedef llvm::StringRef key_type; 94 95 public: 96 // ----- modifiers ----- // 97 /// setRegular - set the source of the file is a regular object 98 void setRegular(); 99 100 /// setDynamic - set the source of the file is a dynamic object 101 void setDynamic(); 102 103 /// setSource - set the source of the file 104 /// @param pIsDyn is the source from a dynamic object? 105 void setSource(bool pIsDyn); 106 107 void setType(uint32_t pType); 108 109 void setDesc(uint32_t pDesc); 110 111 void setBinding(uint32_t pBinding); 112 113 void setOther(uint32_t pOther); 114 115 void setVisibility(Visibility pVisibility); 116 117 void setIsSymbol(bool pIsSymbol); 118 119 void setReserved(uint32_t pReserved); 120 setSize(SizeType pSize)121 void setSize(SizeType pSize) 122 { m_Size = pSize; } 123 124 void override(const ResolveInfo& pForm); 125 126 void overrideAttributes(const ResolveInfo& pFrom); 127 128 void overrideVisibility(const ResolveInfo& pFrom); 129 setSymPtr(const LDSymbol * pSymPtr)130 void setSymPtr(const LDSymbol* pSymPtr) 131 { m_Ptr.sym_ptr = const_cast<LDSymbol*>(pSymPtr); } 132 setLink(const ResolveInfo * pTarget)133 void setLink(const ResolveInfo* pTarget) { 134 m_Ptr.info_ptr = const_cast<ResolveInfo*>(pTarget); 135 m_BitField |= indirect_flag; 136 } 137 138 139 // ----- observers ----- // 140 bool isSymbol() const; 141 142 bool isString() const; 143 144 bool isGlobal() const; 145 146 bool isWeak() const; 147 148 bool isLocal() const; 149 150 bool isAbsolute() const; 151 152 bool isDefine() const; 153 154 bool isUndef() const; 155 156 bool isDyn() const; 157 158 bool isCommon() const; 159 160 bool isIndirect() const; 161 162 uint32_t type() const; 163 164 uint32_t desc() const; 165 166 uint32_t binding() const; 167 168 uint32_t reserved() const; 169 other()170 uint8_t other() const 171 { return (uint8_t)visibility(); } 172 173 Visibility visibility() const; 174 outSymbol()175 LDSymbol* outSymbol() 176 { return m_Ptr.sym_ptr; } 177 outSymbol()178 const LDSymbol* outSymbol() const 179 { return m_Ptr.sym_ptr; } 180 link()181 ResolveInfo* link() 182 { return m_Ptr.info_ptr; } 183 link()184 const ResolveInfo* link() const 185 { return m_Ptr.info_ptr; } 186 size()187 SizeType size() const 188 { return m_Size; } 189 name()190 const char* name() const 191 { return m_Name; } 192 nameSize()193 unsigned int nameSize() const 194 { return (m_BitField >> NAME_LENGTH_OFFSET); } 195 info()196 uint32_t info() const 197 { return (m_BitField & INFO_MASK); } 198 bitfield()199 uint32_t bitfield() const 200 { return m_BitField; } 201 202 // ----- For HashTable ----- // 203 bool compare(const key_type& pKey); 204 205 private: 206 static const uint32_t GLOBAL_OFFSET = 0; 207 static const uint32_t GLOBAL_MASK = 1; 208 209 static const uint32_t DYN_OFFSET = 1; 210 static const uint32_t DYN_MASK = 1 << DYN_OFFSET; 211 212 static const uint32_t DESC_OFFSET = 2; 213 static const uint32_t DESC_MASK = 0x3 << DESC_OFFSET; 214 215 static const uint32_t LOCAL_OFFSET = 4; 216 static const uint32_t LOCAL_MASK = 1 << LOCAL_OFFSET; 217 218 static const uint32_t BINDING_MASK = GLOBAL_MASK | LOCAL_MASK; 219 220 static const uint32_t VISIBILITY_OFFSET = 5; 221 static const uint32_t VISIBILITY_MASK = 0x3 << VISIBILITY_OFFSET; 222 223 static const uint32_t TYPE_OFFSET = 7; 224 static const uint32_t TYPE_MASK = 0xF << TYPE_OFFSET; 225 226 static const uint32_t SYMBOL_OFFSET = 11; 227 static const uint32_t SYMBOL_MASK = 1 << SYMBOL_OFFSET; 228 229 static const uint32_t RESERVED_OFFSET = 12; 230 static const uint32_t RESERVED_MASK = 0xF << RESERVED_OFFSET; 231 static const uint32_t NAME_LENGTH_OFFSET = 16; 232 static const uint32_t INFO_MASK = 0xF; 233 static const uint32_t RESOLVE_MASK = 0xFFFF; 234 235 union SymOrInfo { 236 LDSymbol* sym_ptr; 237 ResolveInfo* info_ptr; 238 }; 239 240 public: 241 static const uint32_t global_flag = 0 << GLOBAL_OFFSET; 242 static const uint32_t weak_flag = 1 << GLOBAL_OFFSET; 243 static const uint32_t regular_flag = 0 << DYN_OFFSET; 244 static const uint32_t dynamic_flag = 1 << DYN_OFFSET; 245 static const uint32_t undefine_flag = 0 << DESC_OFFSET; 246 static const uint32_t define_flag = 1 << DESC_OFFSET; 247 static const uint32_t common_flag = 2 << DESC_OFFSET; 248 static const uint32_t indirect_flag = 3 << DESC_OFFSET; 249 static const uint32_t local_flag = 1 << LOCAL_OFFSET; 250 static const uint32_t absolute_flag = BINDING_MASK; 251 static const uint32_t object_flag = Object << TYPE_OFFSET; 252 static const uint32_t function_flag = Function << TYPE_OFFSET; 253 static const uint32_t section_flag = Section << TYPE_OFFSET; 254 static const uint32_t file_flag = File << TYPE_OFFSET; 255 static const uint32_t string_flag = 0 << SYMBOL_OFFSET; 256 static const uint32_t symbol_flag = 1 << SYMBOL_OFFSET; 257 258 private: 259 ResolveInfo(); 260 ResolveInfo(const ResolveInfo& pCopy); 261 ResolveInfo& operator=(const ResolveInfo& pCopy); 262 ~ResolveInfo(); 263 264 private: 265 SizeType m_Size; 266 SymOrInfo m_Ptr; 267 268 /** m_BitField 269 * 31 ... 16 15 12 11 10..7 6 .. 5 4 3 2 1 0 270 * |length of m_Name|reserved|Symbol|Type |ELF visibility|Local|Com|Def|Dyn|Weak| 271 */ 272 uint32_t m_BitField; 273 char m_Name[0]; 274 }; 275 276 } // namespace of mcld 277 278 #endif 279