]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/Symbols.h
Merge clang 7.0.1 and several follow-up changes
[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 "lld/Common/LLVM.h"
16 #include "lld/Common/Memory.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 InputFile;
34 class ObjFile;
35 class SymbolTable;
36
37 // The base class for real symbol classes.
38 class Symbol {
39 public:
40   enum Kind {
41     // The order of these is significant. We start with the regular defined
42     // symbols as those are the most prevelant and the zero tag is the cheapest
43     // to set. Among the defined kinds, the lower the kind is preferred over
44     // the higher kind when testing wether one symbol should take precedence
45     // over another.
46     DefinedRegularKind = 0,
47     DefinedCommonKind,
48     DefinedLocalImportKind,
49     DefinedImportThunkKind,
50     DefinedImportDataKind,
51     DefinedAbsoluteKind,
52     DefinedSyntheticKind,
53
54     UndefinedKind,
55     LazyKind,
56
57     LastDefinedCOFFKind = DefinedCommonKind,
58     LastDefinedKind = DefinedSyntheticKind,
59   };
60
61   Kind kind() const { return static_cast<Kind>(SymbolKind); }
62
63   // Returns true if this is an external symbol.
64   bool isExternal() { return IsExternal; }
65
66   // Returns the symbol name.
67   StringRef getName();
68
69   // Returns the file from which this symbol was created.
70   InputFile *getFile();
71
72   // Indicates that this symbol will be included in the final image. Only valid
73   // after calling markLive.
74   bool isLive() const;
75
76 protected:
77   friend SymbolTable;
78   explicit Symbol(Kind K, StringRef N = "")
79       : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
80         WrittenToSymtab(false), PendingArchiveLoad(false), IsGCRoot(false),
81         Name(N) {}
82
83   const unsigned SymbolKind : 8;
84   unsigned IsExternal : 1;
85
86   // This bit is used by the \c DefinedRegular subclass.
87   unsigned IsCOMDAT : 1;
88
89 public:
90   // This bit is used by Writer::createSymbolAndStringTable() to prevent
91   // symbols from being written to the symbol table more than once.
92   unsigned WrittenToSymtab : 1;
93
94   // True if this symbol was referenced by a regular (non-bitcode) object.
95   unsigned IsUsedInRegularObj : 1;
96
97   // True if we've seen both a lazy and an undefined symbol with this symbol
98   // name, which means that we have enqueued an archive member load and should
99   // not load any more archive members to resolve the same symbol.
100   unsigned PendingArchiveLoad : 1;
101
102   /// True if we've already added this symbol to the list of GC roots.
103   unsigned IsGCRoot : 1;
104
105 protected:
106   StringRef Name;
107 };
108
109 // The base class for any defined symbols, including absolute symbols,
110 // etc.
111 class Defined : public Symbol {
112 public:
113   Defined(Kind K, StringRef N) : Symbol(K, N) {}
114
115   static bool classof(const Symbol *S) { return S->kind() <= LastDefinedKind; }
116
117   // Returns the RVA (relative virtual address) of this symbol. The
118   // writer sets and uses RVAs.
119   uint64_t getRVA();
120
121   // Returns the chunk containing this symbol. Absolute symbols and __ImageBase
122   // do not have chunks, so this may return null.
123   Chunk *getChunk();
124 };
125
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
129 // as a StringRef.
130 class DefinedCOFF : public Defined {
131   friend Symbol;
132
133 public:
134   DefinedCOFF(Kind K, InputFile *F, StringRef N, const coff_symbol_generic *S)
135       : Defined(K, N), File(F), Sym(S) {}
136
137   static bool classof(const Symbol *S) {
138     return S->kind() <= LastDefinedCOFFKind;
139   }
140
141   InputFile *getFile() { return File; }
142
143   COFFSymbolRef getCOFFSymbol();
144
145   InputFile *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(InputFile *F, StringRef N, bool IsCOMDAT,
155                  bool IsExternal = false,
156                  const coff_symbol_generic *S = nullptr,
157                  SectionChunk *C = nullptr)
158       : DefinedCOFF(DefinedRegularKind, F, N, S), Data(C ? &C->Repl : nullptr) {
159     this->IsExternal = IsExternal;
160     this->IsCOMDAT = IsCOMDAT;
161   }
162
163   static bool classof(const Symbol *S) {
164     return S->kind() == DefinedRegularKind;
165   }
166
167   uint64_t getRVA() const { return (*Data)->getRVA() + Sym->Value; }
168   bool isCOMDAT() const { return IsCOMDAT; }
169   SectionChunk *getChunk() const { return *Data; }
170   uint32_t getValue() const { return Sym->Value; }
171
172   SectionChunk **Data;
173 };
174
175 class DefinedCommon : public DefinedCOFF {
176 public:
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;
182   }
183
184   static bool classof(const Symbol *S) {
185     return S->kind() == DefinedCommonKind;
186   }
187
188   uint64_t getRVA() { return Data->getRVA(); }
189   CommonChunk *getChunk() { return Data; }
190
191 private:
192   friend SymbolTable;
193   uint64_t getSize() const { return Size; }
194   CommonChunk *Data;
195   uint64_t Size;
196 };
197
198 // Absolute symbols.
199 class DefinedAbsolute : public Defined {
200 public:
201   DefinedAbsolute(StringRef N, COFFSymbolRef S)
202       : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
203     IsExternal = S.isExternal();
204   }
205
206   DefinedAbsolute(StringRef N, uint64_t V)
207       : Defined(DefinedAbsoluteKind, N), VA(V) {}
208
209   static bool classof(const Symbol *S) {
210     return S->kind() == DefinedAbsoluteKind;
211   }
212
213   uint64_t getRVA() { return VA - Config->ImageBase; }
214   void setVA(uint64_t V) { VA = V; }
215
216   // Section index relocations against absolute symbols resolve to
217   // this 16 bit number, and it is the largest valid section index
218   // plus one. This variable keeps it.
219   static uint16_t NumOutputSections;
220
221 private:
222   uint64_t VA;
223 };
224
225 // This symbol is used for linker-synthesized symbols like __ImageBase and
226 // __safe_se_handler_table.
227 class DefinedSynthetic : public Defined {
228 public:
229   explicit DefinedSynthetic(StringRef Name, Chunk *C)
230       : Defined(DefinedSyntheticKind, Name), C(C) {}
231
232   static bool classof(const Symbol *S) {
233     return S->kind() == DefinedSyntheticKind;
234   }
235
236   // A null chunk indicates that this is __ImageBase. Otherwise, this is some
237   // other synthesized chunk, like SEHTableChunk.
238   uint32_t getRVA() { return C ? C->getRVA() : 0; }
239   Chunk *getChunk() { return C; }
240
241 private:
242   Chunk *C;
243 };
244
245 // This class represents a symbol defined in an archive file. It is
246 // created from an archive file header, and it knows how to load an
247 // object file from an archive to replace itself with a defined
248 // symbol. If the resolver finds both Undefined and Lazy for
249 // the same name, it will ask the Lazy to load a file.
250 class Lazy : public Symbol {
251 public:
252   Lazy(ArchiveFile *F, const Archive::Symbol S)
253       : Symbol(LazyKind, S.getName()), File(F), Sym(S) {}
254
255   static bool classof(const Symbol *S) { return S->kind() == LazyKind; }
256
257   ArchiveFile *File;
258
259 private:
260   friend SymbolTable;
261
262 private:
263   const Archive::Symbol Sym;
264 };
265
266 // Undefined symbols.
267 class Undefined : public Symbol {
268 public:
269   explicit Undefined(StringRef N) : Symbol(UndefinedKind, N) {}
270
271   static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
272
273   // An undefined symbol can have a fallback symbol which gives an
274   // undefined symbol a second chance if it would remain undefined.
275   // If it remains undefined, it'll be replaced with whatever the
276   // Alias pointer points to.
277   Symbol *WeakAlias = nullptr;
278
279   // If this symbol is external weak, try to resolve it to a defined
280   // symbol by searching the chain of fallback symbols. Returns the symbol if
281   // successful, otherwise returns null.
282   Defined *getWeakAlias();
283 };
284
285 // Windows-specific classes.
286
287 // This class represents a symbol imported from a DLL. This has two
288 // names for internal use and external use. The former is used for
289 // name resolution, and the latter is used for the import descriptor
290 // table in an output. The former has "__imp_" prefix.
291 class DefinedImportData : public Defined {
292 public:
293   DefinedImportData(StringRef N, ImportFile *F)
294       : Defined(DefinedImportDataKind, N), File(F) {
295   }
296
297   static bool classof(const Symbol *S) {
298     return S->kind() == DefinedImportDataKind;
299   }
300
301   uint64_t getRVA() { return File->Location->getRVA(); }
302   Chunk *getChunk() { return File->Location; }
303   void setLocation(Chunk *AddressTable) { File->Location = AddressTable; }
304
305   StringRef getDLLName() { return File->DLLName; }
306   StringRef getExternalName() { return File->ExternalName; }
307   uint16_t getOrdinal() { return File->Hdr->OrdinalHint; }
308
309   ImportFile *File;
310 };
311
312 // This class represents a symbol for a jump table entry which jumps
313 // to a function in a DLL. Linker are supposed to create such symbols
314 // without "__imp_" prefix for all function symbols exported from
315 // DLLs, so that you can call DLL functions as regular functions with
316 // a regular name. A function pointer is given as a DefinedImportData.
317 class DefinedImportThunk : public Defined {
318 public:
319   DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
320
321   static bool classof(const Symbol *S) {
322     return S->kind() == DefinedImportThunkKind;
323   }
324
325   uint64_t getRVA() { return Data->getRVA(); }
326   Chunk *getChunk() { return Data; }
327
328   DefinedImportData *WrappedSym;
329
330 private:
331   Chunk *Data;
332 };
333
334 // If you have a symbol "__imp_foo" in your object file, a symbol name
335 // "foo" becomes automatically available as a pointer to "__imp_foo".
336 // This class is for such automatically-created symbols.
337 // Yes, this is an odd feature. We didn't intend to implement that.
338 // This is here just for compatibility with MSVC.
339 class DefinedLocalImport : public Defined {
340 public:
341   DefinedLocalImport(StringRef N, Defined *S)
342       : Defined(DefinedLocalImportKind, N), Data(make<LocalImportChunk>(S)) {}
343
344   static bool classof(const Symbol *S) {
345     return S->kind() == DefinedLocalImportKind;
346   }
347
348   uint64_t getRVA() { return Data->getRVA(); }
349   Chunk *getChunk() { return Data; }
350
351 private:
352   LocalImportChunk *Data;
353 };
354
355 inline uint64_t Defined::getRVA() {
356   switch (kind()) {
357   case DefinedAbsoluteKind:
358     return cast<DefinedAbsolute>(this)->getRVA();
359   case DefinedSyntheticKind:
360     return cast<DefinedSynthetic>(this)->getRVA();
361   case DefinedImportDataKind:
362     return cast<DefinedImportData>(this)->getRVA();
363   case DefinedImportThunkKind:
364     return cast<DefinedImportThunk>(this)->getRVA();
365   case DefinedLocalImportKind:
366     return cast<DefinedLocalImport>(this)->getRVA();
367   case DefinedCommonKind:
368     return cast<DefinedCommon>(this)->getRVA();
369   case DefinedRegularKind:
370     return cast<DefinedRegular>(this)->getRVA();
371   case LazyKind:
372   case UndefinedKind:
373     llvm_unreachable("Cannot get the address for an undefined symbol.");
374   }
375   llvm_unreachable("unknown symbol kind");
376 }
377
378 inline Chunk *Defined::getChunk() {
379   switch (kind()) {
380   case DefinedRegularKind:
381     return cast<DefinedRegular>(this)->getChunk();
382   case DefinedAbsoluteKind:
383     return nullptr;
384   case DefinedSyntheticKind:
385     return cast<DefinedSynthetic>(this)->getChunk();
386   case DefinedImportDataKind:
387     return cast<DefinedImportData>(this)->getChunk();
388   case DefinedImportThunkKind:
389     return cast<DefinedImportThunk>(this)->getChunk();
390   case DefinedLocalImportKind:
391     return cast<DefinedLocalImport>(this)->getChunk();
392   case DefinedCommonKind:
393     return cast<DefinedCommon>(this)->getChunk();
394   case LazyKind:
395   case UndefinedKind:
396     llvm_unreachable("Cannot get the chunk of an undefined symbol.");
397   }
398   llvm_unreachable("unknown symbol kind");
399 }
400
401 // A buffer class that is large enough to hold any Symbol-derived
402 // object. We allocate memory using this class and instantiate a symbol
403 // using the placement new.
404 union SymbolUnion {
405   alignas(DefinedRegular) char A[sizeof(DefinedRegular)];
406   alignas(DefinedCommon) char B[sizeof(DefinedCommon)];
407   alignas(DefinedAbsolute) char C[sizeof(DefinedAbsolute)];
408   alignas(DefinedSynthetic) char D[sizeof(DefinedSynthetic)];
409   alignas(Lazy) char E[sizeof(Lazy)];
410   alignas(Undefined) char F[sizeof(Undefined)];
411   alignas(DefinedImportData) char G[sizeof(DefinedImportData)];
412   alignas(DefinedImportThunk) char H[sizeof(DefinedImportThunk)];
413   alignas(DefinedLocalImport) char I[sizeof(DefinedLocalImport)];
414 };
415
416 template <typename T, typename... ArgT>
417 void replaceSymbol(Symbol *S, ArgT &&... Arg) {
418   static_assert(std::is_trivially_destructible<T>(),
419                 "Symbol types must be trivially destructible");
420   static_assert(sizeof(T) <= sizeof(SymbolUnion), "Symbol too small");
421   static_assert(alignof(T) <= alignof(SymbolUnion),
422                 "SymbolUnion not aligned enough");
423   assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
424          "Not a Symbol");
425   new (S) T(std::forward<ArgT>(Arg)...);
426 }
427 } // namespace coff
428
429 std::string toString(coff::Symbol &B);
430 } // namespace lld
431
432 #endif