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