Skip to content

Commit 9dbf57a

Browse files
committed
[lld][WebAssembly] Add new __rodata_end symbol
This is similar to etext/_etext in the ELF linker. Its useful in emscripten to know where the RO data data ends and the data begins (even though the Wasm format itself has no concept of RO data). See emscripten-core/emscripten#25939 (reply in thread)
1 parent 6f1e3c3 commit 9dbf57a

File tree

6 files changed

+51
-37
lines changed

6 files changed

+51
-37
lines changed

lld/test/wasm/data-segments.ll

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
; ACTIVE-PIC-NEXT: Offset:
9494
; ACTIVE-PIC-NEXT: Opcode: GLOBAL_GET
9595
; ACTIVE-PIC-NEXT: Index: 1
96-
; ACTIVE-PIC-NEXT: Content: 63000000636F6E7374616E74000000002B00000068656C6C6F00676F6F646279650000002A000000
96+
; ACTIVE-PIC-NEXT: Content: 636F6E7374616E74000000002B0000006300000068656C6C6F00676F6F646279650000002A000000
9797

9898
; PASSIVE-LABEL: - Type: START
9999
; PASSIVE-NEXT: StartFunction: 2
@@ -114,10 +114,10 @@
114114
; PASSIVE-NEXT: Segments:
115115
; PASSIVE-NEXT: - SectionOffset: 3
116116
; PASSIVE-NEXT: InitFlags: 1
117-
; PASSIVE-NEXT: Content: '63000000'
118-
; PASSIVE-NEXT: - SectionOffset: 9
119-
; PASSIVE-NEXT: InitFlags: 1
120117
; PASSIVE-NEXT: Content: 636F6E7374616E74000000002B
118+
; PASSIVE-NEXT: - SectionOffset: 18
119+
; PASSIVE-NEXT: InitFlags: 1
120+
; PASSIVE-NEXT: Content: '63000000'
121121
; PASSIVE-NEXT: - SectionOffset: 24
122122
; PASSIVE-NEXT: InitFlags: 1
123123
; PASSIVE-NEXT: Content: 68656C6C6F00676F6F646279650000002A000000
@@ -153,10 +153,10 @@
153153
; PASSIVE-PIC-NEXT: Segments:
154154
; PASSIVE-PIC-NEXT: - SectionOffset: 3
155155
; PASSIVE-PIC-NEXT: InitFlags: 1
156-
; PASSIVE-PIC-NEXT: Content: '63000000'
157-
; PASSIVE-PIC-NEXT: - SectionOffset: 9
158-
; PASSIVE-PIC-NEXT: InitFlags: 1
159156
; PASSIVE-PIC-NEXT: Content: 636F6E7374616E74000000002B
157+
; PASSIVE-PIC-NEXT: - SectionOffset: 18
158+
; PASSIVE-PIC-NEXT: InitFlags: 1
159+
; PASSIVE-PIC-NEXT: Content: '63000000'
160160
; PASSIVE-PIC-NEXT: - SectionOffset: 24
161161
; PASSIVE-PIC-NEXT: InitFlags: 1
162162
; PASSIVE-PIC-NEXT: Content: 68656C6C6F00676F6F646279650000002A000000
@@ -212,25 +212,25 @@
212212
; DIS-NEXT: end
213213

214214
; NOPIC-DIS-NEXT: [[PTR]].const 65536
215-
; NOPIC-DIS-NEXT: [[PTR]].const 65536
216-
; NOPIC-DIS-NEXT: global.set 1
217215
; PIC-DIS-NEXT: [[PTR]].const 0
218216
; PIC-DIS-NEXT: global.get 1
219217
; PIC-DIS-NEXT: [[PTR]].add
220-
; PIC-DIS-NEXT: local.tee 1
221-
; PIC-DIS-NEXT: global.set {{\d*}}
222-
; PIC-DIS-NEXT: local.get 1
223218
; DIS-NEXT: i32.const 0
224-
; DIS-NEXT: i32.const 4
219+
; DIS-NEXT: i32.const 13
225220
; DIS-NEXT: memory.init 0, 0
226221

227-
; NOPIC-DIS-NEXT: [[PTR]].const 65540
228-
; PIC-DIS-NEXT: [[PTR]].const 4
222+
; NOPIC-DIS-NEXT: [[PTR]].const 65552
223+
; NOPIC-DIS-NEXT: [[PTR]].const 65552
224+
; NOPIC-DIS-NEXT: global.set 1
225+
; PIC-DIS-NEXT: [[PTR]].const 16
229226
; PIC-DIS-NEXT: global.get 1
230227
; PIC-DIS-NEXT: [[PTR]].add
228+
; PIC-DIS-NEXT: local.tee 1
229+
; PIC-DIS-NEXT: global.set {{\d*}}
230+
; PIC-DIS-NEXT: local.get 1
231231

232232
; DIS-NEXT: i32.const 0
233-
; DIS-NEXT: i32.const 13
233+
; DIS-NEXT: i32.const 4
234234
; DIS-NEXT: memory.init 1, 0
235235

236236
; NOPIC-DIS-NEXT: [[PTR]].const 65556
@@ -272,6 +272,6 @@
272272
; DIS-NEXT: memory.atomic.wait32 0
273273
; DIS-NEXT: drop
274274
; DIS-NEXT: end
275-
; DIS-NEXT: data.drop 1
275+
; DIS-NEXT: data.drop 0
276276
; DIS-NEXT: data.drop 2
277277
; DIS-NEXT: end

lld/test/wasm/export-all.s

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,27 @@ foo:
3434
# CHECK-NEXT: - Name: __data_end
3535
# CHECK-NEXT: Kind: GLOBAL
3636
# CHECK-NEXT: Index: 2
37-
# CHECK-NEXT: - Name: __stack_low
37+
# CHECK-NEXT: - Name: __rodata_end
3838
# CHECK-NEXT: Kind: GLOBAL
3939
# CHECK-NEXT: Index: 3
40-
# CHECK-NEXT: - Name: __stack_high
40+
# CHECK-NEXT: - Name: __stack_low
4141
# CHECK-NEXT: Kind: GLOBAL
4242
# CHECK-NEXT: Index: 4
43-
# CHECK-NEXT: - Name: __global_base
43+
# CHECK-NEXT: - Name: __stack_high
4444
# CHECK-NEXT: Kind: GLOBAL
4545
# CHECK-NEXT: Index: 5
46-
# CHECK-NEXT: - Name: __heap_base
46+
# CHECK-NEXT: - Name: __global_base
4747
# CHECK-NEXT: Kind: GLOBAL
4848
# CHECK-NEXT: Index: 6
49-
# CHECK-NEXT: - Name: __heap_end
49+
# CHECK-NEXT: - Name: __heap_base
5050
# CHECK-NEXT: Kind: GLOBAL
5151
# CHECK-NEXT: Index: 7
52-
# CHECK-NEXT: - Name: __memory_base
52+
# CHECK-NEXT: - Name: __heap_end
5353
# CHECK-NEXT: Kind: GLOBAL
5454
# CHECK-NEXT: Index: 8
55-
# CHECK-NEXT: - Name: __table_base
55+
# CHECK-NEXT: - Name: __memory_base
5656
# CHECK-NEXT: Kind: GLOBAL
5757
# CHECK-NEXT: Index: 9
58+
# CHECK-NEXT: - Name: __table_base
59+
# CHECK-NEXT: Kind: GLOBAL
60+
# CHECK-NEXT: Index: 10

lld/test/wasm/mutable-global-exports.s

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,33 @@ _start:
9191
# CHECK-ALL-NEXT: - Name: __data_end
9292
# CHECK-ALL-NEXT: Kind: GLOBAL
9393
# CHECK-ALL-NEXT: Index: 4
94-
# CHECK-ALL-NEXT: - Name: __stack_low
94+
# CHECK-ALL-NEXT: - Name: __rodata_end
9595
# CHECK-ALL-NEXT: Kind: GLOBAL
9696
# CHECK-ALL-NEXT: Index: 5
97-
# CHECK-ALL-NEXT: - Name: __stack_high
97+
# CHECK-ALL-NEXT: - Name: __stack_low
9898
# CHECK-ALL-NEXT: Kind: GLOBAL
9999
# CHECK-ALL-NEXT: Index: 6
100-
# CHECK-ALL-NEXT: - Name: __global_base
100+
# CHECK-ALL-NEXT: - Name: __stack_high
101101
# CHECK-ALL-NEXT: Kind: GLOBAL
102102
# CHECK-ALL-NEXT: Index: 7
103-
# CHECK-ALL-NEXT: - Name: __heap_base
103+
# CHECK-ALL-NEXT: - Name: __global_base
104104
# CHECK-ALL-NEXT: Kind: GLOBAL
105105
# CHECK-ALL-NEXT: Index: 8
106-
# CHECK-ALL-NEXT: - Name: __heap_end
106+
# CHECK-ALL-NEXT: - Name: __heap_base
107107
# CHECK-ALL-NEXT: Kind: GLOBAL
108108
# CHECK-ALL-NEXT: Index: 9
109-
# CHECK-ALL-NEXT: - Name: __memory_base
109+
# CHECK-ALL-NEXT: - Name: __heap_end
110110
# CHECK-ALL-NEXT: Kind: GLOBAL
111111
# CHECK-ALL-NEXT: Index: 10
112-
# CHECK-ALL-NEXT: - Name: __table_base
112+
# CHECK-ALL-NEXT: - Name: __memory_base
113113
# CHECK-ALL-NEXT: Kind: GLOBAL
114114
# CHECK-ALL-NEXT: Index: 11
115-
# CHECK-ALL-NEXT: - Name: __wasm_first_page_end
115+
# CHECK-ALL-NEXT: - Name: __table_base
116116
# CHECK-ALL-NEXT: Kind: GLOBAL
117117
# CHECK-ALL-NEXT: Index: 12
118+
# CHECK-ALL-NEXT: - Name: __wasm_first_page_end
119+
# CHECK-ALL-NEXT: Kind: GLOBAL
120+
# CHECK-ALL-NEXT: Index: 13
118121
# CHECK-ALL-NEXT: - Type: CODE
119122

120123
# CHECK-ALL: Name: target_features

lld/wasm/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ struct Ctx {
173173
// Symbol whose value is the alignment of the TLS block.
174174
GlobalSymbol *tlsAlign;
175175

176+
// __rodata_end
177+
// Symbol marking the end of readonly data (similar to _etext under ELF)
178+
DefinedData *rodataEnd;
179+
176180
// __data_end
177181
// Symbol marking the end of the data and bss.
178182
DefinedData *dataEnd;

lld/wasm/Driver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,10 @@ static void createOptionalSymbols() {
988988

989989
ctx.sym.dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
990990

991-
if (!ctx.arg.shared)
991+
if (!ctx.arg.shared) {
992992
ctx.sym.dataEnd = symtab->addOptionalDataSymbol("__data_end");
993+
ctx.sym.rodataEnd = symtab->addOptionalDataSymbol("__rodata_end");
994+
}
993995

994996
if (!ctx.isPic) {
995997
ctx.sym.stackLow = symtab->addOptionalDataSymbol("__stack_low");

lld/wasm/Writer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,10 @@ void Writer::layoutMemory() {
400400
setGlobalPtr(tlsBase, memoryPtr);
401401
}
402402
}
403-
404403
memoryPtr += seg->size;
404+
// Might get set more than once if segment merging is not enabled.
405+
if (ctx.sym.rodataEnd && seg->name.starts_with(".rodata"))
406+
ctx.sym.rodataEnd->setVA(memoryPtr);
405407
}
406408

407409
// Make space for the memory initialization flag
@@ -1050,13 +1052,13 @@ void Writer::createOutputSegments() {
10501052
}
10511053
}
10521054

1053-
// Sort segments by type, placing .bss last
1055+
// Sort segments by type, placing `.bss` last and `.rodata` first.
10541056
llvm::stable_sort(segments,
10551057
[](const OutputSegment *a, const OutputSegment *b) {
10561058
auto order = [](StringRef name) {
10571059
return StringSwitch<int>(name)
1058-
.StartsWith(".tdata", 0)
1059-
.StartsWith(".rodata", 1)
1060+
.StartsWith(".rodata", 0)
1061+
.StartsWith(".tdata", 1)
10601062
.StartsWith(".data", 2)
10611063
.StartsWith(".bss", 4)
10621064
.Default(3);

0 commit comments

Comments
 (0)