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