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