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
16 #include "lld/Core/LLVM.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;
38 // The base class for real symbol classes.
42 // The order of these is significant. We start with the regular defined
43 // symbols as those are the most prevelant and the zero tag is the cheapest
44 // to set. Among the defined kinds, the lower the kind is preferred over
45 // the higher kind when testing wether one symbol should take precedence
47 DefinedRegularKind = 0,
49 DefinedLocalImportKind,
50 DefinedImportThunkKind,
51 DefinedImportDataKind,
58 LastDefinedCOFFKind = DefinedCommonKind,
59 LastDefinedKind = DefinedSyntheticKind,
62 Kind kind() const { return static_cast<Kind>(SymbolKind); }
64 // Returns true if this is an external symbol.
65 bool isExternal() { return IsExternal; }
67 // Returns the symbol name.
70 // Returns the file from which this symbol was created.
74 const Symbol *symbol() const {
75 return const_cast<SymbolBody *>(this)->symbol();
80 explicit SymbolBody(Kind K, StringRef N = "")
81 : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
82 WrittenToSymtab(false), Name(N) {}
84 const unsigned SymbolKind : 8;
85 unsigned IsExternal : 1;
87 // This bit is used by the \c DefinedRegular subclass.
88 unsigned IsCOMDAT : 1;
91 // This bit is used by Writer::createSymbolAndStringTable() to prevent
92 // symbols from being written to the symbol table more than once.
93 unsigned WrittenToSymtab : 1;
99 // The base class for any defined symbols, including absolute symbols,
101 class Defined : public SymbolBody {
103 Defined(Kind K, StringRef N) : SymbolBody(K, N) {}
105 static bool classof(const SymbolBody *S) {
106 return S->kind() <= LastDefinedKind;
109 // Returns the RVA (relative virtual address) of this symbol. The
110 // writer sets and uses RVAs.
113 // Returns the chunk containing this symbol. Absolute symbols and __ImageBase
114 // do not have chunks, so this may return null.
118 // Symbols defined via a COFF object file or bitcode file. For COFF files, this
119 // stores a coff_symbol_generic*, and names of internal symbols are lazily
120 // loaded through that. For bitcode files, Sym is nullptr and the name is stored
122 class DefinedCOFF : public Defined {
125 DefinedCOFF(Kind K, InputFile *F, StringRef N, const coff_symbol_generic *S)
126 : Defined(K, N), File(F), Sym(S) {}
128 static bool classof(const SymbolBody *S) {
129 return S->kind() <= LastDefinedCOFFKind;
132 InputFile *getFile() { return File; }
134 COFFSymbolRef getCOFFSymbol();
139 const coff_symbol_generic *Sym;
142 // Regular defined symbols read from object file symbol tables.
143 class DefinedRegular : public DefinedCOFF {
145 DefinedRegular(InputFile *F, StringRef N, bool IsCOMDAT,
146 bool IsExternal = false,
147 const coff_symbol_generic *S = nullptr,
148 SectionChunk *C = nullptr)
149 : DefinedCOFF(DefinedRegularKind, F, N, S), Data(C ? &C->Repl : nullptr) {
150 this->IsExternal = IsExternal;
151 this->IsCOMDAT = IsCOMDAT;
154 static bool classof(const SymbolBody *S) {
155 return S->kind() == DefinedRegularKind;
158 uint64_t getRVA() { return (*Data)->getRVA() + Sym->Value; }
159 bool isCOMDAT() { return IsCOMDAT; }
160 SectionChunk *getChunk() { return *Data; }
161 uint32_t getValue() { return Sym->Value; }
167 class DefinedCommon : public DefinedCOFF {
169 DefinedCommon(InputFile *F, StringRef N, uint64_t Size,
170 const coff_symbol_generic *S = nullptr,
171 CommonChunk *C = nullptr)
172 : DefinedCOFF(DefinedCommonKind, F, N, S), Data(C), Size(Size) {
173 this->IsExternal = true;
176 static bool classof(const SymbolBody *S) {
177 return S->kind() == DefinedCommonKind;
180 uint64_t getRVA() { return Data->getRVA(); }
181 Chunk *getChunk() { return Data; }
185 uint64_t getSize() const { return Size; }
191 class DefinedAbsolute : public Defined {
193 DefinedAbsolute(StringRef N, COFFSymbolRef S)
194 : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
195 IsExternal = S.isExternal();
198 DefinedAbsolute(StringRef N, uint64_t V)
199 : Defined(DefinedAbsoluteKind, N), VA(V) {}
201 static bool classof(const SymbolBody *S) {
202 return S->kind() == DefinedAbsoluteKind;
205 uint64_t getRVA() { return VA - Config->ImageBase; }
206 void setVA(uint64_t V) { VA = V; }
208 // The sentinel absolute symbol section index. Section index relocations
209 // against absolute symbols resolve to this 16 bit number, and it is the
210 // largest valid section index plus one. This is written by the Writer.
211 static uint16_t OutputSectionIndex;
212 uint16_t getSecIdx() { return OutputSectionIndex; }
218 // This symbol is used for linker-synthesized symbols like __ImageBase and
219 // __safe_se_handler_table.
220 class DefinedSynthetic : public Defined {
222 explicit DefinedSynthetic(StringRef Name, Chunk *C)
223 : Defined(DefinedSyntheticKind, Name), C(C) {}
225 static bool classof(const SymbolBody *S) {
226 return S->kind() == DefinedSyntheticKind;
229 // A null chunk indicates that this is __ImageBase. Otherwise, this is some
230 // other synthesized chunk, like SEHTableChunk.
231 uint32_t getRVA() { return C ? C->getRVA() : 0; }
232 Chunk *getChunk() { return C; }
238 // This class represents a symbol defined in an archive file. It is
239 // created from an archive file header, and it knows how to load an
240 // object file from an archive to replace itself with a defined
241 // symbol. If the resolver finds both Undefined and Lazy for
242 // the same name, it will ask the Lazy to load a file.
243 class Lazy : public SymbolBody {
245 Lazy(ArchiveFile *F, const Archive::Symbol S)
246 : SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}
248 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
256 const Archive::Symbol Sym;
259 // Undefined symbols.
260 class Undefined : public SymbolBody {
262 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
264 static bool classof(const SymbolBody *S) {
265 return S->kind() == UndefinedKind;
268 // An undefined symbol can have a fallback symbol which gives an
269 // undefined symbol a second chance if it would remain undefined.
270 // If it remains undefined, it'll be replaced with whatever the
271 // Alias pointer points to.
272 SymbolBody *WeakAlias = nullptr;
274 // If this symbol is external weak, try to resolve it to a defined
275 // symbol by searching the chain of fallback symbols. Returns the symbol if
276 // successful, otherwise returns null.
277 Defined *getWeakAlias();
280 // Windows-specific classes.
282 // This class represents a symbol imported from a DLL. This has two
283 // names for internal use and external use. The former is used for
284 // name resolution, and the latter is used for the import descriptor
285 // table in an output. The former has "__imp_" prefix.
286 class DefinedImportData : public Defined {
288 DefinedImportData(StringRef N, ImportFile *F)
289 : Defined(DefinedImportDataKind, N), File(F) {
292 static bool classof(const SymbolBody *S) {
293 return S->kind() == DefinedImportDataKind;
296 uint64_t getRVA() { return File->Location->getRVA(); }
297 Chunk *getChunk() { return File->Location; }
298 void setLocation(Chunk *AddressTable) { File->Location = AddressTable; }
300 StringRef getDLLName() { return File->DLLName; }
301 StringRef getExternalName() { return File->ExternalName; }
302 uint16_t getOrdinal() { return File->Hdr->OrdinalHint; }
307 // This class represents a symbol for a jump table entry which jumps
308 // to a function in a DLL. Linker are supposed to create such symbols
309 // without "__imp_" prefix for all function symbols exported from
310 // DLLs, so that you can call DLL functions as regular functions with
311 // a regular name. A function pointer is given as a DefinedImportData.
312 class DefinedImportThunk : public Defined {
314 DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
316 static bool classof(const SymbolBody *S) {
317 return S->kind() == DefinedImportThunkKind;
320 uint64_t getRVA() { return Data->getRVA(); }
321 Chunk *getChunk() { return Data; }
323 DefinedImportData *WrappedSym;
329 // If you have a symbol "__imp_foo" in your object file, a symbol name
330 // "foo" becomes automatically available as a pointer to "__imp_foo".
331 // This class is for such automatically-created symbols.
332 // Yes, this is an odd feature. We didn't intend to implement that.
333 // This is here just for compatibility with MSVC.
334 class DefinedLocalImport : public Defined {
336 DefinedLocalImport(StringRef N, Defined *S)
337 : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
339 static bool classof(const SymbolBody *S) {
340 return S->kind() == DefinedLocalImportKind;
343 uint64_t getRVA() { return Data->getRVA(); }
344 Chunk *getChunk() { return Data; }
347 LocalImportChunk *Data;
350 inline uint64_t Defined::getRVA() {
352 case DefinedAbsoluteKind:
353 return cast<DefinedAbsolute>(this)->getRVA();
354 case DefinedSyntheticKind:
355 return cast<DefinedSynthetic>(this)->getRVA();
356 case DefinedImportDataKind:
357 return cast<DefinedImportData>(this)->getRVA();
358 case DefinedImportThunkKind:
359 return cast<DefinedImportThunk>(this)->getRVA();
360 case DefinedLocalImportKind:
361 return cast<DefinedLocalImport>(this)->getRVA();
362 case DefinedCommonKind:
363 return cast<DefinedCommon>(this)->getRVA();
364 case DefinedRegularKind:
365 return cast<DefinedRegular>(this)->getRVA();
368 llvm_unreachable("Cannot get the address for an undefined symbol.");
370 llvm_unreachable("unknown symbol kind");
373 inline Chunk *Defined::getChunk() {
375 case DefinedRegularKind:
376 return cast<DefinedRegular>(this)->getChunk();
377 case DefinedAbsoluteKind:
379 case DefinedSyntheticKind:
380 return cast<DefinedSynthetic>(this)->getChunk();
381 case DefinedImportDataKind:
382 return cast<DefinedImportData>(this)->getChunk();
383 case DefinedImportThunkKind:
384 return cast<DefinedImportThunk>(this)->getChunk();
385 case DefinedLocalImportKind:
386 return cast<DefinedLocalImport>(this)->getChunk();
387 case DefinedCommonKind:
388 return cast<DefinedCommon>(this)->getChunk();
391 llvm_unreachable("Cannot get the chunk of an undefined symbol.");
393 llvm_unreachable("unknown symbol kind");
396 // A real symbol object, SymbolBody, is usually stored within a Symbol. There's
397 // always one Symbol for each symbol name. The resolver updates the SymbolBody
398 // stored in the Body field of this object as it resolves symbols. Symbol also
399 // holds computed properties of symbol names.
401 // True if this symbol was referenced by a regular (non-bitcode) object.
402 unsigned IsUsedInRegularObj : 1;
404 // True if we've seen both a lazy and an undefined symbol with this symbol
405 // name, which means that we have enqueued an archive member load and should
406 // not load any more archive members to resolve the same symbol.
407 unsigned PendingArchiveLoad : 1;
409 // This field is used to store the Symbol's SymbolBody. This instantiation of
410 // AlignedCharArrayUnion gives us a struct with a char array field that is
411 // large and aligned enough to store any derived class of SymbolBody.
412 llvm::AlignedCharArrayUnion<
413 DefinedRegular, DefinedCommon, DefinedAbsolute, DefinedSynthetic, Lazy,
414 Undefined, DefinedImportData, DefinedImportThunk, DefinedLocalImport>
418 return reinterpret_cast<SymbolBody *>(Body.buffer);
420 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
423 template <typename T, typename... ArgT>
424 void replaceBody(Symbol *S, ArgT &&... Arg) {
425 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
426 static_assert(alignof(T) <= alignof(decltype(S->Body)),
427 "Body not aligned enough");
428 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
430 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
433 inline Symbol *SymbolBody::symbol() {
434 assert(isExternal());
435 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
436 offsetof(Symbol, Body));
440 std::string toString(coff::SymbolBody &B);