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