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

Kyungwoo Lee via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 02:55:27 PDT 2026


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

>From d050c9a11e1a671dc10e9b61a871e9ccaac1c73f Mon Sep 17 00:00:00 2001
From: Kyungwoo Lee <kyulee at meta.com>
Date: Tue, 25 Aug 2026 01:18:59 -0700
Subject: [PATCH 1/4] [lld][MachO] Order objc stubs by caller priority

__objc_stubs is synthetic, so the input section sorting never reaches
its entries: they stay in symbol interning order even with -order_file.
A stub is faulted in when its caller runs, so stubs called from hot code
end up scattered across pages that startup otherwise never touches.

Record the sections that branch to each stub, give each stub the lowest
priority among its callers, and stable-sort the stubs before addresses
are assigned. Stubs with no prioritized caller keep their relative order
at the end.

General __stubs ordering is left alone because stubsIndex also drives
lazy pointers, indirect symbols, and bind/rebase offsets.

This reduces page faults in __objc_stubs by 40% for a large app.
---
 lld/MachO/SyntheticSections.cpp               | 10 +++
 lld/MachO/SyntheticSections.h                 |  5 ++
 lld/MachO/Writer.cpp                          | 50 +++++++++++
 lld/docs/ReleaseNotes.md                      |  4 +
 .../objc-stubs-order-file-icf-safe-thunks.s   | 63 +++++++++++++
 lld/test/MachO/objc-stubs-order-file.s        | 90 +++++++++++++++++++
 6 files changed, 222 insertions(+)
 create mode 100644 lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s
 create mode 100644 lld/test/MachO/objc-stubs-order-file.s

diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index ba06a95bb753c..d3f5ca1409944 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -944,6 +944,16 @@ uint64_t ObjCStubsSection::getSize() const {
   return stubSize * symbols.size();
 }
 
+void ObjCStubsSection::sortSymbols(
+    llvm::function_ref<bool(const Defined *, const Defined *)> less) {
+  auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
+                      ? target->objcStubsFastSize
+                      : target->objcStubsSmallSize;
+  llvm::stable_sort(symbols, less);
+  for (auto [idx, sym] : llvm::enumerate(symbols))
+    sym->value = idx * stubSize;
+}
+
 void ObjCStubsSection::writeTo(uint8_t *buf) const {
   uint64_t stubOffset = 0;
   for (Defined *sym : symbols) {
diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index e649d1275f821..95f039b893dd6 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -346,6 +346,11 @@ class ObjCStubsSection final : public SyntheticSection {
   static bool isObjCStubSymbol(Symbol *sym);
   static StringRef getMethname(Symbol *sym);
 
+  /// Stably sort the stubs by \p less and reassign their offsets. Must run
+  /// before addresses are assigned.
+  void
+  sortSymbols(llvm::function_ref<bool(const Defined *, const Defined *)> less);
+
 private:
   std::vector<Defined *> symbols;
   Symbol *objcMsgSend = nullptr;
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 89b6d467d0d44..1480a86e38015 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -33,6 +33,7 @@
 #include "llvm/Support/xxhash.h"
 
 #include <algorithm>
+#include <limits>
 
 using namespace llvm;
 using namespace llvm::MachO;
@@ -971,6 +972,53 @@ template <class LP> void Writer::createLoadCommands() {
                               : 0));
 }
 
+// __objc_stubs is synthetic, so the input section sorting in
+// sortSegmentsAndSections() does not reach its entries. Order each stub by the
+// priority of the earliest-laid-out section that calls it, which keeps the
+// stubs reached during startup together.
+static void orderObjCStubsByCallerPriority(
+    const DenseMap<const InputSection *, int> &priorities) {
+  if (priorities.empty() || !in.objcStubs->isNeeded())
+    return;
+
+  // ICF may make the prioritized section differ from the section whose
+  // relocations describe the original calls. Use the canonical section for
+  // priority lookup, but scan the original section's relocations.
+  DenseMap<const Symbol *, int> stubPriority;
+  for (const ConcatInputSection *isec : inputSections) {
+    if (!isCodeSection(isec))
+      continue;
+    const auto *priorityIsec = cast<ConcatInputSection>(isec->canonical());
+    if (priorityIsec->shouldOmitFromOutput())
+      continue;
+    auto prio = priorities.find(priorityIsec);
+    if (prio == priorities.end())
+      continue;
+    for (const Relocation &r : isec->relocs) {
+      if (!target->hasAttr(r.type, RelocAttrBits::BRANCH))
+        continue;
+      auto *stub = dyn_cast_if_present<Symbol *>(r.referent);
+      if (!stub || !ObjCStubsSection::isObjCStubSymbol(stub))
+        continue;
+      auto [it, inserted] = stubPriority.try_emplace(stub, prio->second);
+      if (!inserted)
+        it->second = std::min(it->second, prio->second);
+    }
+  }
+  if (stubPriority.empty())
+    return;
+
+  auto priorityOf = [&](const Defined *sym) {
+    auto it = stubPriority.find(sym);
+    // Stubs with no prioritized caller sort last, keeping their original order.
+    return it == stubPriority.end() ? std::numeric_limits<int>::max()
+                                    : it->second;
+  };
+  in.objcStubs->sortSymbols([&](const Defined *a, const Defined *b) {
+    return priorityOf(a) < priorityOf(b);
+  });
+}
+
 // Sorting only can happen once all outputs have been collected. Here we sort
 // segments, output sections within each segment, and input sections within each
 // output segment.
@@ -981,6 +1029,8 @@ static void sortSegmentsAndSections() {
   DenseMap<const InputSection *, int> isecPriorities =
       priorityBuilder.buildInputSectionPriorities();
 
+  orderObjCStubsByCallerPriority(isecPriorities);
+
   uint32_t sectionIndex = 0;
   for (OutputSegment *seg : outputSegments) {
     seg->sortOutputSections();
diff --git a/lld/docs/ReleaseNotes.md b/lld/docs/ReleaseNotes.md
index f3cae4a0d97ce..00c9f5c85af8f 100644
--- a/lld/docs/ReleaseNotes.md
+++ b/lld/docs/ReleaseNotes.md
@@ -37,6 +37,10 @@ from the [LLVM releases web site](https://llvm.org/releases/).
 
 ### MachO Improvements
 
+* `__objc_stubs` entries are now ordered by the priority of the sections that
+  call them, so that stubs reached from prioritized code are laid out together.
+  This applies whenever section priorities exist, such as with `-order_file`.
+
 ### WebAssembly Improvements
 
 * Added support for resolving and merging common data symbols (allocating them
diff --git a/lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s b/lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s
new file mode 100644
index 0000000000000..75f721764eee1
--- /dev/null
+++ b/lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s
@@ -0,0 +1,63 @@
+# REQUIRES: aarch64
+
+## An order-file symbol folded to a safe thunk should still order the ObjC stub
+## called by the original folded body.
+
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/input.s -o %t/input.o
+# RUN: %lld -arch arm64 -e _entry -U _objc_msgSend -o %t/out %t/input.o \
+# RUN:   -objc_stubs_small --icf=safe_thunks -order_file %t/order
+# RUN: llvm-objdump --no-show-raw-insn -d %t/out | \
+# RUN:   FileCheck %s --check-prefix=THUNK
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
+# RUN:   %t/out | FileCheck %s --check-prefix=ORDERED
+
+# THUNK-LABEL: <_dup>:
+# THUNK-NEXT:  b 0x{{[0-9a-f]+}} <_hot>
+
+# ORDERED:      Contents of (__TEXT,__objc_stubs) section
+# ORDERED-NEXT: _objc_msgSend$hot:
+# ORDERED:      Objc selector ref: hot
+# ORDERED:      _objc_msgSend$cold:
+# ORDERED:      Objc selector ref: cold
+# ORDERED:      _objc_msgSend$mild:
+# ORDERED:      Objc selector ref: mild
+
+#--- input.s
+.text
+.globl _cold
+_cold:
+  bl _objc_msgSend$cold
+  ret
+
+.globl _mild
+_mild:
+  bl _objc_msgSend$mild
+  ret
+
+.globl _hot
+_hot:
+  bl _objc_msgSend$hot
+  ret
+
+.globl _dup
+_dup:
+  bl _objc_msgSend$hot
+  ret
+
+.globl _entry
+_entry:
+  bl _cold
+  bl _mild
+  bl _hot
+  bl _dup
+  ret
+
+.addrsig
+.addrsig_sym _hot
+.addrsig_sym _dup
+
+.subsections_via_symbols
+
+#--- order
+_dup
diff --git a/lld/test/MachO/objc-stubs-order-file.s b/lld/test/MachO/objc-stubs-order-file.s
new file mode 100644
index 0000000000000..0bfe32f1b034c
--- /dev/null
+++ b/lld/test/MachO/objc-stubs-order-file.s
@@ -0,0 +1,90 @@
+# REQUIRES: aarch64
+
+## __objc_stubs is synthetic, so the input section sorting does not reach its
+## entries. Check that they are instead ordered by the priority of the sections
+## that call them.
+
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/a.s -o %t/a.o
+
+## Without an order file the stubs keep the order they were interned in.
+# RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/base.out %t/a.o \
+# RUN:   -objc_stubs_small
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
+# RUN:   %t/base.out | FileCheck %s --check-prefix=BASE
+
+## _hot is ordered first, so the stub it calls moves ahead of the others, which
+## keep their relative order. Each stub must still load its own selector.
+# RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/ordered.out %t/a.o \
+# RUN:   -objc_stubs_small -order_file %t/order
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
+# RUN:   %t/ordered.out | FileCheck %s --check-prefix=ORDERED
+
+# RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/fast.out %t/a.o \
+# RUN:   -objc_stubs_fast -order_file %t/order
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
+# RUN:   %t/fast.out | FileCheck %s --check-prefix=ORDERED
+
+## Callers are recorded after ICF. _dup is identical to _hot and folds with it,
+## so the priority has to reach the stub through the surviving section.
+# RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/icf.out %t/a.o \
+# RUN:   -objc_stubs_small -order_file %t/order --icf=all
+# RUN: llvm-nm --numeric-sort %t/icf.out | FileCheck %s --check-prefix=FOLDED
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
+# RUN:   %t/icf.out | FileCheck %s --check-prefix=ORDERED
+
+## Check that _dup folded into _hot.
+# FOLDED:      [[ADDR:[0-9a-f]+]] T _dup
+# FOLDED-NEXT: [[ADDR]] T _hot
+
+# BASE:      Contents of (__TEXT,__objc_stubs) section
+# BASE-NEXT: _objc_msgSend$cold:
+# BASE:      _objc_msgSend$hot:
+# BASE:      _objc_msgSend$mild:
+
+# ORDERED:      Contents of (__TEXT,__objc_stubs) section
+# ORDERED-NEXT: _objc_msgSend$hot:
+# ORDERED:      Objc selector ref: hot
+# ORDERED:      _objc_msgSend$cold:
+# ORDERED:      Objc selector ref: cold
+# ORDERED:      _objc_msgSend$mild:
+# ORDERED:      Objc selector ref: mild
+
+#--- a.s
+.text
+.globl _cold
+.p2align 2
+_cold:
+  bl _objc_msgSend$cold
+  ret
+
+.globl _mild
+.p2align 2
+_mild:
+  bl _objc_msgSend$mild
+  ret
+
+.globl _hot
+.p2align 2
+_hot:
+  bl _objc_msgSend$hot
+  ret
+
+.globl _dup
+.p2align 2
+_dup:
+  bl _objc_msgSend$hot
+  ret
+
+.globl _main
+.p2align 2
+_main:
+  bl _cold
+  bl _mild
+  bl _hot
+  ret
+
+.subsections_via_symbols
+
+#--- order
+_hot

>From 59d1863d58a11b8b384ca105572bcebd03725503 Mon Sep 17 00:00:00 2001
From: Kyungwoo Lee <kyulee at meta.com>
Date: Wed, 26 Aug 2026 12:37:50 -0700
Subject: [PATCH 2/4] Addresd Ellis' comments

---
 lld/MachO/SyntheticSections.cpp                     | 13 +++++++++++--
 lld/MachO/SyntheticSections.h                       |  7 +++----
 lld/MachO/Writer.cpp                                | 12 +-----------
 .../MachO/objc-stubs-order-file-icf-safe-thunks.s   |  3 ---
 lld/test/MachO/objc-stubs-order-file.s              |  5 +----
 5 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index d3f5ca1409944..0633ce4f5820a 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -26,6 +26,8 @@
 #include "llvm/Support/Parallel.h"
 #include "llvm/Support/xxhash.h"
 
+#include <limits>
+
 #if defined(__APPLE__)
 #include <sys/mman.h>
 
@@ -945,11 +947,18 @@ uint64_t ObjCStubsSection::getSize() const {
 }
 
 void ObjCStubsSection::sortSymbols(
-    llvm::function_ref<bool(const Defined *, const Defined *)> less) {
+    const llvm::DenseMap<const Symbol *, int> &priorities) {
   auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
                       ? target->objcStubsFastSize
                       : target->objcStubsSmallSize;
-  llvm::stable_sort(symbols, less);
+  llvm::stable_sort(symbols, [&](const Defined *a, const Defined *b) {
+    auto priority = [&](const Defined *sym) {
+      auto it = priorities.find(sym);
+      return it == priorities.end() ? std::numeric_limits<int>::max()
+                                    : it->second;
+    };
+    return priority(a) < priority(b);
+  });
   for (auto [idx, sym] : llvm::enumerate(symbols))
     sym->value = idx * stubSize;
 }
diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index 95f039b893dd6..527de455a86a8 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -346,10 +346,9 @@ class ObjCStubsSection final : public SyntheticSection {
   static bool isObjCStubSymbol(Symbol *sym);
   static StringRef getMethname(Symbol *sym);
 
-  /// Stably sort the stubs by \p less and reassign their offsets. Must run
-  /// before addresses are assigned.
-  void
-  sortSymbols(llvm::function_ref<bool(const Defined *, const Defined *)> less);
+  /// Stably sort the stubs by \p priorities and reassign their offsets. Must
+  /// run before addresses are assigned.
+  void sortSymbols(const llvm::DenseMap<const Symbol *, int> &priorities);
 
 private:
   std::vector<Defined *> symbols;
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 1480a86e38015..9189d58ff02a5 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -33,8 +33,6 @@
 #include "llvm/Support/xxhash.h"
 
 #include <algorithm>
-#include <limits>
-
 using namespace llvm;
 using namespace llvm::MachO;
 using namespace llvm::sys;
@@ -1008,15 +1006,7 @@ static void orderObjCStubsByCallerPriority(
   if (stubPriority.empty())
     return;
 
-  auto priorityOf = [&](const Defined *sym) {
-    auto it = stubPriority.find(sym);
-    // Stubs with no prioritized caller sort last, keeping their original order.
-    return it == stubPriority.end() ? std::numeric_limits<int>::max()
-                                    : it->second;
-  };
-  in.objcStubs->sortSymbols([&](const Defined *a, const Defined *b) {
-    return priorityOf(a) < priorityOf(b);
-  });
+  in.objcStubs->sortSymbols(stubPriority);
 }
 
 // Sorting only can happen once all outputs have been collected. Here we sort
diff --git a/lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s b/lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s
index 75f721764eee1..249b14f48a1af 100644
--- a/lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s
+++ b/lld/test/MachO/objc-stubs-order-file-icf-safe-thunks.s
@@ -17,11 +17,8 @@
 
 # ORDERED:      Contents of (__TEXT,__objc_stubs) section
 # ORDERED-NEXT: _objc_msgSend$hot:
-# ORDERED:      Objc selector ref: hot
 # ORDERED:      _objc_msgSend$cold:
-# ORDERED:      Objc selector ref: cold
 # ORDERED:      _objc_msgSend$mild:
-# ORDERED:      Objc selector ref: mild
 
 #--- input.s
 .text
diff --git a/lld/test/MachO/objc-stubs-order-file.s b/lld/test/MachO/objc-stubs-order-file.s
index 0bfe32f1b034c..66fbaaaf4ea8a 100644
--- a/lld/test/MachO/objc-stubs-order-file.s
+++ b/lld/test/MachO/objc-stubs-order-file.s
@@ -14,7 +14,7 @@
 # RUN:   %t/base.out | FileCheck %s --check-prefix=BASE
 
 ## _hot is ordered first, so the stub it calls moves ahead of the others, which
-## keep their relative order. Each stub must still load its own selector.
+## keep their relative order.
 # RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/ordered.out %t/a.o \
 # RUN:   -objc_stubs_small -order_file %t/order
 # RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
@@ -44,11 +44,8 @@
 
 # ORDERED:      Contents of (__TEXT,__objc_stubs) section
 # ORDERED-NEXT: _objc_msgSend$hot:
-# ORDERED:      Objc selector ref: hot
 # ORDERED:      _objc_msgSend$cold:
-# ORDERED:      Objc selector ref: cold
 # ORDERED:      _objc_msgSend$mild:
-# ORDERED:      Objc selector ref: mild
 
 #--- a.s
 .text

>From 41ca49278246056acccee7f19e5139db5e4be0c1 Mon Sep 17 00:00:00 2001
From: Kyungwoo Lee <kyulee at meta.com>
Date: Fri, 28 Aug 2026 15:00:18 -0700
Subject: [PATCH 3/4] Address nocchijiang's comments

---
 lld/MachO/SyntheticSections.cpp | 19 +++++++++----------
 lld/MachO/SyntheticSections.h   |  2 ++
 lld/MachO/Writer.cpp            |  1 +
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 0633ce4f5820a..c4277d54edd38 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -899,15 +899,19 @@ StringRef ObjCStubsSection::getMethname(Symbol *sym) {
   return methname;
 }
 
+size_t ObjCStubsSection::getStubSize() const {
+  return config->objcStubsMode == ObjCStubsMode::fast
+             ? target->objcStubsFastSize
+             : target->objcStubsSmallSize;
+}
+
 void ObjCStubsSection::addEntry(Symbol *sym) {
   StringRef methname = getMethname(sym);
   // We create a selref entry for each unique methname.
   if (!ObjCSelRefsHelper::getSelRef(methname))
     ObjCSelRefsHelper::makeSelRef(methname);
 
-  auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
-                      ? target->objcStubsFastSize
-                      : target->objcStubsSmallSize;
+  size_t stubSize = getStubSize();
   Defined *newSym = replaceSymbol<Defined>(
       sym, sym->getName(), nullptr, isec,
       /*value=*/symbols.size() * stubSize,
@@ -940,17 +944,11 @@ void ObjCStubsSection::setUp() {
 }
 
 uint64_t ObjCStubsSection::getSize() const {
-  auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
-                      ? target->objcStubsFastSize
-                      : target->objcStubsSmallSize;
-  return stubSize * symbols.size();
+  return getStubSize() * symbols.size();
 }
 
 void ObjCStubsSection::sortSymbols(
     const llvm::DenseMap<const Symbol *, int> &priorities) {
-  auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
-                      ? target->objcStubsFastSize
-                      : target->objcStubsSmallSize;
   llvm::stable_sort(symbols, [&](const Defined *a, const Defined *b) {
     auto priority = [&](const Defined *sym) {
       auto it = priorities.find(sym);
@@ -959,6 +957,7 @@ void ObjCStubsSection::sortSymbols(
     };
     return priority(a) < priority(b);
   });
+  size_t stubSize = getStubSize();
   for (auto [idx, sym] : llvm::enumerate(symbols))
     sym->value = idx * stubSize;
 }
diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index 527de455a86a8..9acbd73d277e8 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -351,6 +351,8 @@ class ObjCStubsSection final : public SyntheticSection {
   void sortSymbols(const llvm::DenseMap<const Symbol *, int> &priorities);
 
 private:
+  size_t getStubSize() const;
+
   std::vector<Defined *> symbols;
   Symbol *objcMsgSend = nullptr;
 };
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 9189d58ff02a5..49415ccb70af8 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -33,6 +33,7 @@
 #include "llvm/Support/xxhash.h"
 
 #include <algorithm>
+
 using namespace llvm;
 using namespace llvm::MachO;
 using namespace llvm::sys;

>From 9c40f0254990e1a0f7d98613173de3b3f7042ce5 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 4/4] [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 | 24 ++++++++++++------------
 lld/MachO/SyntheticSections.h   |  5 +++--
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index c4277d54edd38..9aaaeb8ff0e89 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -889,17 +889,16 @@ 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 {
+size_t ObjCStubsSection::getStubSize(Symbol *) const {
   return config->objcStubsMode == ObjCStubsMode::fast
              ? target->objcStubsFastSize
              : target->objcStubsSmallSize;
@@ -911,15 +910,16 @@ void ObjCStubsSection::addEntry(Symbol *sym) {
   if (!ObjCSelRefsHelper::getSelRef(methname))
     ObjCSelRefsHelper::makeSelRef(methname);
 
-  size_t stubSize = getStubSize();
+  size_t stubSize = getStubSize(sym);
   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(sym);
+  }
 }
 
 void ObjCStubsSection::writeTo(uint8_t *buf) const {
diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index 9acbd73d277e8..a747cbc164b3a 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);
 
@@ -351,9 +351,10 @@ class ObjCStubsSection final : public SyntheticSection {
   void sortSymbols(const llvm::DenseMap<const Symbol *, int> &priorities);
 
 private:
-  size_t getStubSize() const;
+  size_t getStubSize(Symbol *) const;
 
   std::vector<Defined *> symbols;
+  size_t stubsSize = 0;
   Symbol *objcMsgSend = nullptr;
 };
 



More information about the llvm-commits mailing list