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