blob: a2dd2dd2add99c8c38e3509f0c9e5046e932b68c [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,
45 UndefinedKind = 3,
46 UndefinedSyntheticKind = 4
Michael J. Spencer84487f12015-07-24 21:03:07 +000047 };
48
Michael J. Spencercdae0a42015-07-28 22:58:25 +000049 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000050
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000051 bool isWeak() const { return IsWeak; }
52 bool isUndefined() const {
Rafael Espindola1bd885a2015-08-14 16:46:28 +000053 return SymbolKind == UndefinedKind || SymbolKind == UndefinedSyntheticKind;
54 }
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000055 bool isDefined() const { return !isUndefined(); }
56 bool isStrongUndefined() { return !IsWeak && isUndefined(); }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000057
Michael J. Spencer84487f12015-07-24 21:03:07 +000058 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000059 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000060
61 // A SymbolBody has a backreference to a Symbol. Originally they are
62 // doubly-linked. A backreference will never change. But the pointer
63 // in the Symbol may be mutated by the resolver. If you have a
64 // pointer P to a SymbolBody and are not sure whether the resolver
65 // has chosen the object among other objects having the same name,
66 // you can access P->Backref->Body to get the resolver's result.
67 void setBackref(Symbol *P) { Backref = P; }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000068 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000069
70 // Decides which symbol should "win" in the symbol table, this or
71 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
72 // they are duplicate (conflicting) symbols.
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000073 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000074
75protected:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000076 SymbolBody(Kind K, StringRef Name, bool IsWeak)
77 : SymbolKind(K), IsWeak(IsWeak), Name(Name) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000078
Michael J. Spencercdae0a42015-07-28 22:58:25 +000079protected:
80 const unsigned SymbolKind : 8;
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000081 const unsigned IsWeak : 1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000082 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000083 Symbol *Backref = nullptr;
84};
85
Rafael Espindola1bd885a2015-08-14 16:46:28 +000086// This is for symbols created from elf files and not from the command line.
87// Since they come from object files, they have a Elf_Sym.
88//
89// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
90// we have to delay creating the symbol table until the output format is
91// known and some of its methods will be templated. We should experiment with
92// that once we have a bit more code.
93template <class ELFT> class ELFSymbolBody : public SymbolBody {
94protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +000095 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +000096 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000097 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK), Sym(Sym) {
98 }
Rafael Espindolac44d17a2015-08-14 15:10:49 +000099
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000100public:
101 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000102
103 static bool classof(const SymbolBody *S) {
104 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000105 return K >= DefinedFirst && K <= UndefinedKind;
106 }
107};
108
109// The base class for any defined symbols, including absolute symbols,
110// etc.
111template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
112 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000113
114protected:
115 typedef typename Base::Kind Kind;
116 typedef typename Base::Elf_Sym Elf_Sym;
117
118public:
119 explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
120 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000121
122 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000123};
124
125template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
126 typedef ELFSymbolBody<ELFT> Base;
127 typedef typename Base::Elf_Sym Elf_Sym;
128
129public:
130 explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
131 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000132
133 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000134 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000135 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000136};
137
Rafael Espindola51d46902015-08-28 21:26:51 +0000138template <class ELFT> class DefinedCommon : public Defined<ELFT> {
139 typedef ELFSymbolBody<ELFT> Base;
140 typedef typename Base::Elf_Sym Elf_Sym;
141
142public:
143 explicit DefinedCommon(StringRef N, const Elf_Sym &Sym)
144 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {}
145
146 static bool classof(const SymbolBody *S) {
147 return S->kind() == Base::DefinedCommonKind;
148 }
149};
150
Michael J. Spencer84487f12015-07-24 21:03:07 +0000151// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000152template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000153 typedef Defined<ELFT> Base;
154 typedef typename Base::Elf_Sym Elf_Sym;
155
Michael J. Spencer84487f12015-07-24 21:03:07 +0000156public:
Rafael Espindola832b93f2015-08-24 20:06:32 +0000157 explicit DefinedRegular(StringRef N, const Elf_Sym &Sym,
158 SectionChunk<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000159 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000160
161 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000162 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000163 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000164
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000165 const SectionChunk<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000166};
167
Michael J. Spencer84487f12015-07-24 21:03:07 +0000168// Undefined symbols.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000169class SyntheticUndefined : public SymbolBody {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000170public:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000171 explicit SyntheticUndefined(StringRef N)
172 : SymbolBody(UndefinedKind, N, false) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000173
174 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000175 return S->kind() == UndefinedSyntheticKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000176 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000177};
178
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000179template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
180 typedef ELFSymbolBody<ELFT> Base;
181 typedef typename Base::Elf_Sym Elf_Sym;
182
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000183public:
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000184 explicit Undefined(StringRef N, const Elf_Sym &Sym)
185 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000186
187 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000188 return S->kind() == Base::UndefinedKind;
189 }
190};
191
Michael J. Spencer84487f12015-07-24 21:03:07 +0000192} // namespace elf2
193} // namespace lld
194
195#endif