blob: 1b09e0113cf19ef5cca29600a4da50c56206b0e6 [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"
16#include "llvm/Object/ELF.h"
17
18namespace lld {
19namespace elf2 {
20
21using llvm::object::ELFFile;
22
23class 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,
44 DefinedLast = 2,
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000045 UndefinedKind = 3
Michael J. Spencer84487f12015-07-24 21:03:07 +000046 };
47
Michael J. Spencercdae0a42015-07-28 22:58:25 +000048 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000049
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000050 bool isWeak() const { return IsWeak; }
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000051 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000052 bool isDefined() const { return !isUndefined(); }
Rafael Espindola30e17972015-08-30 23:17:30 +000053 bool isStrongUndefined() const { return !IsWeak && isUndefined(); }
54 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000055
Michael J. Spencer84487f12015-07-24 21:03:07 +000056 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000057 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000058
59 // A SymbolBody has a backreference to a Symbol. Originally they are
60 // doubly-linked. A backreference will never change. But the pointer
61 // in the Symbol may be mutated by the resolver. If you have a
62 // pointer P to a SymbolBody and are not sure whether the resolver
63 // has chosen the object among other objects having the same name,
64 // you can access P->Backref->Body to get the resolver's result.
65 void setBackref(Symbol *P) { Backref = P; }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000066 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000067
68 // Decides which symbol should "win" in the symbol table, this or
69 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
70 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000071 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000072
73protected:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000074 SymbolBody(Kind K, StringRef Name, bool IsWeak)
75 : SymbolKind(K), IsWeak(IsWeak), Name(Name) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000076
Michael J. Spencercdae0a42015-07-28 22:58:25 +000077protected:
78 const unsigned SymbolKind : 8;
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000079 const unsigned IsWeak : 1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000080 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000081 Symbol *Backref = nullptr;
82};
83
Rafael Espindola1bd885a2015-08-14 16:46:28 +000084// This is for symbols created from elf files and not from the command line.
85// Since they come from object files, they have a Elf_Sym.
86//
87// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
88// we have to delay creating the symbol table until the output format is
89// known and some of its methods will be templated. We should experiment with
90// that once we have a bit more code.
91template <class ELFT> class ELFSymbolBody : public SymbolBody {
92protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +000093 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +000094 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000095 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK), Sym(Sym) {
96 }
Rafael Espindolac44d17a2015-08-14 15:10:49 +000097
Rafael Espindola1bd885a2015-08-14 16:46:28 +000098public:
99 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000100
101 static bool classof(const SymbolBody *S) {
102 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000103 return K >= DefinedFirst && K <= UndefinedKind;
104 }
105};
106
107// The base class for any defined symbols, including absolute symbols,
108// etc.
109template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
110 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000111
112protected:
113 typedef typename Base::Kind Kind;
114 typedef typename Base::Elf_Sym Elf_Sym;
115
116public:
117 explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
118 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000119
120 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000121};
122
123template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
124 typedef ELFSymbolBody<ELFT> Base;
125 typedef typename Base::Elf_Sym Elf_Sym;
126
127public:
128 explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
129 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000130
131 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000132 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000133 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000134};
135
Rafael Espindola51d46902015-08-28 21:26:51 +0000136template <class ELFT> class DefinedCommon : public Defined<ELFT> {
137 typedef ELFSymbolBody<ELFT> Base;
138 typedef typename Base::Elf_Sym Elf_Sym;
139
140public:
141 explicit DefinedCommon(StringRef N, const Elf_Sym &Sym)
142 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {}
143
144 static bool classof(const SymbolBody *S) {
145 return S->kind() == Base::DefinedCommonKind;
146 }
147};
148
Michael J. Spencer84487f12015-07-24 21:03:07 +0000149// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000150template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000151 typedef Defined<ELFT> Base;
152 typedef typename Base::Elf_Sym Elf_Sym;
153
Michael J. Spencer84487f12015-07-24 21:03:07 +0000154public:
Rafael Espindola832b93f2015-08-24 20:06:32 +0000155 explicit DefinedRegular(StringRef N, const Elf_Sym &Sym,
156 SectionChunk<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000157 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000158
159 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000160 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000161 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000162
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000163 const SectionChunk<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000164};
165
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000166// Undefined symbol.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000167template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
168 typedef ELFSymbolBody<ELFT> Base;
169 typedef typename Base::Elf_Sym Elf_Sym;
170
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000171public:
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000172 static Elf_Sym Synthetic;
173
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000174 explicit Undefined(StringRef N, const Elf_Sym &Sym)
175 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000176
177 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000178 return S->kind() == Base::UndefinedKind;
179 }
180};
181
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000182template <class ELFT>
183typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Synthetic;
184
Michael J. Spencer84487f12015-07-24 21:03:07 +0000185} // namespace elf2
186} // namespace lld
187
188#endif