| 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" | 
| Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/MapVector.h" | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 15 |  | 
|  | 16 | namespace lld { | 
|  | 17 | namespace elf2 { | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 18 | struct Symbol; | 
|  | 19 |  | 
|  | 20 | // SymbolTable is a bucket of all known symbols, including defined, | 
|  | 21 | // undefined, or lazy symbols (the last one is symbols in archive | 
|  | 22 | // files whose archive members are not yet loaded). | 
|  | 23 | // | 
|  | 24 | // We put all symbols of all files to a SymbolTable, and the | 
|  | 25 | // SymbolTable selects the "best" symbols if there are name | 
|  | 26 | // conflicts. For example, obviously, a defined symbol is better than | 
|  | 27 | // an undefined symbol. Or, if there's a conflict between a lazy and a | 
|  | 28 | // undefined, it'll read an archive member to read a real definition | 
|  | 29 | // to replace the lazy symbol. The logic is implemented in resolve(). | 
| Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 30 | class SymbolTable { | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 31 | public: | 
|  | 32 | SymbolTable(); | 
|  | 33 |  | 
|  | 34 | void addFile(std::unique_ptr<InputFile> File); | 
|  | 35 |  | 
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 36 | const ELFFileBase *getFirstELF() const { | 
| Rafael Espindola | 8aeb13f | 2015-09-03 19:13:13 +0000 | [diff] [blame] | 37 | if (!ObjectFiles.empty()) | 
|  | 38 | return ObjectFiles[0].get(); | 
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 39 | if (!SharedFiles.empty()) | 
|  | 40 | return SharedFiles[0].get(); | 
| Rafael Espindola | 8aeb13f | 2015-09-03 19:13:13 +0000 | [diff] [blame] | 41 | return nullptr; | 
|  | 42 | } | 
|  | 43 |  | 
| Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 44 | bool shouldUseRela() const; | 
|  | 45 |  | 
| Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 46 | const llvm::MapVector<StringRef, Symbol *> &getSymbols() const { | 
| Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 47 | return Symtab; | 
|  | 48 | } | 
|  | 49 |  | 
| Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 50 | const std::vector<std::unique_ptr<ObjectFileBase>> &getObjectFiles() const { | 
|  | 51 | return ObjectFiles; | 
|  | 52 | } | 
|  | 53 |  | 
| Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 54 | const std::vector<std::unique_ptr<SharedFileBase>> &getSharedFiles() const { | 
|  | 55 | return SharedFiles; | 
|  | 56 | } | 
|  | 57 |  | 
| Michael J. Spencer | 546c64c | 2015-09-08 22:34:57 +0000 | [diff] [blame] | 58 | SymbolBody *getEntrySym() const { | 
| Rafael Espindola | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 59 | if (!EntrySym) | 
|  | 60 | return nullptr; | 
| Michael J. Spencer | ac5f048 | 2015-09-08 22:51:46 +0000 | [diff] [blame] | 61 | return EntrySym->getReplacement(); | 
| Michael J. Spencer | 546c64c | 2015-09-08 22:34:57 +0000 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 64 | void addUndefinedSym(StringRef Name); | 
|  | 65 |  | 
| Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 66 | template <class ELFT> | 
|  | 67 | void addSyntheticSym(StringRef Name, OutputSection<ELFT> &Section, | 
|  | 68 | typename llvm::object::ELFFile<ELFT>::uintX_t Value); | 
|  | 69 |  | 
| Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 70 | template <class ELFT> void addIgnoredSym(StringRef Name); | 
|  | 71 |  | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 72 | private: | 
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 73 | Symbol *insert(SymbolBody *New); | 
| Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 74 | template <class ELFT> void addELFFile(ELFFileBase *File); | 
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 75 | void addELFFile(ELFFileBase *File); | 
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 76 | void addLazy(Lazy *New); | 
|  | 77 | void addMemberFile(Lazy *Body); | 
| Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 78 | template <class ELFT> void addUndefinedSym(StringRef Name); | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 79 |  | 
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 80 | template <class ELFT> void init(uint16_t EMachine); | 
| Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 81 | template <class ELFT> void resolve(SymbolBody *Body); | 
| Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 82 | template <class ELFT> | 
| Rui Ueyama | e1f9152 | 2015-10-07 21:37:24 +0000 | [diff] [blame] | 83 | void reportConflict(const SymbolBody &Old, const SymbolBody &New); | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 84 |  | 
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 85 | std::vector<std::unique_ptr<ArchiveFile>> ArchiveFiles; | 
|  | 86 |  | 
| Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 87 | // The order the global symbols are in is not defined. We can use an arbitrary | 
|  | 88 | // order, but it has to be reproducible. That is true even when cross linking. | 
|  | 89 | // The default hashing of StringRef produces different results on 32 and 64 | 
|  | 90 | // bit systems so we use a MapVector. That is arbitrary, deterministic but | 
|  | 91 | // a bit inefficient. | 
|  | 92 | // FIXME: Experiment with passing in a custom hashing or sorting the symbols | 
|  | 93 | // once symbol resolution is finished. | 
|  | 94 | llvm::MapVector<StringRef, Symbol *> Symtab; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 95 | llvm::BumpPtrAllocator Alloc; | 
| Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 96 |  | 
|  | 97 | // The writer needs to infer the machine type from the object files. | 
|  | 98 | std::vector<std::unique_ptr<ObjectFileBase>> ObjectFiles; | 
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 99 |  | 
|  | 100 | std::vector<std::unique_ptr<SharedFileBase>> SharedFiles; | 
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 101 | llvm::DenseSet<StringRef> IncludedSoNames; | 
| Michael J. Spencer | 546c64c | 2015-09-08 22:34:57 +0000 | [diff] [blame] | 102 |  | 
| Michael J. Spencer | ac5f048 | 2015-09-08 22:51:46 +0000 | [diff] [blame] | 103 | SymbolBody *EntrySym = nullptr; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 104 | }; | 
|  | 105 |  | 
|  | 106 | } // namespace elf2 | 
|  | 107 | } // namespace lld | 
|  | 108 |  | 
|  | 109 | #endif |