[lld] [lld][MachO][NFC] Prepare ObjC stubs for variable sizes (PR #219742)

Kyungwoo Lee via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 1 15:49:09 PDT 2026


https://github.com/kyulee-com updated https://github.com/llvm/llvm-project/pull/219742

>From 751c07cf04eb6e3f055b6aec6ae2dfc8f7c2229b Mon Sep 17 00:00:00 2001
From: Kyungwoo Lee <kyulee at meta.com>
Date: Sat, 29 Aug 2026 15:38:56 -0700
Subject: [PATCH] [lld][MachO] Prepare ObjC stubs for variable sizes

Track ObjC stub sizes explicitly so future changes can mix different stub layouts in one synthetic section.
---
 lld/MachO/SyntheticSections.cpp | 20 ++++++++++----------
 lld/MachO/SyntheticSections.h   |  4 +++-
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index c4277d54edd38..bd39f49ba09e6 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -889,14 +889,13 @@ ObjCStubsSection::ObjCStubsSection()
 }
 
 bool ObjCStubsSection::isObjCStubSymbol(Symbol *sym) {
-  return sym->getName().starts_with(symbolPrefix);
+  return sym->getName().starts_with(objcMsgSendStubPrefix);
 }
 
 StringRef ObjCStubsSection::getMethname(Symbol *sym) {
   assert(isObjCStubSymbol(sym) && "not an objc stub");
   auto name = sym->getName();
-  StringRef methname = name.drop_front(symbolPrefix.size());
-  return methname;
+  return name.drop_front(objcMsgSendStubPrefix.size());
 }
 
 size_t ObjCStubsSection::getStubSize() const {
@@ -914,12 +913,13 @@ void ObjCStubsSection::addEntry(Symbol *sym) {
   size_t stubSize = getStubSize();
   Defined *newSym = replaceSymbol<Defined>(
       sym, sym->getName(), nullptr, isec,
-      /*value=*/symbols.size() * stubSize,
+      /*value=*/stubsSize,
       /*size=*/stubSize,
       /*isWeakDef=*/false, /*isExternal=*/true, /*isPrivateExtern=*/true,
       /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
       /*noDeadStrip=*/false);
   symbols.push_back(newSym);
+  stubsSize += stubSize;
 }
 
 void ObjCStubsSection::setUp() {
@@ -943,9 +943,7 @@ void ObjCStubsSection::setUp() {
   }
 }
 
-uint64_t ObjCStubsSection::getSize() const {
-  return getStubSize() * symbols.size();
-}
+uint64_t ObjCStubsSection::getSize() const { return stubsSize; }
 
 void ObjCStubsSection::sortSymbols(
     const llvm::DenseMap<const Symbol *, int> &priorities) {
@@ -957,9 +955,11 @@ void ObjCStubsSection::sortSymbols(
     };
     return priority(a) < priority(b);
   });
-  size_t stubSize = getStubSize();
-  for (auto [idx, sym] : llvm::enumerate(symbols))
-    sym->value = idx * stubSize;
+  size_t stubOffset = 0;
+  for (Defined *sym : symbols) {
+    sym->value = stubOffset;
+    stubOffset += getStubSize();
+  }
 }
 
 void ObjCStubsSection::writeTo(uint8_t *buf) const {
diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index 9acbd73d277e8..6642ed64112cf 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -342,7 +342,7 @@ class ObjCStubsSection final : public SyntheticSection {
   void writeTo(uint8_t *buf) const override;
   void setUp();
 
-  static constexpr llvm::StringLiteral symbolPrefix = "_objc_msgSend$";
+  static constexpr llvm::StringLiteral objcMsgSendStubPrefix = "_objc_msgSend$";
   static bool isObjCStubSymbol(Symbol *sym);
   static StringRef getMethname(Symbol *sym);
 
@@ -354,6 +354,8 @@ class ObjCStubsSection final : public SyntheticSection {
   size_t getStubSize() const;
 
   std::vector<Defined *> symbols;
+  // Total byte size of all stubs added so far.
+  size_t stubsSize = 0;
   Symbol *objcMsgSend = nullptr;
 };
 



More information about the llvm-commits mailing list