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" |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 19 | #include "Strings.h" |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 20 | |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 21 | #include "lld/Common/LLVM.h" |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 22 | #include "llvm/Object/Archive.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | #include "llvm/Object/ELF.h" |
| 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; |
Dmitry Mikulin | 1e30f07 | 2017-09-08 16:22:43 +0000 | [diff] [blame] | 30 | class BssSection; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | class InputFile; |
Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 +0000 | [diff] [blame] | 32 | class LazyObjFile; |
| 33 | template <class ELFT> class ObjFile; |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 34 | class OutputSection; |
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 | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 47 | DefinedLast = DefinedCommonKind, |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 48 | UndefinedKind, |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 49 | LazyArchiveKind, |
| 50 | LazyObjectKind, |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 53 | SymbolBody(Kind K) : SymbolKind(K) {} |
| 54 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 55 | Symbol *symbol(); |
| 56 | const Symbol *symbol() const { |
| 57 | return const_cast<SymbolBody *>(this)->symbol(); |
| 58 | } |
| 59 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 60 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 61 | |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 62 | bool isUndefined() const { return SymbolKind == UndefinedKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 63 | bool isDefined() const { return SymbolKind <= DefinedLast; } |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 64 | bool isCommon() const { return SymbolKind == DefinedCommonKind; } |
Rui Ueyama | c0f5648 | 2017-10-13 03:37:11 +0000 | [diff] [blame] | 65 | bool isShared() const { return SymbolKind == SharedKind; } |
| 66 | bool isLocal() const { return IsLocal; } |
| 67 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 68 | bool isLazy() const { |
| 69 | return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind; |
| 70 | } |
Rui Ueyama | c0f5648 | 2017-10-13 03:37:11 +0000 | [diff] [blame] | 71 | |
George Rimar | d92e128 | 2017-07-12 11:09:46 +0000 | [diff] [blame] | 72 | bool isInCurrentDSO() const { |
Rui Ueyama | c0f5648 | 2017-10-13 03:37:11 +0000 | [diff] [blame] | 73 | return SymbolKind == DefinedRegularKind || SymbolKind == DefinedCommonKind; |
George Rimar | d92e128 | 2017-07-12 11:09:46 +0000 | [diff] [blame] | 74 | } |
Rafael Espindola | 3d9f1c0 | 2017-09-13 20:43:04 +0000 | [diff] [blame] | 75 | |
| 76 | // True is this is an undefined weak symbol. This only works once |
| 77 | // all input files have been added. |
| 78 | bool isUndefWeak() const; |
| 79 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 80 | InputFile *getFile() const; |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 81 | StringRef getName() const { return Name; } |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 82 | uint8_t getVisibility() const { return StOther & 0x3; } |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 83 | void parseSymbolVersion(); |
Rui Ueyama | 5bbe4a4 | 2017-09-25 00:57:30 +0000 | [diff] [blame] | 84 | void copyFrom(SymbolBody *Other); |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 85 | |
Rafael Espindola | 5c2310c | 2015-09-18 14:40:19 +0000 | [diff] [blame] | 86 | bool isInGot() const { return GotIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 87 | bool isInPlt() const { return PltIndex != -1U; } |
Rafael Espindola | eb79273 | 2015-09-21 15:11:29 +0000 | [diff] [blame] | 88 | |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 89 | uint64_t getVA(int64_t Addend = 0) const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 90 | |
George Rimar | 4afe42e | 2017-03-17 14:12:51 +0000 | [diff] [blame] | 91 | uint64_t getGotOffset() const; |
Rafael Espindola | f9e3c9c | 2017-05-11 23:28:49 +0000 | [diff] [blame] | 92 | uint64_t getGotVA() const; |
George Rimar | 4670bb0 | 2017-03-16 12:58:11 +0000 | [diff] [blame] | 93 | uint64_t getGotPltOffset() const; |
| 94 | uint64_t getGotPltVA() const; |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 95 | uint64_t getPltVA() const; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 96 | template <class ELFT> typename ELFT::uint getSize() const; |
George Rimar | 69268a8 | 2017-03-16 11:06:13 +0000 | [diff] [blame] | 97 | OutputSection *getOutputSection() const; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 98 | |
Rafael Espindola | 7ae2177 | 2016-10-26 00:58:23 +0000 | [diff] [blame] | 99 | uint32_t DynsymIndex = 0; |
Rafael Espindola | ca65623 | 2016-10-21 20:32:41 +0000 | [diff] [blame] | 100 | uint32_t GotIndex = -1; |
| 101 | uint32_t GotPltIndex = -1; |
| 102 | uint32_t PltIndex = -1; |
| 103 | uint32_t GlobalDynIndex = -1; |
| 104 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 105 | protected: |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 106 | SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, |
| 107 | uint8_t Type); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 108 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 109 | const unsigned SymbolKind : 8; |
Rui Ueyama | 7d332f5 | 2015-12-25 06:55:39 +0000 | [diff] [blame] | 110 | |
Rui Ueyama | 662bb00 | 2017-10-13 03:37:26 +0000 | [diff] [blame^] | 111 | // True if this is a local symbol. |
| 112 | unsigned IsLocal : 1; |
| 113 | |
Rafael Espindola | abebed9 | 2016-02-05 15:27:15 +0000 | [diff] [blame] | 114 | public: |
Rui Ueyama | 924b361 | 2017-02-16 06:12:22 +0000 | [diff] [blame] | 115 | // True the symbol should point to its PLT entry. |
| 116 | // For SharedSymbol only. |
| 117 | unsigned NeedsPltAddr : 1; |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 118 | // True if this symbol has an entry in the global part of MIPS GOT. |
| 119 | unsigned IsInGlobalMipsGot : 1; |
| 120 | |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 121 | // True if this symbol is referenced by 32-bit GOT relocations. |
| 122 | unsigned Is32BitMipsGot : 1; |
| 123 | |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 124 | // True if this symbol is in the Iplt sub-section of the Plt. |
| 125 | unsigned IsInIplt : 1; |
| 126 | |
| 127 | // True if this symbol is in the Igot sub-section of the .got.plt or .got. |
| 128 | unsigned IsInIgot : 1; |
| 129 | |
Rafael Espindola | 35c908f | 2017-08-10 15:05:37 +0000 | [diff] [blame] | 130 | unsigned IsPreemptible : 1; |
| 131 | |
Rui Ueyama | dd41807 | 2016-04-04 18:15:38 +0000 | [diff] [blame] | 132 | // The following fields have the same meaning as the ELF symbol attributes. |
| 133 | uint8_t Type; // symbol type |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 134 | uint8_t StOther; // st_other field value |
Rui Ueyama | dd41807 | 2016-04-04 18:15:38 +0000 | [diff] [blame] | 135 | |
Peter Collingbourne | f3a2b0e | 2016-05-03 18:03:47 +0000 | [diff] [blame] | 136 | // The Type field may also have this value. It means that we have not yet seen |
| 137 | // a non-Lazy symbol with this name, so we don't know what its type is. The |
| 138 | // Type field is normally set to this value for Lazy symbols unless we saw a |
| 139 | // weak undefined symbol first, in which case we need to remember the original |
| 140 | // symbol's type in order to check for TLS mismatches. |
| 141 | enum { UnknownType = 255 }; |
| 142 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 143 | bool isSection() const { return Type == llvm::ELF::STT_SECTION; } |
| 144 | bool isTls() const { return Type == llvm::ELF::STT_TLS; } |
| 145 | bool isFunc() const { return Type == llvm::ELF::STT_FUNC; } |
| 146 | bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; } |
| 147 | bool isObject() const { return Type == llvm::ELF::STT_OBJECT; } |
| 148 | bool isFile() const { return Type == llvm::ELF::STT_FILE; } |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 149 | |
George Rimar | 2f0fab5 | 2016-03-06 06:26:18 +0000 | [diff] [blame] | 150 | protected: |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 151 | StringRefZ Name; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 154 | // The base class for any defined symbols. |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 155 | class Defined : public SymbolBody { |
| 156 | public: |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 157 | Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type); |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 158 | static bool classof(const SymbolBody *S) { return S->isDefined(); } |
| 159 | }; |
| 160 | |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 161 | class DefinedCommon : public Defined { |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 162 | public: |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 163 | DefinedCommon(StringRef N, uint64_t Size, uint32_t Alignment, uint8_t StOther, |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 164 | uint8_t Type); |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 165 | |
| 166 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 167 | return S->kind() == SymbolBody::DefinedCommonKind; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 168 | } |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 169 | |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 170 | // The maximum alignment we have seen for this symbol. |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 171 | uint32_t Alignment; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 172 | |
Rui Ueyama | 9c77d27 | 2017-08-10 15:54:27 +0000 | [diff] [blame] | 173 | // The output offset of this common symbol in the output bss. |
| 174 | // Computed by the writer. |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 175 | uint64_t Size; |
Dmitry Mikulin | 1e30f07 | 2017-09-08 16:22:43 +0000 | [diff] [blame] | 176 | BssSection *Section = nullptr; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 179 | // Regular defined symbols read from object file symbol tables. |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 180 | class DefinedRegular : public Defined { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 181 | public: |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 182 | DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type, |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 183 | uint64_t Value, uint64_t Size, SectionBase *Section) |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 184 | : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type), |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 185 | Value(Value), Size(Size), Section(Section) {} |
Rafael Espindola | a8bf71c | 2016-10-26 20:34:59 +0000 | [diff] [blame] | 186 | |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 187 | // Return true if the symbol is a PIC function. |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 188 | template <class ELFT> bool isMipsPIC() const; |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 189 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 190 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 191 | return S->kind() == SymbolBody::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 192 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 193 | |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 194 | uint64_t Value; |
| 195 | uint64_t Size; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 196 | SectionBase *Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 199 | class Undefined : public SymbolBody { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 200 | public: |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 201 | Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 202 | |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 203 | static bool classof(const SymbolBody *S) { |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 204 | return S->kind() == UndefinedKind; |
Rafael Espindola | 5f2c46d | 2015-12-23 01:06:39 +0000 | [diff] [blame] | 205 | } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 208 | class SharedSymbol : public Defined { |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 209 | public: |
| 210 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1e2967e | 2015-12-21 20:47:33 +0000 | [diff] [blame] | 211 | return S->kind() == SymbolBody::SharedKind; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 214 | SharedSymbol(StringRef Name, uint8_t StOther, uint8_t Type, |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 215 | const void *ElfSym, const void *Verdef) |
| 216 | : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, StOther, Type), |
| 217 | Verdef(Verdef), ElfSym(ElfSym) { |
Rui Ueyama | d32f06c | 2017-10-12 21:45:53 +0000 | [diff] [blame] | 218 | // GNU ifunc is a mechanism to allow user-supplied functions to |
| 219 | // resolve PLT slot values at load-time. This is contrary to the |
| 220 | // regualr symbol resolution scheme in which symbols are resolved just |
| 221 | // by name. Using this hook, you can program how symbols are solved |
| 222 | // for you program. For example, you can make "memcpy" to be resolved |
| 223 | // to a SSE-enabled version of memcpy only when a machine running the |
| 224 | // program supports the SSE instruction set. |
| 225 | // |
| 226 | // Naturally, such symbols should always be called through their PLT |
| 227 | // slots. What GNU ifunc symbols point to are resolver functions, and |
| 228 | // calling them directly doesn't make sense (unless you are writing a |
| 229 | // loader). |
| 230 | // |
| 231 | // For DSO symbols, we always call them through PLT slots anyway. |
| 232 | // So there's no difference between GNU ifunc and regular function |
Shoaib Meenai | 14cf514 | 2017-10-12 21:52:14 +0000 | [diff] [blame] | 233 | // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC. |
Rui Ueyama | d32f06c | 2017-10-12 21:45:53 +0000 | [diff] [blame] | 234 | if (this->Type == llvm::ELF::STT_GNU_IFUNC) |
George Rimar | 4a9cfc8 | 2017-07-11 11:40:59 +0000 | [diff] [blame] | 235 | this->Type = llvm::ELF::STT_FUNC; |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | template <class ELFT> SharedFile<ELFT> *getFile() const { |
| 239 | return cast<SharedFile<ELFT>>(SymbolBody::getFile()); |
Peter Collingbourne | 5dd550a | 2016-04-26 16:22:51 +0000 | [diff] [blame] | 240 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 241 | |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 242 | template <class ELFT> uint64_t getShndx() const { |
| 243 | return getSym<ELFT>().st_shndx; |
| 244 | } |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 245 | |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 246 | template <class ELFT> uint64_t getValue() const { |
| 247 | return getSym<ELFT>().st_value; |
| 248 | } |
| 249 | |
| 250 | template <class ELFT> uint64_t getSize() const { |
| 251 | return getSym<ELFT>().st_size; |
| 252 | } |
| 253 | |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 254 | template <class ELFT> uint32_t getAlignment() const; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 255 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 256 | // This field is a pointer to the symbol's version definition. |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 257 | const void *Verdef; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 258 | |
George Rimar | 1ab9cf4 | 2017-03-17 10:14:53 +0000 | [diff] [blame] | 259 | // CopyRelSec and CopyRelSecOff are significant only when NeedsCopy is true. |
| 260 | InputSection *CopyRelSec; |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 261 | |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 262 | private: |
| 263 | template <class ELFT> const typename ELFT::Sym &getSym() const { |
| 264 | return *(const typename ELFT::Sym *)ElfSym; |
| 265 | } |
| 266 | |
| 267 | const void *ElfSym; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 270 | // This class represents a symbol defined in an archive file. It is |
| 271 | // created from an archive file header, and it knows how to load an |
| 272 | // object file from an archive to replace itself with a defined |
| 273 | // symbol. If the resolver finds both Undefined and Lazy for |
| 274 | // the same name, it will ask the Lazy to load a file. |
| 275 | class Lazy : public SymbolBody { |
| 276 | public: |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 277 | static bool classof(const SymbolBody *S) { return S->isLazy(); } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 278 | |
| 279 | // Returns an object file for this symbol, or a nullptr if the file |
| 280 | // was already returned. |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 281 | InputFile *fetch(); |
Rui Ueyama | b06700f | 2016-07-17 17:44:41 +0000 | [diff] [blame] | 282 | |
| 283 | protected: |
| 284 | Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type) |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 285 | : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {} |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | // LazyArchive symbols represents symbols in archive files. |
| 289 | class LazyArchive : public Lazy { |
| 290 | public: |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 291 | LazyArchive(const llvm::object::Archive::Symbol S, uint8_t Type); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 292 | |
| 293 | static bool classof(const SymbolBody *S) { |
| 294 | return S->kind() == LazyArchiveKind; |
| 295 | } |
| 296 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 297 | ArchiveFile *getFile(); |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 298 | InputFile *fetch(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 299 | |
| 300 | private: |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 301 | const llvm::object::Archive::Symbol Sym; |
| 302 | }; |
| 303 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 304 | // LazyObject symbols represents symbols in object files between |
| 305 | // --start-lib and --end-lib options. |
| 306 | class LazyObject : public Lazy { |
| 307 | public: |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 308 | LazyObject(StringRef Name, uint8_t Type); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 309 | |
| 310 | static bool classof(const SymbolBody *S) { |
| 311 | return S->kind() == LazyObjectKind; |
| 312 | } |
| 313 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 314 | LazyObjFile *getFile(); |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 315 | InputFile *fetch(); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 318 | // Some linker-generated symbols need to be created as |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 319 | // DefinedRegular symbols. |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 320 | struct ElfSym { |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 321 | // __bss_start |
George Rimar | e6c5d38 | 2017-04-05 10:03:25 +0000 | [diff] [blame] | 322 | static DefinedRegular *Bss; |
| 323 | |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 324 | // etext and _etext |
| 325 | static DefinedRegular *Etext1; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 326 | static DefinedRegular *Etext2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 327 | |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 328 | // edata and _edata |
| 329 | static DefinedRegular *Edata1; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 330 | static DefinedRegular *Edata2; |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 331 | |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 332 | // end and _end |
| 333 | static DefinedRegular *End1; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 334 | static DefinedRegular *End2; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 335 | |
Rui Ueyama | 92c3781 | 2017-06-26 15:11:24 +0000 | [diff] [blame] | 336 | // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to |
| 337 | // be at some offset from the base of the .got section, usually 0 or |
| 338 | // the end of the .got. |
| 339 | static DefinedRegular *GlobalOffsetTable; |
| 340 | |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 341 | // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS. |
| 342 | static DefinedRegular *MipsGp; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 343 | static DefinedRegular *MipsGpDisp; |
| 344 | static DefinedRegular *MipsLocalGp; |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 347 | // A real symbol object, SymbolBody, is usually stored within a Symbol. There's |
| 348 | // always one Symbol for each symbol name. The resolver updates the SymbolBody |
| 349 | // stored in the Body field of this object as it resolves symbols. Symbol also |
| 350 | // holds computed properties of symbol names. |
| 351 | struct Symbol { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 352 | // Symbol binding. This is on the Symbol to track changes during resolution. |
| 353 | // In particular: |
| 354 | // An undefined weak is still weak when it resolves to a shared library. |
| 355 | // An undefined weak will not fetch archive members, but we have to remember |
| 356 | // it is weak. |
| 357 | uint8_t Binding; |
| 358 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 359 | // Version definition index. |
| 360 | uint16_t VersionId; |
| 361 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 362 | // Symbol visibility. This is the computed minimum visibility of all |
| 363 | // observed non-DSO symbols. |
| 364 | unsigned Visibility : 2; |
| 365 | |
| 366 | // True if the symbol was used for linking and thus need to be added to the |
| 367 | // output file's symbol table. This is true for all symbols except for |
| 368 | // unreferenced DSO symbols and bitcode symbols that are unreferenced except |
| 369 | // by other bitcode objects. |
| 370 | unsigned IsUsedInRegularObj : 1; |
| 371 | |
| 372 | // If this flag is true and the symbol has protected or default visibility, it |
| 373 | // will appear in .dynsym. This flag is set by interposable DSO symbols in |
| 374 | // executables, by most symbols in DSOs and executables built with |
| 375 | // --export-dynamic, and by dynamic lists. |
| 376 | unsigned ExportDynamic : 1; |
| 377 | |
Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 378 | // False if LTO shouldn't inline whatever this symbol points to. If a symbol |
| 379 | // is overwritten after LTO, LTO shouldn't inline the symbol because it |
| 380 | // doesn't know the final contents of the symbol. |
| 381 | unsigned CanInline : 1; |
| 382 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 383 | // True if this symbol is specified by --trace-symbol option. |
| 384 | unsigned Traced : 1; |
| 385 | |
Rafael Espindola | d3fc0c9 | 2017-07-12 17:49:17 +0000 | [diff] [blame] | 386 | // This symbol version was found in a version script. |
| 387 | unsigned InVersionScript : 1; |
| 388 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 389 | // The file from which this symbol was created. |
| 390 | InputFile *File = nullptr; |
| 391 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 392 | bool includeInDynsym() const; |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 +0000 | [diff] [blame] | 393 | uint8_t computeBinding() const; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 394 | bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; } |
| 395 | |
| 396 | // This field is used to store the Symbol's SymbolBody. This instantiation of |
| 397 | // AlignedCharArrayUnion gives us a struct with a char array field that is |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 398 | // large and aligned enough to store any derived class of SymbolBody. |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 399 | llvm::AlignedCharArrayUnion<DefinedCommon, DefinedRegular, Undefined, |
| 400 | SharedSymbol, LazyArchive, LazyObject> |
Rui Ueyama | 4f2f50d | 2016-12-21 08:40:09 +0000 | [diff] [blame] | 401 | Body; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 402 | |
| 403 | SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); } |
| 404 | const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); } |
| 405 | }; |
| 406 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 407 | void printTraceSymbol(Symbol *Sym); |
| 408 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 409 | template <typename T, typename... ArgT> |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 410 | void replaceBody(Symbol *S, InputFile *File, ArgT &&... Arg) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 411 | static_assert(sizeof(T) <= sizeof(S->Body), "Body too small"); |
Benjamin Kramer | 922b63b | 2016-10-20 15:55:41 +0000 | [diff] [blame] | 412 | static_assert(alignof(T) <= alignof(decltype(S->Body)), |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 413 | "Body not aligned enough"); |
Peter Collingbourne | f643fc1 | 2016-05-01 05:39:02 +0000 | [diff] [blame] | 414 | assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr && |
| 415 | "Not a SymbolBody"); |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 416 | S->File = File; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 417 | new (S->Body.buffer) T(std::forward<ArgT>(Arg)...); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 418 | |
| 419 | // Print out a log message if --trace-symbol was specified. |
| 420 | // This is for debugging. |
| 421 | if (S->Traced) |
| 422 | printTraceSymbol(S); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | inline Symbol *SymbolBody::symbol() { |
| 426 | assert(!isLocal()); |
| 427 | return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) - |
| 428 | offsetof(Symbol, Body)); |
| 429 | } |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 430 | } // namespace elf |
Rui Ueyama | ce03926 | 2017-01-06 10:04:08 +0000 | [diff] [blame] | 431 | |
| 432 | std::string toString(const elf::SymbolBody &B); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 433 | } // namespace lld |
| 434 | |
| 435 | #endif |