blob: de4b716974e5456cc7164fee8b348a61810c8a83 [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- SymbolTable.h --------------------------------------------*- C++ -*-===//
2//
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_WASM_SYMBOL_TABLE_H
11#define LLD_WASM_SYMBOL_TABLE_H
12
13#include "InputFiles.h"
Sam Cleggc729c1b2018-05-30 18:07:52 +000014#include "LTO.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "Symbols.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000016#include "llvm/ADT/CachedHashString.h"
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000017#include "llvm/ADT/DenseSet.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000018#include "llvm/Support/raw_ostream.h"
19
Sam Clegg93102972018-02-23 05:08:53 +000020using llvm::wasm::WasmGlobalType;
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +000021using llvm::wasm::WasmSignature;
Sam Cleggc94d3932017-11-17 18:14:09 +000022
23namespace lld {
24namespace wasm {
25
26class InputSegment;
27
28// SymbolTable is a bucket of all known symbols, including defined,
29// undefined, or lazy symbols (the last one is symbols in archive
30// files whose archive members are not yet loaded).
31//
32// We put all symbols of all files to a SymbolTable, and the
33// SymbolTable selects the "best" symbols if there are name
34// conflicts. For example, obviously, a defined symbol is better than
35// an undefined symbol. Or, if there's a conflict between a lazy and a
36// undefined, it'll read an archive member to read a real definition
37// to replace the lazy symbol. The logic is implemented in the
38// add*() functions, which are called by input files as they are parsed.
39// There is one add* function per symbol type.
40class SymbolTable {
41public:
42 void addFile(InputFile *File);
Sam Cleggc729c1b2018-05-30 18:07:52 +000043 void addCombinedLTOObject();
Sam Cleggc94d3932017-11-17 18:14:09 +000044
45 std::vector<ObjFile *> ObjectFiles;
Sam Cleggc729c1b2018-05-30 18:07:52 +000046 std::vector<BitcodeFile *> BitcodeFiles;
Nicholas Wilsonebda41f2018-03-09 16:43:05 +000047 std::vector<InputFunction *> SyntheticFunctions;
48 std::vector<InputGlobal *> SyntheticGlobals;
Sam Cleggc94d3932017-11-17 18:14:09 +000049
Sam Cleggc94d3932017-11-17 18:14:09 +000050 void reportRemainingUndefines();
51
Sam Clegg74fe0ba2017-12-07 01:51:24 +000052 ArrayRef<Symbol *> getSymbols() const { return SymVector; }
Sam Cleggc94d3932017-11-17 18:14:09 +000053 Symbol *find(StringRef Name);
54
Rui Ueyamae3498ec2018-02-28 00:09:22 +000055 Symbol *addDefinedFunction(StringRef Name, uint32_t Flags, InputFile *File,
Rui Ueyamac61b8342018-02-28 00:37:03 +000056 InputFunction *Function);
Rui Ueyamae3498ec2018-02-28 00:09:22 +000057 Symbol *addDefinedData(StringRef Name, uint32_t Flags, InputFile *File,
Rui Ueyamac61b8342018-02-28 00:37:03 +000058 InputSegment *Segment, uint32_t Address,
59 uint32_t Size);
Rui Ueyamae3498ec2018-02-28 00:09:22 +000060 Symbol *addDefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File,
Sam Clegg93102972018-02-23 05:08:53 +000061 InputGlobal *G);
Rui Ueyamae3498ec2018-02-28 00:09:22 +000062
63 Symbol *addUndefinedFunction(StringRef Name, uint32_t Flags, InputFile *File,
64 const WasmSignature *Signature);
65 Symbol *addUndefinedData(StringRef Name, uint32_t Flags, InputFile *File);
66 Symbol *addUndefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File,
67 const WasmGlobalType *Type);
68
Sam Cleggc94d3932017-11-17 18:14:09 +000069 void addLazy(ArchiveFile *F, const Archive::Symbol *Sym);
Rui Ueyamadcf62342018-03-01 23:29:05 +000070
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000071 bool addComdat(StringRef Name);
Sam Cleggc94d3932017-11-17 18:14:09 +000072
Rui Ueyamac61b8342018-02-28 00:37:03 +000073 DefinedData *addSyntheticDataSymbol(StringRef Name, uint32_t Flags);
Sam Clegg93102972018-02-23 05:08:53 +000074 DefinedGlobal *addSyntheticGlobal(StringRef Name, uint32_t Flags,
75 InputGlobal *Global);
Nicholas Wilsonebda41f2018-03-09 16:43:05 +000076 DefinedFunction *addSyntheticFunction(StringRef Name, uint32_t Flags,
77 InputFunction *Function);
Sam Clegg93102972018-02-23 05:08:53 +000078
Sam Cleggc94d3932017-11-17 18:14:09 +000079private:
Sam Clegg4c2cbfe2018-08-02 20:39:19 +000080 std::pair<Symbol *, bool> insert(StringRef Name, InputFile *File);
Sam Cleggc94d3932017-11-17 18:14:09 +000081
Sam Clegga80d94d2017-11-27 23:16:06 +000082 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
Sam Clegg74fe0ba2017-12-07 01:51:24 +000083 std::vector<Symbol *> SymVector;
Rui Ueyamadcf62342018-03-01 23:29:05 +000084
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000085 llvm::DenseSet<llvm::CachedHashStringRef> Comdats;
Sam Cleggc729c1b2018-05-30 18:07:52 +000086
87 // For LTO.
88 std::unique_ptr<BitcodeCompiler> LTO;
Sam Cleggc94d3932017-11-17 18:14:09 +000089};
90
91extern SymbolTable *Symtab;
92
93} // namespace wasm
94} // namespace lld
95
96#endif