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 = DefinedRelativeKind,
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 RVA relative to the beginning of the output section.
114 // Used to implement SECREL relocation type.
115 uint64_t getSecrel();
117 // Returns the output section index.
118 // Used to implement SECTION relocation type.
119 uint64_t getSectionIndex();
121 // Returns true if this symbol points to an executable (e.g. .text) section.
122 // Used to implement ARM relocations.
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 {
133 DefinedCOFF(Kind K, InputFile *F, StringRef N, const coff_symbol_generic *S)
134 : Defined(K, N), File(F), Sym(S) {}
136 static bool classof(const SymbolBody *S) {
137 return S->kind() <= LastDefinedCOFFKind;
140 InputFile *getFile() { return File; }
142 COFFSymbolRef getCOFFSymbol();
147 const coff_symbol_generic *Sym;
150 // Regular defined symbols read from object file symbol tables.
151 class DefinedRegular : public DefinedCOFF {
153 DefinedRegular(InputFile *F, StringRef N, bool IsCOMDAT,
154 bool IsExternal = false,
155 const coff_symbol_generic *S = nullptr,
156 SectionChunk *C = nullptr)
157 : DefinedCOFF(DefinedRegularKind, F, N, S), Data(C ? &C->Repl : nullptr) {
158 this->IsExternal = IsExternal;
159 this->IsCOMDAT = IsCOMDAT;
162 static bool classof(const SymbolBody *S) {
163 return S->kind() == DefinedRegularKind;
166 uint64_t getRVA() { return (*Data)->getRVA() + Sym->Value; }
167 bool isCOMDAT() { return IsCOMDAT; }
168 SectionChunk *getChunk() { return *Data; }
169 uint32_t getValue() { 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 SymbolBody *S) {
185 return S->kind() == DefinedCommonKind;
188 uint64_t getRVA() { return Data->getRVA(); }
192 uint64_t getSize() const { return Size; }
198 class DefinedAbsolute : public Defined {
200 DefinedAbsolute(StringRef N, COFFSymbolRef S)
201 : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
202 IsExternal = S.isExternal();
205 DefinedAbsolute(StringRef N, uint64_t V)
206 : Defined(DefinedAbsoluteKind, N), VA(V) {}
208 static bool classof(const SymbolBody *S) {
209 return S->kind() == DefinedAbsoluteKind;
212 uint64_t getRVA() { return VA - Config->ImageBase; }
213 void setVA(uint64_t V) { VA = V; }
219 // This is a kind of absolute symbol but relative to the image base.
220 // Unlike absolute symbols, relocations referring this kind of symbols
221 // are subject of the base relocation. This type is used rarely --
222 // mainly for __ImageBase.
223 class DefinedRelative : public Defined {
225 explicit DefinedRelative(StringRef Name, uint64_t V = 0)
226 : Defined(DefinedRelativeKind, Name), RVA(V) {}
228 static bool classof(const SymbolBody *S) {
229 return S->kind() == DefinedRelativeKind;
232 uint64_t getRVA() { return RVA; }
233 void setRVA(uint64_t V) { RVA = V; }
239 // This class represents a symbol defined in an archive file. It is
240 // created from an archive file header, and it knows how to load an
241 // object file from an archive to replace itself with a defined
242 // symbol. If the resolver finds both Undefined and Lazy for
243 // the same name, it will ask the Lazy to load a file.
244 class Lazy : public SymbolBody {
246 Lazy(ArchiveFile *F, const Archive::Symbol S)
247 : SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}
249 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
257 const Archive::Symbol Sym;
260 // Undefined symbols.
261 class Undefined : public SymbolBody {
263 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
265 static bool classof(const SymbolBody *S) {
266 return S->kind() == UndefinedKind;
269 // An undefined symbol can have a fallback symbol which gives an
270 // undefined symbol a second chance if it would remain undefined.
271 // If it remains undefined, it'll be replaced with whatever the
272 // Alias pointer points to.
273 SymbolBody *WeakAlias = nullptr;
275 // If this symbol is external weak, try to resolve it to a defined
276 // symbol by searching the chain of fallback symbols. Returns the symbol if
277 // successful, otherwise returns null.
278 Defined *getWeakAlias();
281 // Windows-specific classes.
283 // This class represents a symbol imported from a DLL. This has two
284 // names for internal use and external use. The former is used for
285 // name resolution, and the latter is used for the import descriptor
286 // table in an output. The former has "__imp_" prefix.
287 class DefinedImportData : public Defined {
289 DefinedImportData(StringRef N, ImportFile *F)
290 : Defined(DefinedImportDataKind, N), File(F) {
293 static bool classof(const SymbolBody *S) {
294 return S->kind() == DefinedImportDataKind;
297 uint64_t getRVA() { return File->Location->getRVA(); }
298 StringRef getDLLName() { return File->DLLName; }
299 StringRef getExternalName() { return File->ExternalName; }
300 void setLocation(Chunk *AddressTable) { File->Location = AddressTable; }
301 uint16_t getOrdinal() { return File->Hdr->OrdinalHint; }
306 // This class represents a symbol for a jump table entry which jumps
307 // to a function in a DLL. Linker are supposed to create such symbols
308 // without "__imp_" prefix for all function symbols exported from
309 // DLLs, so that you can call DLL functions as regular functions with
310 // a regular name. A function pointer is given as a DefinedImportData.
311 class DefinedImportThunk : public Defined {
313 DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
315 static bool classof(const SymbolBody *S) {
316 return S->kind() == DefinedImportThunkKind;
319 uint64_t getRVA() { return Data->getRVA(); }
320 Chunk *getChunk() { return Data; }
322 DefinedImportData *WrappedSym;
328 // If you have a symbol "__imp_foo" in your object file, a symbol name
329 // "foo" becomes automatically available as a pointer to "__imp_foo".
330 // This class is for such automatically-created symbols.
331 // Yes, this is an odd feature. We didn't intend to implement that.
332 // This is here just for compatibility with MSVC.
333 class DefinedLocalImport : public Defined {
335 DefinedLocalImport(StringRef N, Defined *S)
336 : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
338 static bool classof(const SymbolBody *S) {
339 return S->kind() == DefinedLocalImportKind;
342 uint64_t getRVA() { return Data->getRVA(); }
343 Chunk *getChunk() { return Data; }
346 LocalImportChunk *Data;
349 inline uint64_t Defined::getRVA() {
351 case DefinedAbsoluteKind:
352 return cast<DefinedAbsolute>(this)->getRVA();
353 case DefinedRelativeKind:
354 return cast<DefinedRelative>(this)->getRVA();
355 case DefinedImportDataKind:
356 return cast<DefinedImportData>(this)->getRVA();
357 case DefinedImportThunkKind:
358 return cast<DefinedImportThunk>(this)->getRVA();
359 case DefinedLocalImportKind:
360 return cast<DefinedLocalImport>(this)->getRVA();
361 case DefinedCommonKind:
362 return cast<DefinedCommon>(this)->getRVA();
363 case DefinedRegularKind:
364 return cast<DefinedRegular>(this)->getRVA();
367 llvm_unreachable("Cannot get the address for an undefined symbol.");
369 llvm_unreachable("unknown symbol kind");
372 // A real symbol object, SymbolBody, is usually stored within a Symbol. There's
373 // always one Symbol for each symbol name. The resolver updates the SymbolBody
374 // stored in the Body field of this object as it resolves symbols. Symbol also
375 // holds computed properties of symbol names.
377 // True if this symbol was referenced by a regular (non-bitcode) object.
378 unsigned IsUsedInRegularObj : 1;
380 // True if we've seen both a lazy and an undefined symbol with this symbol
381 // name, which means that we have enqueued an archive member load and should
382 // not load any more archive members to resolve the same symbol.
383 unsigned PendingArchiveLoad : 1;
385 // This field is used to store the Symbol's SymbolBody. This instantiation of
386 // AlignedCharArrayUnion gives us a struct with a char array field that is
387 // large and aligned enough to store any derived class of SymbolBody.
388 llvm::AlignedCharArrayUnion<
389 DefinedRegular, DefinedCommon, DefinedAbsolute, DefinedRelative, Lazy,
390 Undefined, DefinedImportData, DefinedImportThunk, DefinedLocalImport>
394 return reinterpret_cast<SymbolBody *>(Body.buffer);
396 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
399 template <typename T, typename... ArgT>
400 void replaceBody(Symbol *S, ArgT &&... Arg) {
401 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
402 static_assert(alignof(T) <= alignof(decltype(S->Body)),
403 "Body not aligned enough");
404 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
406 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
409 inline Symbol *SymbolBody::symbol() {
410 assert(isExternal());
411 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
412 offsetof(Symbol, Body));
416 std::string toString(coff::SymbolBody &B);