blob: 08e1b4e290f7986f5fb544f029c5c1dd3e294358 [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);
Peter Collingbourne4f952702016-05-01 04:55:03 +000064 Symbol *addRegular(StringRef Name, const Elf_Sym &Sym,
65 InputSectionBase<ELFT> *Section);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +000066
Rafael Espindolae08e78d2016-11-09 23:23:45 +000067 Symbol *addSynthetic(StringRef N, OutputSectionBase *Section, uintX_t Value,
68 uint8_t StOther);
Rui Ueyama1bdaf3e2016-11-09 23:37:40 +000069
Peter Collingbourne4f952702016-05-01 04:55:03 +000070 void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
71 const typename ELFT::Verdef *Verdef);
72
73 void addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S);
Rafael Espindola65c65ce2016-06-14 21:56:36 +000074 void addLazyObject(StringRef Name, LazyObjectFile &Obj);
Rafael Espindolacceb92a2016-08-30 20:53:26 +000075 Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther,
Davide Italiano786d8e32016-09-29 00:40:08 +000076 uint8_t Type, bool CanOmitFromDynSym, BitcodeFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000077
78 Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment,
79 uint8_t Binding, uint8_t StOther, uint8_t Type,
Davide Italiano786d8e32016-09-29 00:40:08 +000080 InputFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000081
Peter Collingbourne892d49802016-04-27 00:05:03 +000082 void scanUndefinedFlags();
Rui Ueyama93bfee52015-10-13 18:10:33 +000083 void scanShlibUndefined();
Adhemerval Zanella9df07202016-04-13 18:51:11 +000084 void scanDynamicList();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +000085 void scanVersionScript();
Rui Ueyamad60dae8a2016-06-23 07:00:17 +000086
Rui Ueyamac4aaed92015-10-22 18:49:53 +000087 SymbolBody *find(StringRef Name);
Rui Ueyama69c778c2016-07-17 17:50:09 +000088
89 void trace(StringRef Name);
Rui Ueyamadeb15402016-01-07 17:20:07 +000090 void wrap(StringRef Name);
Rafael Espindola5d413262015-10-01 21:22:26 +000091
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +000092 std::vector<InputSectionBase<ELFT> *> Sections;
93
Michael J. Spencer84487f12015-07-24 21:03:07 +000094private:
Rui Ueyamaf91282e2016-11-03 17:57:38 +000095 std::vector<SymbolBody *> findAll(const StringMatcher &M);
Rui Ueyamadace8382016-07-21 13:13:21 +000096 std::pair<Symbol *, bool> insert(StringRef &Name);
97 std::pair<Symbol *, bool> insert(StringRef &Name, uint8_t Type,
Peter Collingbourne4f952702016-05-01 04:55:03 +000098 uint8_t Visibility, bool CanOmitFromDynSym,
Davide Italiano786d8e32016-09-29 00:40:08 +000099 InputFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000100
George Rimar31c25ae2016-09-15 12:44:38 +0000101 std::map<std::string, std::vector<SymbolBody *>> getDemangledSyms();
Rui Ueyamaea265042016-09-13 20:51:30 +0000102 void handleAnonymousVersion();
George Rimar50dcece2016-07-16 12:26:39 +0000103
Rui Ueyamae3357902016-07-18 01:35:00 +0000104 struct SymIndex {
George Rimarb0841252016-07-20 14:26:48 +0000105 SymIndex(int Idx, bool Traced) : Idx(Idx), Traced(Traced) {}
Rui Ueyamae3357902016-07-18 01:35:00 +0000106 int Idx : 31;
107 unsigned Traced : 1;
108 };
109
Rafael Espindola40102eb2015-09-17 18:26:25 +0000110 // The order the global symbols are in is not defined. We can use an arbitrary
111 // order, but it has to be reproducible. That is true even when cross linking.
112 // The default hashing of StringRef produces different results on 32 and 64
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000113 // bit systems so we use a map to a vector. That is arbitrary, deterministic
114 // but a bit inefficient.
Rafael Espindola40102eb2015-09-17 18:26:25 +0000115 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
116 // once symbol resolution is finished.
Rui Ueyamae3357902016-07-18 01:35:00 +0000117 llvm::DenseMap<SymName, SymIndex> Symtab;
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000118 std::vector<Symbol *> SymVector;
Rafael Espindola222edc62015-09-03 18:56:20 +0000119
Rui Ueyama683564e2016-01-08 22:14:15 +0000120 // Comdat groups define "link once" sections. If two comdat groups have the
121 // same name, only one of them is linked, and the other is ignored. This set
122 // is used to uniquify them.
Rafael Espindola8b2c85362016-10-21 19:49:42 +0000123 llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
Rafael Espindola444576d2015-10-09 19:25:07 +0000124
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000125 std::vector<ObjectFile<ELFT> *> ObjectFiles;
126 std::vector<SharedFile<ELFT> *> SharedFiles;
127 std::vector<BitcodeFile *> BitcodeFiles;
Rafael Espindola093abab2016-10-27 17:45:40 +0000128 std::vector<BinaryFile *> BinaryFiles;
Rui Ueyama683564e2016-01-08 22:14:15 +0000129
130 // Set of .so files to not link the same shared object file more than once.
Rui Ueyama131e0ff2016-01-08 22:17:42 +0000131 llvm::DenseSet<StringRef> SoNames;
Rui Ueyama25992482016-03-22 20:52:10 +0000132
133 std::unique_ptr<BitcodeCompiler> Lto;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000134};
135
Peter Collingbourne4f952702016-05-01 04:55:03 +0000136template <class ELFT> struct Symtab { static SymbolTable<ELFT> *X; };
137template <class ELFT> SymbolTable<ELFT> *Symtab<ELFT>::X;
138
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000139} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000140} // namespace lld
141
142#endif