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"
20 #include "lld/Core/LLVM.h"
21 #include "llvm/Object/Archive.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Support/AlignOf.h"
33 template <class ELFT> class ObjectFile;
34 template <class ELFT> class OutputSection;
35 template <class ELFT> class OutputSectionBase;
36 template <class ELFT> class SharedFile;
40 // The base class for real symbol classes.
45 DefinedRegularKind = DefinedFirst,
50 DefinedLast = DefinedSyntheticKind,
56 SymbolBody(Kind K) : SymbolKind(K) {}
59 const Symbol *symbol() const {
60 return const_cast<SymbolBody *>(this)->symbol();
63 Kind kind() const { return static_cast<Kind>(SymbolKind); }
65 bool isUndefined() const { return SymbolKind == UndefinedKind; }
66 bool isDefined() const { return SymbolKind <= DefinedLast; }
67 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
69 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
71 bool isShared() const { return SymbolKind == SharedKind; }
72 bool isLocal() const { return IsLocal; }
73 bool isPreemptible() const;
75 StringRef getName() const;
76 void setName(StringRef S);
78 uint32_t getNameOffset() const {
83 uint8_t getVisibility() const { return StOther & 0x3; }
85 unsigned DynsymIndex = 0;
86 uint32_t GotIndex = -1;
87 uint32_t GotPltIndex = -1;
88 uint32_t PltIndex = -1;
89 uint32_t GlobalDynIndex = -1;
90 bool isInGot() const { return GotIndex != -1U; }
91 bool isInPlt() const { return PltIndex != -1U; }
92 template <class ELFT> bool hasThunk() const;
95 typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
97 template <class ELFT> typename ELFT::uint getGotOffset() const;
98 template <class ELFT> typename ELFT::uint getGotVA() const;
99 template <class ELFT> typename ELFT::uint getGotPltOffset() const;
100 template <class ELFT> typename ELFT::uint getGotPltVA() const;
101 template <class ELFT> typename ELFT::uint getPltVA() const;
102 template <class ELFT> typename ELFT::uint getThunkVA() const;
103 template <class ELFT> typename ELFT::uint getSize() const;
105 // The file from which this symbol was created.
106 InputFile *File = nullptr;
109 SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
111 SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
113 const unsigned SymbolKind : 8;
116 // True if the linker has to generate a copy relocation for this shared
117 // symbol or if the symbol should point to its plt entry.
118 unsigned NeedsCopyOrPltAddr : 1;
120 // True if this is a local symbol.
121 unsigned IsLocal : 1;
123 // True if this symbol has an entry in the global part of MIPS GOT.
124 unsigned IsInGlobalMipsGot : 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; }
155 // The base class for any defined symbols.
156 class Defined : public SymbolBody {
158 Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
159 Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
160 static bool classof(const SymbolBody *S) { return S->isDefined(); }
163 // The defined symbol in LLVM bitcode files.
164 class DefinedBitcode : public Defined {
166 DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type, BitcodeFile *F);
167 static bool classof(const SymbolBody *S);
168 BitcodeFile *file() { return (BitcodeFile *)this->File; }
171 class DefinedCommon : public Defined {
173 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther,
174 uint8_t Type, InputFile *File);
176 static bool classof(const SymbolBody *S) {
177 return S->kind() == SymbolBody::DefinedCommonKind;
180 // The output offset of this common symbol in the output bss. Computed by the
182 uint64_t OffsetInBss;
184 // The maximum alignment we have seen for this symbol.
190 // Regular defined symbols read from object file symbol tables.
191 template <class ELFT> class DefinedRegular : public Defined {
192 typedef typename ELFT::Sym Elf_Sym;
193 typedef typename ELFT::uint uintX_t;
196 DefinedRegular(StringRef Name, const Elf_Sym &Sym,
197 InputSectionBase<ELFT> *Section)
198 : Defined(SymbolBody::DefinedRegularKind, Name, Sym.st_other,
200 Value(Sym.st_value), Size(Sym.st_size),
201 Section(Section ? Section->Repl : NullInputSection) {
203 this->File = Section->getFile();
206 DefinedRegular(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section)
207 : Defined(SymbolBody::DefinedRegularKind, Sym.st_name, Sym.st_other,
209 Value(Sym.st_value), Size(Sym.st_size),
210 Section(Section ? Section->Repl : NullInputSection) {
213 this->File = Section->getFile();
216 DefinedRegular(StringRef Name, uint8_t StOther)
217 : Defined(SymbolBody::DefinedRegularKind, Name, StOther,
218 llvm::ELF::STT_NOTYPE),
219 Value(0), Size(0), Section(NullInputSection) {}
221 static bool classof(const SymbolBody *S) {
222 return S->kind() == SymbolBody::DefinedRegularKind;
228 // The input section this symbol belongs to. Notice that this is
229 // a reference to a pointer. We are using two levels of indirections
230 // because of ICF. If ICF decides two sections need to be merged, it
231 // manipulates this Section pointers so that they point to the same
232 // section. This is a bit tricky, so be careful to not be confused.
233 // If this is null, the symbol is an absolute symbol.
234 InputSectionBase<ELFT> *&Section;
236 // If non-null the symbol has a Thunk that may be used as an alternative
237 // destination for callers of this Symbol.
238 Thunk<ELFT> *ThunkData = nullptr;
241 static InputSectionBase<ELFT> *NullInputSection;
244 template <class ELFT>
245 InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
247 // DefinedSynthetic is a class to represent linker-generated ELF symbols.
248 // The difference from the regular symbol is that DefinedSynthetic symbols
249 // don't belong to any input files or sections. Thus, its constructor
250 // takes an output section to calculate output VA, etc.
251 // If Section is null, this symbol is relative to the image base.
252 template <class ELFT> class DefinedSynthetic : public Defined {
254 typedef typename ELFT::uint uintX_t;
255 DefinedSynthetic(StringRef N, uintX_t Value,
256 OutputSectionBase<ELFT> *Section);
258 static bool classof(const SymbolBody *S) {
259 return S->kind() == SymbolBody::DefinedSyntheticKind;
262 // Special value designates that the symbol 'points'
263 // to the end of the section.
264 static const uintX_t SectionEnd = uintX_t(-1);
267 const OutputSectionBase<ELFT> *Section;
270 class Undefined : public SymbolBody {
272 Undefined(StringRef Name, uint8_t StOther, uint8_t Type, InputFile *F);
273 Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type, InputFile *F);
275 static bool classof(const SymbolBody *S) {
276 return S->kind() == UndefinedKind;
279 InputFile *file() { return this->File; }
282 template <class ELFT> class SharedSymbol : public Defined {
283 typedef typename ELFT::Sym Elf_Sym;
284 typedef typename ELFT::Verdef Elf_Verdef;
285 typedef typename ELFT::uint uintX_t;
288 static bool classof(const SymbolBody *S) {
289 return S->kind() == SymbolBody::SharedKind;
292 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
293 const Elf_Verdef *Verdef)
294 : Defined(SymbolBody::SharedKind, Name, Sym.st_other, Sym.getType()),
295 Sym(Sym), Verdef(Verdef) {
296 // IFuncs defined in DSOs are treated as functions by the static linker.
298 Type = llvm::ELF::STT_FUNC;
302 SharedFile<ELFT> *file() { return (SharedFile<ELFT> *)this->File; }
306 // This field is a pointer to the symbol's version definition.
307 const Elf_Verdef *Verdef;
309 // OffsetInBss is significant only when needsCopy() is true.
310 uintX_t OffsetInBss = 0;
312 // If non-null the symbol has a Thunk that may be used as an alternative
313 // destination for callers of this Symbol.
314 Thunk<ELFT> *ThunkData = nullptr;
315 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
318 // This class represents a symbol defined in an archive file. It is
319 // created from an archive file header, and it knows how to load an
320 // object file from an archive to replace itself with a defined
321 // symbol. If the resolver finds both Undefined and Lazy for
322 // the same name, it will ask the Lazy to load a file.
323 class Lazy : public SymbolBody {
325 static bool classof(const SymbolBody *S) { return S->isLazy(); }
327 // Returns an object file for this symbol, or a nullptr if the file
328 // was already returned.
329 std::unique_ptr<InputFile> fetch();
332 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
333 : SymbolBody(K, Name, llvm::ELF::STV_DEFAULT, Type) {}
336 // LazyArchive symbols represents symbols in archive files.
337 class LazyArchive : public Lazy {
339 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
342 static bool classof(const SymbolBody *S) {
343 return S->kind() == LazyArchiveKind;
346 ArchiveFile *file() { return (ArchiveFile *)this->File; }
347 std::unique_ptr<InputFile> fetch();
350 const llvm::object::Archive::Symbol Sym;
353 // LazyObject symbols represents symbols in object files between
354 // --start-lib and --end-lib options.
355 class LazyObject : public Lazy {
357 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
359 static bool classof(const SymbolBody *S) {
360 return S->kind() == LazyObjectKind;
363 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
364 std::unique_ptr<InputFile> fetch();
367 // Some linker-generated symbols need to be created as
368 // DefinedRegular symbols.
369 template <class ELFT> struct ElfSym {
370 // The content for _etext and etext symbols.
371 static DefinedRegular<ELFT> *Etext;
372 static DefinedRegular<ELFT> *Etext2;
374 // The content for _edata and edata symbols.
375 static DefinedRegular<ELFT> *Edata;
376 static DefinedRegular<ELFT> *Edata2;
378 // The content for _end and end symbols.
379 static DefinedRegular<ELFT> *End;
380 static DefinedRegular<ELFT> *End2;
382 // The content for _gp_disp symbol for MIPS target.
383 static SymbolBody *MipsGpDisp;
386 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
387 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
388 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
389 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
390 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
391 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
392 template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGpDisp;
394 // A real symbol object, SymbolBody, is usually stored within a Symbol. There's
395 // always one Symbol for each symbol name. The resolver updates the SymbolBody
396 // stored in the Body field of this object as it resolves symbols. Symbol also
397 // holds computed properties of symbol names.
399 // Symbol binding. This is on the Symbol to track changes during resolution.
401 // An undefined weak is still weak when it resolves to a shared library.
402 // An undefined weak will not fetch archive members, but we have to remember
406 // Version definition index.
409 // Symbol visibility. This is the computed minimum visibility of all
410 // observed non-DSO symbols.
411 unsigned Visibility : 2;
413 // True if the symbol was used for linking and thus need to be added to the
414 // output file's symbol table. This is true for all symbols except for
415 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
416 // by other bitcode objects.
417 unsigned IsUsedInRegularObj : 1;
419 // If this flag is true and the symbol has protected or default visibility, it
420 // will appear in .dynsym. This flag is set by interposable DSO symbols in
421 // executables, by most symbols in DSOs and executables built with
422 // --export-dynamic, and by dynamic lists.
423 unsigned ExportDynamic : 1;
425 // True if this symbol is specified by --trace-symbol option.
428 bool includeInDynsym() const;
429 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
431 // This field is used to store the Symbol's SymbolBody. This instantiation of
432 // AlignedCharArrayUnion gives us a struct with a char array field that is
433 // large and aligned enough to store any derived class of SymbolBody. We
434 // assume that the size and alignment of ELF64LE symbols is sufficient for any
435 // ELFT, and we verify this with the static_asserts in replaceBody.
436 llvm::AlignedCharArrayUnion<
437 DefinedBitcode, DefinedCommon, DefinedRegular<llvm::object::ELF64LE>,
438 DefinedSynthetic<llvm::object::ELF64LE>, Undefined,
439 SharedSymbol<llvm::object::ELF64LE>, LazyArchive, LazyObject>
442 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
443 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
446 void printTraceSymbol(Symbol *Sym);
448 template <typename T, typename... ArgT>
449 void replaceBody(Symbol *S, ArgT &&... Arg) {
450 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
451 static_assert(llvm::AlignOf<T>::Alignment <=
452 llvm::AlignOf<decltype(S->Body)>::Alignment,
453 "Body not aligned enough");
454 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
457 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
459 // Print out a log message if --trace-symbol was specified.
460 // This is for debugging.
465 inline Symbol *SymbolBody::symbol() {
467 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
468 offsetof(Symbol, Body));