Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 16 | namespace lld { |
| 17 | namespace elf2 { |
| 18 | |
| 19 | using llvm::object::ELFFile; |
| 20 | |
| 21 | class Chunk; |
| 22 | class InputFile; |
| 23 | class 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. |
| 28 | struct Symbol { |
| 29 | explicit Symbol(SymbolBody *P) : Body(P) {} |
| 30 | SymbolBody *Body; |
| 31 | }; |
| 32 | |
| 33 | // The base class for real symbol classes. |
| 34 | class SymbolBody { |
| 35 | public: |
| 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. |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame^] | 64 | int compare(SymbolBody *Other); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 65 | |
| 66 | protected: |
| 67 | SymbolBody(Kind K) : SymbolKind(K) {} |
| 68 | |
| 69 | private: |
| 70 | const Kind SymbolKind; |
| 71 | Symbol *Backref = nullptr; |
| 72 | }; |
| 73 | |
| 74 | // The base class for any defined symbols, including absolute symbols, |
| 75 | // etc. |
| 76 | class Defined : public SymbolBody { |
| 77 | public: |
| 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 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | // Regular defined symbols read from object file symbol tables. |
| 87 | template <class ELFT> class DefinedRegular : public Defined { |
| 88 | public: |
| 89 | DefinedRegular(StringRef Name); |
| 90 | |
| 91 | static bool classof(const SymbolBody *S) { |
| 92 | return S->kind() == DefinedRegularKind; |
| 93 | } |
| 94 | |
| 95 | StringRef getName() override; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | StringRef Name; |
| 99 | }; |
| 100 | |
| 101 | // Undefined symbols. |
| 102 | class Undefined : public SymbolBody { |
| 103 | public: |
| 104 | explicit Undefined(StringRef N) : SymbolBody(UndefinedKind), Name(N) {} |
| 105 | |
| 106 | static bool classof(const SymbolBody *S) { |
| 107 | return S->kind() == UndefinedKind; |
| 108 | } |
| 109 | StringRef getName() override { return Name; } |
| 110 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 111 | private: |
| 112 | StringRef Name; |
| 113 | }; |
| 114 | |
| 115 | } // namespace elf2 |
| 116 | } // namespace lld |
| 117 | |
| 118 | #endif |