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;
34 template <class ELFT> class SharedFile;
38 // The base class for real symbol classes.
43 DefinedRegularKind = DefinedFirst,
46 DefinedLast = DefinedCommonKind,
52 SymbolBody(Kind K) : SymbolKind(K) {}
55 const Symbol *symbol() const {
56 return const_cast<SymbolBody *>(this)->symbol();
59 Kind kind() const { return static_cast<Kind>(SymbolKind); }
61 bool isUndefined() const { return SymbolKind == UndefinedKind; }
62 bool isDefined() const { return SymbolKind <= DefinedLast; }
63 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
65 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
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();
75 bool isInGot() const { return GotIndex != -1U; }
76 bool isInPlt() const { return PltIndex != -1U; }
78 uint64_t getVA(int64_t Addend = 0) const;
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;
88 // The file from which this symbol was created.
89 InputFile *File = nullptr;
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;
98 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
101 const unsigned SymbolKind : 8;
104 // True if the linker has to generate a copy relocation.
105 // For SharedSymbol only.
106 unsigned NeedsCopy : 1;
108 // True the symbol should point to its PLT entry.
109 // For SharedSymbol only.
110 unsigned NeedsPltAddr : 1;
112 // True if this is a local symbol.
113 unsigned IsLocal : 1;
115 // True if this symbol has an entry in the global part of MIPS GOT.
116 unsigned IsInGlobalMipsGot : 1;
118 // True if this symbol is referenced by 32-bit GOT relocations.
119 unsigned Is32BitMipsGot : 1;
121 // True if this symbol is in the Iplt sub-section of the Plt.
122 unsigned IsInIplt : 1;
124 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
125 unsigned IsInIgot : 1;
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
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 };
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; }
149 // The base class for any defined symbols.
150 class Defined : public SymbolBody {
152 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
153 static bool classof(const SymbolBody *S) { return S->isDefined(); }
156 class DefinedCommon : public Defined {
158 DefinedCommon(StringRef N, uint64_t Size, uint32_t Alignment, uint8_t StOther,
159 uint8_t Type, InputFile *File);
161 static bool classof(const SymbolBody *S) {
162 return S->kind() == SymbolBody::DefinedCommonKind;
165 // The output offset of this common symbol in the output bss. Computed by the
169 // The maximum alignment we have seen for this symbol.
175 // Regular defined symbols read from object file symbol tables.
176 class DefinedRegular : public Defined {
178 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
179 uint64_t Value, uint64_t Size, SectionBase *Section,
181 : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
182 Value(Value), Size(Size), Section(Section) {
186 // Return true if the symbol is a PIC function.
187 template <class ELFT> bool isMipsPIC() const;
189 static bool classof(const SymbolBody *S) {
190 return S->kind() == SymbolBody::DefinedRegularKind;
195 SectionBase *Section;
198 class Undefined : public SymbolBody {
200 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
203 static bool classof(const SymbolBody *S) {
204 return S->kind() == UndefinedKind;
208 class SharedSymbol : public Defined {
210 static bool classof(const SymbolBody *S) {
211 return S->kind() == SymbolBody::SharedKind;
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.
220 Type = llvm::ELF::STT_FUNC;
224 template <class ELFT> uint64_t getShndx() const {
225 return getSym<ELFT>().st_shndx;
228 template <class ELFT> uint64_t getValue() const {
229 return getSym<ELFT>().st_value;
232 template <class ELFT> uint64_t getSize() const {
233 return getSym<ELFT>().st_size;
236 template <class ELFT> uint32_t getAlignment() const;
238 // This field is a pointer to the symbol's version definition.
241 // CopyRelSec and CopyRelSecOff are significant only when NeedsCopy is true.
242 InputSection *CopyRelSec;
243 uint64_t CopyRelSecOff;
246 template <class ELFT> const typename ELFT::Sym &getSym() const {
247 return *(const typename ELFT::Sym *)ElfSym;
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 {
260 static bool classof(const SymbolBody *S) { return S->isLazy(); }
262 // Returns an object file for this symbol, or a nullptr if the file
263 // was already returned.
267 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
268 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
271 // LazyArchive symbols represents symbols in archive files.
272 class LazyArchive : public Lazy {
274 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
277 static bool classof(const SymbolBody *S) {
278 return S->kind() == LazyArchiveKind;
281 ArchiveFile *file() { return (ArchiveFile *)this->File; }
285 const llvm::object::Archive::Symbol Sym;
288 // LazyObject symbols represents symbols in object files between
289 // --start-lib and --end-lib options.
290 class LazyObject : public Lazy {
292 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
294 static bool classof(const SymbolBody *S) {
295 return S->kind() == LazyObjectKind;
298 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
302 // Some linker-generated symbols need to be created as
303 // DefinedRegular symbols.
306 static DefinedRegular *Bss;
309 static DefinedRegular *Etext1;
310 static DefinedRegular *Etext2;
313 static DefinedRegular *Edata1;
314 static DefinedRegular *Edata2;
317 static DefinedRegular *End1;
318 static DefinedRegular *End2;
320 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
321 static DefinedRegular *MipsGp;
322 static DefinedRegular *MipsGpDisp;
323 static DefinedRegular *MipsLocalGp;
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.
331 // Symbol binding. This is on the Symbol to track changes during resolution.
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
338 // Version definition index.
341 // Symbol visibility. This is the computed minimum visibility of all
342 // observed non-DSO symbols.
343 unsigned Visibility : 2;
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;
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;
357 // True if this symbol is specified by --trace-symbol option.
360 // This symbol version was found in a version script.
361 unsigned InVersionScript : 1;
363 bool includeInDynsym() const;
364 uint8_t computeBinding() const;
365 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
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>
374 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
375 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
378 void printTraceSymbol(Symbol *Sym);
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 &&
388 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
390 // Print out a log message if --trace-symbol was specified.
391 // This is for debugging.
396 inline Symbol *SymbolBody::symbol() {
398 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
399 offsetof(Symbol, Body));
403 std::string toString(const elf::SymbolBody &B);