blob: 3f71e6f89a48ed45a1a79eb305cb49d2f4a530af [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//
13// File-scope symbols in ELF objects are the only exception of SymbolBody
14// instantiation. We will never create SymbolBodies for them for performance
15// reason. They are often represented as nullptrs. This is fine for symbol
16// resolution because the symbol table naturally cares only about
17// externally-visible symbols. For relocations, you have to deal with both
18// local and non-local functions, and we have two different functions
19// where we need them.
20//
21//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23#ifndef LLD_ELF_SYMBOLS_H
24#define LLD_ELF_SYMBOLS_H
25
Rafael Espindola9d06ab62015-09-22 00:01:39 +000026#include "InputSection.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000027
Michael J. Spencer84487f12015-07-24 21:03:07 +000028#include "lld/Core/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000029#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000030#include "llvm/Object/ELF.h"
31
32namespace lld {
33namespace elf2 {
34
Michael J. Spencer1b348a62015-09-04 22:28:10 +000035class ArchiveFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036class InputFile;
37class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000038template <class ELFT> class ObjectFile;
Michael J. Spencer658dccd2015-09-18 22:13:25 +000039template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000040template <class ELFT> class OutputSectionBase;
Rui Ueyama35da9b62015-10-11 20:59:12 +000041template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000042
Rui Ueyama9ea49c72015-10-07 23:46:11 +000043// Initializes global objects defined in this file.
44// Called at the beginning of main().
45void initSymbols();
46
Michael J. Spencer84487f12015-07-24 21:03:07 +000047// A real symbol object, SymbolBody, is usually accessed indirectly
48// through a Symbol. There's always one Symbol for each symbol name.
49// The resolver updates SymbolBody pointers as it resolves symbols.
50struct Symbol {
51 explicit Symbol(SymbolBody *P) : Body(P) {}
52 SymbolBody *Body;
53};
54
55// The base class for real symbol classes.
56class SymbolBody {
57public:
58 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000059 DefinedFirst,
60 DefinedRegularKind = DefinedFirst,
61 DefinedAbsoluteKind,
62 DefinedCommonKind,
63 DefinedSyntheticKind,
64 SharedKind,
65 DefinedLast = SharedKind,
66 UndefinedKind,
67 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000068 };
69
Michael J. Spencercdae0a42015-07-28 22:58:25 +000070 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000071
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000072 bool isWeak() const { return IsWeak; }
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000073 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000074 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000075 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000076 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000077 bool isShared() const { return SymbolKind == SharedKind; }
78 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rafael Espindola05a3dd22015-09-22 23:38:23 +000079 bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; }
80 void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; }
Igor Kudrin65bddea2015-10-09 09:58:39 +000081 bool isTLS() const { return IsTLS; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000082
Michael J. Spencer84487f12015-07-24 21:03:07 +000083 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000084 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000085
Rui Ueyama8f2c4da2015-10-21 18:13:47 +000086 uint8_t getVisibility() const { return Visibility; }
Rafael Espindola78471f02015-09-01 23:12:52 +000087
Rafael Espindola19e38892015-09-16 15:54:15 +000088 unsigned getDynamicSymbolTableIndex() const {
89 return DynamicSymbolTableIndex;
90 }
91 void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; }
92
Rui Ueyama49c68a72015-10-09 00:42:06 +000093 uint32_t GotIndex = -1;
George Rimar648a2c32015-10-20 08:54:27 +000094 uint32_t GotPltIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000095 uint32_t PltIndex = -1;
Rafael Espindola5c2310c2015-09-18 14:40:19 +000096 bool isInGot() const { return GotIndex != -1U; }
George Rimar648a2c32015-10-20 08:54:27 +000097 bool isInGotPlt() const { return GotPltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000098 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000099
Michael J. Spencer84487f12015-07-24 21:03:07 +0000100 // A SymbolBody has a backreference to a Symbol. Originally they are
101 // doubly-linked. A backreference will never change. But the pointer
102 // in the Symbol may be mutated by the resolver. If you have a
103 // pointer P to a SymbolBody and are not sure whether the resolver
104 // has chosen the object among other objects having the same name,
105 // you can access P->Backref->Body to get the resolver's result.
106 void setBackref(Symbol *P) { Backref = P; }
Rui Ueyamae66e0012015-10-07 23:20:23 +0000107 SymbolBody *repl() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000108
109 // Decides which symbol should "win" in the symbol table, this or
110 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
111 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000112 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000113
114protected:
Igor Kudrin65bddea2015-10-09 09:58:39 +0000115 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
116 bool IsTLS)
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000117 : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTLS(IsTLS),
118 Name(Name) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000119 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000120 IsUsedInDynamicReloc = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000121 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000122
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000123 const unsigned SymbolKind : 8;
Rafael Espindola8614c562015-10-06 14:33:58 +0000124 unsigned IsWeak : 1;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000125 unsigned Visibility : 2;
Rafael Espindola18173d42015-09-08 15:50:05 +0000126 unsigned IsUsedInRegularObj : 1;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000127 unsigned IsUsedInDynamicReloc : 1;
Igor Kudrin65bddea2015-10-09 09:58:39 +0000128 unsigned IsTLS : 1;
Rafael Espindola19e38892015-09-16 15:54:15 +0000129 unsigned DynamicSymbolTableIndex = 0;
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000130 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000131 Symbol *Backref = nullptr;
132};
133
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000134// This is for symbols created from elf files and not from the command line.
135// Since they come from object files, they have a Elf_Sym.
136//
137// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
138// we have to delay creating the symbol table until the output format is
139// known and some of its methods will be templated. We should experiment with
140// that once we have a bit more code.
141template <class ELFT> class ELFSymbolBody : public SymbolBody {
142protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000143 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000144 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola78471f02015-09-01 23:12:52 +0000145 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK,
Igor Kudrin65bddea2015-10-09 09:58:39 +0000146 Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS),
Rafael Espindola78471f02015-09-01 23:12:52 +0000147 Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000148
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000149public:
150 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000151
152 static bool classof(const SymbolBody *S) {
153 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000154 return K >= DefinedFirst && K <= UndefinedKind;
155 }
156};
157
Rui Ueyama34f29242015-10-13 19:51:57 +0000158// The base class for any defined symbols, including absolute symbols, etc.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000159template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
160 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000161
162protected:
163 typedef typename Base::Kind Kind;
164 typedef typename Base::Elf_Sym Elf_Sym;
165
166public:
Rui Ueyama294b1362015-09-30 02:06:17 +0000167 Defined(Kind K, StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000168 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000169
170 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000171};
172
173template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
174 typedef ELFSymbolBody<ELFT> Base;
175 typedef typename Base::Elf_Sym Elf_Sym;
176
177public:
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000178 static Elf_Sym IgnoreUndef;
179
Igor Kudrinb044af52015-11-20 02:32:35 +0000180 // The following symbols must be added early to reserve their places
181 // in symbol tables. The value of the symbols are set when all sections
182 // are finalized and their addresses are determined.
183
184 // The content for _end and end symbols.
185 static Elf_Sym End;
186
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000187 // The content for _gp symbol for MIPS target.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000188 static Elf_Sym MipsGp;
189
Rui Ueyama294b1362015-09-30 02:06:17 +0000190 DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000191 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000192
193 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000194 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000195 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000196};
197
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000198template <class ELFT>
199typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef;
200
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000201template <class ELFT>
Igor Kudrinb044af52015-11-20 02:32:35 +0000202typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::End;
203
204template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000205typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::MipsGp;
206
Rafael Espindola51d46902015-08-28 21:26:51 +0000207template <class ELFT> class DefinedCommon : public Defined<ELFT> {
208 typedef ELFSymbolBody<ELFT> Base;
209 typedef typename Base::Elf_Sym Elf_Sym;
210
211public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000212 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama294b1362015-09-30 02:06:17 +0000213 DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000214 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
215 MaxAlignment = Sym.st_value;
216 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000217
218 static bool classof(const SymbolBody *S) {
219 return S->kind() == Base::DefinedCommonKind;
220 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000221
222 // The output offset of this common symbol in the output bss. Computed by the
223 // writer.
224 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000225
226 // The maximum alignment we have seen for this symbol.
227 uintX_t MaxAlignment;
Rafael Espindola51d46902015-08-28 21:26:51 +0000228};
229
Michael J. Spencer84487f12015-07-24 21:03:07 +0000230// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000231template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000232 typedef Defined<ELFT> Base;
233 typedef typename Base::Elf_Sym Elf_Sym;
234
Michael J. Spencer84487f12015-07-24 21:03:07 +0000235public:
Rafael Espindolac159c962015-10-19 21:00:02 +0000236 DefinedRegular(StringRef N, const Elf_Sym &Sym,
237 InputSectionBase<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000238 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000239
240 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000241 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000242 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000243
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000244 InputSectionBase<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000245};
246
Rafael Espindola0e604f92015-09-25 18:56:53 +0000247template <class ELFT> class DefinedSynthetic : public Defined<ELFT> {
248 typedef Defined<ELFT> Base;
249
250public:
251 typedef typename Base::Elf_Sym Elf_Sym;
Rui Ueyama294b1362015-09-30 02:06:17 +0000252 DefinedSynthetic(StringRef N, const Elf_Sym &Sym,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000253 OutputSectionBase<ELFT> &Section)
Rafael Espindola0e604f92015-09-25 18:56:53 +0000254 : Defined<ELFT>(Base::DefinedSyntheticKind, N, Sym), Section(Section) {}
255
256 static bool classof(const SymbolBody *S) {
257 return S->kind() == Base::DefinedSyntheticKind;
258 }
259
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000260 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000261};
262
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000263// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000264template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
265 typedef ELFSymbolBody<ELFT> Base;
266 typedef typename Base::Elf_Sym Elf_Sym;
267
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000268public:
Rui Ueyama833ce282015-10-08 00:29:00 +0000269 static Elf_Sym Required;
270 static Elf_Sym Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000271
Rui Ueyama294b1362015-09-30 02:06:17 +0000272 Undefined(StringRef N, const Elf_Sym &Sym)
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000273 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000274
275 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000276 return S->kind() == Base::UndefinedKind;
277 }
Denis Protivensky22220d52015-10-05 09:43:57 +0000278
Rui Ueyama833ce282015-10-08 00:29:00 +0000279 bool canKeepUndefined() const { return &this->Sym == &Optional; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000280};
281
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000282template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000283typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Required;
Denis Protivensky22220d52015-10-05 09:43:57 +0000284template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000285typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000286
Rafael Espindola38af1272015-09-25 15:34:03 +0000287template <class ELFT> class SharedSymbol : public Defined<ELFT> {
Rafael Espindola5b197f02015-09-25 18:32:09 +0000288 typedef Defined<ELFT> Base;
Rafael Espindola18173d42015-09-08 15:50:05 +0000289 typedef typename Base::Elf_Sym Elf_Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000290 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000291
292public:
293 static bool classof(const SymbolBody *S) {
294 return S->kind() == Base::SharedKind;
295 }
296
Rui Ueyama35da9b62015-10-11 20:59:12 +0000297 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
298 : Defined<ELFT>(Base::SharedKind, Name, Sym), File(F) {}
299
300 SharedFile<ELFT> *File;
George Rimarbc590fe2015-10-28 16:48:58 +0000301
302 // Can have offset if requires copy relocation.
Rafael Espindola9b896082015-11-03 14:34:11 +0000303 uintX_t OffsetInBSS = -1;
304 bool needsCopy() const { return OffsetInBSS != (uintX_t)-1; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000305};
306
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000307// This class represents a symbol defined in an archive file. It is
308// created from an archive file header, and it knows how to load an
309// object file from an archive to replace itself with a defined
310// symbol. If the resolver finds both Undefined and Lazy for
311// the same name, it will ask the Lazy to load a file.
312class Lazy : public SymbolBody {
313public:
314 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
Igor Kudrin65bddea2015-10-09 09:58:39 +0000315 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000316 File(F), Sym(S) {}
317
318 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
319
320 // Returns an object file for this symbol, or a nullptr if the file
321 // was already returned.
322 std::unique_ptr<InputFile> getMember();
323
Rafael Espindola8614c562015-10-06 14:33:58 +0000324 void setWeak() { IsWeak = true; }
325 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
326
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000327private:
328 ArchiveFile *File;
329 const llvm::object::Archive::Symbol Sym;
330};
331
Michael J. Spencer84487f12015-07-24 21:03:07 +0000332} // namespace elf2
333} // namespace lld
334
335#endif