]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Symbols.h
Merge clang 7.0.1 and several follow-up changes
[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 // This file defines various types of Symbols.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLD_ELF_SYMBOLS_H
15 #define LLD_ELF_SYMBOLS_H
16
17 #include "InputSection.h"
18 #include "lld/Common/LLVM.h"
19 #include "lld/Common/Strings.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/ELF.h"
22
23 namespace lld {
24 namespace elf {
25
26 class ArchiveFile;
27 class BitcodeFile;
28 class BssSection;
29 class InputFile;
30 class LazyObjFile;
31 template <class ELFT> class ObjFile;
32 class OutputSection;
33 template <class ELFT> class SharedFile;
34
35 // This is a StringRef-like container that doesn't run strlen().
36 //
37 // ELF string tables contain a lot of null-terminated strings. Most of them
38 // are not necessary for the linker because they are names of local symbols,
39 // and the linker doesn't use local symbol names for name resolution. So, we
40 // use this class to represents strings read from string tables.
41 struct StringRefZ {
42   StringRefZ(const char *S) : Data(S), Size(-1) {}
43   StringRefZ(StringRef S) : Data(S.data()), Size(S.size()) {}
44
45   const char *Data;
46   const uint32_t Size;
47 };
48
49 // The base class for real symbol classes.
50 class Symbol {
51 public:
52   enum Kind {
53     DefinedKind,
54     SharedKind,
55     UndefinedKind,
56     LazyArchiveKind,
57     LazyObjectKind,
58   };
59
60   Kind kind() const { return static_cast<Kind>(SymbolKind); }
61
62   // The file from which this symbol was created.
63   InputFile *File;
64
65 protected:
66   const char *NameData;
67   mutable uint32_t NameSize;
68
69 public:
70   uint32_t DynsymIndex = 0;
71   uint32_t GotIndex = -1;
72   uint32_t PltIndex = -1;
73   uint32_t GlobalDynIndex = -1;
74
75   // This field is a index to the symbol's version definition.
76   uint32_t VerdefIndex = -1;
77
78   // Version definition index.
79   uint16_t VersionId;
80
81   // Symbol binding. This is not overwritten by replaceSymbol to track
82   // changes during resolution. In particular:
83   //  - An undefined weak is still weak when it resolves to a shared library.
84   //  - An undefined weak will not fetch archive members, but we have to
85   //    remember it is weak.
86   uint8_t Binding;
87
88   // The following fields have the same meaning as the ELF symbol attributes.
89   uint8_t Type;    // symbol type
90   uint8_t StOther; // st_other field value
91
92   const uint8_t SymbolKind;
93
94   // Symbol visibility. This is the computed minimum visibility of all
95   // observed non-DSO symbols.
96   unsigned Visibility : 2;
97
98   // True if the symbol was used for linking and thus need to be added to the
99   // output file's symbol table. This is true for all symbols except for
100   // unreferenced DSO symbols and bitcode symbols that are unreferenced except
101   // by other bitcode objects.
102   unsigned IsUsedInRegularObj : 1;
103
104   // If this flag is true and the symbol has protected or default visibility, it
105   // will appear in .dynsym. This flag is set by interposable DSO symbols in
106   // executables, by most symbols in DSOs and executables built with
107   // --export-dynamic, and by dynamic lists.
108   unsigned ExportDynamic : 1;
109
110   // False if LTO shouldn't inline whatever this symbol points to. If a symbol
111   // is overwritten after LTO, LTO shouldn't inline the symbol because it
112   // doesn't know the final contents of the symbol.
113   unsigned CanInline : 1;
114
115   // True if this symbol is specified by --trace-symbol option.
116   unsigned Traced : 1;
117
118   bool includeInDynsym() const;
119   uint8_t computeBinding() const;
120   bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
121
122   bool isUndefined() const { return SymbolKind == UndefinedKind; }
123   bool isDefined() const { return SymbolKind == DefinedKind; }
124   bool isShared() const { return SymbolKind == SharedKind; }
125   bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; }
126
127   bool isLazy() const {
128     return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
129   }
130
131   // True if this is an undefined weak symbol.
132   bool isUndefWeak() const { return isWeak() && isUndefined(); }
133
134   StringRef getName() const {
135     if (NameSize == (uint32_t)-1)
136       NameSize = strlen(NameData);
137     return {NameData, NameSize};
138   }
139
140   void parseSymbolVersion();
141
142   bool isInGot() const { return GotIndex != -1U; }
143   bool isInPlt() const { return PltIndex != -1U; }
144
145   uint64_t getVA(int64_t Addend = 0) const;
146
147   uint64_t getGotOffset() const;
148   uint64_t getGotVA() const;
149   uint64_t getGotPltOffset() const;
150   uint64_t getGotPltVA() const;
151   uint64_t getPltVA() const;
152   uint64_t getPltOffset() const;
153   uint64_t getSize() const;
154   OutputSection *getOutputSection() const;
155
156 protected:
157   Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding,
158          uint8_t StOther, uint8_t Type)
159       : File(File), NameData(Name.Data), NameSize(Name.Size), Binding(Binding),
160         Type(Type), StOther(StOther), SymbolKind(K), NeedsPltAddr(false),
161         IsInIplt(false), IsInIgot(false), IsPreemptible(false),
162         Used(!Config->GcSections), NeedsTocRestore(false) {}
163
164 public:
165   // True the symbol should point to its PLT entry.
166   // For SharedSymbol only.
167   unsigned NeedsPltAddr : 1;
168
169   // True if this symbol is in the Iplt sub-section of the Plt.
170   unsigned IsInIplt : 1;
171
172   // True if this symbol is in the Igot sub-section of the .got.plt or .got.
173   unsigned IsInIgot : 1;
174
175   // True if this symbol is preemptible at load time.
176   unsigned IsPreemptible : 1;
177
178   // True if an undefined or shared symbol is used from a live section.
179   unsigned Used : 1;
180
181   // True if a call to this symbol needs to be followed by a restore of the
182   // PPC64 toc pointer.
183   unsigned NeedsTocRestore : 1;
184
185   // The Type field may also have this value. It means that we have not yet seen
186   // a non-Lazy symbol with this name, so we don't know what its type is. The
187   // Type field is normally set to this value for Lazy symbols unless we saw a
188   // weak undefined symbol first, in which case we need to remember the original
189   // symbol's type in order to check for TLS mismatches.
190   enum { UnknownType = 255 };
191
192   bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
193   bool isTls() const { return Type == llvm::ELF::STT_TLS; }
194   bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
195   bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
196   bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
197   bool isFile() const { return Type == llvm::ELF::STT_FILE; }
198 };
199
200 // Represents a symbol that is defined in the current output file.
201 class Defined : public Symbol {
202 public:
203   Defined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
204           uint8_t Type, uint64_t Value, uint64_t Size, SectionBase *Section)
205       : Symbol(DefinedKind, File, Name, Binding, StOther, Type), Value(Value),
206         Size(Size), Section(Section) {}
207
208   static bool classof(const Symbol *S) { return S->isDefined(); }
209
210   uint64_t Value;
211   uint64_t Size;
212   SectionBase *Section;
213 };
214
215 class Undefined : public Symbol {
216 public:
217   Undefined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
218             uint8_t Type)
219       : Symbol(UndefinedKind, File, Name, Binding, StOther, Type) {}
220
221   static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
222 };
223
224 class SharedSymbol : public Symbol {
225 public:
226   static bool classof(const Symbol *S) { return S->kind() == SharedKind; }
227
228   SharedSymbol(InputFile &File, StringRef Name, uint8_t Binding,
229                uint8_t StOther, uint8_t Type, uint64_t Value, uint64_t Size,
230                uint32_t Alignment, uint32_t VerdefIndex)
231       : Symbol(SharedKind, &File, Name, Binding, StOther, Type),
232         Alignment(Alignment), Value(Value), Size(Size) {
233     this->VerdefIndex = VerdefIndex;
234     // GNU ifunc is a mechanism to allow user-supplied functions to
235     // resolve PLT slot values at load-time. This is contrary to the
236     // regular symbol resolution scheme in which symbols are resolved just
237     // by name. Using this hook, you can program how symbols are solved
238     // for you program. For example, you can make "memcpy" to be resolved
239     // to a SSE-enabled version of memcpy only when a machine running the
240     // program supports the SSE instruction set.
241     //
242     // Naturally, such symbols should always be called through their PLT
243     // slots. What GNU ifunc symbols point to are resolver functions, and
244     // calling them directly doesn't make sense (unless you are writing a
245     // loader).
246     //
247     // For DSO symbols, we always call them through PLT slots anyway.
248     // So there's no difference between GNU ifunc and regular function
249     // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
250     if (this->Type == llvm::ELF::STT_GNU_IFUNC)
251       this->Type = llvm::ELF::STT_FUNC;
252   }
253
254   template <class ELFT> SharedFile<ELFT> &getFile() const {
255     return *cast<SharedFile<ELFT>>(File);
256   }
257
258   uint32_t Alignment;
259
260   uint64_t Value; // st_value
261   uint64_t Size;  // st_size
262 };
263
264 // LazyArchive and LazyObject represent a symbols that is not yet in the link,
265 // but we know where to find it if needed. If the resolver finds both Undefined
266 // and Lazy for the same name, it will ask the Lazy to load a file.
267 //
268 // A special complication is the handling of weak undefined symbols. They should
269 // not load a file, but we have to remember we have seen both the weak undefined
270 // and the lazy. We represent that with a lazy symbol with a weak binding. This
271 // means that code looking for undefined symbols normally also has to take lazy
272 // symbols into consideration.
273
274 // This class represents a symbol defined in an archive file. It is
275 // created from an archive file header, and it knows how to load an
276 // object file from an archive to replace itself with a defined
277 // symbol.
278 class LazyArchive : public Symbol {
279 public:
280   LazyArchive(InputFile &File, uint8_t Type,
281               const llvm::object::Archive::Symbol S)
282       : Symbol(LazyArchiveKind, &File, S.getName(), llvm::ELF::STB_GLOBAL,
283                llvm::ELF::STV_DEFAULT, Type),
284         Sym(S) {}
285
286   static bool classof(const Symbol *S) { return S->kind() == LazyArchiveKind; }
287
288   InputFile *fetch();
289
290 private:
291   const llvm::object::Archive::Symbol Sym;
292 };
293
294 // LazyObject symbols represents symbols in object files between
295 // --start-lib and --end-lib options.
296 class LazyObject : public Symbol {
297 public:
298   LazyObject(InputFile &File, uint8_t Type, StringRef Name)
299       : Symbol(LazyObjectKind, &File, Name, llvm::ELF::STB_GLOBAL,
300                llvm::ELF::STV_DEFAULT, Type) {}
301
302   static bool classof(const Symbol *S) { return S->kind() == LazyObjectKind; }
303 };
304
305 // Some linker-generated symbols need to be created as
306 // Defined symbols.
307 struct ElfSym {
308   // __bss_start
309   static Defined *Bss;
310
311   // etext and _etext
312   static Defined *Etext1;
313   static Defined *Etext2;
314
315   // edata and _edata
316   static Defined *Edata1;
317   static Defined *Edata2;
318
319   // end and _end
320   static Defined *End1;
321   static Defined *End2;
322
323   // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
324   // be at some offset from the base of the .got section, usually 0 or
325   // the end of the .got.
326   static Defined *GlobalOffsetTable;
327
328   // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
329   static Defined *MipsGp;
330   static Defined *MipsGpDisp;
331   static Defined *MipsLocalGp;
332
333   // __rela_iplt_end or __rel_iplt_end
334   static Defined *RelaIpltEnd;
335 };
336
337 // A buffer class that is large enough to hold any Symbol-derived
338 // object. We allocate memory using this class and instantiate a symbol
339 // using the placement new.
340 union SymbolUnion {
341   alignas(Defined) char A[sizeof(Defined)];
342   alignas(Undefined) char C[sizeof(Undefined)];
343   alignas(SharedSymbol) char D[sizeof(SharedSymbol)];
344   alignas(LazyArchive) char E[sizeof(LazyArchive)];
345   alignas(LazyObject) char F[sizeof(LazyObject)];
346 };
347
348 void printTraceSymbol(Symbol *Sym);
349
350 template <typename T, typename... ArgT>
351 void replaceSymbol(Symbol *S, ArgT &&... Arg) {
352   static_assert(std::is_trivially_destructible<T>(),
353                 "Symbol types must be trivially destructible");
354   static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
355   static_assert(alignof(T) <= alignof(SymbolUnion),
356                 "SymbolUnion not aligned enough");
357   assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
358          "Not a Symbol");
359
360   Symbol Sym = *S;
361
362   new (S) T(std::forward<ArgT>(Arg)...);
363
364   S->VersionId = Sym.VersionId;
365   S->Visibility = Sym.Visibility;
366   S->IsUsedInRegularObj = Sym.IsUsedInRegularObj;
367   S->ExportDynamic = Sym.ExportDynamic;
368   S->CanInline = Sym.CanInline;
369   S->Traced = Sym.Traced;
370
371   // Print out a log message if --trace-symbol was specified.
372   // This is for debugging.
373   if (S->Traced)
374     printTraceSymbol(S);
375 }
376
377 void warnUnorderableSymbol(const Symbol *Sym);
378 } // namespace elf
379
380 std::string toString(const elf::Symbol &B);
381 } // namespace lld
382
383 #endif