blob: 1707047d5c109895d5970f52c19fea8b6badf654 [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;
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 {
38 DefinedFirst,
39 DefinedRegularKind,
40 DefinedLast,
41 UndefinedKind,
42 };
43
Michael J. Spencercdae0a42015-07-28 22:58:25 +000044 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000045
Michael J. Spencer84487f12015-07-24 21:03:07 +000046 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000047 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000048
49 // A SymbolBody has a backreference to a Symbol. Originally they are
50 // doubly-linked. A backreference will never change. But the pointer
51 // in the Symbol may be mutated by the resolver. If you have a
52 // pointer P to a SymbolBody and are not sure whether the resolver
53 // has chosen the object among other objects having the same name,
54 // you can access P->Backref->Body to get the resolver's result.
55 void setBackref(Symbol *P) { Backref = P; }
56 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
57
58 // Decides which symbol should "win" in the symbol table, this or
59 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
60 // they are duplicate (conflicting) symbols.
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000061 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000062
63protected:
Michael J. Spencercdae0a42015-07-28 22:58:25 +000064 SymbolBody(Kind K, StringRef N = "")
Rafael Espindolae3335d82015-08-05 13:26:54 +000065 : SymbolKind(K), Name(N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000066
Michael J. Spencercdae0a42015-07-28 22:58:25 +000067protected:
68 const unsigned SymbolKind : 8;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000069 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000070 Symbol *Backref = nullptr;
71};
72
73// The base class for any defined symbols, including absolute symbols,
74// etc.
75class Defined : public SymbolBody {
76public:
Michael J. Spencercdae0a42015-07-28 22:58:25 +000077 Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000078
79 static bool classof(const SymbolBody *S) {
80 Kind K = S->kind();
81 return DefinedFirst <= K && K <= DefinedLast;
82 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000083};
84
85// Regular defined symbols read from object file symbol tables.
86template <class ELFT> class DefinedRegular : public Defined {
Michael J. Spencercdae0a42015-07-28 22:58:25 +000087 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
88
Michael J. Spencer84487f12015-07-24 21:03:07 +000089public:
Rafael Espindoladf1e05a2015-08-06 15:33:21 +000090 DefinedRegular(StringRef Name);
Michael J. Spencer84487f12015-07-24 21:03:07 +000091
92 static bool classof(const SymbolBody *S) {
93 return S->kind() == DefinedRegularKind;
94 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000095};
96
97// Undefined symbols.
98class Undefined : public SymbolBody {
99public:
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000100 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000101
102 static bool classof(const SymbolBody *S) {
103 return S->kind() == UndefinedKind;
104 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000105};
106
107} // namespace elf2
108} // namespace lld
109
110#endif