Index: wasm/Driver.cpp =================================================================== --- wasm/Driver.cpp +++ wasm/Driver.cpp @@ -292,10 +292,11 @@ Config->AllowUndefined = Args.hasArg(OPT_allow_undefined); Config->EmitRelocs = Args.hasArg(OPT_emit_relocs); - Config->Entry = Args.getLastArgValue(OPT_entry); + Config->Relocatable = Args.hasArg(OPT_relocatable); + Config->Entry = Args.getLastArgValue(OPT_entry, + Config->Relocatable ? "" : "_start"); Config->ImportMemory = Args.hasArg(OPT_import_memory); Config->OutputFile = Args.getLastArgValue(OPT_o); - Config->Relocatable = Args.hasArg(OPT_relocatable); Config->SearchPaths = getArgs(Args, OPT_L); Config->StripAll = Args.hasArg(OPT_strip_all); Config->StripDebug = Args.hasArg(OPT_strip_debug); @@ -323,9 +324,8 @@ error("entry point specified for relocatable output file"); if (!Config->Relocatable) { - if (Config->Entry.empty()) - Config->Entry = "_start"; - addSyntheticUndefinedFunction(Config->Entry); + if (!Config->Entry.empty()) + addSyntheticUndefinedFunction(Config->Entry); addSyntheticGlobal("__stack_pointer", 0); } @@ -348,6 +348,8 @@ if (!Config->Entry.empty()) { Symbol *Sym = Symtab->find(Config->Entry); + if (!Sym) + fatal("entry point not found: " + Config->Entry); if (!Sym->isFunction()) fatal("entry point is not a function: " + Sym->getName()); } Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -305,22 +305,6 @@ writeExport(OS, MemoryExport); } - if (ExportMain) { - Symbol *Sym = Symtab->find(Config->Entry); - if (Sym->isDefined()) { - if (!Sym->isFunction()) - fatal("entry point is not a function: " + Sym->getName()); - - if (!ExportOther) { - WasmExport MainExport; - MainExport.Name = Config->Entry; - MainExport.Kind = WASM_EXTERNAL_FUNCTION; - MainExport.Index = Sym->getOutputIndex(); - writeExport(OS, MainExport); - } - } - } - if (ExportOther) { for (ObjFile *File : Symtab->ObjectFiles) { for (Symbol *Sym : File->getSymbols()) { @@ -345,7 +329,22 @@ } } -void Writer::createStartSection() {} +void Writer::createStartSection() { + if (Config->Entry.empty()) + return; + + const Symbol *Sym = Symtab->find(Config->Entry); + if (!Sym) + fatal("entry point not found: " + Config->Entry); + if (!Sym->isFunction()) + fatal("entry point is not a function: " + Sym->getName()); + + SyntheticSection *Section = createSyntheticSection(WASM_SEC_START); + raw_ostream &OS = Section->getStream(); + + log("Start: " + Sym->getName()); + writeUleb128(OS, Sym->getOutputIndex(), "start index"); +} void Writer::createElemSection() { if (!NumElements)