blob: b30e706c305e0bc9f24b710f390bd565421863cb [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
28// A real symbol object, SymbolBody, is usually accessed indirectly
29// through a Symbol. There's always one Symbol for each symbol name.
30// The resolver updates SymbolBody pointers as it resolves symbols.
31struct Symbol {
32 explicit Symbol(SymbolBody *P) : Body(P) {}
33 SymbolBody *Body;
34};
35
36// The base class for real symbol classes.
37class SymbolBody {
38public:
39 enum Kind {
Rafael Espindolaae1b23b2015-08-11 17:10:02 +000040 DefinedFirst = 0,
41 DefinedRegularKind = 0,
Rafael Espindola0e0c1902015-08-27 12:40:06 +000042 DefinedAbsoluteKind = 1,
Rafael Espindola51d46902015-08-28 21:26:51 +000043 DefinedCommonKind = 2,
Rafael Espindola18173d42015-09-08 15:50:05 +000044 SharedKind = 3,
45 DefinedLast = 3,
46 UndefinedKind = 4,
47 LazyKind = 5,
Michael J. Spencer84487f12015-07-24 21:03:07 +000048 };
49
Michael J. Spencercdae0a42015-07-28 22:58:25 +000050 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000051
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000052 bool isWeak() const { return IsWeak; }
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000053 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000054 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000055 bool isStrongUndefined() const { return !IsWeak && isUndefined(); }
56 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000057 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000058 bool isShared() const { return SymbolKind == SharedKind; }
59 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000060
Michael J. Spencer84487f12015-07-24 21:03:07 +000061 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000062 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000063
Rafael Espindola78471f02015-09-01 23:12:52 +000064 uint8_t getMostConstrainingVisibility() const {
65 return MostConstrainingVisibility;
66 }
67
Rafael Espindola19e38892015-09-16 15:54:15 +000068 unsigned getDynamicSymbolTableIndex() const {
69 return DynamicSymbolTableIndex;
70 }
71 void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; }
72
Rafael Espindola5c2310c2015-09-18 14:40:19 +000073 unsigned getGotIndex() const { return GotIndex; }
74 bool isInGot() const { return GotIndex != -1U; }
75 void setGotIndex(unsigned I) { GotIndex = I; }
76
Rafael Espindolaeb792732015-09-21 15:11:29 +000077 unsigned getPltIndex() const { return PltIndex; }
78 bool isInPlt() const { return PltIndex != -1U; }
79 void setPltIndex(unsigned I) { PltIndex = I; }
80
Michael J. Spencer84487f12015-07-24 21:03:07 +000081 // A SymbolBody has a backreference to a Symbol. Originally they are
82 // doubly-linked. A backreference will never change. But the pointer
83 // in the Symbol may be mutated by the resolver. If you have a
84 // pointer P to a SymbolBody and are not sure whether the resolver
85 // has chosen the object among other objects having the same name,
86 // you can access P->Backref->Body to get the resolver's result.
87 void setBackref(Symbol *P) { Backref = P; }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000088 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000089
90 // Decides which symbol should "win" in the symbol table, this or
91 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
92 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000093 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000094
95protected:
Rafael Espindola78471f02015-09-01 23:12:52 +000096 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility)
97 : SymbolKind(K), IsWeak(IsWeak), MostConstrainingVisibility(Visibility),
Rafael Espindola18173d42015-09-08 15:50:05 +000098 Name(Name) {
99 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
100 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000101
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000102 const unsigned SymbolKind : 8;
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000103 const unsigned IsWeak : 1;
Rafael Espindola78471f02015-09-01 23:12:52 +0000104 unsigned MostConstrainingVisibility : 2;
Rafael Espindola18173d42015-09-08 15:50:05 +0000105 unsigned IsUsedInRegularObj : 1;
Rafael Espindola19e38892015-09-16 15:54:15 +0000106 unsigned DynamicSymbolTableIndex = 0;
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000107 unsigned GotIndex = -1;
Rafael Espindolaeb792732015-09-21 15:11:29 +0000108 unsigned PltIndex = -1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000109 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000110 Symbol *Backref = nullptr;
111};
112
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000113// This is for symbols created from elf files and not from the command line.
114// Since they come from object files, they have a Elf_Sym.
115//
116// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
117// we have to delay creating the symbol table until the output format is
118// known and some of its methods will be templated. We should experiment with
119// that once we have a bit more code.
120template <class ELFT> class ELFSymbolBody : public SymbolBody {
121protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000122 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000123 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola78471f02015-09-01 23:12:52 +0000124 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK,
125 Sym.getVisibility()),
126 Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000127
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000128public:
129 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000130
131 static bool classof(const SymbolBody *S) {
132 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000133 return K >= DefinedFirst && K <= UndefinedKind;
134 }
135};
136
137// The base class for any defined symbols, including absolute symbols,
138// etc.
139template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
140 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000141
142protected:
143 typedef typename Base::Kind Kind;
144 typedef typename Base::Elf_Sym Elf_Sym;
145
146public:
147 explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
148 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000149
150 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000151};
152
153template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
154 typedef ELFSymbolBody<ELFT> Base;
155 typedef typename Base::Elf_Sym Elf_Sym;
156
157public:
158 explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
159 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000160
161 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000162 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000163 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000164};
165
Rafael Espindola51d46902015-08-28 21:26:51 +0000166template <class ELFT> class DefinedCommon : public Defined<ELFT> {
167 typedef ELFSymbolBody<ELFT> Base;
168 typedef typename Base::Elf_Sym Elf_Sym;
169
170public:
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000171 typedef typename std::conditional<ELFT::Is64Bits, uint64_t, uint32_t>::type
172 uintX_t;
Rafael Espindola51d46902015-08-28 21:26:51 +0000173 explicit DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000174 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
175 MaxAlignment = Sym.st_value;
176 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000177
178 static bool classof(const SymbolBody *S) {
179 return S->kind() == Base::DefinedCommonKind;
180 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000181
182 // The output offset of this common symbol in the output bss. Computed by the
183 // writer.
184 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000185
186 // The maximum alignment we have seen for this symbol.
187 uintX_t MaxAlignment;
Michael J. Spencer658dccd2015-09-18 22:13:25 +0000188
189 OutputSection<ELFT> *OutputSec = nullptr;
Rafael Espindola51d46902015-08-28 21:26:51 +0000190};
191
Michael J. Spencer84487f12015-07-24 21:03:07 +0000192// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000193template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000194 typedef Defined<ELFT> Base;
195 typedef typename Base::Elf_Sym Elf_Sym;
196
Michael J. Spencer84487f12015-07-24 21:03:07 +0000197public:
Rafael Espindola832b93f2015-08-24 20:06:32 +0000198 explicit DefinedRegular(StringRef N, const Elf_Sym &Sym,
Rafael Espindola53d5cea2015-09-21 17:47:00 +0000199 InputSection<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000200 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000201
202 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000203 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000204 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000205
Rafael Espindola53d5cea2015-09-21 17:47:00 +0000206 const InputSection<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000207};
208
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000209// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000210template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
211 typedef ELFSymbolBody<ELFT> Base;
212 typedef typename Base::Elf_Sym Elf_Sym;
213
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000214public:
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000215 static Elf_Sym Synthetic;
216
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000217 explicit Undefined(StringRef N, const Elf_Sym &Sym)
218 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000219
220 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000221 return S->kind() == Base::UndefinedKind;
222 }
223};
224
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000225template <class ELFT>
226typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Synthetic;
227
Rafael Espindola18173d42015-09-08 15:50:05 +0000228template <class ELFT> class SharedSymbol : public ELFSymbolBody<ELFT> {
229 typedef ELFSymbolBody<ELFT> Base;
230 typedef typename Base::Elf_Sym Elf_Sym;
231
232public:
233 static bool classof(const SymbolBody *S) {
234 return S->kind() == Base::SharedKind;
235 }
236
237 SharedSymbol(StringRef Name, const Elf_Sym &Sym)
238 : ELFSymbolBody<ELFT>(Base::SharedKind, Name, Sym) {}
239};
240
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000241// This class represents a symbol defined in an archive file. It is
242// created from an archive file header, and it knows how to load an
243// object file from an archive to replace itself with a defined
244// symbol. If the resolver finds both Undefined and Lazy for
245// the same name, it will ask the Lazy to load a file.
246class Lazy : public SymbolBody {
247public:
248 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
249 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT),
250 File(F), Sym(S) {}
251
252 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
253
254 // Returns an object file for this symbol, or a nullptr if the file
255 // was already returned.
256 std::unique_ptr<InputFile> getMember();
257
258private:
259 ArchiveFile *File;
260 const llvm::object::Archive::Symbol Sym;
261};
262
Michael J. Spencer84487f12015-07-24 21:03:07 +0000263} // namespace elf2
264} // namespace lld
265
266#endif