]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/Symbols.h
Bring lld (release_39 branch, r279477) to contrib
[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/Core/LLVM.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Object/COFF.h"
19 #include <atomic>
20 #include <memory>
21 #include <vector>
22
23 namespace lld {
24 namespace coff {
25
26 using llvm::object::Archive;
27 using llvm::object::COFFSymbolRef;
28 using llvm::object::coff_import_header;
29 using llvm::object::coff_symbol_generic;
30
31 class ArchiveFile;
32 class BitcodeFile;
33 class InputFile;
34 class ObjectFile;
35 class SymbolBody;
36
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.
40 struct Symbol {
41   explicit Symbol(SymbolBody *P) : Body(P) {}
42   SymbolBody *Body;
43 };
44
45 // The base class for real symbol classes.
46 class SymbolBody {
47 public:
48   enum Kind {
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
53     // over another.
54     DefinedRegularKind = 0,
55     DefinedCommonKind,
56     DefinedLocalImportKind,
57     DefinedImportThunkKind,
58     DefinedImportDataKind,
59     DefinedAbsoluteKind,
60     DefinedRelativeKind,
61     DefinedBitcodeKind,
62
63     UndefinedKind,
64     LazyKind,
65
66     LastDefinedCOFFKind = DefinedCommonKind,
67     LastDefinedKind = DefinedBitcodeKind,
68   };
69
70   Kind kind() const { return static_cast<Kind>(SymbolKind); }
71
72   // Returns true if this is an external symbol.
73   bool isExternal() { return IsExternal; }
74
75   // Returns the symbol name.
76   StringRef getName();
77
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; }
86
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);
91
92   // Returns a name of this symbol including source file name.
93   // Used only for debugging and logging.
94   std::string getDebugName();
95
96 protected:
97   explicit SymbolBody(Kind K, StringRef N = "")
98       : SymbolKind(K), IsExternal(true), IsCOMDAT(false),
99         IsReplaceable(false), Name(N) {}
100
101   const unsigned SymbolKind : 8;
102   unsigned IsExternal : 1;
103
104   // This bit is used by the \c DefinedRegular subclass.
105   unsigned IsCOMDAT : 1;
106
107   // This bit is used by the \c DefinedBitcode subclass.
108   unsigned IsReplaceable : 1;
109
110   StringRef Name;
111   Symbol *Backref = nullptr;
112 };
113
114 // The base class for any defined symbols, including absolute symbols,
115 // etc.
116 class Defined : public SymbolBody {
117 public:
118   Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}
119
120   static bool classof(const SymbolBody *S) {
121     return S->kind() <= LastDefinedKind;
122   }
123
124   // Returns the RVA (relative virtual address) of this symbol. The
125   // writer sets and uses RVAs.
126   uint64_t getRVA();
127
128   // Returns the RVA relative to the beginning of the output section.
129   // Used to implement SECREL relocation type.
130   uint64_t getSecrel();
131
132   // Returns the output section index.
133   // Used to implement SECTION relocation type.
134   uint64_t getSectionIndex();
135
136   // Returns true if this symbol points to an executable (e.g. .text) section.
137   // Used to implement ARM relocations.
138   bool isExecutable();
139 };
140
141 // Symbols defined via a COFF object file.
142 class DefinedCOFF : public Defined {
143   friend SymbolBody;
144 public:
145   DefinedCOFF(Kind K, ObjectFile *F, COFFSymbolRef S)
146       : Defined(K), File(F), Sym(S.getGeneric()) {}
147
148   static bool classof(const SymbolBody *S) {
149     return S->kind() <= LastDefinedCOFFKind;
150   }
151
152   int getFileIndex() { return File->Index; }
153
154   COFFSymbolRef getCOFFSymbol();
155
156 protected:
157   ObjectFile *File;
158   const coff_symbol_generic *Sym;
159 };
160
161 // Regular defined symbols read from object file symbol tables.
162 class DefinedRegular : public DefinedCOFF {
163 public:
164   DefinedRegular(ObjectFile *F, COFFSymbolRef S, SectionChunk *C)
165       : DefinedCOFF(DefinedRegularKind, F, S), Data(&C->Repl) {
166     IsExternal = S.isExternal();
167     IsCOMDAT = C->isCOMDAT();
168   }
169
170   static bool classof(const SymbolBody *S) {
171     return S->kind() == DefinedRegularKind;
172   }
173
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; }
178
179 private:
180   SectionChunk **Data;
181 };
182
183 class DefinedCommon : public DefinedCOFF {
184 public:
185   DefinedCommon(ObjectFile *F, COFFSymbolRef S, CommonChunk *C)
186       : DefinedCOFF(DefinedCommonKind, F, S), Data(C) {
187     IsExternal = S.isExternal();
188   }
189
190   static bool classof(const SymbolBody *S) {
191     return S->kind() == DefinedCommonKind;
192   }
193
194   uint64_t getRVA() { return Data->getRVA(); }
195
196 private:
197   friend SymbolBody;
198   uint64_t getSize() { return Sym->Value; }
199   CommonChunk *Data;
200 };
201
202 // Absolute symbols.
203 class DefinedAbsolute : public Defined {
204 public:
205   DefinedAbsolute(StringRef N, COFFSymbolRef S)
206       : Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
207     IsExternal = S.isExternal();
208   }
209
210   DefinedAbsolute(StringRef N, uint64_t V)
211       : Defined(DefinedAbsoluteKind, N), VA(V) {}
212
213   static bool classof(const SymbolBody *S) {
214     return S->kind() == DefinedAbsoluteKind;
215   }
216
217   uint64_t getRVA() { return VA - Config->ImageBase; }
218   void setVA(uint64_t V) { VA = V; }
219
220 private:
221   uint64_t VA;
222 };
223
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 {
229 public:
230   explicit DefinedRelative(StringRef Name, uint64_t V = 0)
231       : Defined(DefinedRelativeKind, Name), RVA(V) {}
232
233   static bool classof(const SymbolBody *S) {
234     return S->kind() == DefinedRelativeKind;
235   }
236
237   uint64_t getRVA() { return RVA; }
238   void setRVA(uint64_t V) { RVA = V; }
239
240 private:
241   uint64_t RVA;
242 };
243
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 {
250 public:
251   Lazy(ArchiveFile *F, const Archive::Symbol S)
252       : SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}
253
254   static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
255
256   // Returns an object file for this symbol, or a nullptr if the file
257   // was already returned.
258   std::unique_ptr<InputFile> getMember();
259
260   int getFileIndex() { return File->Index; }
261
262 private:
263   ArchiveFile *File;
264   const Archive::Symbol Sym;
265 };
266
267 // Undefined symbols.
268 class Undefined : public SymbolBody {
269 public:
270   explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
271
272   static bool classof(const SymbolBody *S) {
273     return S->kind() == UndefinedKind;
274   }
275
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;
281
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();
286 };
287
288 // Windows-specific classes.
289
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 {
295 public:
296   DefinedImportData(StringRef D, StringRef N, StringRef E,
297                     const coff_import_header *H)
298       : Defined(DefinedImportDataKind, N), DLLName(D), ExternalName(E), Hdr(H) {
299   }
300
301   static bool classof(const SymbolBody *S) {
302     return S->kind() == DefinedImportDataKind;
303   }
304
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; }
310
311 private:
312   StringRef DLLName;
313   StringRef ExternalName;
314   const coff_import_header *Hdr;
315   Chunk *Location = nullptr;
316 };
317
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 {
324 public:
325   DefinedImportThunk(StringRef Name, DefinedImportData *S, uint16_t Machine);
326
327   static bool classof(const SymbolBody *S) {
328     return S->kind() == DefinedImportThunkKind;
329   }
330
331   uint64_t getRVA() { return Data->getRVA(); }
332   Chunk *getChunk() { return Data.get(); }
333
334 private:
335   std::unique_ptr<Chunk> Data;
336 };
337
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 {
344 public:
345   DefinedLocalImport(StringRef N, Defined *S)
346       : Defined(DefinedLocalImportKind, N), Data(S) {}
347
348   static bool classof(const SymbolBody *S) {
349     return S->kind() == DefinedLocalImportKind;
350   }
351
352   uint64_t getRVA() { return Data.getRVA(); }
353   Chunk *getChunk() { return &Data; }
354
355 private:
356   LocalImportChunk Data;
357 };
358
359 class DefinedBitcode : public Defined {
360   friend SymbolBody;
361 public:
362   DefinedBitcode(BitcodeFile *F, StringRef N, bool IsReplaceable)
363       : Defined(DefinedBitcodeKind, N), File(F) {
364     this->IsReplaceable = IsReplaceable;
365   }
366
367   static bool classof(const SymbolBody *S) {
368     return S->kind() == DefinedBitcodeKind;
369   }
370
371 private:
372   BitcodeFile *File;
373 };
374
375 inline uint64_t Defined::getRVA() {
376   switch (kind()) {
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.");
393   case LazyKind:
394   case UndefinedKind:
395     llvm_unreachable("Cannot get the address for an undefined symbol.");
396   }
397   llvm_unreachable("unknown symbol kind");
398 }
399
400 } // namespace coff
401 } // namespace lld
402
403 #endif