[WebAssembly] Sort output data sections to place .bss last
Summary:
This was always the intended behavior, but had not been
implemented. This ordering is important for Emscripten when generating
.mem files while compiling to JS, since only zeros at the end of
initialized memory can be dropped.
Fixes https://github.com/emscripten-core/emscripten/issues/8999
Reviewers: sbc100
Subscribers: dschuff, jgravelle-google, aheejin, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67736
git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@372284 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/wasm/custom-section-name.ll b/test/wasm/custom-section-name.ll
new file mode 100644
index 0000000..2d46810
--- /dev/null
+++ b/test/wasm/custom-section-name.ll
@@ -0,0 +1,58 @@
+; RUN: llc -filetype=obj %s -o %t.o
+; RUN: wasm-ld -no-gc-sections --no-entry -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s
+; RUN: wasm-ld -no-gc-sections --no-entry -o %t_reloc.o %t.o --relocatable
+; RUN: obj2yaml %t_reloc.o | FileCheck -check-prefix RELOC %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+@bss = hidden global i32 zeroinitializer, align 4
+@foo = hidden global i32 zeroinitializer, section "WowZero!", align 4
+@bar = hidden constant i32 42, section "MyAwesomeSection", align 4
+@baz = hidden global i32 7, section "AnotherGreatSection", align 4
+
+; CHECK-LABEL: - Type: DATA
+; CHECK-NEXT: Segments:
+; CHECK-NEXT: - SectionOffset: 7
+; CHECK-NEXT: InitFlags: 0
+; CHECK-NEXT: Offset:
+; CHECK-NEXT: Opcode: I32_CONST
+; CHECK-NEXT: Value: 1024
+; CHECK-NEXT: Content: '00000000'
+; CHECK-NEXT: - SectionOffset: 17
+; CHECK-NEXT: InitFlags: 0
+; CHECK-NEXT: Offset:
+; CHECK-NEXT: Opcode: I32_CONST
+; CHECK-NEXT: Value: 1028
+; CHECK-NEXT: Content: 2A000000
+; CHECK-NEXT: - SectionOffset: 27
+; CHECK-NEXT: InitFlags: 0
+; CHECK-NEXT: Offset:
+; CHECK-NEXT: Opcode: I32_CONST
+; CHECK-NEXT: Value: 1032
+; CHECK-NEXT: Content: '07000000'
+; CHECK-NEXT: - SectionOffset: 37
+; CHECK-NEXT: InitFlags: 0
+; CHECK-NEXT: Offset:
+; CHECK-NEXT: Opcode: I32_CONST
+; CHECK-NEXT: Value: 1036
+; CHECK-NEXT: Content: '00000000'
+
+; RELOC-LABEL: SegmentInfo:
+; RELOC-NEXT: - Index: 0
+; RELOC-NEXT: Name: 'WowZero!'
+; RELOC-NEXT: Alignment: 2
+; RELOC-NEXT: Flags: [ ]
+; RELOC-NEXT: - Index: 1
+; RELOC-NEXT: Name: MyAwesomeSection
+; RELOC-NEXT: Alignment: 2
+; RELOC-NEXT: Flags: [ ]
+; RELOC-NEXT: - Index: 2
+; RELOC-NEXT: Name: AnotherGreatSection
+; RELOC-NEXT: Alignment: 2
+; RELOC-NEXT: Flags: [ ]
+; RELOC-NEXT: - Index: 3
+; RELOC-NEXT: Name: .bss.bss
+; RELOC-NEXT: Alignment: 2
+; RELOC-NEXT: Flags: [ ]
diff --git a/test/wasm/data-layout.ll b/test/wasm/data-layout.ll
index 8885123..2db83e9 100644
--- a/test/wasm/data-layout.ll
+++ b/test/wasm/data-layout.ll
@@ -26,19 +26,19 @@
; CHECK-NEXT: Mutable: true
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 66608
+; CHECK-NEXT: Value: 66624
; CHECK-NEXT: - Index: 1
; CHECK-NEXT: Type: I32
; CHECK-NEXT: Mutable: false
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 1071
+; CHECK-NEXT: Value: 1080
; CHECK-NEXT: - Index: 2
; CHECK-NEXT: Type: I32
; CHECK-NEXT: Mutable: false
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 66608
+; CHECK-NEXT: Value: 66624
; CHECK: - Type: DATA
; CHECK-NEXT: Segments:
@@ -47,19 +47,19 @@
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 1024
-; CHECK-NEXT: Content: '0100000000000000000000000000000003000000000000002804000024040000'
-; CHECK-NEXT: - SectionOffset: 45
-; CHECK-NEXT: InitFlags: 0
-; CHECK-NEXT: Offset:
-; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 1056
-; CHECK-NEXT: Content: '0000000000000000'
-; CHECK-NEXT: - SectionOffset: 59
-; CHECK-NEXT: InitFlags: 0
-; CHECK-NEXT: Offset:
-; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 1064
; CHECK-NEXT: Content: 68656C6C6F0A00
+; CHECK-NEXT: - SectionOffset: 20
+; CHECK-NEXT: InitFlags: 0
+; CHECK-NEXT: Offset:
+; CHECK-NEXT: Opcode: I32_CONST
+; CHECK-NEXT: Value: 1040
+; CHECK-NEXT: Content: '0100000000000000000000000000000003000000000000000004000034040000'
+; CHECK-NEXT: - SectionOffset: 58
+; CHECK-NEXT: InitFlags: 0
+; CHECK-NEXT: Offset:
+; CHECK-NEXT: Opcode: I32_CONST
+; CHECK-NEXT: Value: 1072
+; CHECK-NEXT: Content: '0000000000000000'
; CHECK-NEXT: - Type: CUSTOM
@@ -92,10 +92,10 @@
; RELOC-NEXT: Relocations:
; RELOC-NEXT: - Type: R_WASM_MEMORY_ADDR_I32
; RELOC-NEXT: Index: 3
-; RELOC-NEXT: Offset: 0x00000018
+; RELOC-NEXT: Offset: 0x00000024
; RELOC-NEXT: - Type: R_WASM_MEMORY_ADDR_I32
; RELOC-NEXT: Index: 4
-; RELOC-NEXT: Offset: 0x0000002E
+; RELOC-NEXT: Offset: 0x0000002D
; RELOC-NEXT: Addend: 4
; RELOC-NEXT: Segments:
; RELOC-NEXT: - SectionOffset: 6
@@ -103,60 +103,60 @@
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 0
+; RELOC-NEXT: Content: 68656C6C6F0A00
+; RELOC-NEXT: - SectionOffset: 18
+; RELOC-NEXT: InitFlags: 0
+; RELOC-NEXT: Offset:
+; RELOC-NEXT: Opcode: I32_CONST
+; RELOC-NEXT: Value: 8
; RELOC-NEXT: Content: '01000000'
-; RELOC-NEXT: - SectionOffset: 15
+; RELOC-NEXT: - SectionOffset: 27
; RELOC-NEXT: InitFlags: 0
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 16
; RELOC-NEXT: Content: '03000000'
-; RELOC-NEXT: - SectionOffset: 24
+; RELOC-NEXT: - SectionOffset: 36
; RELOC-NEXT: InitFlags: 0
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 24
-; RELOC-NEXT: Content: '28000000'
-; RELOC-NEXT: - SectionOffset: 33
+; RELOC-NEXT: Content: '00000000'
+; RELOC-NEXT: - SectionOffset: 45
; RELOC-NEXT: InitFlags: 0
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 28
+; RELOC-NEXT: Content: '24000000'
+; RELOC-NEXT: - SectionOffset: 54
+; RELOC-NEXT: InitFlags: 0
+; RELOC-NEXT: Offset:
+; RELOC-NEXT: Opcode: I32_CONST
+; RELOC-NEXT: Value: 32
; RELOC-NEXT: Content: '0000000000000000'
-; RELOC-NEXT: - SectionOffset: 46
-; RELOC-NEXT: InitFlags: 0
-; RELOC-NEXT: Offset:
-; RELOC-NEXT: Opcode: I32_CONST
-; RELOC-NEXT: Value: 36
-; RELOC-NEXT: Content: '20000000'
-; RELOC-NEXT: - SectionOffset: 55
-; RELOC-NEXT: InitFlags: 0
-; RELOC-NEXT: Offset:
-; RELOC-NEXT: Opcode: I32_CONST
-; RELOC-NEXT: Value: 40
-; RELOC-NEXT: Content: 68656C6C6F0A00
; RELOC: SymbolTable:
; RELOC-NEXT: - Index: 0
; RELOC-NEXT: Kind: DATA
; RELOC-NEXT: Name: foo
; RELOC-NEXT: Flags: [ VISIBILITY_HIDDEN ]
-; RELOC-NEXT: Segment: 0
+; RELOC-NEXT: Segment: 1
; RELOC-NEXT: Size: 4
; RELOC-NEXT: - Index: 1
; RELOC-NEXT: Kind: DATA
; RELOC-NEXT: Name: aligned_bar
; RELOC-NEXT: Flags: [ VISIBILITY_HIDDEN ]
-; RELOC-NEXT: Segment: 1
+; RELOC-NEXT: Segment: 2
; RELOC-NEXT: Size: 4
; RELOC-NEXT: - Index: 2
; RELOC-NEXT: Kind: DATA
; RELOC-NEXT: Name: external_ref
; RELOC-NEXT: Flags: [ ]
-; RELOC-NEXT: Segment: 2
+; RELOC-NEXT: Segment: 3
; RELOC-NEXT: Size: 4
; RELOC-NEXT: - Index: 3
; RELOC-NEXT: Kind: DATA
; RELOC-NEXT: Name: hello_str
; RELOC-NEXT: Flags: [ ]
-; RELOC-NEXT: Segment: 5
+; RELOC-NEXT: Segment: 0
; RELOC-NEXT: Size: 7
diff --git a/test/wasm/data-segment-merging.ll b/test/wasm/data-segment-merging.ll
index 4ab323b..4533823 100644
--- a/test/wasm/data-segment-merging.ll
+++ b/test/wasm/data-segment-merging.ll
@@ -19,11 +19,11 @@
; MERGE-NEXT: - SectionOffset: 7
; MERGE-NEXT: InitFlags: 0
; MERGE-NEXT: Offset:
-; MERGE: Content: 68656C6C6F00676F6F6462796500776861746576657200002A000000
-; MERGE-NEXT: - SectionOffset: 41
+; MERGE: Content: 636F6E7374616E74000000002B
+; MERGE-NEXT: - SectionOffset: 26
; MERGE-NEXT: InitFlags: 0
; MERGE-NEXT: Offset:
-; MERGE: Content: 636F6E7374616E74000000002B
+; MERGE: Content: 68656C6C6F00676F6F6462796500776861746576657200002A000000
; MERGE-NEXT: - Type: CUSTOM
; MERGE-NEXT: Name: name
; MERGE-NEXT: FunctionNames:
@@ -40,27 +40,27 @@
; SEPARATE-NEXT: - SectionOffset: 7
; SEPARATE-NEXT: InitFlags: 0
; SEPARATE-NEXT: Offset:
-; SEPARATE: Content: 68656C6C6F00
-; SEPARATE-NEXT: - SectionOffset: 19
-; SEPARATE-NEXT: InitFlags: 0
-; SEPARATE-NEXT: Offset:
-; SEPARATE: Content: 676F6F6462796500
-; SEPARATE-NEXT: - SectionOffset: 33
-; SEPARATE-NEXT: InitFlags: 0
-; SEPARATE-NEXT: Offset:
-; SEPARATE: Content: '776861746576657200'
-; SEPARATE-NEXT: - SectionOffset: 48
-; SEPARATE-NEXT: InitFlags: 0
-; SEPARATE-NEXT: Offset:
-; SEPARATE: Content: 2A000000
-; SEPARATE-NEXT: - SectionOffset: 58
-; SEPARATE-NEXT: InitFlags: 0
-; SEPARATE-NEXT: Offset:
; SEPARATE: Content: 636F6E7374616E7400
-; SEPARATE-NEXT: - SectionOffset: 73
+; SEPARATE-NEXT: - SectionOffset: 22
; SEPARATE-NEXT: InitFlags: 0
; SEPARATE-NEXT: Offset:
; SEPARATE: Content: 2B
+; SEPARATE-NEXT: - SectionOffset: 29
+; SEPARATE-NEXT: InitFlags: 0
+; SEPARATE-NEXT: Offset:
+; SEPARATE: Content: 68656C6C6F00
+; SEPARATE-NEXT: - SectionOffset: 41
+; SEPARATE-NEXT: InitFlags: 0
+; SEPARATE-NEXT: Offset:
+; SEPARATE: Content: 676F6F6462796500
+; SEPARATE-NEXT: - SectionOffset: 55
+; SEPARATE-NEXT: InitFlags: 0
+; SEPARATE-NEXT: Offset:
+; SEPARATE: Content: '776861746576657200'
+; SEPARATE-NEXT: - SectionOffset: 70
+; SEPARATE-NEXT: InitFlags: 0
+; SEPARATE-NEXT: Offset:
+; SEPARATE: Content: 2A000000
; SEPARATE-NEXT: - Type: CUSTOM
; SEPARATE-NEXT: Name: name
; SEPARATE-NEXT: FunctionNames:
@@ -77,10 +77,10 @@
; PASSIVE-MERGE-NEXT: Segments:
; PASSIVE-MERGE-NEXT: - SectionOffset: 3
; PASSIVE-MERGE-NEXT: InitFlags: 1
-; PASSIVE-MERGE-NEXT: Content: 68656C6C6F00676F6F6462796500776861746576657200002A000000
-; PASSIVE-MERGE-NEXT: - SectionOffset: 33
-; PASSIVE-MERGE-NEXT: InitFlags: 1
; PASSIVE-MERGE-NEXT: Content: 636F6E7374616E74000000002B
+; PASSIVE-MERGE-NEXT: - SectionOffset: 18
+; PASSIVE-MERGE-NEXT: InitFlags: 1
+; PASSIVE-MERGE-NEXT: Content: 68656C6C6F00676F6F6462796500776861746576657200002A000000
; PASSIVE-MERGE-NEXT: - Type: CUSTOM
; PASSIVE-MERGE-NEXT: Name: name
; PASSIVE-MERGE-NEXT: FunctionNames:
@@ -100,22 +100,22 @@
; PASSIVE-SEPARATE-NEXT: Segments:
; PASSIVE-SEPARATE-NEXT: - SectionOffset: 3
; PASSIVE-SEPARATE-NEXT: InitFlags: 1
-; PASSIVE-SEPARATE-NEXT: Content: 68656C6C6F00
-; PASSIVE-SEPARATE-NEXT: - SectionOffset: 11
-; PASSIVE-SEPARATE-NEXT: InitFlags: 1
-; PASSIVE-SEPARATE-NEXT: Content: 676F6F6462796500
-; PASSIVE-SEPARATE-NEXT: - SectionOffset: 21
-; PASSIVE-SEPARATE-NEXT: InitFlags: 1
-; PASSIVE-SEPARATE-NEXT: Content: '776861746576657200'
-; PASSIVE-SEPARATE-NEXT: - SectionOffset: 32
-; PASSIVE-SEPARATE-NEXT: InitFlags: 1
-; PASSIVE-SEPARATE-NEXT: Content: 2A000000
-; PASSIVE-SEPARATE-NEXT: - SectionOffset: 38
-; PASSIVE-SEPARATE-NEXT: InitFlags: 1
; PASSIVE-SEPARATE-NEXT: Content: 636F6E7374616E7400
-; PASSIVE-SEPARATE-NEXT: - SectionOffset: 49
+; PASSIVE-SEPARATE-NEXT: - SectionOffset: 14
; PASSIVE-SEPARATE-NEXT: InitFlags: 1
; PASSIVE-SEPARATE-NEXT: Content: 2B
+; PASSIVE-SEPARATE-NEXT: - SectionOffset: 17
+; PASSIVE-SEPARATE-NEXT: InitFlags: 1
+; PASSIVE-SEPARATE-NEXT: Content: 68656C6C6F00
+; PASSIVE-SEPARATE-NEXT: - SectionOffset: 25
+; PASSIVE-SEPARATE-NEXT: InitFlags: 1
+; PASSIVE-SEPARATE-NEXT: Content: 676F6F6462796500
+; PASSIVE-SEPARATE-NEXT: - SectionOffset: 35
+; PASSIVE-SEPARATE-NEXT: InitFlags: 1
+; PASSIVE-SEPARATE-NEXT: Content: '776861746576657200'
+; PASSIVE-SEPARATE-NEXT: - SectionOffset: 46
+; PASSIVE-SEPARATE-NEXT: InitFlags: 1
+; PASSIVE-SEPARATE-NEXT: Content: 2A000000
; PASSIVE-SEPARATE-NEXT: - Type: CUSTOM
; PASSIVE-SEPARATE-NEXT: Name: name
; PASSIVE-SEPARATE-NEXT: FunctionNames:
diff --git a/test/wasm/data-segments.ll b/test/wasm/data-segments.ll
index b3530ed..0a15b24 100644
--- a/test/wasm/data-segments.ll
+++ b/test/wasm/data-segments.ll
@@ -37,20 +37,20 @@
; ACTIVE-NEXT: Offset:
; ACTIVE-NEXT: Opcode: I32_CONST
; ACTIVE-NEXT: Value: 1024
-; ACTIVE-NEXT: Content: 68656C6C6F00676F6F646279650000002A000000
-; ACTIVE-NEXT: - SectionOffset: 34
+; ACTIVE-NEXT: Content: 636F6E7374616E74000000002B
+; ACTIVE-NEXT: - SectionOffset: 26
; ACTIVE-NEXT: InitFlags: 0
; ACTIVE-NEXT: Offset:
; ACTIVE-NEXT: Opcode: I32_CONST
-; ACTIVE-NEXT: Value: 1044
+; ACTIVE-NEXT: Value: 1040
+; ACTIVE-NEXT: Content: 68656C6C6F00676F6F646279650000002A000000
+; ACTIVE-NEXT: - SectionOffset: 53
+; ACTIVE-NEXT: InitFlags: 0
+; ACTIVE-NEXT: Offset:
+; ACTIVE-NEXT: Opcode: I32_CONST
+; ACTIVE-NEXT: Value: 1060
; ACTIVE-NEXT: Content: '0000000000
; ACTIVE-SAME: 0000000000'
-; ACTIVE-NEXT: - SectionOffset: 10041
-; ACTIVE-NEXT: InitFlags: 0
-; ACTIVE-NEXT: Offset:
-; ACTIVE-NEXT: Opcode: I32_CONST
-; ACTIVE-NEXT: Value: 11044
-; ACTIVE-NEXT: Content: 636F6E7374616E74000000002B
; ACTIVE-NEXT: - Type: CUSTOM
; ACTIVE-NEXT: Name: name
; ACTIVE-NEXT: FunctionNames:
@@ -66,7 +66,7 @@
; PASSIVE-NEXT: Body: 0B
; PASSIVE-NEXT: - Index: 1
; PASSIVE-NEXT: Locals: []
-; PASSIVE-NEXT: Body: 41B4D60041004101FE480200044041B4D6004101427FFE0102001A0541800841004114FC08000041940841004190CE00FC08010041A4D6004100410DFC08020041B4D6004102FE17020041B4D600417FFE0002001A0BFC0900FC0901FC09020B
+; PASSIVE-NEXT: Body: 41B4D60041004101FE480200044041B4D6004101427FFE0102001A054180084100410DFC08000041900841004114FC08010041A40841004190CE00FC08020041B4D6004102FE17020041B4D600417FFE0002001A0BFC0900FC0901FC09020B
; PASSIVE-NEXT: - Index: 2
; PASSIVE-NEXT: Locals: []
; PASSIVE-NEXT: Body: 0B
@@ -74,14 +74,14 @@
; PASSIVE-NEXT: Segments:
; PASSIVE-NEXT: - SectionOffset: 3
; PASSIVE-NEXT: InitFlags: 1
+; PASSIVE-NEXT: Content: 636F6E7374616E74000000002B
+; PASSIVE-NEXT: - SectionOffset: 18
+; PASSIVE-NEXT: InitFlags: 1
; PASSIVE-NEXT: Content: 68656C6C6F00676F6F646279650000002A000000
-; PASSIVE-NEXT: - SectionOffset: 26
+; PASSIVE-NEXT: - SectionOffset: 41
; PASSIVE-NEXT: InitFlags: 1
; PASSIVE-NEXT: Content: '0000000000
; PASSIVE-SAME: 0000000000'
-; PASSIVE-NEXT: - SectionOffset: 10028
-; PASSIVE-NEXT: InitFlags: 1
-; PASSIVE-NEXT: Content: 636F6E7374616E74000000002B
; PASSIVE-NEXT: - Type: CUSTOM
; PASSIVE-NEXT: Name: name
; PASSIVE-NEXT: FunctionNames:
@@ -89,5 +89,5 @@
; PASSIVE-NEXT: Name: __wasm_call_ctors
; PASSIVE-NEXT: - Index: 1
; PASSIVE-NEXT: Name: __wasm_init_memory
-; PASSIVE-NEXT: - Index: 2
-; PASSIVE-NEXT: Name: __wasm_init_tls
+; PASSIVE-NEXT: - Index: 2
+; PASSIVE-NEXT: Name: __wasm_init_tls
diff --git a/test/wasm/reloc-addend.ll b/test/wasm/reloc-addend.ll
index fc00b8e..f232c26 100644
--- a/test/wasm/reloc-addend.ll
+++ b/test/wasm/reloc-addend.ll
@@ -12,8 +12,8 @@
@bar = hidden local_unnamed_addr global i32* getelementptr inbounds ([76 x i32], [76 x i32]* @foo, i32 0, i32 16), align 4
; CHECK: - Type: DATA
-; CHECK-NEXT: Relocations:
+; CHECK-NEXT: Relocations:
; CHECK-NEXT: - Type: R_WASM_MEMORY_ADDR_I32
; CHECK-NEXT: Index: 0
-; CHECK-NEXT: Offset: 0x0000013D
+; CHECK-NEXT: Offset: 0x00000006
; CHECK-NEXT: Addend: 64
diff --git a/test/wasm/relocatable.ll b/test/wasm/relocatable.ll
index 64dc59c..aba4279 100644
--- a/test/wasm/relocatable.ll
+++ b/test/wasm/relocatable.ll
@@ -113,21 +113,21 @@
; CHECK-NEXT: Body: 1081808080001A1082808080001A41010B
; CHECK-NEXT: - Index: 5
; CHECK-NEXT: Locals:
-; CHECK-NEXT: Body: 419C808080000B
+; CHECK-NEXT: Body: 4187808080000B
; NORMAL-NEXT: - Type: DATA
; NORMAL-NEXT: Relocations:
; NORMAL-NEXT: - Type: R_WASM_TABLE_INDEX_I32
; NORMAL-NEXT: Index: 3
-; NORMAL-NEXT: Offset: 0x00000012
+; NORMAL-NEXT: Offset: 0x0000001A
; NORMAL-NEXT: - Type: R_WASM_TABLE_INDEX_I32
; NORMAL-NEXT: Index: 4
-; NORMAL-NEXT: Offset: 0x0000001B
+; NORMAL-NEXT: Offset: 0x00000023
; NORMAL-NEXT: - Type: R_WASM_TABLE_INDEX_I32
; NORMAL-NEXT: Index: 5
-; NORMAL-NEXT: Offset: 0x00000024
+; NORMAL-NEXT: Offset: 0x0000002C
; NORMAL-NEXT: - Type: R_WASM_MEMORY_ADDR_I32
; NORMAL-NEXT: Index: 12
-; NORMAL-NEXT: Offset: 0x0000002D
+; NORMAL-NEXT: Offset: 0x00000035
; NORMAL-NEXT: Segments:
; NORMAL-NEXT: - SectionOffset: 6
; NORMAL-NEXT: InitFlags: 0
@@ -139,32 +139,32 @@
; NORMAL-NEXT: InitFlags: 0
; NORMAL-NEXT: Offset:
; NORMAL-NEXT: Opcode: I32_CONST
-; NORMAL-NEXT: Value: 8
-; NORMAL-NEXT: Content: '01000000'
-; NORMAL-NEXT: - SectionOffset: 27
+; NORMAL-NEXT: Value: 7
+; NORMAL-NEXT: Content: '616263'
+; NORMAL-NEXT: - SectionOffset: 26
; NORMAL-NEXT: InitFlags: 0
; NORMAL-NEXT: Offset:
; NORMAL-NEXT: Opcode: I32_CONST
; NORMAL-NEXT: Value: 12
-; NORMAL-NEXT: Content: '02000000'
-; NORMAL-NEXT: - SectionOffset: 36
+; NORMAL-NEXT: Content: '01000000'
+; NORMAL-NEXT: - SectionOffset: 35
; NORMAL-NEXT: InitFlags: 0
; NORMAL-NEXT: Offset:
; NORMAL-NEXT: Opcode: I32_CONST
; NORMAL-NEXT: Value: 16
+; NORMAL-NEXT: Content: '02000000'
+; NORMAL-NEXT: - SectionOffset: 44
+; NORMAL-NEXT: InitFlags: 0
+; NORMAL-NEXT: Offset:
+; NORMAL-NEXT: Opcode: I32_CONST
+; NORMAL-NEXT: Value: 20
; NORMAL-NEXT: Content: '03000000'
-; NORMAL-NEXT: - SectionOffset: 45
+; NORMAL-NEXT: - SectionOffset: 53
; NORMAL-NEXT: InitFlags: 0
; NORMAL-NEXT: Offset:
; NORMAL-NEXT: Opcode: I32_CONST
; NORMAL-NEXT: Value: 24
; NORMAL-NEXT: Content: '00000000'
-; NORMAL-NEXT: - SectionOffset: 54
-; NORMAL-NEXT: InitFlags: 0
-; NORMAL-NEXT: Offset:
-; NORMAL-NEXT: Opcode: I32_CONST
-; NORMAL-NEXT: Value: 28
-; NORMAL-NEXT: Content: '616263'
; NORMAL-NEXT: - Type: CUSTOM
; NORMAL-NEXT: Name: linking
; NORMAL-NEXT: Version: 2
@@ -209,31 +209,31 @@
; NORMAL-NEXT: Kind: DATA
; NORMAL-NEXT: Name: data_comdat
; NORMAL-NEXT: Flags: [ BINDING_WEAK ]
-; NORMAL-NEXT: Segment: 5
+; NORMAL-NEXT: Segment: 1
; NORMAL-NEXT: Size: 3
; NORMAL-NEXT: - Index: 8
; NORMAL-NEXT: Kind: DATA
; NORMAL-NEXT: Name: func_addr1
; NORMAL-NEXT: Flags: [ VISIBILITY_HIDDEN ]
-; NORMAL-NEXT: Segment: 1
+; NORMAL-NEXT: Segment: 2
; NORMAL-NEXT: Size: 4
; NORMAL-NEXT: - Index: 9
; NORMAL-NEXT: Kind: DATA
; NORMAL-NEXT: Name: func_addr2
; NORMAL-NEXT: Flags: [ VISIBILITY_HIDDEN ]
-; NORMAL-NEXT: Segment: 2
+; NORMAL-NEXT: Segment: 3
; NORMAL-NEXT: Size: 4
; NORMAL-NEXT: - Index: 10
; NORMAL-NEXT: Kind: DATA
; NORMAL-NEXT: Name: func_addr3
; NORMAL-NEXT: Flags: [ VISIBILITY_HIDDEN ]
-; NORMAL-NEXT: Segment: 3
+; NORMAL-NEXT: Segment: 4
; NORMAL-NEXT: Size: 4
; NORMAL-NEXT: - Index: 11
; NORMAL-NEXT: Kind: DATA
; NORMAL-NEXT: Name: data_addr1
; NORMAL-NEXT: Flags: [ VISIBILITY_HIDDEN ]
-; NORMAL-NEXT: Segment: 4
+; NORMAL-NEXT: Segment: 5
; NORMAL-NEXT: Size: 4
; NORMAL-NEXT: - Index: 12
; NORMAL-NEXT: Kind: DATA
@@ -245,32 +245,32 @@
; NORMAL-NEXT: Alignment: 0
; NORMAL-NEXT: Flags: [ ]
; NORMAL-NEXT: - Index: 1
+; NORMAL-NEXT: Name: .rodata.data_comdat
+; NORMAL-NEXT: Alignment: 0
+; NORMAL-NEXT: Flags: [ ]
+; NORMAL-NEXT: - Index: 2
; NORMAL-NEXT: Name: .data.func_addr1
; NORMAL-NEXT: Alignment: 2
; NORMAL-NEXT: Flags: [ ]
-; NORMAL-NEXT: - Index: 2
+; NORMAL-NEXT: - Index: 3
; NORMAL-NEXT: Name: .data.func_addr2
; NORMAL-NEXT: Alignment: 2
; NORMAL-NEXT: Flags: [ ]
-; NORMAL-NEXT: - Index: 3
+; NORMAL-NEXT: - Index: 4
; NORMAL-NEXT: Name: .data.func_addr3
; NORMAL-NEXT: Alignment: 2
; NORMAL-NEXT: Flags: [ ]
-; NORMAL-NEXT: - Index: 4
+; NORMAL-NEXT: - Index: 5
; NORMAL-NEXT: Name: .data.data_addr1
; NORMAL-NEXT: Alignment: 3
; NORMAL-NEXT: Flags: [ ]
-; NORMAL-NEXT: - Index: 5
-; NORMAL-NEXT: Name: .rodata.data_comdat
-; NORMAL-NEXT: Alignment: 0
-; NORMAL-NEXT: Flags: [ ]
; NORMAL-NEXT: Comdats:
; NORMAL-NEXT: - Name: func_comdat
; NORMAL-NEXT: Entries:
; NORMAL-NEXT: - Kind: FUNCTION
; NORMAL-NEXT: Index: 5
; NORMAL-NEXT: - Kind: DATA
-; NORMAL-NEXT: Index: 5
+; NORMAL-NEXT: Index: 1
; NORMAL-NEXT: - Type: CUSTOM
; NORMAL-NEXT: Name: name
; NORMAL-NEXT: FunctionNames:
diff --git a/test/wasm/tls-align.ll b/test/wasm/tls-align.ll
index 62e7c49..8363c82 100644
--- a/test/wasm/tls-align.ll
+++ b/test/wasm/tls-align.ll
@@ -24,7 +24,7 @@
; CHECK-NEXT: Mutable: true
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
-; CHECK-NEXT: Value: 66608
+; CHECK-NEXT: Value: 66592
; __tls_base
; CHECK-NEXT: - Index: 1
diff --git a/test/wasm/tls.ll b/test/wasm/tls.ll
index 304c00f..126c223 100644
--- a/test/wasm/tls.ll
+++ b/test/wasm/tls.ll
@@ -66,7 +66,7 @@
; Skip __wasm_call_ctors and __wasm_init_memory
; CHECK: - Index: 2
; CHECK-NEXT: Locals: []
-; CHECK-NEXT: Body: 20002401200041004108FC0801000B
+; CHECK-NEXT: Body: 20002401200041004108FC0800000B
; Expected body of __wasm_init_tls:
; local.get 0
diff --git a/wasm/OutputSegment.h b/wasm/OutputSegment.h
index 14142fbd..663a9b8 100644
--- a/wasm/OutputSegment.h
+++ b/wasm/OutputSegment.h
@@ -20,7 +20,7 @@
class OutputSegment {
public:
- OutputSegment(StringRef n, uint32_t index) : name(n), index(index) {}
+ OutputSegment(StringRef n) : name(n) {}
void addInputSegment(InputSegment *inSeg) {
alignment = std::max(alignment, inSeg->getAlignment());
@@ -32,7 +32,7 @@
}
StringRef name;
- const uint32_t index;
+ uint32_t index = 0;
uint32_t initFlags = 0;
uint32_t sectionOffset = 0;
uint32_t alignment = 0;
diff --git a/wasm/Writer.cpp b/wasm/Writer.cpp
index 58d585b..c4a87be 100644
--- a/wasm/Writer.cpp
+++ b/wasm/Writer.cpp
@@ -263,7 +263,6 @@
memoryPtr += 4;
}
- // TODO: Add .bss space here.
if (WasmSym::dataEnd)
WasmSym::dataEnd->setVirtualAddress(memoryPtr);
@@ -667,7 +666,7 @@
OutputSegment *&s = segmentMap[name];
if (s == nullptr) {
LLVM_DEBUG(dbgs() << "new segment: " << name << "\n");
- s = make<OutputSegment>(name, segments.size());
+ s = make<OutputSegment>(name);
if (config->sharedMemory || name == ".tdata")
s->initFlags = WASM_SEGMENT_IS_PASSIVE;
segments.push_back(s);
@@ -676,6 +675,24 @@
LLVM_DEBUG(dbgs() << "added data: " << name << ": " << s->size << "\n");
}
}
+
+ // Sort segments by type, placing .bss last
+ std::stable_sort(segments.begin(), segments.end(),
+ [](const OutputSegment *a, const OutputSegment *b) {
+ auto order = [](StringRef name) {
+ return StringSwitch<int>(name)
+ .StartsWith(".rodata", 0)
+ .StartsWith(".data", 1)
+ .StartsWith(".tdata", 2)
+ .StartsWith(".bss", 4)
+ .Default(3);
+ };
+ return order(a->name) < order(b->name);
+ });
+
+ for (size_t i = 0; i < segments.size(); ++i) {
+ segments[i]->index = i;
+ }
}
static void createFunction(DefinedFunction *func, StringRef bodyContent) {