1 //===- Symbols.h ------------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // All symbols are handled as SymbolBodies regardless of their types.
11 // This file defines various types of SymbolBodies.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLD_ELF_SYMBOLS_H
16 #define LLD_ELF_SYMBOLS_H
18 #include "InputSection.h"
21 #include "lld/Core/LLVM.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/ELF.h"
32 template <class ELFT> class ObjectFile;
33 template <class ELFT> class OutputSection;
34 class OutputSectionBase;
35 template <class ELFT> class SharedFile;
39 // The base class for real symbol classes.
44 DefinedRegularKind = DefinedFirst,
48 DefinedLast = DefinedSyntheticKind,
54 SymbolBody(Kind K) : SymbolKind(K) {}
57 const Symbol *symbol() const {
58 return const_cast<SymbolBody *>(this)->symbol();
61 Kind kind() const { return static_cast<Kind>(SymbolKind); }
63 bool isUndefined() const { return SymbolKind == UndefinedKind; }
64 bool isDefined() const { return SymbolKind <= DefinedLast; }
65 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
67 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
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();
76 bool isInGot() const { return GotIndex != -1U; }
77 bool isInPlt() const { return PltIndex != -1U; }
78 template <class ELFT> bool hasThunk() const;
81 typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
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;
91 // The file from which this symbol was created.
92 InputFile *File = nullptr;
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;
101 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
104 const unsigned SymbolKind : 8;
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;
111 // True if this is a local symbol.
112 unsigned IsLocal : 1;
114 // True if this symbol has an entry in the global part of MIPS GOT.
115 unsigned IsInGlobalMipsGot : 1;
117 // True if this symbol is referenced by 32-bit GOT relocations.
118 unsigned Is32BitMipsGot : 1;
120 // True if this symbol is in the Iplt sub-section of the Plt.
121 unsigned IsInIplt : 1;
123 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
124 unsigned IsInIgot : 1;
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
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 };
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; }
148 // The base class for any defined symbols.
149 class Defined : public SymbolBody {
151 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
152 static bool classof(const SymbolBody *S) { return S->isDefined(); }
155 class DefinedCommon : public Defined {
157 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther,
158 uint8_t Type, InputFile *File);
160 static bool classof(const SymbolBody *S) {
161 return S->kind() == SymbolBody::DefinedCommonKind;
164 // The output offset of this common symbol in the output bss. Computed by the
168 // The maximum alignment we have seen for this symbol.
174 // Regular defined symbols read from object file symbol tables.
175 template <class ELFT> class DefinedRegular : public Defined {
176 typedef typename ELFT::Sym Elf_Sym;
177 typedef typename ELFT::uint uintX_t;
180 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
181 uintX_t Value, uintX_t Size, InputSectionBase<ELFT> *Section,
183 : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
184 Value(Value), Size(Size),
185 Section(Section ? Section->Repl : NullInputSection) {
189 // Return true if the symbol is a PIC function.
190 bool isMipsPIC() const;
192 static bool classof(const SymbolBody *S) {
193 return S->kind() == SymbolBody::DefinedRegularKind;
199 // The input section this symbol belongs to. Notice that this is
200 // a reference to a pointer. We are using two levels of indirections
201 // because of ICF. If ICF decides two sections need to be merged, it
202 // manipulates this Section pointers so that they point to the same
203 // section. This is a bit tricky, so be careful to not be confused.
204 // If this is null, the symbol is an absolute symbol.
205 InputSectionBase<ELFT> *&Section;
207 // If non-null the symbol has a Thunk that may be used as an alternative
208 // destination for callers of this Symbol.
209 Thunk<ELFT> *ThunkData = nullptr;
212 static InputSectionBase<ELFT> *NullInputSection;
215 template <class ELFT>
216 InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
218 // DefinedSynthetic is a class to represent linker-generated ELF symbols.
219 // The difference from the regular symbol is that DefinedSynthetic symbols
220 // don't belong to any input files or sections. Thus, its constructor
221 // takes an output section to calculate output VA, etc.
222 // If Section is null, this symbol is relative to the image base.
223 class DefinedSynthetic : public Defined {
225 DefinedSynthetic(StringRef Name, uint64_t Value,
226 const OutputSectionBase *Section)
227 : Defined(SymbolBody::DefinedSyntheticKind, Name, /*IsLocal=*/false,
228 llvm::ELF::STV_HIDDEN, 0 /* Type */),
229 Value(Value), Section(Section) {}
231 static bool classof(const SymbolBody *S) {
232 return S->kind() == SymbolBody::DefinedSyntheticKind;
236 const OutputSectionBase *Section;
239 template <class ELFT> class Undefined : public SymbolBody {
241 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
244 static bool classof(const SymbolBody *S) {
245 return S->kind() == UndefinedKind;
248 // If non-null the symbol has a Thunk that may be used as an alternative
249 // destination for callers of this Symbol. When linking a DSO undefined
250 // symbols are implicitly imported, the symbol lookup will be performed by
251 // the dynamic loader. A call to an undefined symbol will be given a PLT
252 // entry and on ARM this may need a Thunk if the caller is in Thumb state.
253 Thunk<ELFT> *ThunkData = nullptr;
254 InputFile *file() { return this->File; }
257 template <class ELFT> class SharedSymbol : public Defined {
258 typedef typename ELFT::Sym Elf_Sym;
259 typedef typename ELFT::Verdef Elf_Verdef;
260 typedef typename ELFT::uint uintX_t;
263 static bool classof(const SymbolBody *S) {
264 return S->kind() == SymbolBody::SharedKind;
267 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
268 const Elf_Verdef *Verdef)
269 : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, Sym.st_other,
271 Sym(Sym), Verdef(Verdef) {
272 // IFuncs defined in DSOs are treated as functions by the static linker.
274 Type = llvm::ELF::STT_FUNC;
278 SharedFile<ELFT> *file() { return (SharedFile<ELFT> *)this->File; }
282 // This field is a pointer to the symbol's version definition.
283 const Elf_Verdef *Verdef;
285 // OffsetInBss is significant only when needsCopy() is true.
286 uintX_t OffsetInBss = 0;
288 // If non-null the symbol has a Thunk that may be used as an alternative
289 // destination for callers of this Symbol.
290 Thunk<ELFT> *ThunkData = nullptr;
291 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
294 // This class represents a symbol defined in an archive file. It is
295 // created from an archive file header, and it knows how to load an
296 // object file from an archive to replace itself with a defined
297 // symbol. If the resolver finds both Undefined and Lazy for
298 // the same name, it will ask the Lazy to load a file.
299 class Lazy : public SymbolBody {
301 static bool classof(const SymbolBody *S) { return S->isLazy(); }
303 // Returns an object file for this symbol, or a nullptr if the file
304 // was already returned.
308 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
309 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
312 // LazyArchive symbols represents symbols in archive files.
313 class LazyArchive : public Lazy {
315 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
318 static bool classof(const SymbolBody *S) {
319 return S->kind() == LazyArchiveKind;
322 ArchiveFile *file() { return (ArchiveFile *)this->File; }
326 const llvm::object::Archive::Symbol Sym;
329 // LazyObject symbols represents symbols in object files between
330 // --start-lib and --end-lib options.
331 class LazyObject : public Lazy {
333 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
335 static bool classof(const SymbolBody *S) {
336 return S->kind() == LazyObjectKind;
339 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
343 // Some linker-generated symbols need to be created as
344 // DefinedRegular symbols.
345 template <class ELFT> struct ElfSym {
346 // The content for __ehdr_start symbol.
347 static DefinedRegular<ELFT> *EhdrStart;
349 // The content for _etext and etext symbols.
350 static DefinedRegular<ELFT> *Etext;
351 static DefinedRegular<ELFT> *Etext2;
353 // The content for _edata and edata symbols.
354 static DefinedRegular<ELFT> *Edata;
355 static DefinedRegular<ELFT> *Edata2;
357 // The content for _end and end symbols.
358 static DefinedRegular<ELFT> *End;
359 static DefinedRegular<ELFT> *End2;
361 // The content for _gp_disp/__gnu_local_gp symbols for MIPS target.
362 static DefinedRegular<ELFT> *MipsGpDisp;
363 static DefinedRegular<ELFT> *MipsLocalGp;
364 static DefinedRegular<ELFT> *MipsGp;
367 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::EhdrStart;
368 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
369 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
370 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
371 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
372 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
373 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
374 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGpDisp;
375 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsLocalGp;
376 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGp;
378 // A real symbol object, SymbolBody, is usually stored within a Symbol. There's
379 // always one Symbol for each symbol name. The resolver updates the SymbolBody
380 // stored in the Body field of this object as it resolves symbols. Symbol also
381 // holds computed properties of symbol names.
383 // Symbol binding. This is on the Symbol to track changes during resolution.
385 // An undefined weak is still weak when it resolves to a shared library.
386 // An undefined weak will not fetch archive members, but we have to remember
390 // Version definition index.
393 // Symbol visibility. This is the computed minimum visibility of all
394 // observed non-DSO symbols.
395 unsigned Visibility : 2;
397 // True if the symbol was used for linking and thus need to be added to the
398 // output file's symbol table. This is true for all symbols except for
399 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
400 // by other bitcode objects.
401 unsigned IsUsedInRegularObj : 1;
403 // If this flag is true and the symbol has protected or default visibility, it
404 // will appear in .dynsym. This flag is set by interposable DSO symbols in
405 // executables, by most symbols in DSOs and executables built with
406 // --export-dynamic, and by dynamic lists.
407 unsigned ExportDynamic : 1;
409 // True if this symbol is specified by --trace-symbol option.
412 // This symbol version was found in a version script.
413 unsigned InVersionScript : 1;
415 bool includeInDynsym() const;
416 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
418 // This field is used to store the Symbol's SymbolBody. This instantiation of
419 // AlignedCharArrayUnion gives us a struct with a char array field that is
420 // large and aligned enough to store any derived class of SymbolBody. We
421 // assume that the size and alignment of ELF64LE symbols is sufficient for any
422 // ELFT, and we verify this with the static_asserts in replaceBody.
423 llvm::AlignedCharArrayUnion<
424 DefinedCommon, DefinedRegular<llvm::object::ELF64LE>, DefinedSynthetic,
425 Undefined<llvm::object::ELF64LE>, SharedSymbol<llvm::object::ELF64LE>,
426 LazyArchive, LazyObject>
429 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
430 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
433 void printTraceSymbol(Symbol *Sym);
435 template <typename T, typename... ArgT>
436 void replaceBody(Symbol *S, ArgT &&... Arg) {
437 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
438 static_assert(alignof(T) <= alignof(decltype(S->Body)),
439 "Body not aligned enough");
440 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
443 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
445 // Print out a log message if --trace-symbol was specified.
446 // This is for debugging.
451 inline Symbol *SymbolBody::symbol() {
453 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
454 offsetof(Symbol, Body));
457 std::string toString(const SymbolBody &B);