blob: fa7c6a8d29184c0a9650a9a7649bddd3f2f56aae [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 {
Rafael Espindolad26b52f2017-12-09 16:56:18 +000021class Defined;
22class SectionBase;
Sam Cleggc0909622017-06-30 00:34:35 +000023
Michael J. Spencer84487f12015-07-24 21:03:07 +000024// SymbolTable is a bucket of all known symbols, including defined,
25// undefined, or lazy symbols (the last one is symbols in archive
26// files whose archive members are not yet loaded).
27//
28// We put all symbols of all files to a SymbolTable, and the
29// SymbolTable selects the "best" symbols if there are name
30// conflicts. For example, obviously, a defined symbol is better than
31// an undefined symbol. Or, if there's a conflict between a lazy and a
32// undefined, it'll read an archive member to read a real definition
Peter Collingbourne4f952702016-05-01 04:55:03 +000033// to replace the lazy symbol. The logic is implemented in the
34// add*() functions, which are called by input files as they are parsed. There
35// is one add* function per symbol type.
Rafael Espindola244ef982017-07-26 18:42:48 +000036class SymbolTable {
Michael J. Spencer84487f12015-07-24 21:03:07 +000037public:
Rafael Espindola244ef982017-07-26 18:42:48 +000038 template <class ELFT> void addFile(InputFile *File);
39 template <class ELFT> void addCombinedLTOObject();
Rafael Espindola244ef982017-07-26 18:42:48 +000040 template <class ELFT> void addSymbolWrap(StringRef Name);
Rui Ueyamadc0b0b02017-11-04 23:09:43 +000041 void applySymbolWrap();
Michael J. Spencer84487f12015-07-24 21:03:07 +000042
Rui Ueyamaf52496e2017-11-03 21:21:47 +000043 ArrayRef<Symbol *> getSymbols() const { return SymVector; }
Rafael Espindola740fafe2015-09-08 19:43:27 +000044
Rafael Espindola244ef982017-07-26 18:42:48 +000045 template <class ELFT>
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000046 Defined *addAbsolute(StringRef Name,
47 uint8_t Visibility = llvm::ELF::STV_HIDDEN,
48 uint8_t Binding = llvm::ELF::STB_GLOBAL);
Rui Ueyama79c73732016-01-08 21:53:28 +000049
Rui Ueyamaf52496e2017-11-03 21:21:47 +000050 template <class ELFT> Symbol *addUndefined(StringRef Name);
Rafael Espindola244ef982017-07-26 18:42:48 +000051 template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +000052 Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
53 uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
Rafael Espindola244ef982017-07-26 18:42:48 +000054 template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +000055 Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
56 uint64_t Value, uint64_t Size, uint8_t Binding,
57 SectionBase *Section, InputFile *File);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +000058
Rafael Espindola244ef982017-07-26 18:42:48 +000059 template <class ELFT>
Rui Ueyamade3d0cc2017-09-30 12:41:34 +000060 void addShared(StringRef Name, SharedFile<ELFT> *F,
Rui Ueyama7f9694a2017-10-28 20:15:56 +000061 const typename ELFT::Sym &Sym, uint32_t Alignment,
Peter Collingbourne4f952702016-05-01 04:55:03 +000062 const typename ELFT::Verdef *Verdef);
63
Rafael Espindola244ef982017-07-26 18:42:48 +000064 template <class ELFT>
Rui Ueyamaf52496e2017-11-03 21:21:47 +000065 Symbol *addLazyArchive(StringRef Name, ArchiveFile *F,
66 const llvm::object::Archive::Symbol S);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +000067
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000068 template <class ELFT> void addLazyObject(StringRef Name, LazyObjFile &Obj);
Rafael Espindola244ef982017-07-26 18:42:48 +000069
Rui Ueyamaf52496e2017-11-03 21:21:47 +000070 Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther,
71 uint8_t Type, bool CanOmitFromDynSym, BitcodeFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000072
Rui Ueyamaf52496e2017-11-03 21:21:47 +000073 Symbol *addCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
74 uint8_t Binding, uint8_t StOther, uint8_t Type,
75 InputFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000076
Rui Ueyamaf52496e2017-11-03 21:21:47 +000077 std::pair<Symbol *, bool> insert(StringRef Name);
78 std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type,
79 uint8_t Visibility, bool CanOmitFromDynSym,
80 InputFile *File);
Petr Hosek5e51f7d2017-02-21 22:32:51 +000081
Igor Kudrinfb7f8be2017-10-03 12:23:46 +000082 template <class ELFT> void fetchIfLazy(StringRef Name);
Rafael Espindola244ef982017-07-26 18:42:48 +000083 template <class ELFT> void scanShlibUndefined();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +000084 void scanVersionScript();
Rui Ueyamad60dae8a2016-06-23 07:00:17 +000085
Rui Ueyamaf52496e2017-11-03 21:21:47 +000086 Symbol *find(StringRef Name);
Rui Ueyama69c778c2016-07-17 17:50:09 +000087
88 void trace(StringRef Name);
Rafael Espindola5d413262015-10-01 21:22:26 +000089
Rafael Espindolad72d97b2017-09-08 18:16:59 +000090 void handleDynamicList();
91
Michael J. Spencer84487f12015-07-24 21:03:07 +000092private:
Rui Ueyamaf52496e2017-11-03 21:21:47 +000093 std::vector<Symbol *> findByVersion(SymbolVersion Ver);
94 std::vector<Symbol *> findAllByVersion(SymbolVersion Ver);
95 void defsym(Symbol *Dst, Symbol *Src);
Rui Ueyama82492142016-11-15 18:41:52 +000096
Rui Ueyamaf52496e2017-11-03 21:21:47 +000097 llvm::StringMap<std::vector<Symbol *>> &getDemangledSyms();
Rui Ueyamaea265042016-09-13 20:51:30 +000098 void handleAnonymousVersion();
Rui Ueyamada805c42016-11-17 03:39:21 +000099 void assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +0000100 StringRef VersionName);
Rui Ueyamada805c42016-11-17 03:39:21 +0000101 void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId);
George Rimar50dcece2016-07-16 12:26:39 +0000102
Rafael Espindola40102eb2015-09-17 18:26:25 +0000103 // The order the global symbols are in is not defined. We can use an arbitrary
104 // order, but it has to be reproducible. That is true even when cross linking.
105 // The default hashing of StringRef produces different results on 32 and 64
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000106 // bit systems so we use a map to a vector. That is arbitrary, deterministic
107 // but a bit inefficient.
Rafael Espindola40102eb2015-09-17 18:26:25 +0000108 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
109 // once symbol resolution is finished.
Sam Clegga80d94d2017-11-27 23:16:06 +0000110 llvm::DenseMap<llvm::CachedHashStringRef, int> SymMap;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000111 std::vector<Symbol *> SymVector;
Rafael Espindola222edc62015-09-03 18:56:20 +0000112
Rui Ueyama683564e2016-01-08 22:14:15 +0000113 // Comdat groups define "link once" sections. If two comdat groups have the
114 // same name, only one of them is linked, and the other is ignored. This set
115 // is used to uniquify them.
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000116 llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
Rafael Espindola444576d2015-10-09 19:25:07 +0000117
Rui Ueyama683564e2016-01-08 22:14:15 +0000118 // Set of .so files to not link the same shared object file more than once.
Rui Ueyama131e0ff2016-01-08 22:17:42 +0000119 llvm::DenseSet<StringRef> SoNames;
Rui Ueyama25992482016-03-22 20:52:10 +0000120
Rui Ueyama82492142016-11-15 18:41:52 +0000121 // A map from demangled symbol names to their symbol objects.
122 // This mapping is 1:N because two symbols with different versions
123 // can have the same name. We use this map to handle "extern C++ {}"
124 // directive in version scripts.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000125 llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> DemangledSyms;
Rui Ueyama82492142016-11-15 18:41:52 +0000126
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000127 struct WrappedSymbol {
128 Symbol *Sym;
129 Symbol *Real;
130 Symbol *Wrap;
Rui Ueyamabbfe33c2017-09-25 00:57:18 +0000131 };
132
Rafael Espindola46935082017-10-06 20:09:34 +0000133 // For -wrap.
Rui Ueyamadc0b0b02017-11-04 23:09:43 +0000134 std::vector<WrappedSymbol> WrappedSymbols;
Rafael Espindola46935082017-10-06 20:09:34 +0000135
Rui Ueyama82492142016-11-15 18:41:52 +0000136 // For LTO.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000137 std::unique_ptr<BitcodeCompiler> LTO;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000138};
139
Rafael Espindola244ef982017-07-26 18:42:48 +0000140extern SymbolTable *Symtab;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000141} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000142} // namespace lld
143
144#endif