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