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 | f91282e | 2016-11-03 17:57:38 +0000 | [diff] [blame] | 15 | #include "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 { |
Rui Ueyama | c5b9512 | 2015-12-16 23:23:14 +0000 | [diff] [blame] | 21 | class Lazy; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 22 | class OutputSectionBase; |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 23 | struct Symbol; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 24 | |
Justin Lebar | 3c11e93 | 2016-10-18 17:50:36 +0000 | [diff] [blame] | 25 | typedef llvm::CachedHashStringRef SymName; |
Rafael Espindola | c9157d3 | 2016-04-14 19:17:16 +0000 | [diff] [blame] | 26 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | // SymbolTable is a bucket of all known symbols, including defined, |
| 28 | // undefined, or lazy symbols (the last one is symbols in archive |
| 29 | // files whose archive members are not yet loaded). |
| 30 | // |
| 31 | // We put all symbols of all files to a SymbolTable, and the |
| 32 | // SymbolTable selects the "best" symbols if there are name |
| 33 | // conflicts. For example, obviously, a defined symbol is better than |
| 34 | // an undefined symbol. Or, if there's a conflict between a lazy and a |
| 35 | // undefined, it'll read an archive member to read a real definition |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 36 | // to replace the lazy symbol. The logic is implemented in the |
| 37 | // add*() functions, which are called by input files as they are parsed. There |
| 38 | // is one add* function per symbol type. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 39 | template <class ELFT> class SymbolTable { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 40 | typedef typename ELFT::Sym Elf_Sym; |
| 41 | typedef typename ELFT::uint uintX_t; |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 42 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 43 | public: |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 44 | void addFile(InputFile *File); |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 45 | void addCombinedLtoObject(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 46 | |
Rui Ueyama | 07b8376 | 2016-11-02 00:29:06 +0000 | [diff] [blame] | 47 | ArrayRef<Symbol *> getSymbols() const { return SymVector; } |
| 48 | ArrayRef<ObjectFile<ELFT> *> getObjectFiles() const { return ObjectFiles; } |
| 49 | ArrayRef<BinaryFile *> getBinaryFiles() const { return BinaryFiles; } |
| 50 | ArrayRef<SharedFile<ELFT> *> getSharedFiles() const { return SharedFiles; } |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 51 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 52 | DefinedRegular<ELFT> *addAbsolute(StringRef Name, |
| 53 | uint8_t Visibility = llvm::ELF::STV_HIDDEN); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 54 | DefinedRegular<ELFT> *addIgnored(StringRef Name, |
| 55 | uint8_t Visibility = llvm::ELF::STV_HIDDEN); |
Rui Ueyama | 79c7373 | 2016-01-08 21:53:28 +0000 | [diff] [blame] | 56 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 57 | Symbol *addUndefined(StringRef Name); |
| 58 | Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther, |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 59 | uint8_t Type, bool CanOmitFromDynSym, InputFile *File); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 60 | |
Rafael Espindola | 5ceeb60 | 2016-10-26 20:57:14 +0000 | [diff] [blame] | 61 | Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type, |
| 62 | uintX_t Value, uintX_t Size, uint8_t Binding, |
| 63 | InputSectionBase<ELFT> *Section); |
| 64 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 65 | Symbol *addRegular(StringRef Name, const Elf_Sym &Sym, |
| 66 | InputSectionBase<ELFT> *Section); |
Rafael Espindola | 093abab | 2016-10-27 17:45:40 +0000 | [diff] [blame] | 67 | Symbol *addRegular(StringRef Name, uint8_t StOther, |
| 68 | InputSectionBase<ELFT> *Section, uint8_t Binding, |
| 69 | uint8_t Type, uintX_t Value); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 70 | Symbol *addSynthetic(StringRef N, OutputSectionBase *Section, uintX_t Value, |
| 71 | uint8_t StOther); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 72 | void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym, |
| 73 | const typename ELFT::Verdef *Verdef); |
| 74 | |
| 75 | void addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S); |
Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 76 | void addLazyObject(StringRef Name, LazyObjectFile &Obj); |
Rafael Espindola | cceb92a | 2016-08-30 20:53:26 +0000 | [diff] [blame] | 77 | Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther, |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 78 | uint8_t Type, bool CanOmitFromDynSym, BitcodeFile *File); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 79 | |
| 80 | Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment, |
| 81 | uint8_t Binding, uint8_t StOther, uint8_t Type, |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 82 | InputFile *File); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 83 | |
Peter Collingbourne | 892d4980 | 2016-04-27 00:05:03 +0000 | [diff] [blame] | 84 | void scanUndefinedFlags(); |
Rui Ueyama | 93bfee5 | 2015-10-13 18:10:33 +0000 | [diff] [blame] | 85 | void scanShlibUndefined(); |
Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 86 | void scanDynamicList(); |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 87 | void scanVersionScript(); |
Rui Ueyama | d60dae8a | 2016-06-23 07:00:17 +0000 | [diff] [blame] | 88 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 89 | SymbolBody *find(StringRef Name); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 90 | |
| 91 | void trace(StringRef Name); |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 92 | void wrap(StringRef Name); |
Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 93 | |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 94 | std::vector<InputSectionBase<ELFT> *> Sections; |
| 95 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 96 | private: |
Rui Ueyama | f91282e | 2016-11-03 17:57:38 +0000 | [diff] [blame] | 97 | std::vector<SymbolBody *> findAll(const StringMatcher &M); |
Rui Ueyama | dace838 | 2016-07-21 13:13:21 +0000 | [diff] [blame] | 98 | std::pair<Symbol *, bool> insert(StringRef &Name); |
| 99 | std::pair<Symbol *, bool> insert(StringRef &Name, uint8_t Type, |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 100 | uint8_t Visibility, bool CanOmitFromDynSym, |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 101 | InputFile *File); |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 102 | |
George Rimar | 31c25ae | 2016-09-15 12:44:38 +0000 | [diff] [blame] | 103 | std::map<std::string, std::vector<SymbolBody *>> getDemangledSyms(); |
Rui Ueyama | ea26504 | 2016-09-13 20:51:30 +0000 | [diff] [blame] | 104 | void handleAnonymousVersion(); |
George Rimar | 50dcece | 2016-07-16 12:26:39 +0000 | [diff] [blame] | 105 | |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 106 | struct SymIndex { |
George Rimar | b084125 | 2016-07-20 14:26:48 +0000 | [diff] [blame] | 107 | SymIndex(int Idx, bool Traced) : Idx(Idx), Traced(Traced) {} |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 108 | int Idx : 31; |
| 109 | unsigned Traced : 1; |
| 110 | }; |
| 111 | |
Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 112 | // The order the global symbols are in is not defined. We can use an arbitrary |
| 113 | // order, but it has to be reproducible. That is true even when cross linking. |
| 114 | // The default hashing of StringRef produces different results on 32 and 64 |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 115 | // bit systems so we use a map to a vector. That is arbitrary, deterministic |
| 116 | // but a bit inefficient. |
Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 117 | // FIXME: Experiment with passing in a custom hashing or sorting the symbols |
| 118 | // once symbol resolution is finished. |
Rui Ueyama | e335790 | 2016-07-18 01:35:00 +0000 | [diff] [blame] | 119 | llvm::DenseMap<SymName, SymIndex> Symtab; |
Rafael Espindola | 7f0b727 | 2016-04-14 20:42:43 +0000 | [diff] [blame] | 120 | std::vector<Symbol *> SymVector; |
Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 121 | |
Rui Ueyama | 683564e | 2016-01-08 22:14:15 +0000 | [diff] [blame] | 122 | // Comdat groups define "link once" sections. If two comdat groups have the |
| 123 | // same name, only one of them is linked, and the other is ignored. This set |
| 124 | // is used to uniquify them. |
Rafael Espindola | 8b2c8536 | 2016-10-21 19:49:42 +0000 | [diff] [blame] | 125 | llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 126 | |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 127 | std::vector<ObjectFile<ELFT> *> ObjectFiles; |
| 128 | std::vector<SharedFile<ELFT> *> SharedFiles; |
| 129 | std::vector<BitcodeFile *> BitcodeFiles; |
Rafael Espindola | 093abab | 2016-10-27 17:45:40 +0000 | [diff] [blame] | 130 | std::vector<BinaryFile *> BinaryFiles; |
Rui Ueyama | 683564e | 2016-01-08 22:14:15 +0000 | [diff] [blame] | 131 | |
| 132 | // 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] | 133 | llvm::DenseSet<StringRef> SoNames; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 134 | |
| 135 | std::unique_ptr<BitcodeCompiler> Lto; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 138 | template <class ELFT> struct Symtab { static SymbolTable<ELFT> *X; }; |
| 139 | template <class ELFT> SymbolTable<ELFT> *Symtab<ELFT>::X; |
| 140 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 141 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 142 | } // namespace lld |
| 143 | |
| 144 | #endif |