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. |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame^] | 43 | // Symbol also holds computed properties of symbol names. |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 44 | struct Symbol { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 45 | SymbolBody *Body; |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame^] | 46 | |
| 47 | // Symbol visibility. This is the computed minimum visibility of all |
| 48 | // observed non-DSO symbols. |
| 49 | unsigned Visibility : 2; |
| 50 | |
| 51 | // True if the symbol was used for linking and thus need to be added to the |
| 52 | // output file's symbol table. This is true for all symbols except for |
| 53 | // unreferenced DSO symbols and bitcode symbols that are unreferenced except |
| 54 | // by other bitcode objects. |
| 55 | unsigned IsUsedInRegularObj : 1; |
| 56 | |
| 57 | // If this flag is true and the symbol has protected or default visibility, it |
| 58 | // will appear in .dynsym. This flag is set by interposable DSO symbols in |
| 59 | // executables, by most symbols in DSOs and executables built with |
| 60 | // --export-dynamic, and by dynamic lists. |
| 61 | unsigned ExportDynamic : 1; |
| 62 | |
| 63 | bool includeInDynsym() const; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // The base class for real symbol classes. |
| 67 | class SymbolBody { |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 68 | void init(); |
| 69 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 70 | public: |
| 71 | enum Kind { |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 72 | DefinedFirst, |
| 73 | DefinedRegularKind = DefinedFirst, |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 74 | SharedKind, |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 75 | DefinedCommonKind, |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 76 | DefinedBitcodeKind, |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 77 | DefinedSyntheticKind, |
| 78 | DefinedLast = DefinedSyntheticKind, |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 79 | UndefinedElfKind, |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 80 | UndefinedBitcodeKind, |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 81 | LazyArchiveKind, |
| 82 | LazyObjectKind, |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 85 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
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 | bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 88 | bool isUndefined() const { |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 89 | return SymbolKind == UndefinedBitcodeKind || SymbolKind == UndefinedElfKind; |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 90 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 91 | bool isDefined() const { return SymbolKind <= DefinedLast; } |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 92 | bool isCommon() const { return SymbolKind == DefinedCommonKind; } |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 93 | bool isLazy() const { |
| 94 | return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind; |
| 95 | } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 96 | bool isShared() const { return SymbolKind == SharedKind; } |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 97 | bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; } |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 98 | bool isPreemptible() const; |
Rui Ueyama | 7ede543 | 2016-03-13 04:40:14 +0000 | [diff] [blame] | 99 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 100 | // Returns the symbol name. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 101 | StringRef getName() const { |
| 102 | assert(!isLocal()); |
Rafael Espindola | 193b99c | 2016-04-04 14:31:20 +0000 | [diff] [blame] | 103 | return StringRef(Name.S, Name.Len); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 104 | } |
| 105 | uint32_t getNameOffset() const { |
| 106 | assert(isLocal()); |
| 107 | return NameOffset; |
| 108 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 109 | |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 110 | uint8_t getVisibility() const { return StOther & 0x3; } |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 111 | |
Rui Ueyama | 572a6f7 | 2016-01-29 01:49:33 +0000 | [diff] [blame] | 112 | unsigned DynsymIndex = 0; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 113 | uint32_t GlobalDynIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 114 | uint32_t GotIndex = -1; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 115 | uint32_t GotPltIndex = -1; |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 116 | uint32_t PltIndex = -1; |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 117 | uint32_t ThunkIndex = -1; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 118 | bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); } |
Rafael Espindola | 5c2310c | 2015-09-18 14:40:19 +0000 | [diff] [blame] | 119 | bool isInGot() const { return GotIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 120 | bool isInPlt() const { return PltIndex != -1U; } |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 121 | bool hasThunk() const { return ThunkIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 122 | |
Rui Ueyama | 71a8686 | 2016-03-14 19:37:58 +0000 | [diff] [blame] | 123 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 124 | typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const; |
| 125 | |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 126 | template <class ELFT> typename ELFT::uint getGotOffset() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 127 | template <class ELFT> typename ELFT::uint getGotVA() const; |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 128 | template <class ELFT> typename ELFT::uint getGotPltOffset() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 129 | template <class ELFT> typename ELFT::uint getGotPltVA() const; |
| 130 | template <class ELFT> typename ELFT::uint getPltVA() const; |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 131 | template <class ELFT> typename ELFT::uint getThunkVA() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 132 | template <class ELFT> typename ELFT::uint getSize() const; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 133 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 134 | // A SymbolBody has a backreference to a Symbol. Originally they are |
| 135 | // doubly-linked. A backreference will never change. But the pointer |
| 136 | // in the Symbol may be mutated by the resolver. If you have a |
| 137 | // pointer P to a SymbolBody and are not sure whether the resolver |
| 138 | // has chosen the object among other objects having the same name, |
| 139 | // you can access P->Backref->Body to get the resolver's result. |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 140 | SymbolBody &repl() { return Backref ? *Backref->Body : *this; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 141 | |
| 142 | // Decides which symbol should "win" in the symbol table, this or |
| 143 | // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if |
| 144 | // they are duplicate (conflicting) symbols. |
Peter Collingbourne | d0856a6 | 2016-04-05 00:47:58 +0000 | [diff] [blame] | 145 | int compare(SymbolBody *Other); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 146 | |
| 147 | protected: |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 148 | SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 149 | uint8_t Type); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 150 | |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 151 | SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 152 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 153 | const unsigned SymbolKind : 8; |
Rui Ueyama | 7d332f5 | 2015-12-25 06:55:39 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | abebed9 | 2016-02-05 15:27:15 +0000 | [diff] [blame] | 155 | public: |
Rafael Espindola | 4d480ed | 2016-04-21 21:44:25 +0000 | [diff] [blame] | 156 | // True if this symbol can be omitted from the symbol table if nothing else |
| 157 | // requires it to be there. Right now this is only used for linkonce_odr in |
| 158 | // LTO, but we could add the feature to ELF. It would be similar to |
| 159 | // MachO's .weak_def_can_be_hidden. |
| 160 | unsigned CanOmitFromDynSym : 1; |
| 161 | |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 162 | // True if the linker has to generate a copy relocation for this shared |
| 163 | // symbol or if the symbol should point to its plt entry. |
| 164 | unsigned NeedsCopyOrPltAddr : 1; |
| 165 | |
Rafael Espindola | 5e34568 | 2016-04-06 14:31:03 +0000 | [diff] [blame] | 166 | unsigned CanKeepUndefined : 1; |
| 167 | |
Rui Ueyama | dd41807 | 2016-04-04 18:15:38 +0000 | [diff] [blame] | 168 | // The following fields have the same meaning as the ELF symbol attributes. |
| 169 | uint8_t Type; // symbol type |
| 170 | uint8_t Binding; // symbol binding |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 171 | uint8_t StOther; // st_other field value |
Rui Ueyama | dd41807 | 2016-04-04 18:15:38 +0000 | [diff] [blame] | 172 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 173 | bool isSection() const { return Type == llvm::ELF::STT_SECTION; } |
| 174 | bool isTls() const { return Type == llvm::ELF::STT_TLS; } |
| 175 | bool isFunc() const { return Type == llvm::ELF::STT_FUNC; } |
| 176 | bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; } |
| 177 | bool isObject() const { return Type == llvm::ELF::STT_OBJECT; } |
| 178 | bool isFile() const { return Type == llvm::ELF::STT_FILE; } |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame^] | 179 | |
| 180 | Symbol *Backref = nullptr; |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 181 | |
George Rimar | 2f0fab5 | 2016-03-06 06:26:18 +0000 | [diff] [blame] | 182 | protected: |
Rafael Espindola | 193b99c | 2016-04-04 14:31:20 +0000 | [diff] [blame] | 183 | struct Str { |
| 184 | const char *S; |
| 185 | size_t Len; |
| 186 | }; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 187 | union { |
Rafael Espindola | 193b99c | 2016-04-04 14:31:20 +0000 | [diff] [blame] | 188 | Str Name; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 189 | uint32_t NameOffset; |
| 190 | }; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 193 | // The base class for any defined symbols. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 194 | class Defined : public SymbolBody { |
| 195 | public: |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 196 | Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, |
| 197 | uint8_t Type); |
| 198 | Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type); |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 199 | static bool classof(const SymbolBody *S) { return S->isDefined(); } |
| 200 | }; |
| 201 | |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 202 | // The defined symbol in LLVM bitcode files. |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 203 | class DefinedBitcode : public Defined { |
| 204 | public: |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 205 | DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 206 | static bool classof(const SymbolBody *S); |
| 207 | }; |
| 208 | |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 209 | class DefinedCommon : public Defined { |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 210 | public: |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 211 | DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t Binding, |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 212 | uint8_t StOther, uint8_t Type); |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 213 | |
| 214 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 215 | return S->kind() == SymbolBody::DefinedCommonKind; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 216 | } |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 217 | |
| 218 | // The output offset of this common symbol in the output bss. Computed by the |
| 219 | // writer. |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 220 | uint64_t OffsetInBss; |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 221 | |
| 222 | // The maximum alignment we have seen for this symbol. |
Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 223 | uint64_t Alignment; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 224 | |
| 225 | uint64_t Size; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 228 | // Regular defined symbols read from object file symbol tables. |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 229 | template <class ELFT> class DefinedRegular : public Defined { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 230 | typedef typename ELFT::Sym Elf_Sym; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 231 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 232 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 233 | public: |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 234 | DefinedRegular(StringRef Name, const Elf_Sym &Sym, |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 235 | InputSectionBase<ELFT> *Section) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 236 | : Defined(SymbolBody::DefinedRegularKind, Name, Sym.getBinding(), |
| 237 | Sym.st_other, Sym.getType()), |
| 238 | Value(Sym.st_value), Size(Sym.st_size), |
| 239 | Section(Section ? Section->Repl : NullInputSection) {} |
| 240 | |
Rafael Espindola | d9a1717 | 2016-04-05 11:47:46 +0000 | [diff] [blame] | 241 | DefinedRegular(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section) |
| 242 | : Defined(SymbolBody::DefinedRegularKind, Sym.st_name, Sym.st_other, |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 243 | Sym.getType()), |
| 244 | Value(Sym.st_value), Size(Sym.st_size), |
| 245 | Section(Section ? Section->Repl : NullInputSection) { |
| 246 | assert(isLocal()); |
| 247 | } |
| 248 | |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 249 | DefinedRegular(StringRef Name, uint8_t Binding, uint8_t StOther) |
| 250 | : Defined(SymbolBody::DefinedRegularKind, Name, Binding, StOther, |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 251 | llvm::ELF::STT_NOTYPE), |
| 252 | Value(0), Size(0), Section(NullInputSection) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 253 | |
| 254 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 255 | return S->kind() == SymbolBody::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 256 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 257 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 258 | uintX_t Value; |
| 259 | uintX_t Size; |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 260 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 261 | // The input section this symbol belongs to. Notice that this is |
| 262 | // a reference to a pointer. We are using two levels of indirections |
| 263 | // because of ICF. If ICF decides two sections need to be merged, it |
| 264 | // manipulates this Section pointers so that they point to the same |
| 265 | // section. This is a bit tricky, so be careful to not be confused. |
| 266 | // If this is null, the symbol is an absolute symbol. |
| 267 | InputSectionBase<ELFT> *&Section; |
| 268 | |
| 269 | private: |
| 270 | static InputSectionBase<ELFT> *NullInputSection; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 273 | template <class ELFT> |
| 274 | InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection; |
| 275 | |
Rui Ueyama | 8b75b9a | 2015-12-17 00:48:16 +0000 | [diff] [blame] | 276 | // DefinedSynthetic is a class to represent linker-generated ELF symbols. |
| 277 | // The difference from the regular symbol is that DefinedSynthetic symbols |
| 278 | // don't belong to any input files or sections. Thus, its constructor |
| 279 | // takes an output section to calculate output VA, etc. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 280 | template <class ELFT> class DefinedSynthetic : public Defined { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 281 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 282 | typedef typename ELFT::uint uintX_t; |
Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 283 | DefinedSynthetic(StringRef N, uintX_t Value, |
| 284 | OutputSectionBase<ELFT> &Section); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 285 | |
| 286 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 287 | return S->kind() == SymbolBody::DefinedSyntheticKind; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 290 | // Special value designates that the symbol 'points' |
| 291 | // to the end of the section. |
| 292 | static const uintX_t SectionEnd = uintX_t(-1); |
| 293 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 294 | uintX_t Value; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 295 | const OutputSectionBase<ELFT> &Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 298 | class UndefinedBitcode : public SymbolBody { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 299 | public: |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 300 | UndefinedBitcode(StringRef N, bool IsWeak, uint8_t StOther); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 301 | |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 302 | static bool classof(const SymbolBody *S) { |
| 303 | return S->kind() == UndefinedBitcodeKind; |
| 304 | } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 307 | template <class ELFT> class UndefinedElf : public SymbolBody { |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 308 | typedef typename ELFT::uint uintX_t; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 309 | typedef typename ELFT::Sym Elf_Sym; |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 310 | |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 311 | public: |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 312 | UndefinedElf(StringRef N, const Elf_Sym &Sym); |
Rafael Espindola | d9a1717 | 2016-04-05 11:47:46 +0000 | [diff] [blame] | 313 | UndefinedElf(const Elf_Sym &Sym); |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 314 | UndefinedElf(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type, |
| 315 | bool CanKeepUndefined); |
| 316 | |
| 317 | bool canKeepUndefined() const { return CanKeepUndefined; } |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 318 | |
| 319 | uintX_t Size; |
Rafael Espindola | 5f2c46d | 2015-12-23 01:06:39 +0000 | [diff] [blame] | 320 | |
| 321 | static bool classof(const SymbolBody *S) { |
| 322 | return S->kind() == SymbolBody::UndefinedElfKind; |
| 323 | } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 326 | template <class ELFT> class SharedSymbol : public Defined { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 327 | typedef typename ELFT::Sym Elf_Sym; |
| 328 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 329 | |
| 330 | public: |
| 331 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 332 | return S->kind() == SymbolBody::SharedKind; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 335 | SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 336 | : Defined(SymbolBody::SharedKind, Name, Sym.getBinding(), Sym.st_other, |
| 337 | Sym.getType()), |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 338 | File(F), Sym(Sym) {} |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 339 | |
| 340 | SharedFile<ELFT> *File; |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 341 | const Elf_Sym &Sym; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 342 | |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 343 | // OffsetInBss is significant only when needsCopy() is true. |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 344 | uintX_t OffsetInBss = 0; |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 345 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 346 | bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 349 | // This class represents a symbol defined in an archive file. It is |
| 350 | // created from an archive file header, and it knows how to load an |
| 351 | // object file from an archive to replace itself with a defined |
| 352 | // symbol. If the resolver finds both Undefined and Lazy for |
| 353 | // the same name, it will ask the Lazy to load a file. |
| 354 | class Lazy : public SymbolBody { |
| 355 | public: |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 356 | Lazy(SymbolBody::Kind K, StringRef Name) |
| 357 | : SymbolBody(K, Name, llvm::ELF::STB_GLOBAL, llvm::ELF::STV_DEFAULT, |
| 358 | /* Type */ 0) {} |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 359 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 360 | static bool classof(const SymbolBody *S) { return S->isLazy(); } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 361 | |
| 362 | // Returns an object file for this symbol, or a nullptr if the file |
| 363 | // was already returned. |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 364 | std::unique_ptr<InputFile> getFile(); |
| 365 | }; |
| 366 | |
| 367 | // LazyArchive symbols represents symbols in archive files. |
| 368 | class LazyArchive : public Lazy { |
| 369 | public: |
| 370 | LazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S) |
| 371 | : Lazy(LazyArchiveKind, S.getName()), File(F), Sym(S) {} |
| 372 | |
| 373 | static bool classof(const SymbolBody *S) { |
| 374 | return S->kind() == LazyArchiveKind; |
| 375 | } |
| 376 | |
| 377 | std::unique_ptr<InputFile> getFile(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 378 | |
| 379 | private: |
| 380 | ArchiveFile *File; |
| 381 | const llvm::object::Archive::Symbol Sym; |
| 382 | }; |
| 383 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 384 | // LazyObject symbols represents symbols in object files between |
| 385 | // --start-lib and --end-lib options. |
| 386 | class LazyObject : public Lazy { |
| 387 | public: |
| 388 | LazyObject(StringRef Name, MemoryBufferRef M) |
| 389 | : Lazy(LazyObjectKind, Name), MBRef(M) {} |
| 390 | |
| 391 | static bool classof(const SymbolBody *S) { |
| 392 | return S->kind() == LazyObjectKind; |
| 393 | } |
| 394 | |
| 395 | std::unique_ptr<InputFile> getFile(); |
| 396 | |
| 397 | private: |
| 398 | MemoryBufferRef MBRef; |
| 399 | }; |
| 400 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 401 | // Some linker-generated symbols need to be created as |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 402 | // DefinedRegular symbols. |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 403 | template <class ELFT> struct ElfSym { |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 404 | // The content for _etext and etext symbols. |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 405 | static DefinedRegular<ELFT> *Etext; |
| 406 | static DefinedRegular<ELFT> *Etext2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 407 | |
| 408 | // The content for _edata and edata symbols. |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 409 | static DefinedRegular<ELFT> *Edata; |
| 410 | static DefinedRegular<ELFT> *Edata2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 411 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 412 | // The content for _end and end symbols. |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 413 | static DefinedRegular<ELFT> *End; |
| 414 | static DefinedRegular<ELFT> *End2; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 415 | |
| 416 | // The content for _gp symbol for MIPS target. |
Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 417 | static SymbolBody *MipsGp; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 418 | |
Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 419 | static SymbolBody *MipsLocalGp; |
| 420 | static SymbolBody *MipsGpDisp; |
Rafael Espindola | 8396f72 | 2016-04-11 20:34:27 +0000 | [diff] [blame] | 421 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 422 | // __rel_iplt_start/__rel_iplt_end for signaling |
| 423 | // where R_[*]_IRELATIVE relocations do live. |
Rafael Espindola | 03ef404 | 2016-04-11 19:14:59 +0000 | [diff] [blame] | 424 | static SymbolBody *RelaIpltStart; |
| 425 | static SymbolBody *RelaIpltEnd; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 428 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext; |
| 429 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2; |
| 430 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata; |
| 431 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2; |
| 432 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End; |
| 433 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2; |
Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 434 | template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGp; |
| 435 | template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsLocalGp; |
| 436 | template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGpDisp; |
Rafael Espindola | 03ef404 | 2016-04-11 19:14:59 +0000 | [diff] [blame] | 437 | template <class ELFT> SymbolBody *ElfSym<ELFT>::RelaIpltStart; |
| 438 | template <class ELFT> SymbolBody *ElfSym<ELFT>::RelaIpltEnd; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 439 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 440 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 441 | } // namespace lld |
| 442 | |
| 443 | #endif |