Rafael Espindola | beee25e | 2015-08-14 14:12:54 +0000 | [diff] [blame] | 1 | //===- Symbols.h ------------------------------------------------*- C++ -*-===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 9 | // |
| 10 | // All symbols are handled as SymbolBodies regardless of their types. |
| 11 | // This file defines various types of SymbolBodies. |
| 12 | // |
| 13 | // File-scope symbols in ELF objects are the only exception of SymbolBody |
| 14 | // instantiation. We will never create SymbolBodies for them for performance |
| 15 | // reason. They are often represented as nullptrs. This is fine for symbol |
| 16 | // resolution because the symbol table naturally cares only about |
| 17 | // externally-visible symbols. For relocations, you have to deal with both |
| 18 | // local and non-local functions, and we have two different functions |
| 19 | // where we need them. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 22 | |
| 23 | #ifndef LLD_ELF_SYMBOLS_H |
| 24 | #define LLD_ELF_SYMBOLS_H |
| 25 | |
Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 26 | #include "InputSection.h" |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 27 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 28 | #include "lld/Core/LLVM.h" |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 29 | #include "llvm/Object/Archive.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 30 | #include "llvm/Object/ELF.h" |
| 31 | |
| 32 | namespace lld { |
| 33 | namespace elf2 { |
| 34 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 35 | class ArchiveFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 36 | class InputFile; |
| 37 | class SymbolBody; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 38 | template <class ELFT> class ObjectFile; |
Michael J. Spencer | 658dccd | 2015-09-18 22:13:25 +0000 | [diff] [blame] | 39 | template <class ELFT> class OutputSection; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 40 | template <class ELFT> class OutputSectionBase; |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 41 | template <class ELFT> class SharedFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 42 | |
Rui Ueyama | 9ea49c7 | 2015-10-07 23:46:11 +0000 | [diff] [blame] | 43 | // Initializes global objects defined in this file. |
| 44 | // Called at the beginning of main(). |
| 45 | void initSymbols(); |
| 46 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 47 | // A real symbol object, SymbolBody, is usually accessed indirectly |
| 48 | // through a Symbol. There's always one Symbol for each symbol name. |
| 49 | // The resolver updates SymbolBody pointers as it resolves symbols. |
| 50 | struct Symbol { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 51 | SymbolBody *Body; |
| 52 | }; |
| 53 | |
| 54 | // The base class for real symbol classes. |
| 55 | class SymbolBody { |
| 56 | public: |
| 57 | enum Kind { |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 58 | DefinedFirst, |
| 59 | DefinedRegularKind = DefinedFirst, |
| 60 | DefinedAbsoluteKind, |
| 61 | DefinedCommonKind, |
| 62 | DefinedSyntheticKind, |
| 63 | SharedKind, |
| 64 | DefinedLast = SharedKind, |
| 65 | UndefinedKind, |
| 66 | LazyKind |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 69 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 70 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 71 | bool isWeak() const { return IsWeak; } |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 72 | bool isUndefined() const { return SymbolKind == UndefinedKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 73 | bool isDefined() const { return SymbolKind <= DefinedLast; } |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 74 | bool isCommon() const { return SymbolKind == DefinedCommonKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 75 | bool isLazy() const { return SymbolKind == LazyKind; } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 76 | bool isShared() const { return SymbolKind == SharedKind; } |
| 77 | bool isUsedInRegularObj() const { return IsUsedInRegularObj; } |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 78 | bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; } |
| 79 | void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; } |
Rui Ueyama | 61805ec | 2015-12-17 00:12:03 +0000 | [diff] [blame] | 80 | bool isTls() const { return IsTls; } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 81 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 82 | // Returns the symbol name. |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 83 | StringRef getName() const { return Name; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 84 | |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 85 | uint8_t getVisibility() const { return Visibility; } |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 86 | |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 87 | unsigned DynamicSymbolTableIndex = 0; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 88 | uint32_t GlobalDynIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 89 | uint32_t GotIndex = -1; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 90 | uint32_t GotPltIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 91 | uint32_t PltIndex = -1; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 92 | bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); } |
Rafael Espindola | 5c2310c | 2015-09-18 14:40:19 +0000 | [diff] [blame] | 93 | bool isInGot() const { return GotIndex != -1U; } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 94 | bool isInGotPlt() const { return GotPltIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 95 | bool isInPlt() const { return PltIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 96 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 97 | // A SymbolBody has a backreference to a Symbol. Originally they are |
| 98 | // doubly-linked. A backreference will never change. But the pointer |
| 99 | // in the Symbol may be mutated by the resolver. If you have a |
| 100 | // pointer P to a SymbolBody and are not sure whether the resolver |
| 101 | // has chosen the object among other objects having the same name, |
| 102 | // you can access P->Backref->Body to get the resolver's result. |
| 103 | void setBackref(Symbol *P) { Backref = P; } |
Rui Ueyama | e66e001 | 2015-10-07 23:20:23 +0000 | [diff] [blame] | 104 | SymbolBody *repl() { return Backref ? Backref->Body : this; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 105 | |
| 106 | // Decides which symbol should "win" in the symbol table, this or |
| 107 | // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if |
| 108 | // they are duplicate (conflicting) symbols. |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 109 | template <class ELFT> int compare(SymbolBody *Other); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 110 | |
| 111 | protected: |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 112 | SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, |
Rui Ueyama | 61805ec | 2015-12-17 00:12:03 +0000 | [diff] [blame] | 113 | bool IsTls) |
| 114 | : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTls(IsTls), |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 115 | Name(Name) { |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 116 | IsUsedInRegularObj = K != SharedKind && K != LazyKind; |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 117 | IsUsedInDynamicReloc = 0; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 118 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 119 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 120 | const unsigned SymbolKind : 8; |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 121 | unsigned IsWeak : 1; |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 122 | unsigned Visibility : 2; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 123 | unsigned IsUsedInRegularObj : 1; |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 124 | unsigned IsUsedInDynamicReloc : 1; |
Rui Ueyama | 61805ec | 2015-12-17 00:12:03 +0000 | [diff] [blame] | 125 | unsigned IsTls : 1; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 126 | StringRef Name; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 127 | Symbol *Backref = nullptr; |
| 128 | }; |
| 129 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 130 | // This is for symbols created from elf files and not from the command line. |
| 131 | // Since they come from object files, they have a Elf_Sym. |
| 132 | // |
| 133 | // FIXME: Another alternative is to give every symbol an Elf_Sym. To do that |
| 134 | // we have to delay creating the symbol table until the output format is |
| 135 | // known and some of its methods will be templated. We should experiment with |
| 136 | // that once we have a bit more code. |
| 137 | template <class ELFT> class ELFSymbolBody : public SymbolBody { |
| 138 | protected: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 139 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 140 | ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym) |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 141 | : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK, |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 142 | Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS), |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 143 | Sym(Sym) {} |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 144 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 145 | public: |
| 146 | const Elf_Sym &Sym; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 147 | |
| 148 | static bool classof(const SymbolBody *S) { |
| 149 | Kind K = S->kind(); |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 150 | return K >= DefinedFirst && K <= UndefinedKind; |
| 151 | } |
| 152 | }; |
| 153 | |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 154 | // The base class for any defined symbols, including absolute symbols, etc. |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 155 | template <class ELFT> class Defined : public ELFSymbolBody<ELFT> { |
| 156 | typedef ELFSymbolBody<ELFT> Base; |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 157 | |
| 158 | protected: |
| 159 | typedef typename Base::Kind Kind; |
| 160 | typedef typename Base::Elf_Sym Elf_Sym; |
| 161 | |
| 162 | public: |
Rui Ueyama | 294b136 | 2015-09-30 02:06:17 +0000 | [diff] [blame] | 163 | Defined(Kind K, StringRef N, const Elf_Sym &Sym) |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 164 | : ELFSymbolBody<ELFT>(K, N, Sym) {} |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 165 | |
| 166 | static bool classof(const SymbolBody *S) { return S->isDefined(); } |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | template <class ELFT> class DefinedAbsolute : public Defined<ELFT> { |
| 170 | typedef ELFSymbolBody<ELFT> Base; |
| 171 | typedef typename Base::Elf_Sym Elf_Sym; |
| 172 | |
| 173 | public: |
Rafael Espindola | d27adc42e | 2015-09-24 13:34:01 +0000 | [diff] [blame] | 174 | static Elf_Sym IgnoreUndef; |
| 175 | |
Igor Kudrin | b044af5 | 2015-11-20 02:32:35 +0000 | [diff] [blame] | 176 | // The following symbols must be added early to reserve their places |
| 177 | // in symbol tables. The value of the symbols are set when all sections |
| 178 | // are finalized and their addresses are determined. |
| 179 | |
| 180 | // The content for _end and end symbols. |
| 181 | static Elf_Sym End; |
| 182 | |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 183 | // The content for _gp symbol for MIPS target. |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 184 | static Elf_Sym MipsGp; |
| 185 | |
George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 186 | // __rel_iplt_start/__rel_iplt_end for signaling |
| 187 | // where R_[*]_IRELATIVE relocations do live. |
| 188 | static Elf_Sym RelaIpltStart; |
| 189 | static Elf_Sym RelaIpltEnd; |
| 190 | |
Rui Ueyama | 294b136 | 2015-09-30 02:06:17 +0000 | [diff] [blame] | 191 | DefinedAbsolute(StringRef N, const Elf_Sym &Sym) |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 192 | : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {} |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 193 | |
| 194 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 195 | return S->kind() == Base::DefinedAbsoluteKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 196 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Rafael Espindola | d27adc42e | 2015-09-24 13:34:01 +0000 | [diff] [blame] | 199 | template <class ELFT> |
| 200 | typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef; |
| 201 | |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 202 | template <class ELFT> |
Igor Kudrin | b044af5 | 2015-11-20 02:32:35 +0000 | [diff] [blame] | 203 | typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::End; |
| 204 | |
| 205 | template <class ELFT> |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 206 | typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::MipsGp; |
| 207 | |
George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 208 | template <class ELFT> |
| 209 | typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::RelaIpltStart; |
| 210 | |
| 211 | template <class ELFT> |
| 212 | typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::RelaIpltEnd; |
| 213 | |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 214 | template <class ELFT> class DefinedCommon : public Defined<ELFT> { |
| 215 | typedef ELFSymbolBody<ELFT> Base; |
| 216 | typedef typename Base::Elf_Sym Elf_Sym; |
| 217 | |
| 218 | public: |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 219 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rui Ueyama | 294b136 | 2015-09-30 02:06:17 +0000 | [diff] [blame] | 220 | DefinedCommon(StringRef N, const Elf_Sym &Sym) |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 221 | : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) { |
| 222 | MaxAlignment = Sym.st_value; |
| 223 | } |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 224 | |
| 225 | static bool classof(const SymbolBody *S) { |
| 226 | return S->kind() == Base::DefinedCommonKind; |
| 227 | } |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 228 | |
| 229 | // The output offset of this common symbol in the output bss. Computed by the |
| 230 | // writer. |
| 231 | uintX_t OffsetInBSS; |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 232 | |
| 233 | // The maximum alignment we have seen for this symbol. |
| 234 | uintX_t MaxAlignment; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 237 | // Regular defined symbols read from object file symbol tables. |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 238 | template <class ELFT> class DefinedRegular : public Defined<ELFT> { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 239 | typedef Defined<ELFT> Base; |
| 240 | typedef typename Base::Elf_Sym Elf_Sym; |
| 241 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 242 | public: |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 243 | DefinedRegular(StringRef N, const Elf_Sym &Sym, |
| 244 | InputSectionBase<ELFT> &Section) |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 245 | : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 246 | |
| 247 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 248 | return S->kind() == Base::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 249 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 250 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 251 | InputSectionBase<ELFT> &Section; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
Rui Ueyama | 8b75b9a | 2015-12-17 00:48:16 +0000 | [diff] [blame] | 254 | // DefinedSynthetic is a class to represent linker-generated ELF symbols. |
| 255 | // The difference from the regular symbol is that DefinedSynthetic symbols |
| 256 | // don't belong to any input files or sections. Thus, its constructor |
| 257 | // takes an output section to calculate output VA, etc. |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 258 | template <class ELFT> class DefinedSynthetic : public Defined<ELFT> { |
| 259 | typedef Defined<ELFT> Base; |
| 260 | |
| 261 | public: |
| 262 | typedef typename Base::Elf_Sym Elf_Sym; |
Rui Ueyama | 294b136 | 2015-09-30 02:06:17 +0000 | [diff] [blame] | 263 | DefinedSynthetic(StringRef N, const Elf_Sym &Sym, |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 264 | OutputSectionBase<ELFT> &Section) |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 265 | : Defined<ELFT>(Base::DefinedSyntheticKind, N, Sym), Section(Section) {} |
| 266 | |
| 267 | static bool classof(const SymbolBody *S) { |
| 268 | return S->kind() == Base::DefinedSyntheticKind; |
| 269 | } |
| 270 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 271 | const OutputSectionBase<ELFT> &Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 274 | // Undefined symbol. |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 275 | template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> { |
| 276 | typedef ELFSymbolBody<ELFT> Base; |
| 277 | typedef typename Base::Elf_Sym Elf_Sym; |
| 278 | |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 279 | public: |
Rui Ueyama | 833ce28 | 2015-10-08 00:29:00 +0000 | [diff] [blame] | 280 | static Elf_Sym Required; |
| 281 | static Elf_Sym Optional; |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 282 | |
Rui Ueyama | 294b136 | 2015-09-30 02:06:17 +0000 | [diff] [blame] | 283 | Undefined(StringRef N, const Elf_Sym &Sym) |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 284 | : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {} |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 285 | |
| 286 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 287 | return S->kind() == Base::UndefinedKind; |
| 288 | } |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 289 | |
Rui Ueyama | 833ce28 | 2015-10-08 00:29:00 +0000 | [diff] [blame] | 290 | bool canKeepUndefined() const { return &this->Sym == &Optional; } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 293 | template <class ELFT> |
Rui Ueyama | 833ce28 | 2015-10-08 00:29:00 +0000 | [diff] [blame] | 294 | typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Required; |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 295 | template <class ELFT> |
Rui Ueyama | 833ce28 | 2015-10-08 00:29:00 +0000 | [diff] [blame] | 296 | typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Optional; |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 297 | |
Rafael Espindola | 38af127 | 2015-09-25 15:34:03 +0000 | [diff] [blame] | 298 | template <class ELFT> class SharedSymbol : public Defined<ELFT> { |
Rafael Espindola | 5b197f0 | 2015-09-25 18:32:09 +0000 | [diff] [blame] | 299 | typedef Defined<ELFT> Base; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 300 | typedef typename Base::Elf_Sym Elf_Sym; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 301 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 302 | |
| 303 | public: |
| 304 | static bool classof(const SymbolBody *S) { |
| 305 | return S->kind() == Base::SharedKind; |
| 306 | } |
| 307 | |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 308 | SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym) |
| 309 | : Defined<ELFT>(Base::SharedKind, Name, Sym), File(F) {} |
| 310 | |
| 311 | SharedFile<ELFT> *File; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 312 | |
Rui Ueyama | bb93606 | 2015-12-17 01:14:23 +0000 | [diff] [blame] | 313 | // True if the linker has to generate a copy relocation for this shared |
| 314 | // symbol. OffsetInBSS is significant only when NeedsCopy is true. |
| 315 | bool NeedsCopy = false; |
| 316 | uintX_t OffsetInBSS = 0; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 319 | // This class represents a symbol defined in an archive file. It is |
| 320 | // created from an archive file header, and it knows how to load an |
| 321 | // object file from an archive to replace itself with a defined |
| 322 | // symbol. If the resolver finds both Undefined and Lazy for |
| 323 | // the same name, it will ask the Lazy to load a file. |
| 324 | class Lazy : public SymbolBody { |
| 325 | public: |
| 326 | Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S) |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 327 | : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false), |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 328 | File(F), Sym(S) {} |
| 329 | |
| 330 | static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; } |
| 331 | |
| 332 | // Returns an object file for this symbol, or a nullptr if the file |
| 333 | // was already returned. |
| 334 | std::unique_ptr<InputFile> getMember(); |
| 335 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 336 | void setWeak() { IsWeak = true; } |
| 337 | void setUsedInRegularObj() { IsUsedInRegularObj = true; } |
| 338 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 339 | private: |
| 340 | ArchiveFile *File; |
| 341 | const llvm::object::Archive::Symbol Sym; |
| 342 | }; |
| 343 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 344 | } // namespace elf2 |
| 345 | } // namespace lld |
| 346 | |
| 347 | #endif |