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