2017-11-18 02:14:09 +08:00
|
|
|
//===- SymbolTable.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_WASM_SYMBOL_TABLE_H
|
|
|
|
#define LLD_WASM_SYMBOL_TABLE_H
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
2018-05-31 02:07:52 +08:00
|
|
|
#include "LTO.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "Symbols.h"
|
2018-11-27 09:08:16 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/ADT/CachedHashString.h"
|
2018-03-14 23:45:11 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2020-02-06 13:18:55 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2020-05-02 00:14:59 +08:00
|
|
|
#include "llvm/BinaryFormat/WasmTraits.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class InputSegment;
|
|
|
|
|
|
|
|
// SymbolTable is a bucket of all known symbols, including defined,
|
|
|
|
// undefined, or lazy symbols (the last one is symbols in archive
|
|
|
|
// files whose archive members are not yet loaded).
|
|
|
|
//
|
|
|
|
// We put all symbols of all files to a SymbolTable, and the
|
|
|
|
// SymbolTable selects the "best" symbols if there are name
|
|
|
|
// conflicts. For example, obviously, a defined symbol is better than
|
|
|
|
// an undefined symbol. Or, if there's a conflict between a lazy and a
|
|
|
|
// undefined, it'll read an archive member to read a real definition
|
|
|
|
// to replace the lazy symbol. The logic is implemented in the
|
|
|
|
// add*() functions, which are called by input files as they are parsed.
|
|
|
|
// There is one add* function per symbol type.
|
|
|
|
class SymbolTable {
|
|
|
|
public:
|
2019-05-24 22:14:25 +08:00
|
|
|
void wrap(Symbol *sym, Symbol *real, Symbol *wrap);
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void addFile(InputFile *file);
|
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
void addCombinedLTOObject();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2017-12-07 09:51:24 +08:00
|
|
|
ArrayRef<Symbol *> getSymbols() const { return symVector; }
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *find(StringRef name);
|
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
void replace(StringRef name, Symbol* sym);
|
|
|
|
|
2019-02-06 10:35:18 +08:00
|
|
|
void trace(StringRef name);
|
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
Symbol *addDefinedFunction(StringRef name, uint32_t flags, InputFile *file,
|
2018-02-28 08:37:03 +08:00
|
|
|
InputFunction *function);
|
2018-02-28 08:09:22 +08:00
|
|
|
Symbol *addDefinedData(StringRef name, uint32_t flags, InputFile *file,
|
2021-05-15 07:25:04 +08:00
|
|
|
InputChunk *segment, uint64_t address, uint64_t size);
|
2018-02-28 08:09:22 +08:00
|
|
|
Symbol *addDefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
|
2018-02-23 13:08:53 +08:00
|
|
|
InputGlobal *g);
|
2021-06-15 16:49:43 +08:00
|
|
|
Symbol *addDefinedTag(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputTag *t);
|
2021-01-05 19:08:58 +08:00
|
|
|
Symbol *addDefinedTable(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputTable *t);
|
2018-02-28 08:09:22 +08:00
|
|
|
|
2020-02-06 13:18:55 +08:00
|
|
|
Symbol *addUndefinedFunction(StringRef name,
|
|
|
|
llvm::Optional<StringRef> importName,
|
|
|
|
llvm::Optional<StringRef> importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
|
|
|
const WasmSignature *signature,
|
2019-05-25 06:45:08 +08:00
|
|
|
bool isCalledDirectly);
|
2018-02-28 08:09:22 +08:00
|
|
|
Symbol *addUndefinedData(StringRef name, uint32_t flags, InputFile *file);
|
2020-02-06 13:18:55 +08:00
|
|
|
Symbol *addUndefinedGlobal(StringRef name,
|
|
|
|
llvm::Optional<StringRef> importName,
|
|
|
|
llvm::Optional<StringRef> importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
|
|
|
const WasmGlobalType *type);
|
2021-01-05 19:08:58 +08:00
|
|
|
Symbol *addUndefinedTable(StringRef name,
|
|
|
|
llvm::Optional<StringRef> importName,
|
|
|
|
llvm::Optional<StringRef> importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
|
|
|
const WasmTableType *type);
|
2018-02-28 08:09:22 +08:00
|
|
|
|
2021-03-03 18:13:25 +08:00
|
|
|
TableSymbol *resolveIndirectFunctionTable(bool required);
|
|
|
|
|
2018-11-27 09:08:16 +08:00
|
|
|
void addLazy(ArchiveFile *f, const llvm::object::Archive::Symbol *sym);
|
2018-03-02 07:29:05 +08:00
|
|
|
|
2018-03-14 23:45:11 +08:00
|
|
|
bool addComdat(StringRef name);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-28 08:37:03 +08:00
|
|
|
DefinedData *addSyntheticDataSymbol(StringRef name, uint32_t flags);
|
2018-02-23 13:08:53 +08:00
|
|
|
DefinedGlobal *addSyntheticGlobal(StringRef name, uint32_t flags,
|
|
|
|
InputGlobal *global);
|
2018-03-10 00:43:05 +08:00
|
|
|
DefinedFunction *addSyntheticFunction(StringRef name, uint32_t flags,
|
|
|
|
InputFunction *function);
|
2020-08-08 04:24:43 +08:00
|
|
|
DefinedData *addOptionalDataSymbol(StringRef name, uint64_t value = 0);
|
2021-02-09 09:12:23 +08:00
|
|
|
DefinedGlobal *addOptionalGlobalSymbol(StringRef name, InputGlobal *global);
|
2021-01-14 17:15:56 +08:00
|
|
|
DefinedTable *addSyntheticTable(StringRef name, uint32_t flags,
|
|
|
|
InputTable *global);
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
void handleSymbolVariants();
|
2019-02-08 06:42:16 +08:00
|
|
|
void handleWeakUndefines();
|
2020-05-02 00:14:59 +08:00
|
|
|
DefinedFunction *createUndefinedStub(const WasmSignature &sig);
|
2019-02-08 06:42:16 +08:00
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
std::vector<ObjFile *> objectFiles;
|
|
|
|
std::vector<SharedFile *> sharedFiles;
|
|
|
|
std::vector<BitcodeFile *> bitcodeFiles;
|
|
|
|
std::vector<InputFunction *> syntheticFunctions;
|
|
|
|
std::vector<InputGlobal *> syntheticGlobals;
|
2021-01-14 17:15:56 +08:00
|
|
|
std::vector<InputTable *> syntheticTables;
|
2019-05-24 22:14:25 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
private:
|
2019-02-08 06:42:16 +08:00
|
|
|
std::pair<Symbol *, bool> insert(StringRef name, const InputFile *file);
|
2019-02-06 10:35:18 +08:00
|
|
|
std::pair<Symbol *, bool> insertName(StringRef name);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
bool getFunctionVariant(Symbol* sym, const WasmSignature *sig,
|
|
|
|
const InputFile *file, Symbol **out);
|
2019-02-08 06:42:16 +08:00
|
|
|
InputFunction *replaceWithUnreachable(Symbol *sym, const WasmSignature &sig,
|
|
|
|
StringRef debugName);
|
2020-05-02 00:14:59 +08:00
|
|
|
void replaceWithUndefined(Symbol *sym);
|
2019-02-08 06:42:16 +08:00
|
|
|
|
2021-03-03 18:13:25 +08:00
|
|
|
TableSymbol *createDefinedIndirectFunctionTable(StringRef name);
|
|
|
|
TableSymbol *createUndefinedIndirectFunctionTable(StringRef name);
|
|
|
|
|
2019-07-16 16:08:17 +08:00
|
|
|
// Maps symbol names to index into the symVector. -1 means that symbols
|
2019-02-06 10:35:18 +08:00
|
|
|
// is to not yet in the vector but it should have tracing enabled if it is
|
|
|
|
// ever added.
|
|
|
|
llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
|
2017-12-07 09:51:24 +08:00
|
|
|
std::vector<Symbol *> symVector;
|
2018-03-02 07:29:05 +08:00
|
|
|
|
2020-04-02 00:21:08 +08:00
|
|
|
// For certain symbols types, e.g. function symbols, we allow for multiple
|
2019-02-21 07:19:31 +08:00
|
|
|
// variants of the same symbol with different signatures.
|
|
|
|
llvm::DenseMap<llvm::CachedHashStringRef, std::vector<Symbol *>> symVariants;
|
2020-05-02 00:14:59 +08:00
|
|
|
llvm::DenseMap<WasmSignature, DefinedFunction *> stubFunctions;
|
2019-02-21 07:19:31 +08:00
|
|
|
|
2019-05-16 00:03:28 +08:00
|
|
|
// Comdat groups define "link once" sections. If two comdat groups have the
|
|
|
|
// same name, only one of them is linked, and the other is ignored. This set
|
|
|
|
// is used to uniquify them.
|
|
|
|
llvm::DenseSet<llvm::CachedHashStringRef> comdatGroups;
|
2018-05-31 02:07:52 +08:00
|
|
|
|
|
|
|
// For LTO.
|
|
|
|
std::unique_ptr<BitcodeCompiler> lto;
|
2017-11-18 02:14:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
extern SymbolTable *symtab;
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|