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