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