Changeset View
Changeset View
Standalone View
Standalone View
wasm/Writer.cpp
Show All 35 Lines | |||||
using namespace llvm; | using namespace llvm; | ||||
using namespace llvm::wasm; | using namespace llvm::wasm; | ||||
using namespace lld; | using namespace lld; | ||||
using namespace lld::wasm; | using namespace lld::wasm; | ||||
static constexpr int kStackAlignment = 16; | static constexpr int kStackAlignment = 16; | ||||
static constexpr const char *kFunctionTableName = "__indirect_function_table"; | static constexpr const char *kFunctionTableName = "__indirect_function_table"; | ||||
const char *lld::wasm::kDefaultModule = "env"; | |||||
namespace { | namespace { | ||||
// An init entry to be written to either the synthetic init func or the | // An init entry to be written to either the synthetic init func or the | ||||
// linking metadata. | // linking metadata. | ||||
struct WasmInitEntry { | struct WasmInitEntry { | ||||
const FunctionSymbol *Sym; | const FunctionSymbol *Sym; | ||||
uint32_t Priority; | uint32_t Priority; | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | void Writer::createImportSection() { | ||||
SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT); | SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT); | ||||
raw_ostream &OS = Section->getStream(); | raw_ostream &OS = Section->getStream(); | ||||
writeUleb128(OS, NumImports, "import count"); | writeUleb128(OS, NumImports, "import count"); | ||||
if (Config->ImportMemory) { | if (Config->ImportMemory) { | ||||
WasmImport Import; | WasmImport Import; | ||||
Import.Module = "env"; | Import.Module = kDefaultModule; | ||||
Import.Field = "memory"; | Import.Field = "memory"; | ||||
Import.Kind = WASM_EXTERNAL_MEMORY; | Import.Kind = WASM_EXTERNAL_MEMORY; | ||||
Import.Memory.Flags = 0; | Import.Memory.Flags = 0; | ||||
Import.Memory.Initial = NumMemoryPages; | Import.Memory.Initial = NumMemoryPages; | ||||
if (MaxMemoryPages != 0) { | if (MaxMemoryPages != 0) { | ||||
Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX; | Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX; | ||||
Import.Memory.Maximum = MaxMemoryPages; | Import.Memory.Maximum = MaxMemoryPages; | ||||
} | } | ||||
if (Config->SharedMemory) | if (Config->SharedMemory) | ||||
Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED; | Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED; | ||||
writeImport(OS, Import); | writeImport(OS, Import); | ||||
} | } | ||||
if (Config->ImportTable) { | if (Config->ImportTable) { | ||||
uint32_t TableSize = TableBase + IndirectFunctions.size(); | uint32_t TableSize = TableBase + IndirectFunctions.size(); | ||||
WasmImport Import; | WasmImport Import; | ||||
Import.Module = "env"; | Import.Module = kDefaultModule; | ||||
Import.Field = kFunctionTableName; | Import.Field = kFunctionTableName; | ||||
Import.Kind = WASM_EXTERNAL_TABLE; | Import.Kind = WASM_EXTERNAL_TABLE; | ||||
Import.Table.ElemType = WASM_TYPE_FUNCREF; | Import.Table.ElemType = WASM_TYPE_FUNCREF; | ||||
Import.Table.Limits = {0, TableSize, 0}; | Import.Table.Limits = {0, TableSize, 0}; | ||||
writeImport(OS, Import); | writeImport(OS, Import); | ||||
} | } | ||||
for (const Symbol *Sym : ImportedSymbols) { | for (const Symbol *Sym : ImportedSymbols) { | ||||
WasmImport Import; | WasmImport Import; | ||||
Import.Module = "env"; | if (auto *F = dyn_cast<UndefinedFunction>(Sym)) | ||||
Import.Module = F->Module; | |||||
else | |||||
Import.Module = kDefaultModule; | |||||
Import.Field = Sym->getName(); | Import.Field = Sym->getName(); | ||||
if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) { | if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) { | ||||
Import.Kind = WASM_EXTERNAL_FUNCTION; | Import.Kind = WASM_EXTERNAL_FUNCTION; | ||||
Import.SigIndex = lookupType(*FunctionSym->Signature); | Import.SigIndex = lookupType(*FunctionSym->Signature); | ||||
} else if (auto *GlobalSym = dyn_cast<GlobalSymbol>(Sym)) { | } else if (auto *GlobalSym = dyn_cast<GlobalSymbol>(Sym)) { | ||||
Import.Kind = WASM_EXTERNAL_GLOBAL; | Import.Kind = WASM_EXTERNAL_GLOBAL; | ||||
Import.Global = *GlobalSym->getGlobalType(); | Import.Global = *GlobalSym->getGlobalType(); | ||||
} else { | } else { | ||||
▲ Show 20 Lines • Show All 1,054 Lines • Show Last 20 Lines |