blob: 39a0c0f7b4dfd61dbac8161f27ac6bb6748c64a9 [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;
Rafael Espindola24e6f362017-02-24 15:07:30 +000033class OutputSection;
Rui Ueyama35da9b62015-10-11 20:59:12 +000034template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000035
Peter Collingbourne4f952702016-05-01 04:55:03 +000036struct Symbol;
Michael J. Spencer84487f12015-07-24 21:03:07 +000037
38// The base class for real symbol classes.
39class SymbolBody {
40public:
41 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000042 DefinedFirst,
43 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000044 SharedKind,
Rafael Espindola11191912015-12-24 16:23:37 +000045 DefinedCommonKind,
Rafael Espindola5616adf2017-03-08 22:36:28 +000046 DefinedLast = DefinedCommonKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000047 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000048 LazyArchiveKind,
49 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000050 };
51
Rui Ueyama69c778c2016-07-17 17:50:09 +000052 SymbolBody(Kind K) : SymbolKind(K) {}
53
Peter Collingbourne4f952702016-05-01 04:55:03 +000054 Symbol *symbol();
55 const Symbol *symbol() const {
56 return const_cast<SymbolBody *>(this)->symbol();
57 }
58
Michael J. Spencercdae0a42015-07-28 22:58:25 +000059 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000060
Peter Collingbourne60976ed2016-04-27 00:05:06 +000061 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000062 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000063 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +000064 bool isLazy() const {
65 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
66 }
Rafael Espindola18173d42015-09-08 15:50:05 +000067 bool isShared() const { return SymbolKind == SharedKind; }
Rafael Espindola1d6d1b42017-01-17 16:08:06 +000068 bool isInCurrentDSO() const { return !isUndefined() && !isShared(); }
Peter Collingbourne4f952702016-05-01 04:55:03 +000069 bool isLocal() const { return IsLocal; }
Rui Ueyamac4466602016-03-13 19:48:18 +000070 bool isPreemptible() const;
Rui Ueyamaa13efc22016-11-29 18:05:04 +000071 StringRef getName() const { return Name; }
Rui Ueyamab5792b22016-04-04 19:09:08 +000072 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +000073 void parseSymbolVersion();
Rafael Espindola78471f02015-09-01 23:12:52 +000074
Rafael Espindola5c2310c2015-09-18 14:40:19 +000075 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000076 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000077
George Rimarf64618a2017-03-17 11:56:54 +000078 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000079
George Rimar4afe42e2017-03-17 14:12:51 +000080 uint64_t getGotOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000081 template <class ELFT> typename ELFT::uint getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +000082 uint64_t getGotPltOffset() const;
83 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +000084 uint64_t getPltVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000085 template <class ELFT> typename ELFT::uint getSize() const;
George Rimar69268a82017-03-16 11:06:13 +000086 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +000087
Rui Ueyama434b5612016-07-17 03:11:46 +000088 // The file from which this symbol was created.
89 InputFile *File = nullptr;
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +000090
Rafael Espindola7ae21772016-10-26 00:58:23 +000091 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +000092 uint32_t GotIndex = -1;
93 uint32_t GotPltIndex = -1;
94 uint32_t PltIndex = -1;
95 uint32_t GlobalDynIndex = -1;
96
Michael J. Spencer84487f12015-07-24 21:03:07 +000097protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +000098 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
99 uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000100
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000101 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000102
Rafael Espindolaabebed92016-02-05 15:27:15 +0000103public:
Rui Ueyama007c0022017-03-08 17:24:24 +0000104 // True if the linker has to generate a copy relocation.
105 // For SharedSymbol only.
106 unsigned NeedsCopy : 1;
107
Rui Ueyama924b3612017-02-16 06:12:22 +0000108 // True the symbol should point to its PLT entry.
109 // For SharedSymbol only.
110 unsigned NeedsPltAddr : 1;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000111
Peter Collingbourne4f952702016-05-01 04:55:03 +0000112 // True if this is a local symbol.
113 unsigned IsLocal : 1;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000114
Simon Atanasyan41325112016-06-19 21:39:37 +0000115 // True if this symbol has an entry in the global part of MIPS GOT.
116 unsigned IsInGlobalMipsGot : 1;
117
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000118 // True if this symbol is referenced by 32-bit GOT relocations.
119 unsigned Is32BitMipsGot : 1;
120
Peter Smithbaffdb82016-12-08 12:58:55 +0000121 // True if this symbol is in the Iplt sub-section of the Plt.
122 unsigned IsInIplt : 1;
123
124 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
125 unsigned IsInIgot : 1;
126
Rui Ueyamadd418072016-04-04 18:15:38 +0000127 // The following fields have the same meaning as the ELF symbol attributes.
128 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000129 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000130
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000131 // The Type field may also have this value. It means that we have not yet seen
132 // a non-Lazy symbol with this name, so we don't know what its type is. The
133 // Type field is normally set to this value for Lazy symbols unless we saw a
134 // weak undefined symbol first, in which case we need to remember the original
135 // symbol's type in order to check for TLS mismatches.
136 enum { UnknownType = 255 };
137
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000138 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
139 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
140 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
141 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
142 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
143 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000144
George Rimar2f0fab52016-03-06 06:26:18 +0000145protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000146 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000147};
148
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000149// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000150class Defined : public SymbolBody {
151public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000152 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000153 static bool classof(const SymbolBody *S) { return S->isDefined(); }
154};
155
Rafael Espindolae7553e42016-08-31 13:28:33 +0000156class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000157public:
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000158 DefinedCommon(StringRef N, uint64_t Size, uint32_t Alignment, uint8_t StOther,
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000159 uint8_t Type, InputFile *File);
Rafael Espindola51d46902015-08-28 21:26:51 +0000160
161 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000162 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000163 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000164
165 // The output offset of this common symbol in the output bss. Computed by the
166 // writer.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000167 uint64_t Offset;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000168
169 // The maximum alignment we have seen for this symbol.
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000170 uint32_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000171
172 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000173};
174
Michael J. Spencer84487f12015-07-24 21:03:07 +0000175// Regular defined symbols read from object file symbol tables.
Rui Ueyama80474a22017-02-28 19:29:55 +0000176class DefinedRegular : public Defined {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000177public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000178 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
Rafael Espindola5616adf2017-03-08 22:36:28 +0000179 uint64_t Value, uint64_t Size, SectionBase *Section,
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000180 InputFile *File)
181 : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
Rafael Espindola5616adf2017-03-08 22:36:28 +0000182 Value(Value), Size(Size), Section(Section) {
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000183 this->File = File;
184 }
185
Simon Atanasyanf967f092016-09-29 12:58:36 +0000186 // Return true if the symbol is a PIC function.
Rui Ueyama80474a22017-02-28 19:29:55 +0000187 template <class ELFT> bool isMipsPIC() const;
Simon Atanasyanf967f092016-09-29 12:58:36 +0000188
Michael J. Spencer84487f12015-07-24 21:03:07 +0000189 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000190 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000191 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000192
Rui Ueyama80474a22017-02-28 19:29:55 +0000193 uint64_t Value;
194 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000195 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000196};
197
Peter Smith3a52eb02017-02-01 10:26:03 +0000198class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000199public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000200 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
201 InputFile *F);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000202
Rafael Espindolaf4765732016-04-06 13:22:41 +0000203 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000204 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000205 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000206};
207
Rui Ueyama4076fa12017-02-26 23:35:34 +0000208class SharedSymbol : public Defined {
Rafael Espindola18173d42015-09-08 15:50:05 +0000209public:
210 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000211 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000212 }
213
Rui Ueyama4076fa12017-02-26 23:35:34 +0000214 SharedSymbol(InputFile *File, StringRef Name, uint8_t StOther, uint8_t Type,
215 const void *ElfSym, const void *Verdef)
216 : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, StOther, Type),
217 Verdef(Verdef), ElfSym(ElfSym) {
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000218 // IFuncs defined in DSOs are treated as functions by the static linker.
219 if (isGnuIFunc())
220 Type = llvm::ELF::STT_FUNC;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000221 this->File = File;
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000222 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000223
Rui Ueyama4076fa12017-02-26 23:35:34 +0000224 template <class ELFT> uint64_t getShndx() const {
225 return getSym<ELFT>().st_shndx;
226 }
Rui Ueyama434b5612016-07-17 03:11:46 +0000227
Rui Ueyama4076fa12017-02-26 23:35:34 +0000228 template <class ELFT> uint64_t getValue() const {
229 return getSym<ELFT>().st_value;
230 }
231
232 template <class ELFT> uint64_t getSize() const {
233 return getSym<ELFT>().st_size;
234 }
235
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000236 template <class ELFT> uint32_t getAlignment() const;
George Rimarbc590fe2015-10-28 16:48:58 +0000237
George Rimard3566302016-06-20 11:55:12 +0000238 // This field is a pointer to the symbol's version definition.
Rui Ueyama4076fa12017-02-26 23:35:34 +0000239 const void *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000240
George Rimar1ab9cf42017-03-17 10:14:53 +0000241 // CopyRelSec and CopyRelSecOff are significant only when NeedsCopy is true.
242 InputSection *CopyRelSec;
Rui Ueyama6022b2b2017-03-29 00:49:50 +0000243 uint64_t CopyRelSecOff;
Rui Ueyama007c0022017-03-08 17:24:24 +0000244
Rui Ueyama4076fa12017-02-26 23:35:34 +0000245private:
246 template <class ELFT> const typename ELFT::Sym &getSym() const {
247 return *(const typename ELFT::Sym *)ElfSym;
248 }
249
250 const void *ElfSym;
Rafael Espindola18173d42015-09-08 15:50:05 +0000251};
252
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000253// This class represents a symbol defined in an archive file. It is
254// created from an archive file header, and it knows how to load an
255// object file from an archive to replace itself with a defined
256// symbol. If the resolver finds both Undefined and Lazy for
257// the same name, it will ask the Lazy to load a file.
258class Lazy : public SymbolBody {
259public:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000260 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000261
262 // Returns an object file for this symbol, or a nullptr if the file
263 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000264 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000265
266protected:
267 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000268 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000269};
270
271// LazyArchive symbols represents symbols in archive files.
272class LazyArchive : public Lazy {
273public:
Rafael Espindola07543a82016-06-14 21:40:23 +0000274 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
Rui Ueyama434b5612016-07-17 03:11:46 +0000275 uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000276
277 static bool classof(const SymbolBody *S) {
278 return S->kind() == LazyArchiveKind;
279 }
280
Rui Ueyama434b5612016-07-17 03:11:46 +0000281 ArchiveFile *file() { return (ArchiveFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000282 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000283
284private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000285 const llvm::object::Archive::Symbol Sym;
286};
287
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000288// LazyObject symbols represents symbols in object files between
289// --start-lib and --end-lib options.
290class LazyObject : public Lazy {
291public:
Rui Ueyama434b5612016-07-17 03:11:46 +0000292 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000293
294 static bool classof(const SymbolBody *S) {
295 return S->kind() == LazyObjectKind;
296 }
297
Rui Ueyama434b5612016-07-17 03:11:46 +0000298 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000299 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000300};
301
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000302// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000303// DefinedRegular symbols.
Rui Ueyama80474a22017-02-28 19:29:55 +0000304struct ElfSym {
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000305 // __bss_start
George Rimare6c5d382017-04-05 10:03:25 +0000306 static DefinedRegular *Bss;
307
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000308 // etext and _etext
309 static DefinedRegular *Etext1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000310 static DefinedRegular *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000311
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000312 // edata and _edata
313 static DefinedRegular *Edata1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000314 static DefinedRegular *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000315
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000316 // end and _end
317 static DefinedRegular *End1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000318 static DefinedRegular *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000319
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000320 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
321 static DefinedRegular *MipsGp;
Rui Ueyama80474a22017-02-28 19:29:55 +0000322 static DefinedRegular *MipsGpDisp;
323 static DefinedRegular *MipsLocalGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000324};
325
Peter Collingbourne4f952702016-05-01 04:55:03 +0000326// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
327// always one Symbol for each symbol name. The resolver updates the SymbolBody
328// stored in the Body field of this object as it resolves symbols. Symbol also
329// holds computed properties of symbol names.
330struct Symbol {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000331 // Symbol binding. This is on the Symbol to track changes during resolution.
332 // In particular:
333 // An undefined weak is still weak when it resolves to a shared library.
334 // An undefined weak will not fetch archive members, but we have to remember
335 // it is weak.
336 uint8_t Binding;
337
George Rimard3566302016-06-20 11:55:12 +0000338 // Version definition index.
339 uint16_t VersionId;
340
Peter Collingbourne4f952702016-05-01 04:55:03 +0000341 // Symbol visibility. This is the computed minimum visibility of all
342 // observed non-DSO symbols.
343 unsigned Visibility : 2;
344
345 // True if the symbol was used for linking and thus need to be added to the
346 // output file's symbol table. This is true for all symbols except for
347 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
348 // by other bitcode objects.
349 unsigned IsUsedInRegularObj : 1;
350
351 // If this flag is true and the symbol has protected or default visibility, it
352 // will appear in .dynsym. This flag is set by interposable DSO symbols in
353 // executables, by most symbols in DSOs and executables built with
354 // --export-dynamic, and by dynamic lists.
355 unsigned ExportDynamic : 1;
356
Rui Ueyama69c778c2016-07-17 17:50:09 +0000357 // True if this symbol is specified by --trace-symbol option.
358 unsigned Traced : 1;
359
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000360 // This symbol version was found in a version script.
361 unsigned InVersionScript : 1;
362
Peter Collingbourne4f952702016-05-01 04:55:03 +0000363 bool includeInDynsym() const;
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000364 uint8_t computeBinding() const;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000365 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
366
367 // This field is used to store the Symbol's SymbolBody. This instantiation of
368 // AlignedCharArrayUnion gives us a struct with a char array field that is
Rui Ueyama80474a22017-02-28 19:29:55 +0000369 // large and aligned enough to store any derived class of SymbolBody.
Rafael Espindola5616adf2017-03-08 22:36:28 +0000370 llvm::AlignedCharArrayUnion<DefinedCommon, DefinedRegular, Undefined,
371 SharedSymbol, LazyArchive, LazyObject>
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000372 Body;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000373
374 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
375 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
376};
377
Rui Ueyama69c778c2016-07-17 17:50:09 +0000378void printTraceSymbol(Symbol *Sym);
379
Peter Collingbourne4f952702016-05-01 04:55:03 +0000380template <typename T, typename... ArgT>
381void replaceBody(Symbol *S, ArgT &&... Arg) {
382 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
Benjamin Kramer922b63b2016-10-20 15:55:41 +0000383 static_assert(alignof(T) <= alignof(decltype(S->Body)),
Peter Collingbourne4f952702016-05-01 04:55:03 +0000384 "Body not aligned enough");
Peter Collingbournef643fc12016-05-01 05:39:02 +0000385 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
386 "Not a SymbolBody");
Rui Ueyama69c778c2016-07-17 17:50:09 +0000387
Peter Collingbourne4f952702016-05-01 04:55:03 +0000388 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000389
390 // Print out a log message if --trace-symbol was specified.
391 // This is for debugging.
392 if (S->Traced)
393 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000394}
395
396inline Symbol *SymbolBody::symbol() {
397 assert(!isLocal());
398 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
399 offsetof(Symbol, Body));
400}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000401} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000402
403std::string toString(const elf::SymbolBody &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000404} // namespace lld
405
406#endif