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