blob: 1ca7366364d795dfeaa7b6483075b37fc972f443 [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:541//===- Symbols.h ------------------------------------------------*- C++ -*-===//
Rui Ueyama411c63602015-05-28 19:09:302//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLD_COFF_SYMBOLS_H
11#define LLD_COFF_SYMBOLS_H
12
13#include "Chunks.h"
14#include "Config.h"
Rui Ueyama9381eb12016-12-18 14:06:0615#include "Memory.h"
Rui Ueyama411c63602015-05-28 19:09:3016#include "lld/Core/LLVM.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/Object/Archive.h"
19#include "llvm/Object/COFF.h"
Rui Ueyamac80c03d2015-07-05 21:54:4220#include <atomic>
Rui Ueyama411c63602015-05-28 19:09:3021#include <memory>
22#include <vector>
23
24namespace lld {
25namespace coff {
26
27using llvm::object::Archive;
28using llvm::object::COFFSymbolRef;
Rui Ueyamac9bfe322015-05-29 15:45:3529using llvm::object::coff_import_header;
Rui Ueyamac15139b2015-06-30 00:10:5430using llvm::object::coff_symbol_generic;
Rui Ueyama411c63602015-05-28 19:09:3031
32class ArchiveFile;
Peter Collingbournef7b27d12015-06-30 00:47:5233class BitcodeFile;
Rui Ueyama411c63602015-05-28 19:09:3034class InputFile;
35class ObjectFile;
Peter Collingbourne79a5e6b2016-12-09 21:55:2436struct Symbol;
37class SymbolTable;
Rui Ueyama411c63602015-05-28 19:09:3038
39// The base class for real symbol classes.
40class SymbolBody {
41public:
42 enum Kind {
Chandler Carruth64c17c72015-06-29 21:35:4843 // 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
47 // over another.
48 DefinedRegularKind = 0,
Rui Ueyamaefb7e1a2015-06-20 07:21:5749 DefinedCommonKind,
Chandler Carruth64c17c72015-06-29 21:35:4850 DefinedLocalImportKind,
51 DefinedImportThunkKind,
52 DefinedImportDataKind,
53 DefinedAbsoluteKind,
Rui Ueyama3cb895c2015-07-24 22:58:4454 DefinedRelativeKind,
Chandler Carruth64c17c72015-06-29 21:35:4855 DefinedBitcodeKind,
56
Rui Ueyamae2514762015-06-15 19:06:5357 UndefinedKind,
Rui Ueyama8d3010a2015-06-30 19:35:2158 LazyKind,
Chandler Carruth64c17c72015-06-29 21:35:4859
60 LastDefinedCOFFKind = DefinedCommonKind,
61 LastDefinedKind = DefinedBitcodeKind,
Rui Ueyama411c63602015-05-28 19:09:3062 };
63
Chandler Carruth64c17c72015-06-29 21:35:4864 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Rui Ueyama411c63602015-05-28 19:09:3065
66 // Returns true if this is an external symbol.
Chandler Carruth64c17c72015-06-29 21:35:4867 bool isExternal() { return IsExternal; }
Rui Ueyama411c63602015-05-28 19:09:3068
69 // Returns the symbol name.
Chandler Carruth64c17c72015-06-29 21:35:4870 StringRef getName();
Rui Ueyama411c63602015-05-28 19:09:3071
Rui Ueyamaa45d45e2016-12-07 23:17:0272 // Returns the file from which this symbol was created.
73 InputFile *getFile();
74
Peter Collingbourne79a5e6b2016-12-09 21:55:2475 Symbol *symbol();
76 const Symbol *symbol() const {
77 return const_cast<SymbolBody *>(this)->symbol();
78 }
Rui Ueyama411c63602015-05-28 19:09:3079
80protected:
Peter Collingbourne79a5e6b2016-12-09 21:55:2481 friend SymbolTable;
Chandler Carruth64c17c72015-06-29 21:35:4882 explicit SymbolBody(Kind K, StringRef N = "")
83 : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
Peter Collingbourne99111282016-12-11 22:15:2084 IsReplaceable(false), WrittenToSymtab(false), Name(N) {}
Rui Ueyama411c63602015-05-28 19:09:3085
Chandler Carruth64c17c72015-06-29 21:35:4886 const unsigned SymbolKind : 8;
87 unsigned IsExternal : 1;
88
89 // This bit is used by the \c DefinedRegular subclass.
90 unsigned IsCOMDAT : 1;
91
92 // This bit is used by the \c DefinedBitcode subclass.
93 unsigned IsReplaceable : 1;
94
Peter Collingbourne99111282016-12-11 22:15:2095public:
96 // This bit is used by Writer::createSymbolAndStringTable().
97 unsigned WrittenToSymtab : 1;
98
99protected:
Chandler Carruth64c17c72015-06-29 21:35:48100 StringRef Name;
Rui Ueyama411c63602015-05-28 19:09:30101};
102
103// The base class for any defined symbols, including absolute symbols,
104// etc.
105class Defined : public SymbolBody {
106public:
Chandler Carruth64c17c72015-06-29 21:35:48107 Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}
Rui Ueyama411c63602015-05-28 19:09:30108
109 static bool classof(const SymbolBody *S) {
Chandler Carruth64c17c72015-06-29 21:35:48110 return S->kind() <= LastDefinedKind;
Rui Ueyama411c63602015-05-28 19:09:30111 }
112
113 // Returns the RVA (relative virtual address) of this symbol. The
114 // writer sets and uses RVAs.
Chandler Carruth64c17c72015-06-29 21:35:48115 uint64_t getRVA();
Rui Ueyama411c63602015-05-28 19:09:30116
Rui Ueyamaeb26e1d2015-07-29 16:30:45117 // Returns the RVA relative to the beginning of the output section.
118 // Used to implement SECREL relocation type.
119 uint64_t getSecrel();
120
121 // Returns the output section index.
122 // Used to implement SECTION relocation type.
123 uint64_t getSectionIndex();
Rui Ueyama8bc43a12015-07-29 19:25:00124
125 // Returns true if this symbol points to an executable (e.g. .text) section.
126 // Used to implement ARM relocations.
127 bool isExecutable();
Chandler Carruth64c17c72015-06-29 21:35:48128};
Rui Ueyama411c63602015-05-28 19:09:30129
Chandler Carruth64c17c72015-06-29 21:35:48130// Symbols defined via a COFF object file.
131class DefinedCOFF : public Defined {
132 friend SymbolBody;
133public:
134 DefinedCOFF(Kind K, ObjectFile *F, COFFSymbolRef S)
Rui Ueyamac15139b2015-06-30 00:10:54135 : Defined(K), File(F), Sym(S.getGeneric()) {}
Chandler Carruth64c17c72015-06-29 21:35:48136
137 static bool classof(const SymbolBody *S) {
138 return S->kind() <= LastDefinedCOFFKind;
139 }
140
Rui Ueyamaa45d45e2016-12-07 23:17:02141 ObjectFile *getFile() { return File; }
Rui Ueyama65813ed2015-07-02 20:33:48142
David Majnemer3a62d3d2015-07-09 17:43:50143 COFFSymbolRef getCOFFSymbol();
144
Chandler Carruth64c17c72015-06-29 21:35:48145 ObjectFile *File;
Rui Ueyamaa45d45e2016-12-07 23:17:02146
147protected:
Rui Ueyamac15139b2015-06-30 00:10:54148 const coff_symbol_generic *Sym;
Rui Ueyama411c63602015-05-28 19:09:30149};
150
151// Regular defined symbols read from object file symbol tables.
Chandler Carruth64c17c72015-06-29 21:35:48152class DefinedRegular : public DefinedCOFF {
Rui Ueyama411c63602015-05-28 19:09:30153public:
Rui Ueyama68633f12015-06-25 23:22:00154 DefinedRegular(ObjectFile *F, COFFSymbolRef S, SectionChunk *C)
Rui Ueyamade880722015-09-25 16:20:24155 : DefinedCOFF(DefinedRegularKind, F, S), Data(&C->Repl) {
Rui Ueyamac15139b2015-06-30 00:10:54156 IsExternal = S.isExternal();
Chandler Carruth64c17c72015-06-29 21:35:48157 IsCOMDAT = C->isCOMDAT();
158 }
Rui Ueyama411c63602015-05-28 19:09:30159
160 static bool classof(const SymbolBody *S) {
161 return S->kind() == DefinedRegularKind;
162 }
163
Rui Ueyamac15139b2015-06-30 00:10:54164 uint64_t getRVA() { return (*Data)->getRVA() + Sym->Value; }
Rui Ueyama9b921e52015-06-25 22:00:42165 bool isCOMDAT() { return IsCOMDAT; }
Chandler Carruth59013c32015-06-29 21:12:49166 SectionChunk *getChunk() { return *Data; }
Rui Ueyamac15139b2015-06-30 00:10:54167 uint32_t getValue() { return Sym->Value; }
Rui Ueyama411c63602015-05-28 19:09:30168
Rui Ueyamaefb7e1a2015-06-20 07:21:57169private:
Rui Ueyama9b921e52015-06-25 22:00:42170 SectionChunk **Data;
Rui Ueyamaefb7e1a2015-06-20 07:21:57171};
172
Chandler Carruth64c17c72015-06-29 21:35:48173class DefinedCommon : public DefinedCOFF {
Rui Ueyamaefb7e1a2015-06-20 07:21:57174public:
Rui Ueyama68633f12015-06-25 23:22:00175 DefinedCommon(ObjectFile *F, COFFSymbolRef S, CommonChunk *C)
Chandler Carruth64c17c72015-06-29 21:35:48176 : DefinedCOFF(DefinedCommonKind, F, S), Data(C) {
Rui Ueyamac15139b2015-06-30 00:10:54177 IsExternal = S.isExternal();
Chandler Carruth64c17c72015-06-29 21:35:48178 }
Rui Ueyamaefb7e1a2015-06-20 07:21:57179
180 static bool classof(const SymbolBody *S) {
181 return S->kind() == DefinedCommonKind;
182 }
183
Chandler Carruth64c17c72015-06-29 21:35:48184 uint64_t getRVA() { return Data->getRVA(); }
Rui Ueyama411c63602015-05-28 19:09:30185
186private:
Peter Collingbourne79a5e6b2016-12-09 21:55:24187 friend SymbolTable;
Rui Ueyamac15139b2015-06-30 00:10:54188 uint64_t getSize() { return Sym->Value; }
Rui Ueyamafc510f42015-06-25 19:10:58189 CommonChunk *Data;
Rui Ueyama411c63602015-05-28 19:09:30190};
191
192// Absolute symbols.
193class DefinedAbsolute : public Defined {
194public:
Rui Ueyamaccde19d2015-06-26 03:09:23195 DefinedAbsolute(StringRef N, COFFSymbolRef S)
Chandler Carruth64c17c72015-06-29 21:35:48196 : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
197 IsExternal = S.isExternal();
198 }
Rui Ueyamaccde19d2015-06-26 03:09:23199
200 DefinedAbsolute(StringRef N, uint64_t V)
Chandler Carruth64c17c72015-06-29 21:35:48201 : Defined(DefinedAbsoluteKind, N), VA(V) {}
Rui Ueyama411c63602015-05-28 19:09:30202
203 static bool classof(const SymbolBody *S) {
204 return S->kind() == DefinedAbsoluteKind;
205 }
206
Chandler Carruth64c17c72015-06-29 21:35:48207 uint64_t getRVA() { return VA - Config->ImageBase; }
Rui Ueyamacd3f99b2015-07-24 23:51:14208 void setVA(uint64_t V) { VA = V; }
Rui Ueyama411c63602015-05-28 19:09:30209
210private:
Rui Ueyamaccde19d2015-06-26 03:09:23211 uint64_t VA;
Rui Ueyama411c63602015-05-28 19:09:30212};
213
Rui Ueyama3cb895c2015-07-24 22:58:44214// 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.
218class DefinedRelative : public Defined {
219public:
220 explicit DefinedRelative(StringRef Name, uint64_t V = 0)
221 : Defined(DefinedRelativeKind, Name), RVA(V) {}
222
223 static bool classof(const SymbolBody *S) {
224 return S->kind() == DefinedRelativeKind;
225 }
226
227 uint64_t getRVA() { return RVA; }
228 void setRVA(uint64_t V) { RVA = V; }
229
230private:
231 uint64_t RVA;
232};
233
Rui Ueyama411c63602015-05-28 19:09:30234// 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.
239class Lazy : public SymbolBody {
240public:
241 Lazy(ArchiveFile *F, const Archive::Symbol S)
Chandler Carruth64c17c72015-06-29 21:35:48242 : SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}
Rui Ueyama411c63602015-05-28 19:09:30243
244 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
245
Rui Ueyama411c63602015-05-28 19:09:30246 ArchiveFile *File;
Rui Ueyamaa45d45e2016-12-07 23:17:02247
248private:
Peter Collingbourne79a5e6b2016-12-09 21:55:24249 friend SymbolTable;
250
251private:
Rui Ueyama411c63602015-05-28 19:09:30252 const Archive::Symbol Sym;
253};
254
255// Undefined symbols.
256class Undefined : public SymbolBody {
257public:
Rui Ueyama48975962015-07-01 22:32:23258 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
Rui Ueyama411c63602015-05-28 19:09:30259
260 static bool classof(const SymbolBody *S) {
261 return S->kind() == UndefinedKind;
262 }
263
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.
Peter Collingbourneda2f0942015-07-03 22:03:36268 SymbolBody *WeakAlias = nullptr;
Peter Collingbourne2612a322015-07-04 05:28:41269
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();
Rui Ueyama411c63602015-05-28 19:09:30274};
275
276// Windows-specific classes.
277
Rui Ueyama7c4fcdd2015-05-29 15:49:09278// 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.
282class DefinedImportData : public Defined {
283public:
Peter Collingbourne79a5e6b2016-12-09 21:55:24284 DefinedImportData(StringRef N, ImportFile *F)
285 : Defined(DefinedImportDataKind, N), File(F) {
Chandler Carruth64c17c72015-06-29 21:35:48286 }
Rui Ueyama7c4fcdd2015-05-29 15:49:09287
288 static bool classof(const SymbolBody *S) {
289 return S->kind() == DefinedImportDataKind;
290 }
291
Peter Collingbourne79a5e6b2016-12-09 21:55:24292 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; }
Rui Ueyama7c4fcdd2015-05-29 15:49:09297
298private:
Peter Collingbourne79a5e6b2016-12-09 21:55:24299 ImportFile *File;
Rui Ueyama7c4fcdd2015-05-29 15:49:09300};
301
Rui Ueyama411c63602015-05-28 19:09:30302// 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.
307class DefinedImportThunk : public Defined {
308public:
Rui Ueyama5e706b32015-07-25 21:54:50309 DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
Rui Ueyama411c63602015-05-28 19:09:30310
311 static bool classof(const SymbolBody *S) {
312 return S->kind() == DefinedImportThunkKind;
313 }
314
Rui Ueyama28df0422015-07-25 01:16:06315 uint64_t getRVA() { return Data->getRVA(); }
Peter Collingbournee50f4852016-12-12 18:42:09316 Chunk *getChunk() { return Data; }
Rui Ueyama411c63602015-05-28 19:09:30317
318private:
Peter Collingbournee50f4852016-12-12 18:42:09319 Chunk *Data;
Rui Ueyama411c63602015-05-28 19:09:30320};
321
Rui Ueyama88e0f922015-06-25 03:31:47322// 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.
327class DefinedLocalImport : public Defined {
328public:
329 DefinedLocalImport(StringRef N, Defined *S)
Peter Collingbournee50f4852016-12-12 18:42:09330 : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
Rui Ueyama88e0f922015-06-25 03:31:47331
332 static bool classof(const SymbolBody *S) {
333 return S->kind() == DefinedLocalImportKind;
334 }
335
Peter Collingbourne79a5e6b2016-12-09 21:55:24336 uint64_t getRVA() { return Data->getRVA(); }
Peter Collingbournee50f4852016-12-12 18:42:09337 Chunk *getChunk() { return Data; }
Rui Ueyama88e0f922015-06-25 03:31:47338
339private:
Peter Collingbournee50f4852016-12-12 18:42:09340 LocalImportChunk *Data;
Rui Ueyama88e0f922015-06-25 03:31:47341};
342
Peter Collingbourne60c16162015-06-01 20:10:10343class DefinedBitcode : public Defined {
Peter Collingbournef7b27d12015-06-30 00:47:52344 friend SymbolBody;
Peter Collingbourne60c16162015-06-01 20:10:10345public:
Peter Collingbournef7b27d12015-06-30 00:47:52346 DefinedBitcode(BitcodeFile *F, StringRef N, bool IsReplaceable)
347 : Defined(DefinedBitcodeKind, N), File(F) {
Peter Collingbourne79a5e6b2016-12-09 21:55:24348 // 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.
Chandler Carruth64c17c72015-06-29 21:35:48353 this->IsReplaceable = IsReplaceable;
354 }
Peter Collingbourne60c16162015-06-01 20:10:10355
356 static bool classof(const SymbolBody *S) {
357 return S->kind() == DefinedBitcodeKind;
358 }
Peter Collingbournef7b27d12015-06-30 00:47:52359
Peter Collingbournef7b27d12015-06-30 00:47:52360 BitcodeFile *File;
Peter Collingbourne60c16162015-06-01 20:10:10361};
362
Rui Ueyamacb71c722015-07-13 22:01:27363inline uint64_t Defined::getRVA() {
364 switch (kind()) {
365 case DefinedAbsoluteKind:
366 return cast<DefinedAbsolute>(this)->getRVA();
Rui Ueyama3cb895c2015-07-24 22:58:44367 case DefinedRelativeKind:
368 return cast<DefinedRelative>(this)->getRVA();
Rui Ueyamacb71c722015-07-13 22:01:27369 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.");
381 case LazyKind:
382 case UndefinedKind:
383 llvm_unreachable("Cannot get the address for an undefined symbol.");
384 }
385 llvm_unreachable("unknown symbol kind");
386}
387
Peter Collingbourne79a5e6b2016-12-09 21:55:24388// 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.
392struct Symbol {
393 // True if this symbol was referenced by a regular (non-bitcode) object.
394 unsigned IsUsedInRegularObj : 1;
395
Peter Collingbourne6ee0b4e2016-12-15 04:02:23396 // 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;
400
Peter Collingbourne79a5e6b2016-12-09 21:55:24401 // 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>
408 Body;
409
410 SymbolBody *body() {
411 return reinterpret_cast<SymbolBody *>(Body.buffer);
412 }
413 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
414};
415
416template <typename T, typename... ArgT>
417void 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 &&
422 "Not a SymbolBody");
423 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
424}
425
426inline Symbol *SymbolBody::symbol() {
427 assert(isExternal());
428 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
429 offsetof(Symbol, Body));
430}
Rui Ueyama411c63602015-05-28 19:09:30431} // namespace coff
Rui Ueyamace039262017-01-06 10:04:08432
433std::string toString(coff::SymbolBody &B);
Rui Ueyama411c63602015-05-28 19:09:30434} // namespace lld
435
Rui Ueyama411c63602015-05-28 19:09:30436#endif