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