]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/Symbols.h
Update lld to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / Symbols.h
1 //===- Symbols.h ------------------------------------------------*- C++ -*-===//
2 //
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"
15 #include "Memory.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"
20 #include <atomic>
21 #include <memory>
22 #include <vector>
23
24 namespace lld {
25 namespace coff {
26
27 using llvm::object::Archive;
28 using llvm::object::COFFSymbolRef;
29 using llvm::object::coff_import_header;
30 using llvm::object::coff_symbol_generic;
31
32 class ArchiveFile;
33 class BitcodeFile;
34 class InputFile;
35 class ObjectFile;
36 struct Symbol;
37 class SymbolTable;
38
39 // The base class for real symbol classes.
40 class SymbolBody {
41 public:
42   enum Kind {
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
47     // over another.
48     DefinedRegularKind = 0,
49     DefinedCommonKind,
50     DefinedLocalImportKind,
51     DefinedImportThunkKind,
52     DefinedImportDataKind,
53     DefinedAbsoluteKind,
54     DefinedRelativeKind,
55     DefinedBitcodeKind,
56
57     UndefinedKind,
58     LazyKind,
59
60     LastDefinedCOFFKind = DefinedCommonKind,
61     LastDefinedKind = DefinedBitcodeKind,
62   };
63
64   Kind kind() const { return static_cast<Kind>(SymbolKind); }
65
66   // Returns true if this is an external symbol.
67   bool isExternal() { return IsExternal; }
68
69   // Returns the symbol name.
70   StringRef getName();
71
72   // Returns the file from which this symbol was created.
73   InputFile *getFile();
74
75   Symbol *symbol();
76   const Symbol *symbol() const {
77     return const_cast<SymbolBody *>(this)->symbol();
78   }
79
80 protected:
81   friend SymbolTable;
82   explicit SymbolBody(Kind K, StringRef N = "")
83       : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
84         IsReplaceable(false), WrittenToSymtab(false), Name(N) {}
85
86   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
95 public:
96   // This bit is used by Writer::createSymbolAndStringTable().
97   unsigned WrittenToSymtab : 1;
98
99 protected:
100   StringRef Name;
101 };
102
103 // The base class for any defined symbols, including absolute symbols,
104 // etc.
105 class Defined : public SymbolBody {
106 public:
107   Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}
108
109   static bool classof(const SymbolBody *S) {
110     return S->kind() <= LastDefinedKind;
111   }
112
113   // Returns the RVA (relative virtual address) of this symbol. The
114   // writer sets and uses RVAs.
115   uint64_t getRVA();
116
117   // 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();
124
125   // Returns true if this symbol points to an executable (e.g. .text) section.
126   // Used to implement ARM relocations.
127   bool isExecutable();
128 };
129
130 // Symbols defined via a COFF object file.
131 class DefinedCOFF : public Defined {
132   friend SymbolBody;
133 public:
134   DefinedCOFF(Kind K, ObjectFile *F, COFFSymbolRef S)
135       : Defined(K), File(F), Sym(S.getGeneric()) {}
136
137   static bool classof(const SymbolBody *S) {
138     return S->kind() <= LastDefinedCOFFKind;
139   }
140
141   ObjectFile *getFile() { return File; }
142
143   COFFSymbolRef getCOFFSymbol();
144
145   ObjectFile *File;
146
147 protected:
148   const coff_symbol_generic *Sym;
149 };
150
151 // Regular defined symbols read from object file symbol tables.
152 class DefinedRegular : public DefinedCOFF {
153 public:
154   DefinedRegular(ObjectFile *F, COFFSymbolRef S, SectionChunk *C)
155       : DefinedCOFF(DefinedRegularKind, F, S), Data(&C->Repl) {
156     IsExternal = S.isExternal();
157     IsCOMDAT = C->isCOMDAT();
158   }
159
160   static bool classof(const SymbolBody *S) {
161     return S->kind() == DefinedRegularKind;
162   }
163
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; }
168
169 private:
170   SectionChunk **Data;
171 };
172
173 class DefinedCommon : public DefinedCOFF {
174 public:
175   DefinedCommon(ObjectFile *F, COFFSymbolRef S, CommonChunk *C)
176       : DefinedCOFF(DefinedCommonKind, F, S), Data(C) {
177     IsExternal = S.isExternal();
178   }
179
180   static bool classof(const SymbolBody *S) {
181     return S->kind() == DefinedCommonKind;
182   }
183
184   uint64_t getRVA() { return Data->getRVA(); }
185
186 private:
187   friend SymbolTable;
188   uint64_t getSize() { return Sym->Value; }
189   CommonChunk *Data;
190 };
191
192 // Absolute symbols.
193 class DefinedAbsolute : public Defined {
194 public:
195   DefinedAbsolute(StringRef N, COFFSymbolRef S)
196       : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
197     IsExternal = S.isExternal();
198   }
199
200   DefinedAbsolute(StringRef N, uint64_t V)
201       : Defined(DefinedAbsoluteKind, N), VA(V) {}
202
203   static bool classof(const SymbolBody *S) {
204     return S->kind() == DefinedAbsoluteKind;
205   }
206
207   uint64_t getRVA() { return VA - Config->ImageBase; }
208   void setVA(uint64_t V) { VA = V; }
209
210 private:
211   uint64_t VA;
212 };
213
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 {
219 public:
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
230 private:
231   uint64_t RVA;
232 };
233
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 {
240 public:
241   Lazy(ArchiveFile *F, const Archive::Symbol S)
242       : SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}
243
244   static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
245
246   ArchiveFile *File;
247
248 private:
249   friend SymbolTable;
250
251 private:
252   const Archive::Symbol Sym;
253 };
254
255 // Undefined symbols.
256 class Undefined : public SymbolBody {
257 public:
258   explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
259
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.
268   SymbolBody *WeakAlias = nullptr;
269
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();
274 };
275
276 // Windows-specific classes.
277
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 {
283 public:
284   DefinedImportData(StringRef N, ImportFile *F)
285       : Defined(DefinedImportDataKind, N), File(F) {
286   }
287
288   static bool classof(const SymbolBody *S) {
289     return S->kind() == DefinedImportDataKind;
290   }
291
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; }
297
298 private:
299   ImportFile *File;
300 };
301
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 {
308 public:
309   DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
310
311   static bool classof(const SymbolBody *S) {
312     return S->kind() == DefinedImportThunkKind;
313   }
314
315   uint64_t getRVA() { return Data->getRVA(); }
316   Chunk *getChunk() { return Data; }
317
318 private:
319   Chunk *Data;
320 };
321
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 {
328 public:
329   DefinedLocalImport(StringRef N, Defined *S)
330       : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
331
332   static bool classof(const SymbolBody *S) {
333     return S->kind() == DefinedLocalImportKind;
334   }
335
336   uint64_t getRVA() { return Data->getRVA(); }
337   Chunk *getChunk() { return Data; }
338
339 private:
340   LocalImportChunk *Data;
341 };
342
343 class DefinedBitcode : public Defined {
344   friend SymbolBody;
345 public:
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;
354   }
355
356   static bool classof(const SymbolBody *S) {
357     return S->kind() == DefinedBitcodeKind;
358   }
359
360   BitcodeFile *File;
361 };
362
363 inline uint64_t Defined::getRVA() {
364   switch (kind()) {
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.");
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
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.
392 struct Symbol {
393   // True if this symbol was referenced by a regular (non-bitcode) object.
394   unsigned IsUsedInRegularObj : 1;
395
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;
400
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>
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
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 &&
422          "Not a SymbolBody");
423   new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
424 }
425
426 inline Symbol *SymbolBody::symbol() {
427   assert(isExternal());
428   return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
429                                     offsetof(Symbol, Body));
430 }
431
432 std::string toString(SymbolBody &B);
433
434 } // namespace coff
435 } // namespace lld
436
437 #endif