blob: 09c1836efdb27b314b7c10c97524d0fbc38819e0 [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 Espindola832b93f2015-08-24 20:06:32 +000013#include "Chunks.h"
14
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 Chunk;
24class InputFile;
25class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000026template <class ELFT> class ObjectFile;
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
Michael J. Spencer84487f12015-07-24 21:03:07 +000068 // A SymbolBody has a backreference to a Symbol. Originally they are
69 // doubly-linked. A backreference will never change. But the pointer
70 // in the Symbol may be mutated by the resolver. If you have a
71 // pointer P to a SymbolBody and are not sure whether the resolver
72 // has chosen the object among other objects having the same name,
73 // you can access P->Backref->Body to get the resolver's result.
74 void setBackref(Symbol *P) { Backref = P; }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000075 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000076
77 // Decides which symbol should "win" in the symbol table, this or
78 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
79 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000080 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000081
82protected:
Rafael Espindola78471f02015-09-01 23:12:52 +000083 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility)
84 : SymbolKind(K), IsWeak(IsWeak), MostConstrainingVisibility(Visibility),
Rafael Espindola18173d42015-09-08 15:50:05 +000085 Name(Name) {
86 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
87 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000088
Michael J. Spencercdae0a42015-07-28 22:58:25 +000089 const unsigned SymbolKind : 8;
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000090 const unsigned IsWeak : 1;
Rafael Espindola78471f02015-09-01 23:12:52 +000091 unsigned MostConstrainingVisibility : 2;
Rafael Espindola18173d42015-09-08 15:50:05 +000092 unsigned IsUsedInRegularObj : 1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000093 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000094 Symbol *Backref = nullptr;
95};
96
Rafael Espindola1bd885a2015-08-14 16:46:28 +000097// This is for symbols created from elf files and not from the command line.
98// Since they come from object files, they have a Elf_Sym.
99//
100// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
101// we have to delay creating the symbol table until the output format is
102// known and some of its methods will be templated. We should experiment with
103// that once we have a bit more code.
104template <class ELFT> class ELFSymbolBody : public SymbolBody {
105protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000106 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000107 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola78471f02015-09-01 23:12:52 +0000108 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK,
109 Sym.getVisibility()),
110 Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000111
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000112public:
113 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114
115 static bool classof(const SymbolBody *S) {
116 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000117 return K >= DefinedFirst && K <= UndefinedKind;
118 }
119};
120
121// The base class for any defined symbols, including absolute symbols,
122// etc.
123template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
124 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000125
126protected:
127 typedef typename Base::Kind Kind;
128 typedef typename Base::Elf_Sym Elf_Sym;
129
130public:
131 explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
132 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000133
134 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000135};
136
137template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
138 typedef ELFSymbolBody<ELFT> Base;
139 typedef typename Base::Elf_Sym Elf_Sym;
140
141public:
142 explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
143 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000144
145 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000146 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000147 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000148};
149
Rafael Espindola51d46902015-08-28 21:26:51 +0000150template <class ELFT> class DefinedCommon : public Defined<ELFT> {
151 typedef ELFSymbolBody<ELFT> Base;
152 typedef typename Base::Elf_Sym Elf_Sym;
153
154public:
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000155 typedef typename std::conditional<ELFT::Is64Bits, uint64_t, uint32_t>::type
156 uintX_t;
Rafael Espindola51d46902015-08-28 21:26:51 +0000157 explicit DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000158 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
159 MaxAlignment = Sym.st_value;
160 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000161
162 static bool classof(const SymbolBody *S) {
163 return S->kind() == Base::DefinedCommonKind;
164 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000165
166 // The output offset of this common symbol in the output bss. Computed by the
167 // writer.
168 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000169
170 // The maximum alignment we have seen for this symbol.
171 uintX_t MaxAlignment;
Rafael Espindola51d46902015-08-28 21:26:51 +0000172};
173
Michael J. Spencer84487f12015-07-24 21:03:07 +0000174// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000175template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000176 typedef Defined<ELFT> Base;
177 typedef typename Base::Elf_Sym Elf_Sym;
178
Michael J. Spencer84487f12015-07-24 21:03:07 +0000179public:
Rafael Espindola832b93f2015-08-24 20:06:32 +0000180 explicit DefinedRegular(StringRef N, const Elf_Sym &Sym,
181 SectionChunk<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000182 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000183
184 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000185 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000186 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000187
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000188 const SectionChunk<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000189};
190
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000191// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000192template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
193 typedef ELFSymbolBody<ELFT> Base;
194 typedef typename Base::Elf_Sym Elf_Sym;
195
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000196public:
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000197 static Elf_Sym Synthetic;
198
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000199 explicit Undefined(StringRef N, const Elf_Sym &Sym)
200 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000201
202 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000203 return S->kind() == Base::UndefinedKind;
204 }
205};
206
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000207template <class ELFT>
208typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Synthetic;
209
Rafael Espindola18173d42015-09-08 15:50:05 +0000210template <class ELFT> class SharedSymbol : public ELFSymbolBody<ELFT> {
211 typedef ELFSymbolBody<ELFT> Base;
212 typedef typename Base::Elf_Sym Elf_Sym;
213
214public:
215 static bool classof(const SymbolBody *S) {
216 return S->kind() == Base::SharedKind;
217 }
218
219 SharedSymbol(StringRef Name, const Elf_Sym &Sym)
220 : ELFSymbolBody<ELFT>(Base::SharedKind, Name, Sym) {}
221};
222
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000223// This class represents a symbol defined in an archive file. It is
224// created from an archive file header, and it knows how to load an
225// object file from an archive to replace itself with a defined
226// symbol. If the resolver finds both Undefined and Lazy for
227// the same name, it will ask the Lazy to load a file.
228class Lazy : public SymbolBody {
229public:
230 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
231 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT),
232 File(F), Sym(S) {}
233
234 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
235
236 // Returns an object file for this symbol, or a nullptr if the file
237 // was already returned.
238 std::unique_ptr<InputFile> getMember();
239
240private:
241 ArchiveFile *File;
242 const llvm::object::Archive::Symbol Sym;
243};
244
Michael J. Spencer84487f12015-07-24 21:03:07 +0000245} // namespace elf2
246} // namespace lld
247
248#endif