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, |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 60 | SharedKind, |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 61 | DefinedElfLast = SharedKind, |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 62 | DefinedCommonKind, |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 63 | DefinedSyntheticKind, |
| 64 | DefinedLast = DefinedSyntheticKind, |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 65 | UndefinedElfKind, |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 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 | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 73 | bool isUndefined() const { |
| 74 | return SymbolKind == UndefinedKind || SymbolKind == UndefinedElfKind; |
| 75 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 76 | bool isDefined() const { return SymbolKind <= DefinedLast; } |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 77 | bool isCommon() const { return SymbolKind == DefinedCommonKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 78 | bool isLazy() const { return SymbolKind == LazyKind; } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 79 | bool isShared() const { return SymbolKind == SharedKind; } |
| 80 | bool isUsedInRegularObj() const { return IsUsedInRegularObj; } |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 81 | bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; } |
| 82 | void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; } |
Rui Ueyama | 61805ec | 2015-12-17 00:12:03 +0000 | [diff] [blame] | 83 | bool isTls() const { return IsTls; } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 84 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 85 | // Returns the symbol name. |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 86 | StringRef getName() const { return Name; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 87 | |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 88 | uint8_t getVisibility() const { return Visibility; } |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 89 | |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 90 | unsigned DynamicSymbolTableIndex = 0; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 91 | uint32_t GlobalDynIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 92 | uint32_t GotIndex = -1; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 93 | uint32_t GotPltIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 94 | uint32_t PltIndex = -1; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 95 | bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-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, |
Rui Ueyama | 61805ec | 2015-12-17 00:12:03 +0000 | [diff] [blame] | 116 | bool IsTls) |
| 117 | : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTls(IsTls), |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 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; |
Rui Ueyama | 7d332f5 | 2015-12-25 06:55:39 +0000 | [diff] [blame] | 126 | |
| 127 | // True if the symbol was used for linking and thus need to be |
| 128 | // added to the output file's symbol table. It is usually true, |
| 129 | // but if it is a shared symbol that were not referenced by anyone, |
| 130 | // it can be false. |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 131 | unsigned IsUsedInRegularObj : 1; |
Rui Ueyama | 7d332f5 | 2015-12-25 06:55:39 +0000 | [diff] [blame] | 132 | |
| 133 | // If true, the symbol is added to .dynsym symbol table. |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 134 | unsigned IsUsedInDynamicReloc : 1; |
Rui Ueyama | 7d332f5 | 2015-12-25 06:55:39 +0000 | [diff] [blame] | 135 | |
Rui Ueyama | 61805ec | 2015-12-17 00:12:03 +0000 | [diff] [blame] | 136 | unsigned IsTls : 1; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 137 | StringRef Name; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 138 | Symbol *Backref = nullptr; |
| 139 | }; |
| 140 | |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 141 | // The base class for any defined symbols. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 142 | class Defined : public SymbolBody { |
| 143 | public: |
| 144 | Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, bool IsTls); |
| 145 | static bool classof(const SymbolBody *S) { return S->isDefined(); } |
| 146 | }; |
| 147 | |
| 148 | // Any defined symbol from an ELF file. |
| 149 | template <class ELFT> class DefinedElf : public Defined { |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 150 | protected: |
Rafael Espindola | 88dddf9 | 2015-12-21 21:07:31 +0000 | [diff] [blame] | 151 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 152 | |
| 153 | public: |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 154 | DefinedElf(Kind K, StringRef N, const Elf_Sym &Sym) |
| 155 | : Defined(K, N, Sym.getBinding() == llvm::ELF::STB_WEAK, |
| 156 | Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS), |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 157 | Sym(Sym) {} |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 158 | |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 159 | const Elf_Sym &Sym; |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 160 | static bool classof(const SymbolBody *S) { |
| 161 | return S->kind() <= DefinedElfLast; |
| 162 | } |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 165 | class DefinedCommon : public Defined { |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 166 | public: |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 167 | DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, bool IsWeak, |
| 168 | uint8_t Visibility); |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 169 | |
| 170 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 171 | return S->kind() == SymbolBody::DefinedCommonKind; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 172 | } |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 173 | |
| 174 | // The output offset of this common symbol in the output bss. Computed by the |
| 175 | // writer. |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 176 | uint64_t OffsetInBss; |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 177 | |
| 178 | // The maximum alignment we have seen for this symbol. |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 179 | uint64_t MaxAlignment; |
| 180 | |
| 181 | uint64_t Size; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 184 | // Regular defined symbols read from object file symbol tables. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 185 | template <class ELFT> class DefinedRegular : public DefinedElf<ELFT> { |
Rafael Espindola | 88dddf9 | 2015-12-21 21:07:31 +0000 | [diff] [blame] | 186 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 187 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 188 | public: |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 189 | DefinedRegular(StringRef N, const Elf_Sym &Sym, |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 190 | InputSectionBase<ELFT> *Section) |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 191 | : DefinedElf<ELFT>(SymbolBody::DefinedRegularKind, N, Sym), |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 192 | Section(Section) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 193 | |
| 194 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 195 | return S->kind() == SymbolBody::DefinedRegularKind; |
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 | |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 198 | // If this is null, the symbol is absolute. |
| 199 | InputSectionBase<ELFT> *Section; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Rui Ueyama | 8b75b9a | 2015-12-17 00:48:16 +0000 | [diff] [blame] | 202 | // DefinedSynthetic is a class to represent linker-generated ELF symbols. |
| 203 | // The difference from the regular symbol is that DefinedSynthetic symbols |
| 204 | // don't belong to any input files or sections. Thus, its constructor |
| 205 | // takes an output section to calculate output VA, etc. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 206 | template <class ELFT> class DefinedSynthetic : public Defined { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 207 | public: |
Rafael Espindola | 88dddf9 | 2015-12-21 21:07:31 +0000 | [diff] [blame] | 208 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 209 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 210 | DefinedSynthetic(StringRef N, uintX_t Value, |
| 211 | OutputSectionBase<ELFT> &Section); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 212 | |
| 213 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 214 | return S->kind() == SymbolBody::DefinedSyntheticKind; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 217 | uintX_t Value; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 218 | const OutputSectionBase<ELFT> &Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 221 | // Undefined symbol. |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 222 | class Undefined : public SymbolBody { |
| 223 | typedef SymbolBody::Kind Kind; |
| 224 | bool CanKeepUndefined; |
| 225 | |
| 226 | protected: |
| 227 | Undefined(Kind K, StringRef N, bool IsWeak, uint8_t Visibility, bool IsTls); |
| 228 | |
| 229 | public: |
| 230 | Undefined(StringRef N, bool IsWeak, uint8_t Visibility, |
| 231 | bool CanKeepUndefined); |
| 232 | |
| 233 | static bool classof(const SymbolBody *S) { return S->isUndefined(); } |
| 234 | |
| 235 | bool canKeepUndefined() const { return CanKeepUndefined; } |
| 236 | }; |
| 237 | |
| 238 | template <class ELFT> class UndefinedElf : public Undefined { |
Rafael Espindola | 88dddf9 | 2015-12-21 21:07:31 +0000 | [diff] [blame] | 239 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 240 | |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 241 | public: |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 242 | UndefinedElf(StringRef N, const Elf_Sym &Sym); |
| 243 | const Elf_Sym &Sym; |
Rafael Espindola | 5f2c46d | 2015-12-23 01:06:39 +0000 | [diff] [blame] | 244 | |
| 245 | static bool classof(const SymbolBody *S) { |
| 246 | return S->kind() == SymbolBody::UndefinedElfKind; |
| 247 | } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 250 | template <class ELFT> class SharedSymbol : public DefinedElf<ELFT> { |
Rafael Espindola | 88dddf9 | 2015-12-21 21:07:31 +0000 | [diff] [blame] | 251 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 252 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 253 | |
| 254 | public: |
| 255 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 256 | return S->kind() == SymbolBody::SharedKind; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 259 | SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym) |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 260 | : DefinedElf<ELFT>(SymbolBody::SharedKind, Name, Sym), File(F) {} |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 261 | |
| 262 | SharedFile<ELFT> *File; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 263 | |
Rui Ueyama | bb93606 | 2015-12-17 01:14:23 +0000 | [diff] [blame] | 264 | // True if the linker has to generate a copy relocation for this shared |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 265 | // symbol. OffsetInBss is significant only when NeedsCopy is true. |
Rui Ueyama | bb93606 | 2015-12-17 01:14:23 +0000 | [diff] [blame] | 266 | bool NeedsCopy = false; |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 267 | uintX_t OffsetInBss = 0; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 270 | // This class represents a symbol defined in an archive file. It is |
| 271 | // created from an archive file header, and it knows how to load an |
| 272 | // object file from an archive to replace itself with a defined |
| 273 | // symbol. If the resolver finds both Undefined and Lazy for |
| 274 | // the same name, it will ask the Lazy to load a file. |
| 275 | class Lazy : public SymbolBody { |
| 276 | public: |
| 277 | Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S) |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 278 | : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false), |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 279 | File(F), Sym(S) {} |
| 280 | |
| 281 | static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; } |
| 282 | |
| 283 | // Returns an object file for this symbol, or a nullptr if the file |
| 284 | // was already returned. |
| 285 | std::unique_ptr<InputFile> getMember(); |
| 286 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 287 | void setWeak() { IsWeak = true; } |
| 288 | void setUsedInRegularObj() { IsUsedInRegularObj = true; } |
| 289 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 290 | private: |
| 291 | ArchiveFile *File; |
| 292 | const llvm::object::Archive::Symbol Sym; |
| 293 | }; |
| 294 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 295 | // Some linker-generated symbols need to be created as |
| 296 | // DefinedRegular symbols, so they need Elf_Sym symbols. |
| 297 | // Here we allocate such Elf_Sym symbols statically. |
| 298 | template <class ELFT> struct ElfSym { |
| 299 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 300 | |
| 301 | // Used to represent an undefined symbol which we don't want |
| 302 | // to add to the output file's symbol table. |
| 303 | static Elf_Sym IgnoreUndef; |
| 304 | |
| 305 | // The content for _end and end symbols. |
| 306 | static Elf_Sym End; |
| 307 | |
| 308 | // The content for _gp symbol for MIPS target. |
| 309 | static Elf_Sym MipsGp; |
| 310 | |
| 311 | // __rel_iplt_start/__rel_iplt_end for signaling |
| 312 | // where R_[*]_IRELATIVE relocations do live. |
| 313 | static Elf_Sym RelaIpltStart; |
| 314 | static Elf_Sym RelaIpltEnd; |
| 315 | }; |
| 316 | |
| 317 | template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::IgnoreUndef; |
| 318 | template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::End; |
| 319 | template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::MipsGp; |
| 320 | template <class ELFT> |
| 321 | typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::RelaIpltStart; |
| 322 | template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::RelaIpltEnd; |
| 323 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 324 | } // namespace elf2 |
| 325 | } // namespace lld |
| 326 | |
| 327 | #endif |