2017-11-17 18:14:09 +00:00
|
|
|
//===- Symbols.h ------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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-17 18:14:09 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_WASM_SYMBOLS_H
|
|
|
|
#define LLD_WASM_SYMBOLS_H
|
|
|
|
|
2018-04-20 17:18:06 +00:00
|
|
|
#include "Config.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "lld/Common/LLVM.h"
|
2020-02-05 21:18:55 -08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
2019-05-21 09:13:09 +00: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 01:08:16 +00:00
|
|
|
using llvm::wasm::WasmSymbolType;
|
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
class InputFile;
|
2018-01-28 19:57:01 +00:00
|
|
|
class InputChunk;
|
2018-02-20 17:45:38 +00:00
|
|
|
class InputSegment;
|
2018-02-16 23:50:23 +00:00
|
|
|
class InputFunction;
|
2018-02-23 05:08:53 +00:00
|
|
|
class InputGlobal;
|
2018-12-08 06:17:43 +00:00
|
|
|
class InputEvent;
|
2018-05-04 23:14:42 +00:00
|
|
|
class InputSection;
|
2019-05-21 09:13:09 +00:00
|
|
|
class OutputSection;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2018-02-14 22:55:38 +00:00
|
|
|
#define INVALID_INDEX UINT32_MAX
|
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
// The base class for real symbol classes.
|
2017-11-17 18:14:09 +00:00
|
|
|
class Symbol {
|
|
|
|
public:
|
2019-07-05 01:27:39 +00:00
|
|
|
enum Kind : uint8_t {
|
2017-11-17 18:14:09 +00:00
|
|
|
DefinedFunctionKind,
|
2018-02-20 23:38:27 +00:00
|
|
|
DefinedDataKind,
|
2018-02-23 05:08:53 +00:00
|
|
|
DefinedGlobalKind,
|
2018-12-08 06:17:43 +00:00
|
|
|
DefinedEventKind,
|
2018-05-04 23:14:42 +00:00
|
|
|
SectionKind,
|
2019-05-21 09:13:09 +00:00
|
|
|
OutputSectionKind,
|
2017-11-17 18:14:09 +00:00
|
|
|
UndefinedFunctionKind,
|
2018-02-20 23:38:27 +00:00
|
|
|
UndefinedDataKind,
|
2018-02-23 05:08:53 +00:00
|
|
|
UndefinedGlobalKind,
|
2018-02-28 00:16:11 +00:00
|
|
|
LazyKind,
|
2017-11-17 18:14:09 +00:00
|
|
|
};
|
|
|
|
|
2018-02-16 20:26:15 +00:00
|
|
|
Kind kind() const { return symbolKind; }
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2019-05-21 09:13:09 +00:00
|
|
|
bool isDefined() const { return !isLazy() && !isUndefined(); }
|
2018-02-28 00:16:11 +00:00
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
bool isUndefined() const {
|
2018-02-23 05:08:53 +00:00
|
|
|
return symbolKind == UndefinedFunctionKind ||
|
|
|
|
symbolKind == UndefinedDataKind || symbolKind == UndefinedGlobalKind;
|
2017-11-17 18:14:09 +00:00
|
|
|
}
|
2018-02-28 00:16:11 +00:00
|
|
|
|
|
|
|
bool isLazy() const { return symbolKind == LazyKind; }
|
|
|
|
|
2018-01-10 00:52:20 +00:00
|
|
|
bool isLocal() const;
|
2017-11-17 18:14:09 +00:00
|
|
|
bool isWeak() const;
|
2017-12-03 02:38:04 +00:00
|
|
|
bool isHidden() const;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2019-06-07 06:00:46 +00:00
|
|
|
// Returns true if this symbol exists in a discarded (due to COMDAT) section
|
|
|
|
bool isDiscarded() const;
|
|
|
|
|
2019-01-29 22:26:31 +00: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-17 18:14:09 +00: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 05:08:53 +00:00
|
|
|
|
2018-02-20 17:45:38 +00:00
|
|
|
InputChunk *getChunk() const;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2018-04-20 17:18:06 +00:00
|
|
|
// Indicates that the section or import for this symbol will be included in
|
|
|
|
// the final image.
|
2018-02-23 05:08:53 +00:00
|
|
|
bool isLive() const;
|
|
|
|
|
2018-04-20 17:18:06 +00:00
|
|
|
// Marks the symbol's InputChunk as Live, so that it will be included in the
|
|
|
|
// final image.
|
|
|
|
void markLive();
|
|
|
|
|
2018-01-12 22:10:35 +00:00
|
|
|
void setHidden(bool isHidden);
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2018-03-12 19:56:23 +00:00
|
|
|
// Get/set the index in the output symbol table. This is only used for
|
|
|
|
// relocatable output.
|
2018-02-23 05:08:53 +00:00
|
|
|
uint32_t getOutputSymbolIndex() const;
|
|
|
|
void setOutputSymbolIndex(uint32_t index);
|
|
|
|
|
|
|
|
WasmSymbolType getWasmType() const;
|
2018-06-28 17:04:58 +00:00
|
|
|
bool isExported() const;
|
2018-02-23 05:08:53 +00:00
|
|
|
|
2019-08-29 22:41:05 +00:00
|
|
|
// Indicates that the symbol is used in an __attribute__((used)) directive
|
|
|
|
// or similar.
|
|
|
|
bool isNoStrip() const;
|
|
|
|
|
2019-02-20 23:19:31 +00:00
|
|
|
const WasmSignature* getSignature() const;
|
|
|
|
|
2019-03-26 19:46:15 +00:00
|
|
|
uint32_t getGOTIndex() const {
|
|
|
|
assert(gotIndex != INVALID_INDEX);
|
|
|
|
return gotIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setGOTIndex(uint32_t index);
|
|
|
|
bool hasGOTIndex() const { return gotIndex != INVALID_INDEX; }
|
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
protected:
|
2018-02-20 17:45:38 +00:00
|
|
|
Symbol(StringRef name, Kind k, uint32_t flags, InputFile *f)
|
2020-02-27 17:32:22 -08:00
|
|
|
: name(name), file(f), symbolKind(k), referenced(!config->gcSections),
|
|
|
|
requiresGOT(false), isUsedInRegularObj(false), forceExport(false),
|
|
|
|
canInline(false), traced(false), flags(flags) {}
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
StringRef name;
|
|
|
|
InputFile *file;
|
2018-02-23 05:08:53 +00:00
|
|
|
uint32_t outputSymbolIndex = INVALID_INDEX;
|
2019-03-26 19:46:15 +00:00
|
|
|
uint32_t gotIndex = INVALID_INDEX;
|
2019-07-05 01:27:39 +00:00
|
|
|
Kind symbolKind;
|
|
|
|
|
|
|
|
public:
|
2019-07-08 10:35:08 +00:00
|
|
|
bool referenced : 1;
|
|
|
|
|
2019-09-24 20:52:12 +00:00
|
|
|
// True for data symbols that needs a dummy GOT entry. Used for static
|
|
|
|
// linking of GOT accesses.
|
|
|
|
bool requiresGOT : 1;
|
|
|
|
|
2019-07-05 01:27:39 +00: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 07:30:07 +00:00
|
|
|
bool isUsedInRegularObj : 1;
|
2019-07-05 01:27:39 +00:00
|
|
|
|
2020-01-06 10:21:05 -08:00
|
|
|
// True if ths symbol is explicitly marked for export (i.e. via the
|
|
|
|
// -e/--export command line flag)
|
2019-07-08 07:30:07 +00:00
|
|
|
bool forceExport : 1;
|
2019-07-05 01:27:39 +00: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 07:30:07 +00:00
|
|
|
bool canInline : 1;
|
2019-07-05 01:27:39 +00:00
|
|
|
|
|
|
|
// True if this symbol is specified by --trace-symbol option.
|
2019-07-08 07:30:07 +00:00
|
|
|
bool traced : 1;
|
2020-02-27 17:32:22 -08:00
|
|
|
|
|
|
|
uint32_t flags;
|
2018-02-14 18:27:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FunctionSymbol : public Symbol {
|
|
|
|
public:
|
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == DefinedFunctionKind ||
|
|
|
|
s->kind() == UndefinedFunctionKind;
|
|
|
|
}
|
|
|
|
|
2018-03-12 19:56:23 +00:00
|
|
|
// Get/set the table index
|
|
|
|
void setTableIndex(uint32_t index);
|
2018-01-24 21:45:25 +00:00
|
|
|
uint32_t getTableIndex() const;
|
|
|
|
bool hasTableIndex() const;
|
2017-12-11 22:00:56 +00:00
|
|
|
|
2018-03-12 19:56:23 +00:00
|
|
|
// Get/set the function index
|
|
|
|
uint32_t getFunctionIndex() const;
|
|
|
|
void setFunctionIndex(uint32_t index);
|
|
|
|
bool hasFunctionIndex() const;
|
2017-12-11 22:00:56 +00:00
|
|
|
|
2018-12-08 06:17:43 +00:00
|
|
|
const WasmSignature *signature;
|
2018-06-28 16:53:53 +00:00
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
protected:
|
2018-02-16 23:50:23 +00:00
|
|
|
FunctionSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
|
2018-12-08 06:17:43 +00:00
|
|
|
const WasmSignature *sig)
|
|
|
|
: Symbol(name, k, flags, f), signature(sig) {}
|
2018-02-14 18:27:59 +00:00
|
|
|
|
2018-02-14 22:55:38 +00:00
|
|
|
uint32_t tableIndex = INVALID_INDEX;
|
2018-03-12 19:56:23 +00:00
|
|
|
uint32_t functionIndex = INVALID_INDEX;
|
2018-02-14 18:27:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DefinedFunction : public FunctionSymbol {
|
|
|
|
public:
|
2018-02-16 23:50:23 +00:00
|
|
|
DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
|
2018-02-20 17:45:38 +00:00
|
|
|
InputFunction *function);
|
2018-02-14 18:27:59 +00:00
|
|
|
|
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == DefinedFunctionKind;
|
|
|
|
}
|
2018-02-20 17:45:38 +00:00
|
|
|
|
|
|
|
InputFunction *function;
|
2018-02-14 18:27:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class UndefinedFunction : public FunctionSymbol {
|
|
|
|
public:
|
2020-02-05 21:18:55 -08:00
|
|
|
UndefinedFunction(StringRef name, llvm::Optional<StringRef> importName,
|
|
|
|
llvm::Optional<StringRef> importModule, uint32_t flags,
|
2019-02-01 02:29:57 +00:00
|
|
|
InputFile *file = nullptr,
|
2019-05-24 22:45:08 +00:00
|
|
|
const WasmSignature *type = nullptr,
|
|
|
|
bool isCalledDirectly = true)
|
2019-02-01 02:29:57 +00:00
|
|
|
: FunctionSymbol(name, UndefinedFunctionKind, flags, file, type),
|
2020-02-05 21:18:55 -08:00
|
|
|
importName(importName), importModule(importModule),
|
|
|
|
isCalledDirectly(isCalledDirectly) {}
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == UndefinedFunctionKind;
|
|
|
|
}
|
2019-02-01 02:29:57 +00:00
|
|
|
|
2020-02-05 21:18:55 -08:00
|
|
|
llvm::Optional<StringRef> importName;
|
|
|
|
llvm::Optional<StringRef> importModule;
|
2019-05-24 22:45:08 +00:00
|
|
|
bool isCalledDirectly;
|
2018-02-14 18:27:59 +00:00
|
|
|
};
|
|
|
|
|
2019-05-21 09:13:09 +00: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-04 23:14:42 +00:00
|
|
|
class SectionSymbol : public Symbol {
|
|
|
|
public:
|
2019-05-21 09:13:09 +00:00
|
|
|
SectionSymbol(uint32_t flags, const InputSection *s, InputFile *f = nullptr)
|
|
|
|
: Symbol("", SectionKind, flags, f), section(s) {}
|
|
|
|
|
2018-05-04 23:14:42 +00:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == SectionKind; }
|
|
|
|
|
2019-05-21 09:13:09 +00:00
|
|
|
const OutputSectionSymbol *getOutputSectionSymbol() const;
|
2018-05-04 23:14:42 +00:00
|
|
|
|
|
|
|
const InputSection *section;
|
|
|
|
};
|
|
|
|
|
2018-02-20 23:38:27 +00:00
|
|
|
class DataSymbol : public Symbol {
|
2018-02-14 18:27:59 +00:00
|
|
|
public:
|
|
|
|
static bool classof(const Symbol *s) {
|
2018-02-20 23:38:27 +00:00
|
|
|
return s->kind() == DefinedDataKind || s->kind() == UndefinedDataKind;
|
2018-02-14 18:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-02-20 23:38:27 +00:00
|
|
|
DataSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f)
|
2018-02-20 17:45:38 +00:00
|
|
|
: Symbol(name, k, flags, f) {}
|
2018-02-14 18:27:59 +00:00
|
|
|
};
|
|
|
|
|
2018-02-20 23:38:27 +00:00
|
|
|
class DefinedData : public DataSymbol {
|
2018-02-14 18:27:59 +00:00
|
|
|
public:
|
2018-04-27 05:50:40 +00:00
|
|
|
// Constructor for regular data symbols originating from input files.
|
2018-02-23 05:08:53 +00:00
|
|
|
DefinedData(StringRef name, uint32_t flags, InputFile *f,
|
|
|
|
InputSegment *segment, uint32_t offset, uint32_t size)
|
2018-02-20 23:38:27 +00:00
|
|
|
: DataSymbol(name, DefinedDataKind, flags, f), segment(segment),
|
2018-02-23 05:08:53 +00:00
|
|
|
offset(offset), size(size) {}
|
2018-02-14 18:27:59 +00:00
|
|
|
|
2018-02-23 05:08:53 +00:00
|
|
|
// Constructor for linker synthetic data symbols.
|
|
|
|
DefinedData(StringRef name, uint32_t flags)
|
|
|
|
: DataSymbol(name, DefinedDataKind, flags, nullptr) {}
|
2018-02-14 18:27:59 +00:00
|
|
|
|
2018-02-23 05:08:53 +00:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == DefinedDataKind; }
|
|
|
|
|
|
|
|
// Returns the output virtual address of a defined data symbol.
|
2018-01-08 23:39:11 +00:00
|
|
|
uint32_t getVirtualAddress() const;
|
2017-12-05 19:05:45 +00:00
|
|
|
void setVirtualAddress(uint32_t va);
|
|
|
|
|
2018-02-23 05:08:53 +00: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-20 17:45:38 +00:00
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
protected:
|
2018-02-23 05:08:53 +00:00
|
|
|
uint32_t offset = 0;
|
|
|
|
uint32_t size = 0;
|
2018-02-14 18:27:59 +00:00
|
|
|
};
|
|
|
|
|
2018-02-20 23:38:27 +00:00
|
|
|
class UndefinedData : public DataSymbol {
|
2018-02-14 18:27:59 +00:00
|
|
|
public:
|
2018-02-20 23:38:27 +00:00
|
|
|
UndefinedData(StringRef name, uint32_t flags, InputFile *file = nullptr)
|
|
|
|
: DataSymbol(name, UndefinedDataKind, flags, file) {}
|
2018-02-14 18:27:59 +00:00
|
|
|
static bool classof(const Symbol *s) {
|
2018-02-20 23:38:27 +00:00
|
|
|
return s->kind() == UndefinedDataKind;
|
2018-02-14 18:27:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-23 05:08:53 +00: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-12 19:56:23 +00:00
|
|
|
// Get/set the global index
|
|
|
|
uint32_t getGlobalIndex() const;
|
|
|
|
void setGlobalIndex(uint32_t index);
|
|
|
|
bool hasGlobalIndex() const;
|
|
|
|
|
2018-02-23 05:08:53 +00: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-12 19:56:23 +00:00
|
|
|
uint32_t globalIndex = INVALID_INDEX;
|
2018-02-23 05:08:53 +00: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:
|
2020-02-05 21:18:55 -08:00
|
|
|
UndefinedGlobal(StringRef name, llvm::Optional<StringRef> importName,
|
|
|
|
llvm::Optional<StringRef> importModule, uint32_t flags,
|
|
|
|
InputFile *file = nullptr,
|
2018-02-23 05:08:53 +00:00
|
|
|
const WasmGlobalType *type = nullptr)
|
2019-02-07 22:00:48 +00:00
|
|
|
: GlobalSymbol(name, UndefinedGlobalKind, flags, file, type),
|
|
|
|
importName(importName), importModule(importModule) {}
|
2019-07-11 05:40:30 +00:00
|
|
|
|
2018-02-23 05:08:53 +00:00
|
|
|
static bool classof(const Symbol *s) {
|
|
|
|
return s->kind() == UndefinedGlobalKind;
|
|
|
|
}
|
2019-02-07 22:00:48 +00:00
|
|
|
|
2020-02-05 21:18:55 -08:00
|
|
|
llvm::Optional<StringRef> importName;
|
|
|
|
llvm::Optional<StringRef> importModule;
|
2018-02-23 05:08:53 +00:00
|
|
|
};
|
|
|
|
|
2018-12-08 06:17:43 +00: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-29 22:26:31 +00: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-14 18:27:59 +00:00
|
|
|
class LazySymbol : public Symbol {
|
|
|
|
public:
|
2019-01-29 22:26:31 +00:00
|
|
|
LazySymbol(StringRef name, uint32_t flags, InputFile *file,
|
2018-11-27 01:08:16 +00:00
|
|
|
const llvm::object::Archive::Symbol &sym)
|
2019-01-29 22:26:31 +00:00
|
|
|
: Symbol(name, LazyKind, flags, file), archiveSymbol(sym) {}
|
2018-02-14 18:27:59 +00:00
|
|
|
|
|
|
|
static bool classof(const Symbol *s) { return s->kind() == LazyKind; }
|
2018-02-28 22:51:51 +00:00
|
|
|
void fetch();
|
2019-12-19 17:23:59 -08:00
|
|
|
MemoryBufferRef getMemberBuffer();
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2019-01-29 22:26:31 +00:00
|
|
|
// Lazy symbols can have a signature because they can replace an
|
|
|
|
// UndefinedFunction which which case we need to be able to preserve the
|
2020-04-02 01:21:08 +09:00
|
|
|
// signature.
|
2019-01-29 22:26:31 +00:00
|
|
|
// 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-02-28 22:51:51 +00:00
|
|
|
private:
|
2018-11-27 01:08:16 +00:00
|
|
|
llvm::object::Archive::Symbol archiveSymbol;
|
2017-11-17 18:14:09 +00:00
|
|
|
};
|
|
|
|
|
2018-02-02 22:59:56 +00:00
|
|
|
// linker-generated symbols
|
|
|
|
struct WasmSym {
|
2019-06-26 20:12:33 +00:00
|
|
|
// __global_base
|
|
|
|
// Symbol marking the start of the global section.
|
|
|
|
static DefinedData *globalBase;
|
|
|
|
|
2018-02-02 22:59:56 +00:00
|
|
|
// __stack_pointer
|
2018-02-07 03:04:53 +00:00
|
|
|
// Global that holds the address of the top of the explicit value stack in
|
|
|
|
// linear memory.
|
2018-11-15 18:15:54 +00:00
|
|
|
static GlobalSymbol *stackPointer;
|
2018-02-02 22:59:56 +00: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-16 22:00:45 +00: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-19 23:34:16 +00:00
|
|
|
// __tls_size
|
|
|
|
// Symbol whose value is the alignment of the TLS block.
|
|
|
|
static GlobalSymbol *tlsAlign;
|
|
|
|
|
2018-02-07 03:04:53 +00:00
|
|
|
// __data_end
|
|
|
|
// Symbol marking the end of the data and bss.
|
2018-02-20 23:38:27 +00:00
|
|
|
static DefinedData *dataEnd;
|
2018-02-07 03:04:53 +00:00
|
|
|
|
2018-02-02 22:59:56 +00:00
|
|
|
// __heap_base
|
2018-02-07 03:04:53 +00: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-20 23:38:27 +00:00
|
|
|
static DefinedData *heapBase;
|
2018-02-02 22:59:56 +00:00
|
|
|
|
2019-09-04 19:50:39 +00:00
|
|
|
// __wasm_init_memory_flag
|
|
|
|
// Symbol whose contents are nonzero iff memory has already been initialized.
|
|
|
|
static DefinedData *initMemoryFlag;
|
2018-02-02 22:59:56 +00:00
|
|
|
|
2019-07-03 22:04:54 +00:00
|
|
|
// __wasm_init_memory
|
2019-09-04 19:50:39 +00:00
|
|
|
// Function that initializes passive data segments during instantiation.
|
2019-07-03 22:04:54 +00:00
|
|
|
static DefinedFunction *initMemory;
|
|
|
|
|
2019-09-04 19:50:39 +00:00
|
|
|
// __wasm_call_ctors
|
|
|
|
// Function that directly calls all ctors in priority order.
|
|
|
|
static DefinedFunction *callCtors;
|
|
|
|
|
2019-04-04 18:40:51 +00:00
|
|
|
// __wasm_apply_relocs
|
|
|
|
// Function that applies relocations to data segment post-instantiation.
|
|
|
|
static DefinedFunction *applyRelocs;
|
|
|
|
|
[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-16 22:00:45 +00:00
|
|
|
// __wasm_init_tls
|
|
|
|
// Function that allocates thread-local storage and initializes it.
|
|
|
|
static DefinedFunction *initTLS;
|
|
|
|
|
2018-02-02 22:59:56 +00:00
|
|
|
// __dso_handle
|
2018-02-20 23:38:27 +00:00
|
|
|
// Symbol used in calls to __cxa_atexit to determine current DLL
|
|
|
|
static DefinedData *dsoHandle;
|
2018-11-15 00:37:21 +00:00
|
|
|
|
|
|
|
// __table_base
|
|
|
|
// Used in PIC code for offset of indirect function table
|
2018-11-15 18:15:54 +00:00
|
|
|
static UndefinedGlobal *tableBase;
|
2019-08-13 17:02:02 +00:00
|
|
|
static DefinedData *definedTableBase;
|
2018-11-15 00:37:21 +00:00
|
|
|
|
|
|
|
// __memory_base
|
|
|
|
// Used in PIC code for offset of global data
|
2018-11-15 18:15:54 +00:00
|
|
|
static UndefinedGlobal *memoryBase;
|
2019-08-13 17:02:02 +00:00
|
|
|
static DefinedData *definedMemoryBase;
|
2018-02-14 18:27:59 +00: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-20 23:38:27 +00:00
|
|
|
alignas(DefinedData) char b[sizeof(DefinedData)];
|
2018-02-23 05:08:53 +00:00
|
|
|
alignas(DefinedGlobal) char c[sizeof(DefinedGlobal)];
|
2018-12-08 06:17:43 +00: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-04 23:14:42 +00:00
|
|
|
alignas(SectionSymbol) char i[sizeof(SectionSymbol)];
|
2018-02-02 22:59:56 +00:00
|
|
|
};
|
|
|
|
|
2019-07-08 07:30:07 +00: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.
|
2020-02-05 21:18:55 -08:00
|
|
|
static_assert(sizeof(SymbolUnion) <= 112, "SymbolUnion too large");
|
2019-07-08 07:30:07 +00:00
|
|
|
|
2019-02-06 02:35:18 +00:00
|
|
|
void printTraceSymbol(Symbol *sym);
|
2019-05-24 13:29:17 +00:00
|
|
|
void printTraceSymbolUndefined(StringRef name, const InputFile* file);
|
2019-02-06 02:35:18 +00:00
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
template <typename T, typename... ArgT>
|
|
|
|
T *replaceSymbol(Symbol *s, ArgT &&... arg) {
|
2018-02-14 22:55:38 +00:00
|
|
|
static_assert(std::is_trivially_destructible<T>(),
|
|
|
|
"Symbol types must be trivially destructible");
|
2018-11-19 23:31:28 +00:00
|
|
|
static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
|
2018-02-14 18:27:59 +00:00
|
|
|
static_assert(alignof(T) <= alignof(SymbolUnion),
|
|
|
|
"SymbolUnion not aligned enough");
|
2018-02-14 22:43:43 +00:00
|
|
|
assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
|
|
|
|
"Not a Symbol");
|
2018-05-30 18:07:52 +00:00
|
|
|
|
|
|
|
Symbol symCopy = *s;
|
|
|
|
|
|
|
|
T *s2 = new (s) T(std::forward<ArgT>(arg)...);
|
|
|
|
s2->isUsedInRegularObj = symCopy.isUsedInRegularObj;
|
2018-06-28 17:21:46 +00:00
|
|
|
s2->forceExport = symCopy.forceExport;
|
2019-05-24 14:14:25 +00:00
|
|
|
s2->canInline = symCopy.canInline;
|
2019-02-06 02:35:18 +00: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-30 18:07:52 +00:00
|
|
|
return s2;
|
2018-02-14 18:27:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 18:14:09 +00:00
|
|
|
} // namespace wasm
|
|
|
|
|
|
|
|
// Returns a symbol name for an error message.
|
2018-03-09 22:59:34 +00:00
|
|
|
std::string toString(const wasm::Symbol &sym);
|
2017-12-05 16:50:46 +00:00
|
|
|
std::string toString(wasm::Symbol::Kind kind);
|
2018-11-09 16:57:41 +00:00
|
|
|
std::string maybeDemangleSymbol(StringRef name);
|
2017-11-17 18:14:09 +00:00
|
|
|
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|