]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Symbols.h
Merge ^/head r308870 through r309105.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / 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 // All symbols are handled as SymbolBodies regardless of their types.
11 // This file defines various types of SymbolBodies.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLD_ELF_SYMBOLS_H
16 #define LLD_ELF_SYMBOLS_H
17
18 #include "InputSection.h"
19
20 #include "lld/Core/LLVM.h"
21 #include "llvm/Object/Archive.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Support/AlignOf.h"
24
25 namespace lld {
26 namespace elf {
27
28 class ArchiveFile;
29 class BitcodeFile;
30 class InputFile;
31 class LazyObjectFile;
32 class SymbolBody;
33 template <class ELFT> class ObjectFile;
34 template <class ELFT> class OutputSection;
35 template <class ELFT> class OutputSectionBase;
36 template <class ELFT> class SharedFile;
37
38 struct Symbol;
39
40 // The base class for real symbol classes.
41 class SymbolBody {
42 public:
43   enum Kind {
44     DefinedFirst,
45     DefinedRegularKind = DefinedFirst,
46     SharedKind,
47     DefinedCommonKind,
48     DefinedBitcodeKind,
49     DefinedSyntheticKind,
50     DefinedLast = DefinedSyntheticKind,
51     UndefinedKind,
52     LazyArchiveKind,
53     LazyObjectKind,
54   };
55
56   SymbolBody(Kind K) : SymbolKind(K) {}
57
58   Symbol *symbol();
59   const Symbol *symbol() const {
60     return const_cast<SymbolBody *>(this)->symbol();
61   }
62
63   Kind kind() const { return static_cast<Kind>(SymbolKind); }
64
65   bool isUndefined() const { return SymbolKind == UndefinedKind; }
66   bool isDefined() const { return SymbolKind <= DefinedLast; }
67   bool isCommon() const { return SymbolKind == DefinedCommonKind; }
68   bool isLazy() const {
69     return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
70   }
71   bool isShared() const { return SymbolKind == SharedKind; }
72   bool isLocal() const { return IsLocal; }
73   bool isPreemptible() const;
74
75   StringRef getName() const;
76   void setName(StringRef S);
77
78   uint32_t getNameOffset() const {
79     assert(isLocal());
80     return NameOffset;
81   }
82
83   uint8_t getVisibility() const { return StOther & 0x3; }
84
85   unsigned DynsymIndex = 0;
86   uint32_t GotIndex = -1;
87   uint32_t GotPltIndex = -1;
88   uint32_t PltIndex = -1;
89   uint32_t GlobalDynIndex = -1;
90   bool isInGot() const { return GotIndex != -1U; }
91   bool isInPlt() const { return PltIndex != -1U; }
92   template <class ELFT> bool hasThunk() const;
93
94   template <class ELFT>
95   typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
96
97   template <class ELFT> typename ELFT::uint getGotOffset() const;
98   template <class ELFT> typename ELFT::uint getGotVA() const;
99   template <class ELFT> typename ELFT::uint getGotPltOffset() const;
100   template <class ELFT> typename ELFT::uint getGotPltVA() const;
101   template <class ELFT> typename ELFT::uint getPltVA() const;
102   template <class ELFT> typename ELFT::uint getThunkVA() const;
103   template <class ELFT> typename ELFT::uint getSize() const;
104
105   // The file from which this symbol was created.
106   InputFile *File = nullptr;
107
108 protected:
109   SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
110
111   SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
112
113   const unsigned SymbolKind : 8;
114
115 public:
116   // True if the linker has to generate a copy relocation for this shared
117   // symbol or if the symbol should point to its plt entry.
118   unsigned NeedsCopyOrPltAddr : 1;
119
120   // True if this is a local symbol.
121   unsigned IsLocal : 1;
122
123   // True if this symbol has an entry in the global part of MIPS GOT.
124   unsigned IsInGlobalMipsGot : 1;
125
126   // The following fields have the same meaning as the ELF symbol attributes.
127   uint8_t Type;    // symbol type
128   uint8_t StOther; // st_other field value
129
130   // The Type field may also have this value. It means that we have not yet seen
131   // a non-Lazy symbol with this name, so we don't know what its type is. The
132   // Type field is normally set to this value for Lazy symbols unless we saw a
133   // weak undefined symbol first, in which case we need to remember the original
134   // symbol's type in order to check for TLS mismatches.
135   enum { UnknownType = 255 };
136
137   bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
138   bool isTls() const { return Type == llvm::ELF::STT_TLS; }
139   bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
140   bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
141   bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
142   bool isFile() const { return Type == llvm::ELF::STT_FILE; }
143
144 protected:
145   struct Str {
146     const char *S;
147     size_t Len;
148   };
149   union {
150     Str Name;
151     uint32_t NameOffset;
152   };
153 };
154
155 // The base class for any defined symbols.
156 class Defined : public SymbolBody {
157 public:
158   Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
159   Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
160   static bool classof(const SymbolBody *S) { return S->isDefined(); }
161 };
162
163 // The defined symbol in LLVM bitcode files.
164 class DefinedBitcode : public Defined {
165 public:
166   DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type, BitcodeFile *F);
167   static bool classof(const SymbolBody *S);
168   BitcodeFile *file() { return (BitcodeFile *)this->File; }
169 };
170
171 class DefinedCommon : public Defined {
172 public:
173   DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther,
174                 uint8_t Type, InputFile *File);
175
176   static bool classof(const SymbolBody *S) {
177     return S->kind() == SymbolBody::DefinedCommonKind;
178   }
179
180   // The output offset of this common symbol in the output bss. Computed by the
181   // writer.
182   uint64_t OffsetInBss;
183
184   // The maximum alignment we have seen for this symbol.
185   uint64_t Alignment;
186
187   uint64_t Size;
188 };
189
190 // Regular defined symbols read from object file symbol tables.
191 template <class ELFT> class DefinedRegular : public Defined {
192   typedef typename ELFT::Sym Elf_Sym;
193   typedef typename ELFT::uint uintX_t;
194
195 public:
196   DefinedRegular(StringRef Name, const Elf_Sym &Sym,
197                  InputSectionBase<ELFT> *Section)
198       : Defined(SymbolBody::DefinedRegularKind, Name, Sym.st_other,
199                 Sym.getType()),
200         Value(Sym.st_value), Size(Sym.st_size),
201         Section(Section ? Section->Repl : NullInputSection) {
202     if (Section)
203       this->File = Section->getFile();
204   }
205
206   DefinedRegular(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section)
207       : Defined(SymbolBody::DefinedRegularKind, Sym.st_name, Sym.st_other,
208                 Sym.getType()),
209         Value(Sym.st_value), Size(Sym.st_size),
210         Section(Section ? Section->Repl : NullInputSection) {
211     assert(isLocal());
212     if (Section)
213       this->File = Section->getFile();
214   }
215
216   DefinedRegular(StringRef Name, uint8_t StOther)
217       : Defined(SymbolBody::DefinedRegularKind, Name, StOther,
218                 llvm::ELF::STT_NOTYPE),
219         Value(0), Size(0), Section(NullInputSection) {}
220
221   static bool classof(const SymbolBody *S) {
222     return S->kind() == SymbolBody::DefinedRegularKind;
223   }
224
225   uintX_t Value;
226   uintX_t Size;
227
228   // The input section this symbol belongs to. Notice that this is
229   // a reference to a pointer. We are using two levels of indirections
230   // because of ICF. If ICF decides two sections need to be merged, it
231   // manipulates this Section pointers so that they point to the same
232   // section. This is a bit tricky, so be careful to not be confused.
233   // If this is null, the symbol is an absolute symbol.
234   InputSectionBase<ELFT> *&Section;
235
236   // If non-null the symbol has a Thunk that may be used as an alternative
237   // destination for callers of this Symbol.
238   Thunk<ELFT> *ThunkData = nullptr;
239
240 private:
241   static InputSectionBase<ELFT> *NullInputSection;
242 };
243
244 template <class ELFT>
245 InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
246
247 // DefinedSynthetic is a class to represent linker-generated ELF symbols.
248 // The difference from the regular symbol is that DefinedSynthetic symbols
249 // don't belong to any input files or sections. Thus, its constructor
250 // takes an output section to calculate output VA, etc.
251 // If Section is null, this symbol is relative to the image base.
252 template <class ELFT> class DefinedSynthetic : public Defined {
253 public:
254   typedef typename ELFT::uint uintX_t;
255   DefinedSynthetic(StringRef N, uintX_t Value,
256                    OutputSectionBase<ELFT> *Section);
257
258   static bool classof(const SymbolBody *S) {
259     return S->kind() == SymbolBody::DefinedSyntheticKind;
260   }
261
262   // Special value designates that the symbol 'points'
263   // to the end of the section.
264   static const uintX_t SectionEnd = uintX_t(-1);
265
266   uintX_t Value;
267   const OutputSectionBase<ELFT> *Section;
268 };
269
270 class Undefined : public SymbolBody {
271 public:
272   Undefined(StringRef Name, uint8_t StOther, uint8_t Type, InputFile *F);
273   Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type, InputFile *F);
274
275   static bool classof(const SymbolBody *S) {
276     return S->kind() == UndefinedKind;
277   }
278
279   InputFile *file() { return this->File; }
280 };
281
282 template <class ELFT> class SharedSymbol : public Defined {
283   typedef typename ELFT::Sym Elf_Sym;
284   typedef typename ELFT::Verdef Elf_Verdef;
285   typedef typename ELFT::uint uintX_t;
286
287 public:
288   static bool classof(const SymbolBody *S) {
289     return S->kind() == SymbolBody::SharedKind;
290   }
291
292   SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
293                const Elf_Verdef *Verdef)
294       : Defined(SymbolBody::SharedKind, Name, Sym.st_other, Sym.getType()),
295         Sym(Sym), Verdef(Verdef) {
296     // IFuncs defined in DSOs are treated as functions by the static linker.
297     if (isGnuIFunc())
298       Type = llvm::ELF::STT_FUNC;
299     this->File = F;
300   }
301
302   SharedFile<ELFT> *file() { return (SharedFile<ELFT> *)this->File; }
303
304   const Elf_Sym &Sym;
305
306   // This field is a pointer to the symbol's version definition.
307   const Elf_Verdef *Verdef;
308
309   // OffsetInBss is significant only when needsCopy() is true.
310   uintX_t OffsetInBss = 0;
311
312   // If non-null the symbol has a Thunk that may be used as an alternative
313   // destination for callers of this Symbol.
314   Thunk<ELFT> *ThunkData = nullptr;
315   bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
316 };
317
318 // This class represents a symbol defined in an archive file. It is
319 // created from an archive file header, and it knows how to load an
320 // object file from an archive to replace itself with a defined
321 // symbol. If the resolver finds both Undefined and Lazy for
322 // the same name, it will ask the Lazy to load a file.
323 class Lazy : public SymbolBody {
324 public:
325   static bool classof(const SymbolBody *S) { return S->isLazy(); }
326
327   // Returns an object file for this symbol, or a nullptr if the file
328   // was already returned.
329   std::unique_ptr<InputFile> fetch();
330
331 protected:
332   Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
333       : SymbolBody(K, Name, llvm::ELF::STV_DEFAULT, Type) {}
334 };
335
336 // LazyArchive symbols represents symbols in archive files.
337 class LazyArchive : public Lazy {
338 public:
339   LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
340               uint8_t Type);
341
342   static bool classof(const SymbolBody *S) {
343     return S->kind() == LazyArchiveKind;
344   }
345
346   ArchiveFile *file() { return (ArchiveFile *)this->File; }
347   std::unique_ptr<InputFile> fetch();
348
349 private:
350   const llvm::object::Archive::Symbol Sym;
351 };
352
353 // LazyObject symbols represents symbols in object files between
354 // --start-lib and --end-lib options.
355 class LazyObject : public Lazy {
356 public:
357   LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
358
359   static bool classof(const SymbolBody *S) {
360     return S->kind() == LazyObjectKind;
361   }
362
363   LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
364   std::unique_ptr<InputFile> fetch();
365 };
366
367 // Some linker-generated symbols need to be created as
368 // DefinedRegular symbols.
369 template <class ELFT> struct ElfSym {
370   // The content for _etext and etext symbols.
371   static DefinedRegular<ELFT> *Etext;
372   static DefinedRegular<ELFT> *Etext2;
373
374   // The content for _edata and edata symbols.
375   static DefinedRegular<ELFT> *Edata;
376   static DefinedRegular<ELFT> *Edata2;
377
378   // The content for _end and end symbols.
379   static DefinedRegular<ELFT> *End;
380   static DefinedRegular<ELFT> *End2;
381
382   // The content for _gp_disp symbol for MIPS target.
383   static SymbolBody *MipsGpDisp;
384 };
385
386 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
387 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
388 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
389 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
390 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
391 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
392 template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGpDisp;
393
394 // A real symbol object, SymbolBody, is usually stored within a Symbol. There's
395 // always one Symbol for each symbol name. The resolver updates the SymbolBody
396 // stored in the Body field of this object as it resolves symbols. Symbol also
397 // holds computed properties of symbol names.
398 struct Symbol {
399   // Symbol binding. This is on the Symbol to track changes during resolution.
400   // In particular:
401   // An undefined weak is still weak when it resolves to a shared library.
402   // An undefined weak will not fetch archive members, but we have to remember
403   // it is weak.
404   uint8_t Binding;
405
406   // Version definition index.
407   uint16_t VersionId;
408
409   // Symbol visibility. This is the computed minimum visibility of all
410   // observed non-DSO symbols.
411   unsigned Visibility : 2;
412
413   // True if the symbol was used for linking and thus need to be added to the
414   // output file's symbol table. This is true for all symbols except for
415   // unreferenced DSO symbols and bitcode symbols that are unreferenced except
416   // by other bitcode objects.
417   unsigned IsUsedInRegularObj : 1;
418
419   // If this flag is true and the symbol has protected or default visibility, it
420   // will appear in .dynsym. This flag is set by interposable DSO symbols in
421   // executables, by most symbols in DSOs and executables built with
422   // --export-dynamic, and by dynamic lists.
423   unsigned ExportDynamic : 1;
424
425   // True if this symbol is specified by --trace-symbol option.
426   unsigned Traced : 1;
427
428   bool includeInDynsym() const;
429   bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
430
431   // This field is used to store the Symbol's SymbolBody. This instantiation of
432   // AlignedCharArrayUnion gives us a struct with a char array field that is
433   // large and aligned enough to store any derived class of SymbolBody. We
434   // assume that the size and alignment of ELF64LE symbols is sufficient for any
435   // ELFT, and we verify this with the static_asserts in replaceBody.
436   llvm::AlignedCharArrayUnion<
437       DefinedBitcode, DefinedCommon, DefinedRegular<llvm::object::ELF64LE>,
438       DefinedSynthetic<llvm::object::ELF64LE>, Undefined,
439       SharedSymbol<llvm::object::ELF64LE>, LazyArchive, LazyObject>
440       Body;
441
442   SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
443   const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
444 };
445
446 void printTraceSymbol(Symbol *Sym);
447
448 template <typename T, typename... ArgT>
449 void replaceBody(Symbol *S, ArgT &&... Arg) {
450   static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
451   static_assert(llvm::AlignOf<T>::Alignment <=
452                     llvm::AlignOf<decltype(S->Body)>::Alignment,
453                 "Body not aligned enough");
454   assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
455          "Not a SymbolBody");
456
457   new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
458
459   // Print out a log message if --trace-symbol was specified.
460   // This is for debugging.
461   if (S->Traced)
462     printTraceSymbol(S);
463 }
464
465 inline Symbol *SymbolBody::symbol() {
466   assert(!isLocal());
467   return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
468                                     offsetof(Symbol, Body));
469 }
470
471 } // namespace elf
472 } // namespace lld
473
474 #endif