blob: 45741d99e1578dce692ed80d11e02ea31588b8b0 [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"
Rafael Espindola7f0b7272016-04-14 20:42:43 +000015#include "llvm/ADT/DenseMap.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000018namespace elf {
Rui Ueyamac5b95122015-12-16 23:23:14 +000019class Lazy;
Rui Ueyamac5b95122015-12-16 23:23:14 +000020template <class ELFT> class OutputSectionBase;
Rafael Espindola5d7593b2015-12-22 23:00:50 +000021struct Symbol;
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
Rafael Espindola29af4722016-04-21 12:21:06 +000023typedef llvm::CachedHash<StringRef> SymName;
Rafael Espindolac9157d32016-04-14 19:17:16 +000024
Michael J. Spencer84487f12015-07-24 21:03:07 +000025// SymbolTable is a bucket of all known symbols, including defined,
26// undefined, or lazy symbols (the last one is symbols in archive
27// files whose archive members are not yet loaded).
28//
29// We put all symbols of all files to a SymbolTable, and the
30// SymbolTable selects the "best" symbols if there are name
31// conflicts. For example, obviously, a defined symbol is better than
32// an undefined symbol. Or, if there's a conflict between a lazy and a
33// undefined, it'll read an archive member to read a real definition
Peter Collingbourne4f952702016-05-01 04:55:03 +000034// to replace the lazy symbol. The logic is implemented in the
35// add*() functions, which are called by input files as they are parsed. There
36// is one add* function per symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +000037template <class ELFT> class SymbolTable {
Rui Ueyama9328b2c2016-03-14 23:16:09 +000038 typedef typename ELFT::Sym Elf_Sym;
39 typedef typename ELFT::uint uintX_t;
Rui Ueyama79c73732016-01-08 21:53:28 +000040
Michael J. Spencer84487f12015-07-24 21:03:07 +000041public:
Michael J. Spencer84487f12015-07-24 21:03:07 +000042 void addFile(std::unique_ptr<InputFile> File);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000043 void addCombinedLtoObject();
Michael J. Spencer84487f12015-07-24 21:03:07 +000044
Rafael Espindola7f0b7272016-04-14 20:42:43 +000045 llvm::ArrayRef<Symbol *> getSymbols() const { return SymVector; }
Rafael Espindola62b81b82015-08-14 13:07:05 +000046
Rui Ueyama3ce825e2015-10-09 21:07:25 +000047 const std::vector<std::unique_ptr<ObjectFile<ELFT>>> &getObjectFiles() const {
Rafael Espindola222edc62015-09-03 18:56:20 +000048 return ObjectFiles;
49 }
50
Rui Ueyama3ce825e2015-10-09 21:07:25 +000051 const std::vector<std::unique_ptr<SharedFile<ELFT>>> &getSharedFiles() const {
Rafael Espindola740fafe2015-09-08 19:43:27 +000052 return SharedFiles;
53 }
54
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000055 DefinedRegular<ELFT> *addAbsolute(StringRef Name,
56 uint8_t Visibility = llvm::ELF::STV_HIDDEN);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000057 DefinedRegular<ELFT> *addIgnored(StringRef Name,
58 uint8_t Visibility = llvm::ELF::STV_HIDDEN);
Rui Ueyama79c73732016-01-08 21:53:28 +000059
Peter Collingbourne4f952702016-05-01 04:55:03 +000060 Symbol *addUndefined(StringRef Name);
61 Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
Rafael Espindolacc70da32016-06-15 17:56:10 +000062 uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
Peter Collingbourne4f952702016-05-01 04:55:03 +000063
64 Symbol *addRegular(StringRef Name, const Elf_Sym &Sym,
65 InputSectionBase<ELFT> *Section);
66 Symbol *addRegular(StringRef Name, uint8_t Binding, uint8_t StOther);
Peter Collingbourne6a422592016-05-03 01:21:08 +000067 Symbol *addSynthetic(StringRef N, OutputSectionBase<ELFT> *Section,
Peter Collingbourne4f952702016-05-01 04:55:03 +000068 uintX_t Value);
69 void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
70 const typename ELFT::Verdef *Verdef);
71
72 void addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S);
Rafael Espindola65c65ce2016-06-14 21:56:36 +000073 void addLazyObject(StringRef Name, LazyObjectFile &Obj);
Peter Collingbourne4f952702016-05-01 04:55:03 +000074 Symbol *addBitcode(StringRef Name, bool IsWeak, uint8_t StOther, uint8_t Type,
75 bool CanOmitFromDynSym, BitcodeFile *File);
76
77 Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment,
78 uint8_t Binding, uint8_t StOther, uint8_t Type,
79 InputFile *File);
80
Peter Collingbourne892d49802016-04-27 00:05:03 +000081 void scanUndefinedFlags();
Rui Ueyama93bfee52015-10-13 18:10:33 +000082 void scanShlibUndefined();
Adhemerval Zanella9df07202016-04-13 18:51:11 +000083 void scanDynamicList();
Peter Collingbourne66ac1d62016-04-22 20:21:26 +000084 void scanVersionScript();
Rui Ueyamad60dae8a2016-06-23 07:00:17 +000085 void traceDefined();
86
Rui Ueyamac4aaed92015-10-22 18:49:53 +000087 SymbolBody *find(StringRef Name);
Rui Ueyamadeb15402016-01-07 17:20:07 +000088 void wrap(StringRef Name);
Rafael Espindola5d413262015-10-01 21:22:26 +000089
Michael J. Spencer84487f12015-07-24 21:03:07 +000090private:
Peter Collingbourne4f952702016-05-01 04:55:03 +000091 std::pair<Symbol *, bool> insert(StringRef Name);
92 std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type,
93 uint8_t Visibility, bool CanOmitFromDynSym,
94 bool IsUsedInRegularObj, InputFile *File);
95
96 std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile);
97 void reportDuplicate(SymbolBody *Existing, InputFile *NewFile);
Michael J. Spencer84487f12015-07-24 21:03:07 +000098
Rafael Espindola40102eb2015-09-17 18:26:25 +000099 // The order the global symbols are in is not defined. We can use an arbitrary
100 // order, but it has to be reproducible. That is true even when cross linking.
101 // The default hashing of StringRef produces different results on 32 and 64
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000102 // bit systems so we use a map to a vector. That is arbitrary, deterministic
103 // but a bit inefficient.
Rafael Espindola40102eb2015-09-17 18:26:25 +0000104 // FIXME: Experiment with passing in a custom hashing or sorting the symbols
105 // once symbol resolution is finished.
Rafael Espindola7f0b7272016-04-14 20:42:43 +0000106 llvm::DenseMap<SymName, unsigned> Symtab;
107 std::vector<Symbol *> SymVector;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000108 llvm::BumpPtrAllocator Alloc;
Rafael Espindola222edc62015-09-03 18:56:20 +0000109
Rui Ueyama683564e2016-01-08 22:14:15 +0000110 // Comdat groups define "link once" sections. If two comdat groups have the
111 // same name, only one of them is linked, and the other is ignored. This set
112 // is used to uniquify them.
Rui Ueyama52d3b672016-01-06 02:06:33 +0000113 llvm::DenseSet<StringRef> ComdatGroups;
Rafael Espindola444576d2015-10-09 19:25:07 +0000114
Rui Ueyama52c7c5f2016-01-08 22:20:00 +0000115 // The symbol table owns all file objects.
116 std::vector<std::unique_ptr<ArchiveFile>> ArchiveFiles;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000117 std::vector<std::unique_ptr<ObjectFile<ELFT>>> ObjectFiles;
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000118 std::vector<std::unique_ptr<LazyObjectFile>> LazyObjectFiles;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000119 std::vector<std::unique_ptr<SharedFile<ELFT>>> SharedFiles;
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000120 std::vector<std::unique_ptr<BitcodeFile>> BitcodeFiles;
Rui Ueyama683564e2016-01-08 22:14:15 +0000121
122 // Set of .so files to not link the same shared object file more than once.
Rui Ueyama131e0ff2016-01-08 22:17:42 +0000123 llvm::DenseSet<StringRef> SoNames;
Rui Ueyama25992482016-03-22 20:52:10 +0000124
125 std::unique_ptr<BitcodeCompiler> Lto;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000126};
127
Peter Collingbourne4f952702016-05-01 04:55:03 +0000128template <class ELFT> struct Symtab { static SymbolTable<ELFT> *X; };
129template <class ELFT> SymbolTable<ELFT> *Symtab<ELFT>::X;
130
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000131} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000132} // namespace lld
133
134#endif