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/Core/LLVM.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Object/COFF.h"
26 using llvm::object::Archive;
27 using llvm::object::COFFSymbolRef;
28 using llvm::object::coff_import_header;
29 using llvm::object::coff_symbol_generic;
37 // A real symbol object, SymbolBody, is usually accessed indirectly
38 // through a Symbol. There's always one Symbol for each symbol name.
39 // The resolver updates SymbolBody pointers as it resolves symbols.
41 explicit Symbol(SymbolBody *P) : Body(P) {}
45 // The base class for real symbol classes.
49 // The order of these is significant. We start with the regular defined
50 // symbols as those are the most prevelant and the zero tag is the cheapest
51 // to set. Among the defined kinds, the lower the kind is preferred over
52 // the higher kind when testing wether one symbol should take precedence
54 DefinedRegularKind = 0,
56 DefinedLocalImportKind,
57 DefinedImportThunkKind,
58 DefinedImportDataKind,
66 LastDefinedCOFFKind = DefinedCommonKind,
67 LastDefinedKind = DefinedBitcodeKind,
70 Kind kind() const { return static_cast<Kind>(SymbolKind); }
72 // Returns true if this is an external symbol.
73 bool isExternal() { return IsExternal; }
75 // Returns the symbol name.
78 // A SymbolBody has a backreference to a Symbol. Originally they are
79 // doubly-linked. A backreference will never change. But the pointer
80 // in the Symbol may be mutated by the resolver. If you have a
81 // pointer P to a SymbolBody and are not sure whether the resolver
82 // has chosen the object among other objects having the same name,
83 // you can access P->Backref->Body to get the resolver's result.
84 void setBackref(Symbol *P) { Backref = P; }
85 SymbolBody *repl() { return Backref ? Backref->Body : this; }
87 // Decides which symbol should "win" in the symbol table, this or
88 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
89 // they are duplicate (conflicting) symbols.
90 int compare(SymbolBody *Other);
92 // Returns a name of this symbol including source file name.
93 // Used only for debugging and logging.
94 std::string getDebugName();
97 explicit SymbolBody(Kind K, StringRef N = "")
98 : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
99 IsReplaceable(false), Name(N) {}
101 const unsigned SymbolKind : 8;
102 unsigned IsExternal : 1;
104 // This bit is used by the \c DefinedRegular subclass.
105 unsigned IsCOMDAT : 1;
107 // This bit is used by the \c DefinedBitcode subclass.
108 unsigned IsReplaceable : 1;
111 Symbol *Backref = nullptr;
114 // The base class for any defined symbols, including absolute symbols,
116 class Defined : public SymbolBody {
118 Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}
120 static bool classof(const SymbolBody *S) {
121 return S->kind() <= LastDefinedKind;
124 // Returns the RVA (relative virtual address) of this symbol. The
125 // writer sets and uses RVAs.
128 // Returns the RVA relative to the beginning of the output section.
129 // Used to implement SECREL relocation type.
130 uint64_t getSecrel();
132 // Returns the output section index.
133 // Used to implement SECTION relocation type.
134 uint64_t getSectionIndex();
136 // Returns true if this symbol points to an executable (e.g. .text) section.
137 // Used to implement ARM relocations.
141 // Symbols defined via a COFF object file.
142 class DefinedCOFF : public Defined {
145 DefinedCOFF(Kind K, ObjectFile *F, COFFSymbolRef S)
146 : Defined(K), File(F), Sym(S.getGeneric()) {}
148 static bool classof(const SymbolBody *S) {
149 return S->kind() <= LastDefinedCOFFKind;
152 int getFileIndex() { return File->Index; }
154 COFFSymbolRef getCOFFSymbol();
158 const coff_symbol_generic *Sym;
161 // Regular defined symbols read from object file symbol tables.
162 class DefinedRegular : public DefinedCOFF {
164 DefinedRegular(ObjectFile *F, COFFSymbolRef S, SectionChunk *C)
165 : DefinedCOFF(DefinedRegularKind, F, S), Data(&C->Repl) {
166 IsExternal = S.isExternal();
167 IsCOMDAT = C->isCOMDAT();
170 static bool classof(const SymbolBody *S) {
171 return S->kind() == DefinedRegularKind;
174 uint64_t getRVA() { return (*Data)->getRVA() + Sym->Value; }
175 bool isCOMDAT() { return IsCOMDAT; }
176 SectionChunk *getChunk() { return *Data; }
177 uint32_t getValue() { return Sym->Value; }
183 class DefinedCommon : public DefinedCOFF {
185 DefinedCommon(ObjectFile *F, COFFSymbolRef S, CommonChunk *C)
186 : DefinedCOFF(DefinedCommonKind, F, S), Data(C) {
187 IsExternal = S.isExternal();
190 static bool classof(const SymbolBody *S) {
191 return S->kind() == DefinedCommonKind;
194 uint64_t getRVA() { return Data->getRVA(); }
198 uint64_t getSize() { return Sym->Value; }
203 class DefinedAbsolute : public Defined {
205 DefinedAbsolute(StringRef N, COFFSymbolRef S)
206 : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
207 IsExternal = S.isExternal();
210 DefinedAbsolute(StringRef N, uint64_t V)
211 : Defined(DefinedAbsoluteKind, N), VA(V) {}
213 static bool classof(const SymbolBody *S) {
214 return S->kind() == DefinedAbsoluteKind;
217 uint64_t getRVA() { return VA - Config->ImageBase; }
218 void setVA(uint64_t V) { VA = V; }
224 // This is a kind of absolute symbol but relative to the image base.
225 // Unlike absolute symbols, relocations referring this kind of symbols
226 // are subject of the base relocation. This type is used rarely --
227 // mainly for __ImageBase.
228 class DefinedRelative : public Defined {
230 explicit DefinedRelative(StringRef Name, uint64_t V = 0)
231 : Defined(DefinedRelativeKind, Name), RVA(V) {}
233 static bool classof(const SymbolBody *S) {
234 return S->kind() == DefinedRelativeKind;
237 uint64_t getRVA() { return RVA; }
238 void setRVA(uint64_t V) { RVA = V; }
244 // This class represents a symbol defined in an archive file. It is
245 // created from an archive file header, and it knows how to load an
246 // object file from an archive to replace itself with a defined
247 // symbol. If the resolver finds both Undefined and Lazy for
248 // the same name, it will ask the Lazy to load a file.
249 class Lazy : public SymbolBody {
251 Lazy(ArchiveFile *F, const Archive::Symbol S)
252 : SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}
254 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
256 // Returns an object file for this symbol, or a nullptr if the file
257 // was already returned.
258 std::unique_ptr<InputFile> getMember();
260 int getFileIndex() { return File->Index; }
264 const Archive::Symbol Sym;
267 // Undefined symbols.
268 class Undefined : public SymbolBody {
270 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
272 static bool classof(const SymbolBody *S) {
273 return S->kind() == UndefinedKind;
276 // An undefined symbol can have a fallback symbol which gives an
277 // undefined symbol a second chance if it would remain undefined.
278 // If it remains undefined, it'll be replaced with whatever the
279 // Alias pointer points to.
280 SymbolBody *WeakAlias = nullptr;
282 // If this symbol is external weak, try to resolve it to a defined
283 // symbol by searching the chain of fallback symbols. Returns the symbol if
284 // successful, otherwise returns null.
285 Defined *getWeakAlias();
288 // Windows-specific classes.
290 // This class represents a symbol imported from a DLL. This has two
291 // names for internal use and external use. The former is used for
292 // name resolution, and the latter is used for the import descriptor
293 // table in an output. The former has "__imp_" prefix.
294 class DefinedImportData : public Defined {
296 DefinedImportData(StringRef D, StringRef N, StringRef E,
297 const coff_import_header *H)
298 : Defined(DefinedImportDataKind, N), DLLName(D), ExternalName(E), Hdr(H) {
301 static bool classof(const SymbolBody *S) {
302 return S->kind() == DefinedImportDataKind;
305 uint64_t getRVA() { return Location->getRVA(); }
306 StringRef getDLLName() { return DLLName; }
307 StringRef getExternalName() { return ExternalName; }
308 void setLocation(Chunk *AddressTable) { Location = AddressTable; }
309 uint16_t getOrdinal() { return Hdr->OrdinalHint; }
313 StringRef ExternalName;
314 const coff_import_header *Hdr;
315 Chunk *Location = nullptr;
318 // This class represents a symbol for a jump table entry which jumps
319 // to a function in a DLL. Linker are supposed to create such symbols
320 // without "__imp_" prefix for all function symbols exported from
321 // DLLs, so that you can call DLL functions as regular functions with
322 // a regular name. A function pointer is given as a DefinedImportData.
323 class DefinedImportThunk : public Defined {
325 DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
327 static bool classof(const SymbolBody *S) {
328 return S->kind() == DefinedImportThunkKind;
331 uint64_t getRVA() { return Data->getRVA(); }
332 Chunk *getChunk() { return Data.get(); }
335 std::unique_ptr<Chunk> Data;
338 // If you have a symbol "__imp_foo" in your object file, a symbol name
339 // "foo" becomes automatically available as a pointer to "__imp_foo".
340 // This class is for such automatically-created symbols.
341 // Yes, this is an odd feature. We didn't intend to implement that.
342 // This is here just for compatibility with MSVC.
343 class DefinedLocalImport : public Defined {
345 DefinedLocalImport(StringRef N, Defined *S)
346 : Defined(DefinedLocalImportKind, N), Data(S) {}
348 static bool classof(const SymbolBody *S) {
349 return S->kind() == DefinedLocalImportKind;
352 uint64_t getRVA() { return Data.getRVA(); }
353 Chunk *getChunk() { return &Data; }
356 LocalImportChunk Data;
359 class DefinedBitcode : public Defined {
362 DefinedBitcode(BitcodeFile *F, StringRef N, bool IsReplaceable)
363 : Defined(DefinedBitcodeKind, N), File(F) {
364 this->IsReplaceable = IsReplaceable;
367 static bool classof(const SymbolBody *S) {
368 return S->kind() == DefinedBitcodeKind;
375 inline uint64_t Defined::getRVA() {
377 case DefinedAbsoluteKind:
378 return cast<DefinedAbsolute>(this)->getRVA();
379 case DefinedRelativeKind:
380 return cast<DefinedRelative>(this)->getRVA();
381 case DefinedImportDataKind:
382 return cast<DefinedImportData>(this)->getRVA();
383 case DefinedImportThunkKind:
384 return cast<DefinedImportThunk>(this)->getRVA();
385 case DefinedLocalImportKind:
386 return cast<DefinedLocalImport>(this)->getRVA();
387 case DefinedCommonKind:
388 return cast<DefinedCommon>(this)->getRVA();
389 case DefinedRegularKind:
390 return cast<DefinedRegular>(this)->getRVA();
391 case DefinedBitcodeKind:
392 llvm_unreachable("There is no address for a bitcode symbol.");
395 llvm_unreachable("Cannot get the address for an undefined symbol.");
397 llvm_unreachable("unknown symbol kind");