blob: 2b6908dcebdccdacbc1ed6b1ab73fa7de9b0e0ec [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:54 +00001//===- Symbols.h ------------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// All symbols are handled as SymbolBodies regardless of their types.
11// This file defines various types of SymbolBodies.
12//
Rui Ueyama34f29242015-10-13 19:51:57 +000013//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000014
15#ifndef LLD_ELF_SYMBOLS_H
16#define LLD_ELF_SYMBOLS_H
17
Rafael Espindola9d06ab62015-09-22 00:01:39 +000018#include "InputSection.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000019
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "lld/Core/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000021#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022#include "llvm/Object/ELF.h"
Peter Collingbourne4f952702016-05-01 04:55:03 +000023#include "llvm/Support/AlignOf.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
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;
31class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000032template <class ELFT> class ObjectFile;
Michael J. Spencer658dccd2015-09-18 22:13:25 +000033template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000034template <class ELFT> class OutputSectionBase;
Rui Ueyama35da9b62015-10-11 20:59:12 +000035template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036
Rui Ueyamaa4a628f2016-01-13 18:55:39 +000037// Returns a demangled C++ symbol name. If Name is not a mangled
38// name or the system does not provide __cxa_demangle function,
39// it returns the unmodified string.
40std::string demangle(StringRef Name);
41
Peter Collingbourne4f952702016-05-01 04:55:03 +000042struct Symbol;
Michael J. Spencer84487f12015-07-24 21:03:07 +000043
44// The base class for real symbol classes.
45class SymbolBody {
46public:
47 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000048 DefinedFirst,
49 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000050 SharedKind,
Rafael Espindola11191912015-12-24 16:23:37 +000051 DefinedCommonKind,
Rafael Espindola9f77ef02016-02-12 20:54:57 +000052 DefinedBitcodeKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000053 DefinedSyntheticKind,
54 DefinedLast = DefinedSyntheticKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000055 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000056 LazyArchiveKind,
57 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000058 };
59
Peter Collingbourne4f952702016-05-01 04:55:03 +000060 Symbol *symbol();
61 const Symbol *symbol() const {
62 return const_cast<SymbolBody *>(this)->symbol();
63 }
64
Michael J. Spencercdae0a42015-07-28 22:58:25 +000065 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000066
Peter Collingbourne60976ed2016-04-27 00:05:06 +000067 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000068 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000069 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +000070 bool isLazy() const {
71 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
72 }
Rafael Espindola18173d42015-09-08 15:50:05 +000073 bool isShared() const { return SymbolKind == SharedKind; }
Peter Collingbourne4f952702016-05-01 04:55:03 +000074 bool isLocal() const { return IsLocal; }
Rui Ueyamac4466602016-03-13 19:48:18 +000075 bool isPreemptible() const;
Rui Ueyama7ede5432016-03-13 04:40:14 +000076
Michael J. Spencer84487f12015-07-24 21:03:07 +000077 // Returns the symbol name.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000078 StringRef getName() const {
79 assert(!isLocal());
Rafael Espindola193b99c2016-04-04 14:31:20 +000080 return StringRef(Name.S, Name.Len);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000081 }
82 uint32_t getNameOffset() const {
83 assert(isLocal());
84 return NameOffset;
85 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000086
Rui Ueyamab5792b22016-04-04 19:09:08 +000087 uint8_t getVisibility() const { return StOther & 0x3; }
Rafael Espindola78471f02015-09-01 23:12:52 +000088
Rui Ueyama572a6f72016-01-29 01:49:33 +000089 unsigned DynsymIndex = 0;
Rui Ueyama49c68a72015-10-09 00:42:06 +000090 uint32_t GotIndex = -1;
George Rimar648a2c32015-10-20 08:54:27 +000091 uint32_t GotPltIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000092 uint32_t PltIndex = -1;
Simon Atanasyan13f6da12016-03-31 21:26:23 +000093 uint32_t ThunkIndex = -1;
Rafael Espindola6211d9a2016-06-05 19:03:28 +000094 uint32_t GlobalDynIndex = -1;
Rafael Espindola5c2310c2015-09-18 14:40:19 +000095 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000096 bool isInPlt() const { return PltIndex != -1U; }
Simon Atanasyan13f6da12016-03-31 21:26:23 +000097 bool hasThunk() const { return ThunkIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000098
Rui Ueyama71a86862016-03-14 19:37:58 +000099 template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000100 typename ELFT::uint getVA(typename ELFT::uint Addend = 0) const;
101
Rafael Espindola74031ba2016-04-07 15:20:56 +0000102 template <class ELFT> typename ELFT::uint getGotOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000103 template <class ELFT> typename ELFT::uint getGotVA() const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000104 template <class ELFT> typename ELFT::uint getGotPltOffset() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000105 template <class ELFT> typename ELFT::uint getGotPltVA() const;
106 template <class ELFT> typename ELFT::uint getPltVA() const;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000107 template <class ELFT> typename ELFT::uint getThunkVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000108 template <class ELFT> typename ELFT::uint getSize() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000109
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000110 // Returns the file from which the symbol was created.
111 // For logging purpose only.
112 template <class ELFT> InputFile *getSourceFile();
113
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114protected:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000115 SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000116
Rui Ueyamab5792b22016-04-04 19:09:08 +0000117 SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000118
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000119 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000120
Rafael Espindolaabebed92016-02-05 15:27:15 +0000121public:
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000122 // True if the linker has to generate a copy relocation for this shared
123 // symbol or if the symbol should point to its plt entry.
124 unsigned NeedsCopyOrPltAddr : 1;
125
Peter Collingbourne4f952702016-05-01 04:55:03 +0000126 // True if this is a local symbol.
127 unsigned IsLocal : 1;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000128
Rui Ueyamadd418072016-04-04 18:15:38 +0000129 // The following fields have the same meaning as the ELF symbol attributes.
130 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000131 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000132
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000133 // The Type field may also have this value. It means that we have not yet seen
134 // a non-Lazy symbol with this name, so we don't know what its type is. The
135 // Type field is normally set to this value for Lazy symbols unless we saw a
136 // weak undefined symbol first, in which case we need to remember the original
137 // symbol's type in order to check for TLS mismatches.
138 enum { UnknownType = 255 };
139
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000140 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
141 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
142 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
143 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
144 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
145 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000146
George Rimar2f0fab52016-03-06 06:26:18 +0000147protected:
Rafael Espindola193b99c2016-04-04 14:31:20 +0000148 struct Str {
149 const char *S;
150 size_t Len;
151 };
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000152 union {
Rafael Espindola193b99c2016-04-04 14:31:20 +0000153 Str Name;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000154 uint32_t NameOffset;
155 };
Michael J. Spencer84487f12015-07-24 21:03:07 +0000156};
157
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000158// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000159class Defined : public SymbolBody {
160public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000161 Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type);
Rui Ueyamab5792b22016-04-04 19:09:08 +0000162 Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000163 static bool classof(const SymbolBody *S) { return S->isDefined(); }
164};
165
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000166// The defined symbol in LLVM bitcode files.
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000167class DefinedBitcode : public Defined {
168public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000169 DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type, BitcodeFile *F);
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000170 static bool classof(const SymbolBody *S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000171
172 BitcodeFile *File;
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000173};
174
Rafael Espindola11191912015-12-24 16:23:37 +0000175class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000176public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000177 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther,
178 uint8_t Type);
Rafael Espindola51d46902015-08-28 21:26:51 +0000179
180 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000181 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000182 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000183
184 // The output offset of this common symbol in the output bss. Computed by the
185 // writer.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000186 uint64_t OffsetInBss;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000187
188 // The maximum alignment we have seen for this symbol.
Rui Ueyama17d69832016-03-10 18:58:53 +0000189 uint64_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000190
191 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000192};
193
Michael J. Spencer84487f12015-07-24 21:03:07 +0000194// Regular defined symbols read from object file symbol tables.
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000195template <class ELFT> class DefinedRegular : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000196 typedef typename ELFT::Sym Elf_Sym;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000197 typedef typename ELFT::uint uintX_t;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000198
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199public:
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000200 DefinedRegular(StringRef Name, const Elf_Sym &Sym,
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000201 InputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000202 : Defined(SymbolBody::DefinedRegularKind, Name, Sym.st_other,
203 Sym.getType()),
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000204 Value(Sym.st_value), Size(Sym.st_size),
205 Section(Section ? Section->Repl : NullInputSection) {}
206
Rafael Espindolad9a17172016-04-05 11:47:46 +0000207 DefinedRegular(const Elf_Sym &Sym, InputSectionBase<ELFT> *Section)
208 : Defined(SymbolBody::DefinedRegularKind, Sym.st_name, Sym.st_other,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000209 Sym.getType()),
210 Value(Sym.st_value), Size(Sym.st_size),
211 Section(Section ? Section->Repl : NullInputSection) {
212 assert(isLocal());
213 }
214
Peter Collingbourne4f952702016-05-01 04:55:03 +0000215 DefinedRegular(StringRef Name, uint8_t StOther)
216 : Defined(SymbolBody::DefinedRegularKind, Name, StOther,
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000217 llvm::ELF::STT_NOTYPE),
218 Value(0), Size(0), Section(NullInputSection) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000219
220 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000221 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000222 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000223
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000224 uintX_t Value;
225 uintX_t Size;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000226
Rui Ueyama0b289522016-02-25 18:43:51 +0000227 // The input section this symbol belongs to. Notice that this is
228 // a reference to a pointer. We are using two levels of indirections
229 // because of ICF. If ICF decides two sections need to be merged, it
230 // manipulates this Section pointers so that they point to the same
231 // section. This is a bit tricky, so be careful to not be confused.
232 // If this is null, the symbol is an absolute symbol.
233 InputSectionBase<ELFT> *&Section;
234
235private:
236 static InputSectionBase<ELFT> *NullInputSection;
Rafael Espindolab13df652015-08-11 17:33:02 +0000237};
238
Rui Ueyama0b289522016-02-25 18:43:51 +0000239template <class ELFT>
240InputSectionBase<ELFT> *DefinedRegular<ELFT>::NullInputSection;
241
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000242// DefinedSynthetic is a class to represent linker-generated ELF symbols.
243// The difference from the regular symbol is that DefinedSynthetic symbols
244// don't belong to any input files or sections. Thus, its constructor
245// takes an output section to calculate output VA, etc.
Peter Collingbourne6a422592016-05-03 01:21:08 +0000246// If Section is null, this symbol is relative to the image base.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000247template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000248public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000249 typedef typename ELFT::uint uintX_t;
Peter Collingbournef6e9b4e2016-04-13 16:57:28 +0000250 DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000251 OutputSectionBase<ELFT> *Section);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000252
253 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000254 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000255 }
256
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000257 // Special value designates that the symbol 'points'
258 // to the end of the section.
259 static const uintX_t SectionEnd = uintX_t(-1);
260
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000261 uintX_t Value;
Peter Collingbourne6a422592016-05-03 01:21:08 +0000262 const OutputSectionBase<ELFT> *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000263};
264
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000265class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000266public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000267 Undefined(StringRef Name, uint8_t StOther, uint8_t Type);
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000268 Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000269
Rafael Espindolaf4765732016-04-06 13:22:41 +0000270 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000271 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000272 }
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000273
274 // The file this undefined symbol was created from.
275 // For logging purpose only.
276 InputFile *File = nullptr;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000277};
278
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000279template <class ELFT> class SharedSymbol : public Defined {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000280 typedef typename ELFT::Sym Elf_Sym;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000281 typedef typename ELFT::Verdef Elf_Verdef;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000282 typedef typename ELFT::uint uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000283
284public:
285 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000286 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000287 }
288
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000289 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
290 const Elf_Verdef *Verdef)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000291 : Defined(SymbolBody::SharedKind, Name, Sym.st_other, Sym.getType()),
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000292 File(F), Sym(Sym), Verdef(Verdef) {
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000293 // IFuncs defined in DSOs are treated as functions by the static linker.
294 if (isGnuIFunc())
295 Type = llvm::ELF::STT_FUNC;
296 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000297
298 SharedFile<ELFT> *File;
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000299 const Elf_Sym &Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000300
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000301 // This field is initially a pointer to the symbol's version definition. As
302 // symbols are added to the version table, this field is replaced with the
303 // version identifier to be stored in .gnu.version in the output file.
304 union {
305 const Elf_Verdef *Verdef;
306 uint16_t VersionId;
307 };
308
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000309 // OffsetInBss is significant only when needsCopy() is true.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000310 uintX_t OffsetInBss = 0;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000311
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000312 bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
Rafael Espindola18173d42015-09-08 15:50:05 +0000313};
314
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000315// This class represents a symbol defined in an archive file. It is
316// created from an archive file header, and it knows how to load an
317// object file from an archive to replace itself with a defined
318// symbol. If the resolver finds both Undefined and Lazy for
319// the same name, it will ask the Lazy to load a file.
320class Lazy : public SymbolBody {
321public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000322 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
323 : SymbolBody(K, Name, llvm::ELF::STV_DEFAULT, Type) {}
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000324
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000325 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000326
327 // Returns an object file for this symbol, or a nullptr if the file
328 // was already returned.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000329 std::unique_ptr<InputFile> getFile();
330};
331
332// LazyArchive symbols represents symbols in archive files.
333class LazyArchive : public Lazy {
334public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000335 LazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S,
336 uint8_t Type)
337 : Lazy(LazyArchiveKind, S.getName(), Type), File(F), Sym(S) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000338
339 static bool classof(const SymbolBody *S) {
340 return S->kind() == LazyArchiveKind;
341 }
342
343 std::unique_ptr<InputFile> getFile();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000344
345private:
346 ArchiveFile *File;
347 const llvm::object::Archive::Symbol Sym;
348};
349
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000350// LazyObject symbols represents symbols in object files between
351// --start-lib and --end-lib options.
352class LazyObject : public Lazy {
353public:
Peter Collingbourne4f952702016-05-01 04:55:03 +0000354 LazyObject(StringRef Name, MemoryBufferRef M, uint8_t Type)
355 : Lazy(LazyObjectKind, Name, Type), MBRef(M) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000356
357 static bool classof(const SymbolBody *S) {
358 return S->kind() == LazyObjectKind;
359 }
360
361 std::unique_ptr<InputFile> getFile();
362
363private:
364 MemoryBufferRef MBRef;
365};
366
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000367// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000368// DefinedRegular symbols.
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000369template <class ELFT> struct ElfSym {
George Rimar9e859392016-02-26 14:36:36 +0000370 // The content for _etext and etext symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000371 static DefinedRegular<ELFT> *Etext;
372 static DefinedRegular<ELFT> *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000373
374 // The content for _edata and edata symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000375 static DefinedRegular<ELFT> *Edata;
376 static DefinedRegular<ELFT> *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000377
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000378 // The content for _end and end symbols.
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000379 static DefinedRegular<ELFT> *End;
380 static DefinedRegular<ELFT> *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000381
Peter Collingbourne4f952702016-05-01 04:55:03 +0000382 // The content for _gp_disp symbol for MIPS target.
Rafael Espindola6f92e142016-04-12 13:26:51 +0000383 static SymbolBody *MipsGpDisp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000384};
385
Rui Ueyama467dbdd2016-04-21 20:50:15 +0000386template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
387template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
388template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
389template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata2;
390template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End;
391template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::End2;
Rafael Espindola6f92e142016-04-12 13:26:51 +0000392template <class ELFT> SymbolBody *ElfSym<ELFT>::MipsGpDisp;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000393
394// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
395// always one Symbol for each symbol name. The resolver updates the SymbolBody
396// stored in the Body field of this object as it resolves symbols. Symbol also
397// holds computed properties of symbol names.
398struct Symbol {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000399 // Symbol binding. This is on the Symbol to track changes during resolution.
400 // In particular:
401 // An undefined weak is still weak when it resolves to a shared library.
402 // An undefined weak will not fetch archive members, but we have to remember
403 // it is weak.
404 uint8_t Binding;
405
406 // Symbol visibility. This is the computed minimum visibility of all
407 // observed non-DSO symbols.
408 unsigned Visibility : 2;
409
410 // True if the symbol was used for linking and thus need to be added to the
411 // output file's symbol table. This is true for all symbols except for
412 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
413 // by other bitcode objects.
414 unsigned IsUsedInRegularObj : 1;
415
416 // If this flag is true and the symbol has protected or default visibility, it
417 // will appear in .dynsym. This flag is set by interposable DSO symbols in
418 // executables, by most symbols in DSOs and executables built with
419 // --export-dynamic, and by dynamic lists.
420 unsigned ExportDynamic : 1;
421
422 // This flag acts as an additional filter on the dynamic symbol list. It is
423 // set if there is no version script, or if the symbol appears in the global
424 // section of the version script.
425 unsigned VersionScriptGlobal : 1;
426
427 bool includeInDynsym() const;
428 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
429
430 // This field is used to store the Symbol's SymbolBody. This instantiation of
431 // AlignedCharArrayUnion gives us a struct with a char array field that is
432 // large and aligned enough to store any derived class of SymbolBody. We
433 // assume that the size and alignment of ELF64LE symbols is sufficient for any
434 // ELFT, and we verify this with the static_asserts in replaceBody.
435 llvm::AlignedCharArrayUnion<
436 DefinedBitcode, DefinedCommon, DefinedRegular<llvm::object::ELF64LE>,
437 DefinedSynthetic<llvm::object::ELF64LE>, Undefined,
438 SharedSymbol<llvm::object::ELF64LE>, LazyArchive, LazyObject>
439 Body;
440
441 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
442 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
443};
444
445template <typename T, typename... ArgT>
446void replaceBody(Symbol *S, ArgT &&... Arg) {
447 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
Peter Collingbourne6e862bb2016-05-01 05:12:13 +0000448 static_assert(llvm::AlignOf<T>::Alignment <=
449 llvm::AlignOf<decltype(S->Body)>::Alignment,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000450 "Body not aligned enough");
Peter Collingbournef643fc12016-05-01 05:39:02 +0000451 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
452 "Not a SymbolBody");
Peter Collingbourne4f952702016-05-01 04:55:03 +0000453 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
454}
455
456inline Symbol *SymbolBody::symbol() {
457 assert(!isLocal());
458 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
459 offsetof(Symbol, Body));
460}
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000461
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000462} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000463} // namespace lld
464
465#endif