blob: 1d59b73e6bf70c5a6a7b91b1709e948e1787ece4 [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 {
Michael J. Spencer84487f12015-07-24 21:03:07 +000051 SymbolBody *Body;
52};
53
54// The base class for real symbol classes.
55class SymbolBody {
56public:
57 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000058 DefinedFirst,
59 DefinedRegularKind = DefinedFirst,
60 DefinedAbsoluteKind,
61 DefinedCommonKind,
62 DefinedSyntheticKind,
63 SharedKind,
64 DefinedLast = SharedKind,
65 UndefinedKind,
66 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000067 };
68
Michael J. Spencercdae0a42015-07-28 22:58:25 +000069 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000070
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000071 bool isWeak() const { return IsWeak; }
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000072 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000073 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000074 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000075 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000076 bool isShared() const { return SymbolKind == SharedKind; }
77 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rafael Espindola05a3dd22015-09-22 23:38:23 +000078 bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; }
79 void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; }
Rui Ueyama61805ec2015-12-17 00:12:03 +000080 bool isTls() const { return IsTls; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000081
Michael J. Spencer84487f12015-07-24 21:03:07 +000082 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000083 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000084
Rui Ueyama8f2c4da2015-10-21 18:13:47 +000085 uint8_t getVisibility() const { return Visibility; }
Rafael Espindola78471f02015-09-01 23:12:52 +000086
Rafael Espindola19e38892015-09-16 15:54:15 +000087 unsigned getDynamicSymbolTableIndex() const {
88 return DynamicSymbolTableIndex;
89 }
90 void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; }
91
George Rimar90cd0a82015-12-01 19:20:26 +000092 uint32_t GlobalDynIndex = -1;
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;
George Rimar90cd0a82015-12-01 19:20:26 +000096 bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
Rafael Espindola5c2310c2015-09-18 14:40:19 +000097 bool isInGot() const { return GotIndex != -1U; }
George Rimar648a2c32015-10-20 08:54:27 +000098 bool isInGotPlt() const { return GotPltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000099 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000100
Michael J. Spencer84487f12015-07-24 21:03:07 +0000101 // A SymbolBody has a backreference to a Symbol. Originally they are
102 // doubly-linked. A backreference will never change. But the pointer
103 // in the Symbol may be mutated by the resolver. If you have a
104 // pointer P to a SymbolBody and are not sure whether the resolver
105 // has chosen the object among other objects having the same name,
106 // you can access P->Backref->Body to get the resolver's result.
107 void setBackref(Symbol *P) { Backref = P; }
Rui Ueyamae66e0012015-10-07 23:20:23 +0000108 SymbolBody *repl() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000109
110 // Decides which symbol should "win" in the symbol table, this or
111 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
112 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000113 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114
115protected:
Igor Kudrin65bddea2015-10-09 09:58:39 +0000116 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Rui Ueyama61805ec2015-12-17 00:12:03 +0000117 bool IsTls)
118 : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTls(IsTls),
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000119 Name(Name) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000120 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000121 IsUsedInDynamicReloc = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000122 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000123
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000124 const unsigned SymbolKind : 8;
Rafael Espindola8614c562015-10-06 14:33:58 +0000125 unsigned IsWeak : 1;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000126 unsigned Visibility : 2;
Rafael Espindola18173d42015-09-08 15:50:05 +0000127 unsigned IsUsedInRegularObj : 1;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000128 unsigned IsUsedInDynamicReloc : 1;
Rui Ueyama61805ec2015-12-17 00:12:03 +0000129 unsigned IsTls : 1;
Rafael Espindola19e38892015-09-16 15:54:15 +0000130 unsigned DynamicSymbolTableIndex = 0;
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000131 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000132 Symbol *Backref = nullptr;
133};
134
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000135// This is for symbols created from elf files and not from the command line.
136// Since they come from object files, they have a Elf_Sym.
137//
138// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
139// we have to delay creating the symbol table until the output format is
140// known and some of its methods will be templated. We should experiment with
141// that once we have a bit more code.
142template <class ELFT> class ELFSymbolBody : public SymbolBody {
143protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000144 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000145 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola78471f02015-09-01 23:12:52 +0000146 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK,
Igor Kudrin65bddea2015-10-09 09:58:39 +0000147 Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS),
Rafael Espindola78471f02015-09-01 23:12:52 +0000148 Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000149
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000150public:
151 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000152
153 static bool classof(const SymbolBody *S) {
154 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000155 return K >= DefinedFirst && K <= UndefinedKind;
156 }
157};
158
Rui Ueyama34f29242015-10-13 19:51:57 +0000159// The base class for any defined symbols, including absolute symbols, etc.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000160template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
161 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000162
163protected:
164 typedef typename Base::Kind Kind;
165 typedef typename Base::Elf_Sym Elf_Sym;
166
167public:
Rui Ueyama294b1362015-09-30 02:06:17 +0000168 Defined(Kind K, StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000169 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000170
171 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000172};
173
174template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
175 typedef ELFSymbolBody<ELFT> Base;
176 typedef typename Base::Elf_Sym Elf_Sym;
177
178public:
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000179 static Elf_Sym IgnoreUndef;
180
Igor Kudrinb044af52015-11-20 02:32:35 +0000181 // The following symbols must be added early to reserve their places
182 // in symbol tables. The value of the symbols are set when all sections
183 // are finalized and their addresses are determined.
184
185 // The content for _end and end symbols.
186 static Elf_Sym End;
187
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000188 // The content for _gp symbol for MIPS target.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000189 static Elf_Sym MipsGp;
190
Rui Ueyama294b1362015-09-30 02:06:17 +0000191 DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000192 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000193
194 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000195 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000196 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000197};
198
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000199template <class ELFT>
200typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef;
201
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000202template <class ELFT>
Igor Kudrinb044af52015-11-20 02:32:35 +0000203typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::End;
204
205template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000206typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::MipsGp;
207
Rafael Espindola51d46902015-08-28 21:26:51 +0000208template <class ELFT> class DefinedCommon : public Defined<ELFT> {
209 typedef ELFSymbolBody<ELFT> Base;
210 typedef typename Base::Elf_Sym Elf_Sym;
211
212public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000213 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama294b1362015-09-30 02:06:17 +0000214 DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000215 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
216 MaxAlignment = Sym.st_value;
217 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000218
219 static bool classof(const SymbolBody *S) {
220 return S->kind() == Base::DefinedCommonKind;
221 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000222
223 // The output offset of this common symbol in the output bss. Computed by the
224 // writer.
225 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000226
227 // The maximum alignment we have seen for this symbol.
228 uintX_t MaxAlignment;
Rafael Espindola51d46902015-08-28 21:26:51 +0000229};
230
Michael J. Spencer84487f12015-07-24 21:03:07 +0000231// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000232template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000233 typedef Defined<ELFT> Base;
234 typedef typename Base::Elf_Sym Elf_Sym;
235
Michael J. Spencer84487f12015-07-24 21:03:07 +0000236public:
Rafael Espindolac159c962015-10-19 21:00:02 +0000237 DefinedRegular(StringRef N, const Elf_Sym &Sym,
238 InputSectionBase<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000239 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000240
241 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000242 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000243 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000244
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000245 InputSectionBase<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000246};
247
Rafael Espindola0e604f92015-09-25 18:56:53 +0000248template <class ELFT> class DefinedSynthetic : public Defined<ELFT> {
249 typedef Defined<ELFT> Base;
250
251public:
252 typedef typename Base::Elf_Sym Elf_Sym;
Rui Ueyama294b1362015-09-30 02:06:17 +0000253 DefinedSynthetic(StringRef N, const Elf_Sym &Sym,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000254 OutputSectionBase<ELFT> &Section)
Rafael Espindola0e604f92015-09-25 18:56:53 +0000255 : Defined<ELFT>(Base::DefinedSyntheticKind, N, Sym), Section(Section) {}
256
257 static bool classof(const SymbolBody *S) {
258 return S->kind() == Base::DefinedSyntheticKind;
259 }
260
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000261 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000262};
263
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000264// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000265template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
266 typedef ELFSymbolBody<ELFT> Base;
267 typedef typename Base::Elf_Sym Elf_Sym;
268
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000269public:
Rui Ueyama833ce282015-10-08 00:29:00 +0000270 static Elf_Sym Required;
271 static Elf_Sym Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000272
Rui Ueyama294b1362015-09-30 02:06:17 +0000273 Undefined(StringRef N, const Elf_Sym &Sym)
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000274 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000275
276 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000277 return S->kind() == Base::UndefinedKind;
278 }
Denis Protivensky22220d52015-10-05 09:43:57 +0000279
Rui Ueyama833ce282015-10-08 00:29:00 +0000280 bool canKeepUndefined() const { return &this->Sym == &Optional; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000281};
282
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000283template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000284typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Required;
Denis Protivensky22220d52015-10-05 09:43:57 +0000285template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000286typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000287
Rafael Espindola38af1272015-09-25 15:34:03 +0000288template <class ELFT> class SharedSymbol : public Defined<ELFT> {
Rafael Espindola5b197f02015-09-25 18:32:09 +0000289 typedef Defined<ELFT> Base;
Rafael Espindola18173d42015-09-08 15:50:05 +0000290 typedef typename Base::Elf_Sym Elf_Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000291 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000292
293public:
294 static bool classof(const SymbolBody *S) {
295 return S->kind() == Base::SharedKind;
296 }
297
Rui Ueyama35da9b62015-10-11 20:59:12 +0000298 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
299 : Defined<ELFT>(Base::SharedKind, Name, Sym), File(F) {}
300
301 SharedFile<ELFT> *File;
George Rimarbc590fe2015-10-28 16:48:58 +0000302
303 // Can have offset if requires copy relocation.
Rafael Espindola9b896082015-11-03 14:34:11 +0000304 uintX_t OffsetInBSS = -1;
305 bool needsCopy() const { return OffsetInBSS != (uintX_t)-1; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000306};
307
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000308// This class represents a symbol defined in an archive file. It is
309// created from an archive file header, and it knows how to load an
310// object file from an archive to replace itself with a defined
311// symbol. If the resolver finds both Undefined and Lazy for
312// the same name, it will ask the Lazy to load a file.
313class Lazy : public SymbolBody {
314public:
315 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
Igor Kudrin65bddea2015-10-09 09:58:39 +0000316 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000317 File(F), Sym(S) {}
318
319 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
320
321 // Returns an object file for this symbol, or a nullptr if the file
322 // was already returned.
323 std::unique_ptr<InputFile> getMember();
324
Rafael Espindola8614c562015-10-06 14:33:58 +0000325 void setWeak() { IsWeak = true; }
326 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
327
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000328private:
329 ArchiveFile *File;
330 const llvm::object::Archive::Symbol Sym;
331};
332
Michael J. Spencer84487f12015-07-24 21:03:07 +0000333} // namespace elf2
334} // namespace lld
335
336#endif