blob: 43dd518484e8c0a273d341dbe8b152885546b440 [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;
Peter Collingbourne4f952702016-05-01 04:55:03 +000028class BitcodeFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029class InputFile;
Rafael Espindola65c65ce2016-06-14 21:56:36 +000030class LazyObjectFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000032template <class ELFT> class ObjectFile;
Michael J. Spencer658dccd2015-09-18 22:13:25 +000033template <class ELFT> class OutputSection;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000034class OutputSectionBase;
Rui Ueyama35da9b62015-10-11 20:59:12 +000035template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036
Peter Collingbourne4f952702016-05-01 04:55:03 +000037struct Symbol;
Michael J. Spencer84487f12015-07-24 21:03:07 +000038
39// The base class for real symbol classes.
40class SymbolBody {
41public:
42 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000043 DefinedFirst,
44 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000045 SharedKind,
Rafael Espindola11191912015-12-24 16:23:37 +000046 DefinedCommonKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000047 DefinedSyntheticKind,
48 DefinedLast = DefinedSyntheticKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000049 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000050 LazyArchiveKind,
51 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000052 };
53
Rui Ueyama69c778c2016-07-17 17:50:09 +000054 SymbolBody(Kind K) : SymbolKind(K) {}
55
Peter Collingbourne4f952702016-05-01 04:55:03 +000056 Symbol *symbol();
57 const Symbol *symbol() const {
58 return const_cast<SymbolBody *>(this)->symbol();
59 }
60
Michael J. Spencercdae0a42015-07-28 22:58:25 +000061 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000062
Peter Collingbourne60976ed2016-04-27 00:05:06 +000063 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000064 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000065 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +000066 bool isLazy() const {
67 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
68 }
Rafael Espindola18173d42015-09-08 15:50:05 +000069 bool isShared() const { return SymbolKind == SharedKind; }
Peter Collingbourne4f952702016-05-01 04:55:03 +000070 bool isLocal() const { return IsLocal; }
Rui Ueyamac4466602016-03-13 19:48:18 +000071 bool isPreemptible() const;
George Rimar43651582016-06-28 08:21:10 +000072 StringRef getName() const;
Rui Ueyamab5792b22016-04-04 19:09:08 +000073 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +000074 void parseSymbolVersion();
Rafael Espindola78471f02015-09-01 23:12:52 +000075
Rafael Espindola5c2310c2015-09-18 14:40:19 +000076 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000077 bool isInPlt() const { return PltIndex != -1U; }
Peter Smithfb05cd92016-07-08 16:10:27 +000078 template <class ELFT> bool hasThunk() const;
Rafael Espindolaeb792732015-09-21 15:11:29 +000079
Rui Ueyama71a86862016-03-14 19:37:58 +000080 template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000081 typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
82
Rafael Espindola74031ba2016-04-07 15:20:56 +000083 template <class ELFT> typename ELFT::uint getGotOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000084 template <class ELFT> typename ELFT::uint getGotVA() const;
Rafael Espindola74031ba2016-04-07 15:20:56 +000085 template <class ELFT> typename ELFT::uint getGotPltOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000086 template <class ELFT> typename ELFT::uint getGotPltVA() const;
87 template <class ELFT> typename ELFT::uint getPltVA() const;
Simon Atanasyan13f6da12016-03-31 21:26:23 +000088 template <class ELFT> typename ELFT::uint getThunkVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000089 template <class ELFT> typename ELFT::uint getSize() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +000090
Rui Ueyama434b5612016-07-17 03:11:46 +000091 // The file from which this symbol was created.
92 InputFile *File = nullptr;
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +000093
Rafael Espindola7ae21772016-10-26 00:58:23 +000094 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +000095 uint32_t GotIndex = -1;
96 uint32_t GotPltIndex = -1;
97 uint32_t PltIndex = -1;
98 uint32_t GlobalDynIndex = -1;
99
Michael J. Spencer84487f12015-07-24 21:03:07 +0000100protected:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000101 SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000102 SymbolBody(Kind K, const char *Name, uint8_t StOther, uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000103
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000104 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000105
Rafael Espindolaabebed92016-02-05 15:27:15 +0000106public:
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000107 // True if the linker has to generate a copy relocation for this shared
108 // symbol or if the symbol should point to its plt entry.
109 unsigned NeedsCopyOrPltAddr : 1;
110
Peter Collingbourne4f952702016-05-01 04:55:03 +0000111 // True if this is a local symbol.
112 unsigned IsLocal : 1;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000113
Simon Atanasyan41325112016-06-19 21:39:37 +0000114 // True if this symbol has an entry in the global part of MIPS GOT.
115 unsigned IsInGlobalMipsGot : 1;
116
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000117 // True if this symbol is referenced by 32-bit GOT relocations.
118 unsigned Is32BitMipsGot : 1;
119
Rui Ueyamadd418072016-04-04 18:15:38 +0000120 // The following fields have the same meaning as the ELF symbol attributes.
121 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000122 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000123
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000124 // The Type field may also have this value. It means that we have not yet seen
125 // a non-Lazy symbol with this name, so we don't know what its type is. The
126 // Type field is normally set to this value for Lazy symbols unless we saw a
127 // weak undefined symbol first, in which case we need to remember the original
128 // symbol's type in order to check for TLS mismatches.
129 enum { UnknownType = 255 };
130
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000131 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
132 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
133 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
134 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
135 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
136 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000137
George Rimar2f0fab52016-03-06 06:26:18 +0000138protected:
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000139 // Local symbols are not inserted to the symbol table, so we usually
140 // don't need their names at all. We read symbol names lazily if possible.
141 mutable uint32_t NameLen = (uint32_t)-1;
142 const char *Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000143};
144
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000145// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000146class Defined : public SymbolBody {
147public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000148 Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000149 Defined(Kind K, const char *Name, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000150 static bool classof(const SymbolBody *S) { return S->isDefined(); }
151};
152
Rafael Espindolae7553e42016-08-31 13:28:33 +0000153class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000154public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000155 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther,
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000156 uint8_t Type, InputFile *File);
Rafael Espindola51d46902015-08-28 21:26:51 +0000157
158 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000159 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000160 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000161
162 // The output offset of this common symbol in the output bss. Computed by the
163 // writer.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000164 uint64_t Offset;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000165
166 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000167 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000168
169 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000170};
171
Michael J. Spencer84487f12015-07-24 21:03:07 +0000172// Regular defined symbols read from object file symbol tables.
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000173template <class ELFT> class DefinedRegular : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000174 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000175 typedef typename ELFT::uint uintX_t;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000176
Michael J. Spencer84487f12015-07-24 21:03:07 +0000177public:
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000178 DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, uintX_t Value,
179 uintX_t Size, InputSectionBase<ELFT> *Section, InputFile *File)
180 : Defined(SymbolBody::DefinedRegularKind, Name, StOther, Type),
181 Value(Value), Size(Size),
182 Section(Section ? Section->Repl : NullInputSection) {
183 this->File = File;
184 }
185
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000186 DefinedRegular(const char *Name, const Elf_Sym &Sym,
187 InputSectionBase<ELFT> *Section)
188 : Defined(SymbolBody::DefinedRegularKind, Name, Sym.st_other,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000189 Sym.getType()),
190 Value(Sym.st_value), Size(Sym.st_size),
191 Section(Section ? Section->Repl : NullInputSection) {
192 assert(isLocal());
Rui Ueyama434b5612016-07-17 03:11:46 +0000193 if (Section)
194 this->File = Section->getFile();
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000195 }
196
Simon Atanasyanf967f092016-09-29 12:58:36 +0000197 // Return true if the symbol is a PIC function.
198 bool isMipsPIC() const;
199
Michael J. Spencer84487f12015-07-24 21:03:07 +0000200 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000201 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000202 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000203
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000204 uintX_t Value;
205 uintX_t Size;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000206
Rui Ueyama0b289522016-02-25 18:43:51 +0000207 // The input section this symbol belongs to. Notice that this is
208 // a reference to a pointer. We are using two levels of indirections
209 // because of ICF. If ICF decides two sections need to be merged, it
210 // manipulates this Section pointers so that they point to the same
211 // section. This is a bit tricky, so be careful to not be confused.
212 // If this is null, the symbol is an absolute symbol.
213 InputSectionBase<ELFT> *&Section;
214
Peter Smithfb05cd92016-07-08 16:10:27 +0000215 // If non-null the symbol has a Thunk that may be used as an alternative
216 // destination for callers of this Symbol.
Rafael Espindola60914922016-07-08 19:28:55 +0000217 Thunk<ELFT> *ThunkData = nullptr;
Peter Smithfb05cd92016-07-08 16:10:27 +0000218
Rui Ueyama0b289522016-02-25 18:43:51 +0000219private:
220 static InputSectionBase<ELFT> *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000221};
222
Rui Ueyama0b289522016-02-25 18:43:51 +0000223template <class ELFT>
224InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
225
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000226// DefinedSynthetic is a class to represent linker-generated ELF symbols.
227// The difference from the regular symbol is that DefinedSynthetic symbols
228// don't belong to any input files or sections. Thus, its constructor
229// takes an output section to calculate output VA, etc.
Peter Collingbourne6a422592016-05-03 01:21:08 +0000230// If Section is null, this symbol is relative to the image base.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000231template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000232public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000233 typedef typename ELFT::uint uintX_t;
Eugene Leviantafaa9342016-11-16 09:49:39 +0000234 DefinedSynthetic(StringRef N, uintX_t Value,
235 const OutputSectionBase *Section);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000236
237 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000238 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000239 }
240
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000241 // Special value designates that the symbol 'points'
242 // to the end of the section.
243 static const uintX_t SectionEnd = uintX_t(-1);
244
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000245 uintX_t Value;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000246 const OutputSectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000247};
248
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000249class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000250public:
Rui Ueyama434b5612016-07-17 03:11:46 +0000251 Undefined(StringRef Name, uint8_t StOther, uint8_t Type, InputFile *F);
Rui Ueyamac72ba3a2016-11-23 04:57:25 +0000252 Undefined(const char *Name, uint8_t StOther, uint8_t Type, InputFile *F);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000253
Rafael Espindolaf4765732016-04-06 13:22:41 +0000254 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000255 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000256 }
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000257
Rui Ueyama434b5612016-07-17 03:11:46 +0000258 InputFile *file() { return this->File; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000259};
260
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000261template <class ELFT> class SharedSymbol : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000262 typedef typename ELFT::Sym Elf_Sym;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000263 typedef typename ELFT::Verdef Elf_Verdef;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000264 typedef typename ELFT::uint uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000265
266public:
267 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000268 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000269 }
270
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000271 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
272 const Elf_Verdef *Verdef)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000273 : Defined(SymbolBody::SharedKind, Name, Sym.st_other, Sym.getType()),
Rui Ueyama434b5612016-07-17 03:11:46 +0000274 Sym(Sym), Verdef(Verdef) {
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000275 // IFuncs defined in DSOs are treated as functions by the static linker.
276 if (isGnuIFunc())
277 Type = llvm::ELF::STT_FUNC;
Rui Ueyama434b5612016-07-17 03:11:46 +0000278 this->File = F;
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000279 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000280
Rui Ueyama434b5612016-07-17 03:11:46 +0000281 SharedFile<ELFT> *file() { return (SharedFile<ELFT> *)this->File; }
282
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000283 const Elf_Sym &Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000284
George Rimard3566302016-06-20 11:55:12 +0000285 // This field is a pointer to the symbol's version definition.
286 const Elf_Verdef *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000287
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000288 // OffsetInBss is significant only when needsCopy() is true.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000289 uintX_t OffsetInBss = 0;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000290
Peter Smithfb05cd92016-07-08 16:10:27 +0000291 // If non-null the symbol has a Thunk that may be used as an alternative
292 // destination for callers of this Symbol.
Rui Ueyama8c8db472016-07-08 19:59:11 +0000293 Thunk<ELFT> *ThunkData = nullptr;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000294 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
Rafael Espindola18173d42015-09-08 15:50:05 +0000295};
296
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000297// This class represents a symbol defined in an archive file. It is
298// created from an archive file header, and it knows how to load an
299// object file from an archive to replace itself with a defined
300// symbol. If the resolver finds both Undefined and Lazy for
301// the same name, it will ask the Lazy to load a file.
302class Lazy : public SymbolBody {
303public:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000304 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000305
306 // Returns an object file for this symbol, or a nullptr if the file
307 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000308 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000309
310protected:
311 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
312 : SymbolBody(K, Name, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000313};
314
315// LazyArchive symbols represents symbols in archive files.
316class LazyArchive : public Lazy {
317public:
Rafael Espindola07543a82016-06-14 21:40:23 +0000318 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
Rui Ueyama434b5612016-07-17 03:11:46 +0000319 uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000320
321 static bool classof(const SymbolBody *S) {
322 return S->kind() == LazyArchiveKind;
323 }
324
Rui Ueyama434b5612016-07-17 03:11:46 +0000325 ArchiveFile *file() { return (ArchiveFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000326 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000327
328private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000329 const llvm::object::Archive::Symbol Sym;
330};
331
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000332// LazyObject symbols represents symbols in object files between
333// --start-lib and --end-lib options.
334class LazyObject : public Lazy {
335public:
Rui Ueyama434b5612016-07-17 03:11:46 +0000336 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000337
338 static bool classof(const SymbolBody *S) {
339 return S->kind() == LazyObjectKind;
340 }
341
Rui Ueyama434b5612016-07-17 03:11:46 +0000342 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000343 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000344};
345
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000346// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000347// DefinedRegular symbols.
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000348template <class ELFT> struct ElfSym {
Petr Hosek4f7a5c32016-08-22 18:44:04 +0000349 // The content for __ehdr_start symbol.
350 static DefinedRegular<ELFT> *EhdrStart;
351
George Rimar9e859392016-02-26 14:36:36 +0000352 // The content for _etext and etext symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000353 static DefinedRegular<ELFT> *Etext;
354 static DefinedRegular<ELFT> *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000355
356 // The content for _edata and edata symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000357 static DefinedRegular<ELFT> *Edata;
358 static DefinedRegular<ELFT> *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000359
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000360 // The content for _end and end symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000361 static DefinedRegular<ELFT> *End;
362 static DefinedRegular<ELFT> *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000363
Simon Atanasyan8469b882016-11-23 22:22:16 +0000364 // The content for _gp_disp/__gnu_local_gp symbols for MIPS target.
365 static DefinedRegular<ELFT> *MipsGpDisp;
366 static DefinedRegular<ELFT> *MipsLocalGp;
367 static SymbolBody *MipsGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000368};
369
Petr Hosek4f7a5c32016-08-22 18:44:04 +0000370template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::EhdrStart;
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000371template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
372template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
373template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
374template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
375template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
376template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
Simon Atanasyan8469b882016-11-23 22:22:16 +0000377template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGpDisp;
378template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsLocalGp;
379template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGp;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000380
381// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
382// always one Symbol for each symbol name. The resolver updates the SymbolBody
383// stored in the Body field of this object as it resolves symbols. Symbol also
384// holds computed properties of symbol names.
385struct Symbol {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000386 // Symbol binding. This is on the Symbol to track changes during resolution.
387 // In particular:
388 // An undefined weak is still weak when it resolves to a shared library.
389 // An undefined weak will not fetch archive members, but we have to remember
390 // it is weak.
391 uint8_t Binding;
392
George Rimard3566302016-06-20 11:55:12 +0000393 // Version definition index.
394 uint16_t VersionId;
395
Peter Collingbourne4f952702016-05-01 04:55:03 +0000396 // Symbol visibility. This is the computed minimum visibility of all
397 // observed non-DSO symbols.
398 unsigned Visibility : 2;
399
400 // True if the symbol was used for linking and thus need to be added to the
401 // output file's symbol table. This is true for all symbols except for
402 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
403 // by other bitcode objects.
404 unsigned IsUsedInRegularObj : 1;
405
406 // If this flag is true and the symbol has protected or default visibility, it
407 // will appear in .dynsym. This flag is set by interposable DSO symbols in
408 // executables, by most symbols in DSOs and executables built with
409 // --export-dynamic, and by dynamic lists.
410 unsigned ExportDynamic : 1;
411
Rui Ueyama69c778c2016-07-17 17:50:09 +0000412 // True if this symbol is specified by --trace-symbol option.
413 unsigned Traced : 1;
414
Peter Collingbourne4f952702016-05-01 04:55:03 +0000415 bool includeInDynsym() const;
416 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
417
418 // This field is used to store the Symbol's SymbolBody. This instantiation of
419 // AlignedCharArrayUnion gives us a struct with a char array field that is
420 // large and aligned enough to store any derived class of SymbolBody. We
421 // assume that the size and alignment of ELF64LE symbols is sufficient for any
422 // ELFT, and we verify this with the static_asserts in replaceBody.
Rafael Espindolae7553e42016-08-31 13:28:33 +0000423 llvm::AlignedCharArrayUnion<
424 DefinedCommon, DefinedRegular<llvm::object::ELF64LE>,
425 DefinedSynthetic<llvm::object::ELF64LE>, Undefined,
George Rimara4c7e742016-10-20 08:36:42 +0000426 SharedSymbol<llvm::object::ELF64LE>, LazyArchive, LazyObject> Body;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000427
428 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
429 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
430};
431
Rui Ueyama69c778c2016-07-17 17:50:09 +0000432void printTraceSymbol(Symbol *Sym);
433
Peter Collingbourne4f952702016-05-01 04:55:03 +0000434template <typename T, typename... ArgT>
435void replaceBody(Symbol *S, ArgT &&... Arg) {
436 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
Benjamin Kramer922b63b2016-10-20 15:55:41 +0000437 static_assert(alignof(T) <= alignof(decltype(S->Body)),
Peter Collingbourne4f952702016-05-01 04:55:03 +0000438 "Body not aligned enough");
Peter Collingbournef643fc12016-05-01 05:39:02 +0000439 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
440 "Not a SymbolBody");
Rui Ueyama69c778c2016-07-17 17:50:09 +0000441
Peter Collingbourne4f952702016-05-01 04:55:03 +0000442 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000443
444 // Print out a log message if --trace-symbol was specified.
445 // This is for debugging.
446 if (S->Traced)
447 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000448}
449
450inline Symbol *SymbolBody::symbol() {
451 assert(!isLocal());
452 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
453 offsetof(Symbol, Body));
454}
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000455
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000456} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000457} // namespace lld
458
459#endif