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