Rafael Espindola | beee25e | 2015-08-14 14:12:54 +0000 | [diff] [blame] | 1 | //===- Symbols.h ------------------------------------------------*- C++ -*-===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 24 | template <class ELFT> class ObjectFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 25 | |
| 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. |
| 29 | struct Symbol { |
| 30 | explicit Symbol(SymbolBody *P) : Body(P) {} |
| 31 | SymbolBody *Body; |
| 32 | }; |
| 33 | |
| 34 | // The base class for real symbol classes. |
| 35 | class SymbolBody { |
| 36 | public: |
| 37 | enum Kind { |
Rafael Espindola | ae1b23b | 2015-08-11 17:10:02 +0000 | [diff] [blame] | 38 | DefinedFirst = 0, |
| 39 | DefinedRegularKind = 0, |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 40 | DefinedWeakKind = 1, |
| 41 | DefinedLast = 1, |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 42 | UndefinedWeakKind = 2, |
| 43 | UndefinedKind = 3 |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 46 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 47 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 48 | // Returns the symbol name. |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 49 | StringRef getName() const { return Name; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 50 | |
| 51 | // A SymbolBody has a backreference to a Symbol. Originally they are |
| 52 | // doubly-linked. A backreference will never change. But the pointer |
| 53 | // in the Symbol may be mutated by the resolver. If you have a |
| 54 | // pointer P to a SymbolBody and are not sure whether the resolver |
| 55 | // has chosen the object among other objects having the same name, |
| 56 | // you can access P->Backref->Body to get the resolver's result. |
| 57 | void setBackref(Symbol *P) { Backref = P; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 62 | int compare(SymbolBody *Other); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 63 | |
| 64 | protected: |
Rafael Espindola | 3bf356e | 2015-08-14 14:38:44 +0000 | [diff] [blame] | 65 | SymbolBody(Kind K, StringRef Name) : SymbolKind(K), Name(Name) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 66 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 67 | protected: |
| 68 | const unsigned SymbolKind : 8; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 69 | StringRef Name; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 70 | Symbol *Backref = nullptr; |
| 71 | }; |
| 72 | |
| 73 | // The base class for any defined symbols, including absolute symbols, |
| 74 | // etc. |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 75 | template <class ELFT> class Defined : public SymbolBody { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 76 | public: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 77 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 78 | const Elf_Sym &Sym; |
| 79 | |
| 80 | explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym) |
| 81 | : SymbolBody(K, N), Sym(Sym) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 82 | |
| 83 | static bool classof(const SymbolBody *S) { |
| 84 | Kind K = S->kind(); |
| 85 | return DefinedFirst <= K && K <= DefinedLast; |
| 86 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | // Regular defined symbols read from object file symbol tables. |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 90 | template <class ELFT> class DefinedRegular : public Defined<ELFT> { |
| 91 | typedef Defined<ELFT> Base; |
| 92 | typedef typename Base::Elf_Sym Elf_Sym; |
| 93 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 94 | public: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 95 | explicit DefinedRegular(StringRef N, const Elf_Sym &Sym) |
| 96 | : Defined<ELFT>(Base::DefinedRegularKind, N, Sym) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 97 | |
| 98 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 99 | return S->kind() == Base::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 100 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 103 | template <class ELFT> class DefinedWeak : public Defined<ELFT> { |
| 104 | typedef Defined<ELFT> Base; |
| 105 | typedef typename Base::Elf_Sym Elf_Sym; |
| 106 | |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 107 | public: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 108 | explicit DefinedWeak(StringRef N, const Elf_Sym &Sym) |
| 109 | : Defined<ELFT>(Base::DefinedWeakKind, N, Sym) {} |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 110 | |
| 111 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame^] | 112 | return S->kind() == Base::DefinedWeakKind; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 113 | } |
| 114 | }; |
| 115 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 116 | // Undefined symbols. |
| 117 | class Undefined : public SymbolBody { |
| 118 | public: |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 119 | explicit Undefined(StringRef N) : SymbolBody(UndefinedKind, N) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 120 | |
| 121 | static bool classof(const SymbolBody *S) { |
| 122 | return S->kind() == UndefinedKind; |
| 123 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 126 | class UndefinedWeak : public SymbolBody { |
| 127 | public: |
| 128 | explicit UndefinedWeak(StringRef N) : SymbolBody(UndefinedWeakKind, N) {} |
| 129 | |
| 130 | static bool classof(const SymbolBody *S) { |
| 131 | return S->kind() == UndefinedWeakKind; |
| 132 | } |
| 133 | }; |
| 134 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 135 | } // namespace elf2 |
| 136 | } // namespace lld |
| 137 | |
| 138 | #endif |