blob: 696a30cb3904def19d1ecddbea147bc7bbaf965f [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 Espindola4d4b06a2015-12-24 00:47:42 +000046 DefinedSyntheticKind,
47 DefinedLast = DefinedSyntheticKind,
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; }
Rafael Espindola1d6d1b42017-01-17 16:08:06 +000069 bool isInCurrentDSO() const { return !isUndefined() && !isShared(); }
Peter Collingbourne4f952702016-05-01 04:55:03 +000070 bool isLocal() const { return IsLocal; }
Rui Ueyamac4466602016-03-13 19:48:18 +000071 bool isPreemptible() const;
Rui Ueyamaa13efc22016-11-29 18:05:04 +000072 StringRef getName() const { return Name; }
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; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000078
Rafael Espindola7386cea2017-02-16 00:12:34 +000079 template <class ELFT> typename ELFT::uint getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000080
Rafael Espindola74031ba2016-04-07 15:20:56 +000081 template <class ELFT> typename ELFT::uint getGotOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000082 template <class ELFT> typename ELFT::uint getGotVA() const;
Rafael Espindola74031ba2016-04-07 15:20:56 +000083 template <class ELFT> typename ELFT::uint getGotPltOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000084 template <class ELFT> typename ELFT::uint getGotPltVA() const;
85 template <class ELFT> typename ELFT::uint getPltVA() const;
86 template <class ELFT> typename ELFT::uint getSize() const;
Rui Ueyama968db482017-02-28 04:02:42 +000087 template <class ELFT> const OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +000088
Rui Ueyama434b5612016-07-17 03:11:46 +000089 // The file from which this symbol was created.
90 InputFile *File = nullptr;
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +000091
Rafael Espindola7ae21772016-10-26 00:58:23 +000092 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +000093 uint32_t GotIndex = -1;
94 uint32_t GotPltIndex = -1;
95 uint32_t PltIndex = -1;
96 uint32_t GlobalDynIndex = -1;
97
Michael J. Spencer84487f12015-07-24 21:03:07 +000098protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +000099 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
100 uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000101
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000102 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000103
Rafael Espindolaabebed92016-02-05 15:27:15 +0000104public:
Rui Ueyama007c0022017-03-08 17:24:24 +0000105 // True if the linker has to generate a copy relocation.
106 // For SharedSymbol only.
107 unsigned NeedsCopy : 1;
108
Rui Ueyama924b3612017-02-16 06:12:22 +0000109 // True the symbol should point to its PLT entry.
110 // For SharedSymbol only.
111 unsigned NeedsPltAddr : 1;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000112
Peter Collingbourne4f952702016-05-01 04:55:03 +0000113 // True if this is a local symbol.
114 unsigned IsLocal : 1;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000115
Simon Atanasyan41325112016-06-19 21:39:37 +0000116 // True if this symbol has an entry in the global part of MIPS GOT.
117 unsigned IsInGlobalMipsGot : 1;
118
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000119 // True if this symbol is referenced by 32-bit GOT relocations.
120 unsigned Is32BitMipsGot : 1;
121
Peter Smithbaffdb82016-12-08 12:58:55 +0000122 // True if this symbol is in the Iplt sub-section of the Plt.
123 unsigned IsInIplt : 1;
124
125 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
126 unsigned IsInIgot : 1;
127
Rui Ueyamadd418072016-04-04 18:15:38 +0000128 // The following fields have the same meaning as the ELF symbol attributes.
129 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000130 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000131
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000132 // The Type field may also have this value. It means that we have not yet seen
133 // a non-Lazy symbol with this name, so we don't know what its type is. The
134 // Type field is normally set to this value for Lazy symbols unless we saw a
135 // weak undefined symbol first, in which case we need to remember the original
136 // symbol's type in order to check for TLS mismatches.
137 enum { UnknownType = 255 };
138
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000139 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
140 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
141 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
142 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
143 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
144 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000145
George Rimar2f0fab52016-03-06 06:26:18 +0000146protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000147 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000148};
149
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000150// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000151class Defined : public SymbolBody {
152public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000153 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000154 static bool classof(const SymbolBody *S) { return S->isDefined(); }
155};
156
Rafael Espindolae7553e42016-08-31 13:28:33 +0000157class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000158public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000159 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther,
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000160 uint8_t Type, InputFile *File);
Rafael Espindola51d46902015-08-28 21:26:51 +0000161
162 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000163 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000164 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000165
166 // The output offset of this common symbol in the output bss. Computed by the
167 // writer.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000168 uint64_t Offset;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000169
170 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000171 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000172
173 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000174};
175
Michael J. Spencer84487f12015-07-24 21:03:07 +0000176// Regular defined symbols read from object file symbol tables.
Rui Ueyama80474a22017-02-28 19:29:55 +0000177class DefinedRegular : public Defined {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000178public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000179 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
Rui Ueyama80474a22017-02-28 19:29:55 +0000180 uint64_t Value, uint64_t Size, InputSectionBase *Section,
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000181 InputFile *File)
182 : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000183 Value(Value), Size(Size),
184 Section(Section ? Section->Repl : NullInputSection) {
185 this->File = File;
186 }
187
Simon Atanasyanf967f092016-09-29 12:58:36 +0000188 // Return true if the symbol is a PIC function.
Rui Ueyama80474a22017-02-28 19:29:55 +0000189 template <class ELFT> bool isMipsPIC() const;
Simon Atanasyanf967f092016-09-29 12:58:36 +0000190
Michael J. Spencer84487f12015-07-24 21:03:07 +0000191 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000192 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000193 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000194
Rui Ueyama80474a22017-02-28 19:29:55 +0000195 uint64_t Value;
196 uint64_t Size;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000197
Rui Ueyama0b289522016-02-25 18:43:51 +0000198 // The input section this symbol belongs to. Notice that this is
199 // a reference to a pointer. We are using two levels of indirections
200 // because of ICF. If ICF decides two sections need to be merged, it
201 // manipulates this Section pointers so that they point to the same
202 // section. This is a bit tricky, so be careful to not be confused.
203 // If this is null, the symbol is an absolute symbol.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000204 InputSectionBase *&Section;
Rui Ueyama0b289522016-02-25 18:43:51 +0000205
206private:
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000207 static InputSectionBase *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000208};
209
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000210// DefinedSynthetic is a class to represent linker-generated ELF symbols.
211// The difference from the regular symbol is that DefinedSynthetic symbols
212// don't belong to any input files or sections. Thus, its constructor
213// takes an output section to calculate output VA, etc.
Peter Collingbourne6a422592016-05-03 01:21:08 +0000214// If Section is null, this symbol is relative to the image base.
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000215class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000216public:
Rafael Espindola24e6f362017-02-24 15:07:30 +0000217 DefinedSynthetic(StringRef Name, uint64_t Value, const OutputSection *Section)
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000218 : Defined(SymbolBody::DefinedSyntheticKind, Name, /*IsLocal=*/false,
219 llvm::ELF::STV_HIDDEN, 0 /* Type */),
220 Value(Value), Section(Section) {}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000221
222 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000223 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000224 }
225
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000226 uint64_t Value;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000227 const OutputSection *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000228};
229
Peter Smith3a52eb02017-02-01 10:26:03 +0000230class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000231public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000232 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
233 InputFile *F);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000234
Rafael Espindolaf4765732016-04-06 13:22:41 +0000235 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000236 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000237 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000238};
239
Rui Ueyama4076fa12017-02-26 23:35:34 +0000240class SharedSymbol : public Defined {
Rafael Espindola18173d42015-09-08 15:50:05 +0000241public:
242 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000243 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000244 }
245
Rui Ueyama4076fa12017-02-26 23:35:34 +0000246 SharedSymbol(InputFile *File, StringRef Name, uint8_t StOther, uint8_t Type,
247 const void *ElfSym, const void *Verdef)
248 : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, StOther, Type),
249 Verdef(Verdef), ElfSym(ElfSym) {
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000250 // IFuncs defined in DSOs are treated as functions by the static linker.
251 if (isGnuIFunc())
252 Type = llvm::ELF::STT_FUNC;
Rui Ueyama4076fa12017-02-26 23:35:34 +0000253 this->File = File;
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000254 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000255
Rui Ueyama4076fa12017-02-26 23:35:34 +0000256 template <class ELFT> uint64_t getShndx() const {
257 return getSym<ELFT>().st_shndx;
258 }
Rui Ueyama434b5612016-07-17 03:11:46 +0000259
Rui Ueyama4076fa12017-02-26 23:35:34 +0000260 template <class ELFT> uint64_t getValue() const {
261 return getSym<ELFT>().st_value;
262 }
263
264 template <class ELFT> uint64_t getSize() const {
265 return getSym<ELFT>().st_size;
266 }
267
268 template <class ELFT> uint64_t getAlignment() const;
George Rimarbc590fe2015-10-28 16:48:58 +0000269
George Rimard3566302016-06-20 11:55:12 +0000270 // This field is a pointer to the symbol's version definition.
Rui Ueyama4076fa12017-02-26 23:35:34 +0000271 const void *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000272
Rui Ueyama007c0022017-03-08 17:24:24 +0000273 // Section is significant only when NeedsCopy is true.
274 InputSection *Section = nullptr;
275
Rui Ueyama4076fa12017-02-26 23:35:34 +0000276private:
277 template <class ELFT> const typename ELFT::Sym &getSym() const {
278 return *(const typename ELFT::Sym *)ElfSym;
279 }
280
281 const void *ElfSym;
Rafael Espindola18173d42015-09-08 15:50:05 +0000282};
283
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000284// This class represents a symbol defined in an archive file. It is
285// created from an archive file header, and it knows how to load an
286// object file from an archive to replace itself with a defined
287// symbol. If the resolver finds both Undefined and Lazy for
288// the same name, it will ask the Lazy to load a file.
289class Lazy : public SymbolBody {
290public:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000291 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000292
293 // Returns an object file for this symbol, or a nullptr if the file
294 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000295 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000296
297protected:
298 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000299 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000300};
301
302// LazyArchive symbols represents symbols in archive files.
303class LazyArchive : public Lazy {
304public:
Rafael Espindola07543a82016-06-14 21:40:23 +0000305 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
Rui Ueyama434b5612016-07-17 03:11:46 +0000306 uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000307
308 static bool classof(const SymbolBody *S) {
309 return S->kind() == LazyArchiveKind;
310 }
311
Rui Ueyama434b5612016-07-17 03:11:46 +0000312 ArchiveFile *file() { return (ArchiveFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000313 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000314
315private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000316 const llvm::object::Archive::Symbol Sym;
317};
318
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000319// LazyObject symbols represents symbols in object files between
320// --start-lib and --end-lib options.
321class LazyObject : public Lazy {
322public:
Rui Ueyama434b5612016-07-17 03:11:46 +0000323 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000324
325 static bool classof(const SymbolBody *S) {
326 return S->kind() == LazyObjectKind;
327 }
328
Rui Ueyama434b5612016-07-17 03:11:46 +0000329 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
Rui Ueyama55518e72016-10-28 20:57:25 +0000330 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000331};
332
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000333// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000334// DefinedRegular symbols.
Rui Ueyama80474a22017-02-28 19:29:55 +0000335struct ElfSym {
George Rimar9e859392016-02-26 14:36:36 +0000336 // The content for _etext and etext symbols.
Rafael Espindola78493a22017-01-28 17:48:21 +0000337 static DefinedSynthetic *Etext;
338 static DefinedSynthetic *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000339
340 // The content for _edata and edata symbols.
Rafael Espindola78493a22017-01-28 17:48:21 +0000341 static DefinedSynthetic *Edata;
342 static DefinedSynthetic *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000343
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000344 // The content for _end and end symbols.
Rafael Espindola78493a22017-01-28 17:48:21 +0000345 static DefinedSynthetic *End;
346 static DefinedSynthetic *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000347
Simon Atanasyan8469b882016-11-23 22:22:16 +0000348 // The content for _gp_disp/__gnu_local_gp symbols for MIPS target.
Rui Ueyama80474a22017-02-28 19:29:55 +0000349 static DefinedRegular *MipsGpDisp;
350 static DefinedRegular *MipsLocalGp;
351 static DefinedRegular *MipsGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000352};
353
Peter Collingbourne4f952702016-05-01 04:55:03 +0000354// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
355// always one Symbol for each symbol name. The resolver updates the SymbolBody
356// stored in the Body field of this object as it resolves symbols. Symbol also
357// holds computed properties of symbol names.
358struct Symbol {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000359 // Symbol binding. This is on the Symbol to track changes during resolution.
360 // In particular:
361 // An undefined weak is still weak when it resolves to a shared library.
362 // An undefined weak will not fetch archive members, but we have to remember
363 // it is weak.
364 uint8_t Binding;
365
George Rimard3566302016-06-20 11:55:12 +0000366 // Version definition index.
367 uint16_t VersionId;
368
Peter Collingbourne4f952702016-05-01 04:55:03 +0000369 // Symbol visibility. This is the computed minimum visibility of all
370 // observed non-DSO symbols.
371 unsigned Visibility : 2;
372
373 // True if the symbol was used for linking and thus need to be added to the
374 // output file's symbol table. This is true for all symbols except for
375 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
376 // by other bitcode objects.
377 unsigned IsUsedInRegularObj : 1;
378
379 // If this flag is true and the symbol has protected or default visibility, it
380 // will appear in .dynsym. This flag is set by interposable DSO symbols in
381 // executables, by most symbols in DSOs and executables built with
382 // --export-dynamic, and by dynamic lists.
383 unsigned ExportDynamic : 1;
384
Rui Ueyama69c778c2016-07-17 17:50:09 +0000385 // True if this symbol is specified by --trace-symbol option.
386 unsigned Traced : 1;
387
Rafael Espindoladd9dd482016-12-09 22:40:49 +0000388 // This symbol version was found in a version script.
389 unsigned InVersionScript : 1;
390
Peter Collingbourne4f952702016-05-01 04:55:03 +0000391 bool includeInDynsym() const;
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000392 uint8_t computeBinding() const;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000393 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
394
395 // This field is used to store the Symbol's SymbolBody. This instantiation of
396 // AlignedCharArrayUnion gives us a struct with a char array field that is
Rui Ueyama80474a22017-02-28 19:29:55 +0000397 // large and aligned enough to store any derived class of SymbolBody.
398 llvm::AlignedCharArrayUnion<DefinedCommon, DefinedRegular, DefinedSynthetic,
399 Undefined, SharedSymbol, LazyArchive, LazyObject>
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000400 Body;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000401
402 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
403 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
404};
405
Rui Ueyama69c778c2016-07-17 17:50:09 +0000406void printTraceSymbol(Symbol *Sym);
407
Peter Collingbourne4f952702016-05-01 04:55:03 +0000408template <typename T, typename... ArgT>
409void replaceBody(Symbol *S, ArgT &&... Arg) {
410 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
Benjamin Kramer922b63b2016-10-20 15:55:41 +0000411 static_assert(alignof(T) <= alignof(decltype(S->Body)),
Peter Collingbourne4f952702016-05-01 04:55:03 +0000412 "Body not aligned enough");
Peter Collingbournef643fc12016-05-01 05:39:02 +0000413 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
414 "Not a SymbolBody");
Rui Ueyama69c778c2016-07-17 17:50:09 +0000415
Peter Collingbourne4f952702016-05-01 04:55:03 +0000416 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000417
418 // Print out a log message if --trace-symbol was specified.
419 // This is for debugging.
420 if (S->Traced)
421 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000422}
423
424inline Symbol *SymbolBody::symbol() {
425 assert(!isLocal());
426 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
427 offsetof(Symbol, Body));
428}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000429} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000430
431std::string toString(const elf::SymbolBody &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000432} // namespace lld
433
434#endif