blob: dd06565a373d5d2a7073c77afd6d17fcf90bfb1a [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.
Peter Collingbournedadcc172016-04-22 18:42:48 +000043// Symbol also holds computed properties of symbol names.
Michael J. Spencer84487f12015-07-24 21:03:07 +000044struct Symbol {
Michael J. Spencer84487f12015-07-24 21:03:07 +000045 SymbolBody *Body;
Peter Collingbournedadcc172016-04-22 18:42:48 +000046
Rafael Espindola9e32e4f2016-04-26 13:50:46 +000047 // Symbol binding. This is on the Symbol to track changes during resolution.
48 // In particular:
49 // An undefined weak is still weak when it resolves to a shared library.
50 // An undefined weak will not fetch archive members, but we have to remember
51 // it is weak.
52 uint8_t Binding;
53
Peter Collingbournedadcc172016-04-22 18:42:48 +000054 // Symbol visibility. This is the computed minimum visibility of all
55 // observed non-DSO symbols.
56 unsigned Visibility : 2;
57
58 // True if the symbol was used for linking and thus need to be added to the
59 // output file's symbol table. This is true for all symbols except for
60 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
61 // by other bitcode objects.
62 unsigned IsUsedInRegularObj : 1;
63
64 // If this flag is true and the symbol has protected or default visibility, it
65 // will appear in .dynsym. This flag is set by interposable DSO symbols in
66 // executables, by most symbols in DSOs and executables built with
67 // --export-dynamic, and by dynamic lists.
68 unsigned ExportDynamic : 1;
69
Peter Collingbourne66ac1d62016-04-22 20:21:26 +000070 // This flag acts as an additional filter on the dynamic symbol list. It is
71 // set if there is no version script, or if the symbol appears in the global
72 // section of the version script.
73 unsigned VersionScriptGlobal : 1;
74
Peter Collingbournedadcc172016-04-22 18:42:48 +000075 bool includeInDynsym() const;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +000076
77 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000078};
79
80// The base class for real symbol classes.
81class SymbolBody {
Rafael Espindolaf4765732016-04-06 13:22:41 +000082 void init();
83
Michael J. Spencer84487f12015-07-24 21:03:07 +000084public:
85 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000086 DefinedFirst,
87 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000088 SharedKind,
Rafael Espindola11191912015-12-24 16:23:37 +000089 DefinedCommonKind,
Rafael Espindola9f77ef02016-02-12 20:54:57 +000090 DefinedBitcodeKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000091 DefinedSyntheticKind,
92 DefinedLast = DefinedSyntheticKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000093 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000094 LazyArchiveKind,
95 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000096 };
97
Michael J. Spencercdae0a42015-07-28 22:58:25 +000098 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000099
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000100 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000101 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000102 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +0000103 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000104 bool isLazy() const {
105 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
106 }
Rafael Espindola18173d42015-09-08 15:50:05 +0000107 bool isShared() const { return SymbolKind == SharedKind; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000108 bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; }
Rui Ueyamac4466602016-03-13 19:48:18 +0000109 bool isPreemptible() const;
Rui Ueyama7ede5432016-03-13 04:40:14 +0000110
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111 // Returns the symbol name.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000112 StringRef getName() const {
113 assert(!isLocal());
Rafael Espindola193b99c2016-04-04 14:31:20 +0000114 return StringRef(Name.S, Name.Len);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000115 }
116 uint32_t getNameOffset() const {
117 assert(isLocal());
118 return NameOffset;
119 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000120
Rui Ueyamab5792b22016-04-04 19:09:08 +0000121 uint8_t getVisibility() const { return StOther & 0x3; }
Rafael Espindola78471f02015-09-01 23:12:52 +0000122
Rui Ueyama572a6f72016-01-29 01:49:33 +0000123 unsigned DynsymIndex = 0;
George Rimar90cd0a82015-12-01 19:20:26 +0000124 uint32_t GlobalDynIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +0000125 uint32_t GotIndex = -1;
George Rimar648a2c32015-10-20 08:54:27 +0000126 uint32_t GotPltIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +0000127 uint32_t PltIndex = -1;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000128 uint32_t ThunkIndex = -1;
George Rimar90cd0a82015-12-01 19:20:26 +0000129 bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000130 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000131 bool isInPlt() const { return PltIndex != -1U; }
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000132 bool hasThunk() const { return ThunkIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000133
Rui Ueyama71a86862016-03-14 19:37:58 +0000134 template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000135 typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
136
Rafael Espindola74031ba2016-04-07 15:20:56 +0000137 template <class ELFT> typename ELFT::uint getGotOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000138 template <class ELFT> typename ELFT::uint getGotVA() const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000139 template <class ELFT> typename ELFT::uint getGotPltOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000140 template <class ELFT> typename ELFT::uint getGotPltVA() const;
141 template <class ELFT> typename ELFT::uint getPltVA() const;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000142 template <class ELFT> typename ELFT::uint getThunkVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000143 template <class ELFT> typename ELFT::uint getSize() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000144
Michael J. Spencer84487f12015-07-24 21:03:07 +0000145 // A SymbolBody has a backreference to a Symbol. Originally they are
146 // doubly-linked. A backreference will never change. But the pointer
147 // in the Symbol may be mutated by the resolver. If you have a
148 // pointer P to a SymbolBody and are not sure whether the resolver
149 // has chosen the object among other objects having the same name,
150 // you can access P->Backref->Body to get the resolver's result.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000151 SymbolBody &repl() { return Backref ? *Backref->Body : *this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000152
153 // Decides which symbol should "win" in the symbol table, this or
154 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
155 // they are duplicate (conflicting) symbols.
Peter Collingbourned0856a62016-04-05 00:47:58 +0000156 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000157
158protected:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000159 SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
Rafael Espindolaf4765732016-04-06 13:22:41 +0000160 uint8_t Type);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000161
Rui Ueyamab5792b22016-04-04 19:09:08 +0000162 SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000163
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000164 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000165
Rafael Espindolaabebed92016-02-05 15:27:15 +0000166public:
Rafael Espindola4d480ed2016-04-21 21:44:25 +0000167 // True if this symbol can be omitted from the symbol table if nothing else
168 // requires it to be there. Right now this is only used for linkonce_odr in
169 // LTO, but we could add the feature to ELF. It would be similar to
170 // MachO's .weak_def_can_be_hidden.
171 unsigned CanOmitFromDynSym : 1;
172
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000173 // True if the linker has to generate a copy relocation for this shared
174 // symbol or if the symbol should point to its plt entry.
175 unsigned NeedsCopyOrPltAddr : 1;
176
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000177 // True if the symbol is undefined and comes from a bitcode file. We need to
178 // keep track of this because undefined symbols only prevent internalization
179 // of bitcode symbols if they did not come from a bitcode file.
180 unsigned IsUndefinedBitcode : 1;
181
Rui Ueyamadd418072016-04-04 18:15:38 +0000182 // The following fields have the same meaning as the ELF symbol attributes.
183 uint8_t Type; // symbol type
184 uint8_t Binding; // symbol binding
Rui Ueyamab5792b22016-04-04 19:09:08 +0000185 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000186
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000187 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
188 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
189 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
190 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
191 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
192 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000193
194 Symbol *Backref = nullptr;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000195
George Rimar2f0fab52016-03-06 06:26:18 +0000196protected:
Rafael Espindola193b99c2016-04-04 14:31:20 +0000197 struct Str {
198 const char *S;
199 size_t Len;
200 };
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000201 union {
Rafael Espindola193b99c2016-04-04 14:31:20 +0000202 Str Name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000203 uint32_t NameOffset;
204 };
Michael J. Spencer84487f12015-07-24 21:03:07 +0000205};
206
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000207// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000208class Defined : public SymbolBody {
209public:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000210 Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
211 uint8_t Type);
212 Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000213 static bool classof(const SymbolBody *S) { return S->isDefined(); }
214};
215
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000216// The defined symbol in LLVM bitcode files.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000217class DefinedBitcode : public Defined {
218public:
Rui Ueyamab5792b22016-04-04 19:09:08 +0000219 DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000220 static bool classof(const SymbolBody *S);
221};
222
Rafael Espindola11191912015-12-24 16:23:37 +0000223class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000224public:
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000225 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t Binding,
Rui Ueyamab5792b22016-04-04 19:09:08 +0000226 uint8_t StOther, uint8_t Type);
Rafael Espindola51d46902015-08-28 21:26:51 +0000227
228 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000229 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000230 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000231
232 // The output offset of this common symbol in the output bss. Computed by the
233 // writer.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000234 uint64_t OffsetInBss;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000235
236 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000237 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000238
239 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000240};
241
Michael J. Spencer84487f12015-07-24 21:03:07 +0000242// Regular defined symbols read from object file symbol tables.
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000243template <class ELFT> class DefinedRegular : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000244 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000245 typedef typename ELFT::uint uintX_t;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000246
Michael J. Spencer84487f12015-07-24 21:03:07 +0000247public:
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000248 DefinedRegular(StringRef Name, const Elf_Sym &Sym,
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000249 InputSectionBase<ELFT> *Section)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000250 : Defined(SymbolBody::DefinedRegularKind, Name, Sym.getBinding(),
251 Sym.st_other, Sym.getType()),
252 Value(Sym.st_value), Size(Sym.st_size),
253 Section(Section ? Section->Repl : NullInputSection) {}
254
Rafael Espindolad9a17172016-04-05 11:47:46 +0000255 DefinedRegular(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section)
256 : Defined(SymbolBody::DefinedRegularKind, Sym.st_name, Sym.st_other,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000257 Sym.getType()),
258 Value(Sym.st_value), Size(Sym.st_size),
259 Section(Section ? Section->Repl : NullInputSection) {
260 assert(isLocal());
261 }
262
Rui Ueyamab5792b22016-04-04 19:09:08 +0000263 DefinedRegular(StringRef Name, uint8_t Binding, uint8_t StOther)
264 : Defined(SymbolBody::DefinedRegularKind, Name, Binding, StOther,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000265 llvm::ELF::STT_NOTYPE),
266 Value(0), Size(0), Section(NullInputSection) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000267
268 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000269 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000270 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000271
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000272 uintX_t Value;
273 uintX_t Size;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000274
Rui Ueyama0b289522016-02-25 18:43:51 +0000275 // The input section this symbol belongs to. Notice that this is
276 // a reference to a pointer. We are using two levels of indirections
277 // because of ICF. If ICF decides two sections need to be merged, it
278 // manipulates this Section pointers so that they point to the same
279 // section. This is a bit tricky, so be careful to not be confused.
280 // If this is null, the symbol is an absolute symbol.
281 InputSectionBase<ELFT> *&Section;
282
283private:
284 static InputSectionBase<ELFT> *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000285};
286
Rui Ueyama0b289522016-02-25 18:43:51 +0000287template <class ELFT>
288InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
289
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000290// DefinedSynthetic is a class to represent linker-generated ELF symbols.
291// The difference from the regular symbol is that DefinedSynthetic symbols
292// don't belong to any input files or sections. Thus, its constructor
293// takes an output section to calculate output VA, etc.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000294template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000295public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000296 typedef typename ELFT::uint uintX_t;
Peter Collingbournef6e9b4e2016-04-13 16:57:28 +0000297 DefinedSynthetic(StringRef N, uintX_t Value,
298 OutputSectionBase<ELFT> &Section);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000299
300 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000301 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000302 }
303
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000304 // Special value designates that the symbol 'points'
305 // to the end of the section.
306 static const uintX_t SectionEnd = uintX_t(-1);
307
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000308 uintX_t Value;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000309 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000310};
311
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000312class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000313public:
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000314 Undefined(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type,
315 uint64_t Size, bool IsBitcode);
316 Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type, uint64_t Size);
317
318 uint64_t Size;
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000319
Rafael Espindolaf4765732016-04-06 13:22:41 +0000320 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000321 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000322 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000323};
324
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000325template <class ELFT> class SharedSymbol : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000326 typedef typename ELFT::Sym Elf_Sym;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000327 typedef typename ELFT::Verdef Elf_Verdef;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000328 typedef typename ELFT::uint uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000329
330public:
331 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000332 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000333 }
334
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000335 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
336 const Elf_Verdef *Verdef)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000337 : Defined(SymbolBody::SharedKind, Name, Sym.getBinding(), Sym.st_other,
338 Sym.getType()),
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000339 File(F), Sym(Sym), Verdef(Verdef) {
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000340 // IFuncs defined in DSOs are treated as functions by the static linker.
341 if (isGnuIFunc())
342 Type = llvm::ELF::STT_FUNC;
343 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000344
345 SharedFile<ELFT> *File;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000346 const Elf_Sym &Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000347
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000348 // This field is initially a pointer to the symbol's version definition. As
349 // symbols are added to the version table, this field is replaced with the
350 // version identifier to be stored in .gnu.version in the output file.
351 union {
352 const Elf_Verdef *Verdef;
353 uint16_t VersionId;
354 };
355
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000356 // OffsetInBss is significant only when needsCopy() is true.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000357 uintX_t OffsetInBss = 0;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000358
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000359 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
Rafael Espindola18173d42015-09-08 15:50:05 +0000360};
361
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000362// This class represents a symbol defined in an archive file. It is
363// created from an archive file header, and it knows how to load an
364// object file from an archive to replace itself with a defined
365// symbol. If the resolver finds both Undefined and Lazy for
366// the same name, it will ask the Lazy to load a file.
367class Lazy : public SymbolBody {
368public:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000369 Lazy(SymbolBody::Kind K, StringRef Name)
370 : SymbolBody(K, Name, llvm::ELF::STB_GLOBAL, llvm::ELF::STV_DEFAULT,
371 /* Type */ 0) {}
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000372
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000373 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000374
375 // Returns an object file for this symbol, or a nullptr if the file
376 // was already returned.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000377 std::unique_ptr<InputFile> getFile();
378};
379
380// LazyArchive symbols represents symbols in archive files.
381class LazyArchive : public Lazy {
382public:
383 LazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S)
384 : Lazy(LazyArchiveKind, S.getName()), File(F), Sym(S) {}
385
386 static bool classof(const SymbolBody *S) {
387 return S->kind() == LazyArchiveKind;
388 }
389
390 std::unique_ptr<InputFile> getFile();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000391
392private:
393 ArchiveFile *File;
394 const llvm::object::Archive::Symbol Sym;
395};
396
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000397// LazyObject symbols represents symbols in object files between
398// --start-lib and --end-lib options.
399class LazyObject : public Lazy {
400public:
401 LazyObject(StringRef Name, MemoryBufferRef M)
402 : Lazy(LazyObjectKind, Name), MBRef(M) {}
403
404 static bool classof(const SymbolBody *S) {
405 return S->kind() == LazyObjectKind;
406 }
407
408 std::unique_ptr<InputFile> getFile();
409
410private:
411 MemoryBufferRef MBRef;
412};
413
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000414// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000415// DefinedRegular symbols.
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000416template <class ELFT> struct ElfSym {
George Rimar9e859392016-02-26 14:36:36 +0000417 // The content for _etext and etext symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000418 static DefinedRegular<ELFT> *Etext;
419 static DefinedRegular<ELFT> *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000420
421 // The content for _edata and edata symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000422 static DefinedRegular<ELFT> *Edata;
423 static DefinedRegular<ELFT> *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000424
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000425 // The content for _end and end symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000426 static DefinedRegular<ELFT> *End;
427 static DefinedRegular<ELFT> *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000428
429 // The content for _gp symbol for MIPS target.
Rafael Espindola6f92e142016-04-12 13:26:51 +0000430 static SymbolBody *MipsGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000431
Rafael Espindola6f92e142016-04-12 13:26:51 +0000432 static SymbolBody *MipsLocalGp;
433 static SymbolBody *MipsGpDisp;
Rafael Espindola8396f722016-04-11 20:34:27 +0000434
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000435 // __rel_iplt_start/__rel_iplt_end for signaling
436 // where R_[*]_IRELATIVE relocations do live.
Rafael Espindola03ef4042016-04-11 19:14:59 +0000437 static SymbolBody *RelaIpltStart;
438 static SymbolBody *RelaIpltEnd;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000439};
440
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000441template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
442template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
443template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
444template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
445template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
446template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
Rafael Espindola6f92e142016-04-12 13:26:51 +0000447template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGp;
448template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsLocalGp;
449template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGpDisp;
Rafael Espindola03ef4042016-04-11 19:14:59 +0000450template <class ELFT> SymbolBody *ElfSym<ELFT>::RelaIpltStart;
451template <class ELFT> SymbolBody *ElfSym<ELFT>::RelaIpltEnd;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000452
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000453} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000454} // namespace lld
455
456#endif