| Rafael Espindola | beee25e | 2015-08-14 14:12:54 +0000 | [diff] [blame] | 1 | //===- SymbolTable.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_SYMBOL_TABLE_H |
| 11 | #define LLD_ELF_SYMBOL_TABLE_H |
| 12 | |
| 13 | #include "InputFiles.h" |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 14 | #include "LTO.h" |
| Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 15 | #include "lld/Common/Strings.h" |
| Justin Lebar | 3c11e93 | 2016-10-18 17:50:36 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/CachedHashString.h" |
| Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 18 | |
| 19 | namespace lld { |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 20 | namespace elf { |
| Rafael Espindola | d26b52f | 2017-12-09 16:56:18 +0000 | [diff] [blame] | 21 | class Defined; |
| 22 | class SectionBase; |
| Sam Clegg | c090962 | 2017-06-30 00:34:35 +0000 | [diff] [blame] | 23 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 24 | // SymbolTable is a bucket of all known symbols, including defined, |
| 25 | // undefined, or lazy symbols (the last one is symbols in archive |
| 26 | // files whose archive members are not yet loaded). |
| 27 | // |
| 28 | // We put all symbols of all files to a SymbolTable, and the |
| 29 | // SymbolTable selects the "best" symbols if there are name |
| 30 | // conflicts. For example, obviously, a defined symbol is better than |
| 31 | // an undefined symbol. Or, if there's a conflict between a lazy and a |
| 32 | // undefined, it'll read an archive member to read a real definition |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 33 | // to replace the lazy symbol. The logic is implemented in the |
| 34 | // add*() functions, which are called by input files as they are parsed. There |
| 35 | // is one add* function per symbol type. |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 36 | class SymbolTable { |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 37 | public: |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 38 | template <class ELFT> void addFile(InputFile *File); |
| 39 | template <class ELFT> void addCombinedLTOObject(); |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 40 | template <class ELFT> void addSymbolWrap(StringRef Name); |
| Rui Ueyama | dc0b0b0 | 2017-11-04 23:09:43 +0000 | [diff] [blame] | 41 | void applySymbolWrap(); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 42 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 43 | ArrayRef<Symbol *> getSymbols() const { return SymVector; } |
| Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 44 | |
| Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 +0000 | [diff] [blame] | 45 | Defined *addAbsolute(StringRef Name, |
| 46 | uint8_t Visibility = llvm::ELF::STV_HIDDEN, |
| 47 | uint8_t Binding = llvm::ELF::STB_GLOBAL); |
| Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 48 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 49 | template <class ELFT> Symbol *addUndefined(StringRef Name); |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 50 | template <class ELFT> |
| Rafael Espindola | bec3765 | 2017-11-17 01:37:50 +0000 | [diff] [blame] | 51 | Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther, |
| 52 | uint8_t Type, bool CanOmitFromDynSym, InputFile *File); |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 53 | Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type, |
| 54 | uint64_t Value, uint64_t Size, uint8_t Binding, |
| 55 | SectionBase *Section, InputFile *File); |
| Rui Ueyama | 1bdaf3e | 2016-11-09 23:37:40 +0000 | [diff] [blame] | 56 | |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 57 | template <class ELFT> |
| Rafael Espindola | a32ddc4 | 2017-12-20 16:28:19 +0000 | [diff] [blame] | 58 | void addShared(StringRef Name, SharedFile<ELFT> &F, |
| Rui Ueyama | 7f9694a | 2017-10-28 20:15:56 +0000 | [diff] [blame] | 59 | const typename ELFT::Sym &Sym, uint32_t Alignment, |
| Rafael Espindola | 8f619ab | 2017-12-12 01:45:49 +0000 | [diff] [blame] | 60 | uint32_t VerdefIndex); |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 61 | |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 62 | template <class ELFT> |
| Peter Collingbourne | 09e04af | 2018-02-16 20:23:54 +0000 | [diff] [blame] | 63 | void addLazyArchive(StringRef Name, ArchiveFile &F, |
| 64 | const llvm::object::Archive::Symbol S); |
| Rui Ueyama | de3d0cc | 2017-09-30 12:41:34 +0000 | [diff] [blame] | 65 | |
| Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 +0000 | [diff] [blame] | 66 | template <class ELFT> void addLazyObject(StringRef Name, LazyObjFile &Obj); |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 67 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 68 | Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther, |
| Rafael Espindola | f168712 | 2017-12-20 16:16:40 +0000 | [diff] [blame] | 69 | uint8_t Type, bool CanOmitFromDynSym, BitcodeFile &File); |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 70 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 71 | Symbol *addCommon(StringRef Name, uint64_t Size, uint32_t Alignment, |
| 72 | uint8_t Binding, uint8_t StOther, uint8_t Type, |
| Rafael Espindola | 7b5cc6c | 2017-12-20 16:19:48 +0000 | [diff] [blame] | 73 | InputFile &File); |
| Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 74 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 75 | std::pair<Symbol *, bool> insert(StringRef Name); |
| 76 | std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type, |
| 77 | uint8_t Visibility, bool CanOmitFromDynSym, |
| 78 | InputFile *File); |
| Petr Hosek | 5e51f7d | 2017-02-21 22:32:51 +0000 | [diff] [blame] | 79 | |
| Rui Ueyama | cc013f6 | 2018-04-03 18:01:18 +0000 | [diff] [blame^] | 80 | template <class ELFT> void fetchLazy(Symbol *Sym); |
| George Rimar | 1ef746b | 2018-04-03 17:16:52 +0000 | [diff] [blame] | 81 | |
| Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 82 | void scanVersionScript(); |
| Rui Ueyama | d60dae8a | 2016-06-23 07:00:17 +0000 | [diff] [blame] | 83 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 84 | Symbol *find(StringRef Name); |
| Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 85 | |
| 86 | void trace(StringRef Name); |
| Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 87 | |
| Rafael Espindola | d72d97b | 2017-09-08 18:16:59 +0000 | [diff] [blame] | 88 | void handleDynamicList(); |
| 89 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 90 | private: |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 91 | std::vector<Symbol *> findByVersion(SymbolVersion Ver); |
| 92 | std::vector<Symbol *> findAllByVersion(SymbolVersion Ver); |
| Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 93 | |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 94 | llvm::StringMap<std::vector<Symbol *>> &getDemangledSyms(); |
| Rui Ueyama | ea26504 | 2016-09-13 20:51:30 +0000 | [diff] [blame] | 95 | void handleAnonymousVersion(); |
| Rui Ueyama | da805c4 | 2016-11-17 03:39:21 +0000 | [diff] [blame] | 96 | void assignExactVersion(SymbolVersion Ver, uint16_t VersionId, |
| Rui Ueyama | 94bcfae | 2016-11-17 02:09:42 +0000 | [diff] [blame] | 97 | StringRef VersionName); |
| Rui Ueyama | da805c4 | 2016-11-17 03:39:21 +0000 | [diff] [blame] | 98 | void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId); |
| George Rimar | 50dcece | 2016-07-16 12:26:39 +0000 | [diff] [blame] | 99 | |
| Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 100 | // The order the global symbols are in is not defined. We can use an arbitrary |
| 101 | // order, but it has to be reproducible. That is true even when cross linking. |
| 102 | // The default hashing of StringRef produces different results on 32 and 64 |
| Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 103 | // bit systems so we use a map to a vector. That is arbitrary, deterministic |
| 104 | // but a bit inefficient. |
| Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 105 | // FIXME: Experiment with passing in a custom hashing or sorting the symbols |
| 106 | // once symbol resolution is finished. |
| Sam Clegg | a80d94d | 2017-11-27 23:16:06 +0000 | [diff] [blame] | 107 | llvm::DenseMap<llvm::CachedHashStringRef, int> SymMap; |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 108 | std::vector<Symbol *> SymVector; |
| Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 109 | |
| Rui Ueyama | 683564e | 2016-01-08 22:14:15 +0000 | [diff] [blame] | 110 | // Comdat groups define "link once" sections. If two comdat groups have the |
| 111 | // same name, only one of them is linked, and the other is ignored. This set |
| 112 | // is used to uniquify them. |
| Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 113 | llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups; |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 114 | |
| Rui Ueyama | 683564e | 2016-01-08 22:14:15 +0000 | [diff] [blame] | 115 | // Set of .so files to not link the same shared object file more than once. |
| Rui Ueyama | 131e0ff | 2016-01-08 22:17:42 +0000 | [diff] [blame] | 116 | llvm::DenseSet<StringRef> SoNames; |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 117 | |
| Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 118 | // A map from demangled symbol names to their symbol objects. |
| 119 | // This mapping is 1:N because two symbols with different versions |
| 120 | // can have the same name. We use this map to handle "extern C++ {}" |
| 121 | // directive in version scripts. |
| Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 122 | llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> DemangledSyms; |
| Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 123 | |
| Rui Ueyama | dc0b0b0 | 2017-11-04 23:09:43 +0000 | [diff] [blame] | 124 | struct WrappedSymbol { |
| 125 | Symbol *Sym; |
| 126 | Symbol *Real; |
| 127 | Symbol *Wrap; |
| Rui Ueyama | bbfe33c | 2017-09-25 00:57:18 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 130 | // For -wrap. |
| Rui Ueyama | dc0b0b0 | 2017-11-04 23:09:43 +0000 | [diff] [blame] | 131 | std::vector<WrappedSymbol> WrappedSymbols; |
| Rafael Espindola | 4693508 | 2017-10-06 20:09:34 +0000 | [diff] [blame] | 132 | |
| Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 133 | // For LTO. |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 134 | std::unique_ptr<BitcodeCompiler> LTO; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 137 | extern SymbolTable *Symtab; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 138 | } // namespace elf |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 139 | } // namespace lld |
| 140 | |
| 141 | #endif |