[WebAssembly] Add version to object file metadata
authorSam Clegg <sbc@chromium.org>
Thu, 26 Apr 2018 18:15:32 +0000 (18:15 +0000)
committerSam Clegg <sbc@chromium.org>
Thu, 26 Apr 2018 18:15:32 +0000 (18:15 +0000)
Summary: See https://github.com/WebAssembly/tool-conventions/issues/54

Subscribers: jfb, dschuff, jgravelle-google, aheejin, sunfish, llvm-commits

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

llvm-svn: 330969

31 files changed:
llvm/include/llvm/BinaryFormat/Wasm.h
llvm/include/llvm/ObjectYAML/WasmYAML.h
llvm/lib/MC/WasmObjectWriter.cpp
llvm/lib/Object/WasmObjectFile.cpp
llvm/lib/ObjectYAML/WasmYAML.cpp
llvm/test/MC/WebAssembly/array-fill.ll
llvm/test/MC/WebAssembly/bss.ll
llvm/test/MC/WebAssembly/comdat.ll
llvm/test/MC/WebAssembly/explicit-sections.ll
llvm/test/MC/WebAssembly/global-ctor-dtor.ll
llvm/test/MC/WebAssembly/unnamed-data.ll
llvm/test/MC/WebAssembly/visibility.ll
llvm/test/MC/WebAssembly/weak-alias.ll
llvm/test/MC/WebAssembly/weak.ll
llvm/test/Object/Inputs/trivial-object-test.wasm
llvm/test/Object/obj2yaml.test
llvm/test/Object/wasm-bad-metadata-version.yaml [new file with mode: 0644]
llvm/test/ObjectYAML/wasm/code_section.yaml
llvm/test/ObjectYAML/wasm/data_section.yaml
llvm/test/ObjectYAML/wasm/invalid_global_weak.yaml
llvm/test/ObjectYAML/wasm/linking_section.yaml
llvm/test/ObjectYAML/wasm/weak_symbols.yaml
llvm/test/tools/llvm-nm/wasm/exports.yaml
llvm/test/tools/llvm-nm/wasm/imports.yaml
llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml
llvm/test/tools/llvm-objdump/Inputs/trivial.obj.wasm
llvm/test/tools/llvm-objdump/wasm.txt
llvm/test/tools/llvm-readobj/Inputs/trivial.obj.wasm
llvm/test/tools/llvm-readobj/sections.test
llvm/tools/obj2yaml/wasm2yaml.cpp
llvm/tools/yaml2obj/yaml2wasm.cpp

index dc28a3c..47657ca 100644 (file)
@@ -24,6 +24,8 @@ namespace wasm {
 const char WasmMagic[] = {'\0', 'a', 's', 'm'};
 // Wasm binary format version
 const uint32_t WasmVersion = 0x1;
+// Wasm linking metadata version
+const uint32_t WasmMetadataVersion = 0x1;
 // Wasm uses a 64k page size
 const uint32_t WasmPageSize = 65536;
 
@@ -161,6 +163,7 @@ struct WasmFunctionName {
 };
 
 struct WasmLinkingData {
+  uint32_t Version;
   std::vector<WasmInitFunc> InitFunctions;
   std::vector<StringRef> Comdats;
   std::vector<WasmSymbolInfo> SymbolTable;
index 1c5e77e..d794e1d 100644 (file)
@@ -195,6 +195,7 @@ struct LinkingSection : CustomSection {
     return C && C->Name == "linking";
   }
 
+  uint32_t Version;
   std::vector<SymbolInfo> SymbolTable;
   std::vector<SegmentInfo> SegmentInfos;
   std::vector<InitFunction> InitFunctions;
index 0f8a42a..0e12270 100644 (file)
@@ -857,8 +857,9 @@ void WasmObjectWriter::writeLinkingMetaDataSection(
     const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
   SectionBookkeeping Section;
   startCustomSection(Section, "linking");
-  SectionBookkeeping SubSection;
+  encodeULEB128(wasm::WasmMetadataVersion, getStream());
 
+  SectionBookkeeping SubSection;
   if (SymbolInfos.size() != 0) {
     startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
     encodeULEB128(SymbolInfos.size(), getStream());
index e6b7dde..61db5d2 100644 (file)
@@ -313,6 +313,14 @@ Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr,
         "Linking data must come after code section", object_error::parse_failed);
   }
 
+  LinkingData.Version = readVaruint32(Ptr);
+  if (LinkingData.Version != wasm::WasmMetadataVersion) {
+    return make_error<GenericBinaryError>(
+        "Unexpected metadata version: " + Twine(LinkingData.Version) +
+            " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")",
+        object_error::parse_failed);
+  }
+
   while (Ptr < End) {
     uint8_t Type = readUint8(Ptr);
     uint32_t Size = readVaruint32(Ptr);
index 72df93b..ff75bfc 100644 (file)
@@ -57,6 +57,7 @@ static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) {
 static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) {
   commonSectionMapping(IO, Section);
   IO.mapRequired("Name", Section.Name);
+  IO.mapRequired("Version", Section.Version);
   IO.mapOptional("SymbolTable", Section.SymbolTable);
   IO.mapOptional("SegmentInfo", Section.SegmentInfos);
   IO.mapOptional("InitFunctions", Section.InitFunctions);
index 928db06..4430327 100644 (file)
@@ -12,9 +12,7 @@ target triple = "wasm32-unknown-unknown-wasm"
 ; CHECK:        - Type:            DATA
 ; CHECK:              Content:         '0102'
 
-; CHECK:        - Type:            CUSTOM
-; CHECK-NEXT:     Name:            linking
-; CHECK-NEXT:     SymbolTable:
+; CHECK:          SymbolTable:
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            DATA
 ; CHECK-NEXT:         Name:            gBd
index 3147aa5..9ffeacc 100644 (file)
@@ -35,9 +35,8 @@ target triple = "wasm32-unknown-unknown-wasm"
 ; CHECK-NEXT:           Opcode:          I32_CONST
 ; CHECK-NEXT:           Value:           8
 ; CHECK-NEXT:         Content:         ''
-; CHECK-NEXT:   - Type:            CUSTOM
-; CHECK-NEXT:     Name:            linking
-; CHECK-NEXT:     SymbolTable:
+
+; CHECK:          SymbolTable:
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            DATA
 ; CHECK-NEXT:         Name:            g0
index 7812e28..03c5d75 100644 (file)
@@ -75,6 +75,7 @@ define linkonce_odr i32 @sharedFn() #1 comdat($sharedComdat) {
 ; CHECK-NEXT:        Content:         '616263'
 ; CHECK-NEXT:  - Type:            CUSTOM
 ; CHECK-NEXT:    Name:            linking
+; CHECK-NEXT:    Version:         1
 ; CHECK-NEXT:    SymbolTable:
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            FUNCTION
index 9b76927..067f3b3 100644 (file)
@@ -30,9 +30,8 @@ target triple = "wasm32-unknown-unknown-wasm"
 ; CHECK-NEXT:           Opcode:          I32_CONST
 ; CHECK-NEXT:           Value:           24
 ; CHECK-NEXT:         Content:         '08000000'
-; CHECK-NEXT:   - Type:            CUSTOM
-; CHECK-NEXT:     Name:            linking
-; CHECK-NEXT:     SymbolTable:
+
+; CHECK:          SymbolTable:
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            DATA
 ; CHECK-NEXT:         Name:            global0
index c81e9ad..1f1358f 100644 (file)
@@ -110,6 +110,7 @@ declare void @func3()
 ; CHECK-NEXT:         Content:         '01040000'
 ; CHECK-NEXT:   - Type:            CUSTOM
 ; CHECK-NEXT:     Name:            linking
+; CHECK-NEXT:     Version:         1
 ; CHECK-NEXT:     SymbolTable:
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            FUNCTION
index 424209e..ee5abed 100644 (file)
@@ -44,6 +44,7 @@ target triple = "wasm32-unknown-unknown-wasm"
 ; CHECK-NEXT:         Content:         '06000000'
 ; CHECK-NEXT:   - Type:            CUSTOM
 ; CHECK-NEXT:     Name:            linking
+; CHECK-NEXT:     Version:         1
 ; CHECK-NEXT:     SymbolTable:      
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            DATA
index 8bdaa1e..49a922d 100644 (file)
@@ -14,9 +14,7 @@ entry:
   ret void
 }
 
-; CHECK:        - Type:            CUSTOM
-; CHECK-NEXT:     Name:            linking
-; CHECK-NEXT:     SymbolTable:
+; CHECK:          SymbolTable:
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Name:            defaultVis
index b9d855c..fee055f 100644 (file)
@@ -138,6 +138,7 @@ entry:
 ; CHECK-NEXT:         Content:         '01000000'
 ; CHECK-NEXT:   - Type:            CUSTOM
 ; CHECK-NEXT:     Name:            linking
+; CHECK-NEXT:     Version:         1
 ; CHECK-NEXT:     SymbolTable:      
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            FUNCTION
index f040225..1ba48fa 100644 (file)
@@ -20,9 +20,7 @@ entry:
 ; CHECK-NEXT:         Field:           __indirect_function_table
 
 
-; CHECK:        - Type:            CUSTOM
-; CHECK-NEXT:     Name:            linking
-; CHECK-NEXT:     SymbolTable:      
+; CHECK:          SymbolTable:      
 ; CHECK-NEXT:       - Index:           0
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Name:            weak_function
index f992b12..2aa042d 100644 (file)
Binary files a/llvm/test/Object/Inputs/trivial-object-test.wasm and b/llvm/test/Object/Inputs/trivial-object-test.wasm differ
index 235b461..d9540d4 100644 (file)
@@ -651,6 +651,7 @@ WASM-NEXT: FileHeader:
 WASM-NEXT:   Version:         0x00000001
 WASM:        - Type:            CUSTOM
 WASM-NEXT:     Name:            linking
+WASM-NEXT:     Version:         1
 WASM-NEXT:     SymbolTable:
 WASM-NEXT:       - Index:           0
 WASM-NEXT:         Kind:            FUNCTION
diff --git a/llvm/test/Object/wasm-bad-metadata-version.yaml b/llvm/test/Object/wasm-bad-metadata-version.yaml
new file mode 100644 (file)
index 0000000..b2970bb
--- /dev/null
@@ -0,0 +1,11 @@
+# RUN: yaml2obj %s | not llvm-objdump -h - 2>&1 | FileCheck %s
+
+!WASM
+FileHeader:
+  Version:         0x00000001
+Sections:
+  - Type:            CUSTOM
+    Name:            linking
+    Version:         0
+
+# CHECK: {{.*}}: Unexpected metadata version: 0 (Expected: 1)
index 62f60d6..0d7cf61 100644 (file)
@@ -39,6 +39,7 @@ Sections:
         Body:            108180808000210020000F0B
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
       - Index:           0
         Kind:            FUNCTION
index 5c9b688..ef5945d 100644 (file)
@@ -24,6 +24,7 @@ Sections:
         Addend:          -6
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
       - Index:           0
         Kind:            DATA
index 557882d..c364075 100644 (file)
@@ -13,6 +13,7 @@ Sections:
         GlobalMutable:   false
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
       - Index:           0
         Kind:            GLOBAL
index 0c2c864..d1f0243 100644 (file)
@@ -29,6 +29,7 @@ Sections:
         Content:         '11110000'
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
       - Index:           0
         Kind:            FUNCTION
@@ -48,9 +49,7 @@ Sections:
       - Priority:      1
         Symbol:        0
 ...
-# CHECK:       - Type:            CUSTOM
-# CHECK-NEXT:    Name:            linking
-# CHECK-NEXT:    SymbolTable:
+# CHECK:         SymbolTable:
 # CHECK-NEXT:      - Index:           0
 # CHECK-NEXT:        Kind:            FUNCTION
 # CHECK-NEXT:        Name:            bar
index 3957caa..3e9ca34 100644 (file)
@@ -36,6 +36,7 @@ Sections:
         Body:            00
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
       - Index:           0
         Kind:            FUNCTION
index de87978..55d2b76 100644 (file)
@@ -37,6 +37,7 @@ Sections:
         Content:         '616263'
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
        - Index:           0
          Kind:            FUNCTION
index 23f7218..9696972 100644 (file)
@@ -25,6 +25,7 @@ Sections:
         GlobalMutable:   false
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
        - Index:           0
          Kind:            FUNCTION
index e758194..caa981d 100644 (file)
@@ -43,6 +43,7 @@ Sections:
         Content:         '616263'
   - Type:            CUSTOM
     Name:            linking
+    Version:         1
     SymbolTable:
        - Index:           0
          Kind:            DATA
index f992b12..2aa042d 100644 (file)
Binary files a/llvm/test/tools/llvm-objdump/Inputs/trivial.obj.wasm and b/llvm/test/tools/llvm-objdump/Inputs/trivial.obj.wasm differ
index 622df7b..d24db89 100644 (file)
@@ -7,7 +7,7 @@
 # CHECK-NEXT:  2 FUNCTION      00000002 0000000000000000
 # CHECK-NEXT:  3 CODE          00000019 0000000000000000 TEXT
 # CHECK-NEXT:  4 DATA          0000001c 0000000000000000 DATA
-# CHECK-NEXT:  5 linking       0000004d 0000000000000000
+# CHECK-NEXT:  5 linking       00000051 0000000000000000
 # CHECK-NEXT:  6 reloc.CODE    0000000c 0000000000000000
 
 # RUN: llvm-objdump -p %p/Inputs/trivial.obj.wasm | FileCheck %s -check-prefix CHECK-HEADER
index 15700e9..0e3efb6 100644 (file)
Binary files a/llvm/test/tools/llvm-readobj/Inputs/trivial.obj.wasm and b/llvm/test/tools/llvm-readobj/Inputs/trivial.obj.wasm differ
index a5fa7f5..4900c4f 100644 (file)
@@ -528,14 +528,14 @@ WASM-NEXT:     ]
 WASM-NEXT:   }
 WASM-NEXT:   Section {
 WASM-NEXT:     Type: CUSTOM (0x0)
-WASM-NEXT:     Size: 57
+WASM-NEXT:     Size: 61
 WASM-NEXT:     Offset: 191
 WASM-NEXT:     Name: linking
 WASM-NEXT:   }
 WASM-NEXT:   Section {
 WASM-NEXT:     Type: CUSTOM (0x0)
 WASM-NEXT:     Size: 12
-WASM-NEXT:     Offset: 262
+WASM-NEXT:     Offset: 266
 WASM-NEXT:     Name: reloc.CODE
 WASM-NEXT:   }
 WASM-NEXT: ]
index 29bf9b5..55fc2bf 100644 (file)
@@ -62,6 +62,8 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
     CustomSec = std::move(NameSec);
   } else if (WasmSec.Name == "linking") {
     std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = make_unique<WasmYAML::LinkingSection>();
+    LinkingSec->Version = Obj.linkingData().Version;
+
     ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
     for (StringRef ComdatName : Comdats)
       LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
@@ -71,6 +73,7 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
                 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
       }
     }
+
     uint32_t SegmentIndex = 0;
     for (const object::WasmSegment &Segment : Obj.dataSegments()) {
       if (!Segment.Data.Name.empty()) {
@@ -87,6 +90,7 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
       }
       SegmentIndex++;
     }
+
     uint32_t SymbolIndex = 0;
     for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
       WasmYAML::SymbolInfo Info;
@@ -105,10 +109,12 @@ std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const Was
       }
       LinkingSec->SymbolTable.emplace_back(Info);
     }
+
     for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
       WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
       LinkingSec->InitFunctions.emplace_back(F);
     }
+
     CustomSec = std::move(LinkingSec);
   } else {
     CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
index 9b2a98d..9be2f09 100644 (file)
@@ -134,6 +134,7 @@ public:
 
 int WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section) {
   writeStringRef(Section.Name, OS);
+  encodeULEB128(Section.Version, OS);
 
   SubSectionWriter SubSection(OS);