[lld] [WIP] wasm-ld: Implement function pointer alignment (PR #105532)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 07:08:45 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-wasm

Author: Ethan O'Brien (ethanaobrien)

<details>
<summary>Changes</summary>

WIP implementation of function pointer alignment. Currently all values are statically declared.

`padding`: Waits `x` values before beginning the first function pointer. This can be useful for users needing to align on even numbers, or on odd numbers. This should possibly be equal to [`tableBase`](https://github.com/llvm/llvm-project/blob/7ad7f8f7a3d443f4c17264d7e14cccdc020976b9/lld/wasm/Driver.cpp#L635) by default?
`alignment`: Inserts `x-1` `nullptr` values into the function table.

Previous discussion: https://github.com/emscripten-core/emscripten/discussions/22369

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


1 Files Affected:

- (modified) lld/wasm/SyntheticSections.cpp (+22) 


``````````diff
diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp
index f02f55519a2512..13333a67378681 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -563,8 +563,24 @@ void ElemSection::addEntry(FunctionSymbol *sym) {
   // They only exist so that the calls to missing functions can validate.
   if (sym->hasTableIndex() || sym->isStub)
     return;
+
+  uint32_t padding = 0;
+  uint64_t alignment = 1;
+
+  if (indirectFunctions.size() == 0 && padding > 0) {
+    for (uint32_t i=0; i<padding; i++) {
+      indirectFunctions.push_back(nullptr);
+    }
+  }
+
   sym->setTableIndex(config->tableBase + indirectFunctions.size());
   indirectFunctions.emplace_back(sym);
+
+  if (alignment > 1) {
+    for (uint32_t i=0; i<alignment-1; i++) {
+      indirectFunctions.push_back(nullptr);
+    }
+  }
 }
 
 void ElemSection::writeBody() {
@@ -602,6 +618,12 @@ void ElemSection::writeBody() {
   writeUleb128(os, indirectFunctions.size(), "elem count");
   uint32_t tableIndex = config->tableBase;
   for (const FunctionSymbol *sym : indirectFunctions) {
+    if (sym == nullptr) {
+      (void) tableIndex;
+      writeUleb128(os, 0, "function index");
+      ++tableIndex;
+      continue;
+    }
     assert(sym->getTableIndex() == tableIndex);
     (void) tableIndex;
     writeUleb128(os, sym->getFunctionIndex(), "function index");

``````````

</details>


https://github.com/llvm/llvm-project/pull/105532


More information about the llvm-commits mailing list