blob: 12f65cf1dccfbb50b5a80f6c58662fd12c2077b3 [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"
Rui Ueyamaa13efc22016-11-29 18:05:04 +000019#include "Strings.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000020
Michael J. Spencer84487f12015-07-24 21:03:07 +000021#include "lld/Core/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000022#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023#include "llvm/Object/ELF.h"
24
25namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000026namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
Michael J. Spencer1b348a62015-09-04 22:28:10 +000028class ArchiveFile;
Peter Collingbourne4f952702016-05-01 04:55:03 +000029class BitcodeFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030class InputFile;
Rafael Espindola65c65ce2016-06-14 21:56:36 +000031class LazyObjectFile;
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; }
Rafael Espindola1d6d1b42017-01-17 16:08:06 +000070 bool isInCurrentDSO() const { return !isUndefined() && !isShared(); }
Peter Collingbourne4f952702016-05-01 04:55:03 +000071 bool isLocal() const { return IsLocal; }
Rui Ueyamac4466602016-03-13 19:48:18 +000072 bool isPreemptible() const;
Rui Ueyamaa13efc22016-11-29 18:05:04 +000073 StringRef getName() const { return Name; }
Rui Ueyamab5792b22016-04-04 19:09:08 +000074 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +000075 void parseSymbolVersion();
Rafael Espindola78471f02015-09-01 23:12:52 +000076
Rafael Espindola5c2310c2015-09-18 14:40:19 +000077 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000078 bool isInPlt() const { return PltIndex != -1U; }
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;
88 template <class ELFT> typename ELFT::uint getSize() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +000089
Rui Ueyama434b5612016-07-17 03:11:46 +000090 // The file from which this symbol was created.
91 InputFile *File = nullptr;
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +000092
Rafael Espindola7ae21772016-10-26 00:58:23 +000093 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +000094 uint32_t GotIndex = -1;
95 uint32_t GotPltIndex = -1;
96 uint32_t PltIndex = -1;
97 uint32_t GlobalDynIndex = -1;
98
Michael J. Spencer84487f12015-07-24 21:03:07 +000099protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000100 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
101 uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000102
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000103 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000104
Rafael Espindolaabebed92016-02-05 15:27:15 +0000105public:
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000106 // True if the linker has to generate a copy relocation for this shared
107 // symbol or if the symbol should point to its plt entry.
108 unsigned NeedsCopyOrPltAddr : 1;
109
Peter Collingbourne4f952702016-05-01 04:55:03 +0000110 // True if this is a local symbol.
111 unsigned IsLocal : 1;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000112
Simon Atanasyan41325112016-06-19 21:39:37 +0000113 // True if this symbol has an entry in the global part of MIPS GOT.
114 unsigned IsInGlobalMipsGot : 1;
115
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000116 // True if this symbol is referenced by 32-bit GOT relocations.
117 unsigned Is32BitMipsGot : 1;
118
Peter Smithbaffdb82016-12-08 12:58:55 +0000119 // True if this symbol is in the Iplt sub-section of the Plt.
120 unsigned IsInIplt : 1;
121
122 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
123 unsigned IsInIgot : 1;
124
Peter Collingbournefeb66292017-01-10 01:21:50 +0000125 // True if this is a shared symbol in a read-only segment which requires a
126 // copy relocation. This causes space for the symbol to be allocated in the
127 // .bss.rel.ro section.
128 unsigned CopyIsInBssRelRo : 1;
129
Rui Ueyamadd418072016-04-04 18:15:38 +0000130 // The following fields have the same meaning as the ELF symbol attributes.
131 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000132 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000133
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000134 // The Type field may also have this value. It means that we have not yet seen
135 // a non-Lazy symbol with this name, so we don't know what its type is. The
136 // Type field is normally set to this value for Lazy symbols unless we saw a
137 // weak undefined symbol first, in which case we need to remember the original
138 // symbol's type in order to check for TLS mismatches.
139 enum { UnknownType = 255 };
140
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000141 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
142 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
143 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
144 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
145 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
146 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000147
George Rimar2f0fab52016-03-06 06:26:18 +0000148protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000149 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000150};
151
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000152// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000153class Defined : public SymbolBody {
154public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000155 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000156 static bool classof(const SymbolBody *S) { return S->isDefined(); }
157};
158
Rafael Espindolae7553e42016-08-31 13:28:33 +0000159class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000160public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000161 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther,
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000162 uint8_t Type, InputFile *File);
Rafael Espindola51d46902015-08-28 21:26:51 +0000163
164 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000165 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000166 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000167
168 // The output offset of this common symbol in the output bss. Computed by the
169 // writer.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000170 uint64_t Offset;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000171
172 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000173 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000174
175 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000176};
177
Michael J. Spencer84487f12015-07-24 21:03:07 +0000178// Regular defined symbols read from object file symbol tables.
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000179template <class ELFT> class DefinedRegular : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000180 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000181 typedef typename ELFT::uint uintX_t;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000182
Michael J. Spencer84487f12015-07-24 21:03:07 +0000183public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000184 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
185 uintX_t Value, uintX_t Size, InputSectionBase<ELFT> *Section,
186 InputFile *File)
187 : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000188 Value(Value), Size(Size),
189 Section(Section ? Section->Repl : NullInputSection) {
190 this->File = File;
191 }
192
Simon Atanasyanf967f092016-09-29 12:58:36 +0000193 // Return true if the symbol is a PIC function.
194 bool isMipsPIC() const;
195
Michael J. Spencer84487f12015-07-24 21:03:07 +0000196 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000197 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000198 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000200 uintX_t Value;
201 uintX_t Size;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000202
Rui Ueyama0b289522016-02-25 18:43:51 +0000203 // The input section this symbol belongs to. Notice that this is
204 // a reference to a pointer. We are using two levels of indirections
205 // because of ICF. If ICF decides two sections need to be merged, it
206 // manipulates this Section pointers so that they point to the same
207 // section. This is a bit tricky, so be careful to not be confused.
208 // If this is null, the symbol is an absolute symbol.
209 InputSectionBase<ELFT> *&Section;
210
211private:
212 static InputSectionBase<ELFT> *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000213};
214
Rui Ueyama0b289522016-02-25 18:43:51 +0000215template <class ELFT>
216InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
217
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000218// DefinedSynthetic is a class to represent linker-generated ELF symbols.
219// The difference from the regular symbol is that DefinedSynthetic symbols
220// don't belong to any input files or sections. Thus, its constructor
221// takes an output section to calculate output VA, etc.
Peter Collingbourne6a422592016-05-03 01:21:08 +0000222// If Section is null, this symbol is relative to the image base.
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000223class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000224public:
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000225 DefinedSynthetic(StringRef Name, uint64_t Value,
226 const OutputSectionBase *Section)
227 : Defined(SymbolBody::DefinedSyntheticKind, Name, /*IsLocal=*/false,
228 llvm::ELF::STV_HIDDEN, 0 /* Type */),
229 Value(Value), Section(Section) {}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000230
231 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000232 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000233 }
234
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000235 uint64_t Value;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000236 const OutputSectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000237};
238
Peter Smith5191c6f2017-01-27 13:10:16 +0000239class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000240public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000241 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
242 InputFile *F);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000243
Rafael Espindolaf4765732016-04-06 13:22:41 +0000244 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000245 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000246 }
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000247
Rui Ueyama434b5612016-07-17 03:11:46 +0000248 InputFile *file() { return this->File; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000249};
250
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000251template <class ELFT> class SharedSymbol : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000252 typedef typename ELFT::Sym Elf_Sym;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000253 typedef typename ELFT::Verdef Elf_Verdef;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000254 typedef typename ELFT::uint uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000255
256public:
257 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000258 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000259 }
260
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000261 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
262 const Elf_Verdef *Verdef)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000263 : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, Sym.st_other,
264 Sym.getType()),
Rui Ueyama434b5612016-07-17 03:11:46 +0000265 Sym(Sym), Verdef(Verdef) {
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000266 // IFuncs defined in DSOs are treated as functions by the static linker.
267 if (isGnuIFunc())
268 Type = llvm::ELF::STT_FUNC;
Rui Ueyama434b5612016-07-17 03:11:46 +0000269 this->File = F;
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000270 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000271
Rui Ueyama434b5612016-07-17 03:11:46 +0000272 SharedFile<ELFT> *file() { return (SharedFile<ELFT> *)this->File; }
273
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000274 const Elf_Sym &Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000275
George Rimard3566302016-06-20 11:55:12 +0000276 // This field is a pointer to the symbol's version definition.
277 const Elf_Verdef *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000278
Peter Collingbournefeb66292017-01-10 01:21:50 +0000279 // CopyOffset is significant only when needsCopy() is true.
280 uintX_t CopyOffset = 0;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000281
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000282 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
Peter Collingbournefeb66292017-01-10 01:21:50 +0000283
284 OutputSection<ELFT> *getBssSectionForCopy() const;
Rafael Espindola18173d42015-09-08 15:50:05 +0000285};
286
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000287// This class represents a symbol defined in an archive file. It is
288// created from an archive file header, and it knows how to load an
289// object file from an archive to replace itself with a defined
290// symbol. If the resolver finds both Undefined and Lazy for
291// the same name, it will ask the Lazy to load a file.
292class Lazy : public SymbolBody {
293public:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000294 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000295
296 // Returns an object file for this symbol, or a nullptr if the file
297 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000298 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000299
300protected:
301 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000302 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000303};
304
305// LazyArchive symbols represents symbols in archive files.
306class LazyArchive : public Lazy {
307public:
Rafael Espindola07543a82016-06-14 21:40:23 +0000308 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
Rui Ueyama434b5612016-07-17 03:11:46 +0000309 uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000310
311 static bool classof(const SymbolBody *S) {
312 return S->kind() == LazyArchiveKind;
313 }
314
Rui Ueyama434b5612016-07-17 03:11:46 +0000315 ArchiveFile *file() { return (ArchiveFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000316 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000317
318private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000319 const llvm::object::Archive::Symbol Sym;
320};
321
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000322// LazyObject symbols represents symbols in object files between
323// --start-lib and --end-lib options.
324class LazyObject : public Lazy {
325public:
Rui Ueyama434b5612016-07-17 03:11:46 +0000326 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000327
328 static bool classof(const SymbolBody *S) {
329 return S->kind() == LazyObjectKind;
330 }
331
Rui Ueyama434b5612016-07-17 03:11:46 +0000332 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000333 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000334};
335
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000336// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000337// DefinedRegular symbols.
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000338template <class ELFT> struct ElfSym {
Petr Hosek4f7a5c32016-08-22 18:44:04 +0000339 // The content for __ehdr_start symbol.
340 static DefinedRegular<ELFT> *EhdrStart;
341
George Rimar9e859392016-02-26 14:36:36 +0000342 // The content for _etext and etext symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000343 static DefinedRegular<ELFT> *Etext;
344 static DefinedRegular<ELFT> *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000345
346 // The content for _edata and edata symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000347 static DefinedRegular<ELFT> *Edata;
348 static DefinedRegular<ELFT> *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000349
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000350 // The content for _end and end symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000351 static DefinedRegular<ELFT> *End;
352 static DefinedRegular<ELFT> *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000353
Simon Atanasyan8469b882016-11-23 22:22:16 +0000354 // The content for _gp_disp/__gnu_local_gp symbols for MIPS target.
355 static DefinedRegular<ELFT> *MipsGpDisp;
356 static DefinedRegular<ELFT> *MipsLocalGp;
Simon Atanasyan6a4eb752016-12-08 06:19:47 +0000357 static DefinedRegular<ELFT> *MipsGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000358};
359
Petr Hosek4f7a5c32016-08-22 18:44:04 +0000360template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::EhdrStart;
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000361template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
362template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
363template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
364template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
365template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
366template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
Simon Atanasyan8469b882016-11-23 22:22:16 +0000367template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGpDisp;
368template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsLocalGp;
Simon Atanasyan6a4eb752016-12-08 06:19:47 +0000369template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::MipsGp;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000370
371// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
372// always one Symbol for each symbol name. The resolver updates the SymbolBody
373// stored in the Body field of this object as it resolves symbols. Symbol also
374// holds computed properties of symbol names.
375struct Symbol {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000376 // Symbol binding. This is on the Symbol to track changes during resolution.
377 // In particular:
378 // An undefined weak is still weak when it resolves to a shared library.
379 // An undefined weak will not fetch archive members, but we have to remember
380 // it is weak.
381 uint8_t Binding;
382
George Rimard3566302016-06-20 11:55:12 +0000383 // Version definition index.
384 uint16_t VersionId;
385
Peter Collingbourne4f952702016-05-01 04:55:03 +0000386 // Symbol visibility. This is the computed minimum visibility of all
387 // observed non-DSO symbols.
388 unsigned Visibility : 2;
389
390 // True if the symbol was used for linking and thus need to be added to the
391 // output file's symbol table. This is true for all symbols except for
392 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
393 // by other bitcode objects.
394 unsigned IsUsedInRegularObj : 1;
395
396 // If this flag is true and the symbol has protected or default visibility, it
397 // will appear in .dynsym. This flag is set by interposable DSO symbols in
398 // executables, by most symbols in DSOs and executables built with
399 // --export-dynamic, and by dynamic lists.
400 unsigned ExportDynamic : 1;
401
Rui Ueyama69c778c2016-07-17 17:50:09 +0000402 // True if this symbol is specified by --trace-symbol option.
403 unsigned Traced : 1;
404
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000405 // This symbol version was found in a version script.
406 unsigned InVersionScript : 1;
407
Peter Collingbourne4f952702016-05-01 04:55:03 +0000408 bool includeInDynsym() const;
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000409 uint8_t computeBinding() const;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000410 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
411
412 // This field is used to store the Symbol's SymbolBody. This instantiation of
413 // AlignedCharArrayUnion gives us a struct with a char array field that is
414 // large and aligned enough to store any derived class of SymbolBody. We
415 // assume that the size and alignment of ELF64LE symbols is sufficient for any
416 // ELFT, and we verify this with the static_asserts in replaceBody.
Rafael Espindolae7553e42016-08-31 13:28:33 +0000417 llvm::AlignedCharArrayUnion<
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000418 DefinedCommon, DefinedRegular<llvm::object::ELF64LE>, DefinedSynthetic,
Peter Smith5191c6f2017-01-27 13:10:16 +0000419 Undefined, SharedSymbol<llvm::object::ELF64LE>, LazyArchive, LazyObject>
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000420 Body;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000421
422 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
423 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
424};
425
Rui Ueyama69c778c2016-07-17 17:50:09 +0000426void printTraceSymbol(Symbol *Sym);
427
Peter Collingbourne4f952702016-05-01 04:55:03 +0000428template <typename T, typename... ArgT>
429void replaceBody(Symbol *S, ArgT &&... Arg) {
430 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
Benjamin Kramer922b63b2016-10-20 15:55:41 +0000431 static_assert(alignof(T) <= alignof(decltype(S->Body)),
Peter Collingbourne4f952702016-05-01 04:55:03 +0000432 "Body not aligned enough");
Peter Collingbournef643fc12016-05-01 05:39:02 +0000433 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
434 "Not a SymbolBody");
Rui Ueyama69c778c2016-07-17 17:50:09 +0000435
Peter Collingbourne4f952702016-05-01 04:55:03 +0000436 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000437
438 // Print out a log message if --trace-symbol was specified.
439 // This is for debugging.
440 if (S->Traced)
441 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000442}
443
444inline Symbol *SymbolBody::symbol() {
445 assert(!isLocal());
446 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
447 offsetof(Symbol, Body));
448}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000449} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000450
451std::string toString(const elf::SymbolBody &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000452} // namespace lld
453
454#endif