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