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