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 // The sentinel absolute symbol section index. Section index relocations
217 // against absolute symbols resolve to this 16 bit number, and it is the
218 // largest valid section index plus one. This is written by the Writer.
219 static uint16_t OutputSectionIndex;
220 uint16_t getSecIdx() { return OutputSectionIndex; }
226 // This symbol is used for linker-synthesized symbols like __ImageBase and
227 // __safe_se_handler_table.
228 class DefinedSynthetic : public Defined {
230 explicit DefinedSynthetic(StringRef Name, Chunk *C)
231 : Defined(DefinedSyntheticKind, Name), C(C) {}
233 static bool classof(const Symbol *S) {
234 return S->kind() == DefinedSyntheticKind;
237 // A null chunk indicates that this is __ImageBase. Otherwise, this is some
238 // other synthesized chunk, like SEHTableChunk.
239 uint32_t getRVA() { return C ? C->getRVA() : 0; }
240 Chunk *getChunk() { return C; }
246 // This class represents a symbol defined in an archive file. It is
247 // created from an archive file header, and it knows how to load an
248 // object file from an archive to replace itself with a defined
249 // symbol. If the resolver finds both Undefined and Lazy for
250 // the same name, it will ask the Lazy to load a file.
251 class Lazy : public Symbol {
253 Lazy(ArchiveFile *F, const Archive::Symbol S)
254 : Symbol(LazyKind, S.getName()), File(F), Sym(S) {}
256 static bool classof(const Symbol *S) { return S->kind() == LazyKind; }
264 const Archive::Symbol Sym;
267 // Undefined symbols.
268 class Undefined : public Symbol {
270 explicit Undefined(StringRef N) : Symbol(UndefinedKind, N) {}
272 static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
274 // An undefined symbol can have a fallback symbol which gives an
275 // undefined symbol a second chance if it would remain undefined.
276 // If it remains undefined, it'll be replaced with whatever the
277 // Alias pointer points to.
278 Symbol *WeakAlias = nullptr;
280 // If this symbol is external weak, try to resolve it to a defined
281 // symbol by searching the chain of fallback symbols. Returns the symbol if
282 // successful, otherwise returns null.
283 Defined *getWeakAlias();
286 // Windows-specific classes.
288 // This class represents a symbol imported from a DLL. This has two
289 // names for internal use and external use. The former is used for
290 // name resolution, and the latter is used for the import descriptor
291 // table in an output. The former has "__imp_" prefix.
292 class DefinedImportData : public Defined {
294 DefinedImportData(StringRef N, ImportFile *F)
295 : Defined(DefinedImportDataKind, N), File(F) {
298 static bool classof(const Symbol *S) {
299 return S->kind() == DefinedImportDataKind;
302 uint64_t getRVA() { return File->Location->getRVA(); }
303 Chunk *getChunk() { return File->Location; }
304 void setLocation(Chunk *AddressTable) { File->Location = AddressTable; }
306 StringRef getDLLName() { return File->DLLName; }
307 StringRef getExternalName() { return File->ExternalName; }
308 uint16_t getOrdinal() { return File->Hdr->OrdinalHint; }
313 // This class represents a symbol for a jump table entry which jumps
314 // to a function in a DLL. Linker are supposed to create such symbols
315 // without "__imp_" prefix for all function symbols exported from
316 // DLLs, so that you can call DLL functions as regular functions with
317 // a regular name. A function pointer is given as a DefinedImportData.
318 class DefinedImportThunk : public Defined {
320 DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
322 static bool classof(const Symbol *S) {
323 return S->kind() == DefinedImportThunkKind;
326 uint64_t getRVA() { return Data->getRVA(); }
327 Chunk *getChunk() { return Data; }
329 DefinedImportData *WrappedSym;
335 // If you have a symbol "__imp_foo" in your object file, a symbol name
336 // "foo" becomes automatically available as a pointer to "__imp_foo".
337 // This class is for such automatically-created symbols.
338 // Yes, this is an odd feature. We didn't intend to implement that.
339 // This is here just for compatibility with MSVC.
340 class DefinedLocalImport : public Defined {
342 DefinedLocalImport(StringRef N, Defined *S)
343 : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
345 static bool classof(const Symbol *S) {
346 return S->kind() == DefinedLocalImportKind;
349 uint64_t getRVA() { return Data->getRVA(); }
350 Chunk *getChunk() { return Data; }
353 LocalImportChunk *Data;
356 inline uint64_t Defined::getRVA() {
358 case DefinedAbsoluteKind:
359 return cast<DefinedAbsolute>(this)->getRVA();
360 case DefinedSyntheticKind:
361 return cast<DefinedSynthetic>(this)->getRVA();
362 case DefinedImportDataKind:
363 return cast<DefinedImportData>(this)->getRVA();
364 case DefinedImportThunkKind:
365 return cast<DefinedImportThunk>(this)->getRVA();
366 case DefinedLocalImportKind:
367 return cast<DefinedLocalImport>(this)->getRVA();
368 case DefinedCommonKind:
369 return cast<DefinedCommon>(this)->getRVA();
370 case DefinedRegularKind:
371 return cast<DefinedRegular>(this)->getRVA();
374 llvm_unreachable("Cannot get the address for an undefined symbol.");
376 llvm_unreachable("unknown symbol kind");
379 inline Chunk *Defined::getChunk() {
381 case DefinedRegularKind:
382 return cast<DefinedRegular>(this)->getChunk();
383 case DefinedAbsoluteKind:
385 case DefinedSyntheticKind:
386 return cast<DefinedSynthetic>(this)->getChunk();
387 case DefinedImportDataKind:
388 return cast<DefinedImportData>(this)->getChunk();
389 case DefinedImportThunkKind:
390 return cast<DefinedImportThunk>(this)->getChunk();
391 case DefinedLocalImportKind:
392 return cast<DefinedLocalImport>(this)->getChunk();
393 case DefinedCommonKind:
394 return cast<DefinedCommon>(this)->getChunk();
397 llvm_unreachable("Cannot get the chunk of an undefined symbol.");
399 llvm_unreachable("unknown symbol kind");
402 // A buffer class that is large enough to hold any Symbol-derived
403 // object. We allocate memory using this class and instantiate a symbol
404 // using the placement new.
406 alignas(DefinedRegular) char A[sizeof(DefinedRegular)];
407 alignas(DefinedCommon) char B[sizeof(DefinedCommon)];
408 alignas(DefinedAbsolute) char C[sizeof(DefinedAbsolute)];
409 alignas(DefinedSynthetic) char D[sizeof(DefinedSynthetic)];
410 alignas(Lazy) char E[sizeof(Lazy)];
411 alignas(Undefined) char F[sizeof(Undefined)];
412 alignas(DefinedImportData) char G[sizeof(DefinedImportData)];
413 alignas(DefinedImportThunk) char H[sizeof(DefinedImportThunk)];
414 alignas(DefinedLocalImport) char I[sizeof(DefinedLocalImport)];
417 template <typename T, typename... ArgT>
418 void replaceSymbol(Symbol *S, ArgT &&... Arg) {
419 static_assert(sizeof(T) <= sizeof(SymbolUnion), "Symbol too small");
420 static_assert(alignof(T) <= alignof(SymbolUnion),
421 "SymbolUnion not aligned enough");
422 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
424 new (S) T(std::forward<ArgT>(Arg)...);
428 std::string toString(coff::Symbol &B);