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; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 28 | class BitcodeFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | class InputFile; |
Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 30 | class LazyObjectFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | class SymbolBody; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 32 | template <class ELFT> class ObjectFile; |
Michael J. Spencer | 658dccd | 2015-09-18 22:13:25 +0000 | [diff] [blame] | 33 | template <class ELFT> class OutputSection; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 34 | class OutputSectionBase; |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 35 | template <class ELFT> class SharedFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 36 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 37 | struct Symbol; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 38 | |
| 39 | // The base class for real symbol classes. |
| 40 | class SymbolBody { |
| 41 | public: |
| 42 | enum Kind { |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 43 | DefinedFirst, |
| 44 | DefinedRegularKind = DefinedFirst, |
Rafael Espindola | 84aff15 | 2015-09-25 21:20:23 +0000 | [diff] [blame] | 45 | SharedKind, |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 46 | DefinedCommonKind, |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 47 | DefinedSyntheticKind, |
| 48 | DefinedLast = DefinedSyntheticKind, |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 49 | UndefinedKind, |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 50 | LazyArchiveKind, |
| 51 | LazyObjectKind, |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 54 | SymbolBody(Kind K) : SymbolKind(K) {} |
| 55 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 56 | Symbol *symbol(); |
| 57 | const Symbol *symbol() const { |
| 58 | return const_cast<SymbolBody *>(this)->symbol(); |
| 59 | } |
| 60 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 61 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 62 | |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 63 | bool isUndefined() const { return SymbolKind == UndefinedKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 64 | bool isDefined() const { return SymbolKind <= DefinedLast; } |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 65 | bool isCommon() const { return SymbolKind == DefinedCommonKind; } |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 66 | bool isLazy() const { |
| 67 | return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind; |
| 68 | } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 69 | bool isShared() const { return SymbolKind == SharedKind; } |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 70 | bool isLocal() const { return IsLocal; } |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 71 | bool isPreemptible() const; |
Rui Ueyama | 7ede543 | 2016-03-13 04:40:14 +0000 | [diff] [blame] | 72 | |
George Rimar | 4365158 | 2016-06-28 08:21:10 +0000 | [diff] [blame] | 73 | StringRef getName() const; |
Rui Ueyama | 663b8c2 | 2016-07-17 17:23:17 +0000 | [diff] [blame] | 74 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 75 | uint32_t getNameOffset() const { |
| 76 | assert(isLocal()); |
| 77 | return NameOffset; |
| 78 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 79 | |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 80 | uint8_t getVisibility() const { return StOther & 0x3; } |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 81 | |
Rafael Espindola | 5c2310c | 2015-09-18 14:40:19 +0000 | [diff] [blame] | 82 | bool isInGot() const { return GotIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 83 | bool isInPlt() const { return PltIndex != -1U; } |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 84 | template <class ELFT> bool hasThunk() const; |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 85 | |
Rui Ueyama | 71a8686 | 2016-03-14 19:37:58 +0000 | [diff] [blame] | 86 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 87 | typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const; |
| 88 | |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 89 | template <class ELFT> typename ELFT::uint getGotOffset() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 90 | template <class ELFT> typename ELFT::uint getGotVA() const; |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 91 | template <class ELFT> typename ELFT::uint getGotPltOffset() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 92 | template <class ELFT> typename ELFT::uint getGotPltVA() const; |
| 93 | template <class ELFT> typename ELFT::uint getPltVA() const; |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 94 | template <class ELFT> typename ELFT::uint getThunkVA() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 95 | template <class ELFT> typename ELFT::uint getSize() const; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 96 | |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 97 | // The file from which this symbol was created. |
| 98 | InputFile *File = nullptr; |
Rui Ueyama | 6d0cd2b | 2016-05-02 21:30:42 +0000 | [diff] [blame] | 99 | |
Rafael Espindola | 7ae2177 | 2016-10-26 00:58:23 +0000 | [diff] [blame] | 100 | uint32_t DynsymIndex = 0; |
Rafael Espindola | ca65623 | 2016-10-21 20:32:41 +0000 | [diff] [blame] | 101 | uint32_t GotIndex = -1; |
| 102 | uint32_t GotPltIndex = -1; |
| 103 | uint32_t PltIndex = -1; |
| 104 | uint32_t GlobalDynIndex = -1; |
| 105 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 106 | protected: |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 107 | SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 108 | |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 109 | SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 110 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 111 | const unsigned SymbolKind : 8; |
Rui Ueyama | 7d332f5 | 2015-12-25 06:55:39 +0000 | [diff] [blame] | 112 | |
Rafael Espindola | abebed9 | 2016-02-05 15:27:15 +0000 | [diff] [blame] | 113 | public: |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 114 | // True if the linker has to generate a copy relocation for this shared |
| 115 | // symbol or if the symbol should point to its plt entry. |
| 116 | unsigned NeedsCopyOrPltAddr : 1; |
| 117 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 118 | // True if this is a local symbol. |
| 119 | unsigned IsLocal : 1; |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 120 | |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 121 | // True if this symbol has an entry in the global part of MIPS GOT. |
| 122 | unsigned IsInGlobalMipsGot : 1; |
| 123 | |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 124 | // True if this symbol is referenced by 32-bit GOT relocations. |
| 125 | unsigned Is32BitMipsGot : 1; |
| 126 | |
Rui Ueyama | dd41807 | 2016-04-04 18:15:38 +0000 | [diff] [blame] | 127 | // The following fields have the same meaning as the ELF symbol attributes. |
| 128 | uint8_t Type; // symbol type |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 129 | uint8_t StOther; // st_other field value |
Rui Ueyama | dd41807 | 2016-04-04 18:15:38 +0000 | [diff] [blame] | 130 | |
Peter Collingbourne | f3a2b0e | 2016-05-03 18:03:47 +0000 | [diff] [blame] | 131 | // The Type field may also have this value. It means that we have not yet seen |
| 132 | // a non-Lazy symbol with this name, so we don't know what its type is. The |
| 133 | // Type field is normally set to this value for Lazy symbols unless we saw a |
| 134 | // weak undefined symbol first, in which case we need to remember the original |
| 135 | // symbol's type in order to check for TLS mismatches. |
| 136 | enum { UnknownType = 255 }; |
| 137 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 138 | bool isSection() const { return Type == llvm::ELF::STT_SECTION; } |
| 139 | bool isTls() const { return Type == llvm::ELF::STT_TLS; } |
| 140 | bool isFunc() const { return Type == llvm::ELF::STT_FUNC; } |
| 141 | bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; } |
| 142 | bool isObject() const { return Type == llvm::ELF::STT_OBJECT; } |
| 143 | bool isFile() const { return Type == llvm::ELF::STT_FILE; } |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 144 | |
George Rimar | 2f0fab5 | 2016-03-06 06:26:18 +0000 | [diff] [blame] | 145 | protected: |
Rafael Espindola | 193b99c | 2016-04-04 14:31:20 +0000 | [diff] [blame] | 146 | struct Str { |
| 147 | const char *S; |
| 148 | size_t Len; |
| 149 | }; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 150 | union { |
Rafael Espindola | 193b99c | 2016-04-04 14:31:20 +0000 | [diff] [blame] | 151 | Str Name; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 152 | uint32_t NameOffset; |
| 153 | }; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 156 | // The base class for any defined symbols. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 157 | class Defined : public SymbolBody { |
| 158 | public: |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 159 | Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type); |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 160 | Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type); |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 161 | static bool classof(const SymbolBody *S) { return S->isDefined(); } |
| 162 | }; |
| 163 | |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 164 | class DefinedCommon : public Defined { |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 165 | public: |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 166 | DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther, |
Rui Ueyama | 2a7c1c1 | 2016-07-17 17:36:22 +0000 | [diff] [blame] | 167 | uint8_t Type, InputFile *File); |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 168 | |
| 169 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 170 | return S->kind() == SymbolBody::DefinedCommonKind; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 171 | } |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 172 | |
| 173 | // The output offset of this common symbol in the output bss. Computed by the |
| 174 | // writer. |
Eugene Leviant | 3e6b027 | 2016-07-28 19:24:13 +0000 | [diff] [blame] | 175 | uint64_t Offset; |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 176 | |
| 177 | // The maximum alignment we have seen for this symbol. |
Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 178 | uint64_t Alignment; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 179 | |
| 180 | uint64_t Size; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 183 | // Regular defined symbols read from object file symbol tables. |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 184 | template <class ELFT> class DefinedRegular : public Defined { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 185 | typedef typename ELFT::Sym Elf_Sym; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 186 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 187 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 188 | public: |
Rafael Espindola | a8bf71c | 2016-10-26 20:34:59 +0000 | [diff] [blame] | 189 | DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, uintX_t Value, |
| 190 | uintX_t Size, InputSectionBase<ELFT> *Section, InputFile *File) |
| 191 | : Defined(SymbolBody::DefinedRegularKind, Name, StOther, Type), |
| 192 | Value(Value), Size(Size), |
| 193 | Section(Section ? Section->Repl : NullInputSection) { |
| 194 | this->File = File; |
| 195 | } |
| 196 | |
Rafael Espindola | 5ceeb60 | 2016-10-26 20:57:14 +0000 | [diff] [blame] | 197 | DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, uintX_t Value, |
| 198 | uintX_t Size, InputSectionBase<ELFT> *Section) |
| 199 | : DefinedRegular(Name, StOther, Type, Value, Size, Section, |
Rui Ueyama | 4c1cd45 | 2016-10-26 20:44:32 +0000 | [diff] [blame] | 200 | Section ? Section->getFile() : nullptr) {} |
Rafael Espindola | a8bf71c | 2016-10-26 20:34:59 +0000 | [diff] [blame] | 201 | |
Rafael Espindola | a8bf71c | 2016-10-26 20:34:59 +0000 | [diff] [blame] | 202 | DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, BitcodeFile *F) |
| 203 | : DefinedRegular(Name, StOther, Type, 0, 0, NullInputSection, F) {} |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 204 | |
Rafael Espindola | d9a1717 | 2016-04-05 11:47:46 +0000 | [diff] [blame] | 205 | DefinedRegular(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section) |
| 206 | : Defined(SymbolBody::DefinedRegularKind, Sym.st_name, Sym.st_other, |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 207 | Sym.getType()), |
| 208 | Value(Sym.st_value), Size(Sym.st_size), |
| 209 | Section(Section ? Section->Repl : NullInputSection) { |
| 210 | assert(isLocal()); |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 211 | if (Section) |
| 212 | this->File = Section->getFile(); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 215 | // Return true if the symbol is a PIC function. |
| 216 | bool isMipsPIC() const; |
| 217 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 218 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 219 | return S->kind() == SymbolBody::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 220 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 221 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 222 | uintX_t Value; |
| 223 | uintX_t Size; |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 224 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 225 | // The input section this symbol belongs to. Notice that this is |
| 226 | // a reference to a pointer. We are using two levels of indirections |
| 227 | // because of ICF. If ICF decides two sections need to be merged, it |
| 228 | // manipulates this Section pointers so that they point to the same |
| 229 | // section. This is a bit tricky, so be careful to not be confused. |
| 230 | // If this is null, the symbol is an absolute symbol. |
| 231 | InputSectionBase<ELFT> *&Section; |
| 232 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 233 | // If non-null the symbol has a Thunk that may be used as an alternative |
| 234 | // destination for callers of this Symbol. |
Rafael Espindola | 6091492 | 2016-07-08 19:28:55 +0000 | [diff] [blame] | 235 | Thunk<ELFT> *ThunkData = nullptr; |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 236 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 237 | private: |
| 238 | static InputSectionBase<ELFT> *NullInputSection; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 241 | template <class ELFT> |
| 242 | InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection; |
| 243 | |
Rui Ueyama | 8b75b9a | 2015-12-17 00:48:16 +0000 | [diff] [blame] | 244 | // DefinedSynthetic is a class to represent linker-generated ELF symbols. |
| 245 | // The difference from the regular symbol is that DefinedSynthetic symbols |
| 246 | // don't belong to any input files or sections. Thus, its constructor |
| 247 | // takes an output section to calculate output VA, etc. |
Peter Collingbourne | 6a42259 | 2016-05-03 01:21:08 +0000 | [diff] [blame] | 248 | // If Section is null, this symbol is relative to the image base. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 249 | template <class ELFT> class DefinedSynthetic : public Defined { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 250 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 251 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 252 | DefinedSynthetic(StringRef N, uintX_t Value, OutputSectionBase *Section); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +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::DefinedSyntheticKind; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 258 | // Special value designates that the symbol 'points' |
| 259 | // to the end of the section. |
| 260 | static const uintX_t SectionEnd = uintX_t(-1); |
| 261 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 262 | uintX_t Value; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 263 | const OutputSectionBase *Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 266 | class Undefined : public SymbolBody { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 267 | public: |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 268 | Undefined(StringRef Name, uint8_t StOther, uint8_t Type, InputFile *F); |
| 269 | Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type, InputFile *F); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 270 | |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 271 | static bool classof(const SymbolBody *S) { |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 272 | return S->kind() == UndefinedKind; |
Rafael Espindola | 5f2c46d | 2015-12-23 01:06:39 +0000 | [diff] [blame] | 273 | } |
Rui Ueyama | 6d0cd2b | 2016-05-02 21:30:42 +0000 | [diff] [blame] | 274 | |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 275 | InputFile *file() { return this->File; } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 278 | template <class ELFT> class SharedSymbol : public Defined { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 279 | typedef typename ELFT::Sym Elf_Sym; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 280 | typedef typename ELFT::Verdef Elf_Verdef; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 281 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 282 | |
| 283 | public: |
| 284 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 285 | return S->kind() == SymbolBody::SharedKind; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 288 | SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym, |
| 289 | const Elf_Verdef *Verdef) |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 290 | : Defined(SymbolBody::SharedKind, Name, Sym.st_other, Sym.getType()), |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 291 | Sym(Sym), Verdef(Verdef) { |
Peter Collingbourne | 5dd550a | 2016-04-26 16:22:51 +0000 | [diff] [blame] | 292 | // IFuncs defined in DSOs are treated as functions by the static linker. |
| 293 | if (isGnuIFunc()) |
| 294 | Type = llvm::ELF::STT_FUNC; |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 295 | this->File = F; |
Peter Collingbourne | 5dd550a | 2016-04-26 16:22:51 +0000 | [diff] [blame] | 296 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 297 | |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 298 | SharedFile<ELFT> *file() { return (SharedFile<ELFT> *)this->File; } |
| 299 | |
Rui Ueyama | bfc1d9d | 2016-04-02 18:06:18 +0000 | [diff] [blame] | 300 | const Elf_Sym &Sym; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 301 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 302 | // This field is a pointer to the symbol's version definition. |
| 303 | const Elf_Verdef *Verdef; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 304 | |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 305 | // OffsetInBss is significant only when needsCopy() is true. |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 306 | uintX_t OffsetInBss = 0; |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 307 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 308 | // If non-null the symbol has a Thunk that may be used as an alternative |
| 309 | // destination for callers of this Symbol. |
Rui Ueyama | 8c8db47 | 2016-07-08 19:59:11 +0000 | [diff] [blame] | 310 | Thunk<ELFT> *ThunkData = nullptr; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 311 | bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 314 | // This class represents a symbol defined in an archive file. It is |
| 315 | // created from an archive file header, and it knows how to load an |
| 316 | // object file from an archive to replace itself with a defined |
| 317 | // symbol. If the resolver finds both Undefined and Lazy for |
| 318 | // the same name, it will ask the Lazy to load a file. |
| 319 | class Lazy : public SymbolBody { |
| 320 | public: |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 321 | static bool classof(const SymbolBody *S) { return S->isLazy(); } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 322 | |
| 323 | // Returns an object file for this symbol, or a nullptr if the file |
| 324 | // was already returned. |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 325 | InputFile *fetch(); |
Rui Ueyama | b06700f | 2016-07-17 17:44:41 +0000 | [diff] [blame] | 326 | |
| 327 | protected: |
| 328 | Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type) |
| 329 | : SymbolBody(K, Name, llvm::ELF::STV_DEFAULT, Type) {} |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | // LazyArchive symbols represents symbols in archive files. |
| 333 | class LazyArchive : public Lazy { |
| 334 | public: |
Rafael Espindola | 07543a8 | 2016-06-14 21:40:23 +0000 | [diff] [blame] | 335 | LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S, |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 336 | uint8_t Type); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 337 | |
| 338 | static bool classof(const SymbolBody *S) { |
| 339 | return S->kind() == LazyArchiveKind; |
| 340 | } |
| 341 | |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 342 | ArchiveFile *file() { return (ArchiveFile *)this->File; } |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 343 | InputFile *fetch(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 344 | |
| 345 | private: |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 346 | const llvm::object::Archive::Symbol Sym; |
| 347 | }; |
| 348 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 349 | // LazyObject symbols represents symbols in object files between |
| 350 | // --start-lib and --end-lib options. |
| 351 | class LazyObject : public Lazy { |
| 352 | public: |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 353 | LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 354 | |
| 355 | static bool classof(const SymbolBody *S) { |
| 356 | return S->kind() == LazyObjectKind; |
| 357 | } |
| 358 | |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 359 | LazyObjectFile *file() { return (LazyObjectFile *)this->File; } |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 360 | InputFile *fetch(); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 361 | }; |
| 362 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 363 | // Some linker-generated symbols need to be created as |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 364 | // DefinedRegular symbols. |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 365 | template <class ELFT> struct ElfSym { |
Petr Hosek | 4f7a5c3 | 2016-08-22 18:44:04 +0000 | [diff] [blame] | 366 | // The content for __ehdr_start symbol. |
| 367 | static DefinedRegular<ELFT> *EhdrStart; |
| 368 | |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 369 | // The content for _etext and etext symbols. |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 370 | static DefinedRegular<ELFT> *Etext; |
| 371 | static DefinedRegular<ELFT> *Etext2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 372 | |
| 373 | // The content for _edata and edata symbols. |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 374 | static DefinedRegular<ELFT> *Edata; |
| 375 | static DefinedRegular<ELFT> *Edata2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 376 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 377 | // The content for _end and end symbols. |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 378 | static DefinedRegular<ELFT> *End; |
| 379 | static DefinedRegular<ELFT> *End2; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 380 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 381 | // The content for _gp_disp symbol for MIPS target. |
Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 382 | static SymbolBody *MipsGpDisp; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
Petr Hosek | 4f7a5c3 | 2016-08-22 18:44:04 +0000 | [diff] [blame] | 385 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::EhdrStart; |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 386 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext; |
| 387 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2; |
| 388 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata; |
| 389 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2; |
| 390 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End; |
| 391 | template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2; |
Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 392 | template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGpDisp; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 393 | |
| 394 | // A real symbol object, SymbolBody, is usually stored within a Symbol. There's |
| 395 | // always one Symbol for each symbol name. The resolver updates the SymbolBody |
| 396 | // stored in the Body field of this object as it resolves symbols. Symbol also |
| 397 | // holds computed properties of symbol names. |
| 398 | struct Symbol { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 399 | // Symbol binding. This is on the Symbol to track changes during resolution. |
| 400 | // In particular: |
| 401 | // An undefined weak is still weak when it resolves to a shared library. |
| 402 | // An undefined weak will not fetch archive members, but we have to remember |
| 403 | // it is weak. |
| 404 | uint8_t Binding; |
| 405 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 406 | // Version definition index. |
| 407 | uint16_t VersionId; |
| 408 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 409 | // Symbol visibility. This is the computed minimum visibility of all |
| 410 | // observed non-DSO symbols. |
| 411 | unsigned Visibility : 2; |
| 412 | |
| 413 | // True if the symbol was used for linking and thus need to be added to the |
| 414 | // output file's symbol table. This is true for all symbols except for |
| 415 | // unreferenced DSO symbols and bitcode symbols that are unreferenced except |
| 416 | // by other bitcode objects. |
| 417 | unsigned IsUsedInRegularObj : 1; |
| 418 | |
| 419 | // If this flag is true and the symbol has protected or default visibility, it |
| 420 | // will appear in .dynsym. This flag is set by interposable DSO symbols in |
| 421 | // executables, by most symbols in DSOs and executables built with |
| 422 | // --export-dynamic, and by dynamic lists. |
| 423 | unsigned ExportDynamic : 1; |
| 424 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 425 | // True if this symbol is specified by --trace-symbol option. |
| 426 | unsigned Traced : 1; |
| 427 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 428 | bool includeInDynsym() const; |
| 429 | bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; } |
| 430 | |
| 431 | // This field is used to store the Symbol's SymbolBody. This instantiation of |
| 432 | // AlignedCharArrayUnion gives us a struct with a char array field that is |
| 433 | // large and aligned enough to store any derived class of SymbolBody. We |
| 434 | // assume that the size and alignment of ELF64LE symbols is sufficient for any |
| 435 | // ELFT, and we verify this with the static_asserts in replaceBody. |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 436 | llvm::AlignedCharArrayUnion< |
| 437 | DefinedCommon, DefinedRegular<llvm::object::ELF64LE>, |
| 438 | DefinedSynthetic<llvm::object::ELF64LE>, Undefined, |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 439 | SharedSymbol<llvm::object::ELF64LE>, LazyArchive, LazyObject> Body; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 440 | |
| 441 | SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); } |
| 442 | const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); } |
| 443 | }; |
| 444 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 445 | void printTraceSymbol(Symbol *Sym); |
| 446 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 447 | template <typename T, typename... ArgT> |
| 448 | void replaceBody(Symbol *S, ArgT &&... Arg) { |
| 449 | static_assert(sizeof(T) <= sizeof(S->Body), "Body too small"); |
Benjamin Kramer | 922b63b | 2016-10-20 15:55:41 +0000 | [diff] [blame] | 450 | static_assert(alignof(T) <= alignof(decltype(S->Body)), |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 451 | "Body not aligned enough"); |
Peter Collingbourne | f643fc1 | 2016-05-01 05:39:02 +0000 | [diff] [blame] | 452 | assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr && |
| 453 | "Not a SymbolBody"); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 454 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 455 | new (S->Body.buffer) T(std::forward<ArgT>(Arg)...); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 456 | |
| 457 | // Print out a log message if --trace-symbol was specified. |
| 458 | // This is for debugging. |
| 459 | if (S->Traced) |
| 460 | printTraceSymbol(S); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | inline Symbol *SymbolBody::symbol() { |
| 464 | assert(!isLocal()); |
| 465 | return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) - |
| 466 | offsetof(Symbol, Body)); |
| 467 | } |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 468 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 469 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 470 | } // namespace lld |
| 471 | |
| 472 | #endif |