blob: 6677ea0dd80332b14de42ae65a64c120e26538a2 [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 {
Rui Ueyama7d476192019-05-16 02:14:00 +000020
Rui Ueyama5c073a92019-05-16 03:29:03 +000021class CommonSymbol;
Rafael Espindolad26b52f2017-12-09 16:56:18 +000022class Defined;
Rui Ueyama7d476192019-05-16 02:14:00 +000023class LazyArchive;
24class LazyObject;
Rafael Espindolad26b52f2017-12-09 16:56:18 +000025class SectionBase;
Rui Ueyama7d476192019-05-16 02:14:00 +000026class SharedSymbol;
27class Undefined;
Sam Cleggc0909622017-06-30 00:34:35 +000028
Michael J. Spencer84487f12015-07-24 21:03:07 +000029// SymbolTable is a bucket of all known symbols, including defined,
30// undefined, or lazy symbols (the last one is symbols in archive
31// files whose archive members are not yet loaded).
32//
33// We put all symbols of all files to a SymbolTable, and the
34// SymbolTable selects the "best" symbols if there are name
35// conflicts. For example, obviously, a defined symbol is better than
36// an undefined symbol. Or, if there's a conflict between a lazy and a
37// undefined, it'll read an archive member to read a real definition
Peter Collingbourne4f952702016-05-01 04:55:03 +000038// to replace the lazy symbol. The logic is implemented in the
39// add*() functions, which are called by input files as they are parsed. There
40// is one add* function per symbol type.
Rafael Espindola244ef982017-07-26 18:42:48 +000041class SymbolTable {
Michael J. Spencer84487f12015-07-24 21:03:07 +000042public:
Rafael Espindola244ef982017-07-26 18:42:48 +000043 template <class ELFT> void addCombinedLTOObject();
Rui Ueyama07b45362018-08-22 07:02:26 +000044 void wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap);
Michael J. Spencer84487f12015-07-24 21:03:07 +000045
Rui Ueyamaf52496e2017-11-03 21:21:47 +000046 ArrayRef<Symbol *> getSymbols() const { return SymVector; }
Rafael Espindola740fafe2015-09-08 19:43:27 +000047
Rui Ueyamabbf154c2019-05-17 01:55:20 +000048 Symbol *insert(StringRef Name);
Rafael Espindola244ef982017-07-26 18:42:48 +000049
Rui Ueyamabbf154c2019-05-17 01:55:20 +000050 Symbol *addSymbol(const Symbol &New);
Petr Hosek5e51f7d2017-02-21 22:32:51 +000051
Rui Ueyama943cd002019-05-16 03:45:13 +000052 void fetchLazy(Symbol *Sym);
George Rimar1ef746b2018-04-03 17:16:52 +000053
Peter Collingbourne66ac1d62016-04-22 20:21:26 +000054 void scanVersionScript();
Rui Ueyamad60dae8a2016-06-23 07:00:17 +000055
Rui Ueyamaf52496e2017-11-03 21:21:47 +000056 Symbol *find(StringRef Name);
Rui Ueyama69c778c2016-07-17 17:50:09 +000057
58 void trace(StringRef Name);
Rafael Espindola5d413262015-10-01 21:22:26 +000059
Rafael Espindolad72d97b2017-09-08 18:16:59 +000060 void handleDynamicList();
61
Fangrui Songb4744d32019-02-01 02:25:05 +000062 // Set of .so files to not link the same shared object file more than once.
Peter Collingbournecc1618e2019-04-08 17:35:55 +000063 llvm::DenseMap<StringRef, SharedFile *> SoNames;
Fangrui Songb4744d32019-02-01 02:25:05 +000064
Michael J. Spencer84487f12015-07-24 21:03:07 +000065private:
Rui Ueyamaf52496e2017-11-03 21:21:47 +000066 std::vector<Symbol *> findByVersion(SymbolVersion Ver);
67 std::vector<Symbol *> findAllByVersion(SymbolVersion Ver);
Rui Ueyama82492142016-11-15 18:41:52 +000068
Rui Ueyamaf52496e2017-11-03 21:21:47 +000069 llvm::StringMap<std::vector<Symbol *>> &getDemangledSyms();
Rui Ueyamaea265042016-09-13 20:51:30 +000070 void handleAnonymousVersion();
Rui Ueyamada805c42016-11-17 03:39:21 +000071 void assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
Rui Ueyama94bcfae2016-11-17 02:09:42 +000072 StringRef VersionName);
Rui Ueyamada805c42016-11-17 03:39:21 +000073 void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId);
George Rimar50dcece2016-07-16 12:26:39 +000074
Rafael Espindola40102eb2015-09-17 18:26:25 +000075 // The order the global symbols are in is not defined. We can use an arbitrary
76 // order, but it has to be reproducible. That is true even when cross linking.
77 // The default hashing of StringRef produces different results on 32 and 64
Rafael Espindola7f0b7272016-04-14 20:42:43 +000078 // bit systems so we use a map to a vector. That is arbitrary, deterministic
79 // but a bit inefficient.
Rafael Espindola40102eb2015-09-17 18:26:25 +000080 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
81 // once symbol resolution is finished.
Sam Clegga80d94d2017-11-27 23:16:06 +000082 llvm::DenseMap<llvm::CachedHashStringRef, int> SymMap;
Rui Ueyamaf52496e2017-11-03 21:21:47 +000083 std::vector<Symbol *> SymVector;
Rafael Espindola222edc62015-09-03 18:56:20 +000084
Rui Ueyama683564e2016-01-08 22:14:15 +000085 // Comdat groups define "link once" sections. If two comdat groups have the
86 // same name, only one of them is linked, and the other is ignored. This set
87 // is used to uniquify them.
Rafael Espindola1c2baad2017-05-25 21:53:02 +000088 llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
Rafael Espindola444576d2015-10-09 19:25:07 +000089
Rui Ueyama82492142016-11-15 18:41:52 +000090 // A map from demangled symbol names to their symbol objects.
91 // This mapping is 1:N because two symbols with different versions
92 // can have the same name. We use this map to handle "extern C++ {}"
93 // directive in version scripts.
Rui Ueyamaf52496e2017-11-03 21:21:47 +000094 llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> DemangledSyms;
Rui Ueyama82492142016-11-15 18:41:52 +000095
96 // For LTO.
Davide Italiano3bfa0812016-11-26 05:37:04 +000097 std::unique_ptr<BitcodeCompiler> LTO;
Michael J. Spencer84487f12015-07-24 21:03:07 +000098};
99
Rafael Espindola244ef982017-07-26 18:42:48 +0000100extern SymbolTable *Symtab;
Rui Ueyamabbf154c2019-05-17 01:55:20 +0000101
102void mergeSymbolProperties(Symbol *Old, const Symbol &New);
103void resolveSymbol(Symbol *Old, const Symbol &New);
104
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000105} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000106} // namespace lld
107
108#endif