blob: fe9457fa56cb99330cf4d33acd9d30fb79a3a5f9 [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;
Dmitry Mikulin1e30f072017-09-08 16:22:43 +000030class BssSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031class InputFile;
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000032class LazyObjFile;
33template <class ELFT> class ObjFile;
Rafael Espindola24e6f362017-02-24 15:07:30 +000034class OutputSection;
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 Espindola5616adf2017-03-08 22:36:28 +000047 DefinedLast = DefinedCommonKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000048 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000049 LazyArchiveKind,
50 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000051 };
52
Rui Ueyama69c778c2016-07-17 17:50:09 +000053 SymbolBody(Kind K) : SymbolKind(K) {}
54
Peter Collingbourne4f952702016-05-01 04:55:03 +000055 Symbol *symbol();
56 const Symbol *symbol() const {
57 return const_cast<SymbolBody *>(this)->symbol();
58 }
59
Michael J. Spencercdae0a42015-07-28 22:58:25 +000060 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000061
Peter Collingbourne60976ed2016-04-27 00:05:06 +000062 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000063 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000064 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +000065 bool isLazy() const {
66 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
67 }
Rafael Espindola18173d42015-09-08 15:50:05 +000068 bool isShared() const { return SymbolKind == SharedKind; }
George Rimard92e1282017-07-12 11:09:46 +000069 bool isInCurrentDSO() const {
70 return !isUndefined() && !isShared() && !isLazy();
71 }
Peter Collingbourne4f952702016-05-01 04:55:03 +000072 bool isLocal() const { return IsLocal; }
Rafael Espindola3d9f1c02017-09-13 20:43:04 +000073
74 // True is this is an undefined weak symbol. This only works once
75 // all input files have been added.
76 bool isUndefWeak() const;
77
Rafael Espindola6e93d052017-08-04 22:31:42 +000078 InputFile *getFile() const;
Rafael Espindola35c908f2017-08-10 15:05:37 +000079 bool isPreemptible() const { return IsPreemptible; }
Rui Ueyamaa13efc22016-11-29 18:05:04 +000080 StringRef getName() const { return Name; }
Rui Ueyamab5792b22016-04-04 19:09:08 +000081 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +000082 void parseSymbolVersion();
Rui Ueyamab2269ec2017-06-28 19:43:02 +000083 void copy(SymbolBody *Other);
Rafael Espindola78471f02015-09-01 23:12:52 +000084
Rafael Espindola5c2310c2015-09-18 14:40:19 +000085 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000086 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000087
George Rimarf64618a2017-03-17 11:56:54 +000088 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000089
George Rimar4afe42e2017-03-17 14:12:51 +000090 uint64_t getGotOffset() const;
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +000091 uint64_t getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +000092 uint64_t getGotPltOffset() const;
93 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +000094 uint64_t getPltVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000095 template <class ELFT> typename ELFT::uint getSize() const;
George Rimar69268a82017-03-16 11:06:13 +000096 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +000097
Rafael Espindola7ae21772016-10-26 00:58:23 +000098 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +000099 uint32_t GotIndex = -1;
100 uint32_t GotPltIndex = -1;
101 uint32_t PltIndex = -1;
102 uint32_t GlobalDynIndex = -1;
103
Michael J. Spencer84487f12015-07-24 21:03:07 +0000104protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000105 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
106 uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000107
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000108 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000109
Rafael Espindolaabebed92016-02-05 15:27:15 +0000110public:
Rui Ueyama924b3612017-02-16 06:12:22 +0000111 // True the symbol should point to its PLT entry.
112 // For SharedSymbol only.
113 unsigned NeedsPltAddr : 1;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000114
Peter Collingbourne4f952702016-05-01 04:55:03 +0000115 // True if this is a local symbol.
116 unsigned IsLocal : 1;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000117
Simon Atanasyan41325112016-06-19 21:39:37 +0000118 // True if this symbol has an entry in the global part of MIPS GOT.
119 unsigned IsInGlobalMipsGot : 1;
120
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000121 // True if this symbol is referenced by 32-bit GOT relocations.
122 unsigned Is32BitMipsGot : 1;
123
Peter Smithbaffdb82016-12-08 12:58:55 +0000124 // True if this symbol is in the Iplt sub-section of the Plt.
125 unsigned IsInIplt : 1;
126
127 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
128 unsigned IsInIgot : 1;
129
Rafael Espindola35c908f2017-08-10 15:05:37 +0000130 unsigned IsPreemptible : 1;
131
Rui Ueyamadd418072016-04-04 18:15:38 +0000132 // The following fields have the same meaning as the ELF symbol attributes.
133 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000134 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000135
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000136 // The Type field may also have this value. It means that we have not yet seen
137 // a non-Lazy symbol with this name, so we don't know what its type is. The
138 // Type field is normally set to this value for Lazy symbols unless we saw a
139 // weak undefined symbol first, in which case we need to remember the original
140 // symbol's type in order to check for TLS mismatches.
141 enum { UnknownType = 255 };
142
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000143 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
144 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
145 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
146 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
147 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
148 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000149
George Rimar2f0fab52016-03-06 06:26:18 +0000150protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000151 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000152};
153
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000154// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000155class Defined : public SymbolBody {
156public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000157 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000158 static bool classof(const SymbolBody *S) { return S->isDefined(); }
159};
160
Rafael Espindolae7553e42016-08-31 13:28:33 +0000161class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000162public:
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000163 DefinedCommon(StringRef N, uint64_t Size, uint32_t Alignment, uint8_t StOther,
Rafael Espindola6e93d052017-08-04 22:31:42 +0000164 uint8_t Type);
Rafael Espindola51d46902015-08-28 21:26:51 +0000165
166 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000167 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000168 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000169
Rui Ueyama9c77d272017-08-10 15:54:27 +0000170 // True if this symbol is not GC'ed. Liveness is usually a notion of
171 // input sections and not of symbols, but since common symbols don't
172 // belong to any input section, their liveness is managed by this bit.
173 bool Live;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000174
175 // The maximum alignment we have seen for this symbol.
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000176 uint32_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000177
Rui Ueyama9c77d272017-08-10 15:54:27 +0000178 // The output offset of this common symbol in the output bss.
179 // Computed by the writer.
Rafael Espindola11191912015-12-24 16:23:37 +0000180 uint64_t Size;
Dmitry Mikulin1e30f072017-09-08 16:22:43 +0000181 BssSection *Section = nullptr;
Rafael Espindola51d46902015-08-28 21:26:51 +0000182};
183
Michael J. Spencer84487f12015-07-24 21:03:07 +0000184// Regular defined symbols read from object file symbol tables.
Rui Ueyama80474a22017-02-28 19:29:55 +0000185class DefinedRegular : public Defined {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000186public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000187 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
Rafael Espindola6e93d052017-08-04 22:31:42 +0000188 uint64_t Value, uint64_t Size, SectionBase *Section)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000189 : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
Rafael Espindola6e93d052017-08-04 22:31:42 +0000190 Value(Value), Size(Size), Section(Section) {}
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000191
Simon Atanasyanf967f092016-09-29 12:58:36 +0000192 // Return true if the symbol is a PIC function.
Rui Ueyama80474a22017-02-28 19:29:55 +0000193 template <class ELFT> bool isMipsPIC() const;
Simon Atanasyanf967f092016-09-29 12:58:36 +0000194
Michael J. Spencer84487f12015-07-24 21:03:07 +0000195 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000196 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000197 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000198
Rui Ueyama80474a22017-02-28 19:29:55 +0000199 uint64_t Value;
200 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000201 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000202};
203
Peter Smith3a52eb02017-02-01 10:26:03 +0000204class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000205public:
Rafael Espindola6e93d052017-08-04 22:31:42 +0000206 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000207
Rafael Espindolaf4765732016-04-06 13:22:41 +0000208 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000209 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000210 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000211};
212
Rui Ueyama4076fa12017-02-26 23:35:34 +0000213class SharedSymbol : public Defined {
Rafael Espindola18173d42015-09-08 15:50:05 +0000214public:
215 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000216 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000217 }
218
Rafael Espindola6e93d052017-08-04 22:31:42 +0000219 SharedSymbol(StringRef Name, uint8_t StOther, uint8_t Type,
Rui Ueyama4076fa12017-02-26 23:35:34 +0000220 const void *ElfSym, const void *Verdef)
221 : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, StOther, Type),
222 Verdef(Verdef), ElfSym(ElfSym) {
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000223 // IFuncs defined in DSOs are treated as functions by the static linker.
224 if (isGnuIFunc())
George Rimar4a9cfc82017-07-11 11:40:59 +0000225 this->Type = llvm::ELF::STT_FUNC;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000226 }
227
228 template <class ELFT> SharedFile<ELFT> *getFile() const {
229 return cast<SharedFile<ELFT>>(SymbolBody::getFile());
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000230 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000231
Rui Ueyama4076fa12017-02-26 23:35:34 +0000232 template <class ELFT> uint64_t getShndx() const {
233 return getSym<ELFT>().st_shndx;
234 }
Rui Ueyama434b5612016-07-17 03:11:46 +0000235
Rui Ueyama4076fa12017-02-26 23:35:34 +0000236 template <class ELFT> uint64_t getValue() const {
237 return getSym<ELFT>().st_value;
238 }
239
240 template <class ELFT> uint64_t getSize() const {
241 return getSym<ELFT>().st_size;
242 }
243
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000244 template <class ELFT> uint32_t getAlignment() const;
George Rimarbc590fe2015-10-28 16:48:58 +0000245
George Rimard3566302016-06-20 11:55:12 +0000246 // This field is a pointer to the symbol's version definition.
Rui Ueyama4076fa12017-02-26 23:35:34 +0000247 const void *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000248
George Rimar1ab9cf42017-03-17 10:14:53 +0000249 // CopyRelSec and CopyRelSecOff are significant only when NeedsCopy is true.
250 InputSection *CopyRelSec;
Rui Ueyama007c0022017-03-08 17:24:24 +0000251
Rui Ueyama4076fa12017-02-26 23:35:34 +0000252private:
253 template <class ELFT> const typename ELFT::Sym &getSym() const {
254 return *(const typename ELFT::Sym *)ElfSym;
255 }
256
257 const void *ElfSym;
Rafael Espindola18173d42015-09-08 15:50:05 +0000258};
259
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000260// This class represents a symbol defined in an archive file. It is
261// created from an archive file header, and it knows how to load an
262// object file from an archive to replace itself with a defined
263// symbol. If the resolver finds both Undefined and Lazy for
264// the same name, it will ask the Lazy to load a file.
265class Lazy : public SymbolBody {
266public:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000267 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000268
269 // Returns an object file for this symbol, or a nullptr if the file
270 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000271 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000272
273protected:
274 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000275 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000276};
277
278// LazyArchive symbols represents symbols in archive files.
279class LazyArchive : public Lazy {
280public:
Rafael Espindola6e93d052017-08-04 22:31:42 +0000281 LazyArchive(const llvm::object::Archive::Symbol S, uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000282
283 static bool classof(const SymbolBody *S) {
284 return S->kind() == LazyArchiveKind;
285 }
286
Rafael Espindola6e93d052017-08-04 22:31:42 +0000287 ArchiveFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000288 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000289
290private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000291 const llvm::object::Archive::Symbol Sym;
292};
293
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000294// LazyObject symbols represents symbols in object files between
295// --start-lib and --end-lib options.
296class LazyObject : public Lazy {
297public:
Rafael Espindola6e93d052017-08-04 22:31:42 +0000298 LazyObject(StringRef Name, uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000299
300 static bool classof(const SymbolBody *S) {
301 return S->kind() == LazyObjectKind;
302 }
303
Rafael Espindola6e93d052017-08-04 22:31:42 +0000304 LazyObjFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000305 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000306};
307
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000308// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000309// DefinedRegular symbols.
Rui Ueyama80474a22017-02-28 19:29:55 +0000310struct ElfSym {
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000311 // __bss_start
George Rimare6c5d382017-04-05 10:03:25 +0000312 static DefinedRegular *Bss;
313
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000314 // etext and _etext
315 static DefinedRegular *Etext1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000316 static DefinedRegular *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000317
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000318 // edata and _edata
319 static DefinedRegular *Edata1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000320 static DefinedRegular *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000321
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000322 // end and _end
323 static DefinedRegular *End1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000324 static DefinedRegular *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000325
Rui Ueyama92c37812017-06-26 15:11:24 +0000326 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
327 // be at some offset from the base of the .got section, usually 0 or
328 // the end of the .got.
329 static DefinedRegular *GlobalOffsetTable;
330
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000331 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
332 static DefinedRegular *MipsGp;
Rui Ueyama80474a22017-02-28 19:29:55 +0000333 static DefinedRegular *MipsGpDisp;
334 static DefinedRegular *MipsLocalGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000335};
336
Peter Collingbourne4f952702016-05-01 04:55:03 +0000337// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
338// always one Symbol for each symbol name. The resolver updates the SymbolBody
339// stored in the Body field of this object as it resolves symbols. Symbol also
340// holds computed properties of symbol names.
341struct Symbol {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000342 // Symbol binding. This is on the Symbol to track changes during resolution.
343 // In particular:
344 // An undefined weak is still weak when it resolves to a shared library.
345 // An undefined weak will not fetch archive members, but we have to remember
346 // it is weak.
347 uint8_t Binding;
348
George Rimard3566302016-06-20 11:55:12 +0000349 // Version definition index.
350 uint16_t VersionId;
351
Peter Collingbourne4f952702016-05-01 04:55:03 +0000352 // Symbol visibility. This is the computed minimum visibility of all
353 // observed non-DSO symbols.
354 unsigned Visibility : 2;
355
356 // True if the symbol was used for linking and thus need to be added to the
357 // output file's symbol table. This is true for all symbols except for
358 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
359 // by other bitcode objects.
360 unsigned IsUsedInRegularObj : 1;
361
362 // If this flag is true and the symbol has protected or default visibility, it
363 // will appear in .dynsym. This flag is set by interposable DSO symbols in
364 // executables, by most symbols in DSOs and executables built with
365 // --export-dynamic, and by dynamic lists.
366 unsigned ExportDynamic : 1;
367
Rui Ueyama69c778c2016-07-17 17:50:09 +0000368 // True if this symbol is specified by --trace-symbol option.
369 unsigned Traced : 1;
370
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000371 // This symbol version was found in a version script.
372 unsigned InVersionScript : 1;
373
Rafael Espindola6e93d052017-08-04 22:31:42 +0000374 // The file from which this symbol was created.
375 InputFile *File = nullptr;
376
Peter Collingbourne4f952702016-05-01 04:55:03 +0000377 bool includeInDynsym() const;
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000378 uint8_t computeBinding() const;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000379 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
380
381 // This field is used to store the Symbol's SymbolBody. This instantiation of
382 // AlignedCharArrayUnion gives us a struct with a char array field that is
Rui Ueyama80474a22017-02-28 19:29:55 +0000383 // large and aligned enough to store any derived class of SymbolBody.
Rafael Espindola5616adf2017-03-08 22:36:28 +0000384 llvm::AlignedCharArrayUnion<DefinedCommon, DefinedRegular, Undefined,
385 SharedSymbol, LazyArchive, LazyObject>
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000386 Body;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000387
388 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
389 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
390};
391
Rui Ueyama69c778c2016-07-17 17:50:09 +0000392void printTraceSymbol(Symbol *Sym);
393
Peter Collingbourne4f952702016-05-01 04:55:03 +0000394template <typename T, typename... ArgT>
Rafael Espindola6e93d052017-08-04 22:31:42 +0000395void replaceBody(Symbol *S, InputFile *File, ArgT &&... Arg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000396 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
Benjamin Kramer922b63b2016-10-20 15:55:41 +0000397 static_assert(alignof(T) <= alignof(decltype(S->Body)),
Peter Collingbourne4f952702016-05-01 04:55:03 +0000398 "Body not aligned enough");
Peter Collingbournef643fc12016-05-01 05:39:02 +0000399 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
400 "Not a SymbolBody");
Rafael Espindola6e93d052017-08-04 22:31:42 +0000401 S->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000402 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000403
404 // Print out a log message if --trace-symbol was specified.
405 // This is for debugging.
406 if (S->Traced)
407 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000408}
409
410inline Symbol *SymbolBody::symbol() {
411 assert(!isLocal());
412 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
413 offsetof(Symbol, Body));
414}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000415} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000416
417std::string toString(const elf::SymbolBody &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000418} // namespace lld
419
420#endif