blob: 6f8072101abfdb3c32d123f48cdb9c3989ff8f5d [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//===----------------------------------------------------------------------===//
9
10#ifndef LLD_ELF_SYMBOLS_H
11#define LLD_ELF_SYMBOLS_H
12
Rafael Espindola9d06ab62015-09-22 00:01:39 +000013#include "InputSection.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000014
Michael J. Spencer84487f12015-07-24 21:03:07 +000015#include "lld/Core/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000016#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000017#include "llvm/Object/ELF.h"
18
19namespace lld {
20namespace elf2 {
21
Michael J. Spencer1b348a62015-09-04 22:28:10 +000022class ArchiveFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023class InputFile;
24class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000025template <class ELFT> class ObjectFile;
Michael J. Spencer658dccd2015-09-18 22:13:25 +000026template <class ELFT> class OutputSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
Rui Ueyama9ea49c72015-10-07 23:46:11 +000028// Initializes global objects defined in this file.
29// Called at the beginning of main().
30void initSymbols();
31
Michael J. Spencer84487f12015-07-24 21:03:07 +000032// A real symbol object, SymbolBody, is usually accessed indirectly
33// through a Symbol. There's always one Symbol for each symbol name.
34// The resolver updates SymbolBody pointers as it resolves symbols.
35struct Symbol {
36 explicit Symbol(SymbolBody *P) : Body(P) {}
37 SymbolBody *Body;
38};
39
40// The base class for real symbol classes.
41class SymbolBody {
42public:
43 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000044 DefinedFirst,
45 DefinedRegularKind = DefinedFirst,
46 DefinedAbsoluteKind,
47 DefinedCommonKind,
48 DefinedSyntheticKind,
49 SharedKind,
50 DefinedLast = SharedKind,
51 UndefinedKind,
52 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000053 };
54
Michael J. Spencercdae0a42015-07-28 22:58:25 +000055 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000056
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000057 bool isWeak() const { return IsWeak; }
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000058 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000059 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000060 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000061 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000062 bool isShared() const { return SymbolKind == SharedKind; }
63 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rafael Espindola05a3dd22015-09-22 23:38:23 +000064 bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; }
65 void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000066
Michael J. Spencer84487f12015-07-24 21:03:07 +000067 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000068 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000069
Rafael Espindola78471f02015-09-01 23:12:52 +000070 uint8_t getMostConstrainingVisibility() const {
71 return MostConstrainingVisibility;
72 }
73
Rafael Espindola19e38892015-09-16 15:54:15 +000074 unsigned getDynamicSymbolTableIndex() const {
75 return DynamicSymbolTableIndex;
76 }
77 void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; }
78
Rafael Espindola5c2310c2015-09-18 14:40:19 +000079 unsigned getGotIndex() const { return GotIndex; }
80 bool isInGot() const { return GotIndex != -1U; }
81 void setGotIndex(unsigned I) { GotIndex = I; }
82
Rafael Espindolaeb792732015-09-21 15:11:29 +000083 unsigned getPltIndex() const { return PltIndex; }
84 bool isInPlt() const { return PltIndex != -1U; }
85 void setPltIndex(unsigned I) { PltIndex = I; }
86
Michael J. Spencer84487f12015-07-24 21:03:07 +000087 // A SymbolBody has a backreference to a Symbol. Originally they are
88 // doubly-linked. A backreference will never change. But the pointer
89 // in the Symbol may be mutated by the resolver. If you have a
90 // pointer P to a SymbolBody and are not sure whether the resolver
91 // has chosen the object among other objects having the same name,
92 // you can access P->Backref->Body to get the resolver's result.
93 void setBackref(Symbol *P) { Backref = P; }
Rui Ueyamae66e0012015-10-07 23:20:23 +000094 SymbolBody *repl() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000095
96 // Decides which symbol should "win" in the symbol table, this or
97 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
98 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000099 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000100
101protected:
Rafael Espindola78471f02015-09-01 23:12:52 +0000102 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility)
103 : SymbolKind(K), IsWeak(IsWeak), MostConstrainingVisibility(Visibility),
Rafael Espindola18173d42015-09-08 15:50:05 +0000104 Name(Name) {
105 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000106 IsUsedInDynamicReloc = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000107 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000108
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000109 const unsigned SymbolKind : 8;
Rafael Espindola8614c562015-10-06 14:33:58 +0000110 unsigned IsWeak : 1;
Rafael Espindola78471f02015-09-01 23:12:52 +0000111 unsigned MostConstrainingVisibility : 2;
Rafael Espindola18173d42015-09-08 15:50:05 +0000112 unsigned IsUsedInRegularObj : 1;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000113 unsigned IsUsedInDynamicReloc : 1;
Rafael Espindola19e38892015-09-16 15:54:15 +0000114 unsigned DynamicSymbolTableIndex = 0;
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000115 unsigned GotIndex = -1;
Rafael Espindolaeb792732015-09-21 15:11:29 +0000116 unsigned PltIndex = -1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000117 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000118 Symbol *Backref = nullptr;
119};
120
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000121// This is for symbols created from elf files and not from the command line.
122// Since they come from object files, they have a Elf_Sym.
123//
124// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
125// we have to delay creating the symbol table until the output format is
126// known and some of its methods will be templated. We should experiment with
127// that once we have a bit more code.
128template <class ELFT> class ELFSymbolBody : public SymbolBody {
129protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000130 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000131 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola78471f02015-09-01 23:12:52 +0000132 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK,
133 Sym.getVisibility()),
134 Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000135
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000136public:
137 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000138
139 static bool classof(const SymbolBody *S) {
140 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000141 return K >= DefinedFirst && K <= UndefinedKind;
142 }
143};
144
145// The base class for any defined symbols, including absolute symbols,
146// etc.
147template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
148 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000149
150protected:
151 typedef typename Base::Kind Kind;
152 typedef typename Base::Elf_Sym Elf_Sym;
153
154public:
Rui Ueyama294b1362015-09-30 02:06:17 +0000155 Defined(Kind K, StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000156 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000157
158 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000159};
160
161template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
162 typedef ELFSymbolBody<ELFT> Base;
163 typedef typename Base::Elf_Sym Elf_Sym;
164
165public:
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000166 static Elf_Sym IgnoreUndef;
167
Rui Ueyama294b1362015-09-30 02:06:17 +0000168 DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000169 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000170
171 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000172 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000173 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000174};
175
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000176template <class ELFT>
177typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef;
178
Rafael Espindola51d46902015-08-28 21:26:51 +0000179template <class ELFT> class DefinedCommon : public Defined<ELFT> {
180 typedef ELFSymbolBody<ELFT> Base;
181 typedef typename Base::Elf_Sym Elf_Sym;
182
183public:
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000184 typedef typename std::conditional<ELFT::Is64Bits, uint64_t, uint32_t>::type
185 uintX_t;
Rui Ueyama294b1362015-09-30 02:06:17 +0000186 DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000187 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
188 MaxAlignment = Sym.st_value;
189 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000190
191 static bool classof(const SymbolBody *S) {
192 return S->kind() == Base::DefinedCommonKind;
193 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000194
195 // The output offset of this common symbol in the output bss. Computed by the
196 // writer.
197 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000198
199 // The maximum alignment we have seen for this symbol.
200 uintX_t MaxAlignment;
Rafael Espindola51d46902015-08-28 21:26:51 +0000201};
202
Michael J. Spencer84487f12015-07-24 21:03:07 +0000203// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000204template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000205 typedef Defined<ELFT> Base;
206 typedef typename Base::Elf_Sym Elf_Sym;
207
Michael J. Spencer84487f12015-07-24 21:03:07 +0000208public:
Rui Ueyama294b1362015-09-30 02:06:17 +0000209 DefinedRegular(StringRef N, const Elf_Sym &Sym, InputSection<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000210 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000211
212 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000213 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000214 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000215
Rafael Espindola53d5cea2015-09-21 17:47:00 +0000216 const InputSection<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000217};
218
Rafael Espindola0e604f92015-09-25 18:56:53 +0000219template <class ELFT> class DefinedSynthetic : public Defined<ELFT> {
220 typedef Defined<ELFT> Base;
221
222public:
223 typedef typename Base::Elf_Sym Elf_Sym;
Rui Ueyama294b1362015-09-30 02:06:17 +0000224 DefinedSynthetic(StringRef N, const Elf_Sym &Sym,
225 OutputSection<ELFT> &Section)
Rafael Espindola0e604f92015-09-25 18:56:53 +0000226 : Defined<ELFT>(Base::DefinedSyntheticKind, N, Sym), Section(Section) {}
227
228 static bool classof(const SymbolBody *S) {
229 return S->kind() == Base::DefinedSyntheticKind;
230 }
231
232 const OutputSection<ELFT> &Section;
233};
234
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000235// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000236template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
237 typedef ELFSymbolBody<ELFT> Base;
238 typedef typename Base::Elf_Sym Elf_Sym;
239
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000240public:
Rui Ueyama833ce282015-10-08 00:29:00 +0000241 static Elf_Sym Required;
242 static Elf_Sym Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000243
Rui Ueyama294b1362015-09-30 02:06:17 +0000244 Undefined(StringRef N, const Elf_Sym &Sym)
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000245 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000246
247 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000248 return S->kind() == Base::UndefinedKind;
249 }
Denis Protivensky22220d52015-10-05 09:43:57 +0000250
Rui Ueyama833ce282015-10-08 00:29:00 +0000251 bool canKeepUndefined() const { return &this->Sym == &Optional; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000252};
253
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000254template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000255typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Required;
Denis Protivensky22220d52015-10-05 09:43:57 +0000256template <class ELFT>
Rui Ueyama833ce282015-10-08 00:29:00 +0000257typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Optional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000258
Rafael Espindola38af1272015-09-25 15:34:03 +0000259template <class ELFT> class SharedSymbol : public Defined<ELFT> {
Rafael Espindola5b197f02015-09-25 18:32:09 +0000260 typedef Defined<ELFT> Base;
Rafael Espindola18173d42015-09-08 15:50:05 +0000261 typedef typename Base::Elf_Sym Elf_Sym;
262
263public:
264 static bool classof(const SymbolBody *S) {
265 return S->kind() == Base::SharedKind;
266 }
267
268 SharedSymbol(StringRef Name, const Elf_Sym &Sym)
Rafael Espindola38af1272015-09-25 15:34:03 +0000269 : Defined<ELFT>(Base::SharedKind, Name, Sym) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000270};
271
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000272// This class represents a symbol defined in an archive file. It is
273// created from an archive file header, and it knows how to load an
274// object file from an archive to replace itself with a defined
275// symbol. If the resolver finds both Undefined and Lazy for
276// the same name, it will ask the Lazy to load a file.
277class Lazy : public SymbolBody {
278public:
279 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
280 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT),
281 File(F), Sym(S) {}
282
283 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
284
285 // Returns an object file for this symbol, or a nullptr if the file
286 // was already returned.
287 std::unique_ptr<InputFile> getMember();
288
Rafael Espindola8614c562015-10-06 14:33:58 +0000289 void setWeak() { IsWeak = true; }
290 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
291
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000292private:
293 ArchiveFile *File;
294 const llvm::object::Archive::Symbol Sym;
295};
296
Michael J. Spencer84487f12015-07-24 21:03:07 +0000297} // namespace elf2
298} // namespace lld
299
300#endif