blob: 7683706e2b0b4515f007f8ad0828a3ee9ae50b79 [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"
Rui Ueyama25992482016-03-22 20:52:10 +000014#include "LTO.h"
Rui Ueyamaf91282e2016-11-03 17:57:38 +000015#include "Strings.h"
Justin Lebar3c11e932016-10-18 17:50:36 +000016#include "llvm/ADT/CachedHashString.h"
Rafael Espindola7f0b7272016-04-14 20:42:43 +000017#include "llvm/ADT/DenseMap.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018
19namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000020namespace elf {
Rui Ueyamac5b95122015-12-16 23:23:14 +000021class Lazy;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000022class OutputSectionBase;
Rafael Espindola5d7593b2015-12-22 23:00:50 +000023struct Symbol;
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
Justin Lebar3c11e932016-10-18 17:50:36 +000025typedef llvm::CachedHashStringRef SymName;
Rafael Espindolac9157d32016-04-14 19:17:16 +000026
Michael J. Spencer84487f12015-07-24 21:03:07 +000027// 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
Peter Collingbourne4f952702016-05-01 04:55:03 +000036// to replace the lazy symbol. The logic is implemented in the
37// add*() functions, which are called by input files as they are parsed. There
38// is one add* function per symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +000039template <class ELFT> class SymbolTable {
Rui Ueyama9328b2c2016-03-14 23:16:09 +000040 typedef typename ELFT::Sym Elf_Sym;
41 typedef typename ELFT::uint uintX_t;
Rui Ueyama79c73732016-01-08 21:53:28 +000042
Michael J. Spencer84487f12015-07-24 21:03:07 +000043public:
Rui Ueyama38dbd3e2016-09-14 00:05:51 +000044 void addFile(InputFile *File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000045 void addCombinedLtoObject();
Michael J. Spencer84487f12015-07-24 21:03:07 +000046
Rui Ueyama07b83762016-11-02 00:29:06 +000047 ArrayRef<Symbol *> getSymbols() const { return SymVector; }
48 ArrayRef<ObjectFile<ELFT> *> getObjectFiles() const { return ObjectFiles; }
49 ArrayRef<BinaryFile *> getBinaryFiles() const { return BinaryFiles; }
50 ArrayRef<SharedFile<ELFT> *> getSharedFiles() const { return SharedFiles; }
Rafael Espindola740fafe2015-09-08 19:43:27 +000051
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000052 DefinedRegular<ELFT> *addAbsolute(StringRef Name,
53 uint8_t Visibility = llvm::ELF::STV_HIDDEN);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000054 DefinedRegular<ELFT> *addIgnored(StringRef Name,
55 uint8_t Visibility = llvm::ELF::STV_HIDDEN);
Rui Ueyama79c73732016-01-08 21:53:28 +000056
Peter Collingbourne4f952702016-05-01 04:55:03 +000057 Symbol *addUndefined(StringRef Name);
58 Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
Davide Italiano786d8e32016-09-29 00:40:08 +000059 uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000060
Rafael Espindola5ceeb602016-10-26 20:57:14 +000061 Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
62 uintX_t Value, uintX_t Size, uint8_t Binding,
63 InputSectionBase<ELFT> *Section);
64
Peter Collingbourne4f952702016-05-01 04:55:03 +000065 Symbol *addRegular(StringRef Name, const Elf_Sym &Sym,
66 InputSectionBase<ELFT> *Section);
Rafael Espindola093abab2016-10-27 17:45:40 +000067 Symbol *addRegular(StringRef Name, uint8_t StOther,
68 InputSectionBase<ELFT> *Section, uint8_t Binding,
69 uint8_t Type, uintX_t Value);
Rafael Espindolae08e78d2016-11-09 23:23:45 +000070 Symbol *addSynthetic(StringRef N, OutputSectionBase *Section, uintX_t Value,
71 uint8_t StOther);
Peter Collingbourne4f952702016-05-01 04:55:03 +000072 void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
73 const typename ELFT::Verdef *Verdef);
74
75 void addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S);
Rafael Espindola65c65ce2016-06-14 21:56:36 +000076 void addLazyObject(StringRef Name, LazyObjectFile &Obj);
Rafael Espindolacceb92a2016-08-30 20:53:26 +000077 Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther,
Davide Italiano786d8e32016-09-29 00:40:08 +000078 uint8_t Type, bool CanOmitFromDynSym, BitcodeFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000079
80 Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment,
81 uint8_t Binding, uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +000082 InputFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000083
Peter Collingbourne892d49802016-04-27 00:05:03 +000084 void scanUndefinedFlags();
Rui Ueyama93bfee52015-10-13 18:10:33 +000085 void scanShlibUndefined();
Adhemerval Zanella9df07202016-04-13 18:51:11 +000086 void scanDynamicList();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +000087 void scanVersionScript();
Rui Ueyamad60dae8a2016-06-23 07:00:17 +000088
Rui Ueyamac4aaed92015-10-22 18:49:53 +000089 SymbolBody *find(StringRef Name);
Rui Ueyama69c778c2016-07-17 17:50:09 +000090
91 void trace(StringRef Name);
Rui Ueyamadeb15402016-01-07 17:20:07 +000092 void wrap(StringRef Name);
Rafael Espindola5d413262015-10-01 21:22:26 +000093
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +000094 std::vector<InputSectionBase<ELFT> *> Sections;
95
Michael J. Spencer84487f12015-07-24 21:03:07 +000096private:
Rui Ueyamaf91282e2016-11-03 17:57:38 +000097 std::vector<SymbolBody *> findAll(const StringMatcher &M);
Rui Ueyamadace8382016-07-21 13:13:21 +000098 std::pair<Symbol *, bool> insert(StringRef &Name);
99 std::pair<Symbol *, bool> insert(StringRef &Name, uint8_t Type,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000100 uint8_t Visibility, bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +0000101 InputFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000102
George Rimar31c25ae2016-09-15 12:44:38 +0000103 std::map<std::string, std::vector<SymbolBody *>> getDemangledSyms();
Rui Ueyamaea265042016-09-13 20:51:30 +0000104 void handleAnonymousVersion();
George Rimar50dcece2016-07-16 12:26:39 +0000105
Rui Ueyamae3357902016-07-18 01:35:00 +0000106 struct SymIndex {
George Rimarb0841252016-07-20 14:26:48 +0000107 SymIndex(int Idx, bool Traced) : Idx(Idx), Traced(Traced) {}
Rui Ueyamae3357902016-07-18 01:35:00 +0000108 int Idx : 31;
109 unsigned Traced : 1;
110 };
111
Rafael Espindola40102eb2015-09-17 18:26:25 +0000112 // The order the global symbols are in is not defined. We can use an arbitrary
113 // order, but it has to be reproducible. That is true even when cross linking.
114 // The default hashing of StringRef produces different results on 32 and 64
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000115 // bit systems so we use a map to a vector. That is arbitrary, deterministic
116 // but a bit inefficient.
Rafael Espindola40102eb2015-09-17 18:26:25 +0000117 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
118 // once symbol resolution is finished.
Rui Ueyamae3357902016-07-18 01:35:00 +0000119 llvm::DenseMap<SymName, SymIndex> Symtab;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000120 std::vector<Symbol *> SymVector;
Rafael Espindola222edc62015-09-03 18:56:20 +0000121
Rui Ueyama683564e2016-01-08 22:14:15 +0000122 // Comdat groups define "link once" sections. If two comdat groups have the
123 // same name, only one of them is linked, and the other is ignored. This set
124 // is used to uniquify them.
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000125 llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
Rafael Espindola444576d2015-10-09 19:25:07 +0000126
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000127 std::vector<ObjectFile<ELFT> *> ObjectFiles;
128 std::vector<SharedFile<ELFT> *> SharedFiles;
129 std::vector<BitcodeFile *> BitcodeFiles;
Rafael Espindola093abab2016-10-27 17:45:40 +0000130 std::vector<BinaryFile *> BinaryFiles;
Rui Ueyama683564e2016-01-08 22:14:15 +0000131
132 // Set of .so files to not link the same shared object file more than once.
Rui Ueyama131e0ff2016-01-08 22:17:42 +0000133 llvm::DenseSet<StringRef> SoNames;
Rui Ueyama25992482016-03-22 20:52:10 +0000134
135 std::unique_ptr<BitcodeCompiler> Lto;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000136};
137
Peter Collingbourne4f952702016-05-01 04:55:03 +0000138template <class ELFT> struct Symtab { static SymbolTable<ELFT> *X; };
139template <class ELFT> SymbolTable<ELFT> *Symtab<ELFT>::X;
140
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000141} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000142} // namespace lld
143
144#endif