| 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(). |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 30 | template <class ELFT> 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 | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 36 | bool shouldUseRela() const; |
| 37 | |
| Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 38 | const llvm::MapVector<StringRef, Symbol *> &getSymbols() const { |
| Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 39 | return Symtab; |
| 40 | } |
| 41 | |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 42 | const std::vector<std::unique_ptr<ObjectFile<ELFT>>> &getObjectFiles() const { |
| Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 43 | return ObjectFiles; |
| 44 | } |
| 45 | |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 46 | const std::vector<std::unique_ptr<SharedFile<ELFT>>> &getSharedFiles() const { |
| Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 47 | return SharedFiles; |
| 48 | } |
| 49 | |
| Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 50 | SymbolBody *addUndefined(StringRef Name); |
| 51 | SymbolBody *addUndefinedOpt(StringRef Name); |
| Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 52 | void addSyntheticSym(StringRef Name, OutputSection<ELFT> &Section, |
| 53 | typename llvm::object::ELFFile<ELFT>::uintX_t Value); |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 54 | void addIgnoredSym(StringRef Name); |
| Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame^] | 55 | void finalize(); |
| Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 56 | |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 57 | private: |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 58 | Symbol *insert(SymbolBody *New); |
| Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 59 | void addELFFile(ELFFileBase<ELFT> *File); |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 60 | void addLazy(Lazy *New); |
| 61 | void addMemberFile(Lazy *Body); |
| Rui Ueyama | 3d45179 | 2015-10-12 18:03:21 +0000 | [diff] [blame] | 62 | void checkCompatibility(std::unique_ptr<InputFile> &File); |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 63 | void resolve(SymbolBody *Body); |
| Rui Ueyama | f8432d9 | 2015-10-13 16:34:14 +0000 | [diff] [blame^] | 64 | SymbolBody *find(StringRef Name); |
| Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 65 | void reportConflict(const Twine &Message, const SymbolBody &Old, |
| 66 | const SymbolBody &New, bool Warning); |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 67 | |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 68 | std::vector<std::unique_ptr<InputFile>> ArchiveFiles; |
| Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 69 | |
| Rafael Espindola | 40102eb | 2015-09-17 18:26:25 +0000 | [diff] [blame] | 70 | // The order the global symbols are in is not defined. We can use an arbitrary |
| 71 | // order, but it has to be reproducible. That is true even when cross linking. |
| 72 | // The default hashing of StringRef produces different results on 32 and 64 |
| 73 | // bit systems so we use a MapVector. That is arbitrary, deterministic but |
| 74 | // a bit inefficient. |
| 75 | // FIXME: Experiment with passing in a custom hashing or sorting the symbols |
| 76 | // once symbol resolution is finished. |
| 77 | llvm::MapVector<StringRef, Symbol *> Symtab; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 78 | llvm::BumpPtrAllocator Alloc; |
| Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 79 | |
| Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 80 | llvm::DenseSet<StringRef> Comdats; |
| 81 | |
| Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 82 | // The writer needs to infer the machine type from the object files. |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 83 | std::vector<std::unique_ptr<ObjectFile<ELFT>>> ObjectFiles; |
| Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 84 | |
| Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 85 | std::vector<std::unique_ptr<SharedFile<ELFT>>> SharedFiles; |
| Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 86 | llvm::DenseSet<StringRef> IncludedSoNames; |
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace elf2 |
| 90 | } // namespace lld |
| 91 | |
| 92 | #endif |