blob: 98533cf3cbe6cb51048550061a6443981a1b9a4c [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
Rui Ueyama294b1362015-09-30 02:06:17 +0000180 DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000181 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000182
183 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000184 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000185 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000186};
187
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000188template <class ELFT>
189typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef;
190
Rafael Espindola51d46902015-08-28 21:26:51 +0000191template <class ELFT> class DefinedCommon : public Defined<ELFT> {
192 typedef ELFSymbolBody<ELFT> Base;
193 typedef typename Base::Elf_Sym Elf_Sym;
194
195public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000196 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama294b1362015-09-30 02:06:17 +0000197 DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000198 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
199 MaxAlignment = Sym.st_value;
200 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000201
202 static bool classof(const SymbolBody *S) {
203 return S->kind() == Base::DefinedCommonKind;
204 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000205
206 // The output offset of this common symbol in the output bss. Computed by the
207 // writer.
208 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000209
210 // The maximum alignment we have seen for this symbol.
211 uintX_t MaxAlignment;
Rafael Espindola51d46902015-08-28 21:26:51 +0000212};
213
Michael J. Spencer84487f12015-07-24 21:03:07 +0000214// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000215template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000216 typedef Defined<ELFT> Base;
217 typedef typename Base::Elf_Sym Elf_Sym;
218
Michael J. Spencer84487f12015-07-24 21:03:07 +0000219public:
Rafael Espindolac159c962015-10-19 21:00:02 +0000220 DefinedRegular(StringRef N, const Elf_Sym &Sym,
221 InputSectionBase<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000222 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000223
224 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000225 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000226 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000227
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000228 InputSectionBase<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000229};
230
Rafael Espindola0e604f92015-09-25 18:56:53 +0000231template <class ELFT> class DefinedSynthetic : public Defined<ELFT> {
232 typedef Defined<ELFT> Base;
233
234public:
235 typedef typename Base::Elf_Sym Elf_Sym;
Rui Ueyama294b1362015-09-30 02:06:17 +0000236 DefinedSynthetic(StringRef N, const Elf_Sym &Sym,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000237 OutputSectionBase<ELFT> &Section)
Rafael Espindola0e604f92015-09-25 18:56:53 +0000238 : Defined<ELFT>(Base::DefinedSyntheticKind, N, Sym), Section(Section) {}
239
240 static bool classof(const SymbolBody *S) {
241 return S->kind() == Base::DefinedSyntheticKind;
242 }
243
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000244 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000245};
246
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000247// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000248template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
249 typedef ELFSymbolBody<ELFT> Base;
250 typedef typename Base::Elf_Sym Elf_Sym;
251
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000252public:
Rui Ueyama833ce282015-10-08 00:29:00 +0000253 static Elf_Sym Required;
254 static Elf_Sym Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000255
Rui Ueyama294b1362015-09-30 02:06:17 +0000256 Undefined(StringRef N, const Elf_Sym &Sym)
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000257 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000258
259 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000260 return S->kind() == Base::UndefinedKind;
261 }
Denis Protivensky22220d52015-10-05 09:43:57 +0000262
Rui Ueyama833ce282015-10-08 00:29:00 +0000263 bool canKeepUndefined() const { return &this->Sym == &Optional; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000264};
265
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000266template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000267typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Required;
Denis Protivensky22220d52015-10-05 09:43:57 +0000268template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000269typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000270
Rafael Espindola38af1272015-09-25 15:34:03 +0000271template <class ELFT> class SharedSymbol : public Defined<ELFT> {
Rafael Espindola5b197f02015-09-25 18:32:09 +0000272 typedef Defined<ELFT> Base;
Rafael Espindola18173d42015-09-08 15:50:05 +0000273 typedef typename Base::Elf_Sym Elf_Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000274 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000275
276public:
277 static bool classof(const SymbolBody *S) {
278 return S->kind() == Base::SharedKind;
279 }
280
Rui Ueyama35da9b62015-10-11 20:59:12 +0000281 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
282 : Defined<ELFT>(Base::SharedKind, Name, Sym), File(F) {}
283
284 SharedFile<ELFT> *File;
George Rimarbc590fe2015-10-28 16:48:58 +0000285
286 // Can have offset if requires copy relocation.
Rafael Espindola9b896082015-11-03 14:34:11 +0000287 uintX_t OffsetInBSS = -1;
288 bool needsCopy() const { return OffsetInBSS != (uintX_t)-1; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000289};
290
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000291// This class represents a symbol defined in an archive file. It is
292// created from an archive file header, and it knows how to load an
293// object file from an archive to replace itself with a defined
294// symbol. If the resolver finds both Undefined and Lazy for
295// the same name, it will ask the Lazy to load a file.
296class Lazy : public SymbolBody {
297public:
298 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
Igor Kudrin65bddea2015-10-09 09:58:39 +0000299 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000300 File(F), Sym(S) {}
301
302 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
303
304 // Returns an object file for this symbol, or a nullptr if the file
305 // was already returned.
306 std::unique_ptr<InputFile> getMember();
307
Rafael Espindola8614c562015-10-06 14:33:58 +0000308 void setWeak() { IsWeak = true; }
309 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
310
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000311private:
312 ArchiveFile *File;
313 const llvm::object::Archive::Symbol Sym;
314};
315
Michael J. Spencer84487f12015-07-24 21:03:07 +0000316} // namespace elf2
317} // namespace lld
318
319#endif