blob: 82bf354dee7431ccf269cdcbcf781a694410c37e [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
13#include "lld/Core/LLVM.h"
14#include "llvm/Object/ELF.h"
15
16namespace lld {
17namespace elf2 {
18
19using llvm::object::ELFFile;
20
21class Chunk;
22class InputFile;
23class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000024template <class ELFT> class ObjectFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26// A real symbol object, SymbolBody, is usually accessed indirectly
27// through a Symbol. There's always one Symbol for each symbol name.
28// The resolver updates SymbolBody pointers as it resolves symbols.
29struct Symbol {
30 explicit Symbol(SymbolBody *P) : Body(P) {}
31 SymbolBody *Body;
32};
33
34// The base class for real symbol classes.
35class SymbolBody {
36public:
37 enum Kind {
Rafael Espindolaae1b23b2015-08-11 17:10:02 +000038 DefinedFirst = 0,
39 DefinedRegularKind = 0,
Rafael Espindolab13df652015-08-11 17:33:02 +000040 DefinedWeakKind = 1,
41 DefinedLast = 1,
Rafael Espindola76e24ea2015-08-11 17:57:05 +000042 UndefinedWeakKind = 2,
Rafael Espindola1bd885a2015-08-14 16:46:28 +000043 UndefinedKind = 3,
44 UndefinedSyntheticKind = 4
Michael J. Spencer84487f12015-07-24 21:03:07 +000045 };
46
Michael J. Spencercdae0a42015-07-28 22:58:25 +000047 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000048
Rafael Espindola1bd885a2015-08-14 16:46:28 +000049 bool isStrongUndefined() {
50 return SymbolKind == UndefinedKind || SymbolKind == UndefinedSyntheticKind;
51 }
52
Michael J. Spencer84487f12015-07-24 21:03:07 +000053 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000054 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000055
56 // A SymbolBody has a backreference to a Symbol. Originally they are
57 // doubly-linked. A backreference will never change. But the pointer
58 // in the Symbol may be mutated by the resolver. If you have a
59 // pointer P to a SymbolBody and are not sure whether the resolver
60 // has chosen the object among other objects having the same name,
61 // you can access P->Backref->Body to get the resolver's result.
62 void setBackref(Symbol *P) { Backref = P; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000063
64 // Decides which symbol should "win" in the symbol table, this or
65 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
66 // they are duplicate (conflicting) symbols.
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000067 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000068
69protected:
Rafael Espindola3bf356e2015-08-14 14:38:44 +000070 SymbolBody(Kind K, StringRef Name) : SymbolKind(K), Name(Name) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000071
Michael J. Spencercdae0a42015-07-28 22:58:25 +000072protected:
73 const unsigned SymbolKind : 8;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000074 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000075 Symbol *Backref = nullptr;
76};
77
Rafael Espindola1bd885a2015-08-14 16:46:28 +000078// This is for symbols created from elf files and not from the command line.
79// Since they come from object files, they have a Elf_Sym.
80//
81// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
82// we have to delay creating the symbol table until the output format is
83// known and some of its methods will be templated. We should experiment with
84// that once we have a bit more code.
85template <class ELFT> class ELFSymbolBody : public SymbolBody {
86protected:
Rafael Espindolac44d17a2015-08-14 15:10:49 +000087 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +000088 ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
89 : SymbolBody(K, Name), Sym(Sym) {}
Rafael Espindolac44d17a2015-08-14 15:10:49 +000090
Rafael Espindola1bd885a2015-08-14 16:46:28 +000091public:
92 const Elf_Sym &Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +000093
94 static bool classof(const SymbolBody *S) {
95 Kind K = S->kind();
Rafael Espindola1bd885a2015-08-14 16:46:28 +000096 return K >= DefinedFirst && K <= UndefinedKind;
97 }
98};
99
100// The base class for any defined symbols, including absolute symbols,
101// etc.
102template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
103 typedef ELFSymbolBody<ELFT> Base;
104 typedef typename Base::Kind Kind;
105
106public:
107 typedef typename Base::Elf_Sym Elf_Sym;
108
109 explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
110 : ELFSymbolBody<ELFT>(K, N, Sym) {}
111
112 static bool classof(const SymbolBody *S) {
113 Kind K = S->kind();
114 return Base::DefinedFirst <= K && K <= Base::DefinedLast;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000115 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000116};
117
118// Regular defined symbols read from object file symbol tables.
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000119template <class ELFT> class DefinedRegular : public Defined<ELFT> {
120 typedef Defined<ELFT> Base;
121 typedef typename Base::Elf_Sym Elf_Sym;
122
Michael J. Spencer84487f12015-07-24 21:03:07 +0000123public:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000124 explicit DefinedRegular(StringRef N, const Elf_Sym &Sym)
125 : Defined<ELFT>(Base::DefinedRegularKind, N, Sym) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000126
127 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000128 return S->kind() == Base::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000129 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000130};
131
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000132template <class ELFT> class DefinedWeak : public Defined<ELFT> {
133 typedef Defined<ELFT> Base;
134 typedef typename Base::Elf_Sym Elf_Sym;
135
Rafael Espindolab13df652015-08-11 17:33:02 +0000136public:
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000137 explicit DefinedWeak(StringRef N, const Elf_Sym &Sym)
138 : Defined<ELFT>(Base::DefinedWeakKind, N, Sym) {}
Rafael Espindolab13df652015-08-11 17:33:02 +0000139
140 static bool classof(const SymbolBody *S) {
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000141 return S->kind() == Base::DefinedWeakKind;
Rafael Espindolab13df652015-08-11 17:33:02 +0000142 }
143};
144
Michael J. Spencer84487f12015-07-24 21:03:07 +0000145// Undefined symbols.
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000146class SyntheticUndefined : public SymbolBody {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000147public:
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000148 explicit SyntheticUndefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000149
150 static bool classof(const SymbolBody *S) {
151 return S->kind() == UndefinedKind;
152 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000153};
154
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000155template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
156 typedef ELFSymbolBody<ELFT> Base;
157 typedef typename Base::Elf_Sym Elf_Sym;
158
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000159public:
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000160 explicit Undefined(StringRef N, const Elf_Sym &Sym)
161 : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000162
163 static bool classof(const SymbolBody *S) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000164 return S->kind() == Base::UndefinedKind;
165 }
166};
167
168template <class ELFT> class UndefinedWeak : public ELFSymbolBody<ELFT> {
169 typedef ELFSymbolBody<ELFT> Base;
170 typedef typename Base::Elf_Sym Elf_Sym;
171
172public:
173 explicit UndefinedWeak(StringRef N, const Elf_Sym &Sym)
174 : ELFSymbolBody<ELFT>(Base::UndefinedWeakKind, N, Sym) {}
175
176 static bool classof(const SymbolBody *S) {
177 return S->kind() == Base::UndefinedWeakKind;
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000178 }
179};
180
Michael J. Spencer84487f12015-07-24 21:03:07 +0000181} // namespace elf2
182} // namespace lld
183
184#endif