blob: afd8743f6ddd41bfd5b485b9440860a2d52e6af6 [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 {
Rafael Espindolaf4765732016-04-06 13:22:41 +000049 void init();
50
Michael J. Spencer84487f12015-07-24 21:03:07 +000051public:
52 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000053 DefinedFirst,
54 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000055 SharedKind,
Rafael Espindola11191912015-12-24 16:23:37 +000056 DefinedCommonKind,
Rafael Espindola9f77ef02016-02-12 20:54:57 +000057 DefinedBitcodeKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000058 DefinedSyntheticKind,
59 DefinedLast = DefinedSyntheticKind,
Rafael Espindola5d7593b2015-12-22 23:00:50 +000060 UndefinedElfKind,
Rafael Espindolaf4765732016-04-06 13:22:41 +000061 UndefinedBitcodeKind,
Rafael Espindola84aff152015-09-25 21:20:23 +000062 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000063 };
64
Michael J. Spencercdae0a42015-07-28 22:58:25 +000065 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000066
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000067 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
Rafael Espindola5d7593b2015-12-22 23:00:50 +000068 bool isUndefined() const {
Rafael Espindolaf4765732016-04-06 13:22:41 +000069 return SymbolKind == UndefinedBitcodeKind || SymbolKind == UndefinedElfKind;
Rafael Espindola5d7593b2015-12-22 23:00:50 +000070 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000071 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000072 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000073 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000074 bool isShared() const { return SymbolKind == SharedKind; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000075 bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; }
Rafael Espindola18173d42015-09-08 15:50:05 +000076 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rui Ueyamac4466602016-03-13 19:48:18 +000077 bool isPreemptible() const;
Rui Ueyama7ede5432016-03-13 04:40:14 +000078
Michael J. Spencer84487f12015-07-24 21:03:07 +000079 // Returns the symbol name.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000080 StringRef getName() const {
81 assert(!isLocal());
Rafael Espindola193b99c2016-04-04 14:31:20 +000082 return StringRef(Name.S, Name.Len);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000083 }
84 uint32_t getNameOffset() const {
85 assert(isLocal());
86 return NameOffset;
87 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000088
Rui Ueyamab5792b22016-04-04 19:09:08 +000089 uint8_t getVisibility() const { return StOther & 0x3; }
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;
Simon Atanasyan13f6da12016-03-31 21:26:23 +000096 uint32_t ThunkIndex = -1;
George Rimar90cd0a82015-12-01 19:20:26 +000097 bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
Rafael Espindola5c2310c2015-09-18 14:40:19 +000098 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000099 bool isInPlt() const { return PltIndex != -1U; }
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000100 bool hasThunk() const { return ThunkIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000101
Davide Italiano04d6aa22016-03-29 00:15:52 +0000102 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
103
Rui Ueyama71a86862016-03-14 19:37:58 +0000104 template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000105 typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
106
107 template <class ELFT> typename ELFT::uint getGotVA() const;
108 template <class ELFT> typename ELFT::uint getGotPltVA() const;
109 template <class ELFT> typename ELFT::uint getPltVA() const;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000110 template <class ELFT> typename ELFT::uint getThunkVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000111 template <class ELFT> typename ELFT::uint getSize() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000112
Michael J. Spencer84487f12015-07-24 21:03:07 +0000113 // A SymbolBody has a backreference to a Symbol. Originally they are
114 // doubly-linked. A backreference will never change. But the pointer
115 // in the Symbol may be mutated by the resolver. If you have a
116 // pointer P to a SymbolBody and are not sure whether the resolver
117 // has chosen the object among other objects having the same name,
118 // you can access P->Backref->Body to get the resolver's result.
119 void setBackref(Symbol *P) { Backref = P; }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000120 SymbolBody &repl() { return Backref ? *Backref->Body : *this; }
Sean Silvae32368f2016-03-22 21:04:03 +0000121 Symbol *getSymbol() const { return Backref; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000122
123 // Decides which symbol should "win" in the symbol table, this or
124 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
125 // they are duplicate (conflicting) symbols.
Peter Collingbourned0856a62016-04-05 00:47:58 +0000126 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000127
128protected:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000129 SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
Rafael Espindolaf4765732016-04-06 13:22:41 +0000130 uint8_t Type);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000131
Rui Ueyamab5792b22016-04-04 19:09:08 +0000132 SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000133
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000134 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000135
136 // True if the symbol was used for linking and thus need to be
137 // added to the output file's symbol table. It is usually true,
138 // but if it is a shared symbol that were not referenced by anyone,
139 // it can be false.
Rafael Espindola18173d42015-09-08 15:50:05 +0000140 unsigned IsUsedInRegularObj : 1;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000141
Rafael Espindolaabebed92016-02-05 15:27:15 +0000142public:
Rui Ueyama7d332f52015-12-25 06:55:39 +0000143 // If true, the symbol is added to .dynsym symbol table.
Rafael Espindolaabebed92016-02-05 15:27:15 +0000144 unsigned MustBeInDynSym : 1;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000145
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000146 // True if the linker has to generate a copy relocation for this shared
147 // symbol or if the symbol should point to its plt entry.
148 unsigned NeedsCopyOrPltAddr : 1;
149
Rui Ueyamadd418072016-04-04 18:15:38 +0000150 // The following fields have the same meaning as the ELF symbol attributes.
151 uint8_t Type; // symbol type
152 uint8_t Binding; // symbol binding
Rui Ueyamab5792b22016-04-04 19:09:08 +0000153 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000154
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000155 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
156 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
157 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
158 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
159 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
160 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Rui Ueyamab5792b22016-04-04 19:09:08 +0000161 void setVisibility(uint8_t V) { StOther = (StOther & ~0x3) | V; }
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000162
George Rimar2f0fab52016-03-06 06:26:18 +0000163protected:
Rafael Espindola193b99c2016-04-04 14:31:20 +0000164 struct Str {
165 const char *S;
166 size_t Len;
167 };
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000168 union {
Rafael Espindola193b99c2016-04-04 14:31:20 +0000169 Str Name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000170 uint32_t NameOffset;
171 };
Michael J. Spencer84487f12015-07-24 21:03:07 +0000172 Symbol *Backref = nullptr;
173};
174
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000175// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000176class Defined : public SymbolBody {
177public:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000178 Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
179 uint8_t Type);
180 Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000181 static bool classof(const SymbolBody *S) { return S->isDefined(); }
182};
183
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000184// The defined symbol in LLVM bitcode files.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000185class DefinedBitcode : public Defined {
186public:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000187 DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000188 static bool classof(const SymbolBody *S);
189};
190
Rafael Espindola11191912015-12-24 16:23:37 +0000191class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000192public:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000193 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t Binding,
Rui Ueyamab5792b22016-04-04 19:09:08 +0000194 uint8_t StOther, uint8_t Type);
Rafael Espindola51d46902015-08-28 21:26:51 +0000195
196 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000197 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000198 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000199
200 // The output offset of this common symbol in the output bss. Computed by the
201 // writer.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000202 uint64_t OffsetInBss;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000203
204 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000205 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000206
207 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000208};
209
Michael J. Spencer84487f12015-07-24 21:03:07 +0000210// Regular defined symbols read from object file symbol tables.
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000211template <class ELFT> class DefinedRegular : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000212 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000213 typedef typename ELFT::uint uintX_t;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000214
Michael J. Spencer84487f12015-07-24 21:03:07 +0000215public:
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000216 DefinedRegular(StringRef Name, const Elf_Sym &Sym,
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000217 InputSectionBase<ELFT> *Section)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000218 : Defined(SymbolBody::DefinedRegularKind, Name, Sym.getBinding(),
219 Sym.st_other, Sym.getType()),
220 Value(Sym.st_value), Size(Sym.st_size),
221 Section(Section ? Section->Repl : NullInputSection) {}
222
Rafael Espindolad9a17172016-04-05 11:47:46 +0000223 DefinedRegular(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section)
224 : Defined(SymbolBody::DefinedRegularKind, Sym.st_name, Sym.st_other,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000225 Sym.getType()),
226 Value(Sym.st_value), Size(Sym.st_size),
227 Section(Section ? Section->Repl : NullInputSection) {
228 assert(isLocal());
229 }
230
Rui Ueyamab5792b22016-04-04 19:09:08 +0000231 DefinedRegular(StringRef Name, uint8_t Binding, uint8_t StOther)
232 : Defined(SymbolBody::DefinedRegularKind, Name, Binding, StOther,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000233 llvm::ELF::STT_NOTYPE),
234 Value(0), Size(0), Section(NullInputSection) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000235
236 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000237 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000238 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000239
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000240 uintX_t Value;
241 uintX_t Size;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000242
Rui Ueyama0b289522016-02-25 18:43:51 +0000243 // The input section this symbol belongs to. Notice that this is
244 // a reference to a pointer. We are using two levels of indirections
245 // because of ICF. If ICF decides two sections need to be merged, it
246 // manipulates this Section pointers so that they point to the same
247 // section. This is a bit tricky, so be careful to not be confused.
248 // If this is null, the symbol is an absolute symbol.
249 InputSectionBase<ELFT> *&Section;
250
251private:
252 static InputSectionBase<ELFT> *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000253};
254
Rui Ueyama0b289522016-02-25 18:43:51 +0000255template <class ELFT>
256InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
257
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000258// DefinedSynthetic is a class to represent linker-generated ELF symbols.
259// The difference from the regular symbol is that DefinedSynthetic symbols
260// don't belong to any input files or sections. Thus, its constructor
261// takes an output section to calculate output VA, etc.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000262template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000263public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000264 typedef typename ELFT::uint uintX_t;
George Rimaraa4dc202016-03-01 16:23:13 +0000265 DefinedSynthetic(StringRef N, uintX_t Value, OutputSectionBase<ELFT> &Section,
Rui Ueyamab5792b22016-04-04 19:09:08 +0000266 uint8_t StOther);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000267
268 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000269 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000270 }
271
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000272 // Special value designates that the symbol 'points'
273 // to the end of the section.
274 static const uintX_t SectionEnd = uintX_t(-1);
275
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000276 uintX_t Value;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000277 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000278};
279
Rafael Espindolaf4765732016-04-06 13:22:41 +0000280class UndefinedBitcode : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000281public:
Rafael Espindolaf4765732016-04-06 13:22:41 +0000282 UndefinedBitcode(StringRef N, bool IsWeak, uint8_t StOther);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000283
Rafael Espindolaf4765732016-04-06 13:22:41 +0000284 static bool classof(const SymbolBody *S) {
285 return S->kind() == UndefinedBitcodeKind;
286 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000287};
288
Rafael Espindolaf4765732016-04-06 13:22:41 +0000289template <class ELFT> class UndefinedElf : public SymbolBody {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000290 typedef typename ELFT::uint uintX_t;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000291 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindolaf4765732016-04-06 13:22:41 +0000292 bool CanKeepUndefined = false;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000293
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000294public:
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000295 UndefinedElf(StringRef N, const Elf_Sym &Sym);
Rafael Espindolad9a17172016-04-05 11:47:46 +0000296 UndefinedElf(const Elf_Sym &Sym);
Rafael Espindolaf4765732016-04-06 13:22:41 +0000297 UndefinedElf(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type,
298 bool CanKeepUndefined);
299
300 bool canKeepUndefined() const { return CanKeepUndefined; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000301
302 uintX_t Size;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000303
304 static bool classof(const SymbolBody *S) {
305 return S->kind() == SymbolBody::UndefinedElfKind;
306 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000307};
308
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000309template <class ELFT> class SharedSymbol : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000310 typedef typename ELFT::Sym Elf_Sym;
311 typedef typename ELFT::uint uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000312
313public:
314 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000315 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000316 }
317
Rui Ueyama35da9b62015-10-11 20:59:12 +0000318 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000319 : Defined(SymbolBody::SharedKind, Name, Sym.getBinding(), Sym.st_other,
320 Sym.getType()),
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000321 File(F), Sym(Sym) {}
Rui Ueyama35da9b62015-10-11 20:59:12 +0000322
323 SharedFile<ELFT> *File;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000324 const Elf_Sym &Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000325
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000326 // OffsetInBss is significant only when needsCopy() is true.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000327 uintX_t OffsetInBss = 0;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000328
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000329 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
Rafael Espindola18173d42015-09-08 15:50:05 +0000330};
331
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000332// This class represents a symbol defined in an archive file. It is
333// created from an archive file header, and it knows how to load an
334// object file from an archive to replace itself with a defined
335// symbol. If the resolver finds both Undefined and Lazy for
336// the same name, it will ask the Lazy to load a file.
337class Lazy : public SymbolBody {
338public:
339 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000340 : SymbolBody(LazyKind, S.getName(), llvm::ELF::STB_GLOBAL,
341 llvm::ELF::STV_DEFAULT, /* Type */ 0),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000342 File(F), Sym(S) {}
343
344 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
345
346 // Returns an object file for this symbol, or a nullptr if the file
347 // was already returned.
348 std::unique_ptr<InputFile> getMember();
349
350private:
351 ArchiveFile *File;
352 const llvm::object::Archive::Symbol Sym;
353};
354
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000355// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000356// DefinedRegular symbols.
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000357template <class ELFT> struct ElfSym {
George Rimar9e859392016-02-26 14:36:36 +0000358 // The content for _etext and etext symbols.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000359 static DefinedRegular<ELFT> *Etext;
360 static DefinedRegular<ELFT> *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000361
362 // The content for _edata and edata symbols.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000363 static DefinedRegular<ELFT> *Edata;
364 static DefinedRegular<ELFT> *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000365
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000366 // The content for _end and end symbols.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000367 static DefinedRegular<ELFT> *End;
368 static DefinedRegular<ELFT> *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000369
370 // The content for _gp symbol for MIPS target.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000371 static DefinedRegular<ELFT> *MipsGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000372
373 // __rel_iplt_start/__rel_iplt_end for signaling
374 // where R_[*]_IRELATIVE relocations do live.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000375 static DefinedRegular<ELFT> *RelaIpltStart;
376 static DefinedRegular<ELFT> *RelaIpltEnd;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000377};
378
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000379template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
380template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
381template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
382template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
383template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
384template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
385template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGp;
386template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::RelaIpltStart;
387template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::RelaIpltEnd;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000388
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000389} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000390} // namespace lld
391
392#endif