blob: 18e31dacc2f39d4067d10b6d1d698d8321289ef8 [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 {
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,
42 UndefinedKind = 2
Michael J. Spencer84487f12015-07-24 21:03:07 +000043 };
44
Michael J. Spencercdae0a42015-07-28 22:58:25 +000045 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000046
Michael J. Spencer84487f12015-07-24 21:03:07 +000047 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000048 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000049
50 // A SymbolBody has a backreference to a Symbol. Originally they are
51 // doubly-linked. A backreference will never change. But the pointer
52 // in the Symbol may be mutated by the resolver. If you have a
53 // pointer P to a SymbolBody and are not sure whether the resolver
54 // has chosen the object among other objects having the same name,
55 // you can access P->Backref->Body to get the resolver's result.
56 void setBackref(Symbol *P) { Backref = P; }
57 SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
58
59 // Decides which symbol should "win" in the symbol table, this or
60 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
61 // they are duplicate (conflicting) symbols.
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000062 int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +000063
64protected:
Michael J. Spencercdae0a42015-07-28 22:58:25 +000065 SymbolBody(Kind K, StringRef N = "")
Rafael Espindolae3335d82015-08-05 13:26:54 +000066 : SymbolKind(K), Name(N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000067
Michael J. Spencercdae0a42015-07-28 22:58:25 +000068protected:
69 const unsigned SymbolKind : 8;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000070 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +000071 Symbol *Backref = nullptr;
72};
73
74// The base class for any defined symbols, including absolute symbols,
75// etc.
76class Defined : public SymbolBody {
77public:
Michael J. Spencercdae0a42015-07-28 22:58:25 +000078 Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000079
80 static bool classof(const SymbolBody *S) {
81 Kind K = S->kind();
82 return DefinedFirst <= K && K <= DefinedLast;
83 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000084};
85
86// Regular defined symbols read from object file symbol tables.
87template <class ELFT> class DefinedRegular : public Defined {
88public:
Rafael Espindoladf1e05a2015-08-06 15:33:21 +000089 DefinedRegular(StringRef Name);
Michael J. Spencer84487f12015-07-24 21:03:07 +000090
91 static bool classof(const SymbolBody *S) {
92 return S->kind() == DefinedRegularKind;
93 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000094};
95
Rafael Espindolab13df652015-08-11 17:33:02 +000096template <class ELFT> class DefinedWeak : public Defined {
97public:
98 DefinedWeak(StringRef Name);
99
100 static bool classof(const SymbolBody *S) {
101 return S->kind() == DefinedWeakKind;
102 }
103};
104
Michael J. Spencer84487f12015-07-24 21:03:07 +0000105// Undefined symbols.
106class Undefined : public SymbolBody {
107public:
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000108 explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000109
110 static bool classof(const SymbolBody *S) {
111 return S->kind() == UndefinedKind;
112 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000113};
114
115} // namespace elf2
116} // namespace lld
117
118#endif