Skip to content

Conversation

@sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Dec 12, 2025

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)

@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2025

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-wasm

Author: Sam Clegg (sbc100)

Changes

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)


Full diff: https://github.com/llvm/llvm-project/pull/172102.diff

3 Files Affected:

  • (modified) lld/wasm/Config.h (+4)
  • (modified) lld/wasm/Driver.cpp (+3-1)
  • (modified) lld/wasm/Writer.cpp (+3-1)
diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h
index 9d903e0c799db..2c73dabb8c3a0 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -173,6 +173,10 @@ struct Ctx {
     // Symbol whose value is the alignment of the TLS block.
     GlobalSymbol *tlsAlign;
 
+    // __rodata_end
+    // Symbol marking the end of readonly data (similar to _etext under ELF)
+    DefinedData *rodataEnd;
+
     // __data_end
     // Symbol marking the end of the data and bss.
     DefinedData *dataEnd;
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 97e50783985a8..293ebe63fc5e0 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -988,8 +988,10 @@ static void createOptionalSymbols() {
 
   ctx.sym.dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
 
-  if (!ctx.arg.shared)
+  if (!ctx.arg.shared) {
     ctx.sym.dataEnd = symtab->addOptionalDataSymbol("__data_end");
+    ctx.sym.rodataEnd = symtab->addOptionalDataSymbol("__rodata_end");
+  }
 
   if (!ctx.isPic) {
     ctx.sym.stackLow = symtab->addOptionalDataSymbol("__stack_low");
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 9a5b56fc52e2f..9c8b484033834 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -400,8 +400,10 @@ void Writer::layoutMemory() {
         setGlobalPtr(tlsBase, memoryPtr);
       }
     }
-
     memoryPtr += seg->size;
+    // Might get set more than once if segment merging is not enabled.
+    if (ctx.sym.rodataEnd)
+      ctx.sym.rodataEnd->setVA(memoryPtr);
   }
 
   // Make space for the memory initialization flag

@github-actions
Copy link

github-actions bot commented Dec 12, 2025

🪟 Windows x64 Test Results

  • 3102 tests passed
  • 61 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 12, 2025

🐧 Linux x64 Test Results

  • 3747 tests passed
  • 69 tests skipped

✅ The build succeeded and all tests passed.

@sbc100 sbc100 force-pushed the __rodata_end branch 2 times, most recently from 9dbf57a to c5fa5e4 Compare December 12, 2025 23:50
@sbc100 sbc100 changed the title [lld][WebAssembly] Add new __rodata_end symbol [lld][WebAssembly] Add new __rodata_start/__rodata_end symbols Dec 12, 2025
@github-actions
Copy link

github-actions bot commented Dec 12, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

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)
DefinedData *rodataStart;
DefinedData *rodataEnd;

// __data_end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason why __data_start doesn't exist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just historical need I believe.


// __rodata_start/__rodata_end
// Symbols marking the start/end of readonly data
DefinedData *rodataStart;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of unfortunate that we have __stack_low, __heap_base, and __rodata_start. But I guess it's too late to fix the inconsistency.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. that is kind of sad/absurd I agree :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants