[llvm] 8e44f03 - [lld][WebAssembly] Add support for -soname

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 15 18:34:48 PDT 2023


Author: Sam Clegg
Date: 2023-08-15T18:33:45-07:00
New Revision: 8e44f037dc3ff86fb722037cb7832bb172b838eb

URL: https://github.com/llvm/llvm-project/commit/8e44f037dc3ff86fb722037cb7832bb172b838eb
DIFF: https://github.com/llvm/llvm-project/commit/8e44f037dc3ff86fb722037cb7832bb172b838eb.diff

LOG: [lld][WebAssembly] Add support for -soname

This change writes the module name to the name section of the wasm
binary.  We use the `-soname` argument to determine the name and we
default the output file basename if this option is not specified.

In the future we will likely want to embed the soname in the dylink
section too, but this the first step in supporting `-soname`.

Differential Revision: https://reviews.llvm.org/D158001

Added: 
    

Modified: 
    lld/test/wasm/build-id.test
    lld/test/wasm/map-file.s
    lld/test/wasm/weak-alias.ll
    lld/wasm/Config.h
    lld/wasm/Driver.cpp
    lld/wasm/Options.td
    lld/wasm/SyntheticSections.cpp
    lld/wasm/SyntheticSections.h
    llvm/include/llvm/BinaryFormat/Wasm.h

Removed: 
    


################################################################################
diff  --git a/lld/test/wasm/build-id.test b/lld/test/wasm/build-id.test
index c5c3cfd737f01b..b767d8de4a423b 100644
--- a/lld/test/wasm/build-id.test
+++ b/lld/test/wasm/build-id.test
@@ -43,12 +43,12 @@ foo:
 
 
 # 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....<U..
-# SHA1-NEXT: 0010 1b5ec96f 6b                          .^.ok
+# SHA1-NEXT: 0000 145abdda 387a9bc4 e3aed3c3 3319cd37  .Z..8z......3..7
+# SHA1-NEXT: 0010 0212237c e4                          ..#|.
 
 # UUID:      Contents of section build_id:
 # UUID-NEXT: 0000 10

diff  --git a/lld/test/wasm/map-file.s b/lld/test/wasm/map-file.s
index 1aae7a7aa2bd0f..2757f50187ffe5 100644
--- a/lld/test/wasm/map-file.s
+++ b/lld/test/wasm/map-file.s
@@ -67,7 +67,7 @@ somezeroes:
 # CHECK-NEXT:     408        0        4         {{.*}}{{/|\\}}map-file.s.tmp1.o:(.bss.somezeroes)
 # CHECK-NEXT:     408        0        4                 somezeroes
 # CHECK-NEXT:       -       93       12 CUSTOM(.debug_info)
-# CHECK-NEXT:       -       a5       50 CUSTOM(name)
+# CHECK-NEXT:       -       a5       61 CUSTOM(name)
 
 # RUN: not wasm-ld %t1.o -o /dev/null -Map=/ 2>&1 \
 # RUN:  | FileCheck -check-prefix=FAIL %s

diff  --git a/lld/test/wasm/weak-alias.ll b/lld/test/wasm/weak-alias.ll
index cba39acda8e9c0..1768b8fd5b3851 100644
--- a/lld/test/wasm/weak-alias.ll
+++ b/lld/test/wasm/weak-alias.ll
@@ -107,6 +107,7 @@ entry:
 ; 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
index db636947d269da..2ca92674d6dc53 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -82,6 +82,7 @@ struct Configuration {
   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
index 00920fd5b4236a..84304881f5ca34 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -458,6 +458,7 @@ static void readConfigs(opt::InputArgList &args) {
   }
 
   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
index 9670e5fb008470..50417d2928e0a3 100644
--- a/lld/wasm/Options.td
+++ b/lld/wasm/Options.td
@@ -207,6 +207,8 @@ def export_memory_with_name: JJ<"export-memory=">,
 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.cpp b/lld/wasm/SyntheticSections.cpp
index 8aca8776ac1374..ef9547593acdcd 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -779,6 +779,15 @@ unsigned NameSection::numNamedDataSegments() const {
 
 // 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/lld/wasm/SyntheticSections.h b/lld/wasm/SyntheticSections.h
index dd63931b974b0a..915b35dd6fed80 100644
--- a/lld/wasm/SyntheticSections.h
+++ b/lld/wasm/SyntheticSections.h
@@ -375,7 +375,11 @@ class NameSection : public SyntheticSection {
         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/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h
index 48195af7def30e..749d43b840d5c2 100644
--- a/llvm/include/llvm/BinaryFormat/Wasm.h
+++ b/llvm/include/llvm/BinaryFormat/Wasm.h
@@ -348,6 +348,7 @@ enum : uint8_t {
 
 // 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,


        


More information about the llvm-commits mailing list