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 { |
| 49 | public: |
| 50 | enum Kind { |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 51 | DefinedFirst, |
| 52 | DefinedRegularKind = DefinedFirst, |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 53 | SharedKind, |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 54 | DefinedCommonKind, |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 55 | DefinedBitcodeKind, |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 56 | DefinedSyntheticKind, |
| 57 | DefinedLast = DefinedSyntheticKind, |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 58 | UndefinedElfKind, |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 59 | UndefinedKind, |
| 60 | LazyKind |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 63 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 64 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 65 | bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 66 | bool isUndefined() const { |
| 67 | return SymbolKind == UndefinedKind || SymbolKind == UndefinedElfKind; |
| 68 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 69 | bool isDefined() const { return SymbolKind <= DefinedLast; } |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 70 | bool isCommon() const { return SymbolKind == DefinedCommonKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 71 | bool isLazy() const { return SymbolKind == LazyKind; } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 72 | bool isShared() const { return SymbolKind == SharedKind; } |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 73 | bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 74 | bool isUsedInRegularObj() const { return IsUsedInRegularObj; } |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 75 | bool isPreemptible() const; |
Rui Ueyama | 7ede543 | 2016-03-13 04:40:14 +0000 | [diff] [blame] | 76 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 77 | // Returns the symbol name. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 78 | StringRef getName() const { |
| 79 | assert(!isLocal()); |
| 80 | return Name; |
| 81 | } |
| 82 | uint32_t getNameOffset() const { |
| 83 | assert(isLocal()); |
| 84 | return NameOffset; |
| 85 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 86 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 87 | uint8_t getVisibility() const { return Other & 0x3; } |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 88 | |
Rui Ueyama | 572a6f7 | 2016-01-29 01:49:33 +0000 | [diff] [blame] | 89 | unsigned DynsymIndex = 0; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 90 | uint32_t GlobalDynIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 91 | uint32_t GotIndex = -1; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 92 | uint32_t GotPltIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 93 | uint32_t PltIndex = -1; |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 94 | uint32_t ThunkIndex = -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; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 97 | bool isInPlt() const { return PltIndex != -1U; } |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 98 | bool hasThunk() const { return ThunkIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 99 | |
Davide Italiano | 04d6aa2 | 2016-03-29 00:15:52 +0000 | [diff] [blame] | 100 | void setUsedInRegularObj() { IsUsedInRegularObj = true; } |
| 101 | |
Rui Ueyama | 71a8686 | 2016-03-14 19:37:58 +0000 | [diff] [blame] | 102 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 103 | typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const; |
| 104 | |
| 105 | template <class ELFT> typename ELFT::uint getGotVA() const; |
| 106 | template <class ELFT> typename ELFT::uint getGotPltVA() const; |
| 107 | template <class ELFT> typename ELFT::uint getPltVA() const; |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 108 | template <class ELFT> typename ELFT::uint getThunkVA() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 109 | template <class ELFT> typename ELFT::uint 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; } |
Sean Silva | e32368f | 2016-03-22 21:04:03 +0000 | [diff] [blame] | 119 | Symbol *getSymbol() const { 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: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 127 | SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t Other, |
| 128 | uint8_t Type) |
| 129 | : SymbolKind(K), MustBeInDynSym(false), NeedsCopyOrPltAddr(false), |
| 130 | Type(Type), Binding(Binding), Other(Other), Name(Name) { |
| 131 | assert(!isLocal()); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 132 | IsUsedInRegularObj = |
| 133 | K != SharedKind && K != LazyKind && K != DefinedBitcodeKind; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 134 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 135 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 136 | SymbolBody(Kind K, uint32_t NameOffset, uint8_t Other, uint8_t Type); |
| 137 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 138 | const unsigned SymbolKind : 8; |
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 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 154 | uint8_t Type; |
| 155 | uint8_t Binding; |
| 156 | uint8_t Other; |
| 157 | bool isSection() const { return Type == llvm::ELF::STT_SECTION; } |
| 158 | bool isTls() const { return Type == llvm::ELF::STT_TLS; } |
| 159 | bool isFunc() const { return Type == llvm::ELF::STT_FUNC; } |
| 160 | bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; } |
| 161 | bool isObject() const { return Type == llvm::ELF::STT_OBJECT; } |
| 162 | bool isFile() const { return Type == llvm::ELF::STT_FILE; } |
| 163 | void setVisibility(uint8_t V) { Other = (Other & ~0x3) | V; } |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 164 | |
George Rimar | 2f0fab5 | 2016-03-06 06:26:18 +0000 | [diff] [blame] | 165 | protected: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 166 | union { |
| 167 | StringRef Name; |
| 168 | uint32_t NameOffset; |
| 169 | }; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 170 | Symbol *Backref = nullptr; |
| 171 | }; |
| 172 | |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 173 | // The base class for any defined symbols. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 174 | class Defined : public SymbolBody { |
| 175 | public: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 176 | Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t Other, uint8_t Type); |
| 177 | Defined(Kind K, uint32_t NameOffset, uint8_t Other, uint8_t Type); |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 178 | static bool classof(const SymbolBody *S) { return S->isDefined(); } |
| 179 | }; |
| 180 | |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 181 | // The defined symbol in LLVM bitcode files. |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 182 | class DefinedBitcode : public Defined { |
| 183 | public: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 184 | DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Other); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 185 | static bool classof(const SymbolBody *S); |
| 186 | }; |
| 187 | |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 188 | class DefinedCommon : public Defined { |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 189 | public: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 190 | DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t Binding, |
| 191 | uint8_t Other, uint8_t Type); |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 192 | |
| 193 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 194 | return S->kind() == SymbolBody::DefinedCommonKind; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 195 | } |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 196 | |
| 197 | // The output offset of this common symbol in the output bss. Computed by the |
| 198 | // writer. |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 199 | uint64_t OffsetInBss; |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 200 | |
| 201 | // The maximum alignment we have seen for this symbol. |
Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 202 | uint64_t Alignment; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 203 | |
| 204 | uint64_t Size; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 207 | // Regular defined symbols read from object file symbol tables. |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 208 | template <class ELFT> class DefinedRegular : public Defined { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 209 | typedef typename ELFT::Sym Elf_Sym; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 210 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 211 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 212 | public: |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 213 | DefinedRegular(StringRef Name, const Elf_Sym &Sym, |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 214 | InputSectionBase<ELFT> *Section) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 215 | : Defined(SymbolBody::DefinedRegularKind, Name, Sym.getBinding(), |
| 216 | Sym.st_other, Sym.getType()), |
| 217 | Value(Sym.st_value), Size(Sym.st_size), |
| 218 | Section(Section ? Section->Repl : NullInputSection) {} |
| 219 | |
| 220 | DefinedRegular(uint32_t NameOffset, const Elf_Sym &Sym, |
| 221 | InputSectionBase<ELFT> *Section) |
| 222 | : Defined(SymbolBody::DefinedRegularKind, NameOffset, Sym.st_other, |
| 223 | Sym.getType()), |
| 224 | Value(Sym.st_value), Size(Sym.st_size), |
| 225 | Section(Section ? Section->Repl : NullInputSection) { |
| 226 | assert(isLocal()); |
| 227 | } |
| 228 | |
| 229 | DefinedRegular(StringRef Name, uint8_t Binding, uint8_t Other) |
| 230 | : Defined(SymbolBody::DefinedRegularKind, Name, Binding, Other, |
| 231 | llvm::ELF::STT_NOTYPE), |
| 232 | Value(0), Size(0), Section(NullInputSection) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 233 | |
| 234 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 235 | return S->kind() == SymbolBody::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 236 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 237 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 238 | uintX_t Value; |
| 239 | uintX_t Size; |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 240 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 241 | // The input section this symbol belongs to. Notice that this is |
| 242 | // a reference to a pointer. We are using two levels of indirections |
| 243 | // because of ICF. If ICF decides two sections need to be merged, it |
| 244 | // manipulates this Section pointers so that they point to the same |
| 245 | // section. This is a bit tricky, so be careful to not be confused. |
| 246 | // If this is null, the symbol is an absolute symbol. |
| 247 | InputSectionBase<ELFT> *&Section; |
| 248 | |
| 249 | private: |
| 250 | static InputSectionBase<ELFT> *NullInputSection; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 253 | template <class ELFT> |
| 254 | InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection; |
| 255 | |
Rui Ueyama | 8b75b9a | 2015-12-17 00:48:16 +0000 | [diff] [blame] | 256 | // DefinedSynthetic is a class to represent linker-generated ELF symbols. |
| 257 | // The difference from the regular symbol is that DefinedSynthetic symbols |
| 258 | // don't belong to any input files or sections. Thus, its constructor |
| 259 | // takes an output section to calculate output VA, etc. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 260 | template <class ELFT> class DefinedSynthetic : public Defined { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 261 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 262 | typedef typename ELFT::uint uintX_t; |
George Rimar | aa4dc20 | 2016-03-01 16:23:13 +0000 | [diff] [blame] | 263 | DefinedSynthetic(StringRef N, uintX_t Value, OutputSectionBase<ELFT> &Section, |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 264 | uint8_t Other); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 265 | |
| 266 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 267 | return S->kind() == SymbolBody::DefinedSyntheticKind; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 270 | // Special value designates that the symbol 'points' |
| 271 | // to the end of the section. |
| 272 | static const uintX_t SectionEnd = uintX_t(-1); |
| 273 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 274 | uintX_t Value; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 275 | const OutputSectionBase<ELFT> &Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 278 | // Undefined symbol. |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 279 | class Undefined : public SymbolBody { |
| 280 | typedef SymbolBody::Kind Kind; |
| 281 | bool CanKeepUndefined; |
| 282 | |
| 283 | protected: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 284 | Undefined(Kind K, StringRef N, uint8_t Binding, uint8_t Other, uint8_t Type); |
| 285 | Undefined(Kind K, uint32_t NameOffset, uint8_t Other, uint8_t Type); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 286 | |
| 287 | public: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 288 | Undefined(StringRef N, bool IsWeak, uint8_t Other, bool CanKeepUndefined); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 289 | |
| 290 | static bool classof(const SymbolBody *S) { return S->isUndefined(); } |
| 291 | |
| 292 | bool canKeepUndefined() const { return CanKeepUndefined; } |
| 293 | }; |
| 294 | |
| 295 | template <class ELFT> class UndefinedElf : public Undefined { |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 296 | typedef typename ELFT::uint uintX_t; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 297 | typedef typename ELFT::Sym Elf_Sym; |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 298 | |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 299 | public: |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 300 | UndefinedElf(StringRef N, const Elf_Sym &Sym); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 301 | UndefinedElf(uint32_t NameOffset, const Elf_Sym &Sym); |
| 302 | |
| 303 | uintX_t Size; |
Rafael Espindola | 5f2c46d | 2015-12-23 01:06:39 +0000 | [diff] [blame] | 304 | |
| 305 | static bool classof(const SymbolBody *S) { |
| 306 | return S->kind() == SymbolBody::UndefinedElfKind; |
| 307 | } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 310 | template <class ELFT> class SharedSymbol : public Defined { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 311 | typedef typename ELFT::Sym Elf_Sym; |
| 312 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 313 | |
| 314 | public: |
| 315 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 316 | return S->kind() == SymbolBody::SharedKind; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 319 | SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 320 | : Defined(SymbolBody::SharedKind, Name, Sym.getBinding(), Sym.st_other, |
| 321 | Sym.getType()), |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 322 | File(F), Sym(Sym) {} |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 323 | |
| 324 | SharedFile<ELFT> *File; |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 325 | const Elf_Sym &Sym; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 326 | |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 327 | // OffsetInBss is significant only when needsCopy() is true. |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 328 | uintX_t OffsetInBss = 0; |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 329 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 330 | bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 333 | // This class represents a symbol defined in an archive file. It is |
| 334 | // created from an archive file header, and it knows how to load an |
| 335 | // object file from an archive to replace itself with a defined |
| 336 | // symbol. If the resolver finds both Undefined and Lazy for |
| 337 | // the same name, it will ask the Lazy to load a file. |
| 338 | class Lazy : public SymbolBody { |
| 339 | public: |
| 340 | Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 341 | : SymbolBody(LazyKind, S.getName(), llvm::ELF::STB_GLOBAL, |
| 342 | llvm::ELF::STV_DEFAULT, /* Type */ 0), |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 343 | File(F), Sym(S) {} |
| 344 | |
| 345 | static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; } |
| 346 | |
| 347 | // Returns an object file for this symbol, or a nullptr if the file |
| 348 | // was already returned. |
| 349 | std::unique_ptr<InputFile> getMember(); |
| 350 | |
| 351 | private: |
| 352 | ArchiveFile *File; |
| 353 | const llvm::object::Archive::Symbol Sym; |
| 354 | }; |
| 355 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 356 | // Some linker-generated symbols need to be created as |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 357 | // DefinedRegular symbols. |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 358 | template <class ELFT> struct ElfSym { |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 359 | // The content for _etext and etext symbols. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 360 | static DefinedRegular<ELFT> *Etext; |
| 361 | static DefinedRegular<ELFT> *Etext2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 362 | |
| 363 | // The content for _edata and edata symbols. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 364 | static DefinedRegular<ELFT> *Edata; |
| 365 | static DefinedRegular<ELFT> *Edata2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 366 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 367 | // The content for _end and end symbols. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 368 | static DefinedRegular<ELFT> *End; |
| 369 | static DefinedRegular<ELFT> *End2; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 370 | |
| 371 | // The content for _gp symbol for MIPS target. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 372 | static DefinedRegular<ELFT> *MipsGp; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 373 | |
| 374 | // __rel_iplt_start/__rel_iplt_end for signaling |
| 375 | // where R_[*]_IRELATIVE relocations do live. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 376 | static DefinedRegular<ELFT> *RelaIpltStart; |
| 377 | static DefinedRegular<ELFT> *RelaIpltEnd; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 380 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext; |
| 381 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2; |
| 382 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata; |
| 383 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2; |
| 384 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End; |
| 385 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2; |
| 386 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGp; |
| 387 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::RelaIpltStart; |
| 388 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::RelaIpltEnd; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 389 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 390 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 391 | } // namespace lld |
| 392 | |
| 393 | #endif |