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