]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Symbols.h
Merge lld trunk r300422 and resolve conflicts.
[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 class OutputSection;
34 template <class ELFT> class SharedFile;
35
36 struct Symbol;
37
38 // The base class for real symbol classes.
39 class SymbolBody {
40 public:
41   enum Kind {
42     DefinedFirst,
43     DefinedRegularKind = DefinedFirst,
44     SharedKind,
45     DefinedCommonKind,
46     DefinedLast = DefinedCommonKind,
47     UndefinedKind,
48     LazyArchiveKind,
49     LazyObjectKind,
50   };
51
52   SymbolBody(Kind K) : SymbolKind(K) {}
53
54   Symbol *symbol();
55   const Symbol *symbol() const {
56     return const_cast<SymbolBody *>(this)->symbol();
57   }
58
59   Kind kind() const { return static_cast<Kind>(SymbolKind); }
60
61   bool isUndefined() const { return SymbolKind == UndefinedKind; }
62   bool isDefined() const { return SymbolKind <= DefinedLast; }
63   bool isCommon() const { return SymbolKind == DefinedCommonKind; }
64   bool isLazy() const {
65     return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
66   }
67   bool isShared() const { return SymbolKind == SharedKind; }
68   bool isInCurrentDSO() const { return !isUndefined() && !isShared(); }
69   bool isLocal() const { return IsLocal; }
70   bool isPreemptible() const;
71   StringRef getName() const { return Name; }
72   uint8_t getVisibility() const { return StOther & 0x3; }
73   void parseSymbolVersion();
74
75   bool isInGot() const { return GotIndex != -1U; }
76   bool isInPlt() const { return PltIndex != -1U; }
77
78   uint64_t getVA(int64_t Addend = 0) const;
79
80   uint64_t getGotOffset() const;
81   template <class ELFT> typename ELFT::uint getGotVA() const;
82   uint64_t getGotPltOffset() const;
83   uint64_t getGotPltVA() const;
84   uint64_t getPltVA() const;
85   template <class ELFT> typename ELFT::uint getSize() const;
86   OutputSection *getOutputSection() const;
87
88   // The file from which this symbol was created.
89   InputFile *File = nullptr;
90
91   uint32_t DynsymIndex = 0;
92   uint32_t GotIndex = -1;
93   uint32_t GotPltIndex = -1;
94   uint32_t PltIndex = -1;
95   uint32_t GlobalDynIndex = -1;
96
97 protected:
98   SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
99              uint8_t Type);
100
101   const unsigned SymbolKind : 8;
102
103 public:
104   // True if the linker has to generate a copy relocation.
105   // For SharedSymbol only.
106   unsigned NeedsCopy : 1;
107
108   // True the symbol should point to its PLT entry.
109   // For SharedSymbol only.
110   unsigned NeedsPltAddr : 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   // The following fields have the same meaning as the ELF symbol attributes.
128   uint8_t Type;    // symbol type
129   uint8_t StOther; // st_other field value
130
131   // The Type field may also have this value. It means that we have not yet seen
132   // a non-Lazy symbol with this name, so we don't know what its type is. The
133   // Type field is normally set to this value for Lazy symbols unless we saw a
134   // weak undefined symbol first, in which case we need to remember the original
135   // symbol's type in order to check for TLS mismatches.
136   enum { UnknownType = 255 };
137
138   bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
139   bool isTls() const { return Type == llvm::ELF::STT_TLS; }
140   bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
141   bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
142   bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
143   bool isFile() const { return Type == llvm::ELF::STT_FILE; }
144
145 protected:
146   StringRefZ Name;
147 };
148
149 // The base class for any defined symbols.
150 class Defined : public SymbolBody {
151 public:
152   Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
153   static bool classof(const SymbolBody *S) { return S->isDefined(); }
154 };
155
156 class DefinedCommon : public Defined {
157 public:
158   DefinedCommon(StringRef N, uint64_t Size, uint32_t Alignment, uint8_t StOther,
159                 uint8_t Type, InputFile *File);
160
161   static bool classof(const SymbolBody *S) {
162     return S->kind() == SymbolBody::DefinedCommonKind;
163   }
164
165   // The output offset of this common symbol in the output bss. Computed by the
166   // writer.
167   uint64_t Offset;
168
169   // The maximum alignment we have seen for this symbol.
170   uint32_t Alignment;
171
172   uint64_t Size;
173 };
174
175 // Regular defined symbols read from object file symbol tables.
176 class DefinedRegular : public Defined {
177 public:
178   DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
179                  uint64_t Value, uint64_t Size, SectionBase *Section,
180                  InputFile *File)
181       : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
182         Value(Value), Size(Size), Section(Section) {
183     this->File = File;
184   }
185
186   // Return true if the symbol is a PIC function.
187   template <class ELFT> bool isMipsPIC() const;
188
189   static bool classof(const SymbolBody *S) {
190     return S->kind() == SymbolBody::DefinedRegularKind;
191   }
192
193   uint64_t Value;
194   uint64_t Size;
195   SectionBase *Section;
196 };
197
198 class Undefined : public SymbolBody {
199 public:
200   Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
201             InputFile *F);
202
203   static bool classof(const SymbolBody *S) {
204     return S->kind() == UndefinedKind;
205   }
206 };
207
208 class SharedSymbol : public Defined {
209 public:
210   static bool classof(const SymbolBody *S) {
211     return S->kind() == SymbolBody::SharedKind;
212   }
213
214   SharedSymbol(InputFile *File, StringRef Name, uint8_t StOther, uint8_t Type,
215                const void *ElfSym, const void *Verdef)
216       : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, StOther, Type),
217         Verdef(Verdef), ElfSym(ElfSym) {
218     // IFuncs defined in DSOs are treated as functions by the static linker.
219     if (isGnuIFunc())
220       Type = llvm::ELF::STT_FUNC;
221     this->File = File;
222   }
223
224   template <class ELFT> uint64_t getShndx() const {
225     return getSym<ELFT>().st_shndx;
226   }
227
228   template <class ELFT> uint64_t getValue() const {
229     return getSym<ELFT>().st_value;
230   }
231
232   template <class ELFT> uint64_t getSize() const {
233     return getSym<ELFT>().st_size;
234   }
235
236   template <class ELFT> uint32_t getAlignment() const;
237
238   // This field is a pointer to the symbol's version definition.
239   const void *Verdef;
240
241   // CopyRelSec and CopyRelSecOff are significant only when NeedsCopy is true.
242   InputSection *CopyRelSec;
243   uint64_t CopyRelSecOff;
244
245 private:
246   template <class ELFT> const typename ELFT::Sym &getSym() const {
247     return *(const typename ELFT::Sym *)ElfSym;
248   }
249
250   const void *ElfSym;
251 };
252
253 // This class represents a symbol defined in an archive file. It is
254 // created from an archive file header, and it knows how to load an
255 // object file from an archive to replace itself with a defined
256 // symbol. If the resolver finds both Undefined and Lazy for
257 // the same name, it will ask the Lazy to load a file.
258 class Lazy : public SymbolBody {
259 public:
260   static bool classof(const SymbolBody *S) { return S->isLazy(); }
261
262   // Returns an object file for this symbol, or a nullptr if the file
263   // was already returned.
264   InputFile *fetch();
265
266 protected:
267   Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
268       : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
269 };
270
271 // LazyArchive symbols represents symbols in archive files.
272 class LazyArchive : public Lazy {
273 public:
274   LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
275               uint8_t Type);
276
277   static bool classof(const SymbolBody *S) {
278     return S->kind() == LazyArchiveKind;
279   }
280
281   ArchiveFile *file() { return (ArchiveFile *)this->File; }
282   InputFile *fetch();
283
284 private:
285   const llvm::object::Archive::Symbol Sym;
286 };
287
288 // LazyObject symbols represents symbols in object files between
289 // --start-lib and --end-lib options.
290 class LazyObject : public Lazy {
291 public:
292   LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
293
294   static bool classof(const SymbolBody *S) {
295     return S->kind() == LazyObjectKind;
296   }
297
298   LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
299   InputFile *fetch();
300 };
301
302 // Some linker-generated symbols need to be created as
303 // DefinedRegular symbols.
304 struct ElfSym {
305   // __bss_start
306   static DefinedRegular *Bss;
307
308   // etext and _etext
309   static DefinedRegular *Etext1;
310   static DefinedRegular *Etext2;
311
312   // edata and _edata
313   static DefinedRegular *Edata1;
314   static DefinedRegular *Edata2;
315
316   // end and _end
317   static DefinedRegular *End1;
318   static DefinedRegular *End2;
319
320   // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
321   static DefinedRegular *MipsGp;
322   static DefinedRegular *MipsGpDisp;
323   static DefinedRegular *MipsLocalGp;
324 };
325
326 // A real symbol object, SymbolBody, is usually stored within a Symbol. There's
327 // always one Symbol for each symbol name. The resolver updates the SymbolBody
328 // stored in the Body field of this object as it resolves symbols. Symbol also
329 // holds computed properties of symbol names.
330 struct Symbol {
331   // Symbol binding. This is on the Symbol to track changes during resolution.
332   // In particular:
333   // An undefined weak is still weak when it resolves to a shared library.
334   // An undefined weak will not fetch archive members, but we have to remember
335   // it is weak.
336   uint8_t Binding;
337
338   // Version definition index.
339   uint16_t VersionId;
340
341   // Symbol visibility. This is the computed minimum visibility of all
342   // observed non-DSO symbols.
343   unsigned Visibility : 2;
344
345   // True if the symbol was used for linking and thus need to be added to the
346   // output file's symbol table. This is true for all symbols except for
347   // unreferenced DSO symbols and bitcode symbols that are unreferenced except
348   // by other bitcode objects.
349   unsigned IsUsedInRegularObj : 1;
350
351   // If this flag is true and the symbol has protected or default visibility, it
352   // will appear in .dynsym. This flag is set by interposable DSO symbols in
353   // executables, by most symbols in DSOs and executables built with
354   // --export-dynamic, and by dynamic lists.
355   unsigned ExportDynamic : 1;
356
357   // True if this symbol is specified by --trace-symbol option.
358   unsigned Traced : 1;
359
360   // This symbol version was found in a version script.
361   unsigned InVersionScript : 1;
362
363   bool includeInDynsym() const;
364   uint8_t computeBinding() const;
365   bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
366
367   // This field is used to store the Symbol's SymbolBody. This instantiation of
368   // AlignedCharArrayUnion gives us a struct with a char array field that is
369   // large and aligned enough to store any derived class of SymbolBody.
370   llvm::AlignedCharArrayUnion<DefinedCommon, DefinedRegular, Undefined,
371                               SharedSymbol, LazyArchive, LazyObject>
372       Body;
373
374   SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
375   const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
376 };
377
378 void printTraceSymbol(Symbol *Sym);
379
380 template <typename T, typename... ArgT>
381 void replaceBody(Symbol *S, ArgT &&... Arg) {
382   static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
383   static_assert(alignof(T) <= alignof(decltype(S->Body)),
384                 "Body not aligned enough");
385   assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
386          "Not a SymbolBody");
387
388   new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
389
390   // Print out a log message if --trace-symbol was specified.
391   // This is for debugging.
392   if (S->Traced)
393     printTraceSymbol(S);
394 }
395
396 inline Symbol *SymbolBody::symbol() {
397   assert(!isLocal());
398   return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
399                                     offsetof(Symbol, Body));
400 }
401 } // namespace elf
402
403 std::string toString(const elf::SymbolBody &B);
404 } // namespace lld
405
406 #endif