blob: 309c34f1810f243d0eef87802c299321e320f121 [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,
43 DefinedWeakKind = 2,
44 DefinedLast = 2,
45 UndefinedWeakKind = 3,
46 UndefinedKind = 4,
47 UndefinedSyntheticKind = 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 Espindola1bd885a2015-08-14 16:46:28 +000052 bool isStrongUndefined() {
53 return SymbolKind == UndefinedKind || SymbolKind == UndefinedSyntheticKind;
54 }
55
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. Spencer84487f12015-07-24 21:03:07 +000066
67 // Decides which symbol should "win" in the symbol table, this or
68 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
69 // they are duplicate (conflicting) symbols.
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000070 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000071
72protected:
Rafael Espindola3bf356e2015-08-14 14:38:44 +000073 SymbolBody(Kind K, StringRef Name) : SymbolKind(K), Name(Name) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000074
Michael J. Spencercdae0a42015-07-28 22:58:25 +000075protected:
76 const unsigned SymbolKind : 8;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000077 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000078 Symbol *Backref = nullptr;
79};
80
Rafael Espindola1bd885a2015-08-14 16:46:28 +000081// This is for symbols created from elf files and not from the command line.
82// Since they come from object files, they have a Elf_Sym.
83//
84// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
85// we have to delay creating the symbol table until the output format is
86// known and some of its methods will be templated. We should experiment with
87// that once we have a bit more code.
88template <class ELFT> class ELFSymbolBody : public SymbolBody {
89protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +000090 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +000091 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
92 : SymbolBody(K, Name), Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +000093
Rafael Espindola1bd885a2015-08-14 16:46:28 +000094public:
95 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +000096
97 static bool classof(const SymbolBody *S) {
98 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +000099 return K >= DefinedFirst && K <= UndefinedKind;
100 }
101};
102
103// The base class for any defined symbols, including absolute symbols,
104// etc.
105template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
106 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000107
108protected:
109 typedef typename Base::Kind Kind;
110 typedef typename Base::Elf_Sym Elf_Sym;
111
112public:
113 explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
114 : ELFSymbolBody<ELFT>(K, N, Sym) {}
115};
116
117template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
118 typedef ELFSymbolBody<ELFT> Base;
119 typedef typename Base::Elf_Sym Elf_Sym;
120
121public:
122 explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
123 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
124};
125
126template <class ELFT> class DefinedInSection : public Defined<ELFT> {
127 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000128 typedef typename Base::Kind Kind;
129
130public:
131 typedef typename Base::Elf_Sym Elf_Sym;
132
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000133 explicit DefinedInSection(Kind K, StringRef N, const Elf_Sym &Sym,
134 SectionChunk<ELFT> &Section)
135 : Defined<ELFT>(K, N, Sym), Section(Section) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000136
137 static bool classof(const SymbolBody *S) {
138 Kind K = S->kind();
139 return Base::DefinedFirst <= K && K <= Base::DefinedLast;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000140 }
Rafael Espindola832b93f2015-08-24 20:06:32 +0000141
142 const SectionChunk<ELFT> &Section;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000143};
144
145// Regular defined symbols read from object file symbol tables.
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000146template <class ELFT> class DefinedRegular : public DefinedInSection<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000147 typedef Defined<ELFT> Base;
148 typedef typename Base::Elf_Sym Elf_Sym;
149
Michael J. Spencer84487f12015-07-24 21:03:07 +0000150public:
Rafael Espindola832b93f2015-08-24 20:06:32 +0000151 explicit DefinedRegular(StringRef N, const Elf_Sym &Sym,
152 SectionChunk<ELFT> &Section)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000153 : DefinedInSection<ELFT>(Base::DefinedRegularKind, N, Sym, Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000154
155 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000156 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000157 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000158};
159
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000160template <class ELFT> class DefinedWeak : public DefinedInSection<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000161 typedef Defined<ELFT> Base;
162 typedef typename Base::Elf_Sym Elf_Sym;
163
Rafael Espindolab13df652015-08-11 17:33:02 +0000164public:
Rafael Espindola832b93f2015-08-24 20:06:32 +0000165 explicit DefinedWeak(StringRef N, const Elf_Sym &Sym,
166 SectionChunk<ELFT> &Section)
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000167 : DefinedInSection<ELFT>(Base::DefinedWeakKind, N, Sym, Section) {}
Rafael Espindolab13df652015-08-11 17:33:02 +0000168
169 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000170 return S->kind() == Base::DefinedWeakKind;
Rafael Espindolab13df652015-08-11 17:33:02 +0000171 }
172};
173
Michael J. Spencer84487f12015-07-24 21:03:07 +0000174// Undefined symbols.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000175class SyntheticUndefined : public SymbolBody {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000176public:
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000177 explicit SyntheticUndefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000178
179 static bool classof(const SymbolBody *S) {
180 return S->kind() == UndefinedKind;
181 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000182};
183
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000184template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
185 typedef ELFSymbolBody<ELFT> Base;
186 typedef typename Base::Elf_Sym Elf_Sym;
187
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000188public:
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000189 explicit Undefined(StringRef N, const Elf_Sym &Sym)
190 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000191
192 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000193 return S->kind() == Base::UndefinedKind;
194 }
195};
196
197template <class ELFT> class UndefinedWeak : public ELFSymbolBody<ELFT> {
198 typedef ELFSymbolBody<ELFT> Base;
199 typedef typename Base::Elf_Sym Elf_Sym;
200
201public:
202 explicit UndefinedWeak(StringRef N, const Elf_Sym &Sym)
203 : ELFSymbolBody<ELFT>(Base::UndefinedWeakKind, N, Sym) {}
204
205 static bool classof(const SymbolBody *S) {
206 return S->kind() == Base::UndefinedWeakKind;
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000207 }
208};
209
Michael J. Spencer84487f12015-07-24 21:03:07 +0000210} // namespace elf2
211} // namespace lld
212
213#endif