blob: d234d97dbcf5ff7b9db8d2de47d587174593dce0 [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
Rui Ueyama3f851702017-10-02 21:00:41 +000021#include "lld/Common/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 Ueyamac0f56482017-10-13 03:37:11 +000065 bool isShared() const { return SymbolKind == SharedKind; }
66 bool isLocal() const { return IsLocal; }
67
Rui Ueyamaf8baa662016-04-07 19:24:51 +000068 bool isLazy() const {
69 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
70 }
Rui Ueyamac0f56482017-10-13 03:37:11 +000071
Rui Ueyamabda337a2017-10-27 22:54:16 +000072 bool isInCurrentOutput() const {
Rui Ueyamac0f56482017-10-13 03:37:11 +000073 return SymbolKind == DefinedRegularKind || SymbolKind == DefinedCommonKind;
George Rimard92e1282017-07-12 11:09:46 +000074 }
Rafael Espindola3d9f1c02017-09-13 20:43:04 +000075
76 // True is this is an undefined weak symbol. This only works once
77 // all input files have been added.
78 bool isUndefWeak() const;
79
Rafael Espindola6e93d052017-08-04 22:31:42 +000080 InputFile *getFile() const;
Rui Ueyamaa13efc22016-11-29 18:05:04 +000081 StringRef getName() const { return Name; }
Rui Ueyamab5792b22016-04-04 19:09:08 +000082 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +000083 void parseSymbolVersion();
Rui Ueyama5bbe4a42017-09-25 00:57:30 +000084 void copyFrom(SymbolBody *Other);
Rafael Espindola78471f02015-09-01 23:12:52 +000085
Rafael Espindola5c2310c2015-09-18 14:40:19 +000086 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000087 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000088
George Rimarf64618a2017-03-17 11:56:54 +000089 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000090
George Rimar4afe42e2017-03-17 14:12:51 +000091 uint64_t getGotOffset() const;
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +000092 uint64_t getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +000093 uint64_t getGotPltOffset() const;
94 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +000095 uint64_t getPltVA() const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000096 template <class ELFT> typename ELFT::uint getSize() const;
George Rimar69268a82017-03-16 11:06:13 +000097 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +000098
Rafael Espindola7ae21772016-10-26 00:58:23 +000099 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +0000100 uint32_t GotIndex = -1;
101 uint32_t GotPltIndex = -1;
102 uint32_t PltIndex = -1;
103 uint32_t GlobalDynIndex = -1;
104
Michael J. Spencer84487f12015-07-24 21:03:07 +0000105protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000106 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
Rui Ueyama7833afd2017-10-27 23:26:46 +0000107 uint8_t Type)
108 : SymbolKind(K), IsLocal(IsLocal), NeedsPltAddr(false),
109 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
110 IsInIgot(false), IsPreemptible(false), Type(Type), StOther(StOther),
111 Name(Name) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000112
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000113 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000114
Rui Ueyama662bb002017-10-13 03:37:26 +0000115 // True if this is a local symbol.
116 unsigned IsLocal : 1;
117
Rafael Espindolaabebed92016-02-05 15:27:15 +0000118public:
Rui Ueyama924b3612017-02-16 06:12:22 +0000119 // True the symbol should point to its PLT entry.
120 // For SharedSymbol only.
121 unsigned NeedsPltAddr : 1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000122 // True if this symbol has an entry in the global part of MIPS GOT.
123 unsigned IsInGlobalMipsGot : 1;
124
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000125 // True if this symbol is referenced by 32-bit GOT relocations.
126 unsigned Is32BitMipsGot : 1;
127
Peter Smithbaffdb82016-12-08 12:58:55 +0000128 // True if this symbol is in the Iplt sub-section of the Plt.
129 unsigned IsInIplt : 1;
130
131 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
132 unsigned IsInIgot : 1;
133
Rafael Espindola35c908f2017-08-10 15:05:37 +0000134 unsigned IsPreemptible : 1;
135
Rui Ueyamadd418072016-04-04 18:15:38 +0000136 // The following fields have the same meaning as the ELF symbol attributes.
137 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000138 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000139
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000140 // The Type field may also have this value. It means that we have not yet seen
141 // a non-Lazy symbol with this name, so we don't know what its type is. The
142 // Type field is normally set to this value for Lazy symbols unless we saw a
143 // weak undefined symbol first, in which case we need to remember the original
144 // symbol's type in order to check for TLS mismatches.
145 enum { UnknownType = 255 };
146
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000147 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
148 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
149 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
150 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
151 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
152 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000153
George Rimar2f0fab52016-03-06 06:26:18 +0000154protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000155 StringRefZ Name;
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:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000161 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type)
162 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
163
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000164 static bool classof(const SymbolBody *S) { return S->isDefined(); }
165};
166
Rafael Espindolae7553e42016-08-31 13:28:33 +0000167class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000168public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000169 DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
170 uint8_t StOther, uint8_t Type)
Rui Ueyamada524d02017-10-27 23:29:58 +0000171 : Defined(DefinedCommonKind, Name, /*IsLocal=*/false, StOther, Type),
Rui Ueyama7833afd2017-10-27 23:26:46 +0000172 Alignment(Alignment), Size(Size) {}
Rafael Espindola51d46902015-08-28 21:26:51 +0000173
174 static bool classof(const SymbolBody *S) {
Rui Ueyamada524d02017-10-27 23:29:58 +0000175 return S->kind() == DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000176 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000177
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000178 // The maximum alignment we have seen for this symbol.
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000179 uint32_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000180
Rui Ueyama9c77d272017-08-10 15:54:27 +0000181 // The output offset of this common symbol in the output bss.
182 // Computed by the writer.
Rafael Espindola11191912015-12-24 16:23:37 +0000183 uint64_t Size;
Dmitry Mikulin1e30f072017-09-08 16:22:43 +0000184 BssSection *Section = nullptr;
Rafael Espindola51d46902015-08-28 21:26:51 +0000185};
186
Michael J. Spencer84487f12015-07-24 21:03:07 +0000187// Regular defined symbols read from object file symbol tables.
Rui Ueyama80474a22017-02-28 19:29:55 +0000188class DefinedRegular : public Defined {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000189public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000190 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
Rafael Espindola6e93d052017-08-04 22:31:42 +0000191 uint64_t Value, uint64_t Size, SectionBase *Section)
Rui Ueyamada524d02017-10-27 23:29:58 +0000192 : Defined(DefinedRegularKind, Name, IsLocal, StOther, Type), Value(Value),
193 Size(Size), Section(Section) {}
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000194
Simon Atanasyanf967f092016-09-29 12:58:36 +0000195 // Return true if the symbol is a PIC function.
Rui Ueyama80474a22017-02-28 19:29:55 +0000196 template <class ELFT> bool isMipsPIC() const;
Simon Atanasyanf967f092016-09-29 12:58:36 +0000197
Michael J. Spencer84487f12015-07-24 21:03:07 +0000198 static bool classof(const SymbolBody *S) {
Rui Ueyamada524d02017-10-27 23:29:58 +0000199 return S->kind() == DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000200 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000201
Rui Ueyama80474a22017-02-28 19:29:55 +0000202 uint64_t Value;
203 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000204 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000205};
206
Peter Smith3a52eb02017-02-01 10:26:03 +0000207class Undefined : public SymbolBody {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000208public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000209 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type)
Rui Ueyamada524d02017-10-27 23:29:58 +0000210 : SymbolBody(UndefinedKind, Name, IsLocal, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000211
Rafael Espindolaf4765732016-04-06 13:22:41 +0000212 static bool classof(const SymbolBody *S) {
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000213 return S->kind() == UndefinedKind;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000214 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000215};
216
Rui Ueyama4076fa12017-02-26 23:35:34 +0000217class SharedSymbol : public Defined {
Rafael Espindola18173d42015-09-08 15:50:05 +0000218public:
Rui Ueyamada524d02017-10-27 23:29:58 +0000219 static bool classof(const SymbolBody *S) { return S->kind() == SharedKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000220
Rafael Espindola6e93d052017-08-04 22:31:42 +0000221 SharedSymbol(StringRef Name, uint8_t StOther, uint8_t Type,
Rui Ueyama4076fa12017-02-26 23:35:34 +0000222 const void *ElfSym, const void *Verdef)
Rui Ueyamada524d02017-10-27 23:29:58 +0000223 : Defined(SharedKind, Name, /*IsLocal=*/false, StOther, Type),
Rui Ueyama4076fa12017-02-26 23:35:34 +0000224 Verdef(Verdef), ElfSym(ElfSym) {
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000225 // GNU ifunc is a mechanism to allow user-supplied functions to
226 // resolve PLT slot values at load-time. This is contrary to the
227 // regualr symbol resolution scheme in which symbols are resolved just
228 // by name. Using this hook, you can program how symbols are solved
229 // for you program. For example, you can make "memcpy" to be resolved
230 // to a SSE-enabled version of memcpy only when a machine running the
231 // program supports the SSE instruction set.
232 //
233 // Naturally, such symbols should always be called through their PLT
234 // slots. What GNU ifunc symbols point to are resolver functions, and
235 // calling them directly doesn't make sense (unless you are writing a
236 // loader).
237 //
238 // For DSO symbols, we always call them through PLT slots anyway.
239 // So there's no difference between GNU ifunc and regular function
Shoaib Meenai14cf5142017-10-12 21:52:14 +0000240 // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000241 if (this->Type == llvm::ELF::STT_GNU_IFUNC)
George Rimar4a9cfc82017-07-11 11:40:59 +0000242 this->Type = llvm::ELF::STT_FUNC;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000243 }
244
245 template <class ELFT> SharedFile<ELFT> *getFile() const {
246 return cast<SharedFile<ELFT>>(SymbolBody::getFile());
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000247 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000248
Rui Ueyama4076fa12017-02-26 23:35:34 +0000249 template <class ELFT> uint64_t getShndx() const {
250 return getSym<ELFT>().st_shndx;
251 }
Rui Ueyama434b5612016-07-17 03:11:46 +0000252
Rui Ueyama4076fa12017-02-26 23:35:34 +0000253 template <class ELFT> uint64_t getValue() const {
254 return getSym<ELFT>().st_value;
255 }
256
257 template <class ELFT> uint64_t getSize() const {
258 return getSym<ELFT>().st_size;
259 }
260
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000261 template <class ELFT> uint32_t getAlignment() const;
George Rimarbc590fe2015-10-28 16:48:58 +0000262
George Rimard3566302016-06-20 11:55:12 +0000263 // This field is a pointer to the symbol's version definition.
Rui Ueyama4076fa12017-02-26 23:35:34 +0000264 const void *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000265
Rafael Espindolacb562312017-10-13 21:44:10 +0000266 // If not null, there is a copy relocation to this section.
267 InputSection *CopyRelSec = nullptr;
Rui Ueyama007c0022017-03-08 17:24:24 +0000268
Rui Ueyama4076fa12017-02-26 23:35:34 +0000269private:
270 template <class ELFT> const typename ELFT::Sym &getSym() const {
271 return *(const typename ELFT::Sym *)ElfSym;
272 }
273
274 const void *ElfSym;
Rafael Espindola18173d42015-09-08 15:50:05 +0000275};
276
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000277// This represents a symbol that is not yet in the link, but we know where to
278// find it if needed. If the resolver finds both Undefined and Lazy for the same
279// name, it will ask the Lazy to load a file.
280//
281// A special complication is the handling of weak undefined symbols. They should
282// not load a file, but we have to remember we have seen both the weak undefined
283// and the lazy. We represent that with a lazy symbol with a weak binding. This
284// means that code looking for undefined symbols normally also has to take lazy
285// symbols into consideration.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000286class Lazy : public SymbolBody {
287public:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000288 static bool classof(const SymbolBody *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000289
290 // Returns an object file for this symbol, or a nullptr if the file
291 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000292 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000293
294protected:
Rui Ueyamada524d02017-10-27 23:29:58 +0000295 Lazy(Kind K, StringRef Name, uint8_t Type)
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000296 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000297};
298
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000299// This class represents a symbol defined in an archive file. It is
300// created from an archive file header, and it knows how to load an
301// object file from an archive to replace itself with a defined
302// symbol.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000303class LazyArchive : public Lazy {
304public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000305 LazyArchive(const llvm::object::Archive::Symbol S, uint8_t Type)
306 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000307
308 static bool classof(const SymbolBody *S) {
309 return S->kind() == LazyArchiveKind;
310 }
311
Rafael Espindola6e93d052017-08-04 22:31:42 +0000312 ArchiveFile *getFile();
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 Ueyama7833afd2017-10-27 23:26:46 +0000323 LazyObject(StringRef Name, uint8_t Type) : Lazy(LazyObjectKind, Name, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000324
325 static bool classof(const SymbolBody *S) {
326 return S->kind() == LazyObjectKind;
327 }
328
Rafael Espindola6e93d052017-08-04 22:31:42 +0000329 LazyObjFile *getFile();
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 {
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000336 // __bss_start
George Rimare6c5d382017-04-05 10:03:25 +0000337 static DefinedRegular *Bss;
338
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000339 // etext and _etext
340 static DefinedRegular *Etext1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000341 static DefinedRegular *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000342
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000343 // edata and _edata
344 static DefinedRegular *Edata1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000345 static DefinedRegular *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000346
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000347 // end and _end
348 static DefinedRegular *End1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000349 static DefinedRegular *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000350
Rui Ueyama92c37812017-06-26 15:11:24 +0000351 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
352 // be at some offset from the base of the .got section, usually 0 or
353 // the end of the .got.
354 static DefinedRegular *GlobalOffsetTable;
355
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000356 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
357 static DefinedRegular *MipsGp;
Rui Ueyama80474a22017-02-28 19:29:55 +0000358 static DefinedRegular *MipsGpDisp;
359 static DefinedRegular *MipsLocalGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000360};
361
Peter Collingbourne4f952702016-05-01 04:55:03 +0000362// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
363// always one Symbol for each symbol name. The resolver updates the SymbolBody
364// stored in the Body field of this object as it resolves symbols. Symbol also
365// holds computed properties of symbol names.
366struct Symbol {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000367 // Symbol binding. This is on the Symbol to track changes during resolution.
368 // In particular:
369 // An undefined weak is still weak when it resolves to a shared library.
370 // An undefined weak will not fetch archive members, but we have to remember
371 // it is weak.
372 uint8_t Binding;
373
George Rimard3566302016-06-20 11:55:12 +0000374 // Version definition index.
375 uint16_t VersionId;
376
Peter Collingbourne4f952702016-05-01 04:55:03 +0000377 // Symbol visibility. This is the computed minimum visibility of all
378 // observed non-DSO symbols.
379 unsigned Visibility : 2;
380
381 // True if the symbol was used for linking and thus need to be added to the
382 // output file's symbol table. This is true for all symbols except for
383 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
384 // by other bitcode objects.
385 unsigned IsUsedInRegularObj : 1;
386
387 // If this flag is true and the symbol has protected or default visibility, it
388 // will appear in .dynsym. This flag is set by interposable DSO symbols in
389 // executables, by most symbols in DSOs and executables built with
390 // --export-dynamic, and by dynamic lists.
391 unsigned ExportDynamic : 1;
392
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000393 // False if LTO shouldn't inline whatever this symbol points to. If a symbol
394 // is overwritten after LTO, LTO shouldn't inline the symbol because it
395 // doesn't know the final contents of the symbol.
396 unsigned CanInline : 1;
397
Rui Ueyama69c778c2016-07-17 17:50:09 +0000398 // True if this symbol is specified by --trace-symbol option.
399 unsigned Traced : 1;
400
Rafael Espindolad3fc0c92017-07-12 17:49:17 +0000401 // This symbol version was found in a version script.
402 unsigned InVersionScript : 1;
403
Rafael Espindola6e93d052017-08-04 22:31:42 +0000404 // The file from which this symbol was created.
405 InputFile *File = nullptr;
406
Peter Collingbourne4f952702016-05-01 04:55:03 +0000407 bool includeInDynsym() const;
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000408 uint8_t computeBinding() const;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000409 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
410
411 // This field is used to store the Symbol's SymbolBody. This instantiation of
412 // AlignedCharArrayUnion gives us a struct with a char array field that is
Rui Ueyama80474a22017-02-28 19:29:55 +0000413 // large and aligned enough to store any derived class of SymbolBody.
Rafael Espindola5616adf2017-03-08 22:36:28 +0000414 llvm::AlignedCharArrayUnion<DefinedCommon, DefinedRegular, Undefined,
415 SharedSymbol, LazyArchive, LazyObject>
Rui Ueyama4f2f50d2016-12-21 08:40:09 +0000416 Body;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000417
418 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
419 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
420};
421
Rui Ueyama69c778c2016-07-17 17:50:09 +0000422void printTraceSymbol(Symbol *Sym);
423
Peter Collingbourne4f952702016-05-01 04:55:03 +0000424template <typename T, typename... ArgT>
Rafael Espindola6e93d052017-08-04 22:31:42 +0000425void replaceBody(Symbol *S, InputFile *File, ArgT &&... Arg) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000426 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
Benjamin Kramer922b63b2016-10-20 15:55:41 +0000427 static_assert(alignof(T) <= alignof(decltype(S->Body)),
Peter Collingbourne4f952702016-05-01 04:55:03 +0000428 "Body not aligned enough");
Peter Collingbournef643fc12016-05-01 05:39:02 +0000429 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&
430 "Not a SymbolBody");
Rafael Espindola6e93d052017-08-04 22:31:42 +0000431 S->File = File;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000432 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000433
434 // Print out a log message if --trace-symbol was specified.
435 // This is for debugging.
436 if (S->Traced)
437 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000438}
439
440inline Symbol *SymbolBody::symbol() {
441 assert(!isLocal());
442 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
443 offsetof(Symbol, Body));
444}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000445} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000446
447std::string toString(const elf::SymbolBody &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000448} // namespace lld
449
450#endif