blob: d874203be26c32974e0cff1f763dc38fb5030616 [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:54 +00001//===- Symbols.h ------------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
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//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// All symbols are handled as SymbolBodies regardless of their types.
11// This file defines various types of SymbolBodies.
12//
13// File-scope symbols in ELF objects are the only exception of SymbolBody
14// instantiation. We will never create SymbolBodies for them for performance
15// reason. They are often represented as nullptrs. This is fine for symbol
16// resolution because the symbol table naturally cares only about
17// externally-visible symbols. For relocations, you have to deal with both
18// local and non-local functions, and we have two different functions
19// where we need them.
20//
21//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23#ifndef LLD_ELF_SYMBOLS_H
24#define LLD_ELF_SYMBOLS_H
25
Rafael Espindola9d06ab62015-09-22 00:01:39 +000026#include "InputSection.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000027
Michael J. Spencer84487f12015-07-24 21:03:07 +000028#include "lld/Core/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000029#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000030#include "llvm/Object/ELF.h"
31
32namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000033namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000034
Michael J. Spencer1b348a62015-09-04 22:28:10 +000035class ArchiveFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036class InputFile;
37class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000038template <class ELFT> class ObjectFile;
Michael J. Spencer658dccd2015-09-18 22:13:25 +000039template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000040template <class ELFT> class OutputSectionBase;
Rui Ueyama35da9b62015-10-11 20:59:12 +000041template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000042
Rui Ueyamaa4a628f2016-01-13 18:55:39 +000043// Returns a demangled C++ symbol name. If Name is not a mangled
44// name or the system does not provide __cxa_demangle function,
45// it returns the unmodified string.
46std::string demangle(StringRef Name);
47
Michael J. Spencer84487f12015-07-24 21:03:07 +000048// A real symbol object, SymbolBody, is usually accessed indirectly
49// through a Symbol. There's always one Symbol for each symbol name.
50// The resolver updates SymbolBody pointers as it resolves symbols.
51struct Symbol {
Michael J. Spencer84487f12015-07-24 21:03:07 +000052 SymbolBody *Body;
53};
54
55// The base class for real symbol classes.
56class SymbolBody {
57public:
58 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000059 DefinedFirst,
60 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000061 SharedKind,
Rafael Espindola67d72c02016-03-11 12:06:30 +000062 DefinedLocalKind,
63 DefinedElfLast = DefinedLocalKind,
Rafael Espindola11191912015-12-24 16:23:37 +000064 DefinedCommonKind,
Rafael Espindola9f77ef02016-02-12 20:54:57 +000065 DefinedBitcodeKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000066 DefinedSyntheticKind,
67 DefinedLast = DefinedSyntheticKind,
Rafael Espindola5d7593b2015-12-22 23:00:50 +000068 UndefinedElfKind,
Rafael Espindola84aff152015-09-25 21:20:23 +000069 UndefinedKind,
70 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000071 };
72
Michael J. Spencercdae0a42015-07-28 22:58:25 +000073 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000074
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000075 bool isWeak() const { return IsWeak; }
Rafael Espindola5d7593b2015-12-22 23:00:50 +000076 bool isUndefined() const {
77 return SymbolKind == UndefinedKind || SymbolKind == UndefinedElfKind;
78 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000079 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000080 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000081 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000082 bool isShared() const { return SymbolKind == SharedKind; }
Rafael Espindola67d72c02016-03-11 12:06:30 +000083 bool isLocal() const { return SymbolKind == DefinedLocalKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000084 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000085
Michael J. Spencer84487f12015-07-24 21:03:07 +000086 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000087 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000088
Rui Ueyama8f2c4da2015-10-21 18:13:47 +000089 uint8_t getVisibility() const { return Visibility; }
Rafael Espindola78471f02015-09-01 23:12:52 +000090
Rui Ueyama572a6f72016-01-29 01:49:33 +000091 unsigned DynsymIndex = 0;
George Rimar90cd0a82015-12-01 19:20:26 +000092 uint32_t GlobalDynIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000093 uint32_t GotIndex = -1;
George Rimar648a2c32015-10-20 08:54:27 +000094 uint32_t GotPltIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000095 uint32_t PltIndex = -1;
George Rimar90cd0a82015-12-01 19:20:26 +000096 bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
Rafael Espindola5c2310c2015-09-18 14:40:19 +000097 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000098 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000099
Rui Ueyamab5a69702016-02-01 21:00:35 +0000100 template <class ELFT>
Rafael Espindola87d9f102016-03-11 12:19:05 +0000101 typename llvm::object::ELFFile<ELFT>::uintX_t
102 getVA(typename llvm::object::ELFFile<ELFT>::uintX_t Addend = 0) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000103 template <class ELFT>
104 typename llvm::object::ELFFile<ELFT>::uintX_t getGotVA() const;
105 template <class ELFT>
106 typename llvm::object::ELFFile<ELFT>::uintX_t getGotPltVA() const;
107 template <class ELFT>
108 typename llvm::object::ELFFile<ELFT>::uintX_t getPltVA() const;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000109 template <class ELFT>
110 typename llvm::object::ELFFile<ELFT>::uintX_t getSize() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000111
Michael J. Spencer84487f12015-07-24 21:03:07 +0000112 // A SymbolBody has a backreference to a Symbol. Originally they are
113 // doubly-linked. A backreference will never change. But the pointer
114 // in the Symbol may be mutated by the resolver. If you have a
115 // pointer P to a SymbolBody and are not sure whether the resolver
116 // has chosen the object among other objects having the same name,
117 // you can access P->Backref->Body to get the resolver's result.
118 void setBackref(Symbol *P) { Backref = P; }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000119 SymbolBody &repl() { return Backref ? *Backref->Body : *this; }
Rui Ueyamadeb15402016-01-07 17:20:07 +0000120 Symbol *getSymbol() { return Backref; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000121
122 // Decides which symbol should "win" in the symbol table, this or
123 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
124 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000125 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000126
127protected:
Igor Kudrin65bddea2015-10-09 09:58:39 +0000128 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000129 uint8_t Type)
Rafael Espindolaabebed92016-02-05 15:27:15 +0000130 : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility),
George Rimar2f0fab52016-03-06 06:26:18 +0000131 MustBeInDynSym(false), NeedsCopyOrPltAddr(false), Name(Name) {
132 IsFunc = Type == llvm::ELF::STT_FUNC;
133 IsTls = Type == llvm::ELF::STT_TLS;
Rafael Espindola18173d42015-09-08 15:50:05 +0000134 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
135 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000136
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000137 const unsigned SymbolKind : 8;
Rafael Espindola8614c562015-10-06 14:33:58 +0000138 unsigned IsWeak : 1;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000139 unsigned Visibility : 2;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000140
141 // True if the symbol was used for linking and thus need to be
142 // added to the output file's symbol table. It is usually true,
143 // but if it is a shared symbol that were not referenced by anyone,
144 // it can be false.
Rafael Espindola18173d42015-09-08 15:50:05 +0000145 unsigned IsUsedInRegularObj : 1;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000146
Rafael Espindolaabebed92016-02-05 15:27:15 +0000147public:
Rui Ueyama7d332f52015-12-25 06:55:39 +0000148 // If true, the symbol is added to .dynsym symbol table.
Rafael Espindolaabebed92016-02-05 15:27:15 +0000149 unsigned MustBeInDynSym : 1;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000150
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000151 // True if the linker has to generate a copy relocation for this shared
152 // symbol or if the symbol should point to its plt entry.
153 unsigned NeedsCopyOrPltAddr : 1;
154
George Rimar2f0fab52016-03-06 06:26:18 +0000155 unsigned IsTls : 1;
156 unsigned IsFunc : 1;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000157
George Rimar2f0fab52016-03-06 06:26:18 +0000158protected:
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000159 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000160 Symbol *Backref = nullptr;
161};
162
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000163// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000164class Defined : public SymbolBody {
165public:
Davide Italiano255730c2016-03-04 01:55:28 +0000166 Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
167 uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000168 static bool classof(const SymbolBody *S) { return S->isDefined(); }
169};
170
171// Any defined symbol from an ELF file.
172template <class ELFT> class DefinedElf : public Defined {
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000173protected:
Rafael Espindola88dddf92015-12-21 21:07:31 +0000174 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000175
176public:
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000177 DefinedElf(Kind K, StringRef N, const Elf_Sym &Sym)
178 : Defined(K, N, Sym.getBinding() == llvm::ELF::STB_WEAK,
Davide Italiano255730c2016-03-04 01:55:28 +0000179 Sym.getVisibility(), Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000180 Sym(Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000181
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000182 const Elf_Sym &Sym;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000183 static bool classof(const SymbolBody *S) {
184 return S->kind() <= DefinedElfLast;
185 }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000186};
187
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000188class DefinedBitcode : public Defined {
189public:
Rafael Espindola4f29c1a2016-03-07 17:14:36 +0000190 DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000191 static bool classof(const SymbolBody *S);
192};
193
Rafael Espindola11191912015-12-24 16:23:37 +0000194class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000195public:
Rafael Espindola11191912015-12-24 16:23:37 +0000196 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, bool IsWeak,
197 uint8_t Visibility);
Rafael Espindola51d46902015-08-28 21:26:51 +0000198
199 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000200 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000201 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000202
203 // The output offset of this common symbol in the output bss. Computed by the
204 // writer.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000205 uint64_t OffsetInBss;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000206
207 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000208 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000209
210 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000211};
212
Michael J. Spencer84487f12015-07-24 21:03:07 +0000213// Regular defined symbols read from object file symbol tables.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000214template <class ELFT> class DefinedRegular : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000215 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000216
Michael J. Spencer84487f12015-07-24 21:03:07 +0000217public:
Rafael Espindolac159c962015-10-19 21:00:02 +0000218 DefinedRegular(StringRef N, const Elf_Sym &Sym,
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000219 InputSectionBase<ELFT> *Section)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000220 : DefinedElf<ELFT>(SymbolBody::DefinedRegularKind, N, Sym),
Rui Ueyama0b289522016-02-25 18:43:51 +0000221 Section(Section ? Section->Repl : NullInputSection) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000222
223 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000224 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000225 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000226
Rui Ueyama0b289522016-02-25 18:43:51 +0000227 // The input section this symbol belongs to. Notice that this is
228 // a reference to a pointer. We are using two levels of indirections
229 // because of ICF. If ICF decides two sections need to be merged, it
230 // manipulates this Section pointers so that they point to the same
231 // section. This is a bit tricky, so be careful to not be confused.
232 // If this is null, the symbol is an absolute symbol.
233 InputSectionBase<ELFT> *&Section;
234
235private:
236 static InputSectionBase<ELFT> *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000237};
238
Rui Ueyama0b289522016-02-25 18:43:51 +0000239template <class ELFT>
240InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
241
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000242// DefinedSynthetic is a class to represent linker-generated ELF symbols.
243// The difference from the regular symbol is that DefinedSynthetic symbols
244// don't belong to any input files or sections. Thus, its constructor
245// takes an output section to calculate output VA, etc.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000246template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000247public:
Rafael Espindola88dddf92015-12-21 21:07:31 +0000248 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000249 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimaraa4dc202016-03-01 16:23:13 +0000250 DefinedSynthetic(StringRef N, uintX_t Value, OutputSectionBase<ELFT> &Section,
251 uint8_t Visibility);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000252
253 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000254 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000255 }
256
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000257 uintX_t Value;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000258 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000259};
260
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000261// Undefined symbol.
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000262class Undefined : public SymbolBody {
263 typedef SymbolBody::Kind Kind;
264 bool CanKeepUndefined;
265
266protected:
Davide Italiano255730c2016-03-04 01:55:28 +0000267 Undefined(Kind K, StringRef N, bool IsWeak, uint8_t Visibility, uint8_t Type);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000268
269public:
270 Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
271 bool CanKeepUndefined);
272
273 static bool classof(const SymbolBody *S) { return S->isUndefined(); }
274
275 bool canKeepUndefined() const { return CanKeepUndefined; }
276};
277
278template <class ELFT> class UndefinedElf : public Undefined {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000279 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000280
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000281public:
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000282 UndefinedElf(StringRef N, const Elf_Sym &Sym);
283 const Elf_Sym &Sym;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000284
285 static bool classof(const SymbolBody *S) {
286 return S->kind() == SymbolBody::UndefinedElfKind;
287 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000288};
289
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000290template <class ELFT> class SharedSymbol : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000291 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000292 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000293
294public:
295 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000296 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000297 }
298
Rui Ueyama35da9b62015-10-11 20:59:12 +0000299 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000300 : DefinedElf<ELFT>(SymbolBody::SharedKind, Name, Sym), File(F) {}
Rui Ueyama35da9b62015-10-11 20:59:12 +0000301
302 SharedFile<ELFT> *File;
George Rimarbc590fe2015-10-28 16:48:58 +0000303
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000304 // OffsetInBss is significant only when needsCopy() is true.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000305 uintX_t OffsetInBss = 0;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000306
George Rimar2f0fab52016-03-06 06:26:18 +0000307 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->IsFunc; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000308};
309
Rafael Espindola67d72c02016-03-11 12:06:30 +0000310template <class ELFT> class LocalSymbol : public DefinedElf<ELFT> {
311 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
312
313public:
Rafael Espindolaccb8b4d2016-03-11 12:14:02 +0000314 LocalSymbol(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section)
315 : DefinedElf<ELFT>(SymbolBody::DefinedLocalKind, "", Sym),
316 Section(Section) {}
Rafael Espindola67d72c02016-03-11 12:06:30 +0000317
318 static bool classof(const SymbolBody *S) {
319 return S->kind() == SymbolBody::DefinedLocalKind;
320 }
Rafael Espindolaccb8b4d2016-03-11 12:14:02 +0000321
322 InputSectionBase<ELFT> *Section;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000323};
324
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000325// This class represents a symbol defined in an archive file. It is
326// created from an archive file header, and it knows how to load an
327// object file from an archive to replace itself with a defined
328// symbol. If the resolver finds both Undefined and Lazy for
329// the same name, it will ask the Lazy to load a file.
330class Lazy : public SymbolBody {
331public:
332 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
George Rimar5c36e592016-02-02 09:28:53 +0000333 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT,
Davide Italiano255730c2016-03-04 01:55:28 +0000334 /* Type */ 0),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000335 File(F), Sym(S) {}
336
337 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
338
339 // Returns an object file for this symbol, or a nullptr if the file
340 // was already returned.
341 std::unique_ptr<InputFile> getMember();
342
Rafael Espindola8614c562015-10-06 14:33:58 +0000343 void setWeak() { IsWeak = true; }
344 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
345
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000346private:
347 ArchiveFile *File;
348 const llvm::object::Archive::Symbol Sym;
349};
350
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000351// Some linker-generated symbols need to be created as
352// DefinedRegular symbols, so they need Elf_Sym symbols.
353// Here we allocate such Elf_Sym symbols statically.
354template <class ELFT> struct ElfSym {
355 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
356
Rafael Espindola65e80b92016-01-19 21:19:52 +0000357 // Used to represent an undefined symbol which we don't want to add to the
358 // output file's symbol table. It has weak binding and can be substituted.
359 static Elf_Sym Ignored;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000360
George Rimar9e859392016-02-26 14:36:36 +0000361 // The content for _etext and etext symbols.
362 static Elf_Sym Etext;
363
364 // The content for _edata and edata symbols.
365 static Elf_Sym Edata;
366
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000367 // The content for _end and end symbols.
368 static Elf_Sym End;
369
370 // The content for _gp symbol for MIPS target.
371 static Elf_Sym MipsGp;
372
373 // __rel_iplt_start/__rel_iplt_end for signaling
374 // where R_[*]_IRELATIVE relocations do live.
375 static Elf_Sym RelaIpltStart;
376 static Elf_Sym RelaIpltEnd;
377};
378
Rafael Espindola65e80b92016-01-19 21:19:52 +0000379template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::Ignored;
George Rimar9e859392016-02-26 14:36:36 +0000380template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::Etext;
381template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::Edata;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000382template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::End;
383template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::MipsGp;
384template <class ELFT>
385typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::RelaIpltStart;
386template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::RelaIpltEnd;
387
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000388} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000389} // namespace lld
390
391#endif