Index: test/wasm/data-segment-merging.ll =================================================================== --- /dev/null +++ test/wasm/data-segment-merging.ll @@ -0,0 +1,48 @@ +target triple = "wasm32-unknown-unknown-wasm" + +@A_HELLO = hidden global [6 x i8] c"hello\00", align 1 +@A_GOODBYE = hidden global [8 x i8] c"goodbye\00", align 1 +@A_WHATEVER = hidden global [9 x i8] c"whatever\00", align 1 +@N_ITERATIONS = hidden global i32 42, align 4 + +; RUN: llc -filetype=obj %s -o %t.data-segment-merging.o + +; RUN: wasm-ld -no-gc-sections --allow-undefined -o %t.merged.wasm %t.data-segment-merging.o +; RUN: obj2yaml %t.merged.wasm | FileCheck %s --check-prefix=MERGE +; MERGE: - Type: DATA +; MERGE: Segments: +; MERGE: - SectionOffset: 7 +; MERGE: MemoryIndex: 0 +; MERGE: Offset: +; MERGE: Opcode: I32_CONST +; MERGE: Value: 1024 +; MERGE: Content: 68656C6C6F00676F6F6462796500776861746576657200002A000000 + +; RUN: wasm-ld -no-gc-sections --allow-undefined --no-merge-data-segments -o %t.separate.wasm %t.data-segment-merging.o +; RUN: obj2yaml %t.separate.wasm | FileCheck %s --check-prefix=SEPARATE +; SEPARATE: - Type: DATA +; SEPARATE: Segments: +; SEPARATE: - SectionOffset: 7 +; SEPARATE: MemoryIndex: 0 +; SEPARATE: Offset: +; SEPARATE: Opcode: I32_CONST +; SEPARATE: Value: 1024 +; SEPARATE: Content: 68656C6C6F00 +; SEPARATE: - SectionOffset: 19 +; SEPARATE: MemoryIndex: 0 +; SEPARATE: Offset: +; SEPARATE: Opcode: I32_CONST +; SEPARATE: Value: 1030 +; SEPARATE: Content: 676F6F6462796500 +; SEPARATE: - SectionOffset: 33 +; SEPARATE: MemoryIndex: 0 +; SEPARATE: Offset: +; SEPARATE: Opcode: I32_CONST +; SEPARATE: Value: 1038 +; SEPARATE: Content: '776861746576657200' +; SEPARATE: - SectionOffset: 48 +; SEPARATE: MemoryIndex: 0 +; SEPARATE: Offset: +; SEPARATE: Opcode: I32_CONST +; SEPARATE: Value: 1048 +; SEPARATE: Content: 2A000000 Index: wasm/Config.h =================================================================== --- wasm/Config.h +++ wasm/Config.h @@ -25,6 +25,7 @@ bool GcSections; bool ImportMemory; bool ImportTable; + bool MergeDataSegments; bool PrintGcSections; bool Relocatable; bool StripAll; Index: wasm/Driver.cpp =================================================================== --- wasm/Driver.cpp +++ wasm/Driver.cpp @@ -293,6 +293,9 @@ Config->Relocatable = Args.hasArg(OPT_relocatable); Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable); + Config->MergeDataSegments = + Args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments, + !Config->Relocatable); Config->PrintGcSections = Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false); Config->SearchPaths = args::getStrings(Args, OPT_L); Index: wasm/Options.td =================================================================== --- wasm/Options.td +++ wasm/Options.td @@ -40,6 +40,10 @@ "Enable garbage collection of unused sections", "Disable garbage collection of unused sections">; +defm merge_data_segments: B<"merge-data-segments", + "Enable merging data segments", + "Disable merging data segments">; + def help: F<"help">, HelpText<"Print option help">; def l: JoinedOrSeparate<["-"], "l">, MetaVarName<"">, Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -858,7 +858,7 @@ } static StringRef getOutputDataSegmentName(StringRef Name) { - if (Config->Relocatable) + if (!Config->MergeDataSegments) return Name; if (Name.startswith(".text.")) return ".text";