blob: 9360dcbd96c1ac0d096aef174d6b651615a94551 [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(); }
Rafael Espindola30e17972015-08-30 23:17:30 +000056 bool isStrongUndefined() const { return !IsWeak && isUndefined(); }
57 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000058
Michael J. Spencer84487f12015-07-24 21:03:07 +000059 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000060 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000061
62 // A SymbolBody has a backreference to a Symbol. Originally they are
63 // doubly-linked. A backreference will never change. But the pointer
64 // in the Symbol may be mutated by the resolver. If you have a
65 // pointer P to a SymbolBody and are not sure whether the resolver
66 // has chosen the object among other objects having the same name,
67 // you can access P->Backref->Body to get the resolver's result.
68 void setBackref(Symbol *P) { Backref = P; }
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000069 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000070
71 // Decides which symbol should "win" in the symbol table, this or
72 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
73 // they are duplicate (conflicting) symbols.
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000074 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000075
76protected:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000077 SymbolBody(Kind K, StringRef Name, bool IsWeak)
78 : SymbolKind(K), IsWeak(IsWeak), Name(Name) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000079
Michael J. Spencercdae0a42015-07-28 22:58:25 +000080protected:
81 const unsigned SymbolKind : 8;
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000082 const unsigned IsWeak : 1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000083 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000084 Symbol *Backref = nullptr;
85};
86
Rafael Espindola1bd885a2015-08-14 16:46:28 +000087// This is for symbols created from elf files and not from the command line.
88// Since they come from object files, they have a Elf_Sym.
89//
90// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
91// we have to delay creating the symbol table until the output format is
92// known and some of its methods will be templated. We should experiment with
93// that once we have a bit more code.
94template <class ELFT> class ELFSymbolBody : public SymbolBody {
95protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +000096 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +000097 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000098 : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK), Sym(Sym) {
99 }
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000100
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000101public:
102 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000103
104 static bool classof(const SymbolBody *S) {
105 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000106 return K >= DefinedFirst && K <= UndefinedKind;
107 }
108};
109
110// The base class for any defined symbols, including absolute symbols,
111// etc.
112template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
113 typedef ELFSymbolBody<ELFT> Base;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000114
115protected:
116 typedef typename Base::Kind Kind;
117 typedef typename Base::Elf_Sym Elf_Sym;
118
119public:
120 explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
121 : ELFSymbolBody<ELFT>(K, N, Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000122
123 static bool classof(const SymbolBody *S) { return S->isDefined(); }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000124};
125
126template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
127 typedef ELFSymbolBody<ELFT> Base;
128 typedef typename Base::Elf_Sym Elf_Sym;
129
130public:
131 explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
132 : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000133
134 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000135 return S->kind() == Base::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000136 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000137};
138
Rafael Espindola51d46902015-08-28 21:26:51 +0000139template <class ELFT> class DefinedCommon : public Defined<ELFT> {
140 typedef ELFSymbolBody<ELFT> Base;
141 typedef typename Base::Elf_Sym Elf_Sym;
142
143public:
144 explicit DefinedCommon(StringRef N, const Elf_Sym &Sym)
145 : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {}
146
147 static bool classof(const SymbolBody *S) {
148 return S->kind() == Base::DefinedCommonKind;
149 }
150};
151
Michael J. Spencer84487f12015-07-24 21:03:07 +0000152// Regular defined symbols read from object file symbol tables.
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000153template <class ELFT> class DefinedRegular : public Defined<ELFT> {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000154 typedef Defined<ELFT> Base;
155 typedef typename Base::Elf_Sym Elf_Sym;
156
Michael J. Spencer84487f12015-07-24 21:03:07 +0000157public:
Rafael Espindola832b93f2015-08-24 20:06:32 +0000158 explicit DefinedRegular(StringRef N, const Elf_Sym &Sym,
159 SectionChunk<ELFT> &Section)
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000160 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000161
162 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000163 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000164 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000165
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000166 const SectionChunk<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000167};
168
Michael J. Spencer84487f12015-07-24 21:03:07 +0000169// Undefined symbols.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000170class SyntheticUndefined : public SymbolBody {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000171public:
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000172 explicit SyntheticUndefined(StringRef N)
173 : SymbolBody(UndefinedKind, N, false) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000174
175 static bool classof(const SymbolBody *S) {
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000176 return S->kind() == UndefinedSyntheticKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000177 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000178};
179
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000180template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
181 typedef ELFSymbolBody<ELFT> Base;
182 typedef typename Base::Elf_Sym Elf_Sym;
183
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000184public:
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000185 explicit Undefined(StringRef N, const Elf_Sym &Sym)
186 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000187
188 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000189 return S->kind() == Base::UndefinedKind;
190 }
191};
192
Michael J. Spencer84487f12015-07-24 21:03:07 +0000193} // namespace elf2
194} // namespace lld
195
196#endif