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.
|
|
|
|
extern const char *DefaultModule;
|
|
|
|
|
|
|
|
// The name under which to import or export the wasm table.
|
|
|
|
extern const char *FunctionTableName;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-02-17 04:26:15 +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 {
|
2018-02-23 13:08:53 +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
|
|
|
|
|
|
|
bool isLazy() const { return SymbolKind == LazyKind; }
|
|
|
|
|
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.
|
|
|
|
StringRef getName() const { return Name; }
|
|
|
|
|
|
|
|
// Returns the file from which this symbol was created.
|
|
|
|
InputFile *getFile() const { return File; }
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-02-08 06:42:16 +08:00
|
|
|
uint32_t getFlags() const { return Flags; }
|
|
|
|
|
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();
|
|
|
|
|
2018-01-13 06:10:35 +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;
|
|
|
|
void setOutputSymbolIndex(uint32_t Index);
|
|
|
|
|
|
|
|
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-03-27 03:46:15 +08:00
|
|
|
bool isInGOT() const { return GOTIndex != INVALID_INDEX; }
|
|
|
|
|
|
|
|
uint32_t getGOTIndex() const {
|
|
|
|
assert(GOTIndex != INVALID_INDEX);
|
|
|
|
return GOTIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setGOTIndex(uint32_t Index);
|
|
|
|
bool hasGOTIndex() const { return GOTIndex != INVALID_INDEX; }
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
protected:
|
2018-02-21 01:45:38 +08:00
|
|
|
Symbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F)
|
2019-07-05 09:27:39 +08:00
|
|
|
: Name(Name), File(F), Flags(Flags), SymbolKind(K),
|
|
|
|
Referenced(!Config->GcSections), IsUsedInRegularObj(false),
|
|
|
|
ForceExport(false), CanInline(false), Traced(false) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
|
|
|
StringRef Name;
|
|
|
|
InputFile *File;
|
2019-07-05 09:27:39 +08:00
|
|
|
uint32_t Flags;
|
2018-02-23 13:08:53 +08:00
|
|
|
uint32_t OutputSymbolIndex = INVALID_INDEX;
|
2019-03-27 03:46:15 +08:00
|
|
|
uint32_t GOTIndex = INVALID_INDEX;
|
2019-07-05 09:27:39 +08:00
|
|
|
Kind SymbolKind;
|
|
|
|
|
|
|
|
public:
|
2019-07-08 18:35:08 +08:00
|
|
|
bool Referenced : 1;
|
|
|
|
|
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-08 15:30:07 +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-08 15:30:07 +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-08 15:30:07 +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-08 15:30:07 +08:00
|
|
|
bool Traced : 1;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class FunctionSymbol : public Symbol {
|
|
|
|
public:
|
|
|
|
static bool classof(const Symbol *S) {
|
|
|
|
return S->kind() == DefinedFunctionKind ||
|
|
|
|
S->kind() == UndefinedFunctionKind;
|
|
|
|
}
|
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
// Get/set the table index
|
|
|
|
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;
|
|
|
|
void setFunctionIndex(uint32_t Index);
|
|
|
|
bool hasFunctionIndex() const;
|
2017-12-12 06:00:56 +08:00
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
const WasmSignature *Signature;
|
2018-06-29 00:53:53 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
protected:
|
2018-02-17 07:50:23 +08:00
|
|
|
FunctionSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F,
|
2018-12-08 14:17:43 +08:00
|
|
|
const WasmSignature *Sig)
|
|
|
|
: Symbol(Name, K, Flags, F), Signature(Sig) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2018-02-15 06:55:38 +08:00
|
|
|
uint32_t TableIndex = INVALID_INDEX;
|
2018-03-13 03:56:23 +08:00
|
|
|
uint32_t FunctionIndex = INVALID_INDEX;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DefinedFunction : public FunctionSymbol {
|
|
|
|
public:
|
2018-02-17 07:50:23 +08:00
|
|
|
DefinedFunction(StringRef Name, uint32_t Flags, InputFile *F,
|
2018-02-21 01:45:38 +08:00
|
|
|
InputFunction *Function);
|
2018-02-15 02:27:59 +08:00
|
|
|
|
|
|
|
static bool classof(const Symbol *S) {
|
|
|
|
return S->kind() == DefinedFunctionKind;
|
|
|
|
}
|
2018-02-21 01:45:38 +08:00
|
|
|
|
|
|
|
InputFunction *Function;
|
2018-02-15 02:27:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class UndefinedFunction : public FunctionSymbol {
|
|
|
|
public:
|
2019-02-08 06:00:48 +08:00
|
|
|
UndefinedFunction(StringRef Name, StringRef ImportName,
|
|
|
|
StringRef ImportModule, uint32_t Flags,
|
2019-02-01 10:29:57 +08:00
|
|
|
InputFile *File = nullptr,
|
2019-05-25 06:45:08 +08:00
|
|
|
const WasmSignature *Type = nullptr,
|
|
|
|
bool IsCalledDirectly = true)
|
2019-02-01 10:29:57 +08:00
|
|
|
: FunctionSymbol(Name, UndefinedFunctionKind, Flags, File, Type),
|
2019-05-25 06:45:08 +08:00
|
|
|
ImportName(ImportName), ImportModule(ImportModule), IsCalledDirectly(IsCalledDirectly) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
|
|
|
static bool classof(const Symbol *S) {
|
|
|
|
return S->kind() == UndefinedFunctionKind;
|
|
|
|
}
|
2019-02-01 10:29:57 +08:00
|
|
|
|
2019-02-08 06:00:48 +08:00
|
|
|
StringRef ImportName;
|
|
|
|
StringRef ImportModule;
|
2019-05-25 06:45:08 +08:00
|
|
|
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:
|
|
|
|
OutputSectionSymbol(const OutputSection *S)
|
|
|
|
: Symbol("", OutputSectionKind, llvm::wasm::WASM_SYMBOL_BINDING_LOCAL,
|
|
|
|
nullptr),
|
|
|
|
Section(S) {}
|
|
|
|
|
|
|
|
static bool classof(const Symbol *S) {
|
|
|
|
return S->kind() == OutputSectionKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OutputSection *Section;
|
|
|
|
};
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
class SectionSymbol : public Symbol {
|
|
|
|
public:
|
2019-05-21 17:13:09 +08:00
|
|
|
SectionSymbol(uint32_t Flags, const InputSection *S, InputFile *F = nullptr)
|
|
|
|
: Symbol("", SectionKind, Flags, F), Section(S) {}
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
static bool classof(const Symbol *S) { return S->kind() == SectionKind; }
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
const OutputSectionSymbol *getOutputSectionSymbol() const;
|
2018-05-05 07:14:42 +08:00
|
|
|
|
|
|
|
const InputSection *Section;
|
|
|
|
};
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
class DataSymbol : public Symbol {
|
2018-02-15 02:27:59 +08:00
|
|
|
public:
|
|
|
|
static bool classof(const Symbol *S) {
|
2018-02-21 07:38:27 +08:00
|
|
|
return S->kind() == DefinedDataKind || S->kind() == UndefinedDataKind;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-02-21 07:38:27 +08:00
|
|
|
DataSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F)
|
2018-02-21 01:45:38 +08:00
|
|
|
: 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.
|
2018-02-23 13:08:53 +08:00
|
|
|
DefinedData(StringRef Name, uint32_t Flags, InputFile *F,
|
|
|
|
InputSegment *Segment, uint32_t Offset, uint32_t Size)
|
2018-02-21 07:38:27 +08:00
|
|
|
: DataSymbol(Name, DefinedDataKind, Flags, F), Segment(Segment),
|
2018-02-23 13:08:53 +08:00
|
|
|
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.
|
|
|
|
DefinedData(StringRef Name, uint32_t Flags)
|
|
|
|
: DataSymbol(Name, DefinedDataKind, Flags, nullptr) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
static bool classof(const Symbol *S) { return S->kind() == DefinedDataKind; }
|
|
|
|
|
|
|
|
// Returns the output virtual address of a defined data symbol.
|
2018-01-09 07:39:11 +08:00
|
|
|
uint32_t getVirtualAddress() const;
|
2017-12-06 03:05:45 +08:00
|
|
|
void setVirtualAddress(uint32_t VA);
|
|
|
|
|
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;
|
|
|
|
uint32_t getSize() const { return Size; }
|
|
|
|
|
|
|
|
InputSegment *Segment = nullptr;
|
2018-02-21 01:45:38 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
protected:
|
2018-02-23 13:08:53 +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:
|
2018-02-21 07:38:27 +08:00
|
|
|
UndefinedData(StringRef Name, uint32_t Flags, InputFile *File = nullptr)
|
|
|
|
: DataSymbol(Name, UndefinedDataKind, Flags, File) {}
|
2018-02-15 02:27:59 +08:00
|
|
|
static bool classof(const Symbol *S) {
|
2018-02-21 07:38:27 +08:00
|
|
|
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:
|
|
|
|
static bool classof(const Symbol *S) {
|
|
|
|
return S->kind() == DefinedGlobalKind || S->kind() == UndefinedGlobalKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WasmGlobalType *getGlobalType() const { return GlobalType; }
|
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
// Get/set the global index
|
|
|
|
uint32_t getGlobalIndex() const;
|
|
|
|
void setGlobalIndex(uint32_t Index);
|
|
|
|
bool hasGlobalIndex() const;
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
protected:
|
|
|
|
GlobalSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F,
|
|
|
|
const WasmGlobalType *GlobalType)
|
|
|
|
: Symbol(Name, K, Flags, F), GlobalType(GlobalType) {}
|
|
|
|
|
|
|
|
const WasmGlobalType *GlobalType;
|
2018-03-13 03:56:23 +08:00
|
|
|
uint32_t GlobalIndex = INVALID_INDEX;
|
2018-02-23 13:08:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DefinedGlobal : public GlobalSymbol {
|
|
|
|
public:
|
|
|
|
DefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File,
|
|
|
|
InputGlobal *Global);
|
|
|
|
|
|
|
|
static bool classof(const Symbol *S) {
|
|
|
|
return S->kind() == DefinedGlobalKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputGlobal *Global;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UndefinedGlobal : public GlobalSymbol {
|
|
|
|
public:
|
2019-02-08 06:00:48 +08:00
|
|
|
UndefinedGlobal(StringRef Name, StringRef ImportName, StringRef ImportModule,
|
|
|
|
uint32_t Flags, InputFile *File = nullptr,
|
2018-02-23 13:08:53 +08:00
|
|
|
const WasmGlobalType *Type = nullptr)
|
2019-02-08 06:00:48 +08:00
|
|
|
: GlobalSymbol(Name, UndefinedGlobalKind, Flags, File, Type),
|
|
|
|
ImportName(ImportName), ImportModule(ImportModule) {}
|
2018-02-23 13:08:53 +08:00
|
|
|
|
|
|
|
static bool classof(const Symbol *S) {
|
|
|
|
return S->kind() == UndefinedGlobalKind;
|
|
|
|
}
|
2019-02-08 06:00:48 +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:
|
|
|
|
static bool classof(const Symbol *S) { return S->kind() == DefinedEventKind; }
|
|
|
|
|
|
|
|
const WasmEventType *getEventType() const { return EventType; }
|
|
|
|
|
|
|
|
// Get/set the event index
|
|
|
|
uint32_t getEventIndex() const;
|
|
|
|
void setEventIndex(uint32_t Index);
|
|
|
|
bool hasEventIndex() const;
|
|
|
|
|
|
|
|
const WasmSignature *Signature;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
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) {}
|
|
|
|
|
|
|
|
const WasmEventType *EventType;
|
|
|
|
uint32_t EventIndex = INVALID_INDEX;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DefinedEvent : public EventSymbol {
|
|
|
|
public:
|
|
|
|
DefinedEvent(StringRef Name, uint32_t Flags, InputFile *File,
|
|
|
|
InputEvent *Event);
|
|
|
|
|
|
|
|
static bool classof(const Symbol *S) { return S->kind() == DefinedEventKind; }
|
|
|
|
|
|
|
|
InputEvent *Event;
|
|
|
|
};
|
|
|
|
|
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-01-30 06:26:31 +08:00
|
|
|
LazySymbol(StringRef Name, uint32_t Flags, InputFile *File,
|
2018-11-27 09:08:16 +08:00
|
|
|
const llvm::object::Archive::Symbol &Sym)
|
2019-01-30 06:26:31 +08:00
|
|
|
: Symbol(Name, LazyKind, Flags, File), ArchiveSymbol(Sym) {}
|
2018-02-15 02:27:59 +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.
|
|
|
|
const WasmSignature *Signature = nullptr;
|
|
|
|
|
2018-03-01 06:51:51 +08:00
|
|
|
private:
|
2018-11-27 09:08:16 +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.
|
|
|
|
static DefinedData *GlobalBase;
|
|
|
|
|
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.
|
2018-11-16 02:15:54 +08:00
|
|
|
static GlobalSymbol *StackPointer;
|
2018-02-03 06:59:56 +08:00
|
|
|
|
2018-02-07 11:04:53 +08:00
|
|
|
// __data_end
|
|
|
|
// Symbol marking the end of the data and bss.
|
2018-02-21 07:38:27 +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.
|
2018-02-21 07:38:27 +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.
|
2018-02-15 02:27:59 +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.
|
|
|
|
static DefinedFunction *InitMemory;
|
|
|
|
|
2019-04-05 02:40:51 +08:00
|
|
|
// __wasm_apply_relocs
|
|
|
|
// Function that applies relocations to data segment post-instantiation.
|
|
|
|
static DefinedFunction *ApplyRelocs;
|
|
|
|
|
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
|
|
|
|
static DefinedData *DsoHandle;
|
2018-11-15 08:37:21 +08:00
|
|
|
|
|
|
|
// __table_base
|
|
|
|
// Used in PIC code for offset of indirect function table
|
2018-11-16 02:15:54 +08:00
|
|
|
static UndefinedGlobal *TableBase;
|
2018-11-15 08:37:21 +08:00
|
|
|
|
|
|
|
// __memory_base
|
|
|
|
// Used in PIC code for offset of global data
|
2018-11-16 02:15:54 +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 {
|
|
|
|
alignas(DefinedFunction) char A[sizeof(DefinedFunction)];
|
2018-02-21 07:38:27 +08:00
|
|
|
alignas(DefinedData) char B[sizeof(DefinedData)];
|
2018-02-23 13:08:53 +08:00
|
|
|
alignas(DefinedGlobal) char C[sizeof(DefinedGlobal)];
|
2018-12-08 14:17:43 +08:00
|
|
|
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)];
|
2018-05-05 07:14:42 +08:00
|
|
|
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-02-06 10:35:18 +08:00
|
|
|
void printTraceSymbol(Symbol *Sym);
|
2019-05-24 21:29:17 +08:00
|
|
|
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>
|
|
|
|
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
|
|
|
|
|
|
|
Symbol SymCopy = *S;
|
|
|
|
|
|
|
|
T *S2 = new (S) T(std::forward<ArgT>(Arg)...);
|
|
|
|
S2->IsUsedInRegularObj = SymCopy.IsUsedInRegularObj;
|
2018-06-29 01:21:46 +08:00
|
|
|
S2->ForceExport = SymCopy.ForceExport;
|
2019-05-24 22:14:25 +08:00
|
|
|
S2->CanInline = SymCopy.CanInline;
|
2019-02-06 10:35:18 +08:00
|
|
|
S2->Traced = SymCopy.Traced;
|
|
|
|
|
|
|
|
// Print out a log message if --trace-symbol was specified.
|
|
|
|
// This is for debugging.
|
|
|
|
if (S2->Traced)
|
|
|
|
printTraceSymbol(S2);
|
|
|
|
|
2018-05-31 02:07:52 +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.
|
2018-03-10 06:59:34 +08:00
|
|
|
std::string toString(const wasm::Symbol &Sym);
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string toString(wasm::Symbol::Kind Kind);
|
2018-11-10 00:57:41 +08:00
|
|
|
std::string maybeDemangleSymbol(StringRef Name);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|