From 8db61ed29df0776046e64a0bd444f0baa7bc37e9 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 22 Jul 2023 09:37:22 -0700 Subject: [PATCH] [WebAssembly] Stabilize custom section order It currently depends on the StringMap iteration order, which is not guaranteed to be deterministic. Use MapVector to stabilize the order. --- lld/test/wasm/custom-sections.ll | 6 +++--- lld/test/wasm/debug-undefined-fs.s | 4 ++-- lld/test/wasm/section-symbol-relocs.yaml | 8 ++++---- lld/wasm/Writer.cpp | 5 +++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lld/test/wasm/custom-sections.ll b/lld/test/wasm/custom-sections.ll index c33ca277..9d30283 100644 --- a/lld/test/wasm/custom-sections.ll +++ b/lld/test/wasm/custom-sections.ll @@ -15,8 +15,8 @@ entry: !wasm.custom_sections = !{ !0 } ; CHECK: - Type: CUSTOM -; CHECK-NEXT: Name: green -; CHECK-NEXT: Payload: '626172717578' -; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: red ; CHECK-NEXT: Payload: 6578747261666F6F +; CHECK-NEXT: - Type: CUSTOM +; CHECK-NEXT: Name: green +; CHECK-NEXT: Payload: '626172717578' diff --git a/lld/test/wasm/debug-undefined-fs.s b/lld/test/wasm/debug-undefined-fs.s index 4426e84..06d248b 100644 --- a/lld/test/wasm/debug-undefined-fs.s +++ b/lld/test/wasm/debug-undefined-fs.s @@ -31,7 +31,7 @@ _start: .int32 undef .int32 .Ld -# CHECK: Name: .debug_info -# CHECK-NEXT: Payload: 02000000FFFFFFFF00000000 # CHECK: Name: .debug_int # CHECK-NEXT: Payload: '01000000' +# CHECK: Name: .debug_info +# CHECK-NEXT: Payload: 02000000FFFFFFFF00000000 diff --git a/lld/test/wasm/section-symbol-relocs.yaml b/lld/test/wasm/section-symbol-relocs.yaml index 34126cbf..050aaa6 100644 --- a/lld/test/wasm/section-symbol-relocs.yaml +++ b/lld/test/wasm/section-symbol-relocs.yaml @@ -34,18 +34,18 @@ Sections: Flags: [ BINDING_LOCAL ] ... -# CHECK: Name: green -# CHECK-NEXT: Payload: 626172717578AA0600000003000000 # CHECK: Name: red # CHECK-NEXT: Payload: 666F6FBB0000000000000000 +# CHECK: Name: green +# CHECK-NEXT: Payload: 626172717578AA0600000003000000 # RELOC: Relocations: # RELOC-NEXT: - Type: R_WASM_SECTION_OFFSET_I32 -# RELOC-NEXT: Index: 0 +# RELOC-NEXT: Index: 1 # RELOC-NEXT: Offset: 0x7 # RELOC-NEXT: Addend: 6 # RELOC-NEXT: - Type: R_WASM_SECTION_OFFSET_I32 -# RELOC-NEXT: Index: 1 +# RELOC-NEXT: Index: 0 # RELOC-NEXT: Offset: 0xB # RELOC-NEXT: Addend: 3 # RELOC-NEXT: Name: green diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp index 25a9c15..eb64783 100644 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -22,6 +22,7 @@ #include "lld/Common/Strings.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/MapVector.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" @@ -112,7 +113,7 @@ private: uint64_t fileSize = 0; std::vector initFunctions; - llvm::StringMap> customSectionMapping; + llvm::MapVector> customSectionMapping; // Stable storage for command export wrapper function name strings. std::list commandExportWrapperNames; @@ -162,7 +163,7 @@ void Writer::calculateCustomSections() { void Writer::createCustomSections() { log("createCustomSections"); for (auto &pair : customSectionMapping) { - StringRef name = pair.first(); + StringRef name = pair.first; LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n"); OutputSection *sec = make(std::string(name), pair.second); -- 2.7.4