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

Ethan O'Brien via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 29 09:15:41 PST 2024


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

>From 4ce4fe223091fe461384a36f476fae48e7d854d1 Mon Sep 17 00:00:00 2001
From: Ethan O'Brien <ethan.a.obrien at gmail.com>
Date: Wed, 21 Aug 2024 08:56:25 -0500
Subject: [PATCH] Implement wip WebAssembly function alignment

---
 lld/wasm/SyntheticSections.cpp | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

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");



More information about the llvm-commits mailing list