diff --git a/lld/test/wasm/build-id.test b/lld/test/wasm/build-id.test --- a/lld/test/wasm/build-id.test +++ b/lld/test/wasm/build-id.test @@ -43,12 +43,12 @@ # DEFAULT: Contents of section build_id: -# DEFAULT-NEXT: 0000 100e228e 4e2fa853 6393b43d ed1d4676 -# DEFAULT-NEXT: 0010 13 . +# DEFAULT-NEXT: 0000 10299168 1e3c845a 3c8f80ae 2f16cc22 .).h.<.Z<.../.." +# DEFAULT-NEXT: 0010 2d # SHA1: Contents of section build_id: -# SHA1-NEXT: 0000 14ad22e8 54d72438 94af85de 3c5592bd ..".T.$8....&1 \ # RUN: | FileCheck -check-prefix=FAIL %s diff --git a/lld/test/wasm/weak-alias.ll b/lld/test/wasm/weak-alias.ll --- a/lld/test/wasm/weak-alias.ll +++ b/lld/test/wasm/weak-alias.ll @@ -107,6 +107,7 @@ ; CHECK-NEXT: Count: 2 ; CHECK-NEXT: Body: 23808080800041106B220024808080800020004181808080003602081081808080002101200041106A24808080800020010B ; CHECK-NEXT: - Type: CUSTOM +; CHECK-NEXT: HeaderSecSizeEncodingLen: 2 ; CHECK-NEXT: Name: name ; CHECK-NEXT: FunctionNames: ; CHECK-NEXT: - Index: 0 diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h --- a/lld/wasm/Config.h +++ b/lld/wasm/Config.h @@ -82,6 +82,7 @@ llvm::StringRef entry; llvm::StringRef mapFile; llvm::StringRef outputFile; + llvm::StringRef soName; llvm::StringRef thinLTOCacheDir; llvm::StringRef whyExtract; diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -458,6 +458,7 @@ } config->sharedMemory = args.hasArg(OPT_shared_memory); + config->soName = args.getLastArgValue(OPT_soname); config->importTable = args.hasArg(OPT_import_table); config->importUndefined = args.hasArg(OPT_import_undefined); config->ltoo = args::getInteger(args, OPT_lto_O, 2); diff --git a/lld/wasm/Options.td b/lld/wasm/Options.td --- a/lld/wasm/Options.td +++ b/lld/wasm/Options.td @@ -207,6 +207,8 @@ def shared_memory: FF<"shared-memory">, HelpText<"Use shared linear memory">; +defm soname: Eq<"soname", "Set the module name in the generated name section">; + def import_table: FF<"import-table">, HelpText<"Import function table from the environment">; diff --git a/lld/wasm/SyntheticSections.h b/lld/wasm/SyntheticSections.h --- a/lld/wasm/SyntheticSections.h +++ b/lld/wasm/SyntheticSections.h @@ -375,7 +375,11 @@ segments(segments) {} bool isNeeded() const override { return !config->stripAll && numNames() > 0; } void writeBody() override; - unsigned numNames() const { return numNamedGlobals() + numNamedFunctions(); } + unsigned numNames() const { + // We always write at least one name which is the name of the + // module itself. + return 1 + numNamedGlobals() + numNamedFunctions(); + } unsigned numNamedGlobals() const; unsigned numNamedFunctions() const; unsigned numNamedDataSegments() const; diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -779,6 +779,15 @@ // Create the custom "name" section containing debug symbol names. void NameSection::writeBody() { + { + SubSection sub(WASM_NAMES_MODULE); + StringRef moduleName = config->soName; + if (config->soName.empty()) + moduleName = llvm::sys::path::filename(config->outputFile); + writeStr(sub.os, moduleName, "module name"); + sub.writeTo(bodyOutputStream); + } + unsigned count = numNamedFunctions(); if (count) { SubSection sub(WASM_NAMES_FUNCTION); diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -348,6 +348,7 @@ // Kind codes used in the custom "name" section enum : unsigned { + WASM_NAMES_MODULE = 0, WASM_NAMES_FUNCTION = 1, WASM_NAMES_LOCAL = 2, WASM_NAMES_GLOBAL = 7,