blob: f33a36c7a9b11cb877f787ee355af4cd766a6970 [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:54 +00001//===- SymbolTable.h --------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
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 Espindola40102eb2015-09-17 18:26:25 +000014#include "llvm/ADT/MapVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000015
16namespace lld {
17namespace elf2 {
Michael J. Spencer84487f12015-07-24 21:03:07 +000018struct 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 Espindola2ffdd4d2015-08-04 14:29:01 +000030class SymbolTable {
Michael J. Spencer84487f12015-07-24 21:03:07 +000031public:
32 SymbolTable();
33
34 void addFile(std::unique_ptr<InputFile> File);
35
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000036 const ELFFileBase *getFirstELF() const {
Rafael Espindola8aeb13f2015-09-03 19:13:13 +000037 if (!ObjectFiles.empty())
38 return ObjectFiles[0].get();
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000039 if (!SharedFiles.empty())
40 return SharedFiles[0].get();
Rafael Espindola8aeb13f2015-09-03 19:13:13 +000041 return nullptr;
42 }
43
Rafael Espindola67a5da62015-09-17 14:02:10 +000044 bool shouldUseRela() const;
45
Rafael Espindola40102eb2015-09-17 18:26:25 +000046 const llvm::MapVector<StringRef, Symbol *> &getSymbols() const {
Rafael Espindola62b81b82015-08-14 13:07:05 +000047 return Symtab;
48 }
49
Rafael Espindola222edc62015-09-03 18:56:20 +000050 const std::vector<std::unique_ptr<ObjectFileBase>> &getObjectFiles() const {
51 return ObjectFiles;
52 }
53
Rafael Espindola740fafe2015-09-08 19:43:27 +000054 const std::vector<std::unique_ptr<SharedFileBase>> &getSharedFiles() const {
55 return SharedFiles;
56 }
57
Michael J. Spencer546c64c2015-09-08 22:34:57 +000058 SymbolBody *getEntrySym() const {
Rafael Espindola4340aad2015-09-11 22:42:45 +000059 if (!EntrySym)
60 return nullptr;
Rui Ueyamae66e0012015-10-07 23:20:23 +000061 return EntrySym->repl();
Michael J. Spencer546c64c2015-09-08 22:34:57 +000062 }
63
Denis Protivensky22220d52015-10-05 09:43:57 +000064 void addUndefinedSym(StringRef Name);
65
Rafael Espindola0e604f92015-09-25 18:56:53 +000066 template <class ELFT>
67 void addSyntheticSym(StringRef Name, OutputSection<ELFT> &Section,
68 typename llvm::object::ELFFile<ELFT>::uintX_t Value);
69
Rafael Espindola5d413262015-10-01 21:22:26 +000070 template <class ELFT> void addIgnoredSym(StringRef Name);
71
Michael J. Spencer84487f12015-07-24 21:03:07 +000072private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +000073 Symbol *insert(SymbolBody *New);
Rafael Espindola824d1a92015-09-04 00:09:43 +000074 template <class ELFT> void addELFFile(ELFFileBase *File);
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000075 void addELFFile(ELFFileBase *File);
Michael J. Spencer1b348a62015-09-04 22:28:10 +000076 void addLazy(Lazy *New);
77 void addMemberFile(Lazy *Body);
Denis Protivensky22220d52015-10-05 09:43:57 +000078 template <class ELFT> void addUndefinedSym(StringRef Name);
Michael J. Spencer84487f12015-07-24 21:03:07 +000079
Rafael Espindola01205f72015-09-22 18:19:46 +000080 template <class ELFT> void init(uint16_t EMachine);
Rafael Espindoladaa92a62015-08-31 01:16:19 +000081 template <class ELFT> void resolve(SymbolBody *Body);
Rafael Espindola1a49e582015-09-23 14:10:24 +000082 template <class ELFT>
Igor Kudrin65bddea2015-10-09 09:58:39 +000083 void reportConflict(const Twine &Message, const SymbolBody &Old,
84 const SymbolBody &New, bool Warning);
Michael J. Spencer84487f12015-07-24 21:03:07 +000085
Michael J. Spencer1b348a62015-09-04 22:28:10 +000086 std::vector<std::unique_ptr<ArchiveFile>> ArchiveFiles;
87
Rafael Espindola40102eb2015-09-17 18:26:25 +000088 // The order the global symbols are in is not defined. We can use an arbitrary
89 // order, but it has to be reproducible. That is true even when cross linking.
90 // The default hashing of StringRef produces different results on 32 and 64
91 // bit systems so we use a MapVector. That is arbitrary, deterministic but
92 // a bit inefficient.
93 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
94 // once symbol resolution is finished.
95 llvm::MapVector<StringRef, Symbol *> Symtab;
Michael J. Spencer84487f12015-07-24 21:03:07 +000096 llvm::BumpPtrAllocator Alloc;
Rafael Espindola222edc62015-09-03 18:56:20 +000097
98 // The writer needs to infer the machine type from the object files.
99 std::vector<std::unique_ptr<ObjectFileBase>> ObjectFiles;
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000100
101 std::vector<std::unique_ptr<SharedFileBase>> SharedFiles;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +0000102 llvm::DenseSet<StringRef> IncludedSoNames;
Michael J. Spencer546c64c2015-09-08 22:34:57 +0000103
Michael J. Spencerac5f0482015-09-08 22:51:46 +0000104 SymbolBody *EntrySym = nullptr;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000105};
106
107} // namespace elf2
108} // namespace lld
109
110#endif