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 { |
Rui Ueyama | 7d47619 | 2019-05-16 02:14:00 +0000 | [diff] [blame] | 20 | |
Rui Ueyama | 5c073a9 | 2019-05-16 03:29:03 +0000 | [diff] [blame] | 21 | class CommonSymbol; |
Rafael Espindola | d26b52f | 2017-12-09 16:56:18 +0000 | [diff] [blame] | 22 | class Defined; |
Rui Ueyama | 7d47619 | 2019-05-16 02:14:00 +0000 | [diff] [blame] | 23 | class LazyArchive; |
| 24 | class LazyObject; |
Rafael Espindola | d26b52f | 2017-12-09 16:56:18 +0000 | [diff] [blame] | 25 | class SectionBase; |
Rui Ueyama | 7d47619 | 2019-05-16 02:14:00 +0000 | [diff] [blame] | 26 | class SharedSymbol; |
| 27 | class Undefined; |
Sam Clegg | c090962 | 2017-06-30 00:34:35 +0000 | [diff] [blame] | 28 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | // SymbolTable is a bucket of all known symbols, including defined, |
| 30 | // undefined, or lazy symbols (the last one is symbols in archive |
| 31 | // files whose archive members are not yet loaded). |
| 32 | // |
| 33 | // We put all symbols of all files to a SymbolTable, and the |
| 34 | // SymbolTable selects the "best" symbols if there are name |
| 35 | // conflicts. For example, obviously, a defined symbol is better than |
| 36 | // an undefined symbol. Or, if there's a conflict between a lazy and a |
| 37 | // undefined, it'll read an archive member to read a real definition |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 38 | // to replace the lazy symbol. The logic is implemented in the |
| 39 | // add*() functions, which are called by input files as they are parsed. There |
| 40 | // is one add* function per symbol type. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 41 | class SymbolTable { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 42 | public: |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 43 | template <class ELFT> void addCombinedLTOObject(); |
Rui Ueyama | 07b4536 | 2018-08-22 07:02:26 +0000 | [diff] [blame] | 44 | void wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 45 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 46 | ArrayRef<Symbol *> getSymbols() const { return SymVector; } |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 47 | |
Rui Ueyama | bbf154c | 2019-05-17 01:55:20 +0000 | [diff] [blame^] | 48 | Symbol *insert(StringRef Name); |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 49 | |
Rui Ueyama | bbf154c | 2019-05-17 01:55:20 +0000 | [diff] [blame^] | 50 | Symbol *addSymbol(const Symbol &New); |
Petr Hosek | 5e51f7d | 2017-02-21 22:32:51 +0000 | [diff] [blame] | 51 | |
Rui Ueyama | 943cd00 | 2019-05-16 03:45:13 +0000 | [diff] [blame] | 52 | void fetchLazy(Symbol *Sym); |
George Rimar | 1ef746b | 2018-04-03 17:16:52 +0000 | [diff] [blame] | 53 | |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 54 | void scanVersionScript(); |
Rui Ueyama | d60dae8a | 2016-06-23 07:00:17 +0000 | [diff] [blame] | 55 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 56 | Symbol *find(StringRef Name); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 57 | |
| 58 | void trace(StringRef Name); |
Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 59 | |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 +0000 | [diff] [blame] | 60 | void handleDynamicList(); |
| 61 | |
Fangrui Song | b4744d3 | 2019-02-01 02:25:05 +0000 | [diff] [blame] | 62 | // Set of .so files to not link the same shared object file more than once. |
Peter Collingbourne | cc1618e | 2019-04-08 17:35:55 +0000 | [diff] [blame] | 63 | llvm::DenseMap<StringRef, SharedFile *> SoNames; |
Fangrui Song | b4744d3 | 2019-02-01 02:25:05 +0000 | [diff] [blame] | 64 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 65 | private: |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 66 | std::vector<Symbol *> findByVersion(SymbolVersion Ver); |
| 67 | std::vector<Symbol *> findAllByVersion(SymbolVersion Ver); |
Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 68 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 69 | llvm::StringMap<std::vector<Symbol *>> &getDemangledSyms(); |
Rui Ueyama | ea26504 | 2016-09-13 20:51:30 +0000 | [diff] [blame] | 70 | void handleAnonymousVersion(); |
Rui Ueyama | da805c4 | 2016-11-17 03:39:21 +0000 | [diff] [blame] | 71 | void assignExactVersion(SymbolVersion Ver, uint16_t VersionId, |
Rui Ueyama | 94bcfae | 2016-11-17 02:09:42 +0000 | [diff] [blame] | 72 | StringRef VersionName); |
Rui Ueyama | da805c4 | 2016-11-17 03:39:21 +0000 | [diff] [blame] | 73 | void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId); |
George Rimar | 50dcece | 2016-07-16 12:26:39 +0000 | [diff] [blame] | 74 | |
Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 75 | // The order the global symbols are in is not defined. We can use an arbitrary |
| 76 | // order, but it has to be reproducible. That is true even when cross linking. |
| 77 | // The default hashing of StringRef produces different results on 32 and 64 |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 78 | // bit systems so we use a map to a vector. That is arbitrary, deterministic |
| 79 | // but a bit inefficient. |
Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 80 | // FIXME: Experiment with passing in a custom hashing or sorting the symbols |
| 81 | // once symbol resolution is finished. |
Sam Clegg | a80d94d | 2017-11-27 23:16:06 +0000 | [diff] [blame] | 82 | llvm::DenseMap<llvm::CachedHashStringRef, int> SymMap; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 83 | std::vector<Symbol *> SymVector; |
Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 84 | |
Rui Ueyama | 683564e | 2016-01-08 22:14:15 +0000 | [diff] [blame] | 85 | // Comdat groups define "link once" sections. If two comdat groups have the |
| 86 | // same name, only one of them is linked, and the other is ignored. This set |
| 87 | // is used to uniquify them. |
Rafael Espindola | 1c2baad | 2017-05-25 21:53:02 +0000 | [diff] [blame] | 88 | llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 89 | |
Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 90 | // A map from demangled symbol names to their symbol objects. |
| 91 | // This mapping is 1:N because two symbols with different versions |
| 92 | // can have the same name. We use this map to handle "extern C++ {}" |
| 93 | // directive in version scripts. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 94 | llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> DemangledSyms; |
Rui Ueyama | 8249214 | 2016-11-15 18:41:52 +0000 | [diff] [blame] | 95 | |
| 96 | // For LTO. |
Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 97 | std::unique_ptr<BitcodeCompiler> LTO; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 100 | extern SymbolTable *Symtab; |
Rui Ueyama | bbf154c | 2019-05-17 01:55:20 +0000 | [diff] [blame^] | 101 | |
| 102 | void mergeSymbolProperties(Symbol *Old, const Symbol &New); |
| 103 | void resolveSymbol(Symbol *Old, const Symbol &New); |
| 104 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 105 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 106 | } // namespace lld |
| 107 | |
| 108 | #endif |