Skip to content

Commit c5fa5e4

Browse files
committed
[lld][WebAssembly] Add new __rodata_start/__rodata_end symbols
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 c5fa5e4

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

lld/test/wasm/export-all.s

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,30 @@ 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_start
3838
# CHECK-NEXT: Kind: GLOBAL
3939
# CHECK-NEXT: Index: 3
40-
# CHECK-NEXT: - Name: __stack_high
40+
# CHECK-NEXT: - Name: __rodata_end
4141
# CHECK-NEXT: Kind: GLOBAL
4242
# CHECK-NEXT: Index: 4
43-
# CHECK-NEXT: - Name: __global_base
43+
# CHECK-NEXT: - Name: __stack_low
4444
# CHECK-NEXT: Kind: GLOBAL
4545
# CHECK-NEXT: Index: 5
46-
# CHECK-NEXT: - Name: __heap_base
46+
# CHECK-NEXT: - Name: __stack_high
4747
# CHECK-NEXT: Kind: GLOBAL
4848
# CHECK-NEXT: Index: 6
49-
# CHECK-NEXT: - Name: __heap_end
49+
# CHECK-NEXT: - Name: __global_base
5050
# CHECK-NEXT: Kind: GLOBAL
5151
# CHECK-NEXT: Index: 7
52-
# CHECK-NEXT: - Name: __memory_base
52+
# CHECK-NEXT: - Name: __heap_base
5353
# CHECK-NEXT: Kind: GLOBAL
5454
# CHECK-NEXT: Index: 8
55-
# CHECK-NEXT: - Name: __table_base
55+
# CHECK-NEXT: - Name: __heap_end
5656
# CHECK-NEXT: Kind: GLOBAL
5757
# CHECK-NEXT: Index: 9
58+
# CHECK-NEXT: - Name: __memory_base
59+
# CHECK-NEXT: Kind: GLOBAL
60+
# CHECK-NEXT: Index: 10
61+
# CHECK-NEXT: - Name: __table_base
62+
# CHECK-NEXT: Kind: GLOBAL
63+
# CHECK-NEXT: Index: 11

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,36 @@ _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_start
9595
# CHECK-ALL-NEXT: Kind: GLOBAL
9696
# CHECK-ALL-NEXT: Index: 5
97-
# CHECK-ALL-NEXT: - Name: __stack_high
97+
# CHECK-ALL-NEXT: - Name: __rodata_end
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_low
101101
# CHECK-ALL-NEXT: Kind: GLOBAL
102102
# CHECK-ALL-NEXT: Index: 7
103-
# CHECK-ALL-NEXT: - Name: __heap_base
103+
# CHECK-ALL-NEXT: - Name: __stack_high
104104
# CHECK-ALL-NEXT: Kind: GLOBAL
105105
# CHECK-ALL-NEXT: Index: 8
106-
# CHECK-ALL-NEXT: - Name: __heap_end
106+
# CHECK-ALL-NEXT: - Name: __global_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_base
110110
# CHECK-ALL-NEXT: Kind: GLOBAL
111111
# CHECK-ALL-NEXT: Index: 10
112-
# CHECK-ALL-NEXT: - Name: __table_base
112+
# CHECK-ALL-NEXT: - Name: __heap_end
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: __memory_base
116116
# CHECK-ALL-NEXT: Kind: GLOBAL
117117
# CHECK-ALL-NEXT: Index: 12
118+
# CHECK-ALL-NEXT: - Name: __table_base
119+
# CHECK-ALL-NEXT: Kind: GLOBAL
120+
# CHECK-ALL-NEXT: Index: 13
121+
# CHECK-ALL-NEXT: - Name: __wasm_first_page_end
122+
# CHECK-ALL-NEXT: Kind: GLOBAL
123+
# CHECK-ALL-NEXT: Index: 14
118124
# CHECK-ALL-NEXT: - Type: CODE
119125

120126
# CHECK-ALL: Name: target_features

lld/wasm/Config.h

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

176+
// __rodata_start/__rodata_end
177+
// Symbols marking the start/end of readonly data
178+
DefinedData *rodataStart;
179+
DefinedData *rodataEnd;
180+
176181
// __data_end
177182
// Symbol marking the end of the data and bss.
178183
DefinedData *dataEnd;

lld/wasm/Driver.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,11 @@ 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.rodataStart = symtab->addOptionalDataSymbol("__rodata_start");
994+
ctx.sym.rodataEnd = symtab->addOptionalDataSymbol("__rodata_end");
995+
}
993996

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

lld/wasm/Writer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,14 @@ void Writer::layoutMemory() {
401401
}
402402
}
403403

404+
if (ctx.sym.rodataStart && seg->name.starts_with(".rodata") && !ctx.sym.rodataStart->getVA())
405+
ctx.sym.rodataStart->setVA(memoryPtr);
406+
404407
memoryPtr += seg->size;
408+
409+
// Might get set more than once if segment merging is not enabled.
410+
if (ctx.sym.rodataEnd && seg->name.starts_with(".rodata"))
411+
ctx.sym.rodataEnd->setVA(memoryPtr);
405412
}
406413

407414
// Make space for the memory initialization flag

0 commit comments

Comments
 (0)