2017-11-18 02:14:09 +08:00
|
|
|
//===- Symbols.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_SYMBOLS_H
|
|
|
|
#define LLD_WASM_SYMBOLS_H
|
|
|
|
|
2018-04-21 01:18:06 +08:00
|
|
|
#include "Config.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
// Shared string constants
|
|
|
|
|
|
|
|
// The default module name to use for symbol imports.
|
2019-07-11 13:40:30 +08:00
|
|
|
extern const char *defaultModule;
|
2019-05-21 17:13:09 +08:00
|
|
|
|
|
|
|
// The name under which to import or export the wasm table.
|
2019-07-11 13:40:30 +08:00
|
|
|
extern const char *functionTableName;
|
2019-05-21 17:13:09 +08:00
|
|
|
|
2018-11-27 09:08:16 +08:00
|
|
|
using llvm::wasm::WasmSymbolType;
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
class InputFile;
|
2018-01-29 03:57:01 +08:00
|
|
|
class InputChunk;
|
2018-02-21 01:45:38 +08:00
|
|
|
class InputSegment;
|
2018-02-17 07:50:23 +08:00
|
|
|
class InputFunction;
|
2018-02-23 13:08:53 +08:00
|
|
|
class InputGlobal;
|
2018-12-08 14:17:43 +08:00
|
|
|
class InputEvent;
|
2018-05-05 07:14:42 +08:00
|
|
|
class InputSection;
|
2019-05-21 17:13:09 +08:00
|
|
|
class OutputSection;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-15 06:55:38 +08:00
|
|
|
#define INVALID_INDEX UINT32_MAX
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
// The base class for real symbol classes.
|
2017-11-18 02:14:09 +08:00
|
|
|
class Symbol {
|
|
|
|
public:
|
2019-07-05 09:27:39 +08:00
|
|
|
enum Kind : uint8_t {
|
2017-11-18 02:14:09 +08:00
|
|
|
DefinedFunctionKind,
|
2018-02-21 07:38:27 +08:00
|
|
|
DefinedDataKind,
|
2018-02-23 13:08:53 +08:00
|
|
|
DefinedGlobalKind,
|
2018-12-08 14:17:43 +08:00
|
|
|
DefinedEventKind,
|
2018-05-05 07:14:42 +08:00
|
|
|
SectionKind,
|
2019-05-21 17:13:09 +08:00
|
|
|
OutputSectionKind,
|
2017-11-18 02:14:09 +08:00
|
|
|
UndefinedFunctionKind,
|
2018-02-21 07:38:27 +08:00
|
|
|
UndefinedDataKind,
|
2018-02-23 13:08:53 +08:00
|
|
|
UndefinedGlobalKind,
|
2018-02-28 08:16:11 +08:00
|
|
|
LazyKind,
|
2017-11-18 02:14:09 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Kind kind() const { return symbolKind; }
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
bool isDefined() const { return !isLazy() && !isUndefined(); }
|
2018-02-28 08:16:11 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
bool isUndefined() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
return symbolKind == UndefinedFunctionKind ||
|
|
|
|
symbolKind == UndefinedDataKind || symbolKind == UndefinedGlobalKind;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2018-02-28 08:16:11 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
bool isLazy() const { return symbolKind == LazyKind; }
|
2018-02-28 08:16:11 +08:00
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
bool isLocal() const;
|
2017-11-18 02:14:09 +08:00
|
|
|
bool isWeak() const;
|
2017-12-03 10:38:04 +08:00
|
|
|
bool isHidden() const;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-06-07 14:00:46 +08:00
|
|
|
// Returns true if this symbol exists in a discarded (due to COMDAT) section
|
|
|
|
bool isDiscarded() const;
|
|
|
|
|
2019-01-30 06:26:31 +08:00
|
|
|
// True if this is an undefined weak symbol. This only works once
|
|
|
|
// all input files have been added.
|
|
|
|
bool isUndefWeak() const {
|
|
|
|
// See comment on lazy symbols for details.
|
|
|
|
return isWeak() && (isUndefined() || isLazy());
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
// Returns the symbol name.
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef getName() const { return name; }
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Returns the file from which this symbol was created.
|
2019-07-11 13:40:30 +08:00
|
|
|
InputFile *getFile() const { return file; }
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
uint32_t getFlags() const { return flags; }
|
2019-02-08 06:42:16 +08:00
|
|
|
|
2018-02-21 01:45:38 +08:00
|
|
|
InputChunk *getChunk() const;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-04-21 01:18:06 +08:00
|
|
|
// Indicates that the section or import for this symbol will be included in
|
|
|
|
// the final image.
|
2018-02-23 13:08:53 +08:00
|
|
|
bool isLive() const;
|
|
|
|
|
2018-04-21 01:18:06 +08:00
|
|
|
// Marks the symbol's InputChunk as Live, so that it will be included in the
|
|
|
|
// final image.
|
|
|
|
void markLive();
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void setHidden(bool isHidden);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
// Get/set the index in the output symbol table. This is only used for
|
|
|
|
// relocatable output.
|
2018-02-23 13:08:53 +08:00
|
|
|
uint32_t getOutputSymbolIndex() const;
|
2019-07-11 13:40:30 +08:00
|
|
|
void setOutputSymbolIndex(uint32_t index);
|
2018-02-23 13:08:53 +08:00
|
|
|
|
|
|
|
WasmSymbolType getWasmType() const;
|
2018-06-29 01:04:58 +08:00
|
|
|
bool isExported() const;
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
const WasmSignature* getSignature() const;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
bool isInGOT() const { return gotIndex != INVALID_INDEX; }
|
2019-03-27 03:46:15 +08:00
|
|
|
|
|
|
|
uint32_t getGOTIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(gotIndex != INVALID_INDEX);
|
|
|
|
return gotIndex;
|
2019-03-27 03:46:15 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void setGOTIndex(uint32_t index);
|
|
|
|
bool hasGOTIndex() const { return gotIndex != INVALID_INDEX; }
|
2019-03-27 03:46:15 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
protected:
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol(StringRef name, Kind k, uint32_t flags, InputFile *f)
|
|
|
|
: name(name), file(f), flags(flags), symbolKind(k),
|
|
|
|
referenced(!config->gcSections), isUsedInRegularObj(false),
|
|
|
|
forceExport(false), canInline(false), traced(false) {}
|
|
|
|
|
|
|
|
StringRef name;
|
|
|
|
InputFile *file;
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t outputSymbolIndex = INVALID_INDEX;
|
|
|
|
uint32_t gotIndex = INVALID_INDEX;
|
|
|
|
Kind symbolKind;
|
2019-07-05 09:27:39 +08:00
|
|
|
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
bool referenced : 1;
|
2019-07-08 18:35:08 +08:00
|
|
|
|
2019-07-05 09:27:39 +08:00
|
|
|
// True if the symbol was used for linking and thus need to be added to the
|
|
|
|
// output file's symbol table. This is true for all symbols except for
|
|
|
|
// unreferenced DSO symbols, lazy (archive) symbols, and bitcode symbols that
|
|
|
|
// are unreferenced except by other bitcode objects.
|
2019-07-11 13:40:30 +08:00
|
|
|
bool isUsedInRegularObj : 1;
|
2019-07-05 09:27:39 +08:00
|
|
|
|
|
|
|
// True if ths symbol is explicity marked for export (i.e. via the -e/--export
|
|
|
|
// command line flag)
|
2019-07-11 13:40:30 +08:00
|
|
|
bool forceExport : 1;
|
2019-07-05 09:27:39 +08:00
|
|
|
|
|
|
|
// False if LTO shouldn't inline whatever this symbol points to. If a symbol
|
|
|
|
// is overwritten after LTO, LTO shouldn't inline the symbol because it
|
|
|
|
// doesn't know the final contents of the symbol.
|
2019-07-11 13:40:30 +08:00
|
|
|
bool canInline : 1;
|
2019-07-05 09:27:39 +08:00
|
|
|
|
|
|
|
// True if this symbol is specified by --trace-symbol option.
|
2019-07-11 13:40:30 +08:00
|
|
|
bool traced : 1;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class FunctionSymbol : public Symbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == DefinedFunctionKind ||
|
|
|
|
s->kind() == UndefinedFunctionKind;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
// Get/set the table index
|
2019-07-11 13:40:30 +08:00
|
|
|
void setTableIndex(uint32_t index);
|
2018-01-25 05:45:25 +08:00
|
|
|
uint32_t getTableIndex() const;
|
|
|
|
bool hasTableIndex() const;
|
2017-12-12 06:00:56 +08:00
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
// Get/set the function index
|
|
|
|
uint32_t getFunctionIndex() const;
|
2019-07-11 13:40:30 +08:00
|
|
|
void setFunctionIndex(uint32_t index);
|
2018-03-13 03:56:23 +08:00
|
|
|
bool hasFunctionIndex() const;
|
2017-12-12 06:00:56 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmSignature *signature;
|
2018-06-29 00:53:53 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
protected:
|
2019-07-11 13:40:30 +08:00
|
|
|
FunctionSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
|
|
|
|
const WasmSignature *sig)
|
|
|
|
: Symbol(name, k, flags, f), signature(sig) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
uint32_t tableIndex = INVALID_INDEX;
|
|
|
|
uint32_t functionIndex = INVALID_INDEX;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DefinedFunction : public FunctionSymbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
|
|
|
|
InputFunction *function);
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == DefinedFunctionKind;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
2018-02-21 01:45:38 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
InputFunction *function;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class UndefinedFunction : public FunctionSymbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
UndefinedFunction(StringRef name, StringRef importName,
|
|
|
|
StringRef importModule, uint32_t flags,
|
|
|
|
InputFile *file = nullptr,
|
|
|
|
const WasmSignature *type = nullptr,
|
|
|
|
bool isCalledDirectly = true)
|
|
|
|
: FunctionSymbol(name, UndefinedFunctionKind, flags, file, type),
|
|
|
|
importName(importName), importModule(importModule), isCalledDirectly(isCalledDirectly) {}
|
|
|
|
|
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == UndefinedFunctionKind;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
2019-02-01 10:29:57 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef importName;
|
|
|
|
StringRef importModule;
|
|
|
|
bool isCalledDirectly;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
// Section symbols for output sections are different from those for input
|
|
|
|
// section. These are generated by the linker and point the OutputSection
|
|
|
|
// rather than an InputSection.
|
|
|
|
class OutputSectionSymbol : public Symbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
OutputSectionSymbol(const OutputSection *s)
|
2019-05-21 17:13:09 +08:00
|
|
|
: Symbol("", OutputSectionKind, llvm::wasm::WASM_SYMBOL_BINDING_LOCAL,
|
|
|
|
nullptr),
|
2019-07-11 13:40:30 +08:00
|
|
|
section(s) {}
|
2019-05-21 17:13:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == OutputSectionKind;
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const OutputSection *section;
|
2019-05-21 17:13:09 +08:00
|
|
|
};
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
class SectionSymbol : public Symbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
SectionSymbol(uint32_t flags, const InputSection *s, InputFile *f = nullptr)
|
|
|
|
: Symbol("", SectionKind, flags, f), section(s) {}
|
2019-05-21 17:13:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == SectionKind; }
|
2018-05-05 07:14:42 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
const OutputSectionSymbol *getOutputSectionSymbol() const;
|
2018-05-05 07:14:42 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const InputSection *section;
|
2018-05-05 07:14:42 +08:00
|
|
|
};
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
class DataSymbol : public Symbol {
|
2018-02-15 02:27:59 +08:00
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == DefinedDataKind || s->kind() == UndefinedDataKind;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-07-11 13:40:30 +08:00
|
|
|
DataSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f)
|
|
|
|
: Symbol(name, k, flags, f) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
class DefinedData : public DataSymbol {
|
2018-02-15 02:27:59 +08:00
|
|
|
public:
|
2018-04-27 13:50:40 +08:00
|
|
|
// Constructor for regular data symbols originating from input files.
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedData(StringRef name, uint32_t flags, InputFile *f,
|
|
|
|
InputSegment *segment, uint32_t offset, uint32_t size)
|
|
|
|
: DataSymbol(name, DefinedDataKind, flags, f), segment(segment),
|
|
|
|
offset(offset), size(size) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
// Constructor for linker synthetic data symbols.
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedData(StringRef name, uint32_t flags)
|
|
|
|
: DataSymbol(name, DefinedDataKind, flags, nullptr) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == DefinedDataKind; }
|
2018-02-23 13:08:53 +08:00
|
|
|
|
|
|
|
// Returns the output virtual address of a defined data symbol.
|
2018-01-09 07:39:11 +08:00
|
|
|
uint32_t getVirtualAddress() const;
|
2019-07-11 13:40:30 +08:00
|
|
|
void setVirtualAddress(uint32_t va);
|
2017-12-06 03:05:45 +08:00
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
// Returns the offset of a defined data symbol within its OutputSegment.
|
|
|
|
uint32_t getOutputSegmentOffset() const;
|
|
|
|
uint32_t getOutputSegmentIndex() const;
|
2019-07-11 13:40:30 +08:00
|
|
|
uint32_t getSize() const { return size; }
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
InputSegment *segment = nullptr;
|
2018-02-21 01:45:38 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
protected:
|
2019-07-11 13:40:30 +08:00
|
|
|
uint32_t offset = 0;
|
|
|
|
uint32_t size = 0;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
class UndefinedData : public DataSymbol {
|
2018-02-15 02:27:59 +08:00
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
UndefinedData(StringRef name, uint32_t flags, InputFile *file = nullptr)
|
|
|
|
: DataSymbol(name, UndefinedDataKind, flags, file) {}
|
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == UndefinedDataKind;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
class GlobalSymbol : public Symbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == DefinedGlobalKind || s->kind() == UndefinedGlobalKind;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmGlobalType *getGlobalType() const { return globalType; }
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
// Get/set the global index
|
|
|
|
uint32_t getGlobalIndex() const;
|
2019-07-11 13:40:30 +08:00
|
|
|
void setGlobalIndex(uint32_t index);
|
2018-03-13 03:56:23 +08:00
|
|
|
bool hasGlobalIndex() const;
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
protected:
|
2019-07-11 13:40:30 +08:00
|
|
|
GlobalSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
|
|
|
|
const WasmGlobalType *globalType)
|
|
|
|
: Symbol(name, k, flags, f), globalType(globalType) {}
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmGlobalType *globalType;
|
|
|
|
uint32_t globalIndex = INVALID_INDEX;
|
2018-02-23 13:08:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DefinedGlobal : public GlobalSymbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputGlobal *global);
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == DefinedGlobalKind;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
InputGlobal *global;
|
2018-02-23 13:08:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class UndefinedGlobal : public GlobalSymbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
UndefinedGlobal(StringRef name, StringRef importName, StringRef importModule,
|
|
|
|
uint32_t flags, InputFile *file = nullptr,
|
|
|
|
const WasmGlobalType *type = nullptr)
|
|
|
|
: GlobalSymbol(name, UndefinedGlobalKind, flags, file, type),
|
|
|
|
importName(importName), importModule(importModule) {}
|
|
|
|
|
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == UndefinedGlobalKind;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
2019-02-08 06:00:48 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef importName;
|
|
|
|
StringRef importModule;
|
2018-02-23 13:08:53 +08:00
|
|
|
};
|
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
// Wasm events are features that suspend the current execution and transfer the
|
|
|
|
// control flow to a corresponding handler. Currently the only supported event
|
|
|
|
// kind is exceptions.
|
|
|
|
//
|
|
|
|
// Event tags are values to distinguish different events. For exceptions, they
|
|
|
|
// can be used to distinguish different language's exceptions, i.e., all C++
|
|
|
|
// exceptions have the same tag. Wasm can generate code capable of doing
|
|
|
|
// different handling actions based on the tag of caught exceptions.
|
|
|
|
//
|
|
|
|
// A single EventSymbol object represents a single tag. C++ exception event
|
|
|
|
// symbol is a weak symbol generated in every object file in which exceptions
|
|
|
|
// are used, and has name '__cpp_exception' for linking.
|
|
|
|
class EventSymbol : public Symbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == DefinedEventKind; }
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmEventType *getEventType() const { return eventType; }
|
2018-12-08 14:17:43 +08:00
|
|
|
|
|
|
|
// Get/set the event index
|
|
|
|
uint32_t getEventIndex() const;
|
2019-07-11 13:40:30 +08:00
|
|
|
void setEventIndex(uint32_t index);
|
2018-12-08 14:17:43 +08:00
|
|
|
bool hasEventIndex() const;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmSignature *signature;
|
2018-12-08 14:17:43 +08:00
|
|
|
|
|
|
|
protected:
|
2019-07-11 13:40:30 +08:00
|
|
|
EventSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
|
|
|
|
const WasmEventType *eventType, const WasmSignature *sig)
|
|
|
|
: Symbol(name, k, flags, f), signature(sig), eventType(eventType) {}
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmEventType *eventType;
|
|
|
|
uint32_t eventIndex = INVALID_INDEX;
|
2018-12-08 14:17:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DefinedEvent : public EventSymbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedEvent(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputEvent *event);
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == DefinedEventKind; }
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
InputEvent *event;
|
2018-12-08 14:17:43 +08:00
|
|
|
};
|
|
|
|
|
2019-01-30 06:26:31 +08:00
|
|
|
// LazySymbol represents a symbol that is not yet in the link, but we know where
|
|
|
|
// to find it if needed. If the resolver finds both Undefined and Lazy for the
|
|
|
|
// same name, it will ask the Lazy to load a file.
|
|
|
|
//
|
|
|
|
// A special complication is the handling of weak undefined symbols. They should
|
|
|
|
// not load a file, but we have to remember we have seen both the weak undefined
|
|
|
|
// and the lazy. We represent that with a lazy symbol with a weak binding. This
|
|
|
|
// means that code looking for undefined symbols normally also has to take lazy
|
|
|
|
// symbols into consideration.
|
2018-02-15 02:27:59 +08:00
|
|
|
class LazySymbol : public Symbol {
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
LazySymbol(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
const llvm::object::Archive::Symbol &sym)
|
|
|
|
: Symbol(name, LazyKind, flags, file), archiveSymbol(sym) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == LazyKind; }
|
2018-03-01 06:51:51 +08:00
|
|
|
void fetch();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-01-30 06:26:31 +08:00
|
|
|
// Lazy symbols can have a signature because they can replace an
|
|
|
|
// UndefinedFunction which which case we need to be able to preserve the
|
|
|
|
// signture.
|
|
|
|
// TODO(sbc): This repetition of the signature field is inelegant. Revisit
|
|
|
|
// the use of class hierarchy to represent symbol taxonomy.
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmSignature *signature = nullptr;
|
2019-01-30 06:26:31 +08:00
|
|
|
|
2018-03-01 06:51:51 +08:00
|
|
|
private:
|
2019-07-11 13:40:30 +08:00
|
|
|
llvm::object::Archive::Symbol archiveSymbol;
|
2017-11-18 02:14:09 +08:00
|
|
|
};
|
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
// linker-generated symbols
|
|
|
|
struct WasmSym {
|
2019-06-27 04:12:33 +08:00
|
|
|
// __global_base
|
|
|
|
// Symbol marking the start of the global section.
|
2019-07-11 13:40:30 +08:00
|
|
|
static DefinedData *globalBase;
|
2019-06-27 04:12:33 +08:00
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
// __stack_pointer
|
2018-02-07 11:04:53 +08:00
|
|
|
// Global that holds the address of the top of the explicit value stack in
|
|
|
|
// linear memory.
|
2019-07-11 13:40:30 +08:00
|
|
|
static GlobalSymbol *stackPointer;
|
2018-02-03 06:59:56 +08:00
|
|
|
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
// __tls_base
|
|
|
|
// Global that holds the address of the base of the current thread's
|
|
|
|
// TLS block.
|
|
|
|
static GlobalSymbol *tlsBase;
|
|
|
|
|
|
|
|
// __tls_size
|
|
|
|
// Symbol whose value is the size of the TLS block.
|
|
|
|
static GlobalSymbol *tlsSize;
|
|
|
|
|
[WebAssembly] Compute and export TLS block alignment
Summary:
Add immutable WASM global `__tls_align` which stores the alignment
requirements of the TLS segment.
Add `__builtin_wasm_tls_align()` intrinsic to get this alignment in Clang.
The expected usage has now changed to:
__wasm_init_tls(memalign(__builtin_wasm_tls_align(),
__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, sbc100, sunfish, alexcrichton
Reviewed By: tlively
Subscribers: dschuff, jgravelle-google, hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D65028
llvm-svn: 366624
2019-07-20 07:34:16 +08:00
|
|
|
// __tls_size
|
|
|
|
// Symbol whose value is the alignment of the TLS block.
|
|
|
|
static GlobalSymbol *tlsAlign;
|
|
|
|
|
2018-02-07 11:04:53 +08:00
|
|
|
// __data_end
|
|
|
|
// Symbol marking the end of the data and bss.
|
2019-07-11 13:40:30 +08:00
|
|
|
static DefinedData *dataEnd;
|
2018-02-07 11:04:53 +08:00
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
// __heap_base
|
2018-02-07 11:04:53 +08:00
|
|
|
// Symbol marking the end of the data, bss and explicit stack. Any linear
|
|
|
|
// memory following this address is not used by the linked code and can
|
|
|
|
// therefore be used as a backing store for brk()/malloc() implementations.
|
2019-07-11 13:40:30 +08:00
|
|
|
static DefinedData *heapBase;
|
2018-02-03 06:59:56 +08:00
|
|
|
|
|
|
|
// __wasm_call_ctors
|
|
|
|
// Function that directly calls all ctors in priority order.
|
2019-07-11 13:40:30 +08:00
|
|
|
static DefinedFunction *callCtors;
|
2018-02-03 06:59:56 +08:00
|
|
|
|
2019-07-04 06:04:54 +08:00
|
|
|
// __wasm_init_memory
|
|
|
|
// Function that initializes passive data segments post-instantiation.
|
2019-07-11 13:40:30 +08:00
|
|
|
static DefinedFunction *initMemory;
|
2019-07-04 06:04:54 +08:00
|
|
|
|
2019-04-05 02:40:51 +08:00
|
|
|
// __wasm_apply_relocs
|
|
|
|
// Function that applies relocations to data segment post-instantiation.
|
2019-07-11 13:40:30 +08:00
|
|
|
static DefinedFunction *applyRelocs;
|
2019-04-05 02:40:51 +08:00
|
|
|
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-17 06:00:45 +08:00
|
|
|
// __wasm_init_tls
|
|
|
|
// Function that allocates thread-local storage and initializes it.
|
|
|
|
static DefinedFunction *initTLS;
|
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
// __dso_handle
|
2018-02-21 07:38:27 +08:00
|
|
|
// Symbol used in calls to __cxa_atexit to determine current DLL
|
2019-07-11 13:40:30 +08:00
|
|
|
static DefinedData *dsoHandle;
|
2018-11-15 08:37:21 +08:00
|
|
|
|
|
|
|
// __table_base
|
|
|
|
// Used in PIC code for offset of indirect function table
|
2019-07-11 13:40:30 +08:00
|
|
|
static UndefinedGlobal *tableBase;
|
2018-11-15 08:37:21 +08:00
|
|
|
|
|
|
|
// __memory_base
|
|
|
|
// Used in PIC code for offset of global data
|
2019-07-11 13:40:30 +08:00
|
|
|
static UndefinedGlobal *memoryBase;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// A buffer class that is large enough to hold any Symbol-derived
|
|
|
|
// object. We allocate memory using this class and instantiate a symbol
|
|
|
|
// using the placement new.
|
|
|
|
union SymbolUnion {
|
2019-07-11 13:40:30 +08:00
|
|
|
alignas(DefinedFunction) char a[sizeof(DefinedFunction)];
|
|
|
|
alignas(DefinedData) char b[sizeof(DefinedData)];
|
|
|
|
alignas(DefinedGlobal) char c[sizeof(DefinedGlobal)];
|
|
|
|
alignas(DefinedEvent) char d[sizeof(DefinedEvent)];
|
|
|
|
alignas(LazySymbol) char e[sizeof(LazySymbol)];
|
|
|
|
alignas(UndefinedFunction) char f[sizeof(UndefinedFunction)];
|
|
|
|
alignas(UndefinedData) char g[sizeof(UndefinedData)];
|
|
|
|
alignas(UndefinedGlobal) char h[sizeof(UndefinedGlobal)];
|
|
|
|
alignas(SectionSymbol) char i[sizeof(SectionSymbol)];
|
2018-02-03 06:59:56 +08:00
|
|
|
};
|
|
|
|
|
2019-07-08 15:30:07 +08:00
|
|
|
// It is important to keep the size of SymbolUnion small for performance and
|
|
|
|
// memory usage reasons. 96 bytes is a soft limit based on the size of
|
|
|
|
// UndefinedFunction on a 64-bit system.
|
|
|
|
static_assert(sizeof(SymbolUnion) <= 96, "SymbolUnion too large");
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void printTraceSymbol(Symbol *sym);
|
|
|
|
void printTraceSymbolUndefined(StringRef name, const InputFile* file);
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
template <typename T, typename... ArgT>
|
2019-07-11 13:40:30 +08:00
|
|
|
T *replaceSymbol(Symbol *s, ArgT &&... arg) {
|
2018-02-15 06:55:38 +08:00
|
|
|
static_assert(std::is_trivially_destructible<T>(),
|
|
|
|
"Symbol types must be trivially destructible");
|
2018-11-20 07:31:28 +08:00
|
|
|
static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
|
2018-02-15 02:27:59 +08:00
|
|
|
static_assert(alignof(T) <= alignof(SymbolUnion),
|
|
|
|
"SymbolUnion not aligned enough");
|
2018-02-15 06:43:43 +08:00
|
|
|
assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
|
|
|
|
"Not a Symbol");
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol symCopy = *s;
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
T *s2 = new (s) T(std::forward<ArgT>(arg)...);
|
|
|
|
s2->isUsedInRegularObj = symCopy.isUsedInRegularObj;
|
|
|
|
s2->forceExport = symCopy.forceExport;
|
|
|
|
s2->canInline = symCopy.canInline;
|
|
|
|
s2->traced = symCopy.traced;
|
2019-02-06 10:35:18 +08:00
|
|
|
|
|
|
|
// Print out a log message if --trace-symbol was specified.
|
|
|
|
// This is for debugging.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (s2->traced)
|
|
|
|
printTraceSymbol(s2);
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return s2;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
} // namespace wasm
|
|
|
|
|
|
|
|
// Returns a symbol name for an error message.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::string toString(const wasm::Symbol &sym);
|
|
|
|
std::string toString(wasm::Symbol::Kind kind);
|
|
|
|
std::string maybeDemangleSymbol(StringRef name);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|