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" |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 16 | #include "llvm/Object/Archive.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ELF.h" |
| 18 | |
| 19 | namespace lld { |
| 20 | namespace elf2 { |
| 21 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 22 | class ArchiveFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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, |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 44 | SharedKind = 3, |
| 45 | DefinedLast = 3, |
| 46 | UndefinedKind = 4, |
| 47 | LazyKind = 5, |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 50 | Kind kind() const { return static_cast<Kind>(SymbolKind); } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 51 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 52 | bool isWeak() const { return IsWeak; } |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 53 | bool isUndefined() const { return SymbolKind == UndefinedKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 54 | bool isDefined() const { return SymbolKind <= DefinedLast; } |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 55 | bool isStrongUndefined() const { return !IsWeak && isUndefined(); } |
| 56 | bool isCommon() const { return SymbolKind == DefinedCommonKind; } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 57 | bool isLazy() const { return SymbolKind == LazyKind; } |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 58 | bool isShared() const { return SymbolKind == SharedKind; } |
| 59 | bool isUsedInRegularObj() const { return IsUsedInRegularObj; } |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 60 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 61 | // Returns the symbol name. |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 62 | StringRef getName() const { return Name; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 63 | |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 64 | uint8_t getMostConstrainingVisibility() const { |
| 65 | return MostConstrainingVisibility; |
| 66 | } |
| 67 | |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame^] | 68 | unsigned getDynamicSymbolTableIndex() const { |
| 69 | return DynamicSymbolTableIndex; |
| 70 | } |
| 71 | void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; } |
| 72 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 73 | // A SymbolBody has a backreference to a Symbol. Originally they are |
| 74 | // doubly-linked. A backreference will never change. But the pointer |
| 75 | // in the Symbol may be mutated by the resolver. If you have a |
| 76 | // pointer P to a SymbolBody and are not sure whether the resolver |
| 77 | // has chosen the object among other objects having the same name, |
| 78 | // you can access P->Backref->Body to get the resolver's result. |
| 79 | void setBackref(Symbol *P) { Backref = P; } |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 80 | SymbolBody *getReplacement() { return Backref ? Backref->Body : this; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 81 | |
| 82 | // Decides which symbol should "win" in the symbol table, this or |
| 83 | // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if |
| 84 | // they are duplicate (conflicting) symbols. |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 85 | template <class ELFT> int compare(SymbolBody *Other); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 86 | |
| 87 | protected: |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 88 | SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility) |
| 89 | : SymbolKind(K), IsWeak(IsWeak), MostConstrainingVisibility(Visibility), |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 90 | Name(Name) { |
| 91 | IsUsedInRegularObj = K != SharedKind && K != LazyKind; |
| 92 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 93 | |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 94 | const unsigned SymbolKind : 8; |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 95 | const unsigned IsWeak : 1; |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 96 | unsigned MostConstrainingVisibility : 2; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 97 | unsigned IsUsedInRegularObj : 1; |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame^] | 98 | unsigned DynamicSymbolTableIndex = 0; |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 99 | StringRef Name; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 100 | Symbol *Backref = nullptr; |
| 101 | }; |
| 102 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 103 | // This is for symbols created from elf files and not from the command line. |
| 104 | // Since they come from object files, they have a Elf_Sym. |
| 105 | // |
| 106 | // FIXME: Another alternative is to give every symbol an Elf_Sym. To do that |
| 107 | // we have to delay creating the symbol table until the output format is |
| 108 | // known and some of its methods will be templated. We should experiment with |
| 109 | // that once we have a bit more code. |
| 110 | template <class ELFT> class ELFSymbolBody : public SymbolBody { |
| 111 | protected: |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 112 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 113 | ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym) |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 114 | : SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK, |
| 115 | Sym.getVisibility()), |
| 116 | Sym(Sym) {} |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 117 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 118 | public: |
| 119 | const Elf_Sym &Sym; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 120 | |
| 121 | static bool classof(const SymbolBody *S) { |
| 122 | Kind K = S->kind(); |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 123 | return K >= DefinedFirst && K <= UndefinedKind; |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | // The base class for any defined symbols, including absolute symbols, |
| 128 | // etc. |
| 129 | template <class ELFT> class Defined : public ELFSymbolBody<ELFT> { |
| 130 | typedef ELFSymbolBody<ELFT> Base; |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 131 | |
| 132 | protected: |
| 133 | typedef typename Base::Kind Kind; |
| 134 | typedef typename Base::Elf_Sym Elf_Sym; |
| 135 | |
| 136 | public: |
| 137 | explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym) |
| 138 | : ELFSymbolBody<ELFT>(K, N, Sym) {} |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 139 | |
| 140 | static bool classof(const SymbolBody *S) { return S->isDefined(); } |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | template <class ELFT> class DefinedAbsolute : public Defined<ELFT> { |
| 144 | typedef ELFSymbolBody<ELFT> Base; |
| 145 | typedef typename Base::Elf_Sym Elf_Sym; |
| 146 | |
| 147 | public: |
| 148 | explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym) |
| 149 | : Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {} |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 150 | |
| 151 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 152 | return S->kind() == Base::DefinedAbsoluteKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 153 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 156 | template <class ELFT> class DefinedCommon : public Defined<ELFT> { |
| 157 | typedef ELFSymbolBody<ELFT> Base; |
| 158 | typedef typename Base::Elf_Sym Elf_Sym; |
| 159 | |
| 160 | public: |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 161 | typedef typename std::conditional<ELFT::Is64Bits, uint64_t, uint32_t>::type |
| 162 | uintX_t; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 163 | explicit DefinedCommon(StringRef N, const Elf_Sym &Sym) |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 164 | : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) { |
| 165 | MaxAlignment = Sym.st_value; |
| 166 | } |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 167 | |
| 168 | static bool classof(const SymbolBody *S) { |
| 169 | return S->kind() == Base::DefinedCommonKind; |
| 170 | } |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 171 | |
| 172 | // The output offset of this common symbol in the output bss. Computed by the |
| 173 | // writer. |
| 174 | uintX_t OffsetInBSS; |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 175 | |
| 176 | // The maximum alignment we have seen for this symbol. |
| 177 | uintX_t MaxAlignment; |
Rafael Espindola | 51d4690 | 2015-08-28 21:26:51 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 180 | // Regular defined symbols read from object file symbol tables. |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 181 | template <class ELFT> class DefinedRegular : public Defined<ELFT> { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 182 | typedef Defined<ELFT> Base; |
| 183 | typedef typename Base::Elf_Sym Elf_Sym; |
| 184 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 185 | public: |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 186 | explicit DefinedRegular(StringRef N, const Elf_Sym &Sym, |
| 187 | SectionChunk<ELFT> &Section) |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 188 | : Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 189 | |
| 190 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 191 | return S->kind() == Base::DefinedRegularKind; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 192 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 193 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 194 | const SectionChunk<ELFT> &Section; |
Rafael Espindola | b13df65 | 2015-08-11 17:33:02 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 197 | // Undefined symbol. |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 198 | template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> { |
| 199 | typedef ELFSymbolBody<ELFT> Base; |
| 200 | typedef typename Base::Elf_Sym Elf_Sym; |
| 201 | |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 202 | public: |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 203 | static Elf_Sym Synthetic; |
| 204 | |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 205 | explicit Undefined(StringRef N, const Elf_Sym &Sym) |
| 206 | : ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {} |
Rafael Espindola | 76e24ea | 2015-08-11 17:57:05 +0000 | [diff] [blame] | 207 | |
| 208 | static bool classof(const SymbolBody *S) { |
Rafael Espindola | 1bd885a | 2015-08-14 16:46:28 +0000 | [diff] [blame] | 209 | return S->kind() == Base::UndefinedKind; |
| 210 | } |
| 211 | }; |
| 212 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 213 | template <class ELFT> |
| 214 | typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Synthetic; |
| 215 | |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 216 | template <class ELFT> class SharedSymbol : public ELFSymbolBody<ELFT> { |
| 217 | typedef ELFSymbolBody<ELFT> Base; |
| 218 | typedef typename Base::Elf_Sym Elf_Sym; |
| 219 | |
| 220 | public: |
| 221 | static bool classof(const SymbolBody *S) { |
| 222 | return S->kind() == Base::SharedKind; |
| 223 | } |
| 224 | |
| 225 | SharedSymbol(StringRef Name, const Elf_Sym &Sym) |
| 226 | : ELFSymbolBody<ELFT>(Base::SharedKind, Name, Sym) {} |
| 227 | }; |
| 228 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 229 | // This class represents a symbol defined in an archive file. It is |
| 230 | // created from an archive file header, and it knows how to load an |
| 231 | // object file from an archive to replace itself with a defined |
| 232 | // symbol. If the resolver finds both Undefined and Lazy for |
| 233 | // the same name, it will ask the Lazy to load a file. |
| 234 | class Lazy : public SymbolBody { |
| 235 | public: |
| 236 | Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S) |
| 237 | : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT), |
| 238 | File(F), Sym(S) {} |
| 239 | |
| 240 | static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; } |
| 241 | |
| 242 | // Returns an object file for this symbol, or a nullptr if the file |
| 243 | // was already returned. |
| 244 | std::unique_ptr<InputFile> getMember(); |
| 245 | |
| 246 | private: |
| 247 | ArchiveFile *File; |
| 248 | const llvm::object::Archive::Symbol Sym; |
| 249 | }; |
| 250 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 251 | } // namespace elf2 |
| 252 | } // namespace lld |
| 253 | |
| 254 | #endif |