blob: 1f602e702e443b95bea06e18c86e1369448c7c3d [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
Rafael Espindola9f77ef02016-02-12 20:54:57 +000016namespace llvm {
17class Module;
18}
19
Michael J. Spencer84487f12015-07-24 21:03:07 +000020namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000021namespace elf {
Rui Ueyamac5b95122015-12-16 23:23:14 +000022class Lazy;
Rui Ueyamac5b95122015-12-16 23:23:14 +000023template <class ELFT> class OutputSectionBase;
Rafael Espindola5d7593b2015-12-22 23:00:50 +000024struct Symbol;
25class Undefined;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27// SymbolTable is a bucket of all known symbols, including defined,
28// undefined, or lazy symbols (the last one is symbols in archive
29// files whose archive members are not yet loaded).
30//
31// We put all symbols of all files to a SymbolTable, and the
32// SymbolTable selects the "best" symbols if there are name
33// conflicts. For example, obviously, a defined symbol is better than
34// an undefined symbol. Or, if there's a conflict between a lazy and a
35// undefined, it'll read an archive member to read a real definition
36// to replace the lazy symbol. The logic is implemented in resolve().
Rui Ueyama3ce825e2015-10-09 21:07:25 +000037template <class ELFT> class SymbolTable {
Rui Ueyama79c73732016-01-08 21:53:28 +000038 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
39 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
40
Michael J. Spencer84487f12015-07-24 21:03:07 +000041public:
Michael J. Spencer84487f12015-07-24 21:03:07 +000042 void addFile(std::unique_ptr<InputFile> File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000043 void addCombinedLtoObject();
Michael J. Spencer84487f12015-07-24 21:03:07 +000044
Rafael Espindola40102eb2015-09-17 18:26:25 +000045 const llvm::MapVector<StringRef, Symbol *> &getSymbols() const {
Rafael Espindola62b81b82015-08-14 13:07:05 +000046 return Symtab;
47 }
48
Rui Ueyama3ce825e2015-10-09 21:07:25 +000049 const std::vector<std::unique_ptr<ObjectFile<ELFT>>> &getObjectFiles() const {
Rafael Espindola222edc62015-09-03 18:56:20 +000050 return ObjectFiles;
51 }
52
Rui Ueyama3ce825e2015-10-09 21:07:25 +000053 const std::vector<std::unique_ptr<SharedFile<ELFT>>> &getSharedFiles() const {
Rafael Espindola740fafe2015-09-08 19:43:27 +000054 return SharedFiles;
55 }
56
Rui Ueyamaff777682015-10-09 21:12:40 +000057 SymbolBody *addUndefined(StringRef Name);
58 SymbolBody *addUndefinedOpt(StringRef Name);
Rui Ueyama79c73732016-01-08 21:53:28 +000059 SymbolBody *addAbsolute(StringRef Name, Elf_Sym &ESym);
60 SymbolBody *addSynthetic(StringRef Name, OutputSectionBase<ELFT> &Section,
61 uintX_t Value);
Rui Ueyamadd7d9982015-12-16 22:31:14 +000062 SymbolBody *addIgnored(StringRef Name);
Rui Ueyama79c73732016-01-08 21:53:28 +000063
Rui Ueyama93bfee52015-10-13 18:10:33 +000064 void scanShlibUndefined();
Rui Ueyamac4aaed92015-10-22 18:49:53 +000065 SymbolBody *find(StringRef Name);
Rui Ueyamadeb15402016-01-07 17:20:07 +000066 void wrap(StringRef Name);
Rafael Espindola18f09502016-02-26 21:49:38 +000067 InputFile *findFile(SymbolBody *B);
Rafael Espindola5d413262015-10-01 21:22:26 +000068
Michael J. Spencer84487f12015-07-24 21:03:07 +000069private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +000070 Symbol *insert(SymbolBody *New);
Michael J. Spencer1b348a62015-09-04 22:28:10 +000071 void addLazy(Lazy *New);
Rafael Espindola5d7593b2015-12-22 23:00:50 +000072 void addMemberFile(Undefined *Undef, Lazy *L);
Rui Ueyama3ce825e2015-10-09 21:07:25 +000073 void resolve(SymbolBody *Body);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000074 std::unique_ptr<InputFile> codegen(llvm::Module &M);
Rui Ueyamaf0904012015-12-16 22:26:45 +000075 std::string conflictMsg(SymbolBody *Old, SymbolBody *New);
Michael J. Spencer84487f12015-07-24 21:03:07 +000076
Rafael Espindola9f77ef02016-02-12 20:54:57 +000077 SmallString<0> OwningLTOData;
78 std::unique_ptr<MemoryBuffer> LtoBuffer;
79 ObjectFile<ELFT> *createCombinedLtoObject();
80
Rafael Espindola40102eb2015-09-17 18:26:25 +000081 // The order the global symbols are in is not defined. We can use an arbitrary
82 // order, but it has to be reproducible. That is true even when cross linking.
83 // The default hashing of StringRef produces different results on 32 and 64
84 // bit systems so we use a MapVector. That is arbitrary, deterministic but
85 // a bit inefficient.
86 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
87 // once symbol resolution is finished.
88 llvm::MapVector<StringRef, Symbol *> Symtab;
Michael J. Spencer84487f12015-07-24 21:03:07 +000089 llvm::BumpPtrAllocator Alloc;
Rafael Espindola222edc62015-09-03 18:56:20 +000090
Rui Ueyama683564e2016-01-08 22:14:15 +000091 // Comdat groups define "link once" sections. If two comdat groups have the
92 // same name, only one of them is linked, and the other is ignored. This set
93 // is used to uniquify them.
Rui Ueyama52d3b672016-01-06 02:06:33 +000094 llvm::DenseSet<StringRef> ComdatGroups;
Rafael Espindola444576d2015-10-09 19:25:07 +000095
Rui Ueyama52c7c5f2016-01-08 22:20:00 +000096 // The symbol table owns all file objects.
97 std::vector<std::unique_ptr<ArchiveFile>> ArchiveFiles;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000098 std::vector<std::unique_ptr<ObjectFile<ELFT>>> ObjectFiles;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000099 std::vector<std::unique_ptr<SharedFile<ELFT>>> SharedFiles;
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000100 std::vector<std::unique_ptr<BitcodeFile>> BitcodeFiles;
Rui Ueyama683564e2016-01-08 22:14:15 +0000101
102 // Set of .so files to not link the same shared object file more than once.
Rui Ueyama131e0ff2016-01-08 22:17:42 +0000103 llvm::DenseSet<StringRef> SoNames;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000104};
105
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000106} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000107} // namespace lld
108
109#endif