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, |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 43 | UndefinedKind = 3, |
| 44 | UndefinedSyntheticKind = 4 |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 47 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 48 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 49 | bool isStrongUndefined() { |
| 50 | return SymbolKind == UndefinedKind || SymbolKind == UndefinedSyntheticKind; |
| 51 | } |
| 52 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 53 | // Returns the symbol name. |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 54 | StringRef getName() const { return Name; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 55 | |
| 56 | // A SymbolBody has a backreference to a Symbol. Originally they are |
| 57 | // doubly-linked. A backreference will never change. But the pointer |
| 58 | // in the Symbol may be mutated by the resolver. If you have a |
| 59 | // pointer P to a SymbolBody and are not sure whether the resolver |
| 60 | // has chosen the object among other objects having the same name, |
| 61 | // you can access P->Backref->Body to get the resolver's result. |
| 62 | void setBackref(Symbol *P) { Backref = P; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 63 | |
| 64 | // Decides which symbol should "win" in the symbol table, this or |
| 65 | // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if |
| 66 | // they are duplicate (conflicting) symbols. |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 67 | int compare(SymbolBody *Other); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 68 | |
| 69 | protected: |
Rafael Espindola | 3bf356e | 2015-08-14 14:38:44 +0000 | [diff] [blame] | 70 | SymbolBody(Kind K, StringRef Name) : SymbolKind(K), Name(Name) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 71 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 72 | protected: |
| 73 | const unsigned SymbolKind : 8; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 74 | StringRef Name; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 75 | Symbol *Backref = nullptr; |
| 76 | }; |
| 77 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 78 | // This is for symbols created from elf files and not from the command line. |
| 79 | // Since they come from object files, they have a Elf_Sym. |
| 80 | // |
| 81 | // FIXME: Another alternative is to give every symbol an Elf_Sym. To do that |
| 82 | // we have to delay creating the symbol table until the output format is |
| 83 | // known and some of its methods will be templated. We should experiment with |
| 84 | // that once we have a bit more code. |
| 85 | template <class ELFT> class ELFSymbolBody : public SymbolBody { |
| 86 | protected: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 87 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 88 | ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym) |
| 89 | : SymbolBody(K, Name), Sym(Sym) {} |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 90 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 91 | public: |
| 92 | const Elf_Sym &Sym; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 93 | |
| 94 | static bool classof(const SymbolBody *S) { |
| 95 | Kind K = S->kind(); |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 96 | return K >= DefinedFirst && K <= UndefinedKind; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | // The base class for any defined symbols, including absolute symbols, |
| 101 | // etc. |
| 102 | template <class ELFT> class Defined : public ELFSymbolBody<ELFT> { |
| 103 | typedef ELFSymbolBody<ELFT> Base; |
| 104 | typedef typename Base::Kind Kind; |
| 105 | |
| 106 | public: |
| 107 | typedef typename Base::Elf_Sym Elf_Sym; |
| 108 | |
| 109 | explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym) |
| 110 | : ELFSymbolBody<ELFT>(K, N, Sym) {} |
| 111 | |
| 112 | static bool classof(const SymbolBody *S) { |
| 113 | Kind K = S->kind(); |
| 114 | return Base::DefinedFirst <= K && K <= Base::DefinedLast; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 115 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // Regular defined symbols read from object file symbol tables. |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 119 | template <class ELFT> class DefinedRegular : public Defined<ELFT> { |
| 120 | typedef Defined<ELFT> Base; |
| 121 | typedef typename Base::Elf_Sym Elf_Sym; |
| 122 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 123 | public: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 124 | explicit DefinedRegular(StringRef N, const Elf_Sym &Sym) |
| 125 | : Defined<ELFT>(Base::DefinedRegularKind, N, Sym) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 126 | |
| 127 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 128 | return S->kind() == Base::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 129 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 132 | template <class ELFT> class DefinedWeak : public Defined<ELFT> { |
| 133 | typedef Defined<ELFT> Base; |
| 134 | typedef typename Base::Elf_Sym Elf_Sym; |
| 135 | |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 136 | public: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 137 | explicit DefinedWeak(StringRef N, const Elf_Sym &Sym) |
| 138 | : Defined<ELFT>(Base::DefinedWeakKind, N, Sym) {} |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 139 | |
| 140 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 141 | return S->kind() == Base::DefinedWeakKind; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 145 | // Undefined symbols. |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 146 | class SyntheticUndefined : public SymbolBody { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 147 | public: |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 148 | explicit SyntheticUndefined(StringRef N) : SymbolBody(UndefinedKind, N) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 149 | |
| 150 | static bool classof(const SymbolBody *S) { |
| 151 | return S->kind() == UndefinedKind; |
| 152 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 155 | template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> { |
| 156 | typedef ELFSymbolBody<ELFT> Base; |
| 157 | typedef typename Base::Elf_Sym Elf_Sym; |
| 158 | |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 159 | public: |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 160 | explicit Undefined(StringRef N, const Elf_Sym &Sym) |
| 161 | : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {} |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 162 | |
| 163 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 164 | return S->kind() == Base::UndefinedKind; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | template <class ELFT> class UndefinedWeak : public ELFSymbolBody<ELFT> { |
| 169 | typedef ELFSymbolBody<ELFT> Base; |
| 170 | typedef typename Base::Elf_Sym Elf_Sym; |
| 171 | |
| 172 | public: |
| 173 | explicit UndefinedWeak(StringRef N, const Elf_Sym &Sym) |
| 174 | : ELFSymbolBody<ELFT>(Base::UndefinedWeakKind, N, Sym) {} |
| 175 | |
| 176 | static bool classof(const SymbolBody *S) { |
| 177 | return S->kind() == Base::UndefinedWeakKind; |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 178 | } |
| 179 | }; |
| 180 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 181 | } // namespace elf2 |
| 182 | } // namespace lld |
| 183 | |
| 184 | #endif |