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 | b4731a5 | 2015-07-29 16:30:40 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include "llvm/ADT/DenseMapInfo.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | |
| 17 | namespace lld { |
| 18 | namespace elf2 { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | struct Symbol; |
| 20 | |
| 21 | // SymbolTable is a bucket of all known symbols, including defined, |
| 22 | // undefined, or lazy symbols (the last one is symbols in archive |
| 23 | // files whose archive members are not yet loaded). |
| 24 | // |
| 25 | // We put all symbols of all files to a SymbolTable, and the |
| 26 | // SymbolTable selects the "best" symbols if there are name |
| 27 | // conflicts. For example, obviously, a defined symbol is better than |
| 28 | // an undefined symbol. Or, if there's a conflict between a lazy and a |
| 29 | // undefined, it'll read an archive member to read a real definition |
| 30 | // to replace the lazy symbol. The logic is implemented in resolve(). |
Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 31 | class SymbolTable { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 32 | public: |
| 33 | SymbolTable(); |
| 34 | |
| 35 | void addFile(std::unique_ptr<InputFile> File); |
| 36 | |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 37 | const ELFFileBase *getFirstELF() const { |
Rafael Espindola | 8aeb13f | 2015-09-03 19:13:13 +0000 | [diff] [blame] | 38 | if (!ObjectFiles.empty()) |
| 39 | return ObjectFiles[0].get(); |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 40 | if (!SharedFiles.empty()) |
| 41 | return SharedFiles[0].get(); |
Rafael Espindola | 8aeb13f | 2015-09-03 19:13:13 +0000 | [diff] [blame] | 42 | return nullptr; |
| 43 | } |
| 44 | |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 45 | const llvm::DenseMap<StringRef, Symbol *> &getSymbols() const { |
| 46 | return Symtab; |
| 47 | } |
| 48 | |
Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 49 | const std::vector<std::unique_ptr<ObjectFileBase>> &getObjectFiles() const { |
| 50 | return ObjectFiles; |
| 51 | } |
| 52 | |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame^] | 53 | const std::vector<std::unique_ptr<SharedFileBase>> &getSharedFiles() const { |
| 54 | return SharedFiles; |
| 55 | } |
| 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 | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 59 | template <class ELFT> void addELFFile(ELFFileBase *File); |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 60 | void addELFFile(ELFFileBase *File); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 61 | void addLazy(Lazy *New); |
| 62 | void addMemberFile(Lazy *Body); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 63 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 64 | template <class ELFT> void init(); |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 65 | template <class ELFT> void resolve(SymbolBody *Body); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 66 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 67 | std::vector<std::unique_ptr<ArchiveFile>> ArchiveFiles; |
| 68 | |
Rui Ueyama | b4731a5 | 2015-07-29 16:30:40 +0000 | [diff] [blame] | 69 | llvm::DenseMap<StringRef, Symbol *> Symtab; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 70 | llvm::BumpPtrAllocator Alloc; |
Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 71 | |
| 72 | // The writer needs to infer the machine type from the object files. |
| 73 | std::vector<std::unique_ptr<ObjectFileBase>> ObjectFiles; |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 74 | |
| 75 | std::vector<std::unique_ptr<SharedFileBase>> SharedFiles; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace elf2 |
| 79 | } // namespace lld |
| 80 | |
| 81 | #endif |