blob: 39e3d31b4aefc57b61be8be23e1d5339432c094d [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 Espindola84aff152015-09-25 21:20:23 +000040 DefinedFirst,
41 DefinedRegularKind = DefinedFirst,
42 DefinedAbsoluteKind,
43 DefinedCommonKind,
44 DefinedSyntheticKind,
45 SharedKind,
46 DefinedLast = SharedKind,
47 UndefinedKind,
48 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000049 };
50
Michael J. Spencercdae0a42015-07-28 22:58:25 +000051 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000052
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000053 bool isWeak() const { return IsWeak; }
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000054 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000055 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000056 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 Espindola05a3dd22015-09-22 23:38:23 +000060 bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; }
61 void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000062
Michael J. Spencer84487f12015-07-24 21:03:07 +000063 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000064 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000065
Rafael Espindola78471f02015-09-01 23:12:52 +000066 uint8_t getMostConstrainingVisibility() const {
67 return MostConstrainingVisibility;
68 }
69
Rafael Espindola19e38892015-09-16 15:54:15 +000070 unsigned getDynamicSymbolTableIndex() const {
71 return DynamicSymbolTableIndex;
72 }
73 void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; }
74
Rafael Espindola5c2310c2015-09-18 14:40:19 +000075 unsigned getGotIndex() const { return GotIndex; }
76 bool isInGot() const { return GotIndex != -1U; }
77 void setGotIndex(unsigned I) { GotIndex = I; }
78
Rafael Espindolaeb792732015-09-21 15:11:29 +000079 unsigned getPltIndex() const { return PltIndex; }
80 bool isInPlt() const { return PltIndex != -1U; }
81 void setPltIndex(unsigned I) { PltIndex = I; }
82
Michael J. Spencer84487f12015-07-24 21:03:07 +000083 // A SymbolBody has a backreference to a Symbol. Originally they are
84 // doubly-linked. A backreference will never change. But the pointer
85 // in the Symbol may be mutated by the resolver. If you have a
86 // pointer P to a SymbolBody and are not sure whether the resolver
87 // has chosen the object among other objects having the same name,
88 // you can access P->Backref->Body to get the resolver's result.
89 void setBackref(Symbol *P) { Backref = P; }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000090 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000091
92 // Decides which symbol should "win" in the symbol table, this or
93 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
94 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000095 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000096
97protected:
Rafael Espindola78471f02015-09-01 23:12:52 +000098 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility)
99 : SymbolKind(K), IsWeak(IsWeak), MostConstrainingVisibility(Visibility),
Rafael Espindola18173d42015-09-08 15:50:05 +0000100 Name(Name) {
101 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000102 IsUsedInDynamicReloc = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000103 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000104
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000105 const unsigned SymbolKind : 8;
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000106 const unsigned IsWeak : 1;
Rafael Espindola78471f02015-09-01 23:12:52 +0000107 unsigned MostConstrainingVisibility : 2;
Rafael Espindola18173d42015-09-08 15:50:05 +0000108 unsigned IsUsedInRegularObj : 1;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000109 unsigned IsUsedInDynamicReloc : 1;
Rafael Espindola19e38892015-09-16 15:54:15 +0000110 unsigned DynamicSymbolTableIndex = 0;
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000111 unsigned GotIndex = -1;
Rafael Espindolaeb792732015-09-21 15:11:29 +0000112 unsigned PltIndex = -1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000113 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114 Symbol *Backref = nullptr;
115};
116
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000117// This is for symbols created from elf files and not from the command line.
118// Since they come from object files, they have a Elf_Sym.
119//
120// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
121// we have to delay creating the symbol table until the output format is
122// known and some of its methods will be templated. We should experiment with
123// that once we have a bit more code.
124template <class ELFT> class ELFSymbolBody : public SymbolBody {
125protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000126 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000127 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola78471f02015-09-01 23:12:52 +0000128 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK,
129 Sym.getVisibility()),
130 Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000131
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000132public:
133 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000134
135 static bool classof(const SymbolBody *S) {
136 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000137 return K >= DefinedFirst && K <= UndefinedKind;
138 }
139};
140
141// The base class for any defined symbols, including absolute symbols,
142// etc.
143template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
144 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000145
146protected:
147 typedef typename Base::Kind Kind;
148 typedef typename Base::Elf_Sym Elf_Sym;
149
150public:
Rui Ueyama294b1362015-09-30 02:06:17 +0000151 Defined(Kind K, StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000152 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000153
154 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000155};
156
157template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
158 typedef ELFSymbolBody<ELFT> Base;
159 typedef typename Base::Elf_Sym Elf_Sym;
160
161public:
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000162 static Elf_Sym IgnoreUndef;
163
Rui Ueyama294b1362015-09-30 02:06:17 +0000164 DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000165 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000166
167 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000168 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000169 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000170};
171
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000172template <class ELFT>
173typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef;
174
Rafael Espindola51d46902015-08-28 21:26:51 +0000175template <class ELFT> class DefinedCommon : public Defined<ELFT> {
176 typedef ELFSymbolBody<ELFT> Base;
177 typedef typename Base::Elf_Sym Elf_Sym;
178
179public:
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000180 typedef typename std::conditional<ELFT::Is64Bits, uint64_t, uint32_t>::type
181 uintX_t;
Rui Ueyama294b1362015-09-30 02:06:17 +0000182 DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000183 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
184 MaxAlignment = Sym.st_value;
185 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000186
187 static bool classof(const SymbolBody *S) {
188 return S->kind() == Base::DefinedCommonKind;
189 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000190
191 // The output offset of this common symbol in the output bss. Computed by the
192 // writer.
193 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000194
195 // The maximum alignment we have seen for this symbol.
196 uintX_t MaxAlignment;
Rafael Espindola51d46902015-08-28 21:26:51 +0000197};
198
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000200template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000201 typedef Defined<ELFT> Base;
202 typedef typename Base::Elf_Sym Elf_Sym;
203
Michael J. Spencer84487f12015-07-24 21:03:07 +0000204public:
Rui Ueyama294b1362015-09-30 02:06:17 +0000205 DefinedRegular(StringRef N, const Elf_Sym &Sym, InputSection<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000206 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000207
208 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000209 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000210 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000211
Rafael Espindola53d5cea2015-09-21 17:47:00 +0000212 const InputSection<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000213};
214
Rafael Espindola0e604f92015-09-25 18:56:53 +0000215template <class ELFT> class DefinedSynthetic : public Defined<ELFT> {
216 typedef Defined<ELFT> Base;
217
218public:
219 typedef typename Base::Elf_Sym Elf_Sym;
Rui Ueyama294b1362015-09-30 02:06:17 +0000220 DefinedSynthetic(StringRef N, const Elf_Sym &Sym,
221 OutputSection<ELFT> &Section)
Rafael Espindola0e604f92015-09-25 18:56:53 +0000222 : Defined<ELFT>(Base::DefinedSyntheticKind, N, Sym), Section(Section) {}
223
224 static bool classof(const SymbolBody *S) {
225 return S->kind() == Base::DefinedSyntheticKind;
226 }
227
228 const OutputSection<ELFT> &Section;
229};
230
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000231// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000232template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
233 typedef ELFSymbolBody<ELFT> Base;
234 typedef typename Base::Elf_Sym Elf_Sym;
235
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000236public:
Denis Protivensky22220d52015-10-05 09:43:57 +0000237 static Elf_Sym SyntheticRequired;
238 static Elf_Sym SyntheticOptional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000239
Rui Ueyama294b1362015-09-30 02:06:17 +0000240 Undefined(StringRef N, const Elf_Sym &Sym)
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000241 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000242
243 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000244 return S->kind() == Base::UndefinedKind;
245 }
Denis Protivensky22220d52015-10-05 09:43:57 +0000246
247 bool canKeepUndefined() const { return &this->Sym == &SyntheticOptional; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000248};
249
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000250template <class ELFT>
Denis Protivensky22220d52015-10-05 09:43:57 +0000251typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::SyntheticRequired;
252template <class ELFT>
253typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::SyntheticOptional;
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000254
Rafael Espindola38af1272015-09-25 15:34:03 +0000255template <class ELFT> class SharedSymbol : public Defined<ELFT> {
Rafael Espindola5b197f02015-09-25 18:32:09 +0000256 typedef Defined<ELFT> Base;
Rafael Espindola18173d42015-09-08 15:50:05 +0000257 typedef typename Base::Elf_Sym Elf_Sym;
258
259public:
260 static bool classof(const SymbolBody *S) {
261 return S->kind() == Base::SharedKind;
262 }
263
264 SharedSymbol(StringRef Name, const Elf_Sym &Sym)
Rafael Espindola38af1272015-09-25 15:34:03 +0000265 : Defined<ELFT>(Base::SharedKind, Name, Sym) {}
Rafael Espindola18173d42015-09-08 15:50:05 +0000266};
267
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000268// This class represents a symbol defined in an archive file. It is
269// created from an archive file header, and it knows how to load an
270// object file from an archive to replace itself with a defined
271// symbol. If the resolver finds both Undefined and Lazy for
272// the same name, it will ask the Lazy to load a file.
273class Lazy : public SymbolBody {
274public:
275 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
276 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT),
277 File(F), Sym(S) {}
278
279 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
280
281 // Returns an object file for this symbol, or a nullptr if the file
282 // was already returned.
283 std::unique_ptr<InputFile> getMember();
284
285private:
286 ArchiveFile *File;
287 const llvm::object::Archive::Symbol Sym;
288};
289
Michael J. Spencer84487f12015-07-24 21:03:07 +0000290} // namespace elf2
291} // namespace lld
292
293#endif