blob: 7180ccb483fcb34ea9d92aacebad98424a950c64 [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:54 +00001//===- SymbolTable.h --------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencer84487f12015-07-24 21:03:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_ELF_SYMBOL_TABLE_H
10#define LLD_ELF_SYMBOL_TABLE_H
11
12#include "InputFiles.h"
Rui Ueyama25992482016-03-22 20:52:10 +000013#include "LTO.h"
Rui Ueyamaee173712018-02-28 17:38:19 +000014#include "lld/Common/Strings.h"
Justin Lebar3c11e932016-10-18 17:50:36 +000015#include "llvm/ADT/CachedHashString.h"
Rafael Espindola7f0b7272016-04-14 20:42:43 +000016#include "llvm/ADT/DenseMap.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000017
18namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000019namespace elf {
Rafael Espindolad26b52f2017-12-09 16:56:18 +000020class Defined;
21class SectionBase;
Sam Cleggc0909622017-06-30 00:34:35 +000022
Michael J. Spencer84487f12015-07-24 21:03:07 +000023// SymbolTable is a bucket of all known symbols, including defined,
24// undefined, or lazy symbols (the last one is symbols in archive
25// files whose archive members are not yet loaded).
26//
27// We put all symbols of all files to a SymbolTable, and the
28// SymbolTable selects the "best" symbols if there are name
29// conflicts. For example, obviously, a defined symbol is better than
30// an undefined symbol. Or, if there's a conflict between a lazy and a
31// undefined, it'll read an archive member to read a real definition
Peter Collingbourne4f952702016-05-01 04:55:03 +000032// to replace the lazy symbol. The logic is implemented in the
33// add*() functions, which are called by input files as they are parsed. There
34// is one add* function per symbol type.
Rafael Espindola244ef982017-07-26 18:42:48 +000035class SymbolTable {
Michael J. Spencer84487f12015-07-24 21:03:07 +000036public:
Rafael Espindola244ef982017-07-26 18:42:48 +000037 template <class ELFT> void addFile(InputFile *File);
38 template <class ELFT> void addCombinedLTOObject();
Rui Ueyama07b45362018-08-22 07:02:26 +000039 void wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap);
Michael J. Spencer84487f12015-07-24 21:03:07 +000040
Rui Ueyamaf52496e2017-11-03 21:21:47 +000041 ArrayRef<Symbol *> getSymbols() const { return SymVector; }
Rafael Espindola740fafe2015-09-08 19:43:27 +000042
Rafael Espindola244ef982017-07-26 18:42:48 +000043 template <class ELFT>
Rafael Espindolabec37652017-11-17 01:37:50 +000044 Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
45 uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
Rui Ueyamac7497d32018-10-11 20:34:29 +000046
George Rimar94a16cb2018-11-22 11:40:08 +000047 Defined *addDefined(StringRef Name, uint8_t StOther, uint8_t Type,
48 uint64_t Value, uint64_t Size, uint8_t Binding,
49 SectionBase *Section, InputFile *File);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +000050
Rafael Espindola244ef982017-07-26 18:42:48 +000051 template <class ELFT>
Rafael Espindolaa32ddc42017-12-20 16:28:19 +000052 void addShared(StringRef Name, SharedFile<ELFT> &F,
Rui Ueyama7f9694a2017-10-28 20:15:56 +000053 const typename ELFT::Sym &Sym, uint32_t Alignment,
Rafael Espindola8f619ab2017-12-12 01:45:49 +000054 uint32_t VerdefIndex);
Peter Collingbourne4f952702016-05-01 04:55:03 +000055
Rafael Espindola244ef982017-07-26 18:42:48 +000056 template <class ELFT>
Peter Collingbourne09e04af2018-02-16 20:23:54 +000057 void addLazyArchive(StringRef Name, ArchiveFile &F,
58 const llvm::object::Archive::Symbol S);
Rui Ueyamade3d0cc2017-09-30 12:41:34 +000059
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000060 template <class ELFT> void addLazyObject(StringRef Name, LazyObjFile &Obj);
Rafael Espindola244ef982017-07-26 18:42:48 +000061
Rui Ueyamaf52496e2017-11-03 21:21:47 +000062 Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther,
Rafael Espindolaf1687122017-12-20 16:16:40 +000063 uint8_t Type, bool CanOmitFromDynSym, BitcodeFile &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000064
Rui Ueyamaf52496e2017-11-03 21:21:47 +000065 Symbol *addCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
66 uint8_t Binding, uint8_t StOther, uint8_t Type,
Rafael Espindola7b5cc6c2017-12-20 16:19:48 +000067 InputFile &File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000068
Rui Ueyamaf3fad552018-10-12 18:29:18 +000069 std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Visibility,
70 bool CanOmitFromDynSym, InputFile *File);
Petr Hosek5e51f7d2017-02-21 22:32:51 +000071
Rui Ueyamacc013f62018-04-03 18:01:18 +000072 template <class ELFT> void fetchLazy(Symbol *Sym);
George Rimar1ef746b2018-04-03 17:16:52 +000073
Peter Collingbourne66ac1d62016-04-22 20:21:26 +000074 void scanVersionScript();
Rui Ueyamad60dae8a2016-06-23 07:00:17 +000075
Rui Ueyamaf52496e2017-11-03 21:21:47 +000076 Symbol *find(StringRef Name);
Rui Ueyama69c778c2016-07-17 17:50:09 +000077
78 void trace(StringRef Name);
Rafael Espindola5d413262015-10-01 21:22:26 +000079
Rafael Espindolad72d97b2017-09-08 18:16:59 +000080 void handleDynamicList();
81
Michael J. Spencer84487f12015-07-24 21:03:07 +000082private:
Rui Ueyama3c434482018-10-10 22:49:29 +000083 std::pair<Symbol *, bool> insertName(StringRef Name);
84
Rui Ueyamaf52496e2017-11-03 21:21:47 +000085 std::vector<Symbol *> findByVersion(SymbolVersion Ver);
86 std::vector<Symbol *> findAllByVersion(SymbolVersion Ver);
Rui Ueyama82492142016-11-15 18:41:52 +000087
Rui Ueyamaf52496e2017-11-03 21:21:47 +000088 llvm::StringMap<std::vector<Symbol *>> &getDemangledSyms();
Rui Ueyamaea265042016-09-13 20:51:30 +000089 void handleAnonymousVersion();
Rui Ueyamada805c42016-11-17 03:39:21 +000090 void assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +000091 StringRef VersionName);
Rui Ueyamada805c42016-11-17 03:39:21 +000092 void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId);
George Rimar50dcece2016-07-16 12:26:39 +000093
Rafael Espindola40102eb2015-09-17 18:26:25 +000094 // The order the global symbols are in is not defined. We can use an arbitrary
95 // order, but it has to be reproducible. That is true even when cross linking.
96 // The default hashing of StringRef produces different results on 32 and 64
Rafael Espindola7f0b7272016-04-14 20:42:43 +000097 // bit systems so we use a map to a vector. That is arbitrary, deterministic
98 // but a bit inefficient.
Rafael Espindola40102eb2015-09-17 18:26:25 +000099 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
100 // once symbol resolution is finished.
Sam Clegga80d94d2017-11-27 23:16:06 +0000101 llvm::DenseMap<llvm::CachedHashStringRef, int> SymMap;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000102 std::vector<Symbol *> SymVector;
Rafael Espindola222edc62015-09-03 18:56:20 +0000103
Rui Ueyama683564e2016-01-08 22:14:15 +0000104 // Comdat groups define "link once" sections. If two comdat groups have the
105 // same name, only one of them is linked, and the other is ignored. This set
106 // is used to uniquify them.
Rafael Espindola1c2baad2017-05-25 21:53:02 +0000107 llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
Rafael Espindola444576d2015-10-09 19:25:07 +0000108
Rui Ueyama683564e2016-01-08 22:14:15 +0000109 // Set of .so files to not link the same shared object file more than once.
Fangrui Song50394f62018-12-27 22:24:45 +0000110 llvm::DenseMap<StringRef, InputFile *> SoNames;
Rui Ueyama25992482016-03-22 20:52:10 +0000111
Rui Ueyama82492142016-11-15 18:41:52 +0000112 // A map from demangled symbol names to their symbol objects.
113 // This mapping is 1:N because two symbols with different versions
114 // can have the same name. We use this map to handle "extern C++ {}"
115 // directive in version scripts.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000116 llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> DemangledSyms;
Rui Ueyama82492142016-11-15 18:41:52 +0000117
118 // For LTO.
Davide Italiano3bfa0812016-11-26 05:37:04 +0000119 std::unique_ptr<BitcodeCompiler> LTO;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000120};
121
Rafael Espindola244ef982017-07-26 18:42:48 +0000122extern SymbolTable *Symtab;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000123} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000124} // namespace lld
125
126#endif