Rafael Espindola | beee25e | 2015-08-14 14:12:54 | [diff] [blame] | 1 | //===- Symbols.h ------------------------------------------------*- C++ -*-===// |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLD_COFF_SYMBOLS_H |
| 10 | #define LLD_COFF_SYMBOLS_H |
| 11 | |
| 12 | #include "Chunks.h" |
| 13 | #include "Config.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 14 | #include "lld/Common/LLVM.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 | [diff] [blame] | 15 | #include "lld/Common/Memory.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/Object/Archive.h" |
| 18 | #include "llvm/Object/COFF.h" |
Rui Ueyama | c80c03d | 2015-07-05 21:54:42 | [diff] [blame] | 19 | #include <atomic> |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace lld { |
Nico Weber | 9c0716f | 2019-07-23 19:00:01 | [diff] [blame] | 24 | |
| 25 | std::string toString(coff::Symbol &b); |
| 26 | |
| 27 | // There are two different ways to convert an Archive::Symbol to a string: |
| 28 | // One for Microsoft name mangling and one for Itanium name mangling. |
| 29 | // Call the functions toCOFFString and toELFString, not just toString. |
| 30 | std::string toCOFFString(const coff::Archive::Symbol &b); |
| 31 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 32 | namespace coff { |
| 33 | |
| 34 | using llvm::object::Archive; |
| 35 | using llvm::object::COFFSymbolRef; |
Rui Ueyama | c9bfe32 | 2015-05-29 15:45:35 | [diff] [blame] | 36 | using llvm::object::coff_import_header; |
Rui Ueyama | c15139b | 2015-06-30 00:10:54 | [diff] [blame] | 37 | using llvm::object::coff_symbol_generic; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 38 | |
| 39 | class ArchiveFile; |
| 40 | class InputFile; |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 | [diff] [blame] | 41 | class ObjFile; |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 | [diff] [blame] | 42 | class SymbolTable; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 43 | |
| 44 | // The base class for real symbol classes. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 45 | class Symbol { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 46 | public: |
| 47 | enum Kind { |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 48 | // The order of these is significant. We start with the regular defined |
Nico Weber | 1f3ab98 | 2019-01-14 19:05:21 | [diff] [blame] | 49 | // symbols as those are the most prevalent and the zero tag is the cheapest |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 50 | // to set. Among the defined kinds, the lower the kind is preferred over |
Nico Weber | c7bad57 | 2018-08-27 14:22:25 | [diff] [blame] | 51 | // the higher kind when testing whether one symbol should take precedence |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 52 | // over another. |
| 53 | DefinedRegularKind = 0, |
Rui Ueyama | efb7e1a | 2015-06-20 07:21:57 | [diff] [blame] | 54 | DefinedCommonKind, |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 55 | DefinedLocalImportKind, |
| 56 | DefinedImportThunkKind, |
| 57 | DefinedImportDataKind, |
| 58 | DefinedAbsoluteKind, |
Reid Kleckner | 502d4ce | 2017-06-26 15:39:52 | [diff] [blame] | 59 | DefinedSyntheticKind, |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 60 | |
Rui Ueyama | e251476 | 2015-06-15 19:06:53 | [diff] [blame] | 61 | UndefinedKind, |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 62 | LazyArchiveKind, |
| 63 | LazyObjectKind, |
Martin Storsjö | a9ff1ce | 2021-06-16 13:59:46 | [diff] [blame] | 64 | LazyDLLSymbolKind, |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 65 | |
| 66 | LastDefinedCOFFKind = DefinedCommonKind, |
Reid Kleckner | 502d4ce | 2017-06-26 15:39:52 | [diff] [blame] | 67 | LastDefinedKind = DefinedSyntheticKind, |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 68 | }; |
| 69 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 70 | Kind kind() const { return static_cast<Kind>(symbolKind); } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 71 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 72 | // Returns the symbol name. |
Reid Kleckner | 9b7f614 | 2020-05-03 02:53:49 | [diff] [blame] | 73 | StringRef getName() { |
| 74 | // COFF symbol names are read lazily for a performance reason. |
| 75 | // Non-external symbol names are never used by the linker except for logging |
| 76 | // or debugging. Their internal references are resolved not by name but by |
| 77 | // symbol index. And because they are not external, no one can refer them by |
| 78 | // name. Object files contain lots of non-external symbols, and creating |
| 79 | // StringRefs for them (which involves lots of strlen() on the string table) |
| 80 | // is a waste of time. |
| 81 | if (nameData == nullptr) |
| 82 | computeName(); |
| 83 | return StringRef(nameData, nameSize); |
| 84 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 85 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 86 | void replaceKeepingName(Symbol *other, size_t size); |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 87 | |
Rui Ueyama | a45d45e | 2016-12-07 23:17:02 | [diff] [blame] | 88 | // Returns the file from which this symbol was created. |
| 89 | InputFile *getFile(); |
| 90 | |
Reid Kleckner | eacdf04 | 2017-07-27 18:25:59 | [diff] [blame] | 91 | // Indicates that this symbol will be included in the final image. Only valid |
| 92 | // after calling markLive. |
| 93 | bool isLive() const; |
| 94 | |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 95 | bool isLazy() const { |
Martin Storsjö | a9ff1ce | 2021-06-16 13:59:46 | [diff] [blame] | 96 | return symbolKind == LazyArchiveKind || symbolKind == LazyObjectKind || |
| 97 | symbolKind == LazyDLLSymbolKind; |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 98 | } |
| 99 | |
Reid Kleckner | 9b7f614 | 2020-05-03 02:53:49 | [diff] [blame] | 100 | private: |
| 101 | void computeName(); |
| 102 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 103 | protected: |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 | [diff] [blame] | 104 | friend SymbolTable; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 105 | explicit Symbol(Kind k, StringRef n = "") |
| 106 | : symbolKind(k), isExternal(true), isCOMDAT(false), |
| 107 | writtenToSymtab(false), pendingArchiveLoad(false), isGCRoot(false), |
Martin Storsjö | 3785a41 | 2020-10-06 10:54:49 | [diff] [blame] | 108 | isRuntimePseudoReloc(false), deferUndefined(false), canInline(true), |
| 109 | nameSize(n.size()), nameData(n.empty() ? nullptr : n.data()) {} |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 110 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 111 | const unsigned symbolKind : 8; |
| 112 | unsigned isExternal : 1; |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 113 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 | [diff] [blame] | 114 | public: |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 115 | // This bit is used by the \c DefinedRegular subclass. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 116 | unsigned isCOMDAT : 1; |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 117 | |
Peter Collingbourne | f874bd6 | 2017-11-21 01:14:14 | [diff] [blame] | 118 | // This bit is used by Writer::createSymbolAndStringTable() to prevent |
| 119 | // symbols from being written to the symbol table more than once. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 120 | unsigned writtenToSymtab : 1; |
Peter Collingbourne | f874bd6 | 2017-11-21 01:14:14 | [diff] [blame] | 121 | |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 | [diff] [blame] | 122 | // True if this symbol was referenced by a regular (non-bitcode) object. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 123 | unsigned isUsedInRegularObj : 1; |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 | [diff] [blame] | 124 | |
| 125 | // True if we've seen both a lazy and an undefined symbol with this symbol |
| 126 | // name, which means that we have enqueued an archive member load and should |
| 127 | // not load any more archive members to resolve the same symbol. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 128 | unsigned pendingArchiveLoad : 1; |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 | [diff] [blame] | 129 | |
Reid Kleckner | 5883989 | 2017-11-13 18:38:53 | [diff] [blame] | 130 | /// True if we've already added this symbol to the list of GC roots. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 131 | unsigned isGCRoot : 1; |
Reid Kleckner | 5883989 | 2017-11-13 18:38:53 | [diff] [blame] | 132 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 133 | unsigned isRuntimePseudoReloc : 1; |
Martin Storsjo | 2bfa125 | 2018-09-26 06:13:39 | [diff] [blame] | 134 | |
Martin Storsjö | 3785a41 | 2020-10-06 10:54:49 | [diff] [blame] | 135 | // True if we want to allow this symbol to be undefined in the early |
| 136 | // undefined check pass in SymbolTable::reportUnresolvable(), as it |
| 137 | // might be fixed up later. |
| 138 | unsigned deferUndefined : 1; |
| 139 | |
| 140 | // False if LTO shouldn't inline whatever this symbol points to. If a symbol |
| 141 | // is overwritten after LTO, LTO shouldn't inline the symbol because it |
| 142 | // doesn't know the final contents of the symbol. |
| 143 | unsigned canInline : 1; |
| 144 | |
Peter Collingbourne | 9911128 | 2016-12-11 22:15:20 | [diff] [blame] | 145 | protected: |
Reid Kleckner | a30920c | 2019-04-19 22:51:49 | [diff] [blame] | 146 | // Symbol name length. Assume symbol lengths fit in a 32-bit integer. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 147 | uint32_t nameSize; |
Reid Kleckner | a30920c | 2019-04-19 22:51:49 | [diff] [blame] | 148 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 149 | const char *nameData; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | // The base class for any defined symbols, including absolute symbols, |
| 153 | // etc. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 154 | class Defined : public Symbol { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 155 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 156 | Defined(Kind k, StringRef n) : Symbol(k, n) {} |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 157 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 158 | static bool classof(const Symbol *s) { return s->kind() <= LastDefinedKind; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 159 | |
| 160 | // Returns the RVA (relative virtual address) of this symbol. The |
| 161 | // writer sets and uses RVAs. |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 162 | uint64_t getRVA(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 163 | |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 | [diff] [blame] | 164 | // Returns the chunk containing this symbol. Absolute symbols and __ImageBase |
| 165 | // do not have chunks, so this may return null. |
| 166 | Chunk *getChunk(); |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 167 | }; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 168 | |
Bob Haarman | cde5e5b | 2017-02-02 23:58:14 | [diff] [blame] | 169 | // Symbols defined via a COFF object file or bitcode file. For COFF files, this |
| 170 | // stores a coff_symbol_generic*, and names of internal symbols are lazily |
| 171 | // loaded through that. For bitcode files, Sym is nullptr and the name is stored |
Reid Kleckner | a30920c | 2019-04-19 22:51:49 | [diff] [blame] | 172 | // as a decomposed StringRef. |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 173 | class DefinedCOFF : public Defined { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 174 | friend Symbol; |
| 175 | |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 176 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 177 | DefinedCOFF(Kind k, InputFile *f, StringRef n, const coff_symbol_generic *s) |
| 178 | : Defined(k, n), file(f), sym(s) {} |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 179 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 180 | static bool classof(const Symbol *s) { |
| 181 | return s->kind() <= LastDefinedCOFFKind; |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 182 | } |
| 183 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 184 | InputFile *getFile() { return file; } |
Rui Ueyama | 65813ed | 2015-07-02 20:33:48 | [diff] [blame] | 185 | |
David Majnemer | 3a62d3d | 2015-07-09 17:43:50 | [diff] [blame] | 186 | COFFSymbolRef getCOFFSymbol(); |
| 187 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 188 | InputFile *file; |
Rui Ueyama | a45d45e | 2016-12-07 23:17:02 | [diff] [blame] | 189 | |
| 190 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 191 | const coff_symbol_generic *sym; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | // Regular defined symbols read from object file symbol tables. |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 195 | class DefinedRegular : public DefinedCOFF { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 196 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 197 | DefinedRegular(InputFile *f, StringRef n, bool isCOMDAT, |
| 198 | bool isExternal = false, |
| 199 | const coff_symbol_generic *s = nullptr, |
| 200 | SectionChunk *c = nullptr) |
| 201 | : DefinedCOFF(DefinedRegularKind, f, n, s), data(c ? &c->repl : nullptr) { |
| 202 | this->isExternal = isExternal; |
| 203 | this->isCOMDAT = isCOMDAT; |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 204 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 205 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 206 | static bool classof(const Symbol *s) { |
| 207 | return s->kind() == DefinedRegularKind; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 208 | } |
| 209 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 210 | uint64_t getRVA() const { return (*data)->getRVA() + sym->Value; } |
| 211 | SectionChunk *getChunk() const { return *data; } |
| 212 | uint32_t getValue() const { return sym->Value; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 213 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 214 | SectionChunk **data; |
Rui Ueyama | efb7e1a | 2015-06-20 07:21:57 | [diff] [blame] | 215 | }; |
| 216 | |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 217 | class DefinedCommon : public DefinedCOFF { |
Rui Ueyama | efb7e1a | 2015-06-20 07:21:57 | [diff] [blame] | 218 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 219 | DefinedCommon(InputFile *f, StringRef n, uint64_t size, |
| 220 | const coff_symbol_generic *s = nullptr, |
| 221 | CommonChunk *c = nullptr) |
| 222 | : DefinedCOFF(DefinedCommonKind, f, n, s), data(c), size(size) { |
| 223 | this->isExternal = true; |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 224 | } |
Rui Ueyama | efb7e1a | 2015-06-20 07:21:57 | [diff] [blame] | 225 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 226 | static bool classof(const Symbol *s) { |
| 227 | return s->kind() == DefinedCommonKind; |
Rui Ueyama | efb7e1a | 2015-06-20 07:21:57 | [diff] [blame] | 228 | } |
| 229 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 230 | uint64_t getRVA() { return data->getRVA(); } |
| 231 | CommonChunk *getChunk() { return data; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 232 | |
| 233 | private: |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 | [diff] [blame] | 234 | friend SymbolTable; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 235 | uint64_t getSize() const { return size; } |
| 236 | CommonChunk *data; |
| 237 | uint64_t size; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | // Absolute symbols. |
| 241 | class DefinedAbsolute : public Defined { |
| 242 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 243 | DefinedAbsolute(StringRef n, COFFSymbolRef s) |
| 244 | : Defined(DefinedAbsoluteKind, n), va(s.getValue()) { |
| 245 | isExternal = s.isExternal(); |
Chandler Carruth | 64c17c7 | 2015-06-29 21:35:48 | [diff] [blame] | 246 | } |
Rui Ueyama | ccde19d | 2015-06-26 03:09:23 | [diff] [blame] | 247 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 248 | DefinedAbsolute(StringRef n, uint64_t v) |
| 249 | : Defined(DefinedAbsoluteKind, n), va(v) {} |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 250 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 251 | static bool classof(const Symbol *s) { |
| 252 | return s->kind() == DefinedAbsoluteKind; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 253 | } |
| 254 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 255 | uint64_t getRVA() { return va - config->imageBase; } |
| 256 | void setVA(uint64_t v) { va = v; } |
Martin Storsjö | 78ce19b | 2020-01-06 11:54:12 | [diff] [blame] | 257 | uint64_t getVA() const { return va; } |
Martin Storsjö | 1737cc7 | 2019-12-29 22:32:22 | [diff] [blame] | 258 | |
Rui Ueyama | b310747 | 2018-02-17 20:41:38 | [diff] [blame] | 259 | // Section index relocations against absolute symbols resolve to |
| 260 | // this 16 bit number, and it is the largest valid section index |
| 261 | // plus one. This variable keeps it. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 262 | static uint16_t numOutputSections; |
Reid Kleckner | 8456411 | 2017-06-22 23:33:04 | [diff] [blame] | 263 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 264 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 265 | uint64_t va; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 266 | }; |
| 267 | |
Reid Kleckner | 502d4ce | 2017-06-26 15:39:52 | [diff] [blame] | 268 | // This symbol is used for linker-synthesized symbols like __ImageBase and |
| 269 | // __safe_se_handler_table. |
| 270 | class DefinedSynthetic : public Defined { |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 | [diff] [blame] | 271 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 272 | explicit DefinedSynthetic(StringRef name, Chunk *c) |
| 273 | : Defined(DefinedSyntheticKind, name), c(c) {} |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 | [diff] [blame] | 274 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 275 | static bool classof(const Symbol *s) { |
| 276 | return s->kind() == DefinedSyntheticKind; |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 | [diff] [blame] | 277 | } |
| 278 | |
Reid Kleckner | 502d4ce | 2017-06-26 15:39:52 | [diff] [blame] | 279 | // A null chunk indicates that this is __ImageBase. Otherwise, this is some |
| 280 | // other synthesized chunk, like SEHTableChunk. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 281 | uint32_t getRVA() { return c ? c->getRVA() : 0; } |
| 282 | Chunk *getChunk() { return c; } |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 | [diff] [blame] | 283 | |
| 284 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 285 | Chunk *c; |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 | [diff] [blame] | 286 | }; |
| 287 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 288 | // This class represents a symbol defined in an archive file. It is |
| 289 | // created from an archive file header, and it knows how to load an |
| 290 | // object file from an archive to replace itself with a defined |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 291 | // symbol. If the resolver finds both Undefined and LazyArchive for |
| 292 | // the same name, it will ask the LazyArchive to load a file. |
| 293 | class LazyArchive : public Symbol { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 294 | public: |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 295 | LazyArchive(ArchiveFile *f, const Archive::Symbol s) |
| 296 | : Symbol(LazyArchiveKind, s.getName()), file(f), sym(s) {} |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 297 | |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 298 | static bool classof(const Symbol *s) { return s->kind() == LazyArchiveKind; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 299 | |
Amy Huang | a1c022c | 2019-08-22 19:40:07 | [diff] [blame] | 300 | MemoryBufferRef getMemberBuffer(); |
| 301 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 302 | ArchiveFile *file; |
Vlad Tsyrklevich | 802aab5 | 2019-08-30 23:24:41 | [diff] [blame] | 303 | const Archive::Symbol sym; |
Bob Haarman | fd7569c | 2019-08-30 16:50:10 | [diff] [blame] | 304 | }; |
| 305 | |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 306 | class LazyObject : public Symbol { |
| 307 | public: |
Fangrui Song | d496abb | 2022-01-04 23:11:44 | [diff] [blame] | 308 | LazyObject(InputFile *f, StringRef n) : Symbol(LazyObjectKind, n), file(f) {} |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 309 | static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; } |
Fangrui Song | d496abb | 2022-01-04 23:11:44 | [diff] [blame] | 310 | InputFile *file; |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 311 | }; |
| 312 | |
Martin Storsjö | e0e0948 | 2021-07-20 20:56:00 | [diff] [blame] | 313 | // MinGW only. |
Martin Storsjö | a9ff1ce | 2021-06-16 13:59:46 | [diff] [blame] | 314 | class LazyDLLSymbol : public Symbol { |
| 315 | public: |
| 316 | LazyDLLSymbol(DLLFile *f, DLLFile::Symbol *s, StringRef n) |
| 317 | : Symbol(LazyDLLSymbolKind, n), file(f), sym(s) {} |
| 318 | static bool classof(const Symbol *s) { |
| 319 | return s->kind() == LazyDLLSymbolKind; |
| 320 | } |
| 321 | |
| 322 | DLLFile *file; |
| 323 | DLLFile::Symbol *sym; |
| 324 | }; |
| 325 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 326 | // Undefined symbols. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 327 | class Undefined : public Symbol { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 328 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 329 | explicit Undefined(StringRef n) : Symbol(UndefinedKind, n) {} |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 330 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 331 | static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 332 | |
| 333 | // An undefined symbol can have a fallback symbol which gives an |
| 334 | // undefined symbol a second chance if it would remain undefined. |
| 335 | // If it remains undefined, it'll be replaced with whatever the |
| 336 | // Alias pointer points to. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 337 | Symbol *weakAlias = nullptr; |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 | [diff] [blame] | 338 | |
| 339 | // If this symbol is external weak, try to resolve it to a defined |
| 340 | // symbol by searching the chain of fallback symbols. Returns the symbol if |
| 341 | // successful, otherwise returns null. |
| 342 | Defined *getWeakAlias(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | // Windows-specific classes. |
| 346 | |
Rui Ueyama | 7c4fcdd | 2015-05-29 15:49:09 | [diff] [blame] | 347 | // This class represents a symbol imported from a DLL. This has two |
| 348 | // names for internal use and external use. The former is used for |
| 349 | // name resolution, and the latter is used for the import descriptor |
| 350 | // table in an output. The former has "__imp_" prefix. |
| 351 | class DefinedImportData : public Defined { |
| 352 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 353 | DefinedImportData(StringRef n, ImportFile *f) |
| 354 | : Defined(DefinedImportDataKind, n), file(f) { |
Rui Ueyama | b6632d9 | 2017-05-22 06:01:37 | [diff] [blame] | 355 | } |
Rui Ueyama | 7c4fcdd | 2015-05-29 15:49:09 | [diff] [blame] | 356 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 357 | static bool classof(const Symbol *s) { |
| 358 | return s->kind() == DefinedImportDataKind; |
Rui Ueyama | 7c4fcdd | 2015-05-29 15:49:09 | [diff] [blame] | 359 | } |
| 360 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 361 | uint64_t getRVA() { return file->location->getRVA(); } |
| 362 | Chunk *getChunk() { return file->location; } |
| 363 | void setLocation(Chunk *addressTable) { file->location = addressTable; } |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 | [diff] [blame] | 364 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 365 | StringRef getDLLName() { return file->dllName; } |
| 366 | StringRef getExternalName() { return file->externalName; } |
| 367 | uint16_t getOrdinal() { return file->hdr->OrdinalHint; } |
Rui Ueyama | 7c4fcdd | 2015-05-29 15:49:09 | [diff] [blame] | 368 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 369 | ImportFile *file; |
Andrew Paverd | 0139c8a | 2020-11-18 02:02:13 | [diff] [blame] | 370 | |
| 371 | // This is a pointer to the synthetic symbol associated with the load thunk |
| 372 | // for this symbol that will be called if the DLL is delay-loaded. This is |
| 373 | // needed for Control Flow Guard because if this DefinedImportData symbol is a |
| 374 | // valid call target, the corresponding load thunk must also be marked as a |
| 375 | // valid call target. |
| 376 | DefinedSynthetic *loadThunkSym = nullptr; |
Rui Ueyama | 7c4fcdd | 2015-05-29 15:49:09 | [diff] [blame] | 377 | }; |
| 378 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 379 | // This class represents a symbol for a jump table entry which jumps |
| 380 | // to a function in a DLL. Linker are supposed to create such symbols |
| 381 | // without "__imp_" prefix for all function symbols exported from |
| 382 | // DLLs, so that you can call DLL functions as regular functions with |
| 383 | // a regular name. A function pointer is given as a DefinedImportData. |
| 384 | class DefinedImportThunk : public Defined { |
| 385 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 386 | DefinedImportThunk(StringRef name, DefinedImportData *s, uint16_t machine); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 387 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 388 | static bool classof(const Symbol *s) { |
| 389 | return s->kind() == DefinedImportThunkKind; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 390 | } |
| 391 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 392 | uint64_t getRVA() { return data->getRVA(); } |
| 393 | Chunk *getChunk() { return data; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 394 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 395 | DefinedImportData *wrappedSym; |
Rui Ueyama | 9aa82f7 | 2017-05-24 22:30:06 | [diff] [blame] | 396 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 397 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 398 | Chunk *data; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 399 | }; |
| 400 | |
Martin Storsjo | d9fd4a0 | 2018-08-07 06:42:53 | [diff] [blame] | 401 | // If you have a symbol "foo" in your object file, a symbol name |
| 402 | // "__imp_foo" becomes automatically available as a pointer to "foo". |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 403 | // This class is for such automatically-created symbols. |
| 404 | // Yes, this is an odd feature. We didn't intend to implement that. |
| 405 | // This is here just for compatibility with MSVC. |
| 406 | class DefinedLocalImport : public Defined { |
| 407 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 408 | DefinedLocalImport(StringRef n, Defined *s) |
| 409 | : Defined(DefinedLocalImportKind, n), data(make<LocalImportChunk>(s)) {} |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 410 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 411 | static bool classof(const Symbol *s) { |
| 412 | return s->kind() == DefinedLocalImportKind; |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 413 | } |
| 414 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 415 | uint64_t getRVA() { return data->getRVA(); } |
| 416 | Chunk *getChunk() { return data; } |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 417 | |
| 418 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 419 | LocalImportChunk *data; |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 420 | }; |
| 421 | |
Rui Ueyama | cb71c72 | 2015-07-13 22:01:27 | [diff] [blame] | 422 | inline uint64_t Defined::getRVA() { |
| 423 | switch (kind()) { |
| 424 | case DefinedAbsoluteKind: |
| 425 | return cast<DefinedAbsolute>(this)->getRVA(); |
Reid Kleckner | 502d4ce | 2017-06-26 15:39:52 | [diff] [blame] | 426 | case DefinedSyntheticKind: |
| 427 | return cast<DefinedSynthetic>(this)->getRVA(); |
Rui Ueyama | cb71c72 | 2015-07-13 22:01:27 | [diff] [blame] | 428 | case DefinedImportDataKind: |
| 429 | return cast<DefinedImportData>(this)->getRVA(); |
| 430 | case DefinedImportThunkKind: |
| 431 | return cast<DefinedImportThunk>(this)->getRVA(); |
| 432 | case DefinedLocalImportKind: |
| 433 | return cast<DefinedLocalImport>(this)->getRVA(); |
| 434 | case DefinedCommonKind: |
| 435 | return cast<DefinedCommon>(this)->getRVA(); |
| 436 | case DefinedRegularKind: |
| 437 | return cast<DefinedRegular>(this)->getRVA(); |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 438 | case LazyArchiveKind: |
| 439 | case LazyObjectKind: |
Martin Storsjö | a9ff1ce | 2021-06-16 13:59:46 | [diff] [blame] | 440 | case LazyDLLSymbolKind: |
Rui Ueyama | cb71c72 | 2015-07-13 22:01:27 | [diff] [blame] | 441 | case UndefinedKind: |
| 442 | llvm_unreachable("Cannot get the address for an undefined symbol."); |
| 443 | } |
| 444 | llvm_unreachable("unknown symbol kind"); |
| 445 | } |
| 446 | |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 | [diff] [blame] | 447 | inline Chunk *Defined::getChunk() { |
| 448 | switch (kind()) { |
| 449 | case DefinedRegularKind: |
| 450 | return cast<DefinedRegular>(this)->getChunk(); |
| 451 | case DefinedAbsoluteKind: |
| 452 | return nullptr; |
| 453 | case DefinedSyntheticKind: |
| 454 | return cast<DefinedSynthetic>(this)->getChunk(); |
| 455 | case DefinedImportDataKind: |
| 456 | return cast<DefinedImportData>(this)->getChunk(); |
| 457 | case DefinedImportThunkKind: |
| 458 | return cast<DefinedImportThunk>(this)->getChunk(); |
| 459 | case DefinedLocalImportKind: |
| 460 | return cast<DefinedLocalImport>(this)->getChunk(); |
| 461 | case DefinedCommonKind: |
| 462 | return cast<DefinedCommon>(this)->getChunk(); |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 463 | case LazyArchiveKind: |
| 464 | case LazyObjectKind: |
Martin Storsjö | a9ff1ce | 2021-06-16 13:59:46 | [diff] [blame] | 465 | case LazyDLLSymbolKind: |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 | [diff] [blame] | 466 | case UndefinedKind: |
| 467 | llvm_unreachable("Cannot get the chunk of an undefined symbol."); |
| 468 | } |
| 469 | llvm_unreachable("unknown symbol kind"); |
| 470 | } |
| 471 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 472 | // A buffer class that is large enough to hold any Symbol-derived |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 | [diff] [blame] | 473 | // object. We allocate memory using this class and instantiate a symbol |
| 474 | // using the placement new. |
| 475 | union SymbolUnion { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 476 | alignas(DefinedRegular) char a[sizeof(DefinedRegular)]; |
| 477 | alignas(DefinedCommon) char b[sizeof(DefinedCommon)]; |
| 478 | alignas(DefinedAbsolute) char c[sizeof(DefinedAbsolute)]; |
| 479 | alignas(DefinedSynthetic) char d[sizeof(DefinedSynthetic)]; |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 480 | alignas(LazyArchive) char e[sizeof(LazyArchive)]; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 481 | alignas(Undefined) char f[sizeof(Undefined)]; |
| 482 | alignas(DefinedImportData) char g[sizeof(DefinedImportData)]; |
| 483 | alignas(DefinedImportThunk) char h[sizeof(DefinedImportThunk)]; |
| 484 | alignas(DefinedLocalImport) char i[sizeof(DefinedLocalImport)]; |
Bob Haarman | 7dc5e7a0 | 2019-09-03 20:32:16 | [diff] [blame] | 485 | alignas(LazyObject) char j[sizeof(LazyObject)]; |
Martin Storsjö | a9ff1ce | 2021-06-16 13:59:46 | [diff] [blame] | 486 | alignas(LazyDLLSymbol) char k[sizeof(LazyDLLSymbol)]; |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | template <typename T, typename... ArgT> |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 490 | void replaceSymbol(Symbol *s, ArgT &&... arg) { |
Sam Clegg | 38f52b2 | 2018-02-13 17:32:31 | [diff] [blame] | 491 | static_assert(std::is_trivially_destructible<T>(), |
| 492 | "Symbol types must be trivially destructible"); |
Rui Ueyama | 616cd99 | 2017-10-31 16:10:24 | [diff] [blame] | 493 | static_assert(sizeof(T) <= sizeof(SymbolUnion), "Symbol too small"); |
| 494 | static_assert(alignof(T) <= alignof(SymbolUnion), |
| 495 | "SymbolUnion not aligned enough"); |
Rui Ueyama | 38781a5 | 2018-02-14 22:43:43 | [diff] [blame] | 496 | assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr && |
| 497 | "Not a Symbol"); |
Martin Storsjö | 3785a41 | 2020-10-06 10:54:49 | [diff] [blame] | 498 | bool canInline = s->canInline; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 499 | new (s) T(std::forward<ArgT>(arg)...); |
Martin Storsjö | 3785a41 | 2020-10-06 10:54:49 | [diff] [blame] | 500 | s->canInline = canInline; |
Peter Collingbourne | 79a5e6b | 2016-12-09 21:55:24 | [diff] [blame] | 501 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 502 | } // namespace coff |
Rui Ueyama | ce03926 | 2017-01-06 10:04:08 | [diff] [blame] | 503 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 504 | } // namespace lld |
| 505 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 506 | #endif |