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;
39 // The base class for real symbol classes.
43 // The order of these is significant. We start with the regular defined
44 // symbols as those are the most prevelant and the zero tag is the cheapest
45 // to set. Among the defined kinds, the lower the kind is preferred over
46 // the higher kind when testing wether one symbol should take precedence
48 DefinedRegularKind = 0,
50 DefinedLocalImportKind,
51 DefinedImportThunkKind,
52 DefinedImportDataKind,
60 LastDefinedCOFFKind = DefinedCommonKind,
61 LastDefinedKind = DefinedBitcodeKind,
64 Kind kind() const { return static_cast<Kind>(SymbolKind); }
66 // Returns true if this is an external symbol.
67 bool isExternal() { return IsExternal; }
69 // Returns the symbol name.
72 // Returns the file from which this symbol was created.
76 const Symbol *symbol() const {
77 return const_cast<SymbolBody *>(this)->symbol();
82 explicit SymbolBody(Kind K, StringRef N = "")
83 : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
84 IsReplaceable(false), WrittenToSymtab(false), Name(N) {}
86 const unsigned SymbolKind : 8;
87 unsigned IsExternal : 1;
89 // This bit is used by the \c DefinedRegular subclass.
90 unsigned IsCOMDAT : 1;
92 // This bit is used by the \c DefinedBitcode subclass.
93 unsigned IsReplaceable : 1;
96 // This bit is used by Writer::createSymbolAndStringTable().
97 unsigned WrittenToSymtab : 1;
103 // The base class for any defined symbols, including absolute symbols,
105 class Defined : public SymbolBody {
107 Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}
109 static bool classof(const SymbolBody *S) {
110 return S->kind() <= LastDefinedKind;
113 // Returns the RVA (relative virtual address) of this symbol. The
114 // writer sets and uses RVAs.
117 // Returns the RVA relative to the beginning of the output section.
118 // Used to implement SECREL relocation type.
119 uint64_t getSecrel();
121 // Returns the output section index.
122 // Used to implement SECTION relocation type.
123 uint64_t getSectionIndex();
125 // Returns true if this symbol points to an executable (e.g. .text) section.
126 // Used to implement ARM relocations.
130 // Symbols defined via a COFF object file.
131 class DefinedCOFF : public Defined {
134 DefinedCOFF(Kind K, ObjectFile *F, COFFSymbolRef S)
135 : Defined(K), File(F), Sym(S.getGeneric()) {}
137 static bool classof(const SymbolBody *S) {
138 return S->kind() <= LastDefinedCOFFKind;
141 ObjectFile *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(ObjectFile *F, COFFSymbolRef S, SectionChunk *C)
155 : DefinedCOFF(DefinedRegularKind, F, S), Data(&C->Repl) {
156 IsExternal = S.isExternal();
157 IsCOMDAT = C->isCOMDAT();
160 static bool classof(const SymbolBody *S) {
161 return S->kind() == DefinedRegularKind;
164 uint64_t getRVA() { return (*Data)->getRVA() + Sym->Value; }
165 bool isCOMDAT() { return IsCOMDAT; }
166 SectionChunk *getChunk() { return *Data; }
167 uint32_t getValue() { return Sym->Value; }
173 class DefinedCommon : public DefinedCOFF {
175 DefinedCommon(ObjectFile *F, COFFSymbolRef S, CommonChunk *C)
176 : DefinedCOFF(DefinedCommonKind, F, S), Data(C) {
177 IsExternal = S.isExternal();
180 static bool classof(const SymbolBody *S) {
181 return S->kind() == DefinedCommonKind;
184 uint64_t getRVA() { return Data->getRVA(); }
188 uint64_t getSize() { return Sym->Value; }
193 class DefinedAbsolute : public Defined {
195 DefinedAbsolute(StringRef N, COFFSymbolRef S)
196 : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
197 IsExternal = S.isExternal();
200 DefinedAbsolute(StringRef N, uint64_t V)
201 : Defined(DefinedAbsoluteKind, N), VA(V) {}
203 static bool classof(const SymbolBody *S) {
204 return S->kind() == DefinedAbsoluteKind;
207 uint64_t getRVA() { return VA - Config->ImageBase; }
208 void setVA(uint64_t V) { VA = V; }
214 // This is a kind of absolute symbol but relative to the image base.
215 // Unlike absolute symbols, relocations referring this kind of symbols
216 // are subject of the base relocation. This type is used rarely --
217 // mainly for __ImageBase.
218 class DefinedRelative : public Defined {
220 explicit DefinedRelative(StringRef Name, uint64_t V = 0)
221 : Defined(DefinedRelativeKind, Name), RVA(V) {}
223 static bool classof(const SymbolBody *S) {
224 return S->kind() == DefinedRelativeKind;
227 uint64_t getRVA() { return RVA; }
228 void setRVA(uint64_t V) { RVA = V; }
234 // This class represents a symbol defined in an archive file. It is
235 // created from an archive file header, and it knows how to load an
236 // object file from an archive to replace itself with a defined
237 // symbol. If the resolver finds both Undefined and Lazy for
238 // the same name, it will ask the Lazy to load a file.
239 class Lazy : public SymbolBody {
241 Lazy(ArchiveFile *F, const Archive::Symbol S)
242 : SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}
244 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
252 const Archive::Symbol Sym;
255 // Undefined symbols.
256 class Undefined : public SymbolBody {
258 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
260 static bool classof(const SymbolBody *S) {
261 return S->kind() == UndefinedKind;
264 // An undefined symbol can have a fallback symbol which gives an
265 // undefined symbol a second chance if it would remain undefined.
266 // If it remains undefined, it'll be replaced with whatever the
267 // Alias pointer points to.
268 SymbolBody *WeakAlias = nullptr;
270 // If this symbol is external weak, try to resolve it to a defined
271 // symbol by searching the chain of fallback symbols. Returns the symbol if
272 // successful, otherwise returns null.
273 Defined *getWeakAlias();
276 // Windows-specific classes.
278 // This class represents a symbol imported from a DLL. This has two
279 // names for internal use and external use. The former is used for
280 // name resolution, and the latter is used for the import descriptor
281 // table in an output. The former has "__imp_" prefix.
282 class DefinedImportData : public Defined {
284 DefinedImportData(StringRef N, ImportFile *F)
285 : Defined(DefinedImportDataKind, N), File(F) {
288 static bool classof(const SymbolBody *S) {
289 return S->kind() == DefinedImportDataKind;
292 uint64_t getRVA() { return File->Location->getRVA(); }
293 StringRef getDLLName() { return File->DLLName; }
294 StringRef getExternalName() { return File->ExternalName; }
295 void setLocation(Chunk *AddressTable) { File->Location = AddressTable; }
296 uint16_t getOrdinal() { return File->Hdr->OrdinalHint; }
302 // This class represents a symbol for a jump table entry which jumps
303 // to a function in a DLL. Linker are supposed to create such symbols
304 // without "__imp_" prefix for all function symbols exported from
305 // DLLs, so that you can call DLL functions as regular functions with
306 // a regular name. A function pointer is given as a DefinedImportData.
307 class DefinedImportThunk : public Defined {
309 DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
311 static bool classof(const SymbolBody *S) {
312 return S->kind() == DefinedImportThunkKind;
315 uint64_t getRVA() { return Data->getRVA(); }
316 Chunk *getChunk() { return Data; }
322 // If you have a symbol "__imp_foo" in your object file, a symbol name
323 // "foo" becomes automatically available as a pointer to "__imp_foo".
324 // This class is for such automatically-created symbols.
325 // Yes, this is an odd feature. We didn't intend to implement that.
326 // This is here just for compatibility with MSVC.
327 class DefinedLocalImport : public Defined {
329 DefinedLocalImport(StringRef N, Defined *S)
330 : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
332 static bool classof(const SymbolBody *S) {
333 return S->kind() == DefinedLocalImportKind;
336 uint64_t getRVA() { return Data->getRVA(); }
337 Chunk *getChunk() { return Data; }
340 LocalImportChunk *Data;
343 class DefinedBitcode : public Defined {
346 DefinedBitcode(BitcodeFile *F, StringRef N, bool IsReplaceable)
347 : Defined(DefinedBitcodeKind, N), File(F) {
348 // IsReplaceable tracks whether the bitcode symbol may be replaced with some
349 // other (defined, common or bitcode) symbol. This is the case for common,
350 // comdat and weak external symbols. We try to replace bitcode symbols with
351 // "real" symbols (see SymbolTable::add{Regular,Bitcode}), and resolve the
352 // result against the real symbol from the combined LTO object.
353 this->IsReplaceable = IsReplaceable;
356 static bool classof(const SymbolBody *S) {
357 return S->kind() == DefinedBitcodeKind;
363 inline uint64_t Defined::getRVA() {
365 case DefinedAbsoluteKind:
366 return cast<DefinedAbsolute>(this)->getRVA();
367 case DefinedRelativeKind:
368 return cast<DefinedRelative>(this)->getRVA();
369 case DefinedImportDataKind:
370 return cast<DefinedImportData>(this)->getRVA();
371 case DefinedImportThunkKind:
372 return cast<DefinedImportThunk>(this)->getRVA();
373 case DefinedLocalImportKind:
374 return cast<DefinedLocalImport>(this)->getRVA();
375 case DefinedCommonKind:
376 return cast<DefinedCommon>(this)->getRVA();
377 case DefinedRegularKind:
378 return cast<DefinedRegular>(this)->getRVA();
379 case DefinedBitcodeKind:
380 llvm_unreachable("There is no address for a bitcode symbol.");
383 llvm_unreachable("Cannot get the address for an undefined symbol.");
385 llvm_unreachable("unknown symbol kind");
388 // A real symbol object, SymbolBody, is usually stored within a Symbol. There's
389 // always one Symbol for each symbol name. The resolver updates the SymbolBody
390 // stored in the Body field of this object as it resolves symbols. Symbol also
391 // holds computed properties of symbol names.
393 // True if this symbol was referenced by a regular (non-bitcode) object.
394 unsigned IsUsedInRegularObj : 1;
396 // True if we've seen both a lazy and an undefined symbol with this symbol
397 // name, which means that we have enqueued an archive member load and should
398 // not load any more archive members to resolve the same symbol.
399 unsigned PendingArchiveLoad : 1;
401 // This field is used to store the Symbol's SymbolBody. This instantiation of
402 // AlignedCharArrayUnion gives us a struct with a char array field that is
403 // large and aligned enough to store any derived class of SymbolBody.
404 llvm::AlignedCharArrayUnion<DefinedRegular, DefinedCommon, DefinedAbsolute,
405 DefinedRelative, Lazy, Undefined,
406 DefinedImportData, DefinedImportThunk,
407 DefinedLocalImport, DefinedBitcode>
411 return reinterpret_cast<SymbolBody *>(Body.buffer);
413 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
416 template <typename T, typename... ArgT>
417 void replaceBody(Symbol *S, ArgT &&... Arg) {
418 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
419 static_assert(alignof(T) <= alignof(decltype(S->Body)),
420 "Body not aligned enough");
421 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
423 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
426 inline Symbol *SymbolBody::symbol() {
427 assert(isExternal());
428 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
429 offsetof(Symbol, Body));
432 std::string toString(SymbolBody &B);