blob: 9edeb47bdf1fa20f2b80dfecf23313a718fa8e22 [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//
Rui Ueyama34f29242015-10-13 19:51:57 +000013//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000014
15#ifndef LLD_ELF_SYMBOLS_H
16#define LLD_ELF_SYMBOLS_H
17
Rafael Espindola9d06ab62015-09-22 00:01:39 +000018#include "InputSection.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000019
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "lld/Core/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000021#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022#include "llvm/Object/ELF.h"
23
24namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000025namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
Michael J. Spencer1b348a62015-09-04 22:28:10 +000027class ArchiveFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028class InputFile;
29class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000030template <class ELFT> class ObjectFile;
Michael J. Spencer658dccd2015-09-18 22:13:25 +000031template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000032template <class ELFT> class OutputSectionBase;
Rui Ueyama35da9b62015-10-11 20:59:12 +000033template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000034
Rui Ueyamaa4a628f2016-01-13 18:55:39 +000035// Returns a demangled C++ symbol name. If Name is not a mangled
36// name or the system does not provide __cxa_demangle function,
37// it returns the unmodified string.
38std::string demangle(StringRef Name);
39
Michael J. Spencer84487f12015-07-24 21:03:07 +000040// A real symbol object, SymbolBody, is usually accessed indirectly
41// through a Symbol. There's always one Symbol for each symbol name.
42// The resolver updates SymbolBody pointers as it resolves symbols.
43struct Symbol {
Michael J. Spencer84487f12015-07-24 21:03:07 +000044 SymbolBody *Body;
45};
46
47// The base class for real symbol classes.
48class SymbolBody {
49public:
50 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000051 DefinedFirst,
52 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000053 SharedKind,
Rafael Espindola11191912015-12-24 16:23:37 +000054 DefinedCommonKind,
Rafael Espindola9f77ef02016-02-12 20:54:57 +000055 DefinedBitcodeKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000056 DefinedSyntheticKind,
57 DefinedLast = DefinedSyntheticKind,
Rafael Espindola5d7593b2015-12-22 23:00:50 +000058 UndefinedElfKind,
Rafael Espindola84aff152015-09-25 21:20:23 +000059 UndefinedKind,
60 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000061 };
62
Michael J. Spencercdae0a42015-07-28 22:58:25 +000063 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000064
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000065 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
Rafael Espindola5d7593b2015-12-22 23:00:50 +000066 bool isUndefined() const {
67 return SymbolKind == UndefinedKind || SymbolKind == UndefinedElfKind;
68 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000069 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000070 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000071 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000072 bool isShared() const { return SymbolKind == SharedKind; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000073 bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; }
Rafael Espindola18173d42015-09-08 15:50:05 +000074 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rui Ueyamac4466602016-03-13 19:48:18 +000075 bool isPreemptible() const;
Rui Ueyama7ede5432016-03-13 04:40:14 +000076
Michael J. Spencer84487f12015-07-24 21:03:07 +000077 // Returns the symbol name.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000078 StringRef getName() const {
79 assert(!isLocal());
Rafael Espindola193b99c2016-04-04 14:31:20 +000080 return StringRef(Name.S, Name.Len);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000081 }
82 uint32_t getNameOffset() const {
83 assert(isLocal());
84 return NameOffset;
85 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000086
Rui Ueyamab5792b22016-04-04 19:09:08 +000087 uint8_t getVisibility() const { return StOther & 0x3; }
Rafael Espindola78471f02015-09-01 23:12:52 +000088
Rui Ueyama572a6f72016-01-29 01:49:33 +000089 unsigned DynsymIndex = 0;
George Rimar90cd0a82015-12-01 19:20:26 +000090 uint32_t GlobalDynIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000091 uint32_t GotIndex = -1;
George Rimar648a2c32015-10-20 08:54:27 +000092 uint32_t GotPltIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000093 uint32_t PltIndex = -1;
Simon Atanasyan13f6da12016-03-31 21:26:23 +000094 uint32_t ThunkIndex = -1;
George Rimar90cd0a82015-12-01 19:20:26 +000095 bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
Rafael Espindola5c2310c2015-09-18 14:40:19 +000096 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000097 bool isInPlt() const { return PltIndex != -1U; }
Simon Atanasyan13f6da12016-03-31 21:26:23 +000098 bool hasThunk() const { return ThunkIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000099
Davide Italiano04d6aa22016-03-29 00:15:52 +0000100 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
101
Rui Ueyama71a86862016-03-14 19:37:58 +0000102 template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000103 typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
104
105 template <class ELFT> typename ELFT::uint getGotVA() const;
106 template <class ELFT> typename ELFT::uint getGotPltVA() const;
107 template <class ELFT> typename ELFT::uint getPltVA() const;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000108 template <class ELFT> typename ELFT::uint getThunkVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000109 template <class ELFT> typename ELFT::uint getSize() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000110
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111 // A SymbolBody has a backreference to a Symbol. Originally they are
112 // doubly-linked. A backreference will never change. But the pointer
113 // in the Symbol may be mutated by the resolver. If you have a
114 // pointer P to a SymbolBody and are not sure whether the resolver
115 // has chosen the object among other objects having the same name,
116 // you can access P->Backref->Body to get the resolver's result.
117 void setBackref(Symbol *P) { Backref = P; }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000118 SymbolBody &repl() { return Backref ? *Backref->Body : *this; }
Sean Silvae32368f2016-03-22 21:04:03 +0000119 Symbol *getSymbol() const { return Backref; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000120
121 // Decides which symbol should "win" in the symbol table, this or
122 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
123 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000124 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000125
126protected:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000127 SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000128 uint8_t Type)
129 : SymbolKind(K), MustBeInDynSym(false), NeedsCopyOrPltAddr(false),
Rui Ueyamab5792b22016-04-04 19:09:08 +0000130 Type(Type), Binding(Binding), StOther(StOther),
Rafael Espindola193b99c2016-04-04 14:31:20 +0000131 Name({Name.data(), Name.size()}) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000132 assert(!isLocal());
Davide Italiano828ac5412016-03-28 15:44:21 +0000133 IsUsedInRegularObj =
134 K != SharedKind && K != LazyKind && K != DefinedBitcodeKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000135 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000136
Rui Ueyamab5792b22016-04-04 19:09:08 +0000137 SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000138
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000139 const unsigned SymbolKind : 8;
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
Rui Ueyamadd418072016-04-04 18:15:38 +0000155 // The following fields have the same meaning as the ELF symbol attributes.
156 uint8_t Type; // symbol type
157 uint8_t Binding; // symbol binding
Rui Ueyamab5792b22016-04-04 19:09:08 +0000158 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000159
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000160 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
161 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
162 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
163 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
164 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
165 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Rui Ueyamab5792b22016-04-04 19:09:08 +0000166 void setVisibility(uint8_t V) { StOther = (StOther & ~0x3) | V; }
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000167
George Rimar2f0fab52016-03-06 06:26:18 +0000168protected:
Rafael Espindola193b99c2016-04-04 14:31:20 +0000169 struct Str {
170 const char *S;
171 size_t Len;
172 };
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000173 union {
Rafael Espindola193b99c2016-04-04 14:31:20 +0000174 Str Name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000175 uint32_t NameOffset;
176 };
Michael J. Spencer84487f12015-07-24 21:03:07 +0000177 Symbol *Backref = nullptr;
178};
179
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000180// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000181class Defined : public SymbolBody {
182public:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000183 Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
184 uint8_t Type);
185 Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000186 static bool classof(const SymbolBody *S) { return S->isDefined(); }
187};
188
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000189// The defined symbol in LLVM bitcode files.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000190class DefinedBitcode : public Defined {
191public:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000192 DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000193 static bool classof(const SymbolBody *S);
194};
195
Rafael Espindola11191912015-12-24 16:23:37 +0000196class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000197public:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000198 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t Binding,
Rui Ueyamab5792b22016-04-04 19:09:08 +0000199 uint8_t StOther, uint8_t Type);
Rafael Espindola51d46902015-08-28 21:26:51 +0000200
201 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000202 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000203 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000204
205 // The output offset of this common symbol in the output bss. Computed by the
206 // writer.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000207 uint64_t OffsetInBss;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000208
209 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000210 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000211
212 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000213};
214
Michael J. Spencer84487f12015-07-24 21:03:07 +0000215// Regular defined symbols read from object file symbol tables.
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000216template <class ELFT> class DefinedRegular : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000217 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000218 typedef typename ELFT::uint uintX_t;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000219
Michael J. Spencer84487f12015-07-24 21:03:07 +0000220public:
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000221 DefinedRegular(StringRef Name, const Elf_Sym &Sym,
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000222 InputSectionBase<ELFT> *Section)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000223 : Defined(SymbolBody::DefinedRegularKind, Name, Sym.getBinding(),
224 Sym.st_other, Sym.getType()),
225 Value(Sym.st_value), Size(Sym.st_size),
226 Section(Section ? Section->Repl : NullInputSection) {}
227
228 DefinedRegular(uint32_t NameOffset, const Elf_Sym &Sym,
229 InputSectionBase<ELFT> *Section)
230 : Defined(SymbolBody::DefinedRegularKind, NameOffset, Sym.st_other,
231 Sym.getType()),
232 Value(Sym.st_value), Size(Sym.st_size),
233 Section(Section ? Section->Repl : NullInputSection) {
234 assert(isLocal());
235 }
236
Rui Ueyamab5792b22016-04-04 19:09:08 +0000237 DefinedRegular(StringRef Name, uint8_t Binding, uint8_t StOther)
238 : Defined(SymbolBody::DefinedRegularKind, Name, Binding, StOther,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000239 llvm::ELF::STT_NOTYPE),
240 Value(0), Size(0), Section(NullInputSection) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000241
242 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000243 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000244 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000245
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000246 uintX_t Value;
247 uintX_t Size;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000248
Rui Ueyama0b289522016-02-25 18:43:51 +0000249 // The input section this symbol belongs to. Notice that this is
250 // a reference to a pointer. We are using two levels of indirections
251 // because of ICF. If ICF decides two sections need to be merged, it
252 // manipulates this Section pointers so that they point to the same
253 // section. This is a bit tricky, so be careful to not be confused.
254 // If this is null, the symbol is an absolute symbol.
255 InputSectionBase<ELFT> *&Section;
256
257private:
258 static InputSectionBase<ELFT> *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000259};
260
Rui Ueyama0b289522016-02-25 18:43:51 +0000261template <class ELFT>
262InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
263
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000264// DefinedSynthetic is a class to represent linker-generated ELF symbols.
265// The difference from the regular symbol is that DefinedSynthetic symbols
266// don't belong to any input files or sections. Thus, its constructor
267// takes an output section to calculate output VA, etc.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000268template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000269public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000270 typedef typename ELFT::uint uintX_t;
George Rimaraa4dc202016-03-01 16:23:13 +0000271 DefinedSynthetic(StringRef N, uintX_t Value, OutputSectionBase<ELFT> &Section,
Rui Ueyamab5792b22016-04-04 19:09:08 +0000272 uint8_t StOther);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000273
274 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000275 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000276 }
277
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000278 // Special value designates that the symbol 'points'
279 // to the end of the section.
280 static const uintX_t SectionEnd = uintX_t(-1);
281
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000282 uintX_t Value;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000283 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000284};
285
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000286// Undefined symbol.
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000287class Undefined : public SymbolBody {
288 typedef SymbolBody::Kind Kind;
289 bool CanKeepUndefined;
290
291protected:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000292 Undefined(Kind K, StringRef N, uint8_t Binding, uint8_t StOther,
293 uint8_t Type);
294 Undefined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000295
296public:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000297 Undefined(StringRef N, bool IsWeak, uint8_t StOther, bool CanKeepUndefined);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000298
299 static bool classof(const SymbolBody *S) { return S->isUndefined(); }
300
301 bool canKeepUndefined() const { return CanKeepUndefined; }
302};
303
304template <class ELFT> class UndefinedElf : public Undefined {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000305 typedef typename ELFT::uint uintX_t;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000306 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000307
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000308public:
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000309 UndefinedElf(StringRef N, const Elf_Sym &Sym);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000310 UndefinedElf(uint32_t NameOffset, const Elf_Sym &Sym);
311
312 uintX_t Size;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000313
314 static bool classof(const SymbolBody *S) {
315 return S->kind() == SymbolBody::UndefinedElfKind;
316 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000317};
318
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000319template <class ELFT> class SharedSymbol : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000320 typedef typename ELFT::Sym Elf_Sym;
321 typedef typename ELFT::uint uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000322
323public:
324 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000325 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000326 }
327
Rui Ueyama35da9b62015-10-11 20:59:12 +0000328 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000329 : Defined(SymbolBody::SharedKind, Name, Sym.getBinding(), Sym.st_other,
330 Sym.getType()),
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000331 File(F), Sym(Sym) {}
Rui Ueyama35da9b62015-10-11 20:59:12 +0000332
333 SharedFile<ELFT> *File;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000334 const Elf_Sym &Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000335
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000336 // OffsetInBss is significant only when needsCopy() is true.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000337 uintX_t OffsetInBss = 0;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000338
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000339 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
Rafael Espindola18173d42015-09-08 15:50:05 +0000340};
341
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000342// This class represents a symbol defined in an archive file. It is
343// created from an archive file header, and it knows how to load an
344// object file from an archive to replace itself with a defined
345// symbol. If the resolver finds both Undefined and Lazy for
346// the same name, it will ask the Lazy to load a file.
347class Lazy : public SymbolBody {
348public:
349 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000350 : SymbolBody(LazyKind, S.getName(), llvm::ELF::STB_GLOBAL,
351 llvm::ELF::STV_DEFAULT, /* Type */ 0),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000352 File(F), Sym(S) {}
353
354 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
355
356 // Returns an object file for this symbol, or a nullptr if the file
357 // was already returned.
358 std::unique_ptr<InputFile> getMember();
359
360private:
361 ArchiveFile *File;
362 const llvm::object::Archive::Symbol Sym;
363};
364
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000365// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000366// DefinedRegular symbols.
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000367template <class ELFT> struct ElfSym {
George Rimar9e859392016-02-26 14:36:36 +0000368 // The content for _etext and etext symbols.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000369 static DefinedRegular<ELFT> *Etext;
370 static DefinedRegular<ELFT> *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000371
372 // The content for _edata and edata symbols.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000373 static DefinedRegular<ELFT> *Edata;
374 static DefinedRegular<ELFT> *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000375
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000376 // The content for _end and end symbols.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000377 static DefinedRegular<ELFT> *End;
378 static DefinedRegular<ELFT> *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000379
380 // The content for _gp symbol for MIPS target.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000381 static DefinedRegular<ELFT> *MipsGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000382
383 // __rel_iplt_start/__rel_iplt_end for signaling
384 // where R_[*]_IRELATIVE relocations do live.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000385 static DefinedRegular<ELFT> *RelaIpltStart;
386 static DefinedRegular<ELFT> *RelaIpltEnd;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000387};
388
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000389template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
390template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
391template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
392template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
393template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
394template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
395template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGp;
396template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::RelaIpltStart;
397template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::RelaIpltEnd;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000398
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000399} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000400} // namespace lld
401
402#endif