1 //===- Symbols.h ------------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_COFF_SYMBOLS_H
11 #define LLD_COFF_SYMBOLS_H
15 #include "lld/Common/LLVM.h"
16 #include "lld/Common/Memory.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/COFF.h"
27 using llvm::object::Archive;
28 using llvm::object::COFFSymbolRef;
29 using llvm::object::coff_import_header;
30 using llvm::object::coff_symbol_generic;
37 // The base class for real symbol classes.
41 // The order of these is significant. We start with the regular defined
42 // symbols as those are the most prevelant and the zero tag is the cheapest
43 // to set. Among the defined kinds, the lower the kind is preferred over
44 // the higher kind when testing wether one symbol should take precedence
46 DefinedRegularKind = 0,
48 DefinedLocalImportKind,
49 DefinedImportThunkKind,
50 DefinedImportDataKind,
57 LastDefinedCOFFKind = DefinedCommonKind,
58 LastDefinedKind = DefinedSyntheticKind,
61 Kind kind() const { return static_cast<Kind>(SymbolKind); }
63 // Returns true if this is an external symbol.
64 bool isExternal() { return IsExternal; }
66 // Returns the symbol name.
69 // Returns the file from which this symbol was created.
72 // Indicates that this symbol will be included in the final image. Only valid
73 // after calling markLive.
78 explicit Symbol(Kind K, StringRef N = "")
79 : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
80 WrittenToSymtab(false), PendingArchiveLoad(false), IsGCRoot(false),
83 const unsigned SymbolKind : 8;
84 unsigned IsExternal : 1;
86 // This bit is used by the \c DefinedRegular subclass.
87 unsigned IsCOMDAT : 1;
90 // This bit is used by Writer::createSymbolAndStringTable() to prevent
91 // symbols from being written to the symbol table more than once.
92 unsigned WrittenToSymtab : 1;
94 // True if this symbol was referenced by a regular (non-bitcode) object.
95 unsigned IsUsedInRegularObj : 1;
97 // True if we've seen both a lazy and an undefined symbol with this symbol
98 // name, which means that we have enqueued an archive member load and should
99 // not load any more archive members to resolve the same symbol.
100 unsigned PendingArchiveLoad : 1;
102 /// True if we've already added this symbol to the list of GC roots.
103 unsigned IsGCRoot : 1;
109 // The base class for any defined symbols, including absolute symbols,
111 class Defined : public Symbol {
113 Defined(Kind K, StringRef N) : Symbol(K, N) {}
115 static bool classof(const Symbol *S) { return S->kind() <= LastDefinedKind; }
117 // Returns the RVA (relative virtual address) of this symbol. The
118 // writer sets and uses RVAs.
121 // Returns the chunk containing this symbol. Absolute symbols and __ImageBase
122 // do not have chunks, so this may return null.
126 // Symbols defined via a COFF object file or bitcode file. For COFF files, this
127 // stores a coff_symbol_generic*, and names of internal symbols are lazily
128 // loaded through that. For bitcode files, Sym is nullptr and the name is stored
130 class DefinedCOFF : public Defined {
134 DefinedCOFF(Kind K, InputFile *F, StringRef N, const coff_symbol_generic *S)
135 : Defined(K, N), File(F), Sym(S) {}
137 static bool classof(const Symbol *S) {
138 return S->kind() <= LastDefinedCOFFKind;
141 InputFile *getFile() { return File; }
143 COFFSymbolRef getCOFFSymbol();
148 const coff_symbol_generic *Sym;
151 // Regular defined symbols read from object file symbol tables.
152 class DefinedRegular : public DefinedCOFF {
154 DefinedRegular(InputFile *F, StringRef N, bool IsCOMDAT,
155 bool IsExternal = false,
156 const coff_symbol_generic *S = nullptr,
157 SectionChunk *C = nullptr)
158 : DefinedCOFF(DefinedRegularKind, F, N, S), Data(C ? &C->Repl : nullptr) {
159 this->IsExternal = IsExternal;
160 this->IsCOMDAT = IsCOMDAT;
163 static bool classof(const Symbol *S) {
164 return S->kind() == DefinedRegularKind;
167 uint64_t getRVA() const { return (*Data)->getRVA() + Sym->Value; }
168 bool isCOMDAT() const { return IsCOMDAT; }
169 SectionChunk *getChunk() const { return *Data; }
170 uint32_t getValue() const { return Sym->Value; }
175 class DefinedCommon : public DefinedCOFF {
177 DefinedCommon(InputFile *F, StringRef N, uint64_t Size,
178 const coff_symbol_generic *S = nullptr,
179 CommonChunk *C = nullptr)
180 : DefinedCOFF(DefinedCommonKind, F, N, S), Data(C), Size(Size) {
181 this->IsExternal = true;
184 static bool classof(const Symbol *S) {
185 return S->kind() == DefinedCommonKind;
188 uint64_t getRVA() { return Data->getRVA(); }
189 CommonChunk *getChunk() { return Data; }
193 uint64_t getSize() const { return Size; }
199 class DefinedAbsolute : public Defined {
201 DefinedAbsolute(StringRef N, COFFSymbolRef S)
202 : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
203 IsExternal = S.isExternal();
206 DefinedAbsolute(StringRef N, uint64_t V)
207 : Defined(DefinedAbsoluteKind, N), VA(V) {}
209 static bool classof(const Symbol *S) {
210 return S->kind() == DefinedAbsoluteKind;
213 uint64_t getRVA() { return VA - Config->ImageBase; }
214 void setVA(uint64_t V) { VA = V; }
216 // Section index relocations against absolute symbols resolve to
217 // this 16 bit number, and it is the largest valid section index
218 // plus one. This variable keeps it.
219 static uint16_t NumOutputSections;
225 // This symbol is used for linker-synthesized symbols like __ImageBase and
226 // __safe_se_handler_table.
227 class DefinedSynthetic : public Defined {
229 explicit DefinedSynthetic(StringRef Name, Chunk *C)
230 : Defined(DefinedSyntheticKind, Name), C(C) {}
232 static bool classof(const Symbol *S) {
233 return S->kind() == DefinedSyntheticKind;
236 // A null chunk indicates that this is __ImageBase. Otherwise, this is some
237 // other synthesized chunk, like SEHTableChunk.
238 uint32_t getRVA() { return C ? C->getRVA() : 0; }
239 Chunk *getChunk() { return C; }
245 // This class represents a symbol defined in an archive file. It is
246 // created from an archive file header, and it knows how to load an
247 // object file from an archive to replace itself with a defined
248 // symbol. If the resolver finds both Undefined and Lazy for
249 // the same name, it will ask the Lazy to load a file.
250 class Lazy : public Symbol {
252 Lazy(ArchiveFile *F, const Archive::Symbol S)
253 : Symbol(LazyKind, S.getName()), File(F), Sym(S) {}
255 static bool classof(const Symbol *S) { return S->kind() == LazyKind; }
263 const Archive::Symbol Sym;
266 // Undefined symbols.
267 class Undefined : public Symbol {
269 explicit Undefined(StringRef N) : Symbol(UndefinedKind, N) {}
271 static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
273 // An undefined symbol can have a fallback symbol which gives an
274 // undefined symbol a second chance if it would remain undefined.
275 // If it remains undefined, it'll be replaced with whatever the
276 // Alias pointer points to.
277 Symbol *WeakAlias = nullptr;
279 // If this symbol is external weak, try to resolve it to a defined
280 // symbol by searching the chain of fallback symbols. Returns the symbol if
281 // successful, otherwise returns null.
282 Defined *getWeakAlias();
285 // Windows-specific classes.
287 // This class represents a symbol imported from a DLL. This has two
288 // names for internal use and external use. The former is used for
289 // name resolution, and the latter is used for the import descriptor
290 // table in an output. The former has "__imp_" prefix.
291 class DefinedImportData : public Defined {
293 DefinedImportData(StringRef N, ImportFile *F)
294 : Defined(DefinedImportDataKind, N), File(F) {
297 static bool classof(const Symbol *S) {
298 return S->kind() == DefinedImportDataKind;
301 uint64_t getRVA() { return File->Location->getRVA(); }
302 Chunk *getChunk() { return File->Location; }
303 void setLocation(Chunk *AddressTable) { File->Location = AddressTable; }
305 StringRef getDLLName() { return File->DLLName; }
306 StringRef getExternalName() { return File->ExternalName; }
307 uint16_t getOrdinal() { return File->Hdr->OrdinalHint; }
312 // This class represents a symbol for a jump table entry which jumps
313 // to a function in a DLL. Linker are supposed to create such symbols
314 // without "__imp_" prefix for all function symbols exported from
315 // DLLs, so that you can call DLL functions as regular functions with
316 // a regular name. A function pointer is given as a DefinedImportData.
317 class DefinedImportThunk : public Defined {
319 DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
321 static bool classof(const Symbol *S) {
322 return S->kind() == DefinedImportThunkKind;
325 uint64_t getRVA() { return Data->getRVA(); }
326 Chunk *getChunk() { return Data; }
328 DefinedImportData *WrappedSym;
334 // If you have a symbol "__imp_foo" in your object file, a symbol name
335 // "foo" becomes automatically available as a pointer to "__imp_foo".
336 // This class is for such automatically-created symbols.
337 // Yes, this is an odd feature. We didn't intend to implement that.
338 // This is here just for compatibility with MSVC.
339 class DefinedLocalImport : public Defined {
341 DefinedLocalImport(StringRef N, Defined *S)
342 : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
344 static bool classof(const Symbol *S) {
345 return S->kind() == DefinedLocalImportKind;
348 uint64_t getRVA() { return Data->getRVA(); }
349 Chunk *getChunk() { return Data; }
352 LocalImportChunk *Data;
355 inline uint64_t Defined::getRVA() {
357 case DefinedAbsoluteKind:
358 return cast<DefinedAbsolute>(this)->getRVA();
359 case DefinedSyntheticKind:
360 return cast<DefinedSynthetic>(this)->getRVA();
361 case DefinedImportDataKind:
362 return cast<DefinedImportData>(this)->getRVA();
363 case DefinedImportThunkKind:
364 return cast<DefinedImportThunk>(this)->getRVA();
365 case DefinedLocalImportKind:
366 return cast<DefinedLocalImport>(this)->getRVA();
367 case DefinedCommonKind:
368 return cast<DefinedCommon>(this)->getRVA();
369 case DefinedRegularKind:
370 return cast<DefinedRegular>(this)->getRVA();
373 llvm_unreachable("Cannot get the address for an undefined symbol.");
375 llvm_unreachable("unknown symbol kind");
378 inline Chunk *Defined::getChunk() {
380 case DefinedRegularKind:
381 return cast<DefinedRegular>(this)->getChunk();
382 case DefinedAbsoluteKind:
384 case DefinedSyntheticKind:
385 return cast<DefinedSynthetic>(this)->getChunk();
386 case DefinedImportDataKind:
387 return cast<DefinedImportData>(this)->getChunk();
388 case DefinedImportThunkKind:
389 return cast<DefinedImportThunk>(this)->getChunk();
390 case DefinedLocalImportKind:
391 return cast<DefinedLocalImport>(this)->getChunk();
392 case DefinedCommonKind:
393 return cast<DefinedCommon>(this)->getChunk();
396 llvm_unreachable("Cannot get the chunk of an undefined symbol.");
398 llvm_unreachable("unknown symbol kind");
401 // A buffer class that is large enough to hold any Symbol-derived
402 // object. We allocate memory using this class and instantiate a symbol
403 // using the placement new.
405 alignas(DefinedRegular) char A[sizeof(DefinedRegular)];
406 alignas(DefinedCommon) char B[sizeof(DefinedCommon)];
407 alignas(DefinedAbsolute) char C[sizeof(DefinedAbsolute)];
408 alignas(DefinedSynthetic) char D[sizeof(DefinedSynthetic)];
409 alignas(Lazy) char E[sizeof(Lazy)];
410 alignas(Undefined) char F[sizeof(Undefined)];
411 alignas(DefinedImportData) char G[sizeof(DefinedImportData)];
412 alignas(DefinedImportThunk) char H[sizeof(DefinedImportThunk)];
413 alignas(DefinedLocalImport) char I[sizeof(DefinedLocalImport)];
416 template <typename T, typename... ArgT>
417 void replaceSymbol(Symbol *S, ArgT &&... Arg) {
418 static_assert(std::is_trivially_destructible<T>(),
419 "Symbol types must be trivially destructible");
420 static_assert(sizeof(T) <= sizeof(SymbolUnion), "Symbol too small");
421 static_assert(alignof(T) <= alignof(SymbolUnion),
422 "SymbolUnion not aligned enough");
423 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
425 new (S) T(std::forward<ArgT>(Arg)...);
429 std::string toString(coff::Symbol &B);