blob: 975302ede9d77c11285deb9013764e82e96ca272 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- Symbols.h ----------------------------------------------------------===//
2//
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;
24
25// A real symbol object, SymbolBody, is usually accessed indirectly
26// through a Symbol. There's always one Symbol for each symbol name.
27// The resolver updates SymbolBody pointers as it resolves symbols.
28struct Symbol {
29 explicit Symbol(SymbolBody *P) : Body(P) {}
30 SymbolBody *Body;
31};
32
33// The base class for real symbol classes.
34class SymbolBody {
35public:
36 enum Kind {
37 DefinedFirst,
38 DefinedRegularKind,
39 DefinedLast,
40 UndefinedKind,
41 };
42
43 Kind kind() const { return SymbolKind; }
44 virtual ~SymbolBody() {}
45
46 // Returns true if this is an external symbol.
47 virtual bool isExternal() { return true; }
48
49 // Returns the symbol name.
50 virtual StringRef getName() = 0;
51
52 // A SymbolBody has a backreference to a Symbol. Originally they are
53 // doubly-linked. A backreference will never change. But the pointer
54 // in the Symbol may be mutated by the resolver. If you have a
55 // pointer P to a SymbolBody and are not sure whether the resolver
56 // has chosen the object among other objects having the same name,
57 // you can access P->Backref->Body to get the resolver's result.
58 void setBackref(Symbol *P) { Backref = P; }
59 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
60
61 // Decides which symbol should "win" in the symbol table, this or
62 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
63 // they are duplicate (conflicting) symbols.
64 virtual int compare(SymbolBody *Other) = 0;
65
66protected:
67 SymbolBody(Kind K) : SymbolKind(K) {}
68
69private:
70 const Kind SymbolKind;
71 Symbol *Backref = nullptr;
72};
73
74// The base class for any defined symbols, including absolute symbols,
75// etc.
76class Defined : public SymbolBody {
77public:
78 Defined(Kind K) : SymbolBody(K) {}
79
80 static bool classof(const SymbolBody *S) {
81 Kind K = S->kind();
82 return DefinedFirst <= K && K <= DefinedLast;
83 }
84
85 int compare(SymbolBody *Other) override;
86};
87
88// Regular defined symbols read from object file symbol tables.
89template <class ELFT> class DefinedRegular : public Defined {
90public:
91 DefinedRegular(StringRef Name);
92
93 static bool classof(const SymbolBody *S) {
94 return S->kind() == DefinedRegularKind;
95 }
96
97 StringRef getName() override;
98 int compare(SymbolBody *Other) override;
99
100private:
101 StringRef Name;
102};
103
104// Undefined symbols.
105class Undefined : public SymbolBody {
106public:
107 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind), Name(N) {}
108
109 static bool classof(const SymbolBody *S) {
110 return S->kind() == UndefinedKind;
111 }
112 StringRef getName() override { return Name; }
113
114 int compare(SymbolBody *Other) override;
115
116private:
117 StringRef Name;
118};
119
120} // namespace elf2
121} // namespace lld
122
123#endif