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